Search code examples
unity-game-engineshadermesh

Creating a clipping tool for a surface mesh


I am trying to create a shader/material that once contacted with a specific mesh, it will reveal the inside of it, similar to this: enter image description here

So far, I have done it's creating a shader that apply to the mesh itself, but I don't want that because I don't want to change the material of the mesh.

enter image description here

Any ideas on how can I make a material apply to a sphere/plane that cut one specific mesh?


Solution

  • I think I managed to solve it. ShaderGraph screenshot

    using UnityEngine;
    using System.Collections.Generic;
    
    public class MeshClipper : MonoBehaviour
    {
        public GameObject model3D;
        public Transform planeTransform;
        private Material cuttingMaterial;
    
        private void Awake() {
            MeshRenderer renderer = model3D.GetComponent<MeshRenderer>();
            cuttingMaterial = renderer.material;
        }
    
        void Update()
        {
            Vector3 planePosition = planeTransform.position;
            Vector3 planeNormal = planeTransform.up * -1;
    
            cuttingMaterial.SetVector("_PlanePosition", planePosition);
            cuttingMaterial.SetVector("_PlaneNormal", planeNormal);
        }
    }