protected override void Initialize() { IsMouseVisible = true; map = new Map (new Vector3(400, 200, 0)); cam = new Camera(); player = EntityFactory.GetPlayer(); player.Postion = new Vector3(150, 200, 0); gameWorld = new World(); gameWorld.AddEntity(player); gameWorld.Map = map; render = new Renderer(); render.Groups.Add( new RenderGroup(400000, "atlas", map) ); render.Groups.Add( entitygroup = new RenderGroup(200, "char1", player ) ); render.Groups.Add( itemgroup = new RenderGroup(2000, "char1") {TileCount = 32 } ); //enemy.SetType(new NodeType { Texture = new Vector3(0, 2, 0) }); GraphicsDevice.BlendState = BlendState.AlphaBlend; GraphicsDevice.DepthStencilState = DepthStencilState.None; map.Init(); base.Initialize(); }
public Renderer() { planeCam = new Camera { View = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.Up), Projection = Matrix.CreateOrthographic(1f, 1f, 1f, 100.0f)}; Device = Game1.Instance.GraphicsDevice; instanceVertexBuffer = new DynamicVertexBuffer( Game1.Instance.GraphicsDevice, typeof(InstanceData), 1, BufferUsage.WriteOnly); instanceVertexBuffer.SetData( new []{new InstanceData { World = Matrix.Identity }}, 0, 1, SetDataOptions.Discard); }
public void Update(Camera cam) { int startX = (int)MathHelper.Clamp(cam.Position.X - cam.ViewSize.X / 2 - 15, 0, X); int endX = (int)MathHelper.Clamp(cam.Position.X + cam.ViewSize.X / 2 + 15, 0, X); int startY = (int)MathHelper.Clamp(cam.Position.Y - cam.ViewSize.Y / 2 - 15, 0, Y); int endY = (int)MathHelper.Clamp(cam.Position.Y + cam.ViewSize.Y / 2 + 15, 0, Y); var area = new BoundingBox(new Vector3(startX, startY, 0), new Vector3(endX, endY, 0)); PreUpdate(area); for (int i1 = startX; i1 < endX; i1++) { for (int i2 = startY; i2 < endY; i2++) { var node = ForeGround[i1, i2]; foreach (var component in components) component.Update(node); } } }
public void Update(Camera cam) { Camera = cam; GetInstanceData(); if (InstanceCount == 0) return; if ((instanceVertexBuffer == null) || (InstanceCount > instanceVertexBuffer.VertexCount)) { if (instanceVertexBuffer != null) instanceVertexBuffer.Dispose(); instanceVertexBuffer = new DynamicVertexBuffer( Game1.Instance.GraphicsDevice, typeof(InstanceData), InstanceCount, BufferUsage.WriteOnly); } instanceVertexBuffer.SetData(instances, 0, InstanceCount, SetDataOptions.Discard); }
private Vector2 toScreenSpace(Vector3 r, Camera cam ) { r = Vector3.Transform(r, cam.View); r = Vector3.Transform(r, cam.Projection); return new Vector2(r.X / 2 + .5f, -r.Y / 2 + 0.5f); }
private void RenderShadows(ModelMeshPart meshPart, Effect effect, Camera cam) { for (int i = 0; i < lights.Count; i++) { Vector3 r = lights[i].Postion; if (Vector3.Distance(r, cam.Target) > cam.ViewSize.Length()) continue; Device.SetRenderTarget(zmaps[i]); effect.Parameters["Light"].SetValue(toScreenSpace(r, cam)); effect.Parameters["Distance"].SetValue(0.5f); effect.Parameters["Shadow"].SetValue(opacity); effect.CurrentTechnique = effect.Techniques["ZMap"]; effect.CurrentTechnique.Passes[1].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, 1); } Device.SetRenderTarget(shadow); Device.Clear(Color.Black); for (int i = 0; i < lights.Count; i++) { Vector3 r = lights[i].Postion; if (Vector3.Distance(r, cam.Target) > cam.ViewSize.Length()) continue; effect.Parameters["Light"].SetValue(toScreenSpace(r, cam)); effect.Parameters["Distance"].SetValue(0.5f); effect.Parameters["Shadow"].SetValue(zmaps[i]); effect.CurrentTechnique = effect.Techniques["ShadowAccum"]; effect.CurrentTechnique.Passes[0].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, 1); } }
private void RenderInstances2(ModelMeshPart meshPart, Effect effect, Camera cam) { effect.Parameters["View"].SetValue(cam.View); effect.Parameters["Projection"].SetValue(cam.Projection); Device.Indices = meshPart.IndexBuffer; effect.CurrentTechnique = effect.Techniques["Wire"]; Device.Clear(Color.Black); foreach (var group in Groups) { group.Update(cam); if (group.InstanceCount == 0) continue; effect.Parameters["Texture"].SetValue(group.Texture); effect.Parameters["TileCount"].SetValue(group.TileCount); Device.SetVertexBuffers( new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0), new VertexBufferBinding(group.instanceVertexBuffer, 0, 1) ); effect.CurrentTechnique.Passes[0].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, group.InstanceCount); } }
private void RenderInstances(ModelMeshPart meshPart, Effect effect, Camera cam) { effect.Parameters["View"].SetValue(planeCam.View); effect.Parameters["Projection"].SetValue(planeCam.Projection); effect.Parameters["Offset"].SetValue(planeCam.Offset); effect.Parameters["skycolor"].SetValue(Game1.Instance.gameWorld.SkyColor); effect.Parameters["downcolor"].SetValue(Game1.Instance.gameWorld.SunColor); Device.Indices = meshPart.IndexBuffer; Device.SetRenderTargets(color, opacity, ambience); Device.Clear(new Color(1f, 1f, 1f, 0f)); effect.CurrentTechnique = effect.Techniques["Sky"]; Device.SetVertexBuffers( new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0), new VertexBufferBinding(instanceVertexBuffer, 0, 1) ); effect.CurrentTechnique.Passes[0].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, 1); effect.Parameters["View"].SetValue(cam.View); effect.Parameters["Projection"].SetValue(cam.Projection); effect.Parameters["Offset"].SetValue(cam.Offset); effect.CurrentTechnique = effect.Techniques["Color"]; foreach (var group in Groups) { group.Update(cam); if (group.InstanceCount == 0) continue; effect.Parameters["Texture"].SetValue(group.Texture); effect.Parameters["TileCount"].SetValue(group.TileCount); Device.SetVertexBuffers( new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0), new VertexBufferBinding(group.instanceVertexBuffer, 0, 1) ); effect.CurrentTechnique.Passes[0].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, group.InstanceCount); } }
private void render(ModelMeshPart meshPart, Camera cam) { // Set up the instance rendering effect. Effect effect = meshPart.Effect; RenderInstances(meshPart, effect, cam); Device.SetVertexBuffers( new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0), new VertexBufferBinding(instanceVertexBuffer, 0, 1) ); effect.Parameters["View"].SetValue(planeCam.View); effect.Parameters["Projection"].SetValue(planeCam.Projection); effect.Parameters["Offset"].SetValue(planeCam.Offset); RenderShadows(meshPart, effect, cam); Device.SetRenderTarget(null); effect.Parameters["Texture"].SetValue(color); effect.Parameters["Ambient"].SetValue(ambience); effect.Parameters["Ambient2"].SetValue(opacity); effect.Parameters["Shadow"].SetValue(shadow); effect.CurrentTechnique = effect.Techniques["Final"]; effect.CurrentTechnique.Passes[0].Apply(); Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, 1); //using (System.IO.Stream s = System.IO.File.OpenWrite("D:\\sh.png")) // shadow.SaveAsPng(s, shadow.Width, shadow.Height); }
public void Draw(Camera cam) { render(model.Meshes[0].MeshParts[0], cam); }