Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- public class Snow : MonoBehaviour
- {
- [Header("Appearance")]
- public float planeSize;
- public float snowHeight;
- public float snowSharpness;
- [Header("Mesh")]
- public float threshold;
- public float errorThreshold;
- public int maxResolution;
- public int minResolution;
- public bool rebuild;
- public bool drawVertices;
- [Header("")]
- public Transform player;
- public Material snowMat;
- public ComputeShader updateTexture;
- public ComputeShader buildMesh;
- RenderTexture displacementMap;
- GameObject snowPlane;
- Mesh snowMesh;
- MeshBuilder meshBuilder;
- void Start()
- {
- InitRenderTexture();
- InitShader();
- InitSnowPlane();
- }
- void Update()
- {
- updateTexture.SetFloats("playerPos", new float[] { player.position.x, player.position.z });
- updateTexture.Dispatch(0, minResolution / 8, minResolution / 8, 1);
- snowMat.SetTexture("_Disp", displacementMap);
- snowMesh = GeneratePlaneMesh2();
- snowPlane.GetComponent<MeshFilter>().mesh = snowMesh;
- 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("dstSharpness", snowSharpness);
- updateTexture.SetFloat("threshold", threshold);
- updateTexture.SetFloat("playerRadius", player.transform.localScale.x / 2);
- updateTexture.SetInt("width", minResolution);
- updateTexture.SetInt("height", minResolution);
- }
- void InitRenderTexture()
- {
- displacementMap = new RenderTexture(minResolution, minResolution, 24);
- displacementMap.enableRandomWrite = true;
- displacementMap.Create();
- updateTexture.SetTexture(0, "Result", displacementMap);
- }
- void InitSnowPlane()
- {
- snowPlane = new GameObject("Snow Plane");
- snowPlane.AddComponent<MeshRenderer>().material = snowMat;
- snowPlane.transform.position = Vector3.up * snowHeight;
- snowMesh = GeneratePlaneMesh();
- snowPlane.AddComponent<MeshFilter>().mesh = snowMesh;
- var collider = GameObject.CreatePrimitive(PrimitiveType.Plane);
- collider.GetComponent<MeshRenderer>().enabled = false;
- collider.transform.localScale = Vector3.one * planeSize / 10;
- collider.transform.parent = snowPlane.transform;
- snowMat.SetFloat("_DispAmount", -snowHeight);
- }
- Mesh GeneratePlaneMesh()
- {
- if (meshBuilder == null)
- {
- meshBuilder = new MeshBuilder(planeSize, planeSize / minResolution, planeSize / maxResolution, errorThreshold);
- }
- meshBuilder.Rebuild(ToTexture2D(displacementMap));
- Vector2[] uvs = new Vector2[minResolution * minResolution];
- for (int z = 0, i = 0; z < minResolution; z++)
- {
- for (int x = 0; x < minResolution; x++, i++)
- {
- uvs[i] = new Vector2((float)x / minResolution, (float)z / minResolution);
- }
- }
- return new Mesh()
- {
- indexFormat = UnityEngine.Rendering.IndexFormat.UInt32,
- vertices = meshBuilder.vertices.ToArray(),
- triangles = meshBuilder.indices.ToArray(),
- uv = meshBuilder.uvs.ToArray()
- };
- }
- Mesh GeneratePlaneMesh2()
- {
- Vector3[] vertices = new Vector3[minResolution * minResolution];
- Vector2[] uvs = new Vector2[vertices.Length];
- int[] indices = new int[(minResolution - 1) * (minResolution - 1) * 6];
- float spacing = planeSize / minResolution;
- for (int z = 0, i = 0; z < minResolution; z++)
- {
- for (int x = 0; x < minResolution; x++, i++)
- {
- vertices[i] = new Vector3(x * spacing - planeSize / 2, 0, z * spacing - planeSize / 2);
- uvs[i] = new Vector2((float)x / minResolution, (float)z / minResolution);
- }
- }
- for (int z = 0, i = 0, j = 0; z < minResolution - 1; z++)
- {
- for (int x = 0; x < minResolution - 1; x++, i++, j += 6)
- {
- indices[j] = i;
- indices[j + 1] = i + (minResolution - 1) + 1;
- indices[j + 2] = i + 1;
- indices[j + 3] = i + 1;
- indices[j + 4] = i + (minResolution - 1) + 1;
- indices[j + 5] = i + (minResolution - 1) + 2;
- }
- i++;
- }
- return new Mesh()
- {
- indexFormat = UnityEngine.Rendering.IndexFormat.UInt32,
- vertices = vertices,
- triangles = indices,
- uv = uvs
- };
- }
- Texture2D ToTexture2D(RenderTexture rTex)
- {
- Texture2D tex = new Texture2D(rTex.width, rTex.height);
- RenderTexture.active = rTex;
- tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
- tex.Apply();
- return tex;
- }
- void OnDestroy()
- {
- displacementMap.Release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement