Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Clipping plane shader

Discussion in 'Shaders' started by Sarashinai, Aug 6, 2020.

  1. Sarashinai

    Sarashinai

    Joined:
    Jun 16, 2013
    Posts:
    20
    Hello!

    I've been trying to follow this clipping plane tutorial https://www.ronja-tutorials.com/2018/08/06/plane-clipping.html.

    I'm not getting the same result as shown in the tutorial (image below).



    Instead of the smooth gradation from one side to the other, I get every pixel the same color, making the faces uniform (screenshots below).


    Full size


    Full size

    Can someone tell me where I've gone wrong?

    ClippingPlane script as seen above.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClippingPlane : MonoBehaviour {
    6.     public Material ClippingMaterial;
    7.  
    8.     void Update(){
    9.         Plane plane = new Plane(transform.up, transform.position);
    10.  
    11.         Vector4 planeRepresentation = new Vector4(plane.normal.x, plane.normal.y, plane.normal.z, plane.distance);
    12.  
    13.         ClippingMaterial.SetVector("_ClippingPlaneRepresentation", planeRepresentation);      
    14.     }
    15. }
    Shader code applied to the ClippingPlane material.
    Code (CSharp):
    1. Shader "Custom/ClippingPlane"
    2. {
    3.     SubShader
    4.     {
    5.             Tags
    6.             {
    7.                 "RenderType" = "Opaque"
    8.                 "Queue" = "Geometry"
    9.             }
    10.  
    11.             CGPROGRAM
    12.             #include "UnityCG.cginc"
    13.  
    14.             #pragma target 3.0
    15.             #pragma surface surfaceShader Standard
    16.  
    17.             struct Input {
    18.                 float2 uv_MainTex;
    19.                 float3 worldPosition;
    20.             };
    21.  
    22.             float4 _ClippingPlaneRepresentation;
    23.            
    24.             void surfaceShader(Input i, inout SurfaceOutputStandard o){
    25.                 float distance = dot(i.worldPosition, _ClippingPlaneRepresentation.xyz);
    26.  
    27.                 o.Emission = distance;
    28.             }
    29.  
    30.             ENDCG
    31.       }
    32.    
    33.     Fallback "Standard"
    34. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That's not the correct name for that variable.
    https://docs.unity3d.com/Manual/SL-SurfaceShaders.html

    The automatically filled Input variables need to explicitly match the name shown in the tutorial / documentation. Otherwise they won't be filled with any useful data, just zeros (unless you explicitly set the data manually using a custom vertex function).

    TLDR; it really does need to be
    float3 worldPos;
    and not
    worldPosition
    .
     
    Sarashinai likes this.
  3. Sarashinai

    Sarashinai

    Joined:
    Jun 16, 2013
    Posts:
    20
    That was an exceedingly helpful reply and solved the issue immediately. Thank you!

    The tutorial gave me the impression that the variable names were arbitrary but the link you provided shows exactly what you wrote, that they have to match the variable names expected by Unity.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Technically, the variable names are arbitrary ... for custom variables. The ones that are auto-filled by Unity's Surface Shader system need to match exactly the format they expect.