- Home /
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.
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.
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;
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!
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.
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.
But that wouldn't mask them, that would just shut them off completely. So you could never see partial of the object.
I've considered this, but Naphier is correct you wouldn't be able to see partials of objects.
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.
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.
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.
Your answer
Follow this Question
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