Log in or register to post comments

Depth masking in Unity

October 12, 2012 - 12:29am #1

Hello,

Thanks for such gr8 unity plugin for QCAR. I am successfully able to load Cube Game Object over my imagetarget when it comes in front of camera. Now I want to do one thing i.e. to show load Cube inside Mask. I mean to say I want to draw a 2d rect overlay in front of camera and show resultant Cube inside only that rectangle and rest of the screen around rect should be masked and should show camera view, I heard that is possible with depth masking. Can  anybody tell me how to do that?

I hope question is clear.

Thanks

Ayaz Alavi

Depth masking in Unity

April 4, 2013 - 6:25pm #18

You should be able to adapt the same technique for the transparency effect by using the RenderOcclusionLayer shader and BoxSetUpShader.cs on the outer wall meshes. Take a look at how these are used on the BlenderCube in the sample. It's not a feature of the target type, it's just a technique for supporting transparency on meshes.

Depth masking in Unity

April 4, 2013 - 5:14pm #17

The other question seems limited to multitargets peering from the outside of a room-in. Is there a way to make this work from a single image target? 

Depth masking in Unity

April 4, 2013 - 8:43am #16

Please see my response to your other post on this question.

Depth masking in Unity

April 3, 2013 - 3:17pm #15

It seems that this ends up occluding the planes completely? 

How would this work for a setup where you want an inwards-room, inside walls to show, outside walls to not show? 

Depth masking in Unity

October 15, 2012 - 4:07am #14

Yes my planes are direct children of ARCamera now.

Depth masking in Unity

October 15, 2012 - 2:10am #13

I resolved it by making my ARCamera look at ImageTarget prefab Object from above

I think that's what I had in mind.  Using Image Targets as an example the ARCamera is above pointing down to the Image Targets

adjust my planes to block the viewport 

yes that's what I did in my test, though I found I had to experiment with the depth value quite a bit

and these planes have DepthMask applied over them.

yes me too

BTW did you move your planes to now be parented by the ARCamera? this is what I had

N

 

 

Depth masking in Unity

October 14, 2012 - 11:08pm #12

I resolved it by making my ARCamera look at ImageTarget prefab Object from above and then adjust my planes to block the viewport and these planes have DepthMask applied over them.

Depth masking in Unity

October 14, 2012 - 10:52pm #11

I added 4 planes and made a rectangular empty space in the middle. It is working if these planes are NOT the child of ARCamera then there is small fixed rectangle over the image target. But if i move these planes into ARCamera as a child then there is no more rectangluar space instead there is bar in the middle of ARCameraview and my masked image which should only inside the rectangle is over it and below it. Please see this image https://www.dropbox.com/s/t2pkwwc2luyw8sy/IMG_0270.PNG.

I also tried removing GUITexture component from my planes which was attached initially and now imae target appears as it is without any rectangle or slice as above image. Here is how my unity editor looks like https://www.dropbox.com/s/rpl87j2qpjrhfuo/Screen%20Shot%202012-10-15%20at%2010.50.08%20AM.png

 

Depth masking in Unity

October 12, 2012 - 8:15pm #10

+1 on Nalin's advice. That will be the easiest way to accomplish what you're looking for.

Unity doesn't provide a built-in mesh editor. There are some extensions for mesh editing available through the asset store, but they're not very sophisticated. If you want to design a custom mesh that you can apply a depth mask to, your best bet is to do this in another 3D program and import it to your Unity project.

But if you're simply trying to present a rectangular viewport then you can compose this using planes, as Nalin's described.

Depth masking in Unity

October 12, 2012 - 7:27am #9

Hi Ayaz

I am not sure which questions to answer as you have been making rapid progress :)

..though I have been trying this out, so let's see how far we can get..

Also how would I create 3d Mesh in Unity with open center? Is this possuble to create such mesh in unity?

  • Just create some simple plane objects - four to cover top, bottom,left and right leaving a square/rectangle with a hold in the middle.
  • Make these children of the ARCamera and apply the DepthMask material in QCAR materials to these.  Note if you do not want the video outside of the rectangle/square and you want your own frame instead then simply use your own material here.
  • Also, ensure that they are as close to the camera as you can get so the Z value is useable with respect to the expected distance of the Image Target, otherwise it will never mask.
  • Another thing to check is that the ARCamera is set to clear Depth Only
 

how will I remove rectangular center of Plane Mesh

not sure what you mean

N

Depth masking in Unity

October 12, 2012 - 7:09am #8

I am able to get the shader to clip part of the Plane Mesh row by row. How can I modify it to clip center of the plane in rectangular shape.

 

Shader "Example/Slices" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _BumpMap ("Bumpmap", 2D) = "bump" {}
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      Cull Off
      CGPROGRAM
      #pragma surface surf Lambert
      struct Input {
          float2 uv_MainTex;
          float2 uv_BumpMap;
          float3 worldPos;
      };
      sampler2D _MainTex;
      sampler2D _BumpMap;
      void surf (Input IN, inout SurfaceOutput o) {
          clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

Depth masking in Unity

October 12, 2012 - 6:41am #7

Now I am unable to understand that how will I remove rectangular center of Plane Mesh. I think I can do this with shader. Any idea how to do that?

But this is again 3d, I want to use 2D object as a MASK OR an Object that is attached to certain coordinates od ARCamera and move along with camera.

Depth masking in Unity

October 12, 2012 - 5:59am #6

Also how would I create 3d Mesh in Unity with open center? Is this possuble to create such mesh in unity?

Depth masking in Unity

October 12, 2012 - 5:30am #5

I looked at this thread and this is how I am able to apply mask but I want to know 1) region outside the rectangle shuld be masked, currently region inside the rectangle is masked, How would I do that?

2) I want to use this mask on 2D object so how will I add this material to 2d Object which contains DepthMask shader?

 

Thanks

Depth masking in Unity

October 12, 2012 - 5:09am #4

Hi Ayaz,

I just wanted to check you had seen this thread:

https://ar.qualcomm.at/content/mask-ar-camera-using-custom-alpha-shape

are you wanting to do do something more than this?

N

Depth masking in Unity

October 12, 2012 - 5:02am #3

I am able to apply Depth Mask to Plane Mesh filter but I see the result is in 3d. I want to apply mask so that only 2d rectangle in the middle of screen be able to show result on image targets. Also plane mesh filter is hiding the 3d Cube Game OBject inside and showing camera view instead inside a rectangle. I want opposite of that i.e. inner rectangle to show 3d Game Object and rest of the Game Object outside the rectangle to be masked. How would I do that?

Depth masking in Unity

October 12, 2012 - 3:23am #2

For the below problem I create a material with following Depth Mask as a Shader

Shader "Depth Mask" {
    SubShader {
        Tags {"Queue" = "Geometry-10" }
        Lighting Off
        ZTest LEqual
        ZWrite On
        ColorMask 0
        Pass {}
    }
}

Then I applied this material to a Mesh Renderer componenet of a Plane type of Mesh filter. I also changed clear flags to Depth Only for ARCamera.

Then I tried adding this Mesh as a child of ARCamera and also child of ImageTarget prefabs but still resultant Cube Game Object is not masked. Any idea how to proceed forward.

Thanks

Log in or register to post comments