-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstDysonShellRenderer.cs
274 lines (233 loc) · 9.82 KB
/
InstDysonShellRenderer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using UnityEngine;
using UnityEngine.Rendering;
namespace SphereOpt
{
public class InstDysonShellRenderer
{
private static Mesh _HexMesh;
private static Material _HexMat;
public static Material HexMat
{
get {
if(_HexMat == null)
SetupMesh();
return _HexMat;
}
}
public static Mesh HexMesh
{
get {
if(_HexMesh == null)
SetupMesh();
return _HexMesh;
}
}
private ComputeBuffer argsBuffer;
private uint[] args = new uint[55];
private InstDysonShellLayer[] instShellLayers = new InstDysonShellLayer[11];
private bool argsBufferIsDirty = true;
public DysonSphere dysonSphere;
private GameData gameData;
private StarData starData;
private static readonly int GlobalDSSunPosition = Shader.PropertyToID("_Global_DS_SunPosition");
private static readonly int GlobalDSSunPositionMap = Shader.PropertyToID("_Global_DS_SunPosition_Map");
private static readonly int ObjectToWorld = Shader.PropertyToID("_ObjectToWorld");
private static readonly int SunColor = Shader.PropertyToID("_SunColor");
private static readonly int DysonEmission = Shader.PropertyToID("_DysonEmission");
private static void SetupMesh()
{
_HexMesh = new Mesh();
var newVerts = new Vector3[7];
var t1axis = new Vector3(-40f, (float)(40.0 / Math.Sqrt(3)), 0);
var t2axis = new Vector3(40f, (float)(40.0 / Math.Sqrt(3)), 0);
var t0axis = new Vector3(0, (float)(80.0 / Math.Sqrt(3)), 0);
newVerts[0] = Vector3.zero;
newVerts[1] = t0axis;
newVerts[2] = Vector3.zero - t0axis;
newVerts[3] = t1axis;
newVerts[4] = Vector3.zero - t1axis;
newVerts[5] = t2axis;
newVerts[6] = Vector3.zero - t2axis;
var newTris = new[]
{
1, 0, 3,
3, 0, 6,
6, 0, 2,
2, 0, 4,
4, 0, 5,
5, 0, 1
};
_HexMesh.vertices = newVerts;
_HexMesh.triangles = newTris;
Material material = Resources.Load<Material>("Dyson Sphere/Materials/dyson-shell-unlit-0");
Texture2D cc2 = (Texture2D)material.GetTexture("_ColorControlTex2");
Texture2D emm2 = (Texture2D)material.GetTexture("_EmissionTex2");
Texture2DArray colorControlTex2 = new Texture2DArray(cc2.width, cc2.height, 7, cc2.format, true);
Texture2DArray emissionTex2 = new Texture2DArray(emm2.width, emm2.height, 7, emm2.format, true);
float[] emissionMultiplier = new float[7];
for (int m = 0; m < cc2.mipmapCount; m++)
{
Graphics.CopyTexture(cc2, 0, m, colorControlTex2, 0, m);
}
for (int m = 0; m < emm2.mipmapCount; m++)
{
Graphics.CopyTexture(emm2, 0, m, emissionTex2, 0, m);
}
emissionMultiplier[0] = material.GetFloat("_EmissionMultiplier");
for (int i = 1; i < 7; i++)
{
material = Resources.Load<Material>($"Dyson Sphere/Materials/dyson-shell-unlit-{i}");
cc2 = (Texture2D)material.GetTexture("_ColorControlTex2");
emm2 = (Texture2D)material.GetTexture("_EmissionTex2");
for (int m = 0; m < cc2.mipmapCount; m++)
{
Graphics.CopyTexture(cc2, 0, m, colorControlTex2, i, m);
}
for (int m = 0; m < emm2.mipmapCount; m++)
{
Graphics.CopyTexture(emm2, 0, m, emissionTex2, i, m);
}
emissionMultiplier[i] = material.GetFloat("_EmissionMultiplier");
}
_HexMat = UnityEngine.Object.Instantiate(material);
CustomShaderManager.ApplyCustomShaderToMaterial(_HexMat, "dysonshell-inst");
_HexMat.SetTexture("_ColorControlTex2", colorControlTex2);
_HexMat.SetTexture("_EmissionTex2", emissionTex2);
_HexMat.SetFloatArray("_EmissionMultiplier", emissionMultiplier);
}
public void Free()
{
if (argsBuffer != null)
{
argsBuffer.Release();
argsBuffer = null;
}
args = null;
dysonSphere = null;
gameData = null;
starData = null;
foreach (var t in instShellLayers)
{
RemoveLayer(t);
}
instShellLayers = null;
}
public InstDysonShellLayer getOrCreateInstShellLayer(int layerId)
{
return instShellLayers[layerId] = instShellLayers[layerId] ?? new InstDysonShellLayer(layerId);
}
public InstDysonShellLayer getInstShellLayer(int layerId)
{
return instShellLayers[layerId];
}
public InstDysonShellRenderer(DysonSphere _dysonSphere)
{
dysonSphere = _dysonSphere;
gameData = dysonSphere.gameData;
starData = dysonSphere.starData;
argsBuffer = new ComputeBuffer(11, 5 * sizeof(uint), ComputeBufferType.IndirectArguments);
for (int i = 0; i <= 10; i++)
{
args[i * 5 + 0] = HexMesh.GetIndexCount(0);
args[i * 5 + 1] = 0u;
args[i * 5 + 2] = HexMesh.GetIndexStart(0);
args[i * 5 + 3] = HexMesh.GetBaseVertex(0);
args[i * 5 + 4] = 0u;
}
}
public void UpdateLayerArgs(int layerId, uint hexCount)
{
args[layerId * 5 + 1] = hexCount;
argsBufferIsDirty = true;
}
public void RenderShells(ERenderPlace place, int editorMask, int gameMask)
{
for (var i = 1; i <= 10; i++)
{
var instLayer = instShellLayers[i];
if (instLayer == null) continue;
if (instLayer.hexPool.Count > 0)
{
instLayer.SyncBufferData();
if (instLayer.cachedHexCount != instLayer.hexPool.Count)
{
UpdateLayerArgs(i, (uint)instLayer.hexPool.Count);
instLayer.cachedHexCount = instLayer.hexPool.Count;
}
}
}
if (argsBufferIsDirty)
{
argsBuffer.SetData(args);
argsBufferIsDirty = false;
}
var localRot = new Quaternion(0f, 0f, 0f, 1f);
var sunPos = Vector3.zero;
var sunPosMap = Vector3.zero;
if (starData != null && gameData != null)
{
var localPlanet = gameData.localPlanet;
var mainPlayer = gameData.mainPlayer;
sunPos = localPlanet == null
? (Vector3)(starData.uPosition - mainPlayer.uPosition)
: (Vector3)Maths.QInvRotateLF(localPlanet.runtimeRotation,
starData.uPosition - localPlanet.uPosition);
if (place == ERenderPlace.Starmap)
sunPosMap = (starData.uPosition - UIStarmap.viewTargetStatic) * 0.00025;
if (localPlanet != null && place == ERenderPlace.Universe)
localRot = new Quaternion(localPlanet.runtimeRotation.x, localPlanet.runtimeRotation.y, localPlanet.runtimeRotation.z, 0f - localPlanet.runtimeRotation.w);
}
var layer = 16;
switch (place)
{
case ERenderPlace.Starmap:
layer = 20;
break;
case ERenderPlace.Dysonmap:
layer = 21;
break;
case ERenderPlace.Universe:
case ERenderPlace.DemoScene:
default:
break;
}
Shader.SetGlobalVector(GlobalDSSunPosition, sunPos);
Shader.SetGlobalVector(GlobalDSSunPositionMap, sunPosMap);
HexMat.SetColor(SunColor, dysonSphere.sunColor);
HexMat.SetColor(DysonEmission, dysonSphere.emissionColor);
var pos = place == ERenderPlace.Universe ? sunPos : sunPosMap;
for (var i = 1; i <= 10; i++)
{
if (instShellLayers[i] == null)
continue;
var shiftLayer = 1 << i;
if (!(layer != 16 && layer != 20 ? (editorMask & shiftLayer) > 0 : (gameMask & shiftLayer) > 0))
continue;
var instLayer = instShellLayers[i];
if (instLayer.hexPool.Count <= 0)
continue;
instLayer.SetProps();
//TODO: No need to reset the props every time, I think.
var dysonSphereLayer = dysonSphere.layersIdBased[i];
instLayer.props.SetMatrix(ObjectToWorld, Matrix4x4.TRS(pos, localRot * dysonSphereLayer.currentRotation, Vector3.one));
Graphics.DrawMeshInstancedIndirect(HexMesh, 0, HexMat,
new Bounds(Vector3.zero, new Vector3(300000f, 300000f, 300000f)), argsBuffer, i * 5 * 4,
instLayer.props, ShadowCastingMode.Off, receiveShadows: false, layer);
}
}
public void RemoveLayer(int layerId)
{
if (instShellLayers[layerId] != null)
{
instShellLayers[layerId].Free();
instShellLayers[layerId] = null;
}
}
public void RemoveLayer(InstDysonShellLayer layer)
{
if (layer == null) return;
RemoveLayer(layer.layerId);
}
}
}