Advertisement
Staggart

Stylized Grass - Runtime color map baking

Jun 22nd, 2023 (edited)
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace StylizedGrass
  5. {
  6.     //Testing runtime generation
  7.     [ExecuteInEditMode]
  8.     public class RuntimeColorMapTest : MonoBehaviour
  9.     {
  10.         public GrassColorMapRenderer colorMapRenderer;
  11.  
  12.         private void Reset()
  13.         {
  14.             colorMapRenderer = GetComponent<GrassColorMapRenderer>();
  15.         }
  16.        
  17.         public void OnGUI()
  18.         {
  19.             if (GUILayout.Button("Render"))
  20.             {
  21.                 if(colorMapRenderer.colorMap == null) colorMapRenderer.colorMap = ScriptableObject.CreateInstance<GrassColorMap>();
  22.                
  23.                 colorMapRenderer.terrainObjects.Clear();
  24.                 colorMapRenderer.AssignActiveTerrains();
  25.                 colorMapRenderer.RecalculateBounds();
  26.                
  27.                 //Use some randomization, in order to see a difference
  28.                 float r = UnityEngine.Random.Range(0.9f, 1.1f);
  29.                 colorMapRenderer.colorMap.bounds.size = Vector3.Scale(colorMapRenderer.colorMap.bounds.size, new Vector3(r, 1f, r));
  30.                 colorMapRenderer.textureDetail = UnityEngine.Random.value * 100f;
  31.                
  32.                 colorMapRenderer.Render();
  33.             }
  34.  
  35.             if (colorMapRenderer.colorMap && colorMapRenderer.colorMap.texture)
  36.             {
  37.                 GUI.DrawTexture(new Rect(5, 32, 256, 256), colorMapRenderer.colorMap.texture, ScaleMode.ScaleToFit, false);
  38.                 GUI.DrawTexture(new Rect(256 + 15, 32, 256, 256), colorMapRenderer.colorMap.texture, ScaleMode.ScaleToFit, true);
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement