I’m trying to create the experience that you’re wearing AR glasses in VR so that anything outside of the glasses is not rendered. This would include different UI elements and some gameobjects.
I’ve looked into UI Masks, Camera mask culling and other techniques but could find a way of masking a specific area that work across UI and 3D objects.
To be honest the shader code I’ve written might be wrong… but it works for me and it’s short so :shrug:
Setup
Make two cameras that see the same things:
Make two cameras, one called RealWorldCam, the other called ‘ARCam’.
Delete one of their audio sources (you can only have one in view, not really related but you’ll get warnings when running)
Make an empty game object called ‘Cameras’
Make both cameras children of ‘Cameras’ and make sure they are at the correct position
Setup layers for each to view
In the top right of the screen there is a drop down called ‘Layers’, click it and go to ‘Edit Layers…’
Add two new layers, one called ‘Mask’ and one called ‘AR’
Go to your ‘ARCam’ and click the dropdown for ‘Culling Mask’
Edit it to include Everything EXCEPT ‘Mask’ layer.
Make sure the depth is ‘-1’
Go to your ‘RealWorldCam’ and click the dropdown for ‘Culling Mask’
Edit it to include Everything EXCEPT ‘AR’ layer.
Change the depth to ‘0’
Make the mask
Add an object to the scene that is the shape you want your mask and call it ‘mask’
Position it in front of the cameras so that it covers the area you want to become AR (it doesn’t matter how near or far from the camera it is)
Make it a child of the ‘Cameras’ object (just so it stays at the correct relative position)
In the inspector, change its layer to ‘Mask’
Create a new shader in unity and copy in the code from below
Create a new material called ‘mask’ and make it use the shader
Make your ‘mask’ object use the new ‘mask’ material
Make real world objects
By default when you make a primitive it will be viewable in all areas of the screen, this is because it will not belong to the layer ‘AR’
Make some AR only objects
Add an object to your scene
In the inspector change the layer to ‘AR’
It should now only be viewable when it overlaps with your ‘mask’ object in the camera
Make some AR only UI
If you want to have UI outside of your AR you will need to canvases
Call your AR canvas ‘ARCanvas’
Click on the ARCanvas and in the inspector look under the header ‘Canvas’
Change the Render Mode to ‘Screen Space - Camera’
Change the camera to ‘ARCam’
If you want it to act like an overlay render camera then set the plane distance to ‘0.31’ (this number is chosen as it is the default near clipping plane value of the ARCam + 0.01)
Alternatively you can also use a world space UI, in this case you need to set the layer of the canvas and it’s children to ‘AR’
Not sure if it is possible to get overlay space to work, I somewhat doubt it, since overlay implies it will render afterwards regardless of setup.
Mask shader code
Shader "Custom/ARMask" {
SubShader{
Tags { "RenderType" = "Opaque" "Queue" = "Geometry-1" }
Pass {
Blend Zero One
ZWrite On
}
}
}