Jump to content
  • Advertisement
Sign in to follow this  
XXChester

Destructable terrain last step

This topic is 2477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone. I am working on a scorthed earth sorta clone in my free time and the last step I need to get working is the destructable terrain. The way I am doing this is I use RenderTarget2D to render the terrain and overlay a crater image. My terrain is black and everything else is transparent. My crater(round part) is white. The problem I am having is I know how to overlay the textures but I cannot seem to wrap my head around the last step of changing all of the white pixels of the crater to transparent in the new texture.

Here is the code I am using to create the new texture where terrain is the object containing and responsible for rendering the terrain, and crater is the object containing and responsible for rendering the crater.



// Render to texture the crater

this.crater.Position = position;

RenderTarget2D renderTarget = new RenderTarget2D(device, this.terrain.Texture.Width, this.terrain.Texture.Height);

device.SetRenderTarget(renderTarget);

device.Clear(Color.Transparent);

// first pass set the collision point

SpriteBatch spriteBatch = new SpriteBatch(device);

//spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearClamp, DepthStencilState.Default,

// RasterizerState.CullNone);

spriteBatch.Begin();

this.terrain.render(spriteBatch);

this.crater.render(spriteBatch);

spriteBatch.End();

// second pass make the collision point transparent

?

this.terrain.Texture = (Texture2D)renderTarget;

device.SetRenderTarget(null);

this.textureColourData = TextureUtils.getColourData2D(this.terrain.Texture);

}


Here is the resulting image after the terrain has been hit twice.

exampleeb.png

Uploaded with ImageShack.us

If anyone could point me in the direction of the second pass for changing these pixels to transparent that would be great. Please note I also tried just manually going through the textures Color data but this is noticeably slow.

Any help would be great.
Thanks.


Share this post


Link to post
Share on other sites
Advertisement
I found a simpler approach that doesn't use the render target. Thank's for taking a look at the topic anyway. Here is the solution that popped into my head, it is simple and quite basic.

First I cache a list of points in relation to the center of the crater;

this.distancesFromMiddle = new List<Vector2>();
Texture2D crater = LoadingUtils.loadTexture2D(content, "Crater");
Color[,] colours = TextureUtils.getColourData2D(crater);
for (int y = 0; y < crater.Height; y++) {
for (int x = 0; x < crater.Width; x++) {
if (colours[x, y] == Color.White) {
this.distancesFromMiddle.Add(new Vector2(x - 32f, y - 32f));// half of the textures size so we render at the origin
}
}
}


Than on impact I do this;

int tcdW = this.textureColourData.GetUpperBound(0) + 1;
int tcdH = this.textureColourData.GetUpperBound(1) + 1;
Vector2 pixelPosition;
for (int i = 0; i < this.distancesFromMiddle.Count; i++) {
pixelPosition = Vector2.Add(position, this.distancesFromMiddle);
if (pixelPosition.X >= 0 && pixelPosition.X < tcdW && pixelPosition.Y >= 0 && pixelPosition.Y < tcdH) {
this.textureColourData[(int)pixelPosition.X, (int)pixelPosition.Y] = Color.Transparent;
}
}
// flatten the array
Color[] colours = new Color[tcdH * tcdW];
for (int x = 0; x < tcdW; x++) {
for (int y = 0; y < tcdH; y++) {
colours[x + y * tcdW] = this.textureColourData[x, y];
}
}
this.terrain.Texture.SetData<Color>(colours);


So basically I am caching a set of points in relation to the origin of the crater and than on impact I am applying that position to the collision point.

Share this post


Link to post
Share on other sites
I just wrote a tutorial on creating scorched earth. My approach is this:

Put depth testing on.
Clear screen black.
Draw the terrain with the cutout alpha sky. Depth = 1
Draw all your collision holes in black with Depth = 1000 (must put depth test to GREATER) so that the 1000 parts overwrite the 1 parts.
This effectively makes all the explosion hole pixels act as if they were never written to because they will be overwritten by:
Draw sky depth = 2. All the hole points with 1000 depth get overwritten by sky with 2 depth.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!