Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
2,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3.  
  4. [ExecuteInEditMode]
  5. public class TerrainBlendingBaker : MonoBehaviour
  6. {
  7.     //Shader that renders object based on distance to camera
  8.     public Shader depthShader;
  9.  
  10.     //The render texture which will store the depth of our terrain
  11.     public RenderTexture depthTexture;
  12.  
  13.     //The camera this script is attached to
  14.     private Camera cam;
  15.  
  16.     // The context menu tag allows us to run methods from the inspector (https://docs.unity3d.com/ScriptReference/ContextMenu.html)
  17.     [ContextMenu("Bake Depth Texture")]
  18.     public void BakeTerrainDepth()
  19.     {
  20.         //call our update camera method
  21.         UpdateBakingCamera();
  22.  
  23.         //Make sure the shader and texture are assigned in the inspector
  24.         if (depthShader != null && depthTexture != null)
  25.         {
  26.             //Set the camera replacment shader to the depth shader that we will assign in the inspector
  27.             cam.SetReplacementShader(depthShader, "RenderType");
  28.             //set the target render texture of the camera to the depth texture
  29.             cam.targetTexture = depthTexture;
  30.             //set the render texture we just created as a global shader texture variable
  31.             Shader.SetGlobalTexture("TB_DEPTH", depthTexture);
  32.         }
  33.         else
  34.         {
  35.             Debug.Log("You need to assign the depth shader and depth texture in the inspector");
  36.         }
  37.     }
  38.  
  39.     private void UpdateBakingCamera()
  40.     {
  41.         //if the camera hasn't been assigned then assign it
  42.         if (cam == null)
  43.         {
  44.             cam = GetComponent<Camera>();
  45.         }
  46.  
  47.         //the total width of the bounding box of our cameras view
  48.         Shader.SetGlobalFloat("TB_SCALE", GetComponent<Camera>().orthographicSize * 2);
  49.         //find the bottom corner of the texture in world scale by subtracting the size of the camera from its x and z position
  50.         Shader.SetGlobalFloat("TB_OFFSET_X", cam.transform.position.x - cam.orthographicSize);
  51.         Shader.SetGlobalFloat("TB_OFFSET_Z", cam.transform.position.z - cam.orthographicSize);
  52.         //we'll also need the relative y position of the camera, lets get this by subtracting the far clip plane from the camera y position
  53.         Shader.SetGlobalFloat("TB_OFFSET_Y", cam.transform.position.y - cam.farClipPlane);
  54.         //we'll also need the far clip plane itself to know the range of y values in the depth texture
  55.         Shader.SetGlobalFloat("TB_FARCLIP", cam.farClipPlane);
  56.  
  57.         //NOTE: some of the arithmatic here could be moved to the shader but keeping it here makes the shader cleaner so ¯\_(ツ)_/¯
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement