示例#1
0
        private void EmitNewParticle(ref Particle particle)
        {
            timeSinceEmit = 0;

            particle.Energy = particle.StartingEnergy = Random.Range(minEnergy, maxEnergy);

            Vector3 pointInUnitSphere = Random.insideUnitSphere;
            particle.Position = Microsoft.Xna.Framework.Vector3.Transform(ellipsoid, transform.rotation) * pointInUnitSphere.x;

            Vector3 velocity = Vector3.zero;

            if (useWorldSpace && gameObject.rigidbody != null && emitterVelocityScale > 0)
            {
                velocity += gameObject.rigidbody.velocity * emitterVelocityScale;
            }

            if (rndVelocity.x != 0 || rndVelocity.y != 0 || rndVelocity.z != 0)
            {
                velocity += Random.onUnitSphere * rndVelocity;
            }

            if (tangentVelocity.x != 0 || tangentVelocity.y != 0 || tangentVelocity.z != 0)
            {
                velocity += Random.onUnitSphere * tangentVelocity;
            }
            if (localVelocity.x != 0 || localVelocity.y != 0 || localVelocity.z != 0)
            {
                //velocity += (Vector3)(-Microsoft.Xna.Framework.Vector3.Transform(localVelocity, transform.rotation));
                velocity += (Vector3)(Microsoft.Xna.Framework.Vector3.Transform(localVelocity, transform.rotation));
            }

            if (worldVelocity.x != 0 || worldVelocity.y != 0 || worldVelocity.z != 0)
            {
                //velocity += (Vector3)(-Microsoft.Xna.Framework.Vector3.Transform(localVelocity, transform.rotation));
                velocity += worldVelocity;
            }

            particle.Velocity = velocity;

            if (useWorldSpace)
            {
                particle.Position += gameObject.transform.position;
            }

            particle.Size = Random.Range(minSize, maxSize);
            particle.Color = renderer.material.color;

            particle.RotationSpeed = Random.Range(minRotationSpeed, maxRotationSpeed);
            if (randomRotation)
            {
                particle.Rotation = MathHelper.TwoPi * Random.value;
            }

            int x = Random.Range(0, textureTileCountX);
            int y = Random.Range(0, textureTileCountY);
            particle.TextureScale = new Vector2(1.0f / textureTileCountX, 1.0f / textureTileCountY);
            particle.TextureOffset = new Vector2(x * particle.TextureScale.x, y * particle.TextureScale.y);
        }
示例#2
0
 public void UpdateParticleColor(ref Particle particle)
 {
     if (doesAnimateColor)
     {
         float colorScale = 1 - (particle.Energy / particle.StartingEnergy);
         float startIndex = colorScale * 4;
         int iStartIndex = (int)Mathf.Floor(startIndex);
         if (iStartIndex == 4)
         {
             iStartIndex = 3;
         }
         colorScale = startIndex - iStartIndex;
         Microsoft.Xna.Framework.Color nonPremul = Microsoft.Xna.Framework.Color.Lerp(_colorAnimation[iStartIndex], _colorAnimation[iStartIndex + 1], colorScale);
         particle.Color = Microsoft.Xna.Framework.Color.FromNonPremultiplied(nonPremul.R, nonPremul.G, nonPremul.B, nonPremul.A);
     }
     else
     {
         particle.Color = renderer.material.color;
     }
 }
