How do i transfer information to a shader (SOLVED)

i’m making a building system, and i want the color of the building selection to change according to placement validity (turn green if you can place it, turn red if you can’t)

If you are using sprites you can use the tint constant like in this example:

Or you can create your own constant in the material and use it the same as the sprite tint constant in the shader. If you open up the sprite fragment program you can see how its used in the shader.

2 Likes

so in my case, using a 3d model, i can assign to the model a custom material, set a fragment constant in said material, and use go.set('/go#model', 'name_here') to change that fragment constant?

won’t that affect every model with the same shader? if so, how could i change individual models with the same shader?

Yes you can use go.set("/go#model", "tint", vmath.vector4(1,0,0,1)) where "/go#model" is is the url to the model component, "tint" is the name of the fragment constant set in the material and vmath.vector4(1,0,0,1) is the color rgba in this case would be full opaque red.

You should test this out and dispel any suspicions. One thing to note is that when you use this type of constant there will be an additional draw call for each different tint applied. You can use profiling to see this type of information in your games. Profiling in Defold

2 Likes

I’m going to revive this PR as well, which adds functions to get/set data on a material level. So if your models share the same material resource and you want to set some global data for it then this would help:

3 Likes