Guest User

RenderTerrainMap.cs

a guest
Jun 12th, 2023
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [ExecuteInEditMode]
  6. public class RenderTerrainMap : MonoBehaviour
  7. {
  8.     public Camera camToDrawWith;
  9.     // layer to render
  10.     [SerializeField]
  11.     LayerMask layer;
  12.     // objects to render
  13.     [SerializeField]
  14.     Renderer[] renderers;
  15.     // unity terrain to render
  16.     [SerializeField]
  17.     Terrain[] terrains;
  18.     // map resolution
  19.     public int resolution = 512;
  20.  
  21.     // padding the total size
  22.     public float adjustScaling = 2.5f;
  23.     [SerializeField]
  24.     bool RealTimeDiffuse;
  25.     RenderTexture tempTex;
  26.  
  27.     private Bounds bounds;
  28.  
  29.     void GetBounds()
  30.     {
  31.         bounds = new Bounds(transform.position, Vector3.zero);
  32.         if (renderers.Length > 0)
  33.         {
  34.             foreach (Renderer renderer in renderers)
  35.             {
  36.                 bounds.Encapsulate(renderer.bounds);
  37.             }
  38.         }
  39.  
  40.         if (terrains.Length > 0)
  41.         {
  42.             foreach (Terrain terrain in terrains)
  43.             {
  44.                 Vector3 terrainCenter = terrain.GetPosition() + terrain.terrainData.bounds.center;
  45.                 Bounds worldBounds = new Bounds(terrainCenter, terrain.terrainData.bounds.size);
  46.                 bounds.Encapsulate(worldBounds);
  47.             }
  48.         }
  49.     }
  50.  
  51.     void OnEnable()
  52.     {
  53.         tempTex = new RenderTexture(resolution, resolution, 24);
  54.         GetBounds();
  55.         SetUpCam();
  56.         DrawDiffuseMap();
  57.     }
  58.  
  59.  
  60.     void Start()
  61.     {
  62.         GetBounds();
  63.         SetUpCam();
  64.         DrawDiffuseMap();
  65.     }
  66.  
  67.     void OnRenderObject()
  68.     {
  69.         if (!RealTimeDiffuse)
  70.         {
  71.             return;
  72.         }
  73.         UpdateTex();
  74.     }
  75.  
  76.     void UpdateTex()
  77.     {
  78.         camToDrawWith.enabled = true;
  79.         camToDrawWith.targetTexture = tempTex;
  80.         Shader.SetGlobalTexture("_TerrainDiffuse", tempTex);
  81.     }
  82.     public void DrawDiffuseMap()
  83.     {
  84.         DrawToMap("_TerrainDiffuse");
  85.     }
  86.  
  87.     void DrawToMap(string target)
  88.     {
  89.         camToDrawWith.enabled = true;
  90.         camToDrawWith.targetTexture = tempTex;
  91.         camToDrawWith.depthTextureMode = DepthTextureMode.Depth;
  92.         Shader.SetGlobalFloat("_OrthographicCamSizeTerrain", camToDrawWith.orthographicSize);
  93.         Shader.SetGlobalVector("_OrthographicCamPosTerrain", camToDrawWith.transform.position);
  94.         camToDrawWith.Render();
  95.         Shader.SetGlobalTexture(target, tempTex);
  96.         camToDrawWith.enabled = false;
  97.     }
  98.  
  99.     void SetUpCam()
  100.     {
  101.         if (camToDrawWith == null)
  102.         {
  103.             camToDrawWith = GetComponentInChildren<Camera>();
  104.         }
  105.         float size = bounds.size.magnitude;
  106.         camToDrawWith.cullingMask = layer;
  107.         camToDrawWith.orthographicSize = size / adjustScaling;
  108.         camToDrawWith.transform.parent = null;
  109.         camToDrawWith.transform.position = bounds.center + new Vector3(0, bounds.extents.y + 5f, 0);
  110.         camToDrawWith.transform.parent = gameObject.transform;
  111.     }
  112.  
  113. }
Add Comment
Please, Sign In to add comment