Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Snow : MonoBehaviour
- {
- public int snowResolution;
- public float planeSize;
- public float snowHeight;
- public Transform player;
- public ComputeShader updateTexture;
- public Material snowMat;
- RenderTexture displacementTex;
- GameObject snowPlane;
- Mesh snowMesh;
- void Start()
- {
- InitRenderTexture();
- InitShader();
- //if (snowMat.shader.name != "Snow") Debug.LogError("Material must have snow shader");
- snowPlane = new GameObject();
- snowPlane.AddComponent<MeshRenderer>().material = snowMat;
- snowMesh = GeneratePlaneMesh(snowResolution, planeSize);
- snowPlane.AddComponent<MeshFilter>().mesh = snowMesh;
- }
- void Update()
- {
- updateTexture.SetFloats("playerPos", new float[] { player.transform.position.x, player.transform.position.z });
- updateTexture.Dispatch(0, snowResolution / 8, snowResolution / 8, 1);
- snowMat.SetTexture("_Disp", displacementTex);
- snowMat.SetFloat("_DispAmount", -snowHeight - 0.001f);
- Vector3 lightDir = GameObject.Find("Directional Light").transform.rotation * Vector3.forward;
- snowMat.SetFloat("_LightDirX", lightDir.x);
- snowMat.SetFloat("_LightDirY", lightDir.y);
- snowMat.SetFloat("_LightDirZ", lightDir.z);
- snowPlane.transform.position = Vector3.up * snowHeight;
- snowMesh.RecalculateNormals();
- }
- void InitShader()
- {
- updateTexture.SetFloat("maxPlayerPosX", planeSize / 2);
- updateTexture.SetFloat("minPlayerPosX", -(planeSize / 2));
- updateTexture.SetFloat("maxPlayerPosY", planeSize / 2);
- updateTexture.SetFloat("minPlayerPosY", -(planeSize / 2));
- updateTexture.SetFloat("playerRadius", player.transform.localScale.x / 2);
- updateTexture.SetInt("width", snowResolution);
- updateTexture.SetInt("height", snowResolution);
- }
- void InitRenderTexture()
- {
- displacementTex = new RenderTexture(snowResolution, snowResolution, 24);
- displacementTex.enableRandomWrite = true;
- displacementTex.Create();
- updateTexture.SetTexture(0, "Result", displacementTex);
- }
- Mesh GeneratePlaneMesh(int resolution, float size)
- {
- Vector3[] vertices = new Vector3[resolution * resolution];
- Vector2[] uvs = new Vector2[vertices.Length];
- int[] indices = new int[(resolution - 1) * (resolution - 1) * 6];
- float spacing = size / resolution;
- for (int z = 0, i = 0; z < resolution; z++)
- {
- for (int x = 0; x < resolution; x++, i++)
- {
- vertices[i] = new Vector3(x * spacing - size / 2, 0, z * spacing - size / 2);
- uvs[i] = new Vector2((float)x / resolution, (float)z / resolution);
- }
- }
- for (int z = 0, i = 0, j = 0; z < resolution - 1; z++)
- {
- for (int x = 0; x < resolution - 1; x++, i++, j += 6)
- {
- indices[j] = i;
- indices[j + 1] = i + (resolution - 1) + 1;
- indices[j + 2] = i + 1;
- indices[j + 3] = i + 1;
- indices[j + 4] = i + (resolution - 1) + 1;
- indices[j + 5] = i + (resolution - 1) + 2;
- }
- i++;
- }
- Mesh mesh = new Mesh()
- {
- vertices = vertices,
- triangles = indices,
- uv = uvs
- };
- return mesh;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement