Log in or register to post comments

Using marker as occluder

November 15, 2012 - 9:27am #4

Hi, whilst pondering over how to implement some "photorealism" into Vuforia AR I came up with following simple shader idea. As basis there would be one marker in the middle of a paper plus a 3D object which is partly "below" the marker. In test implementation I'm about to try this would be a rotating cube which is positioned at (0, 0, 0). In this case you would want to occlude parts of the object that are under the marker.

To implement this I was thinking about following shaders.

uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
attribute vec3 aPosition;
varying vec4 vPosition;
void main() {
  vPosition = uModelViewMatrix * vec4(aPosition, 1.0);
  gl_Position = uProjectionMatrix * vPosition;
}

varying vec4 vPosition;
void main() {
  if (vPosition.z < 0.0) {
    discard;
  } else {
    // Render pixel with actual color.
  }
}

I still have to try this out but leaving the idea here for comments and future reference.

Using marker as occluder

November 16, 2012 - 3:27am #3

Hi harism, it's a very nice idea, in principle it should work;

however keep in mind one thing:

 in you fragment shader, you check the .Z component of your vertex position; this will provide the desired "clipping" effect ONLY if your object is centered on the marker itself; however, if you somehow add any custom translation to your model (e.g. to offset it a bit with respect to the marker reference frame), then it will not work anymore as expected (because the Z=0 plane will be shifted off from the marker plane);

If you then add custom rotations, that becomes even worse;

so, in that case, you could workaorund that by passing to the vertex shader an extra uniform parameter that just represents your local model transformation matrix with respect to the marker (i.e. the transformation that you add on top of the trackable Pose);

then you could use that to compute the vertex position "in the marker reference frame" and pass the transformed vector to the fragment shader.

 

Another approach to obtain occlusion effects (if you're interested) is the following:

- disable color writing (i.e. set the glColorMask(false, false, false, false);

- render a quad which matches the marker shape (i.e. just a quad with the same size and position/orientation of the marker); this will only be rendered into the depth buffer (because we disabled color buffer writing in the previous step)

- enable back the color mask (glColorMask(true, true, true, true); )

- render your 3D models

With that approach, occlusion clipping will occur automatically (no need to touch the shaders.)

 

 

Using marker as occluder

November 17, 2012 - 10:22am #2

Hi, thank you for your extensive feedback. I tried the idea and will post a video utilizing this into "Shadow mapping" thread. Anyway, I made a mistake earlier and clipping has to be done in model space, of course, easy to say now after testing, instead of view space. Here's the modified shaders;

uniform mat4 uModelM;
uniform mat4 uViewProjectionM;
attribute vec3 aPosition;
varying vec3 vPosition;
void main() {
  vec4 pos = uModelM * vec4(aPosition, 1.0);
  gl_Position = uViewProjectionM * pos;
  vPosition = pos.xyz / pos.w;
}

precision mediump float;
varying vec3 vPosition;
void main() {
  if (vPosition.z     discard;
  } else {
    // Choose actual color for rendering..
  }
}

Using marker as occluder

November 19, 2012 - 12:57am #1

Interesting work.

Log in or register to post comments