예제 #1
0
    private void stepSimulation()
    {
        _simTime += Time.deltaTime * simulationSpeed;

        while (_simTime > _currState.simTime)
        {
            _prevState.CopyFrom(_currState);
            _currState.Step();
        }

        if (OnUpdateSystem != null)
        {
            OnUpdateSystem();
        }
    }
예제 #2
0
    private void updateTrails()
    {
        int stepsTaken = 0;

        while (_cometPathState.frames < (_currState.frames + _cometPathLength))
        {
            _cometPathState.Step();
            stepsTaken++;

            for (int i = 0; i < _cometPaths.Length; i++)
            {
                _cometPaths[i].Enqueue(_cometPathState.comets[i].position);
                while (_cometPaths[i].Count > _cometPathLength)
                {
                    _cometPaths[i].Dequeue();
                }
            }

            if (stepsTaken >= _maxPathStepsPerFrame)
            {
                break;
            }
        }

        if (_cometPathState.frames - _currState.frames >= _cometPathLength)
        {
            using (new ProfilerSample("Build Comet Paths")) {
                _tempVerts.Clear();
                _tempLines.Clear();

                using (new ProfilerSample("Build Vertex List")) {
                    for (int i = 0; i < _cometPaths.Length; i++)
                    {
                        int index = 0;
                        foreach (var point in _cometPaths[i])
                        {
                            if (index != 0)
                            {
                                _tempLines.Add(_tempVerts.Count);
                                _tempLines.Add(_tempVerts.Count - 1);
                            }

                            _tempVerts.Add(point);
                            _tempColors.Add(_cometPathGradient.Evaluate(index / (_cometPaths[i].Count - 1.0f)) * Color.white);

                            index++;
                        }
                    }
                }

                int[] indexArray;
                using (new ProfilerSample("Build Index Array")) {
                    int goalLength = Mathf.NextPowerOfTwo(_tempLines.Count);

                    if (!_trailIndexCache.TryGetValue(goalLength, out indexArray))
                    {
                        indexArray = new int[goalLength];
                        _trailIndexCache[goalLength] = indexArray;
                    }

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

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

                using (new ProfilerSample("Upload Mesh")) {
                    _cometPathMesh.Clear();
                    _cometPathMesh.SetVertices(_tempVerts);
                    _cometPathMesh.SetColors(_tempColors);
                    _cometPathMesh.SetIndices(indexArray, MeshTopology.Lines, 0);

                    _tempVerts.Clear();
                    _tempLines.Clear();
                    _tempColors.Clear();
                }
            }
        }

        Graphics.DrawMesh(_cometPathMesh, _displayAnchor.localToWorldMatrix, _cometPathMaterial, 0);
    }