LuxURP_GrassMeshPostprocessor.__cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // GRASSMESHPOSTPROCESSOR
  2. // Adds vertex color and stores bending in vertex color alpha according to the local position of the given vertex
  3. using System;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace LuxURPEssentials
  8. {
  9. internal class LuxURP_GrassMeshPostprocessor : AssetPostprocessor {
  10. public const string GMSuffix = "_LuxGM";
  11. public const string FMSuffix = "_LuxFM";
  12. public const string Power = "_POW";
  13. private bool isFoliageMesh = false;
  14. public void OnPostprocessModel(GameObject GrassMesh) {
  15. if (assetPath.Contains(GMSuffix) || assetPath.Contains(FMSuffix)) {
  16. string filename = Path.GetFileNameWithoutExtension(assetPath);
  17. Debug.Log("Processing Mesh: " + filename);
  18. if (assetPath.Contains(FMSuffix)) {
  19. isFoliageMesh = true;
  20. }
  21. int index;
  22. string filename_Remainder;
  23. if (isFoliageMesh) {
  24. index = filename.IndexOf(FMSuffix, 0);
  25. filename_Remainder = filename.Substring(index + FMSuffix.Length, 2);
  26. }
  27. else {
  28. index = filename.IndexOf(GMSuffix, 0);
  29. filename_Remainder = filename.Substring(index + GMSuffix.Length, 2);
  30. }
  31. float maxBending = 10.0f;
  32. try {
  33. maxBending = float.Parse(filename_Remainder);
  34. }
  35. catch {
  36. Debug.LogWarning("Max Bending: Invalid numerical Expression.");
  37. }
  38. maxBending *= 0.1f;
  39. maxBending = Mathf.Clamp(maxBending, 0.0f, 1.0f);
  40. float power = 10.0f;
  41. if (assetPath.Contains(Power)) {
  42. index = filename.IndexOf(Power, 0);
  43. filename_Remainder = filename.Substring(index+Power.Length, 2);
  44. try {
  45. power = float.Parse(filename_Remainder);
  46. }
  47. catch {
  48. Debug.LogWarning("Power: Invalid numerical Expression.");
  49. }
  50. }
  51. power *= 0.1f;
  52. Debug.Log("Max Bending: " + maxBending + " / Power: " + power);
  53. Mesh currentMesh = GrassMesh.GetComponent<MeshFilter>().sharedMesh;
  54. if (currentMesh.subMeshCount < 2) {
  55. Vector3[] vertices = currentMesh.vertices;
  56. Color[] colors = currentMesh.colors;
  57. // Create vertex color in case there are no
  58. if (colors.Length == 0) {
  59. colors = new Color[vertices.Length];
  60. for (int i = 0; i < vertices.Length; i++) {
  61. colors[i] = new Color(0.0f,0.0f,0.0f,0.0f);
  62. }
  63. }
  64. Bounds bounds = currentMesh.bounds;
  65. for (int i = 0; i < vertices.Length; i++) {
  66. if (vertices[i].y <= 0.0f) {
  67. if (isFoliageMesh) {
  68. colors[i].a = 0.0f;
  69. }
  70. else {
  71. colors[i].b = 0.0f;
  72. }
  73. }
  74. else {
  75. if (isFoliageMesh) {
  76. colors[i].a = Mathf.Lerp (0.0f, maxBending, Mathf.Pow(vertices[i].y, power)/bounds.size.y );
  77. //Debug.Log("doit" + colors[i].a);
  78. }
  79. else {
  80. colors[i].b = Mathf.Lerp (0.0f, maxBending, Mathf.Pow(vertices[i].y, power)/bounds.size.y );
  81. }
  82. }
  83. }
  84. // Update mesh
  85. currentMesh.colors = colors;
  86. }
  87. }
  88. }
  89. }
  90. }