Advertisement
Staggart

Vertex color baker for Stylized Water 1

Nov 30th, 2021 (edited)
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace StylizedWaterShader
  5. {
  6.     [RequireComponent(typeof(MeshFilter))]
  7.     [ExecuteInEditMode]
  8.     public class VertexColorBaker : MonoBehaviour
  9.     {
  10.         public Mesh sourceMesh;
  11.  
  12.         [Min(0.01f)]
  13.         public float intersectionLength = 1f;
  14.         [Min(0.01f)]
  15.         public float depthExponent = 1f;
  16.        
  17.         private MeshFilter mf;
  18.         private Mesh newMesh;
  19.  
  20.         private void Reset()
  21.         {
  22.             mf = GetComponent<MeshFilter>();
  23.            
  24.             if (mf)
  25.             {
  26.                 sourceMesh = mf.sharedMesh;
  27.             }
  28.         }
  29.  
  30.         private void Start()
  31.         {
  32.             //Procedural meshes cannot be saved to prefabs. If it is missing, regenerate
  33.             if(!newMesh) Bake();
  34.         }
  35.  
  36.         private void OnValidate()
  37.         {
  38.             depthExponent = Mathf.Max(depthExponent, 0.01f);
  39.             intersectionLength = Mathf.Max(intersectionLength, 0.01f);
  40.            
  41.             Bake();
  42.         }
  43.  
  44.         [ContextMenu("BAKE")]
  45.         void Bake()
  46.         {
  47.             if (!sourceMesh) return;
  48.            
  49.             newMesh = new Mesh();
  50.             newMesh.name = "VertexColorMesh";
  51.            
  52.             Vector3[] verts = sourceMesh.vertices;
  53.             Color[] colors = new Color[verts.Length];
  54.  
  55.             RaycastHit hit;
  56.             float dist = 0;
  57.             bool hasHitBelow = false;
  58.             bool hasHitAbove = false;
  59.            
  60.             //Go over every vertex position, and raycast down to find the distance to the terrain
  61.             for (int i = 0; i < verts.Length; i++)
  62.             {
  63.                 Vector3 position = transform.TransformPoint(verts[i]);
  64.                 Vector3 rayOrigin = position;
  65.  
  66.                 //Default value for missed hits
  67.                 dist = 0;
  68.                 hasHitAbove = false;
  69.                 hasHitBelow = false;
  70.                
  71.                 rayOrigin.y += 100f;
  72.                 if ((Physics.Raycast(rayOrigin, Vector3.down, out hit, Mathf.Infinity, -1, QueryTriggerInteraction.Ignore)))
  73.                 {
  74.                     //Only accept hits from terrain colliders
  75.                     if (hit.collider.GetType() == typeof(TerrainCollider))
  76.                     {
  77.                         hasHitBelow = hit.point.y < position.y;
  78.                         hasHitAbove = hit.point.y >= position.y;
  79.                        
  80.                         dist = hit.point.y - position.y;
  81.                     }
  82.                 }
  83.  
  84.                 //Normalized depth values
  85.                 float intersection = 1 - Mathf.Abs(dist / intersectionLength);
  86.                 float depthVal = 1 - Mathf.Clamp01((-dist) / depthExponent);
  87.  
  88.                 intersection = hasHitBelow ? intersection : 0f;
  89.                 intersection = hasHitAbove ? 1f : intersection;
  90.  
  91.                 depthVal = hasHitBelow ? depthVal : 0f;
  92.                 depthVal = hasHitAbove ? 1f : depthVal;
  93.                
  94.                 colors[i] = new Color(intersection, depthVal, colors[i].b, colors[i].a);
  95.             }
  96.  
  97.             newMesh.vertices = verts;
  98.             newMesh.triangles = sourceMesh.triangles;
  99.             newMesh.normals = sourceMesh.normals;
  100.             newMesh.uv = sourceMesh.uv;
  101.             newMesh.colors = colors;
  102.            
  103.             newMesh.RecalculateBounds();
  104.             newMesh.RecalculateTangents();
  105.  
  106.             mf.sharedMesh = newMesh;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement