Example #1
0
    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();
                }
            }
        }
    }
Example #2
0
    private void Assignations()
    {
        uiManager = FindObjectOfType <UIManager>();
        switch (type)
        {
        case TrailTypes.Right:
            record = GameObject.FindGameObjectWithTag("R_Record").GetComponent <TrailRecord>();
            break;

        case TrailTypes.Left:
            record = GameObject.FindGameObjectWithTag("L_Record").GetComponent <TrailRecord>();
            break;

        case TrailTypes.Undefined:
            Debug.Log("Trail record not defined");
            break;

        default:
            throw new ArgumentOutOfRangeException();
        }
    }
Example #3
0
    private void Update()
    {
        if (Input.GetKeyDown(resetKeycode))
        {
            ResetSimulation();
        }

        if ((loop && mainState->time > loopTime) || respawnMode)
        {
            ResetSimulation();
            return;
        }

        Random.InitState(Time.frameCount);
        _seed = Random.Range(int.MinValue, int.MaxValue);

        if (simulate)
        {
            stepSimulation();
        }

        if (_enableTrails)
        {
            using (new ProfilerSample("Simulate Trails")) {
                if (_profileTrails)
                {
                    var stopwatch = new System.Diagnostics.Stopwatch();
                    stopwatch.Reset();
                    stopwatch.Start();
                    const int FRAMES_TO_TEST = 1000;
                    for (int i = 0; i < FRAMES_TO_TEST; i++)
                    {
                        NBodyC.StepGalaxy(_trailState);
                    }
                    double seconds         = stopwatch.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency;
                    double framesPerSecond = FRAMES_TO_TEST / seconds;
                    _trailFramerate = framesPerSecond;
                    Debug.Log("#####: " + _trailFramerate);
                }
                else
                {
                    int simTime = 0;
                    while (_trailState->frames < mainState->frames + _maxTrailLength)
                    {
                        NBodyC.StepGalaxy(_trailState);

                        unsafe {
                            BlackHole * src = _trailState->blackHoles;
                            TrailRecord trail;
                            for (int j = 0; j < _trailState->numBlackHoles; j++, src++)
                            {
                                if (!_trails.TryGetValue(src->id, out trail))
                                {
                                    trail = new TrailRecord()
                                    {
                                        startFrame = _trailState->frames
                                    };
                                    _trails[src->id] = trail;
                                }

                                trail.queue.PushBack(src->position);
                            }
                        }

                        simTime++;
                        if (simTime >= _trailUpdateRate)
                        {
                            break;
                        }
                    }
                }
            }

            //Build and display trail mesh
            //but only if it's already reached its max length
            if (_trailState->frames - mainState->frames >= _trailShowLength)
            {
                using (new ProfilerSample("Display Trails")) {
                    _trailVerts.Clear();
                    _trailIndices.Clear();

                    using (new ProfilerSample("Build Vertex List")) {
                        foreach (var pair in _trails)
                        {
                            for (int i = 0; i < pair.Value.queue.Count; i++)
                            {
                                if (i != 0)
                                {
                                    _trailIndices.Add(_trailVerts.Count);
                                    _trailIndices.Add(_trailVerts.Count - 1);
                                }

                                _trailVerts.Add(pair.Value.queue[i]);
                            }
                        }
                    }

                    int[] indexArray;
                    using (new ProfilerSample("Build Index Array")) {
                        indexArray = ArrayPool <int> .Spawn(_trailIndices.Count);

                        for (int i = 0; i < _trailIndices.Count; i++)
                        {
                            indexArray[i] = _trailIndices[i];
                        }

                        for (int i = _trailIndices.Count; i < indexArray.Length; i++)
                        {
                            indexArray[i] = 0;
                        }
                    }

                    using (new ProfilerSample("Upload Mesh")) {
                        _trailMesh.Clear();
                        _trailMesh.SetVertices(_trailVerts);
                        _trailMesh.SetIndices(indexArray, MeshTopology.Lines, 0);

                        ArrayPool <int> .Recycle(indexArray);

                        indexArray = null;
                    }

                    if (_trailResetQueued)
                    {
                        ResetTrails(forceReset: true);
                    }
                }
            }

            _trailPropertyBlock.SetColor("_Color", _trailColor);
            Graphics.DrawMesh(_trailMesh, galaxyRenderer.displayAnchor.localToWorldMatrix, _trailMaterial, 0, null, 0, _trailPropertyBlock);
        }

        //Render the black holes themselves
        unsafe {
            BlackHole *prevSrc  = prevState->blackHoles;
            BlackHole *mainSrc  = mainState->blackHoles;
            float      fraction = Mathf.InverseLerp(prevState->time, mainState->time, simulationTime);
            for (int j = 0; j < mainState->numBlackHoles; j++, prevSrc++, mainSrc++)
            {
                Vector3 position = Vector3.Lerp(prevSrc->position, mainSrc->position, fraction);
                galaxyRenderer.DrawBlackHole(position);
            }
        }
    }