0

In WebGL or in pure matrix math I would like to match the pixels in one view to another view. That is, imagine I take pixel with x,y = 0,0. This pixel lies on the surface of a 3d object in my world. I then orbit around the object slightly. Where does that pixel that was at 0,0 now lie in my new view?

How would I calculate a correspondence between each pixel in the first view with each pixel in the second view?

The goal of all this is to run a genetic algorithm to generate camouflage patterns that disrupt a shape from multiple directions.

So I want to know what the effect of adding a texture over the object would be from multiple angles. I want the pixel correspondencies because rendering all the time would be too slow.

Marcus
  • 97
  • 1
  • 2
  • 8

1 Answers1

1

To transform a point from world to screen coordinates, you multiply it by view and projection matrices. So if you have a pixel on the screen, you can multiply its coordinates (in range -1..1 for all three axes) by inverse transforms to find the corresponding point in world space, then multiply it by new view/projection matrices for the next frame.

The catch is that you need the correct depth (Z coordinate) if you want to find the movement of mesh points. For that, you can either do trace a ray through that pixel and find its intersection with your mesh the hard way, or you can simply read the contents of the Z-buffer by rendering it to texture first.

A similar technique is used for motion blur, where a velocity of each pixel is calculated in fragment shader. A detailed explanation can be found in GPU Gems 3 ch27.

I made a jsfiddle with this technique: http://jsfiddle.net/Rivvy/f9kpxeaw/126/

Here's the relevant fragment code:

// reconstruct normalized device coordinates
ivec2 coord = ivec2(gl_FragCoord.xy);
vec4 pos = vec4(v_Position, texelFetch(u_Depth, coord, 0).x * 2.0 - 1.0, 1.0);
// convert to previous frame
pos = u_ToPrevFrame * pos;
vec2 prevCoord = pos.xy / pos.w;
// calculate velocity
vec2 velocity = -(v_Position - prevCoord) / 8.0;
riv
  • 6,846
  • 2
  • 34
  • 63
  • This sounds right. Could you provide an example? Will definitely mark as answer then. Also I am unclear how I can use the Z-buffer, could you include that in the example? – Marcus Jul 04 '18 at 10:08