Shader affects wrong model

Heres my code, it produces two shapes, textured with some 2px x 2px texture. Ive aplied simple shader on this shapes, providing some INT flag, 1 - red color, 2 - blue color. According this shapes having different flags, given to shader, they should have different colors, but the last shader like overlaps all shapes textures. So if you comment 2nd shape, the first will have a true color, but if not ull get just one color for all shapes, why this happens and why shader affects not only shape area texture, but like all the screen. Test project attached.

Great,that works BUT we should define setShader before setShaderConstant ! Can you explain, why this happens ? Shader needs some time to precompile ? Because I found another behavior is strange, when I put shader definition in to some loop it gives me error.

Well shader compilation takes some time, but this is done synchronously, so it should work even in a loop. You may be limited by how many shaders the GPU support at the same time however.

About shader:setConstant() vs sprite:setShaderConstant(), the former sets a uniform value directly in the shader program, while the latter instruct gideros to set the uniform right before actually using the shader. The second form is more suitable to customize shader behavior sprite by sprite, while the first can be used to set global behavior for all sprites using the shader.

And yes, you should setShader() before setShaderConstant(), so that gideros can check the constant you gave vs what the shader expects. Removing or changing the shader for the sprite also clears any setShaderConstant() issued beforehand.

Yes, children sprites are technically enclosed in the drawing process of parent sprite, so all attributes set to the parent Sprite are somehow applied to children, such as colorTransform, clip, etc. The way it is done now, the same goes for Shader args, and even worse since shader constants aren't reset to previous value when Sprite drawing has ended, so whatever has been set is still valid for subsequent draws unless overriden.

Yes, children sprites are technically enclosed in the drawing process of parent sprite, so all attributes set to the parent Sprite are somehow applied to children, such as colorTransform, clip, etc. The way it is done now, the same goes for Shader args, and even worse since shader constants aren't reset to previous value when Sprite drawing has ended, so whatever has been set is still valid for subsequent draws unless overriden.

Well if you look at testcase, you should notice that shader applied to different sprites that are only siblings of parent stage, and they not inheited from themselves.

So if shaderConstant set to one of sprites this constant used for other shaders that not yet has setted constant.

Ive solved this by forcing constant value to each sprite after I do setShader to this sprite.

yes, true, I didn't realise they were siblings in your example, but you got it now: shader constant is in fact a variable of the shader program itself, so its value is retained as long as no setConstant/setShaderConstant is issued.