public unsafe int UpdateBuffer() { int quadIndex = 0; fixed(ParticleVertex *pQuads = _quads) { fixed(Particle *pParticles = _particlesBuffer) { for (int i = 0, len = _particlesBuffer.Length; i < len; i++) { int k = 4 * quadIndex++; Particle *ptc = pParticles + i; for (int j = 0; j < 4; j++) { ParticleVertex *vertex = pQuads + k + j; vertex->PositionPtc = ptc->GlobalPosition; vertex->Alpha = ptc->Alpha; vertex->Color = ptc->Color; vertex->Size = ptc->Size; } } } _vb.Write(_quads); } return(quadIndex); }
/// <summary> /// Performs rendering of particles. /// </summary> /// <param name="iterator">The particle iterator object.</param> /// <param name="context">The render context containing rendering information.</param> protected override void Render(ref RenderContext context, ref ParticleIterator iterator) { Int32 vertexCount = 0; Vector3 cameraPos = context.CameraPosition; Vector3 rotationAxis = context.BillboardRotationalAxis; bool squareTexture = (context.Texture.Height == context.Texture.Width); float aspectRatio = context.Texture.Height / (float)context.Texture.Width ; SetDataOptions setDataOptions = SetDataOptions.NoOverwrite; //Use the SpriteBatch style of filling buffers //http://blogs.msdn.com/b/shawnhar/archive/2010/07/07/setdataoptions-nooverwrite-versus-discard.aspx if (_vertexBufferPosition + context.Count * VerticesPerParticle > NumVertices) { //Too much to fit in the remaining space - start at the beginning and discard _vertexBufferPosition = 0; setDataOptions = SetDataOptions.Discard; } #if UNSAFE unsafe { fixed (ParticleVertex* vertexArray = Vertices) { ParticleVertex* verts = vertexArray +_vertexBufferPosition; #else int vertexIndex = _vertexBufferPosition; #endif var particle = iterator.First; do { #if UNSAFE Single scale = particle->Scale; Vector3 position = particle->Position; Vector3 rotation = particle->Rotation; Vector4 colour = particle->Colour; //Work out our world transform - and set a flag to avoid some multiplies if world ends up as zero bool worldIsNotIdentity = true; Matrix effectWorld; if (context.WorldIsIdentity) { if (particle->EffectProxyIndex > 0) { effectWorld = ParticleEffectProxy.Proxies[particle->EffectProxyIndex].World; } else { worldIsNotIdentity = false; effectWorld = Matrix.Identity; //Makes the compiler happy though we will never actually use it. } } else { effectWorld = particle->EffectProxyIndex > 0 ? ParticleEffectProxy.Proxies[particle->EffectProxyIndex].FinalWorld : context.World; } #else Single scale = particle.Scale; Vector3 position = particle.Position; Vector3 rotation = particle.Rotation; Vector4 colour = particle.Colour; //If we have a proxy then multiply in the proxy world matrix bool worldIsNotIdentity = true; Matrix effectWorld; if (context.WorldIsIdentity) { if (particle.EffectProxyIndex > 0) { effectWorld = ParticleEffectProxy.Proxies[particle.EffectProxyIndex].World; } else { worldIsNotIdentity = false; effectWorld = Matrix.Identity; //Makes the compiler happy though we will never actually use it. } } else { effectWorld = particle.EffectProxyIndex > 0 ? ParticleEffectProxy.Proxies[particle.EffectProxyIndex].FinalWorld : context.World; } #endif //Individual particle transformations - scale and rotation //The Rotation setup will fill in the top 3x3 so we just need to initialise 44 var transform = new Matrix {M44 = 1}; float scaleX = scale; float scaleY = squareTexture ? scale : scale * aspectRatio; //ScaleZ is always 1 so no need multiple into M_3 //Inline the rotation and scale calculations and do each element in one go //Fast rotation matrix - see http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations //This set matches //Matrix temp2 = Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z); //Matches math od Rotx*RotY*RotZ //var cosX = (float) Math.Cos(rotation.X); //var cosY = (float) Math.Cos(rotation.Y); //var cosZ = (float) Math.Cos(rotation.Z); //var sinX = (float) Math.Sin(rotation.X); //var sinY = (float) Math.Sin(rotation.Y); //var sinZ = (float) Math.Sin(rotation.Z); //transform.M11 = cosY*cosZ; //transform.M12 = cosY * sinZ; //transform.M13 = -sinY; //transform.M21 = sinX*sinY*cosZ - cosX * sinZ; //transform.M22 = sinX*sinY*sinZ + cosX*cosZ; //transform.M23 = sinX*cosY; //transform.M31 = cosX*sinY*cosZ + sinX * sinZ; //transform.M32 = cosX*sinY*sinZ - sinX * cosZ; //transform.M33 = cosX * cosY; //This set matches //Matrix temp2 = Matrix.CreateScale(new VEctor3(scaleX, scaleY,1) * Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateRotationX(rotation.Y) * Matrix.CreateRotationY(rotation.X) ; //Matches YawPitchRoll order //Matrix temp = Matrix.CreateScale(new VEctor3(scaleX, scaleY,1) * Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z); //TODO - can we optimise out a rotation e.g.fast path if rotation.Y=0 etc //TODO - can we optimise out rotation(s) if we know its a billboard? That overwrites much of the transform var cosX = (float)Math.Cos(rotation.Y); var cosY = (float)Math.Cos(rotation.X); var cosZ = (float)Math.Cos(rotation.Z); var sinX = (float)Math.Sin(rotation.Y); var sinY = (float)Math.Sin(rotation.X); var sinZ = (float)Math.Sin(rotation.Z); var cosYcosZ = cosY*cosZ; var cosYsinZ = cosY*sinZ; var sinXsinY = sinX*sinY; transform.M11 = (cosYcosZ + sinXsinY * sinZ) * scaleX; transform.M12 = (cosX * sinZ) * scaleX; transform.M13 = (sinX * cosYsinZ - sinY * cosZ) * scaleX; transform.M21 = (sinXsinY * cosZ - cosYsinZ) * scaleY; transform.M22 = (cosX * cosZ) *scaleY; transform.M23 = (sinY * sinZ + sinX * cosYcosZ) * scaleY; transform.M31 = cosX * sinY; transform.M32 = -sinX; transform.M33 = cosX * cosY; switch (context.BillboardStyle) { case BillboardStyle.None: //Position the particle without a multiplication! transform.M41 = position.X; transform.M42 = position.Y; transform.M43 = position.Z; //Just apply the world //TODO - we can just do this in Basic effect instead of per vertex - only if there is no proxy, sort of a fast path! if (worldIsNotIdentity) { Matrix.Multiply(ref transform, ref effectWorld, out transform); } break; default: //Its billboarded Vector3 worldPos; if (worldIsNotIdentity) { Vector3.Transform(ref position, ref effectWorld, out worldPos); } else { worldPos = position; } //Apply the billboard (which includes the world translation) Matrix billboardMatrix; if (context.BillboardStyle == BillboardStyle.Spherical) { //Spherical billboards (always face the camera) Matrix.CreateBillboard(ref worldPos, ref cameraPos, ref Up, Forward, out billboardMatrix); } else { //HACK: For progenitor DBP use the velocity as the axis for a per particle axis if (context.UseVelocityAsBillboardAxis) { #if UNSAFE Matrix.CreateConstrainedBillboard(ref worldPos, ref cameraPos, ref particle->Velocity, Forward, null, out billboardMatrix); #else Matrix.CreateConstrainedBillboard(ref worldPos, ref cameraPos, ref particle.Velocity, Forward, null, out billboardMatrix); #endif } else { //Cylindrical billboards have a vector they are allowed to rotate around Matrix.CreateConstrainedBillboard(ref worldPos, ref cameraPos, ref rotationAxis, Forward, null, out billboardMatrix); } } Matrix.Multiply(ref transform, ref billboardMatrix, out transform); break; } Vector3 v1; Vector3 v2; Vector3 v3; Vector3 v4; Vector3.Transform(ref inv1, ref transform, out v1); Vector3.Transform(ref inv2, ref transform, out v2); Vector3.Transform(ref inv3, ref transform, out v3); Vector3.Transform(ref inv4, ref transform, out v4); #if UNSAFE //inline particle value assignments - removes 4 calls with their parameters and its a struct anyway verts->Position.X = v1.X; verts->Position.Y = v1.Y; verts->Position.Z = v1.Z; verts->Colour.X = colour.X; verts->Colour.Y = colour.Y; verts->Colour.Z = colour.Z; verts->Colour.W = colour.W; verts++; verts->Position.X = v2.X; verts->Position.Y = v2.Y; verts->Position.Z = v2.Z; verts->Colour.X = colour.X; verts->Colour.Y = colour.Y; verts->Colour.Z = colour.Z; verts->Colour.W = colour.W; verts++; verts->Position.X = v3.X; verts->Position.Y = v3.Y; verts->Position.Z = v3.Z; verts->Colour.X = colour.X; verts->Colour.Y = colour.Y; verts->Colour.Z = colour.Z; verts->Colour.W = colour.W; verts++; verts->Position.X = v4.X; verts->Position.Y = v4.Y; verts->Position.Z = v4.Z; verts->Colour.X = colour.X; verts->Colour.Y = colour.Y; verts->Colour.Z = colour.Z; verts->Colour.W = colour.W; verts++; #else //inline particle value assignments - removes 4 calls with their parameters and its a struct anyway this.Vertices[vertexIndex].Position = v1; this.Vertices[vertexIndex].Colour = colour; this.Vertices[vertexIndex++].TextureCoordinate = uv1; this.Vertices[vertexIndex].Position = v2; this.Vertices[vertexIndex].Colour = colour; this.Vertices[vertexIndex++].TextureCoordinate = uv2; this.Vertices[vertexIndex].Position = v3; this.Vertices[vertexIndex].Colour = colour; this.Vertices[vertexIndex++].TextureCoordinate = uv3; this.Vertices[vertexIndex].Position = v4; this.Vertices[vertexIndex].Colour = colour; this.Vertices[vertexIndex++].TextureCoordinate = uv4; #endif vertexCount += 4; } #if UNSAFE while (iterator.MoveNext(&particle)); #else while (iterator.MoveNext(ref particle)); #endif base.GraphicsDeviceService.GraphicsDevice.BlendState = context.BlendState; this.BasicEffect.Texture = context.Texture; //Xbox need the vertex buffer to be set to null before SetData is called //Windows does not //TODO: Is this a bug? see http://forums.create.msdn.com/forums/p/61885/399495.aspx#399495 #if XBOX if (setDataOptions == SetDataOptions.Discard) { base.GraphicsDeviceService.GraphicsDevice.SetVertexBuffer(null); } #endif _vertexBuffer.SetData(_vertexBufferPosition * ParticleVertex.Size, Vertices, _vertexBufferPosition, vertexCount, ParticleVertex.Size, setDataOptions); Debug.WriteLine(String.Format("position: {0} Count: {1} Hint: {2}", _vertexBufferPosition, vertexCount, setDataOptions)); base.GraphicsDeviceService.GraphicsDevice.SetVertexBuffer(_vertexBuffer); foreach (EffectPass pass in this.BasicEffect.CurrentTechnique.Passes) { pass.Apply(); base.GraphicsDeviceService.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, _vertexBufferPosition, vertexCount, _vertexBufferPosition/4*6, vertexCount/2); } //Move to the next free part of the array _vertexBufferPosition += vertexCount; #if UNSAFE } } #endif }