-
Notifications
You must be signed in to change notification settings - Fork 157
/
MeshVerticesParallelUpdate.cs
79 lines (59 loc) · 1.86 KB
/
MeshVerticesParallelUpdate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class MeshVerticesParallelUpdate : MonoBehaviour
{
[Range(0.05f, 1f)]
[SerializeField]
protected float m_Strength = 0.25f;
NativeArray<Vector3> m_Vertices;
Vector3[] m_ModifiedVertices;
CalculateJob m_CalculateJob;
JobHandle m_JobHandle;
MeshFilter m_MeshFilter;
Mesh m_Mesh;
protected void Start()
{
m_MeshFilter = gameObject.GetComponent<MeshFilter>();
m_Mesh = m_MeshFilter.mesh;
m_Mesh.MarkDynamic();
// this persistent memory setup assumes our vertex count will not expand
m_Vertices = new NativeArray<Vector3>(m_Mesh.vertices, Allocator.Persistent);
m_ModifiedVertices = new Vector3[m_Vertices.Length];
}
struct CalculateJob : IJobParallelFor
{
public NativeArray<Vector3> vertices;
public float sineTime;
public float strength;
public void Execute(int i)
{
var vertex = vertices[i];
var perlin = Mathf.PerlinNoise(vertex.z, vertex.y) * strength;
var noise = Vector3.one * perlin;
var sine = Vector3.one * sineTime * strength;
vertex = vertex - sine + noise;
vertices[i] = vertex;
}
}
public void Update()
{
m_CalculateJob = new CalculateJob()
{
vertices = m_Vertices,
sineTime = Mathf.Sin(Time.time),
strength = m_Strength / 5f // map .05-1 range to smaller real strength
};
m_JobHandle = m_CalculateJob.Schedule(m_Vertices.Length, 64);
}
public void LateUpdate()
{
m_JobHandle.Complete();
m_CalculateJob.vertices.CopyTo(m_ModifiedVertices);
m_Mesh.vertices = m_ModifiedVertices;
}
private void OnDestroy()
{
m_Vertices.Dispose();
}
}