public _2DWorldBatch(GraphicsDevice device, int numBuffers, SurfaceFormat[] surfaceFormats, bool[] alwaysDS, int scrollBuffer) { this.Device = device; this.Effect = WorldContent._2DWorldBatchEffect; Effect.OutsideDark = new Vector4(0.5f, 0.5f, 0.5f, 1.0f); Effect.WorldToLightFactor = new Vector3(1f / (3 * 75), 1f / (3 * 2.95f), 1f / (3 * 75)); //TODO: World size Sprites = new List <_2DSpriteGroup>(); ScrollBuffer = scrollBuffer; NumBuffers = numBuffers; SurfaceFormats = surfaceFormats; AlwaysDS = alwaysDS; GenBuffers(device.Viewport.Width, device.Viewport.Height); SpriteIndices = new IndexBuffer(device, IndexElementSize.ThirtyTwoBits, 6, BufferUsage.None); SpriteIndices.SetData(_2DStandaloneSprite.indices); }
public static void LoadEffects(bool reload) { _2DWorldBatchEffect = new WorldBatchEffect(ContentManager.Load <Effect>("Effects/2DWorldBatch" + EffectSuffix)); Grad2DEffect = new GradEffect(ContentManager.Load <Effect>("Effects/gradpoly2D")); Light2DEffect = new LightMap2DEffect(ContentManager.Load <Effect>("Effects/LightMap2D")); GrassEffect = new GrassEffect(ContentManager.Load <Effect>("Effects/GrassShader" + EffectSuffix)); RCObject = new RCObjectEffect(ContentManager.Load <Effect>("Effects/RCObject" + EffectSuffix)); SSAA = ContentManager.Load <Effect>("Effects/SSAA"); SpriteEffect = new Effects.SpriteEffect(ContentManager.Load <Effect>("Effects/SpriteEffects" + EffectSuffix)); ParticleEffect = new LightMappedEffect(ContentManager.Load <Effect>("Effects/ParticleShader")); AvatarEffect = new LightMappedEffect(ContentManager.Load <Effect>("Effects/Vitaboy" + EffectSuffix)); Files.RC.Utils.DepthTreatment.SpriteEffect = SpriteEffect; LightEffects = new List <LightMappedEffect>() { _2DWorldBatchEffect, GrassEffect, RCObject, ParticleEffect, AvatarEffect }; }
private void RenderSpriteList(List <_2DSprite> sprites, WorldBatchEffect effect, WorldBatchTechniques technique, List <_2DDrawGroup> cache) { if (sprites.Count == 0) { return; } bool floors = sprites.First().RenderMode == _2DBatchRenderMode.FLOOR; /** Group by texture **/ var groupByTexture = GroupByTexture(sprites); foreach (var group in groupByTexture) { var numSprites = group.Sprites.Count; var texture = group.Pixel; /** Build vertex data **/ var vertices = new _2DSpriteVertex[4 * numSprites]; var indices = new short[6 * numSprites]; var indexCount = 0; var vertexCount = 0; foreach (var sprite in group.Sprites) { //TODO: We want to pre-generate the sprite vertices, to reduce CPU usage. //To do this they'll need to be scrolled by the gpu, all updates to sprite state //will need to regenerate the _2DSpriteVertices, etc. var srcRectangle = sprite.SrcRect; var dstRectangle = sprite.AbsoluteDestRect; indices[indexCount++] = (short)(vertexCount + 0); indices[indexCount++] = (short)(vertexCount + 1); indices[indexCount++] = (short)(vertexCount + 3); indices[indexCount++] = (short)(vertexCount + 1); indices[indexCount++] = (short)(vertexCount + 2); indices[indexCount++] = (short)(vertexCount + 3); // add the new vertices var left = sprite.FlipHorizontally ? srcRectangle.Right : srcRectangle.Left; var right = sprite.FlipHorizontally ? srcRectangle.Left : srcRectangle.Right; var top = sprite.FlipVertically ? srcRectangle.Bottom : srcRectangle.Top; var bot = sprite.FlipVertically ? srcRectangle.Top : srcRectangle.Bottom; vertices[vertexCount++] = new _2DSpriteVertex( new Vector3(dstRectangle.Left, dstRectangle.Top, 0) , GetUV(texture, left, top), sprite.AbsoluteWorldPosition, (Single)sprite.ObjectID, sprite.Room, sprite.Floor); vertices[vertexCount++] = new _2DSpriteVertex( new Vector3(dstRectangle.Right, dstRectangle.Top, 0) , GetUV(texture, right, top), sprite.AbsoluteWorldPosition, (Single)sprite.ObjectID, sprite.Room, sprite.Floor); vertices[vertexCount++] = new _2DSpriteVertex( new Vector3(dstRectangle.Right, dstRectangle.Bottom, 0) , GetUV(texture, right, bot), sprite.AbsoluteWorldPosition, (Single)sprite.ObjectID, sprite.Room, sprite.Floor); vertices[vertexCount++] = new _2DSpriteVertex( new Vector3(dstRectangle.Left, dstRectangle.Bottom, 0) , GetUV(texture, left, bot), sprite.AbsoluteWorldPosition, (Single)sprite.ObjectID, sprite.Room, sprite.Floor); } VertexBuffer vb = null; IndexBuffer ib = null; var count = indices.Length / 3; if (count > 50) //completely arbitrary number, but seems to keep things fast. dont gen if it isn't "worth it". { vb = new VertexBuffer(Device, typeof(_2DSpriteVertex), vertices.Length, BufferUsage.WriteOnly); vb.SetData(vertices); ib = new IndexBuffer(Device, IndexElementSize.SixteenBits, indices.Length, BufferUsage.WriteOnly); ib.SetData(indices); vertices = null; indices = null; } var dg = new _2DDrawGroup() { Pixel = group.Pixel, Depth = group.Depth, Mask = group.Mask, VertBuf = vb, IndexBuf = ib, Vertices = vertices, Indices = indices, Primitives = count, Technique = technique, Floors = floors }; if (cache != null) { cache.Add(dg); } else { RenderDrawGroup(dg); dg.Dispose(); } } }