Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So, I want to use textures for weapons in my FPS game instead of actual models. However, if I just draw the texture onto a square in the game, it has the rotation and stuff, but it looks "2D". How can I make it look like it has width?

The best example I can think of is probably Minecraft. If you hold a tool in Minecraft (such as a pickaxe), the texture is drawn and it has an "edge" that shares a color with the color of the actual pixel next to it.

That was probably really confusing, so here's a picture of what I mean: Original: Minecraft tool with edge

Edge highlighted: Highlighted edge

How can I do this with XNA?

My code basically makes a square right now (four vertexpositiontexture vertices), and I use BasicEffect to attach an image to it.

share|improve this question
Why not just extrude it using the image as an alpha/colour map? – ashes999 May 10 '12 at 3:00
How does that help me create the "edge" effect? Doesn't an alpha map just tell the renderer about transparent places? – elite_dragon_slayer_3252583 May 10 '12 at 3:21
1  
You could simply create a cube for every pixel in your texture that is not 100% transparent. You could also identify the outer boundaries in your texture, and use those positions to create the rim; then just put two flat squares with the texture on either side. – melak47 May 10 '12 at 3:37
Wouldn't that be rather inefficient, though? Is there no way I can do some magical (for lack of a better word) technique that will allow an edge to appear? – elite_dragon_slayer_3252583 May 10 '12 at 3:51
1  
Is there a reason you can't just create a model that matches the shape of the texture, and UV map it accordingly? Otherwise you need to generate the vertex data based off the texture and extrude it like ashes999 suggested. – michael.bartnett May 10 '12 at 5:10
show 1 more comment

3 Answers

Two approaches come to my mind:

  1. Render bounding box of the object. The box would be an extrusion of the quad that you are currently rendering. Then implement a shader that does raymarching inside the box to look for texels. This is like volume rendering a 3D texture, but the depth of the texture is 1. This might not be efficient enough though. The positive side about this solution is that after implementing the shader you can easily use any texture.
  2. As a preprocessing step calculate all edges of the texture (including interior edges). Create a mesh based on the edges. There are probably many ways to find the edges, but the following algorithm should be quite simple to implement.
    1. Start by creating two vertices in the corners between and around all texels. For w * h texture you would have 2*(w+1)*(h+1) vertices.
    2. Create faces for all horizontal edges around each texel if the left side is empty and the right side is not, or vice versa. If left and right are both empty or filled, no edge is created. The normal for the face is either (-1,0,0) or (1,0,0) depending which side was empty. Do the same thing in vertical direction.
    3. To reduce the number of faces go through the faces and combine adjacent edges together where vertices are shared and the normals are pointing to same direction. If you have the faces stored in a 2D-array the adjacent faces should be fast to locate. Alternatively you can just compare all faces against each other.
    4. Remove vertices that are not referenced by any of the remaining faces.

I would choose the second approach if the textures are static. Both of the approaches can produce the same visual style you have in the screenshots. If you want less pixelated look, smooth edges, support for semi-transparency or something like that, you might need different algorithms or improve those listed above.

share|improve this answer

Well this involves 3D projection drawing which is generally handled by the graphics card. It's possible to do obviously in software, but it will be a lot slower and XNA isnt really designed for that. However, what I believe minecraft does is all of the models are actually converted polygons (or they begin like that) and are drawn with textures applied. That said, if you want to draw a 3D image, just map it to a quad, which sounds like what you're doing, however maybe you could post some of your images so we can see what you are having trouble with.

share|improve this answer
Well, as I said, just drawing a "flat" 2D image to a quad looks bad; it has no edge and doesn't look 3D. – elite_dragon_slayer_3252583 May 10 '12 at 3:47
yes, if you want it to look 3D, then you'd probably have to apply some kind of distortion mapping like normal mapping or parallax mapping, if thats what i think you want. – CobaltHex May 10 '12 at 6:28

Well, minecraft uses no models it draws each pixel in the image as a cube. It uses the alpha channel to get rid of the pixels, which I don't know if XNA supports it. So pretty much I recommend going through all the pixels and render ones that aren't the color that means it's not a part of the texture.

share|improve this answer
There's quite a lot of Minecraft-likes being made with XNA, and they all tend to draw the cubes and voxels as geometry. Since there are a lot of them on the screen at once, instanced geometry is usually the approach that people take. You can draw a ton of different-looking cubes with a single draw call if you feed a second vertex buffer with data for colors, and separate texture lookups. – ChrisC Feb 4 at 20:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.