- Home /
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: https://answers.unity.com/questions/1762908/render-only-whats-inside-a-box.html 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!! :)
Answer by BastianUrbach · Dec 10, 2021 at 09:31 AM
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
Okay, I've test it and it works perfectly, thank you very much! I have a last question. The toon shader asset I purchased is written as a (archivename).shader, and has the following structure:
Shader ".../ToonShader"
{
Properties
{
[MainTexture]
_BaseMap ("Texture", 2D) = "white" {}
[MainColor]
_BaseColor ("Tint", Color) = (1.0, 1.0, 1.0)
_ShadowTint ("Shadow Tint", Color) = (0.0, 0.0, 0.0, 1.0)
.....
}
SubShader
{
Tags{"RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline" "IgnoreProjector" = "True"}
LOD 300
Pass
{
Name "..."
Tags{"LightMode" = "DepthOnly"}
.....
#include "./(...).hlsl"
#include "./(...).hlsl"
ENDHLSL
}
Pass
{
Name "..."
Tags{"LightMode" = "DepthOnly"}
.....
#include "./(...).hlsl"
#include "./(...).hlsl"
ENDHLSL
}
Pass
{
Name "..."
Tags{"LightMode" = "DepthOnly"}
.....
#include "./(...).hlsl"
#include "./(...).hlsl"
ENDHLSL
}
}
CustomEditor "..."
}
Is there any possibility to add the clipbox shader functionality to this shader, to make them work together? I've tried to translate the toonshader code to shadergraph, but it has been impossible to me, and also the inverse thing. Thank you very much in advance!
It's almost certainly possible but you'd have to do a bit of reverse engineering. I've never actually written a URP shader by hand before (only through Shader Graph) but there has to be a fragment shader somewhere. Don't be confused by the many #include "..."
lines, those literally just mean "insert the entire content of that other file here". Most of the relevant code is probably in the included files.
Maybe this helps: I added box clipping to the URP unlit example shader. It should work basically the same with your shader, just with a lot more other stuff.
Shader "Example/URPClipBox" {
SubShader {
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline" }
Pass {
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// declare matrix
float4x4 _WorldToBox;
struct Attributes {
float4 positionOS : POSITION;
};
struct Varyings {
float4 positionHCS : SV_POSITION;
// declare world space position
float3 positionWS : TEXCOORD0;
};
Varyings vert(Attributes IN) {
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// compute world space position
OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target {
// clip
float3 boxPosition = mul(_WorldToBox, float4(IN.positionWS, 1)).xyz;
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);
return half4(0.5, 0, 0, 1);
}
ENDHLSL
}
}
}
Answer by sacredgeometry · Dec 08, 2021 at 07:03 PM
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.