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 AtomikTM · Oct 09, 2016 at 10:17 PM · camerashaderrendering

Is it possible to only render on one side of a plane?

I'm trying to create some sort of mask that will hide all objects / parts of a mesh, that are not behind a plane. This includes all objects/meshes in front of the plane as well.

alt text
Is this possible? I attempted to use the camera nearClipPlane for a similar effect, but found it was not accurate enough. I'm a beginner to Unity, I've been using it for only a few months. I have experience with C and C#, but no experience with OpenGL or Shaders. Yet. And I fear this may be a bit out of my range of experience.

example.png (81.8 kB)
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

4 Replies

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

Answer by Namey5 · Oct 29, 2016 at 05:17 AM

The way you would do what you are asking is to use stencil shaders to 'mask' other objects based on the plane's geometry. However, stencil shading is a fairly advanced topic, so it is moderately difficult to explain. From memory, Alan Zucconi did a good tutorial on stencil shaders;

http://www.alanzucconi.com/2015/12/09/3873/

Comment
Add comment · Show 2 · 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 AtomikTM · Oct 30, 2016 at 05:53 AM 0
Share

While not a direct answer to solve my exact problem, this is all of the information I need to get started working on solving it myself. This blog also has some other extremely useful information to a beginner. Thanks!

avatar image Namey5 AtomikTM · Oct 30, 2016 at 06:11 AM 0
Share

I must've blanked and forgot to include the other part of this. In your case, you can check whether an object is on either side of the plane by using a dot product between the normal of the plane and the direction of the plane from the object, i.e.

 using UnityEngine;
 using System.Collections;
 
 public class Object$$anonymous$$ask : $$anonymous$$onoBehaviour 
 {
 
     public Transform plane;    //The plane object
     
     // Update is called once per frame
     void Update () 
     {
         $$anonymous$$aterial mat = GetComponent<Renderer>().material;    //$$anonymous$$aterial attached to this object
         Vector3 dir = (plane.position - transform.position).normalized;    //The direction of the plane from this object
         
         mat.shader.SetInt ("_Stencil$$anonymous$$ask", Vector3.Dot (plane.forward, dir) > 0 ? 1 : 0);    //If the dot product of the plane's forward direction (this can be changed depending on the plane's actual forward direction) and the dir variable is greater than 0, i.e. if this object is in front of the plane, set the Stencil$$anonymous$$ask value of this object's shader to 1, else set it to 0
     }
 }

Just make sure that you set the plane's mask value to 1 in this scenario.

avatar image
1

Answer by b1gry4n · Oct 21, 2016 at 02:47 AM

You could create a square trigger around the camera that extends to the distance you want and disable/enable gameobjects as they enter/leave the trigger.

Comment
Add comment · Show 2 · 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 Naphier · Oct 21, 2016 at 08:03 PM 0
Share

But that wouldn't mask them, that would just shut them off completely. So you could never see partial of the object.

avatar image AtomikTM · Oct 29, 2016 at 05:02 AM 0
Share

I've considered this, but Naphier is correct you wouldn't be able to see partials of objects.

avatar image
0

Answer by Landerk · Oct 20, 2016 at 07:57 PM

Forgive me if I misread your question, but it sounds like you are trying to render only the plane, or to get it to render over everything else to "hide" the other objects? I'd say the easiest way to do that would be to put that plane on its own sorting layer. Then, you could have a camera set to a higher depth than the rest (so it will render over everything) and you could set the Culling Mask on that camera to only render the layer that plane is on. With code, you could either turn the plane renderer on and off as needed, or do the same with the camera. If you have anything you want to render with/over the plane, it might be easier to turn the camera on and off. To render something in front of the plane, make sure it is closer to the camera and the sorting order is lower than the plane, on the same layer.

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 AtomikTM · Oct 29, 2016 at 05:01 AM 0
Share

I'm trying to render only the meshes behind the plane, and not in-front of the plane. In the final product the plane would be invisible.

avatar image
0

Answer by Naphier · Oct 21, 2016 at 02:44 AM

You could add a second camera that renders to a texture, that texture would be on the plane. This camera would have to be assigned to only render the layers you want (i.e. not the plane, not the cubes, the skybox and any scenery). Then this plane is always showing the scene without the cubes. It'll take some fiddling to get the second camera's size correct, but this would be the easiest way to do it, I think.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

effect on clipping plane 1 Answer

Get distance to camera from CameraDepthTexture 1 Answer

Is that possible to create my own custom projector using unity camera 0 Answers

different material for multi camera 1 Answer

Camera with shader? 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