Original Post
Howdy, I'd like to get your opinions about the best way to implement terrain deformation in my 2D game. I am using a TileMap to display a 2D landscape on the screen. I have implemented per-pixel collision against the terrain with no issues and am now looking to implement the deformation routines. Whenever an explosion or collision occurs on the landscape it will become deformed, essentially a crater is carved out of the terrain and the collision map is affected accordingly. I have achieved this once in my original prototype that effectively had the terrain as a single texture; I would call Texture2D.GetData() into a Color [] array, modify the texture and send the data back to the texture using SetData. However, this method was slow and often caused issues due to the texture being in use by the graphics card. It also doesn't apply very well to a tile-based landscape unless I did something ugly like render out the tilemap to a large texture and worked with it from there. I've tried a new solution but I didn't like it - that was to manually build another texture from the destruction map each time the camera moved (or there was another destruction) that was used in the pixel shader to erode the landscape. This worked to a degree, but I was still creating a texture most frames and building it from a Color map using SetData - ugly. A twisted version of this would be do the opposite and render the visible landscape to a separate render target each frame, get the color data, modify it and push it back - but again, this is what I am trying to avoid. I'd like a better way of doing it. So far, I've seen two other ways that boil down to me storing a list of the deformation objects and rendering their textures to either the stencil buffer or another texture and combining it with the 'normal' background in the pixel shader. The main issue I have with this approach is that deformations will actually be common and with lots of small objects (bullets) - it is possible that I'd spend an age rendering the deformation texture and my memory usage would skyrocket as all these small objects hang around until the tile has been eroded completely or the game ends. Some tips or guidance would be much appreciated - I'm fairly new to graphics dev, especially shaders.