public void RemoveTransform(int index) { if (index >= 0 && index < LocalTransforms.Count) { LocalTransforms.RemoveAt(index); Rotations.RemoveAt(index); Tints.RemoveAt(index); } Primitive = new BatchBillboardPrimitive(graphicsDevice, SpriteSheet.GetTexture(), Width, Height, Frame, 1.0f, 1.0f, false, LocalTransforms, Tints, Colors); }
public void SetFrame(SpriteSheet Sheet, Rectangle Rect, float Width, float Height, Color Color, Color VertColor, bool Flipped) { var texture = Sheet.GetTexture(); var tileBounds = new Vector4( ((float)Rect.X / texture.Width) + 0.001f, ((float)Rect.Y / texture.Height) + 0.001f, ((float)Rect.Right / texture.Width) - 0.001f, ((float)Rect.Bottom / texture.Height) - 0.001f); Vertices[0] = new ExtendedVertex( new Vector3(-0.5f * Width, 0.5f * Height, 0.0f), Color, VertColor, new Vector2((float)Rect.Right / texture.Width, (float)Rect.Y / texture.Height), tileBounds); Vertices[1] = new ExtendedVertex( new Vector3(-0.5f * Width, -0.5f * Height, 0.0f), Color, VertColor, new Vector2((float)Rect.Right / texture.Width, (float)Rect.Bottom / texture.Height), tileBounds); Vertices[2] = new ExtendedVertex( new Vector3(0.5f * Width, 0.5f * Height, 0.0f), Color, VertColor, new Vector2((float)Rect.X / texture.Width, (float)Rect.Y / texture.Height), tileBounds); Vertices[3] = new ExtendedVertex( new Vector3(0.5f * Width, -0.5f * Height, 0.0f), Color, VertColor, new Vector2((float)Rect.X / texture.Width, (float)Rect.Bottom / texture.Height), tileBounds); //GameState.Game.GraphicsDevice.SetVertexBuffer(null); if (VertexBuffer == null) { ResetBuffer(GameState.Game.GraphicsDevice); } else { var buffers = GameState.Game.GraphicsDevice.GetVertexBuffers(); if (buffers.Any(buffer => buffer.VertexBuffer == VertexBuffer)) { GameState.Game.GraphicsDevice.SetVertexBuffer(null); } VertexBuffer.SetData(Vertices); } }
public override void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Effect effect, bool renderingForWater) { if (Primitive == null) { Primitive = new BatchBillboardPrimitive(graphicsDevice, SpriteSheet.GetTexture(), Width, Height, Frame, 1.0f, 1.0f, false, LocalTransforms, Tints, Colors); } if (IsVisible && ShouldDraw(camera)) { if (!LightsWithVoxels) { effect.Parameters["xTint"].SetValue(new Vector4(1, 1, 1, 1)); } else { effect.Parameters["xTint"].SetValue(Tint.ToVector4()); } RasterizerState r = graphicsDevice.RasterizerState; graphicsDevice.RasterizerState = rasterState; effect.Parameters["xTexture"].SetValue(SpriteSheet.GetTexture()); DepthStencilState origDepthStencil = graphicsDevice.DepthStencilState; DepthStencilState newDepthStencil = DepthStencilState.DepthRead; graphicsDevice.DepthStencilState = newDepthStencil; //Matrix oldWorld = effect.Parameters["xWorld"].GetValueMatrix(); effect.Parameters["xWorld"].SetValue(GlobalTransform); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); Primitive.Render(graphicsDevice); } effect.Parameters["xWorld"].SetValue(Matrix.Identity); if (origDepthStencil != null) { graphicsDevice.DepthStencilState = origDepthStencil; } if (r != null) { graphicsDevice.RasterizerState = r; } } }
public void RebuildPrimitive() { lock (Primitive.VertexBuffer) { if (Primitive != null && Primitive.VertexBuffer != null) { Primitive.VertexBuffer.Dispose(); } } Primitive = new BatchBillboardPrimitive(graphicsDevice, SpriteSheet.GetTexture(), Width, Height, Frame, 1.0f, 1.0f, false, LocalTransforms, Tints, Colors); }
public override void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Effect effect, bool renderingForWater) { base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater); if(!IsVisible) { return; } if (CurrentAnimation != null && CurrentAnimation.CurrentFrame >= 0 && CurrentAnimation.CurrentFrame < CurrentAnimation.Primitives.Count) { CurrentAnimation.PreRender(); SpriteSheet = CurrentAnimation.SpriteSheet; effect.Parameters["xTexture"].SetValue(SpriteSheet.GetTexture()); if(OrientationType != OrientMode.Fixed) { if(camera.Projection == Camera.ProjectionMode.Perspective) { if(OrientationType == OrientMode.Spherical) { float xscale = GlobalTransform.Left.Length(); float yscale = GlobalTransform.Up.Length(); float zscale = GlobalTransform.Forward.Length(); Matrix rot = Matrix.CreateRotationZ(BillboardRotation); Matrix bill = Matrix.CreateBillboard(GlobalTransform.Translation, camera.Position, camera.UpVector, null); Matrix noTransBill = bill; noTransBill.Translation = Vector3.Zero; Matrix worldRot = Matrix.CreateScale(new Vector3(xscale, yscale, zscale)) * rot * noTransBill; worldRot.Translation = bill.Translation; effect.Parameters["xWorld"].SetValue(worldRot); } else { Vector3 axis = Vector3.Zero; switch(OrientationType) { case OrientMode.XAxis: axis = Vector3.UnitX; break; case OrientMode.YAxis: axis = Vector3.UnitY; break; case OrientMode.ZAxis: axis = Vector3.UnitZ; break; } Matrix worldRot = Matrix.CreateConstrainedBillboard(GlobalTransform.Translation, camera.Position, axis, null, null); effect.Parameters["xWorld"].SetValue(worldRot); } } else { Matrix rotation = Matrix.CreateRotationY(-(float) Math.PI * 0.25f) * Matrix.CreateTranslation(GlobalTransform.Translation); effect.Parameters["xWorld"].SetValue(rotation); } } else { effect.Parameters["xWorld"].SetValue(GlobalTransform); } foreach(EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } } }
public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater) { if (!IsVisible) { return; } if (CurrentAnimation == null || CurrentAnimation.CurrentFrame < 0 || CurrentAnimation.CurrentFrame >= CurrentAnimation.Primitives.Count) { return; } GamePerformance.Instance.StartTrackPerformance("Render - Sprite"); // Everything that draws should set it's tint, making this pointless. Color origTint = effect.VertexColorTint; ApplyTintingToEffect(effect); CurrentAnimation.PreRender(); SpriteSheet = CurrentAnimation.SpriteSheet; var currDistortion = VertexNoise.GetNoiseVectorFromRepeatingTexture(GlobalTransform.Translation); var distortion = currDistortion * 0.1f + prevDistortion * 0.9f; prevDistortion = distortion; switch (OrientationType) { case OrientMode.Spherical: { Matrix bill = Matrix.CreateBillboard(GlobalTransform.Translation, camera.Position, camera.UpVector, null) * Matrix.CreateTranslation(distortion); //Matrix noTransBill = bill; //noTransBill.Translation = Vector3.Zero; //Matrix worldRot = noTransBill; //worldRot.Translation = bill.Translation;// + VertexNoise.GetNoiseVectorFromRepeatingTexture(bill.Translation); effect.World = bill; break; } case OrientMode.Fixed: { Matrix rotation = GlobalTransform; rotation.Translation = rotation.Translation + distortion; effect.World = rotation; break; } case OrientMode.YAxis: { Matrix worldRot = Matrix.CreateConstrainedBillboard(GlobalTransform.Translation, camera.Position, Vector3.UnitY, null, null); worldRot.Translation = worldRot.Translation + distortion; effect.World = worldRot; break; } } effect.MainTexture = SpriteSheet.GetTexture(); if (DrawSilhouette) { Color oldTint = effect.VertexColorTint; effect.VertexColorTint = SilhouetteColor; graphicsDevice.DepthStencilState = DepthStencilState.None; var oldTechnique = effect.CurrentTechnique; effect.CurrentTechnique = effect.Techniques[Shader.Technique.Silhouette]; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } graphicsDevice.DepthStencilState = DepthStencilState.Default; effect.VertexColorTint = oldTint; effect.CurrentTechnique = oldTechnique; } if (EnableWind) { effect.EnableWind = true; } foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } effect.VertexColorTint = origTint; effect.EnableWind = false; GamePerformance.Instance.StopTrackPerformance("Render - Sprite"); }
public override void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Effect effect, bool renderingForWater) { base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater); if (!IsVisible) { return; } if (CurrentAnimation != null && CurrentAnimation.CurrentFrame >= 0 && CurrentAnimation.CurrentFrame < CurrentAnimation.Primitives.Count) { CurrentAnimation.PreRender(); SpriteSheet = CurrentAnimation.SpriteSheet; effect.Parameters["xTexture"].SetValue(SpriteSheet.GetTexture()); if (OrientationType != OrientMode.Fixed) { if (camera.Projection == Camera.ProjectionMode.Perspective) { if (OrientationType == OrientMode.Spherical) { float xscale = GlobalTransform.Left.Length(); float yscale = GlobalTransform.Up.Length(); float zscale = GlobalTransform.Forward.Length(); Matrix rot = Matrix.CreateRotationZ(BillboardRotation); Matrix bill = Matrix.CreateBillboard(GlobalTransform.Translation, camera.Position, camera.UpVector, null); Matrix noTransBill = bill; noTransBill.Translation = Vector3.Zero; Matrix worldRot = Matrix.CreateScale(new Vector3(xscale, yscale, zscale)) * rot * noTransBill; worldRot.Translation = bill.Translation; effect.Parameters["xWorld"].SetValue(worldRot); } else { Vector3 axis = Vector3.Zero; switch (OrientationType) { case OrientMode.XAxis: axis = Vector3.UnitX; break; case OrientMode.YAxis: axis = Vector3.UnitY; break; case OrientMode.ZAxis: axis = Vector3.UnitZ; break; } Matrix worldRot = Matrix.CreateConstrainedBillboard(GlobalTransform.Translation, camera.Position, axis, null, null); effect.Parameters["xWorld"].SetValue(worldRot); } } else { Matrix rotation = Matrix.CreateRotationY(-(float)Math.PI * 0.25f) * Matrix.CreateTranslation(GlobalTransform.Translation); effect.Parameters["xWorld"].SetValue(rotation); } } else { effect.Parameters["xWorld"].SetValue(GlobalTransform); } foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } } }
public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater) { if (!IsVisible) { return; } if (Verticies == null) { float normalizeX = Sheet.FrameWidth / (float)(Sheet.Width); float normalizeY = Sheet.FrameHeight / (float)(Sheet.Height); List <Vector2> uvs = new List <Vector2> { new Vector2(0.0f, 0.0f), new Vector2(1.0f, 0.0f), new Vector2(1.0f, 1.0f), new Vector2(0.0f, 1.0f) }; Vector2 pixelCoords = new Vector2(Frame.X * Sheet.FrameWidth, Frame.Y * Sheet.FrameHeight); Vector2 normalizedCoords = new Vector2(pixelCoords.X / (float)Sheet.Width, pixelCoords.Y / (float)Sheet.Height); var bounds = new Vector4(normalizedCoords.X + 0.001f, normalizedCoords.Y + 0.001f, normalizedCoords.X + normalizeX - 0.001f, normalizedCoords.Y + normalizeY - 0.001f); for (int vert = 0; vert < 4; vert++) { uvs[vert] = new Vector2(normalizedCoords.X + uvs[vert].X * normalizeX, normalizedCoords.Y + uvs[vert].Y * normalizeY); } Vector3 topLeftFront = new Vector3(-0.5f * WorldWidth, 0.5f * WorldHeight, 0.0f); Vector3 topRightFront = new Vector3(0.5f * WorldWidth, 0.5f * WorldHeight, 0.0f); Vector3 btmRightFront = new Vector3(0.5f * WorldWidth, -0.5f * WorldHeight, 0.0f); Vector3 btmLeftFront = new Vector3(-0.5f * WorldWidth, -0.5f * WorldHeight, 0.0f); Verticies = new[] { new ExtendedVertex(topLeftFront, Color.White, Color.White, uvs[0], bounds), // 0 new ExtendedVertex(topRightFront, Color.White, Color.White, uvs[1], bounds), // 1 new ExtendedVertex(btmRightFront, Color.White, Color.White, uvs[2], bounds), // 2 new ExtendedVertex(btmLeftFront, Color.White, Color.White, uvs[3], bounds) // 3 }; Indicies = new int[] { 0, 1, 3, 1, 2, 3 }; } GamePerformance.Instance.StartTrackPerformance("Render - Simple Sprite"); // Everything that draws should set it's tint, making this pointless. Color origTint = effect.VertexColorTint; ApplyTintingToEffect(effect); var currDistortion = VertexNoise.GetNoiseVectorFromRepeatingTexture(GlobalTransform.Translation); var distortion = currDistortion * 0.1f + prevDistortion * 0.9f; prevDistortion = distortion; switch (OrientationType) { case OrientMode.Spherical: { Matrix bill = Matrix.CreateBillboard(GlobalTransform.Translation, camera.Position, camera.UpVector, null) * Matrix.CreateTranslation(distortion); effect.World = bill; break; } case OrientMode.Fixed: { Matrix rotation = GlobalTransform; rotation.Translation = rotation.Translation + distortion; effect.World = rotation; break; } case OrientMode.YAxis: { Matrix worldRot = Matrix.CreateConstrainedBillboard(GlobalTransform.Translation, camera.Position, Vector3.UnitY, null, null); worldRot.Translation = worldRot.Translation + distortion; effect.World = worldRot; break; } } effect.MainTexture = Sheet.GetTexture(); if (DrawSilhouette) { Color oldTint = effect.VertexColorTint; effect.VertexColorTint = SilhouetteColor; graphicsDevice.DepthStencilState = DepthStencilState.None; var oldTechnique = effect.CurrentTechnique; effect.CurrentTechnique = effect.Techniques[Shader.Technique.Silhouette]; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, Verticies, 0, 4, Indicies, 0, 2); } graphicsDevice.DepthStencilState = DepthStencilState.Default; effect.VertexColorTint = oldTint; effect.CurrentTechnique = oldTechnique; } effect.EnableWind = EnableWind; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, Verticies, 0, 4, Indicies, 0, 2); } effect.VertexColorTint = origTint; effect.EnableWind = false; GamePerformance.Instance.StopTrackPerformance("Render - Simple Sprite"); }
public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Shader effect, bool renderingForWater) { ApplyTintingToEffect(effect); if (!IsVisible) { return; } if (CurrentAnimation != null && CurrentAnimation.CurrentFrame >= 0 && CurrentAnimation.CurrentFrame < CurrentAnimation.Primitives.Count) { CurrentAnimation.PreRender(); SpriteSheet = CurrentAnimation.SpriteSheet; if (OrientationType != OrientMode.Fixed) { if (camera.Projection == Camera.ProjectionMode.Perspective) { if (OrientationType == OrientMode.Spherical) { float xscale = GlobalTransform.Left.Length(); float yscale = GlobalTransform.Up.Length(); float zscale = GlobalTransform.Forward.Length(); Matrix rot = Matrix.CreateRotationZ(BillboardRotation); Matrix bill = Matrix.CreateBillboard(GlobalTransform.Translation, camera.Position, camera.UpVector, null); Matrix noTransBill = bill; noTransBill.Translation = Vector3.Zero; Matrix worldRot = Matrix.CreateScale(new Vector3(xscale, yscale, zscale)) * rot * noTransBill; worldRot.Translation = DistortPosition ? bill.Translation + VertexNoise.GetNoiseVectorFromRepeatingTexture(bill.Translation) : bill.Translation; effect.World = worldRot; } else { Vector3 axis = Vector3.Zero; switch (OrientationType) { case OrientMode.XAxis: axis = Vector3.UnitX; break; case OrientMode.YAxis: axis = Vector3.UnitY; break; case OrientMode.ZAxis: axis = Vector3.UnitZ; break; } Matrix worldRot = Matrix.CreateConstrainedBillboard(GlobalTransform.Translation, camera.Position, axis, null, null); worldRot.Translation = DistortPosition ? worldRot.Translation + VertexNoise.GetNoiseVectorFromRepeatingTexture(worldRot.Translation) : worldRot.Translation; effect.World = worldRot; } } else { Matrix rotation = Matrix.CreateRotationY(-(float)Math.PI * 0.25f) * Matrix.CreateTranslation(GlobalTransform.Translation); rotation.Translation = DistortPosition ? rotation.Translation + VertexNoise.GetNoiseVectorFromRepeatingTexture(rotation.Translation) : rotation.Translation; effect.World = rotation; } } else { Matrix rotation = GlobalTransform; rotation.Translation = DistortPosition ? rotation.Translation + VertexNoise.GetNoiseVectorFromRepeatingTexture(rotation.Translation) : rotation.Translation; effect.World = rotation; } effect.MainTexture = SpriteSheet.GetTexture(); if (DrawSilhouette) { Color oldTint = effect.VertexColorTint; effect.VertexColorTint = SilhouetteColor; graphicsDevice.DepthStencilState = DepthStencilState.None; var oldTechnique = effect.CurrentTechnique; effect.CurrentTechnique = effect.Techniques[Shader.Technique.Silhouette]; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } graphicsDevice.DepthStencilState = DepthStencilState.Default; effect.VertexColorTint = oldTint; effect.CurrentTechnique = oldTechnique; } foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); CurrentAnimation.Primitives[CurrentAnimation.CurrentFrame].Render(graphicsDevice); } } }