136 lines
3.9 KiB
C#
136 lines
3.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
|
|
public class SnowRainVERTEX_SM : MonoBehaviour
|
|
{
|
|
public float Range = 1f;
|
|
|
|
public int SNOW_NUM = 16000;
|
|
|
|
private Vector3[] vertices_;
|
|
|
|
private int[] triangles_;
|
|
|
|
private Vector2[] uvs_;
|
|
|
|
private float range_;
|
|
|
|
private float rangeR_;
|
|
|
|
private Vector3 move_ = Vector3.zero;
|
|
|
|
public Vector3 rain_speed = new Vector3(0f, -1f, 0f);
|
|
|
|
public bool isRain;
|
|
|
|
public float rainSnowsize = 1f;
|
|
|
|
public float rainLength = 1f;
|
|
|
|
public bool controlVortex;
|
|
|
|
public float vortexRadius;
|
|
|
|
public float vorticity;
|
|
|
|
public float vortexSpeed;
|
|
|
|
public float vortexDepth;
|
|
|
|
public Vector3 vortexPosition = new Vector3(0f, 0f, 0f);
|
|
|
|
public float collisionplaneY;
|
|
|
|
public float collideThreashold;
|
|
|
|
public float collidePower;
|
|
|
|
private void Start()
|
|
{
|
|
range_ = 16f * Range;
|
|
rangeR_ = 1f / range_;
|
|
vertices_ = new Vector3[SNOW_NUM * 4];
|
|
for (int i = 0; i < SNOW_NUM; i++)
|
|
{
|
|
float x = Random.Range(0f - range_, range_);
|
|
float y = Random.Range(0f - range_, range_);
|
|
float z = Random.Range(0f - range_, range_);
|
|
Vector3 vector = new Vector3(x, y, z);
|
|
vertices_[i * 4] = vector;
|
|
vertices_[i * 4 + 1] = vector;
|
|
vertices_[i * 4 + 2] = vector;
|
|
vertices_[i * 4 + 3] = vector;
|
|
}
|
|
triangles_ = new int[SNOW_NUM * 6];
|
|
for (int j = 0; j < SNOW_NUM; j++)
|
|
{
|
|
triangles_[j * 6] = j * 4;
|
|
triangles_[j * 6 + 1] = j * 4 + 1;
|
|
triangles_[j * 6 + 2] = j * 4 + 2;
|
|
triangles_[j * 6 + 3] = j * 4 + 2;
|
|
triangles_[j * 6 + 4] = j * 4 + 1;
|
|
triangles_[j * 6 + 5] = j * 4 + 3;
|
|
}
|
|
uvs_ = new Vector2[SNOW_NUM * 4];
|
|
for (int k = 0; k < SNOW_NUM; k++)
|
|
{
|
|
uvs_[k * 4] = new Vector2(0f, 0f);
|
|
uvs_[k * 4 + 1] = new Vector2(1f, 0f);
|
|
uvs_[k * 4 + 2] = new Vector2(0f, 1f);
|
|
uvs_[k * 4 + 3] = new Vector2(1f, 1f);
|
|
}
|
|
Mesh mesh = new Mesh();
|
|
mesh.name = "MeshSnowFlakes";
|
|
mesh.vertices = vertices_;
|
|
mesh.triangles = triangles_;
|
|
mesh.uv = uvs_;
|
|
mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 100000000f);
|
|
GetComponent<MeshFilter>().sharedMesh = mesh;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
Vector3 vector = Camera.main.transform.TransformPoint(Vector3.forward * range_);
|
|
Renderer component = GetComponent<Renderer>();
|
|
component.material.SetFloat("_Range", range_);
|
|
component.material.SetFloat("_RangeR", rangeR_);
|
|
component.material.SetFloat("_Size", 0.1f * rainSnowsize);
|
|
component.material.SetVector("_MoveTotal", move_);
|
|
component.material.SetVector("_CamUp", Camera.main.transform.up);
|
|
component.material.SetVector("_TargetPosition", vector);
|
|
float x = (Mathf.PerlinNoise(0f, Time.time * 0.1f) - 0.5f) * 10f;
|
|
float y = -2f;
|
|
float z = (Mathf.PerlinNoise(Time.time * 0.1f, 0f) - 0.5f) * 10f;
|
|
if (controlVortex)
|
|
{
|
|
component.material.SetVector("vortexControl", new Vector4(vorticity, vortexSpeed, vortexDepth, vortexRadius));
|
|
component.material.SetVector("vortexPosRadius", new Vector4(vortexPosition.x, vortexPosition.y, vortexPosition.z, 0f));
|
|
}
|
|
if (isRain)
|
|
{
|
|
component.material.SetVector("_MoveR", rain_speed);
|
|
component.material.SetFloat("isRain", 1f);
|
|
component.material.SetFloat("rainLength", rainLength);
|
|
move_ += rain_speed * 0.1f;
|
|
move_.x = Mathf.Repeat(move_.x, range_ * 2f);
|
|
move_.y = Mathf.Repeat(move_.y, range_ * 2f);
|
|
move_.z = Mathf.Repeat(move_.z, range_ * 2f);
|
|
}
|
|
else
|
|
{
|
|
component.material.SetFloat("isRain", 0f);
|
|
component.material.SetFloat("rainLength", rainLength);
|
|
move_ += new Vector3(x, y, z) * Time.deltaTime;
|
|
move_.x = Mathf.Repeat(move_.x, range_ * 2f);
|
|
move_.y = Mathf.Repeat(move_.y, range_ * 2f);
|
|
move_.z = Mathf.Repeat(move_.z, range_ * 2f);
|
|
}
|
|
component.material.SetFloat("planeY", collisionplaneY);
|
|
component.material.SetFloat("hitThres", collideThreashold);
|
|
component.material.SetFloat("hitPower", collidePower);
|
|
}
|
|
}
|
|
}
|