forked from stella3d/job-system-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshComplexParallel.cs
94 lines (72 loc) · 2.47 KB
/
MeshComplexParallel.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using Unity.Collections;
using Unity.Jobs;
public class MeshComplexParallel : MonoBehaviour
{
[Range(0.05f, 1f)]
[SerializeField]
protected float m_Strength = 0.1f;
NativeArray<Vector3> m_Vertices;
NativeArray<Vector3> m_Normals;
Vector3[] m_ModifiedVertices;
Vector3[] m_ModifiedNormals;
MeshModJob m_MeshModJob;
JobHandle m_JobHandle;
Mesh m_Mesh;
protected void Start()
{
m_Mesh = gameObject.GetComponent<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_Normals = new NativeArray<Vector3>(m_Mesh.normals, Allocator.Persistent);
m_ModifiedVertices = new Vector3[m_Vertices.Length];
m_ModifiedNormals = new Vector3[m_Vertices.Length];
}
struct MeshModJob : IJobParallelFor
{
public NativeArray<Vector3> vertices;
public NativeArray<Vector3> normals;
public float sinTime;
public float cosTime;
public float strength;
public void Execute(int i)
{
var vertex = vertices[i];
var perlin = Mathf.PerlinNoise(vertex.z, vertex.y * vertex.x);
perlin *= strength * 2;
var noise = normals[i] * perlin;
var sine = normals[i] * sinTime * strength;
vertex = vertex - sine + noise;
vertices[i] = vertex;
normals[i] += Vector3.one * cosTime * perlin;
}
}
public void Update()
{
m_MeshModJob = new MeshModJob()
{
vertices = m_Vertices,
normals = m_Normals,
sinTime = Mathf.Sin(Time.time),
cosTime = Mathf.Cos(Time.time),
strength = m_Strength / 5f // map .05-1 range to smaller real strength
};
m_JobHandle = m_MeshModJob.Schedule(m_Vertices.Length, 64);
}
public void LateUpdate()
{
m_JobHandle.Complete();
// copy our results to managed arrays so we can assign them
m_MeshModJob.vertices.CopyTo(m_ModifiedVertices);
m_MeshModJob.normals.CopyTo(m_ModifiedNormals);
m_Mesh.vertices = m_ModifiedVertices;
m_Mesh.normals = m_ModifiedNormals;
}
private void OnDestroy()
{
// make sure to Dispose() any NativeArrays when we're done
m_Vertices.Dispose();
m_Normals.Dispose();
}
}