Exemplo n.º 1
0
    public TransformState CalculateTransformAtTime(float time, GameContext context)
    {
        var timeDiff = context.State.Time - context.State.PrevTime;

        if (Mathf.Approximately(timeDiff, 0f))
        {
            return(this.Transform.Clone());
        }

        var t = (time - context.State.PrevTime) / timeDiff;

        return(TransformState.Lerp(this.prevTransform, this.Transform, t));
    }
Exemplo n.º 2
0
    private void Update()
    {
        float time = Time;
        float dt   = UnityEngine.Time.deltaTime;

        _spawnTimer  -= dt;
        _updateTimer -= dt;

        int spawnCount = 1;

        while (_spawnTimer < 0)
        {
            var newInstance = new Instance()
            {
                Mesh         = Meshes.Random(),
                Material     = Materials.Random(),
                SpawnTime    = _lastSpawnTime + SpawnTime * spawnCount,
                CurrentState = new TransformState(Vector3.back * 100, Vector3.zero, Vector3.one),
                TargetState  = new TransformState(Vector3.back * 100, Vector3.zero, Vector3.one),
                Color        = ColorManager == null ? Color.white : ColorManager.GetTemplateAtTime(ColorOffset).GetByIndex(ColorIndex),
            };
            _instances.Add(newInstance);
            _spawnTimer   += SpawnTime;
            _lastSpawnTime = time;
            spawnCount++;
        }

        // Rebuild poly points if needed
        if (_polygonPoints.Count != Sides)
        {
            _polygonPoints.Clear();
            for (var i = 0; i < Sides; ++i)
            {
                var tPi = (i / (float)(Sides - 1)) * Mathf.PI * 2;
                _polygonPoints.Add(new Vector2(Mathf.Sin(tPi), Mathf.Cos(tPi)));
            }
        }

        for (int i = _instances.Count - 1; i >= 0; --i)
        {
            var instance = _instances[i];
            if (time - instance.SpawnTime > TotalTime)
            {
                _instances.RemoveAt(i);
                continue;
            }

            if (_updateTimer < 0)
            {
                instance.TargetState = GetTransformState(time - instance.SpawnTime);
            }

            instance.CurrentState = TransformState.Lerp(instance.CurrentState, instance.TargetState, TransitionSpeed);
            if (_materialPropertyBlock == null)
            {
                _materialPropertyBlock = new MaterialPropertyBlock();
            }
            _materialPropertyBlock.Clear();
            _materialPropertyBlock.SetColor("_Color", instance.Color);
            Graphics.DrawMesh(instance.Mesh, transform.localToWorldMatrix * instance.CurrentState.ToMatrix(), instance.Material, 0, null, 0, _materialPropertyBlock);
        }
        if (_updateTimer < 0)
        {
            _updateTimer = UpdateTime;
        }
    }