void InvokeAnimationKernels()
        {
            if (_kernel == null)
            {
                _kernel = new AnimationKernelSet <Kernels, Buffers>(_kernelShader, x => (int)x, x => (int)x);
            }

            if (!_kernel.ready)
            {
                // Initialize the animation kernels and buffers.
                _kernel.Setup(_source.vertexCount, _historyLength);
                _kernel.material.SetFloat("_RandomSeed", _randomSeed);
                _kernel.Invoke(Kernels.InitializePosition, Buffers.Position);
                _kernel.Invoke(Kernels.InitializeVelocity, Buffers.Velocity);
            }
            else
            {
                // Transfer the source position attributes.
                _kernel.material.SetTexture("_SourcePositionBuffer0", _source.previousPositionBuffer);
                _kernel.material.SetTexture("_SourcePositionBuffer1", _source.positionBuffer);

                // Invoke the position update kernel.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetLastBuffer(Buffers.Position));
                _kernel.material.SetTexture("_VelocityBuffer", _kernel.GetLastBuffer(Buffers.Velocity));
                _kernel.material.SetFloat("_VelocityScale", _velocityScale);
                _kernel.Invoke(Kernels.UpdatePosition, Buffers.Position);

                // Invoke the velocity update kernel with the updated positions.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetWorkingBuffer(Buffers.Position));
                _kernel.Invoke(Kernels.UpdateVelocity, Buffers.Velocity);
            }

            _kernel.SwapBuffers();
        }
Ejemplo n.º 2
0
        void InvokeAnimationKernels()
        {
            if (_kernel == null)
            {
                _kernel = new AnimationKernelSet <Kernels, Buffers>(_kernelShader, x => (int)x, x => (int)x);
            }

            if (!_kernel.ready)
            {
                // Initialize the animation kernels and buffers.
                _kernel.Setup(_source.vertexCount, _template.historyLength);
                _kernel.material.SetTexture("_SourcePositionBuffer1", _source.positionBuffer);
                _kernel.material.SetFloat("_RandomSeed", _randomSeed);
                _kernel.Invoke(Kernels.InitializePosition, Buffers.Position);
                _kernel.Invoke(Kernels.InitializeVelocity, Buffers.Velocity);
                _kernel.Invoke(Kernels.InitializeOrthnorm, Buffers.Orthnorm);
            }
            else
            {
                // Transfer the source position attributes.
                _kernel.material.SetTexture("_SourcePositionBuffer0", _source.previousPositionBuffer);
                _kernel.material.SetTexture("_SourcePositionBuffer1", _source.positionBuffer);

                // Invoke the velocity update kernel.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetLastBuffer(Buffers.Position));
                _kernel.material.SetTexture("_VelocityBuffer", _kernel.GetLastBuffer(Buffers.Velocity));
                _kernel.material.SetFloat("_SpeedLimit", _speedLimit);
                _kernel.Invoke(Kernels.UpdateVelocity, Buffers.Velocity);

                // Invoke the position update kernel with the updated velocity.
                _kernel.material.SetTexture("_VelocityBuffer", _kernel.GetWorkingBuffer(Buffers.Velocity));
                _kernel.material.SetFloat("_Drag", Mathf.Exp(-_drag * Time.deltaTime));
                _kernel.Invoke(Kernels.UpdatePosition, Buffers.Position);

                // Invoke the orthonormal update kernel with the updated velocity.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetWorkingBuffer(Buffers.Position));
                _kernel.material.SetTexture("_OrthnormBuffer", _kernel.GetLastBuffer(Buffers.Orthnorm));
                _kernel.Invoke(Kernels.UpdateOrthnorm, Buffers.Orthnorm);
            }

            _kernel.SwapBuffers();
        }
        void InvokeAnimationKernels()
        {
            if (_kernel == null)
            {
                _kernel = new AnimationKernelSet <Kernels, Buffers>(_kernelShader, x => (int)x, x => (int)x);
            }

            if (!_kernel.ready)
            {
                // Initialize the animation kernels and buffers.
                _kernel.Setup(_template.instanceCount, 1);
                _kernel.material.SetTexture("_SourcePositionBuffer1", _source.positionBuffer);
                _kernel.material.SetFloat("_RandomSeed", _randomSeed);
                _kernel.Invoke(Kernels.InitializePosition, Buffers.Position);
                _kernel.Invoke(Kernels.InitializeVelocity, Buffers.Velocity);
                _kernel.Invoke(Kernels.InitializeRotation, Buffers.Rotation);
            }
            else
            {
                // Update kernel parameters.
                _kernel.material.SetVector("_Damper", new Vector2(
                                               Mathf.Exp(-_drag * Time.deltaTime), _speedLimit
                                               ));

                _kernel.material.SetVector("_Gravity", _gravity * Time.deltaTime);

                _kernel.material.SetVector("_Life", new Vector2(
                                               Time.deltaTime / _maxLife, Time.deltaTime / (_maxLife * _speedToLife)
                                               ));

                var pi360dt = Mathf.PI * Time.deltaTime / 360;
                _kernel.material.SetVector("_Spin", new Vector2(
                                               _maxSpin * pi360dt, _speedToSpin * pi360dt
                                               ));

                _kernel.material.SetVector("_NoiseParams", new Vector2(
                                               _noiseFrequency, _noiseAmplitude * Time.deltaTime
                                               ));

                // Move the noise field backward in the direction of the
                // gravity vector, or simply pull up if no gravity is set.
                var noiseDir = (_gravity == Vector3.zero) ? Vector3.up : _gravity.normalized;
                _noiseOffset += noiseDir * _noiseMotion * Time.deltaTime;
                _kernel.material.SetVector("_NoiseOffset", _noiseOffset);

                // Transfer the source position attributes.
                _kernel.material.SetTexture("_SourcePositionBuffer0", _source.previousPositionBuffer);
                _kernel.material.SetTexture("_SourcePositionBuffer1", _source.positionBuffer);

                // Invoke the position update kernel.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetLastBuffer(Buffers.Position));
                _kernel.material.SetTexture("_VelocityBuffer", _kernel.GetLastBuffer(Buffers.Velocity));
                _kernel.Invoke(Kernels.UpdatePosition, Buffers.Position);

                // Invoke the velocity update kernel with the updated positions.
                _kernel.material.SetTexture("_PositionBuffer", _kernel.GetWorkingBuffer(Buffers.Position));
                _kernel.Invoke(Kernels.UpdateVelocity, Buffers.Velocity);

                // Invoke the rotation update kernel with the updated velocity.
                _kernel.material.SetTexture("_RotationBuffer", _kernel.GetLastBuffer(Buffers.Rotation));
                _kernel.material.SetTexture("_VelocityBuffer", _kernel.GetWorkingBuffer(Buffers.Velocity));
                _kernel.Invoke(Kernels.UpdateRotation, Buffers.Rotation);
            }

            _kernel.SwapBuffers();
        }