示例#3
0
        private bool RenderParticle(Camera cam, int vertexIndex, int triangleIndex, ref Particle particle)
        {
            float size = particle.Size / 2;
            if (size <= 0)
            {
                return false;
            }

            Microsoft.Xna.Framework.Vector3 pos = particle.Position;
            if (!emitter.useWorldSpace)
            {
                pos += (Microsoft.Xna.Framework.Vector3)transform.position;
            }

            if (doViewportCulling)
            {
                BoundingSphere sphere = new BoundingSphere(pos, size);
                if (cam.DoFrustumCulling(ref sphere))
                {
                    return false;
                }
            }

            vertices[vertexIndex].TextureCoordinate = new Microsoft.Xna.Framework.Vector2(particle.TextureOffset.x, particle.TextureOffset.y + particle.TextureScale.y);
            vertices[vertexIndex].Color = particle.Color;
            vertices[vertexIndex + 1].TextureCoordinate = new Microsoft.Xna.Framework.Vector2(particle.TextureOffset.x, particle.TextureOffset.y);
            vertices[vertexIndex + 1].Color = particle.Color;
            vertices[vertexIndex + 2].TextureCoordinate = new Microsoft.Xna.Framework.Vector2(particle.TextureOffset.x + particle.TextureScale.x, particle.TextureOffset.y + particle.TextureScale.y);
            vertices[vertexIndex + 2].Color = particle.Color;
            vertices[vertexIndex + 3].TextureCoordinate = new Microsoft.Xna.Framework.Vector2(particle.TextureOffset.x + particle.TextureScale.x, particle.TextureOffset.y);
            vertices[vertexIndex + 3].Color = particle.Color;

            //rotated particles
            if (stretchParticles == StretchParticles.Billboard)
            {
                Microsoft.Xna.Framework.Vector3 particlePosition = particle.Position;

                Matrix.CreateBillboard(
                    ref particlePosition,
                    ref camPosition,
                    ref camUpVector,
                    camForwardVector,
                    out m
                    );

                Microsoft.Xna.Framework.Vector3 vertical = new Microsoft.Xna.Framework.Vector3(0, -size, 0);
                Microsoft.Xna.Framework.Vector3 horizontal = new Microsoft.Xna.Framework.Vector3(size, 0, 0);

                m.Translation = Microsoft.Xna.Framework.Vector3.Zero;
                //Microsoft.Xna.Framework.Vector3.Transform(ref p, ref m, out p);

                Microsoft.Xna.Framework.Vector3.Transform(ref vertical, ref m, out vertical);
                Microsoft.Xna.Framework.Vector3.Transform(ref horizontal, ref m, out horizontal);

                vertices[vertexIndex].Position = pos - vertical + horizontal;
                vertices[vertexIndex+1].Position = pos - vertical - horizontal;
                vertices[vertexIndex+2].Position = pos + vertical + horizontal;
                vertices[vertexIndex+3].Position = pos + vertical - horizontal;
            }

            if (stretchParticles == StretchParticles.Stretched)
            {
                size *= 2;
                Microsoft.Xna.Framework.Vector3 particlePosition = particle.Position;

                //TODO make this work correctly, and add velocityScale functionality.
                Microsoft.Xna.Framework.Vector3 vertical = -particle.Velocity;
                vertical.Normalize();
                vertical *= size * lengthScale;
                Microsoft.Xna.Framework.Vector3 horizontal = -Microsoft.Xna.Framework.Vector3.Cross(vertical, camForwardVector);
                horizontal.Normalize();
                horizontal *= size;

                vertices[vertexIndex].Position = pos - vertical + horizontal;
                vertices[vertexIndex + 1].Position = pos - vertical - horizontal;
                vertices[vertexIndex + 2].Position = pos + vertical + horizontal;
                vertices[vertexIndex + 3].Position = pos + vertical - horizontal;
            }

            if (stretchParticles == StretchParticles.HorizontalBillboard)
            {
                vertices[vertexIndex].Position = pos + new Microsoft.Xna.Framework.Vector3(-size, vertexIndex * 0.0001f, size);
                vertices[vertexIndex + 1].Position = pos + new Microsoft.Xna.Framework.Vector3(-size, vertexIndex * 0.0001f, -size);
                vertices[vertexIndex + 2].Position = pos + new Microsoft.Xna.Framework.Vector3(size, vertexIndex * 0.0001f, size);
                vertices[vertexIndex + 3].Position = pos + new Microsoft.Xna.Framework.Vector3(size, vertexIndex * 0.0001f, -size);
            }

            return true;
        }