Search Unity

Question Clipping beyond a plane still allows light to pass through as if the clipped object isnt there.

Discussion in 'General Graphics' started by cinderousrex, Jul 23, 2022.

  1. cinderousrex

    cinderousrex

    Joined:
    May 21, 2019
    Posts:
    43
    Hello, I am clipping certain areas beyond a plane, which works wonderfully after I turned off Fallback.

    Which was necessary because shadows produced by itself were causing my clip to look strange.

    What I am having trouble with is lets say I take off half a room with this, I don't want to let in the light sources from outside, but this is exactly what happens.

    Is there anyway to tell a shader to pretend as if its actually there still for lights? Is there something I can research from a manual or example to add to my shader? I just want the object to still be there and effect lights, but the clip seems to make it so that portion just completely voids out and lights render through it. I am very sad.

    Code (CSharp):
    1. Shader "Tutorial/021_Clipping_Plane"{
    2.     //show values to edit in inspector
    3.     Properties{
    4.         _Color("Tint", Color) = (0, 0, 0, 1)
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _Smoothness("Smoothness", Range(0, 1)) = 0
    7.         _Metallic("Metalness", Range(0, 1)) = 0
    8.         [HDR]_Emission("Emission", color) = (0,0,0)
    9.  
    10.         [HDR]_CutoffColor("Cutoff Color", Color) = (1,0,0,0)
    11.     }
    12.  
    13.         SubShader{
    14.             //the material is completely non-transparent and is rendered at the same time as the other opaque geometry
    15.             Tags{ "RenderType" = "Opaque" "Queue" = "Geometry"}
    16.  
    17.             // render faces regardless if they point towards the camera or away from it
    18.             Cull Off
    19.  
    20.             CGPROGRAM
    21.             //the shader is a surface shader, meaning that it will be extended by unity in the background
    22.             //to have fancy lighting and other features
    23.             //our surface shader function is called surf and we use our custom lighting model
    24.             //fullforwardshadows makes sure unity adds the shadow passes the shader might need
    25.             //vertex:vert makes the shader use vert as a vertex shader function
    26.             #pragma surface surf Standard fullforwardshadows
    27.             #pragma target 3.0
    28.  
    29.             sampler2D _MainTex;
    30.             fixed4 _Color;
    31.  
    32.             half _Smoothness;
    33.             half _Metallic;
    34.             half3 _Emission;
    35.  
    36.             float4 _Plane;
    37.  
    38.             float4 _CutoffColor;
    39.  
    40.             //input struct which is automatically filled by unity
    41.             struct Input {
    42.                 float2 uv_MainTex;
    43.                 float3 worldPos;
    44.                 float facing : VFACE;
    45.             };
    46.  
    47.             //the surface shader function which sets parameters the lighting function then uses
    48.             void surf(Input i, inout SurfaceOutputStandard o) {
    49.                 //calculate signed distance to plane
    50.                 float distance = dot(i.worldPos, _Plane.xyz);
    51.                 distance = distance + _Plane.w;
    52.                 //discard surface above plane
    53.                 clip(-distance);
    54.  
    55.                 float facing = i.facing * 0.5 + 0.5;
    56.  
    57.                 //normal color stuff
    58.                 fixed4 col = tex2D(_MainTex, i.uv_MainTex);
    59.                 col *= _Color;
    60.                 o.Albedo = col.rgb * facing;
    61.                 o.Metallic = _Metallic * facing;
    62.                 o.Smoothness = _Smoothness * facing;
    63.                 o.Emission = lerp(_CutoffColor, _Emission, facing);
    64.             }
    65.             ENDCG
    66.         }
    67.             FallBack Off //fallback adds a shadow pass so we get shadows on other objects
    68. }