-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeBatch.cs
107 lines (90 loc) · 3.14 KB
/
NodeBatch.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
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using UnityEngine;
using UnityMeshSimplifier;
namespace SphereOpt
{
public class NodeBatch
{
public ComputeBuffer[] lodBatchBuffers;
public Mesh[] lodMeshes;
public Material protoMat;
public ComputeBuffer buffer;
public int cursor;
public int capacity;
public NodeSegment[] nodes;
public bool isBatchBufferDirty;
public MaterialPropertyBlock mpb;
public void SetupMat(Material mat)
{
if (protoMat != null)
return;
protoMat = UnityEngine.Object.Instantiate(mat);
CustomShaderManager.ReplaceShaderIfAvailable(protoMat);
protoMat.SetBuffer("_InstBuffer", buffer);
}
public void SetupMeshes(Mesh mesh)
{
if (lodMeshes != null)
return;
lodMeshes = new Mesh[3];
lodMeshes[0] = UnityEngine.Object.Instantiate(mesh);
lodMeshes[1] = UnityEngine.Object.Instantiate(mesh);
lodMeshes[2] = UnityEngine.Object.Instantiate(mesh);
var options = SimplificationOptions.Default;
options.PreserveBorderEdges = true;
options.PreserveUVSeamEdges = true;
options.MaxIterationCount = 1000;
var meshSimplifier = new MeshSimplifier();
meshSimplifier.Initialize(mesh);
meshSimplifier.SimplificationOptions = options;
meshSimplifier.SimplifyMesh(0.7f);
lodMeshes[1] = meshSimplifier.ToMesh();
meshSimplifier = new MeshSimplifier();
meshSimplifier.Initialize(mesh);
meshSimplifier.SimplificationOptions = options;
meshSimplifier.SimplifyMesh(0.2f);
lodMeshes[2] = meshSimplifier.ToMesh();
}
public void ResetCounters()
{
lodBatchBuffers[0].SetCounterValue(0u);
lodBatchBuffers[1].SetCounterValue(0u);
lodBatchBuffers[2].SetCounterValue(0u);
}
public void SyncBufferData()
{
if (isBatchBufferDirty && buffer != null)
buffer.SetData(nodes);
isBatchBufferDirty = false;
}
public void SetBatchBufferDirty()
{
isBatchBufferDirty = true;
}
public void SetCapacity(int newCap)
{
var newArray = new NodeSegment[newCap];
if (nodes != null)
Array.Copy(nodes, newArray, capacity);
nodes = newArray;
capacity = newCap;
buffer?.Release();
buffer = new ComputeBuffer(capacity, 44);
protoMat.SetBuffer("_InstBuffer", buffer);
for (int i = 0; i < 3; i++)
{
lodBatchBuffers[i]?.Release();
lodBatchBuffers[i] = new ComputeBuffer(capacity, 4, ComputeBufferType.Append);
}
}
public void AddNode(NodeSegment seg)
{
if (capacity == 0)
SetCapacity(32);
else if (capacity == cursor)
SetCapacity(capacity * 2);
nodes[cursor] = seg;
cursor++;
}
}
}