- Home /
Rendering an Object only inside of a certain area
I'm working on a VR game where you are looking down on at a level with your units (think RTS view in VR basically), and I'm trying to get it so that only objects in a given area are rendered, and objects on the edge are partially rendered, very similar to what you see in this video of Airmech Command.
However, despite my best efforts I've yet to find a great way to do this. I've tried stencils, but have only managed to create something like a window, where things to the left or right of the area aren't shown at all, but even if an object is in front or behind the area they are still rendered. I've tried depthmasking, but had similar problems. The best I've managed is a setup where I use a depth mask to remove an area from being rendered at all on one camera, and filled the area with the rendering of another camera, but this resulted in just a flat image basically, so if you had objects with height, they would be cut off at a certain height.
Basically, everything I've found seems to just cull pixels on a 2d level, just based on screen position, as opposed to world space, and I need something that stops rendering based on 3D space. Any help given would be greatly appreciated
Answer by BenHolz · Nov 09, 2016 at 01:13 PM
you definetly need a stencil buffer. One object is the mask it has the shape of everything you want to cut and is placed directly in front of the camera. The rest of the objects are normal where the schould be.
the mask object need a shader, put this in the subshader part of the shader you create (assets->create->shader->Standard Surface Shader):
Tags { "RenderType"="Opaque" "Queue"="Geometry-1" }
ColorMask 0
ZWrite off
Stencil{
Ref 1
Comp always
Pass replace
}
and this in the shader that will be put on every object that should be hidden:
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" }
LOD 200
Stencil{
Ref 1
Comp notequal
Pass keep
}
I hope this helps
The problem with the stencil buffer, at least as I implemented it which looks very similar to the way you are suggesting, is it acts like a window. Anything behind the area is rendered, so you can neither stop an object from being rendered if it is behind the volume you want renderered (too far), or if it is in front of said volume (too close). I really need a solution that works in 3D space I think, the stencil stuff just checks if a pixel falls within or without the masking area on the screen
Your answer
Follow this Question
Related Questions
Does the projection matrix calculation affect the shader? 1 Answer
RTX Recursive Rendering HDRP Issue 0 Answers
How to render a Layer without the Volume Mask 0 Answers
Applying full screen image effects to UI only 2 Answers
(HELP) Volume Ray Marching rendered always on top of the other objects 2 Answers