- Home /
Render Only whats inside a box
Hi all
Has anyone had any succeess with shaders and only rendering what is inside a mesh (simply a box). I have been messing around with stencil shaders, but its not quite offering fully what i want as when the object is outside the box but the camera looks though the box you can still see the object. What i would be looking for is that this object to still not render (or the parts of the object that are out the box).
Thanks for any Help.
Answer by BastianUrbach · Aug 19, 2020 at 03:19 PM
An arbitrary mesh might be difficult but if it's just a box and you have no problem with using a special shader for the content of the box then it's relatively simple:
Put this script on the box:
using UnityEngine;
[ExecuteAlways]
public class ClipBox : MonoBehaviour {
void Update() {
Shader.SetGlobalMatrix("_WorldToBox", transform.worldToLocalMatrix);
}
}
And this shader on everything else:
Shader "Custom/ClipBox" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows addshadow
#pragma target 3.0
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
float4x4 _WorldToBox;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutputStandard o) {
float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
The magic happens in these three lines:
float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);
We calculate the position of the fragment relative to the box, which then makes it easy to cut away any fragments outside of it.
Answer by thomas4d · Sep 09, 2021 at 07:16 AM
how could you get this working with URP ?
In principle the same method can be used. The C# script is exactly the same but converting the shader to a ShaderGraph is a bit annoying. Image upload isn't working for some reason so I'll try to explain it the best I can:
Create a Matrix4x4 property _WorldToBox, enable Override Property Declaration in the node settings and set Shader Declaration to Global
Make a custom function node, set Type to String and add the following inputs and outputs:
Input DummyIn of any type you chose (explanation below)
Output DummyOut of any type you chose (explanation below)
Input Position of type Vector3
Set this as the body of the custom function node:float3 boxPosition = mul(_WorldToBox, float4(Position, 1)).xyz; clip(boxPosition + 0.5); clip(0.5 - boxPosition); DummyOut = DummyIn;
Connect the Position input to an Input/Geometry/Position node with Space set to World.
To make sure the node gets compiled into the fragment shader, you must also use the DummyIn and DummyOut connectors in some way. They simply pass a value through without any change. Connect DummyOut to something in the Fragment output block and pass whatever you actually want there to DummyIn. I know it's weird but I don't think there is a better workaround.
Thanks BastianUrbach but can you explain alittle more,. I got this Maybe I got messed up with the Dummy input out stuff ? and i'm New to shader graph
https://drive.google.com/file/d/1-Y_CtJD90wypn6hQS9nwQAeYweHMEfqH/view?usp=sharing
( yeah your right uploading images is broken at the moment on the forum )
OH got the error code to change but now it saying undeclared identifier"_WorldToBox"
https://drive.google.com/file/d/1y4ui5XSXRQSJJ7Sr8_7ufOUmlQBpxR1o/view?usp=sharing
Oh, weird. It seems you have to set a name for the CustomFunction node. I believe that is a bug in ShaderGraph. I don't see why it should make a difference. The source of the other error might be that Reference in the _WorldToBox property has the wrong value. Make sure that is set to _WorldToBox. Basically Name is just what you see in the graph while Reference is the internal name used in the code.
This is the graph I have:
Screenshot1 Screenshot2 ShaderGraph
Your answer

Follow this Question
Related Questions
Shader Problem 0 Answers
calculating 1 Answer
perform action on shader compilation/update 0 Answers
How to access Tilemap Tile's x-y-z positions from shader, to mask or move tiles 0 Answers
Shader - Render to Texture 0 Answers