Search Unity

Question Shader math to cull part of mesh

Discussion in 'Shader Graph' started by ProtagonistKun, Dec 3, 2019.

  1. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Hello,

    I am trying to refresh my math knowledge since I have not done my vectors for a while and have run into trouble trying to make this shader work.

    What I would like to do is create a material (shader) that culls part of a mesh (think of it as if you are pulling something out of a portal and it is slowly appearing on one side of it). A plane (any location, any orientation) can be placed over an object and it will clip one side of the mesh.

    However, as I have it now, nothing is happening... the shader gets applied to a cube and there is an intersecting plane, but everything is still being rendered. Does anyone have an idea as to where I am going wrong with this shader to achieve the effect I am looking for? I remember finding a similar shader (regular HLSL) but I wanted to try and make it with shadergraph, resulting in a few headaches... I assume my vector logic is wrong, but I can't seem to find the solution.

    The Shadergraph is as follows:
    Unity_2019-12-03_23-36-45.png
    It has a script attached to it like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. public class PortalPullProperties : MonoBehaviour
    5. {
    6.     [SerializeField] private GameObject _clippingPlane = null;
    7.     [SerializeField] private Vector3 _rotationOffset = new Vector3(0, 90, 0);
    8.  
    9.     private Material _material = null;
    10.     private MeshRenderer _renderer = null;
    11.  
    12.     public GameObject ClippingPlane { get => _clippingPlane; }
    13.  
    14.     private void Start()
    15.     {
    16.         _renderer = gameObject.GetComponent<MeshRenderer>();
    17.         _material = _renderer.sharedMaterial;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if((_clippingPlane && _clippingPlane.transform.hasChanged) || transform.hasChanged)
    23.         {
    24.             Vector3 position = _clippingPlane.transform.position;
    25.             Vector3 rotation = _clippingPlane.transform.forward + _rotationOffset;
    26.  
    27.             _material.SetVector("_ClippingPlanePosition", new Vector4(position.x, position.y, position.z));
    28.             _material.SetVector("_ClippingPlaneOrientation", new Vector4(rotation.x, rotation.y, rotation.z));
    29.         }
    30.     }
    31.  
    32.     private void Reset()
    33.     {
    34.         this.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Shader Graphs/PortalPull"));
    35.     }
    36. }
     
  2. teutonicus

    teutonicus

    Joined:
    Jul 4, 2012
    Posts:
    70
    Your math looks right, you should just need to plug your dot product result straight into the Master node's Alpha input. Alpha Clip Threshold would then be 0 to cull everything behind your plane.
     
  3. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Seems like that was not the only thing going wrong with my shader. The result looks like this. Which is still not doing anything sadly... I wonder what is going wrong, given that you are telling me my math is correct I am pretty much at a loss.
    upload_2019-12-4_8-21-1.png
    upload_2019-12-4_8-23-39.png
     
    Last edited: Dec 4, 2019