My vertex shader line w = position.x*position.x always gives me a constant value


Please first refer to

I was playing with shaders and In a ground mesh on the xz plane, I managed to paint it with a rainbow disk of radius 5.

Now, having each point in my ground coordinates (x,0,z), I want to move those points to a position (f(x,z),0,g(x,z)), lets say to the position (x + 10.0,0,z), and paint them according to its original coordinates (x,y,z), i.e. in this example I want to get the exact rainbow disk but painted 10 units to the right.

So I made this:

If I am understanding correctly, I only needed to:

  1. Make two copies of the attribute position vector, lets say
    in vec3 vPosition = position and
    vec3 v = position.

  2. Modify the coordinates of v, in this example I only need to change v.x, i.e
    v.x = v.x + 10.0;
    v.y = v.y;
    v.z = v.z;
    and make gl_Position = worldViewProjection * (v, 1.0);

  3. Pass the vPosition (which is equal to the unmodified position) as a varying vec3 to the fragment shader and use my previously working formulas that paint my original disk, as a function of vPosition.

This way I will have different positions for my vertexes (10 to the right), but the color will be given by their original position (the vec3 vPosition) … Right?

Well, doing so I got the desired result. The disk is painted 10 units to the right.

Playing with the lines
v.x = something
v.y = 0.0
v.z = somethin else

I get some nice paints. (commented in the playground)

Such as
v.x = v.x+v.z;
v.z = 3.0*v.z + v.x

The problem is that when I use anything besides a sum or a multiplication by a float, I get wrong pictures.
For example if
v.x = v.x*v.x;
v.y = 0.0
v.z = v.z;

Nothing is being drawn!

In order to have a clue of what is happening I did (also commented in playground)
v.x = v.x;
v.y = v.x*v.x;
v.z = v.z;

i.e. I made the graph of y seen as a function of x and z.
And I found that v.xv.x is always equal to 25. (putting v.y = v.xv.x - 25.0 gives the graph just on the xy plane). 25 is the maxium the value v.x*v.x can be, since the ground goes from (-5, -5) to (5, 5), but why it takes this value at every point on the ground?

I also put:

v.x = v.x;
v.y = v.x;
v.z = v.z;

Just to be extra sure that v.x is NOT constant. And it is not.

Yet v.x*v.x IS constant = 25. I am super puzzled. Please help :frowning:

What is happening? Why is v.x*.v.x always 25? How can I get the points
v.x = v.x*v.x
v.y = 0.0
v.z = v.z

be correctly painted?

My final goal is to paint some nice complex numbers functions, something like this

but in order to do it, first of all I need to be able to deal with the basic operations +, -, *, /.

Yes this would work !!! vertex to move around and Fragment to shade or paint :slight_smile:

This is expected as -5 * -5 would be 25 and you need to keep the sign to create a triangle.

Basically your vertices data needs to represent a triangle to be drawn but by squaring x, you end up with a point or rather a line which is not visible, so you simply need to keep the sign in

1 Like

Thank you, now I know why there was nothing on the screen when I made v.x = v.x*v.x.
Furthermore you made me realize I was not taking truly in consideration the vertexes of my ground mesh. Which is more evident when I then made

groundShaderMat.wireframe = true;

In fact, the picture in your playground was not what I thought it would be. It is stretched since my ground mesh had too few triangles. So in the inputs of the ground I put many more triangles and now I have exactly what I wanted, in the following I used a more interesting function (an exponential one) rather than a multiplication of the x coordinate: