Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pieduke · Apr 17, 2020 at 09:27 AM · cameramask

Masking Camera rendering,Simulate reduced field of view with camera mask

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.

Any suggestions? thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Apr 17, 2020 at 05:00 PM

Effect

test scene effect


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
         }
     }
 }



arview.png (170.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AutumnM · Feb 04 at 07:43 PM 0
Share

Thank you for this detailed answer. It was incredibly helpful!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mario Camera Culling Mask (Secret Areas), 1 Answer

Camera Shape Masks? 2 Answers

Culling Mask with Depth Occlusion 1 Answer

Mask to Camera? 0 Answers

UI Mask doesnt work with Screen Space - Camera canvas? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges