public void ResetTrails(bool forceReset = false) { if (!forceReset && _onlyResetWhenComplete) { _trailResetQueued = true; } else { _trailResetQueued = false; if (_trailState != null) { NBodyC.DestroyGalaxy(_trailState); _trailState = null; } _trailState = NBodyC.Clone(mainState); _trails.Clear(); unsafe { BlackHole *src = mainState->blackHoles; for (int i = 0; i < mainState->numBlackHoles; i++, src++) { _trails[src->id] = new TrailRecord(); } } } }
private void OnDisable() { if (mainState != null) { NBodyC.DestroyGalaxy(mainState); mainState = null; } if (prevState != null) { NBodyC.DestroyGalaxy(prevState); prevState = null; } if (_trailState != null) { NBodyC.DestroyGalaxy(_trailState); _trailState = null; } }
public unsafe void ResetSimulation() { if (mainState != null) { NBodyC.DestroyGalaxy(mainState); mainState = null; } if (prevState != null) { NBodyC.DestroyGalaxy(prevState); prevState = null; } if (_trailState != null) { NBodyC.DestroyGalaxy(_trailState); _trailState = null; } _trails.Clear(); _trailMesh.Clear(); simulationTime = 0; mainState = NBodyC.CreateGalaxy(blackHoleCount); mainState->time = 0; mainState->frames = 0; _initialBlackHoleCount = blackHoleCount; { Random.InitState(_seed); BlackHole *dst = mainState->blackHoles; int nextId = 1; for (int i = 0; i < blackHoleCount; i++, dst++) { Vector3 position = Random.onUnitSphere * blackHoleSpawnRadius; *dst = new BlackHole() { position = position, velocity = Vector3.Slerp(Vector3.zero - position, Random.onUnitSphere, initialDirVariance).normalized *blackHoleVelocity, mass = Random.Range(1 - blackHoleMassVariance, 1 + blackHoleMassVariance), id = nextId, rotation = Random.rotationUniform }; nextId = nextId << 1; } } Texture2D tex = new Texture2D(2048, 1, TextureFormat.RFloat, mipmap: false, linear: true); for (int i = 0; i < tex.width; i++) { float t = 1 - i / 2047f; t = Mathf.Pow(t, pow); tex.SetPixel(i, 0, new Color(t, 0, 0, 0)); //tex.SetPixel(i, 0, new Color(radiusDistribution.Evaluate(i / 2048.0f), 0, 0, 0)); } tex.Apply(); tex.filterMode = FilterMode.Bilinear; tex.wrapMode = TextureWrapMode.Clamp; simulateMat.SetTexture("_RadiusDistribution", tex); updateShaderConstants(); { BlackHole *src = mainState->blackHoles; for (int i = 0; i < mainState->numBlackHoles; i++, src++) { _vectorArray[i] = src->velocity; } simulateMat.SetVectorArray("_PlanetVelocities", _vectorArray); } { BlackHole *src = mainState->blackHoles; _floatArray.Fill(0); for (int i = 0; i < mainState->numBlackHoles; i++, src++) { _floatArray[i] = Mathf.Lerp(1, src->mass, blackHoleMassAffectsDensity); } simulateMat.SetFloatArray("_PlanetDensities", _floatArray); simulateMat.SetFloat("_TotalDensity", _floatArray.Query().Fold((a, b) => a + b)); } { BlackHole *src = mainState->blackHoles; for (int i = 0; i < mainState->numBlackHoles; i++, src++) { _floatArray[i] = Mathf.Lerp(1, src->mass, blackHoleMassAffectsSize); } simulateMat.SetFloatArray("_PlanetSizes", _floatArray); } GL.LoadPixelMatrix(0, 1, 0, 1); prevPos.DiscardContents(); currPos.DiscardContents(); RenderBuffer[] buffer = new RenderBuffer[2]; buffer[0] = prevPos.colorBuffer; buffer[1] = currPos.colorBuffer; Graphics.SetRenderTarget(buffer, prevPos.depthBuffer); simulateMat.SetPass(1); GL.Begin(GL.QUADS); GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0); GL.TexCoord2(1, 0); GL.Vertex3(1, 0, 0); GL.TexCoord2(1, 1); GL.Vertex3(1, 1, 0); GL.TexCoord2(0, 1); GL.Vertex3(0, 1, 0); GL.End(); prevState = NBodyC.Clone(mainState); prevState->time = mainState->time - 1.0f / REFERENCE_FRAMERATE; ResetTrails(forceReset: true); if (OnReset != null) { OnReset(); } }