Godot Version
4.6.1 mono version with C#
Question
Hiya. I’m using SurfaceTool to procedural generate meshes at runtime, and I would like to pass some custom data that is unique per vertex from my script generating the mesh into a spatial shader I am writing for that mesh’s material. I have a version of this working already that uses the built in support for per vertex coloring to encode the data using SurfaceTool.SetColor and spatial shader’s per vertex built-in COLOR, but it feels wasteful since I’m only using the R and G of the Color channel and don’t want to waste GPU memory on the B and A values I’m not using. I noticed SurfaceTool has support for passing custom data, so I’m trying to use;
surface.SetCustomFormat(0, SurfaceTool.CustomFormat.RgFloat);
But I can’t figure out how to access this data in my spatial shader. The online documentation claims there are vertex built-ins for CUSTOM0, CUSTOM1, CUSTOM2, and CUSTOM3, but I get compiler errors when attempting to access this in my shader code.
Unknown identifier in expression: ‘CUSTOM0’.
How do I go about this? I can’t seem to find any comprehensive documentation or tutorial on how custom per vertex data works.
It’s only accessible from the vertex() function. If you need it in fragment() send it via a varying.
Btw, you won’t “save” anything by using those compared to COLOR. They’re all vec4
1 Like
You’re absolutely right again. Thanks for clearing that up. Now that I’ve reread over the documentation more carefully I see it does differentiate between the vertex and fragment built-ins, and mentions the varying solution. My bad
I didn’t understand the significance of that yet.
About the 'not saving anything’, I follow what you’re saying about CUSTOM0 - 4 all being defined as vec4 on the shader side of things, but the SurfaceTool.SetCustomFormat CustomFormat enum includes values for modes such as CUSTOM_R_FLOAT and ARRAY_CUSTOM_RG_HALF. What is the point of including options like this for restricting the assigning of custom data to such limited constraints if ‘under the covers’ it just writes it all out to full sized vec4 buffers anyway? That seems kind of counter-intuitive, but maybe there is some other very good use case for this and I’m misunderstanding the purpose of it?
(Thanks again for all the help as I stumble my way through finally learning to write shaders
)
I wasn’t thinking straight. Yeah, you can save space using a more compact format.
1 Like