Manipulate Shader constant by script - Error "does not have any property"(SOLVED)

Hi !

I create a new shader and try to manipulate constants using a script.
Unfortunately, I got this error when I launch the project :
#sprite’ does not have any property called ‘mouse’

I must miss something but I can’t figure what.

Thanks for your help !

What I have done :

  1. copy files .fp, .vp, .material from materials/sprite in a new folder

  2. rename files

  3. open the new material and select new VP and FP files. Then add 3 new constants

  4. add simple FP code to test

varying mediump vec2 var_texcoord0;
uniform lowp sampler2D texture_sampler;
uniform lowp vec4 mouse;
void main()
{
vec2 mouse = vec2(mouse.x, mouse.y);
gl_FragColor = vec4(var_texcoord0.xy, 0.0, 1.0);
}

  1. created a new script

function init(self)
go.set(“#sprite”, “mouse”, vmath.vector4(150.0, 150.0, 0, 0))
end

  1. in the collection, add a new GameObject and attach a Sprite (id = sprite) and the Script
  2. change the Sprite Material’s property to attach the material file

You not use mouse in code. Compiler throw it away.

If you make something like this, is should work

gl_FragColor.x = mouse.x;
2 Likes

Thanks d954mas !

It was definitively something I didn’t know :slight_smile:

I am just starting writing shaders, so maybe my question is not revelant, but does this means that all constants must be used in the gl_FragColor ?

I try to make a flashlight effect, and I would like to use mouse coordinates only to check a distance.
Is there a solution to do this ?

varying mediump vec2 var_texcoord0;
uniform lowp sampler2D texture_sampler;

uniform lowp vec4 mouse;
uniform lowp vec4 radius;
uniform lowp vec4 light;

float Tween(float k, float t, float T, float S, float E)
{
float c = E - S;
return c * pow(t/T, k) + S;
}

void main()
{
vec4 color = vec4(0,0,0,0);
vec2 screen_coord = vec2(var_texcoord0.x, var_texcoord0.y);

lowp vec4 color_of_pixel = texture2D(texture_sampler, var_texcoord0.xy);

float dist = distance(vec2(mouse.x, mouse.y), screen_coord);

if (dist <= radius.x)
{
    float alpha = Tween(light.x, dist, radius.x, 1, 0);
    color = vec4(1,1,1,alpha);
}
else
{
    vec4 color = vec4(0,0,0,0);

}

gl_FragColor = color_of_pixel * color;

}

No:)
gl_FragColor must depend on that constant. You can use it on code, functions or some others way.
Your example should work)

2 Likes

Thank you d954mas, you save my day !