using UnityEngine; public class NormalsReplacementShader : MonoBehaviour { [SerializeField] private Shader normalsShader = null; private RenderTexture renderTexture; private new Camera camera; private void Start() { Camera thisCamera = GetComponent(); // Create a render texture matching the main camera's current dimensions. renderTexture = new RenderTexture(thisCamera.pixelWidth, thisCamera.pixelHeight, 24); // Surface the render texture as a global variable, available to all shaders. Shader.SetGlobalTexture("_CameraNormalsTexture", renderTexture); // Setup a copy of the camera to render the scene using the normals shader. GameObject copy = new GameObject("Normals camera"); camera = copy.AddComponent(); camera.CopyFrom(thisCamera); camera.transform.SetParent(transform); camera.targetTexture = renderTexture; camera.SetReplacementShader(normalsShader, "RenderType"); camera.depth = thisCamera.depth - 1; } }