Beispiel #1
0
        protected override void Start()
        {
            base.Start();

            _particleEffect = ContentManager.GetEffect <ParticleEffect>();

            _velocityType = VelocityTypeUtility.GetImplementation(Definition.VelocityType);
            _volumeType   = VolumeTypeUtility.GetImplementation(Definition.VolumeType);

            var texturePath = Path.Combine("Art", "Textures", Definition.ParticleName);

            _texture = ContentManager.Load <Texture>(texturePath, uploadBatch: null);

            var blendState = GetBlendState(Definition.Shader);

            _pipelineStateHandle = new EffectPipelineState(
                RasterizerStateDescription.CullBackSolid,
                DepthStencilStateDescription.DepthRead,
                blendState)
                                   .GetHandle();

            _initialDelay = Definition.InitialDelay.GetRandomInt();

            _startSizeRate = Definition.StartSizeRate.GetRandomFloat();
            _startSize     = 0;

            _colorKeyframes = new List <ParticleColorKeyframe>();

            if (Definition.Color1 != null)
            {
                _colorKeyframes.Add(new ParticleColorKeyframe(Definition.Color1));
            }

            void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous)
            {
                if (keyframe != null && keyframe.Time > previous.Time)
                {
                    _colorKeyframes.Add(new ParticleColorKeyframe(keyframe));
                }
            }

            addColorKeyframe(Definition.Color2, Definition.Color1);
            addColorKeyframe(Definition.Color3, Definition.Color2);
            addColorKeyframe(Definition.Color4, Definition.Color3);
            addColorKeyframe(Definition.Color5, Definition.Color4);
            addColorKeyframe(Definition.Color6, Definition.Color5);
            addColorKeyframe(Definition.Color7, Definition.Color6);
            addColorKeyframe(Definition.Color8, Definition.Color7);

            var maxParticles = CalculateMaxParticles();

            _particles = new Particle[maxParticles];
            for (var i = 0; i < _particles.Length; i++)
            {
                _particles[i].Dead = true;
            }

            _deadList = new List <int>();
            _deadList.AddRange(Enumerable.Range(0, maxParticles));

            _vertexBuffer = DynamicBuffer <ParticleVertex> .CreateArray(
                GraphicsDevice,
                maxParticles * 4,
                BufferUsageFlags.None);

            _vertices = new ParticleVertex[_vertexBuffer.ElementCount];

            _indexBuffer = CreateIndexBuffer(GraphicsDevice, maxParticles);

            State = ParticleSystemState.Active;
        }
Beispiel #2
0
        public void Update(GraphicsDevice graphicsDevice)
        {
            NumInstances = 0;

            foreach (var instancedRenderable in InstancedRenderables)
            {
                if (instancedRenderable.Visible)
                {
                    NumInstances += 1;
                }
            }

            if (NumInstances == 0)
            {
                return;
            }

            if (Mesh.Skinned)
            {
                var numElements = (int)(Mesh.NumBones * NumInstances);
                if (SkinningBuffer == null || SkinningBuffer.ElementCount < numElements)
                {
                    SkinningBuffer = DynamicBuffer <Matrix4x3> .CreateArray(graphicsDevice, numElements, BufferUsageFlags.None);

                    _skinningBones = new Matrix4x3[numElements];
                }

                var boneIndex = 0;
                foreach (var instancedRenderable in InstancedRenderables)
                {
                    if (instancedRenderable.Visible)
                    {
                        for (var i = 0; i < Mesh.NumBones; i++)
                        {
                            // Bone matrix should be relative to root bone transform.
                            var rootBoneMatrix = instancedRenderable.Bones[0].LocalToWorldMatrix;
                            var boneMatrix     = instancedRenderable.Bones[i].LocalToWorldMatrix;

                            var boneMatrixRelativeToRoot = boneMatrix * Matrix4x4Utility.Invert(rootBoneMatrix);

                            boneMatrixRelativeToRoot.ToMatrix4x3(out _skinningBones[boneIndex++]);
                        }
                    }
                }

                SkinningBuffer.UpdateData(_skinningBones);
            }

            if (WorldBuffer == null || WorldBuffer.ElementCount < NumInstances)
            {
                WorldBuffer = DynamicBuffer <Matrix4x4> .CreateArray(
                    graphicsDevice,
                    (int)NumInstances,
                    BufferUsageFlags.None);

                _worldTransforms = new Matrix4x4[NumInstances];
            }

            var worldTransformIndex = 0;

            foreach (var instancedRenderable in InstancedRenderables)
            {
                if (instancedRenderable.Visible)
                {
                    _worldTransforms[worldTransformIndex++] = instancedRenderable.Renderable.Transform.LocalToWorldMatrix;
                }
            }

            WorldBuffer.UpdateData(_worldTransforms);
        }