The fragment's world position on the scale

I am trying to get the world position in a fragment shader. I am currently using this in a vertex shader:

"varying vec3 vPosition;",

      

...

"vPosition = modelViewMatrix * position;",

      

EDIT

The vPosition variable is passed to the fragment shader. Basically I am trying to draw a grid with a red circle wherever the mouse is. The vPosition is then used in the fragment shader to determine if the mesh should be drawn.

This works great, but not when I scale the grid. Pls see the pictures below:

No scaling: enter image description here

With 2X scaling: enter image description here

I don't quite understand the whole pipeline. I get the gist of most of them, but I think I am trying to go wrong ...?

Edit This is what the code for drawing the grid looks like in the fragment shader:

// Draw the ring and grid on the terrain for when we are editing it.
"if (show_ring == true){",
   "float distance = sqrt((vPosition.x - ring_center.x) * (vPosition.x - ring_center.x) + (vPosition.z - ring_center.z) * (vPosition.z - ring_center.z));",
   "if (distance < ring_radius + ring_width / 2.0 && distance > ring_radius - ring_width / 2.0) {",
   "gl_FragColor.r += ring_color.r;",
   "gl_FragColor.b += ring_color.b;",
   "gl_FragColor.g += ring_color.g;",
   "gl_FragColor.a += ring_color.a;",
   "gl_FragColor *= 2.0;",
   "gl_FragColor = normalize(gl_FragColor);",                   
"}",

// Grid overlay
"float gridDist = ring_radius / 4.0 * 3.0;",
"if (distance < gridDist ) {",
   "float tiles = 1.0 / 100.0;",
   "float val = mod(vUv.y, tiles);",
   "if (mod(vUv.x, tiles) < .001 || mod(vUv.y, tiles) < .001) {",
   "gl_FragColor = gl_FragColor * (distance / gridDist);",
   "gl_FragColor.a = 1.0;",
"}",

      

Thanks for any help on this.

+3


source to share


1 answer


I managed to get this to work using "worldPosition":



"vec4 worldPosition = modelMatrix * vec4 (position, 1.0);",

+1


source







All Articles