private void UpdateParticleSystem(ParticleSystemComponentState state, float deltaTime)
        {
            var speed = state.ParticleSystemComponent.Speed;
            var transformComponent = state.TransformComponent;
            var particleSystem     = state.ParticleSystemComponent.ParticleSystem;

            // We must update the TRS location of the particle system prior to updating the system itself.
            // Particles only handle uniform scale.

            if (transformComponent.Parent is null)
            {
                // The transform doesn't have a parent. Local transform IS world transform

                particleSystem.Translation  = transformComponent.Position;  // Local position
                particleSystem.UniformScale = transformComponent.Scale.X;   // Local scale
                particleSystem.Rotation     = transformComponent.Rotation;  // Local rotation
            }
            else
            {
                transformComponent.WorldMatrix.Decompose(out _, out particleSystem.Rotation, out particleSystem.Translation);

                // Rotation breaks uniform scaling, so only inherit the X-scaling manually
                float xScale     = transformComponent.Scale.X;
                var   nextParent = transformComponent.Parent;
                while (nextParent != null)
                {
                    xScale    *= nextParent.Scale.X;
                    nextParent = nextParent.Parent;
                }
                particleSystem.UniformScale = xScale;
            }

            particleSystem.Update(deltaTime * speed);
        }
Exemple #2
0
        internal void UpdateParticleSystem(ParticleSystemComponentState state, float deltaTime)
        {
            var speed = state.ParticleSystemComponent.Speed;
            var transformComponent = state.TransformComponent;
            var particleSystem     = state.ParticleSystemComponent.ParticleSystem;

            // We must update the TRS location of the particle system prior to updating the system itself.
            // Particles only handle uniform scale.

            if (transformComponent.Parent == null)
            {
                // The transform doesn't have a parent. Local transform IS world transform

                particleSystem.Translation = transformComponent.Position;   // This is the local position!

                particleSystem.UniformScale = transformComponent.Scale.X;   // This is the local scale!

                particleSystem.Rotation = transformComponent.Rotation;      // This is the local rotation!
            }
            else
            {
                // TODO: Check transformComponent.WorldMatrix.Decompose(out scale, out rot, out pos);

                // The transform has a parent - do not use local transform values, instead check the world matrix

                // Position
                particleSystem.Translation = new Vector3(transformComponent.WorldMatrix.M41, transformComponent.WorldMatrix.M42, transformComponent.WorldMatrix.M43);

                // Scale
                var uniformScaleX = new Vector3(transformComponent.WorldMatrix.M11, transformComponent.WorldMatrix.M12, transformComponent.WorldMatrix.M13);
                particleSystem.UniformScale = uniformScaleX.Length();

                // Rotation
                // TODO Maybe implement Quaternion.RotationMatrix which also handles cases when the matrix has scaling?
                var invScl    = (particleSystem.UniformScale > 0) ? 1f / particleSystem.UniformScale : 1f;
                var rotMatrix = new Matrix(
                    transformComponent.WorldMatrix.M11 * invScl, transformComponent.WorldMatrix.M12 * invScl, transformComponent.WorldMatrix.M13 * invScl, 0,
                    transformComponent.WorldMatrix.M21 * invScl, transformComponent.WorldMatrix.M22 * invScl, transformComponent.WorldMatrix.M23 * invScl, 0,
                    transformComponent.WorldMatrix.M31 * invScl, transformComponent.WorldMatrix.M32 * invScl, transformComponent.WorldMatrix.M33 * invScl, 0,
                    0, 0, 0, 1);
                Quaternion.RotationMatrix(ref rotMatrix, out particleSystem.Rotation);
            }

            particleSystem.Update(deltaTime * speed);
        }
        public override void HandleComponentState(dynamic _state)
        {
            ParticleSystemComponentState state = (ParticleSystemComponentState)_state;

            foreach (var a in state.emitters)
            {
                if (_emitters.ContainsKey(a.Key))
                {
                    SetParticleSystemActive(a.Key, a.Value);
                }
                else
                {
                    AddParticleSystem(a.Key, a.Value);
                }
            }

            foreach (var toRemove in new List <string>(_emitters.Keys.Except <string>(state.emitters.Keys))) //Remove emitters that are not in the new state.
            {
                RemoveParticleSystem(toRemove);
            }
        }
 /// <inheritdoc />
 protected override void OnEntityComponentRemoved(Entity entity, ParticleSystemComponent component, ParticleSystemComponentState data)
 {
     // TODO: Reset low-level data only. This method also gets called when moving the entity in the hierarchy!
     // component.ParticleSystem.Dispose();
     component.ParticleSystem.ResetSimulation();
 }
 /// <inheritdoc />
 protected override void OnEntityComponentAdding(Entity entity, ParticleSystemComponent component, ParticleSystemComponentState data)
 {
     // Do some particle system initialization
 }
 /// <inheritdoc />
 protected override bool IsAssociatedDataValid(Entity entity, ParticleSystemComponent component, ParticleSystemComponentState associatedData)
 {
     return
         (component == associatedData.ParticleSystemComponent &&
          entity.Transform == associatedData.TransformComponent);
 }
Exemple #7
0
 /// <inheritdoc />
 protected override void OnEntityComponentRemoved(Entity entity, ParticleSystemComponent component, ParticleSystemComponentState data)
 {
     component.ParticleSystem.Dispose();
 }