Advertisement
JontePonte

shitty snow main

Jan 1st, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Snow : MonoBehaviour
  6. {
  7. public int snowResolution;
  8. public float planeSize;
  9. public float snowHeight;
  10. public Transform player;
  11. public ComputeShader updateTexture;
  12. public Material snowMat;
  13.  
  14. RenderTexture displacementTex;
  15. GameObject snowPlane;
  16. Mesh snowMesh;
  17.  
  18. void Start()
  19. {
  20. InitRenderTexture();
  21. InitShader();
  22.  
  23. //if (snowMat.shader.name != "Snow") Debug.LogError("Material must have snow shader");
  24.  
  25. snowPlane = new GameObject();
  26. snowPlane.AddComponent<MeshRenderer>().material = snowMat;
  27. snowMesh = GeneratePlaneMesh(snowResolution, planeSize);
  28. snowPlane.AddComponent<MeshFilter>().mesh = snowMesh;
  29. }
  30.  
  31. void Update()
  32. {
  33. updateTexture.SetFloats("playerPos", new float[] { player.transform.position.x, player.transform.position.z });
  34. updateTexture.Dispatch(0, snowResolution / 8, snowResolution / 8, 1);
  35. snowMat.SetTexture("_Disp", displacementTex);
  36. snowMat.SetFloat("_DispAmount", -snowHeight - 0.001f);
  37. Vector3 lightDir = GameObject.Find("Directional Light").transform.rotation * Vector3.forward;
  38. snowMat.SetFloat("_LightDirX", lightDir.x);
  39. snowMat.SetFloat("_LightDirY", lightDir.y);
  40. snowMat.SetFloat("_LightDirZ", lightDir.z);
  41. snowPlane.transform.position = Vector3.up * snowHeight;
  42. snowMesh.RecalculateNormals();
  43. }
  44.  
  45. void InitShader()
  46. {
  47. updateTexture.SetFloat("maxPlayerPosX", planeSize / 2);
  48. updateTexture.SetFloat("minPlayerPosX", -(planeSize / 2));
  49. updateTexture.SetFloat("maxPlayerPosY", planeSize / 2);
  50. updateTexture.SetFloat("minPlayerPosY", -(planeSize / 2));
  51.  
  52. updateTexture.SetFloat("playerRadius", player.transform.localScale.x / 2);
  53.  
  54. updateTexture.SetInt("width", snowResolution);
  55. updateTexture.SetInt("height", snowResolution);
  56. }
  57. void InitRenderTexture()
  58. {
  59. displacementTex = new RenderTexture(snowResolution, snowResolution, 24);
  60. displacementTex.enableRandomWrite = true;
  61. displacementTex.Create();
  62.  
  63. updateTexture.SetTexture(0, "Result", displacementTex);
  64. }
  65.  
  66. Mesh GeneratePlaneMesh(int resolution, float size)
  67. {
  68.  
  69. Vector3[] vertices = new Vector3[resolution * resolution];
  70. Vector2[] uvs = new Vector2[vertices.Length];
  71. int[] indices = new int[(resolution - 1) * (resolution - 1) * 6];
  72.  
  73. float spacing = size / resolution;
  74.  
  75. for (int z = 0, i = 0; z < resolution; z++)
  76. {
  77. for (int x = 0; x < resolution; x++, i++)
  78. {
  79. vertices[i] = new Vector3(x * spacing - size / 2, 0, z * spacing - size / 2);
  80. uvs[i] = new Vector2((float)x / resolution, (float)z / resolution);
  81. }
  82. }
  83.  
  84. for (int z = 0, i = 0, j = 0; z < resolution - 1; z++)
  85. {
  86. for (int x = 0; x < resolution - 1; x++, i++, j += 6)
  87. {
  88. indices[j] = i;
  89. indices[j + 1] = i + (resolution - 1) + 1;
  90. indices[j + 2] = i + 1;
  91.  
  92. indices[j + 3] = i + 1;
  93. indices[j + 4] = i + (resolution - 1) + 1;
  94. indices[j + 5] = i + (resolution - 1) + 2;
  95. }
  96. i++;
  97. }
  98.  
  99. Mesh mesh = new Mesh()
  100. {
  101. vertices = vertices,
  102. triangles = indices,
  103. uv = uvs
  104. };
  105. return mesh;
  106. }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement