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
1
Question by Reddevildragg · Aug 18, 2020 at 01:18 PM · shadershader programming

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.

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
1
Best Answer

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.

Comment
Add comment · Show 1 · 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 Reddevildragg · Aug 21, 2020 at 04:21 PM 0
Share

This is brilliant, thanks very much :)

avatar image
0

Answer by thomas4d · Sep 09, 2021 at 07:16 AM

how could you get this working with URP ?

Comment
Add comment · Show 7 · 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 BastianUrbach · Sep 09, 2021 at 08:26 AM 1
Share

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.
avatar image thomas4d BastianUrbach · Sep 09, 2021 at 10:59 PM 0
Share

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

google drive

( 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

avatar image BastianUrbach thomas4d · Sep 10, 2021 at 05:45 AM 2
Share

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

Show more comments

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

206 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

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


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