URP - Render only whats inside a cube

Hi everyone!
Time ago, I found an excellent solution for rendering only what is inside a cube, from this question:

The thing is, now I have updated my project to URP, and have a toon shader for most of my game objects.
Is there any option to “upgrade” the code from the clipbox shader, and to add it to my current toon shader?
The code from the clipbox is this one:

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

Thank you all in advance!! :slight_smile:

Hi, I wrote the answer you linked to. The important part is this snippet:

float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);

To make it work in a different shader, you just need to pass the right world position and have a global variable for the _WorldToBox matrix. Unfortunately, it’s a bit more annoying in Shader Graph. Here is what you’ll need to do:

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.

This is the graph I have:

Screenshot1 Screenshot2 ShaderGraph

Yes. You just have to take this code and combine it with the other code. The but that looks like its doing the work is the clip function calls and the _worldToBox float4x4.