Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jordoe · Dec 08, 2021 at 06:58 PM · shadershadersunityeditorshader programming

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!! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jordoe · Dec 10, 2021 at 10:19 PM 0
Share

Thank you very much!! I'll give it a try!

avatar image jordoe · Dec 11, 2021 at 01:48 PM 0
Share

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!

avatar image BastianUrbach jordoe · Dec 11, 2021 at 05:26 PM 0
Share

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


avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Advanced Shader Help 0 Answers

Silhouette overlay shader 0 Answers

Displace and show normals in fragment shader 1 Answer

Custom UI Shader Stencil Problem [willing to pay for fix] 1 Answer

Shader: 1 Base Texture Overlaid by 1 Transparent Texture. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges