forked from Gameslinx/Tessellation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScatterManager.cs
452 lines (431 loc) · 19.8 KB
/
ScatterManager.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using ComputeLoader;
using ParallaxGrass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Profiling;
namespace Grass
{
[KSPAddon(KSPAddon.Startup.PSystemSpawn, true)]
public class ScatterManagerPlus : MonoBehaviour
{
public static Dictionary<string, List<ScatterComponent>> scatterComponents = new Dictionary<string, List<ScatterComponent>>(); //Planet name, component
public static Dictionary<string, List<SharedScatterComponent>> sharedScatterComponents = new Dictionary<string, List<SharedScatterComponent>>();
public static Dictionary<string, GameObject> gameObjects = new Dictionary<string, GameObject>(); //GO to hold scatter components
public static ScatterManagerPlus Instance;
public delegate void QuadRangeCheck();
public static event QuadRangeCheck OnQuadRangeCheck;
public delegate void QuadPhysicsCheck();
public static event QuadPhysicsCheck OnQuadPhysicsCheck;
public string[] keys;
public bool suppressPlanetChange = false;
void Awake()
{
GameObject.DontDestroyOnLoad(this);
Instance = this;
}
void OnEnable()
{
BodySwitchManager.onBodyChange += OnPlanetChange;
BodySwitchManager.onSceneChange += OnSceneChange;
GameEvents.onVesselGoOffRails.Add(OnOffRails);
GameEvents.onFlightReady.Add(OnFlightReady);
GameEvents.OnFlightGlobalsReady.Add(FlightGlobalsReady);
}
void OnDisable()
{
BodySwitchManager.onBodyChange -= OnPlanetChange;
BodySwitchManager.onSceneChange -= OnSceneChange;
GameEvents.onVesselGoOffRails.Remove(OnOffRails);
GameEvents.onFlightReady.Remove(OnFlightReady);
GameEvents.OnFlightGlobalsReady.Remove(FlightGlobalsReady);
}
public void FlightGlobalsReady(bool ready)
{
if (!ready)
{
Debug.Log("Flight globals not ready - Setting active vessel at frame " + Time.frameCount);
}
}
void Start()
{
keys = ScatterBodies.scatterBodies.Keys.ToArray();
for (int i = 0; i < ScatterBodies.scatterBodies.Count; i++)
{
int scatterCount = ScatterBodies.scatterBodies[keys[i]].scatters.Count;
List<ScatterComponent> planetComponents = new List<ScatterComponent>();
List<SharedScatterComponent> sharedPlanetComponents = new List<SharedScatterComponent>();
GameObject go = new GameObject(keys[i] + " (Scatter Manager)");
GameObject.DontDestroyOnLoad(go);
go.SetActive(false);
for (int b = 0; b < scatterCount; b++)
{
string[] scatterKeys = ScatterBodies.scatterBodies[keys[i]].scatters.Keys.ToArray();
Scatter scatter = ScatterBodies.scatterBodies[keys[i]].scatters[scatterKeys[b]];
if (!scatter.shared)
{
ScatterComponent comp = go.gameObject.AddComponent<ScatterComponent>();
comp.name = scatter.scatterName + " Manager";
comp.scatter = scatter;
comp.pc = go.AddComponent<PostCompute>();
comp.pc.scatterName = scatter.scatterName;
planetComponents.Add(comp);
}
else
{
SharedScatterComponent comp = go.AddComponent<SharedScatterComponent>();
comp.name = scatter.scatterName + " Shared Mananger";
comp.scatter = scatter;
comp.pc = go.AddComponent<PostCompute>();
comp.pc.scatterName = scatter.scatterName;
comp.parentName = scatter.planetName + "-" + scatter.sharedParent;
sharedPlanetComponents.Add(comp);
}
}
scatterComponents.Add(keys[i], planetComponents);
gameObjects.Add(keys[i], go);
}
}
public void FixedUpdate() //Maybe make a coroutine to space out range checks :D
{
if (HighLogic.LoadedScene != GameScenes.FLIGHT) { return; }
if (OnQuadRangeCheck != null)
{
OnQuadRangeCheck();
}
if (OnQuadPhysicsCheck != null && ScatterGlobalSettings.enableCollisions)
{
OnQuadPhysicsCheck();
}
}
//public void Update()
//{
//
//}
public void Restart()
{
foreach(KeyValuePair<string, GameObject> pair in gameObjects)
{
UnityEngine.Object.Destroy(pair.Value);
}
gameObjects.Clear();
scatterComponents.Clear();
sharedScatterComponents.Clear();
Start();
foreach (KeyValuePair<string, GameObject> pair in gameObjects)
{
if (pair.Key == FlightGlobals.currentMainBody.name)
{
pair.Value.SetActive(true);
}
}
}
public void OnSceneChange(GameScenes from, GameScenes to)
{
Debug.Log("Scene change: " + from.ToString() + " -> " + to.ToString());
StartCoroutine(WaitForDominantBody());
}
public IEnumerator WaitForDominantBody() //wdym by that O_o
{
ScatterLog.Log("Scene change logged, awaiting planet change before starting scatters...");
yield return new WaitUntil(() => FlightGlobals.currentMainBody != null);
ScatterLog.Log("Planet found, starting scatters");
string currentBody = FlightGlobals.currentMainBody.name;
foreach (KeyValuePair<string, GameObject> go in gameObjects)
{
if (go.Key == currentBody)
{
go.Value.SetActive(true);
}
else
{
go.Value.SetActive(false);
}
}
}
public void OnPlanetChange(string from, string to)
{
Debug.Log("Planet change: " + from + " -> " + to);
if (suppressPlanetChange) //Will be called again by the switch manager
{
suppressPlanetChange = false;
Debug.Log(" - Blocked by previous request");
return;
}
foreach (KeyValuePair<string, GameObject> go in gameObjects)
{
if (go.Key == to)
{
go.Value.SetActive(true);
}
else
{
go.Value.SetActive(false);
}
}
}
public void RequestEarlyInitialization(string name) //Called from harmony patched PQS to get the buffers created before the terrain builds, as KSP does not provide a method for this itself
{
foreach (KeyValuePair<string, List<ScatterComponent>> go in scatterComponents)
{
if (go.Key == name)
{
foreach(ScatterComponent component in go.Value)
{
component.DestroyComputes();
component.CreateComputes(component.scatter.properties.scatterDistribution.noise.noiseMode, FlightGlobals.GetBodyByName(component.scatter.planetName).pqsController.maxLevel - component.scatter.properties.subdivisionSettings.minLevel + 1);
component.CreateBuffers();
}
}
}
}
public void OnFlightReady()
{
Debug.Log("Flight ready at frame " + Time.frameCount);
foreach (KeyValuePair<string, List<ScatterComponent>> go in scatterComponents)
{
if (go.Key == FlightGlobals.currentMainBody.name)
{
for (int i = 0; i < go.Value.Count; i++)
{
go.Value[i].CheckQueue();
}
}
}
//foreach (KeyValuePair<string, GameObject> go in gameObjects)
//{
// if (go.Key == FlightGlobals.currentMainBody.name && !go.Value.activeSelf)
// {
// go.Value.SetActive(true);
// }
// else if (go.Key != FlightGlobals.currentMainBody.name)
// {
// go.Value.SetActive(false);
// }
//}
//foreach (QuadData qd in PQSMod_ParallaxScatter.quadList.Values) //So the reason for this is annoying. For all scatters, they are generated, evaluated and work properly
//{ //But for SOME reason, SOME scatters regardless of their type won't show up on flight scene start
// foreach (ScatterCompute sc in qd.comps.Values) //So this forces them to regenerate at the start of the flight scene and they all show up
// { //Idk, I spent hours and hours trying to work this out, it's a hacky but a working solution. If someone else
// if (sc.scatter.planetName == FlightGlobals.currentMainBody.name) //Wants to help me out by investigating this (just remove the double foreach and see for yourself)
// { //I will sell you my firstborn or something idk but yeah i cba to do more on this
// sc.Start();
// }
// }
//}
}
public void OnOffRails(Vessel v)
{
if (v.Landed && !v.parts[0].isKerbalEVA() && (v.parts.Where((x) => x.name == "groundAnchor").Count() == 0))
{
Debug.Log("Vessel terrain height: " + v.heightFromTerrain);
Debug.Log("manager off rails");
Vector3d up = Vector3.Normalize(v.vesselTransform.position - v.mainBody.transform.position);
v.SetPosition(v.vesselTransform.position + up * 0.1d); //Avoid spawning inside scatter colliders. It's a pretty dog fix but I've given up on this
v.GetHeightFromTerrain();
List<Part> parts = v.parts;
for (int i = 0; i < parts.Count; i++)
{
}
Debug.Log("New vessel terrain height: " + v.heightFromTerrain);
}
}
}
public class ScatterComponent : MonoBehaviour
{
public Scatter scatter;
public int triCount = 131;
public BufferList bufferList;
public delegate void ForceEvaluate();
public event ForceEvaluate OnForceEvaluate;
public delegate void RangeCheck(); //Check if quad is close enough to subdivide
public event RangeCheck OnRangeCheck;
public PostCompute pc;
public Coroutine co;
public Queue<ScatterCompute> scatterQueue = new Queue<ScatterCompute>(); //Queue up quads to be processed by the distribute compute shaders
public List<ComputeShader> computePool = new List<ComputeShader>(); //Pool of compute shaders that are currently awaiting instructions
public int maxObjects = 0; //Max amount of objects for this scatter - Massively contributes to memory usage
public bool buffersCreated = false;
public bool computesCreated = false;
int requiredMemory = 0;
public void OnEnable()
{
Debug.Log("Scatter enabled: " + scatter.scatterName);
CreateBuffers();
CreateComputes(scatter.properties.scatterDistribution.noise.noiseMode, FlightGlobals.GetBodyByName(scatter.planetName).pqsController.maxLevel - scatter.properties.subdivisionSettings.minLevel + 1);
co = StartCoroutine(OnUpdate());
StartCoroutine(WaitForFlight()); //Wait until flight and regenerate. In some edge cases without this, the nearest quad scatters would disappear on scene change. No idea why. Yet
}
public void OnDisable()
{
Debug.Log("Scatter Disabled: " + scatter.scatterName);
buffersCreated = false;
StopCoroutine(co);
DestroyBuffers();
DestroyComputes();
}
public ComputeShader InstantiateNew(DistributionNoiseMode noiseMode)
{
if (noiseMode == DistributionNoiseMode.NonPersistent)
{
return GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistributeNearest"));
}
if (noiseMode == DistributionNoiseMode.Persistent)
{
return GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistributeFixed"));
}
if (noiseMode == DistributionNoiseMode.VerticalStack)
{
return GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistFTH"));
}
Debug.Log("[Exception] Unrecognized DistributionNoiseMode");
return null;
}
public void CreateComputes(DistributionNoiseMode noiseMode, int subdivisionDifference)
{
//When subdivision difference is high, it means the scatter covers a lot of quads
//so when doing camera changes we want them generating quickly, which needs more computes
//but also results in a slightly higher memory usage. Unlucky, L + cope
if (computesCreated) { return; }
computePool.Clear();
if (noiseMode == DistributionNoiseMode.NonPersistent)
{
for (int i = 0; i < 2 * subdivisionDifference; i++)
{
ComputeShader cs = GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistributeNearest"));
DontDestroyOnLoad(cs); //this doesn't work
computePool.Add(cs);
}
}
if (noiseMode == DistributionNoiseMode.Persistent)
{
for (int i = 0; i < 10 * subdivisionDifference; i++)
{
ComputeShader cs = GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistributeFixed"));
DontDestroyOnLoad(cs); //this doesn't work
computePool.Add(cs);
}
}
if (noiseMode == DistributionNoiseMode.VerticalStack)
{
for (int i = 0; i < 10 * subdivisionDifference; i++)
{
ComputeShader cs = GameObject.Instantiate(ScatterShaderHolder.GetCompute("DistFTH"));
DontDestroyOnLoad(cs); //this doesn't work
computePool.Add(cs);
}
}
computesCreated = true;
CheckQueue();
}
public void CheckQueue() //There may be items waiting in the queue that were added by QuadData/ScatterCompute before the compute pool was initialized - Process them now
{
if (scatterQueue.Count > 0)
{
int max = Mathf.Min(computePool.Count, scatterQueue.Count);
for (int i = 0; i < max; i++)
{
if (scatterQueue.Count > 0) //Dequeue CAN happen inside QuadData if the GPU finishes early
{
ScatterCompute comp = scatterQueue.Dequeue();
comp.Start();
}
}
}
}
public void DestroyComputes()
{
for (int i = 0; i < computePool.Count; i++)
{
Destroy(computePool[i]);
}
computePool.Clear();
computesCreated = false;
}
public void DestroyBuffers()
{
if (Buffers.activeBuffers.ContainsKey(scatter.scatterName))
{
buffersCreated = false;
Buffers.activeBuffers[scatter.scatterName].Dispose();
Buffers.activeBuffers.Remove(scatter.scatterName);
}
}
public void GetMaxMemory()
{
maxObjects = scatter.maxObjects;
if (maxObjects == 0)
{
requiredMemory = triCount * (int)scatter.properties.scatterDistribution._PopulationMultiplier * 100 * scatter.properties.scatterDistribution.noise._MaxStacks;
}
else
{
requiredMemory = (int)((float)maxObjects * ScatterGlobalSettings.densityMult * ScatterGlobalSettings.rangeMult);
}
if (requiredMemory == 0) { ScatterLog.Log("[Exception] Attempting to create a 0-length buffer, setting length to 10"); requiredMemory = 10; }
}
public void CreateBuffers()
{
if (buffersCreated) { Debug.Log("Buffers already created externally"); return; }
DestroyBuffers();
GetMaxMemory();
bufferList = new BufferList(requiredMemory, GrassData.Size(), Time.frameCount);
Buffers.activeBuffers.Add(scatter.scatterName, bufferList);
buffersCreated = true;
}
WaitForSeconds rapidWait = new WaitForSeconds(0.0606f);
public IEnumerator OnUpdate()
{
while (true)
{
Buffers.activeBuffers[scatter.scatterName].SetCounterValue(0);
if (scatter.properties.subdivisionSettings.mode == SubdivisionMode.NearestQuads && OnRangeCheck != null) { OnRangeCheck(); }
if (OnForceEvaluate != null) { OnForceEvaluate(); }
pc.Setup(Buffers.activeBuffers[scatter.scatterName].buffer, Buffers.activeBuffers[scatter.scatterName].farBuffer, Buffers.activeBuffers[scatter.scatterName].furtherBuffer, scatter);
yield return rapidWait;
}
}
public IEnumerator WaitForFlight() //Should only be used for NearestQuad scatters, then remove this method entirely when I fix body1 -> track -> body2 -> track -> body1 which makes them disappear
{
yield return new WaitUntil(() => FlightGlobals.ready);
Debug.Log("FlightGlobals: Ready");
foreach (KeyValuePair<PQ, QuadData> data in PQSMod_ParallaxScatter.quadList)
{
foreach (KeyValuePair<Scatter, ScatterCompute> sc in data.Value.comps)
{
sc.Value.Start();
}
}
}
}
public class SharedScatterComponent : MonoBehaviour
{
public Scatter scatter;
public string parentName;
public PostCompute pc;
WaitForSeconds rapidWait = new WaitForSeconds(0.0606f);
Coroutine co;
void OnEnable()
{
Debug.Log("Shared scatter enabled: " + scatter.scatterName);
co = StartCoroutine(OnUpdate());
}
void OnDisable()
{
Debug.Log("Shared scatter disabled: " + scatter.scatterName);
StopCoroutine(co);
}
public IEnumerator OnUpdate()
{
while (true)
{
pc.Setup(Buffers.activeBuffers[parentName].buffer, Buffers.activeBuffers[parentName].farBuffer, Buffers.activeBuffers[parentName].furtherBuffer, scatter);
yield return rapidWait;
}
}
}
}