-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameBatch.cs
135 lines (113 loc) · 4.01 KB
/
FrameBatch.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using UnityEngine;
using UnityMeshSimplifier;
namespace SphereOpt
{
public class FrameBatch
{
public FrameSegment[] segs;
public ComputeBuffer buffer;
public int cursor;
public int capacity;
public bool isBatchBufferDirty;
public ComputeBuffer[] lodBatchBuffers;
public Material protoMat;
public Material protoMatLOD2;
public Mesh[] lodMeshes;
public MaterialPropertyBlock mpb;
public void AddSegment(FrameSegment seg)
{
if (capacity == 0)
SetCapacity(32);
else if (capacity == cursor)
SetCapacity(capacity * 2);
segs[cursor] = seg;
cursor++;
}
public void ClearSegments()
{
cursor = 0;
}
public void SetCapacity(int newCap)
{
var newArray = new FrameSegment[newCap];
if (segs != null)
Array.Copy(segs, newArray, capacity);
segs = newArray;
capacity = newCap;
buffer?.Release();
buffer = new ComputeBuffer(capacity, 32);
protoMat.SetBuffer("_InstBuffer", buffer);
protoMatLOD2.SetBuffer("_InstBuffer", buffer);
for (int i = 0; i < 3; i++)
{
lodBatchBuffers[i]?.Release();
lodBatchBuffers[i] = new ComputeBuffer(capacity, 4, ComputeBufferType.Append);
}
}
public void SyncBufferData()
{
if (isBatchBufferDirty && buffer != null)
buffer.SetData(segs);
isBatchBufferDirty = false;
}
public void SetBatchBufferDirty()
{
isBatchBufferDirty = true;
}
public void Free()
{
if (buffer != null)
{
buffer.Release();
buffer = null;
}
segs = null;
cursor = capacity = 0;
}
public void SetupMat(Material mat)
{
if (protoMat != null)
return;
protoMat = UnityEngine.Object.Instantiate(mat);
CustomShaderManager.ReplaceShaderIfAvailable(protoMat);
protoMat.SetBuffer("_InstBuffer", buffer);
SphereOpt.logger.LogInfo($"protomat: {protoMat.shader.name}");
protoMatLOD2 = UnityEngine.Object.Instantiate(mat);
CustomShaderManager.ApplyCustomShaderToMaterial(protoMatLOD2, "instFrameLOD2");
protoMatLOD2.SetBuffer("_InstBuffer", buffer);
SphereOpt.logger.LogInfo($"protoMatLOD2: {protoMatLOD2.shader.name}");
}
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.PreserveUVFoldoverEdges = true;
options.MaxIterationCount = 1000;
var meshSimplifier = new MeshSimplifier();
meshSimplifier.Initialize(lodMeshes[1]);
meshSimplifier.SimplificationOptions = options;
meshSimplifier.SimplifyMesh(0.7f);
lodMeshes[1] = meshSimplifier.ToMesh();
meshSimplifier = new MeshSimplifier();
meshSimplifier.Initialize(lodMeshes[2]);
meshSimplifier.SimplificationOptions = options;
meshSimplifier.SimplifyMesh(0.2f);
lodMeshes[2] = meshSimplifier.ToMesh();
lodMeshes[2].normals = null;
lodMeshes[2].tangents = null;
}
public void ResetCounters()
{
lodBatchBuffers[0].SetCounterValue(0u);
lodBatchBuffers[1].SetCounterValue(0u);
lodBatchBuffers[2].SetCounterValue(0u);
}
}
}