XNA Creators Club Online
Page 1 of 1 (11 items)
Sort Posts: Previous Next

How can I draw decals to complex geometry?

Last post 10-15-2008 4:24 PM by Plebe. 10 replies.
  • 09-05-2008 6:56 AM

    How can I draw decals to complex geometry?

    I am trying to implement the drawing of decals on to complex geometry. For example, I want to draw a circle below an object, but the object is on stairs. How would I place the decal to wrap to the stairs below the object? Here is a picture of what I hope to achieve. Also I think that this is possible but I'm not sure, how would I use an animated sprite or texture as a decal? Would I have to apply a new decal, deleting the previous one, for each frame of the animation? I have just one more question that I am not a hundred percent on. Can I apply an effect or a shader to the decal, or would this only work if the decal is a geometry object and not a sprite or texture?

  • 09-05-2008 3:47 PM In reply to

    Re: How can I draw decals to complex geometry?

    One way to achieve the effect in the picture is to project the circle texture on to the stairs.

    To do this you want to do something like the following:

    -Setup a camera that looks down on the character, orthogonal to the ground plane.

    -When drawing the ground, calculate the projective texture coordinates to sample the circle texture using the camera's view and projection matrices.

    -Combine the color of the texture with the color of the ground.

    Here's how you would generate projective texture coordinates to sample the circle texture:

    float4x4 WorldViewProj;  
    float4x4 DecalCameraViewProj;  
      
    texture DecalHighlightTexture;  
      
    sampler Sampler = sampler_state  
    {  
        Texture = <DecalHighlightTexture>;  
    };  
      
    struct OutputVS  
    {  
        float4 posH          : POSITION0;  
        float4 projTexCoords : TEXCOORD0;  
    };  
      
    OutputVS VS(float3 posL : POSITION0)  
    {  
        OutputVS OUT = (OutputVS)0;  
      
        OUT.posH = mul(float4(posL, 1.0f), WorldViewProj);  
      
        //now, project the point with the camera used to capture the object  
        //and pass this to the pixel shader  
        OUT.projTexCoords = mul(float4(posL, 1.0f), DecalCameraViewProj);  
      
        return OUT;  
    }  
      
    float4 PS(float4 projTexCoords : TEXCOORD0) : COLOR0  
    {  
        //calcuate the projection texture coordinates  
      
        //perform the perspective divide  
        projTexCoords.xy /= projTexCoords.w;  
      
        //now scale and offset the texture coordinates to [0, 1]  
        projTexCoords.x = .5f * projTexCoords.x + .5f;  
        projTexCoords.y = -.5f * projTexCoords.y + .5f;  
      
        //now use these texture coordinates to sample the texture of the object  
        float4 decal = tex2D(Sampler, projTexCoords);  
     
        //... do shading as usual for your ground and add the highlight// 
        //e.g. FinalColor = groundColor + decal
    }  

    To animate the decal texture, you could simply use a sprite sheet and just supply the shader with the current frame in the animation.

  • 09-05-2008 9:55 PM In reply to

    Re: How can I draw decals to complex geometry?

    Is there another way to do this?  Potentially through multipassing?  What if you want to render multiple projected decals at once?
  • 09-05-2008 10:40 PM In reply to

    Re: How can I draw decals to complex geometry?

    There's always another way :-)

    For instance if you have a CPU side copy of the target mesh for collision detection, you could do a mesh intersection computation between that and the decal volume, computing exactly the triangles that make up the planes where the two intersect, then render exactly those triangles with a slight zbuffer bias to avoid fighting.

    Another way to think of this: decals are basically the same thing as shadows, cast from an overhead light, which happen to be some other color than black. So any shadow rendering technique can be used for decals at well. The technique described above is basically the same thing as shadow buffers. You could also use the stencil buffer to compute a "decal volume" (same thing as standard shadow volume rendering) or just fake it and project the decal onto a single plane with a big depth bias, as in hacky blob shadow rendering.


    XNA Framework Developer - blog - homepage
  • 09-06-2008 5:31 AM In reply to

    Re: How can I draw decals to complex geometry?

    I never thought about looking at decals like shadows... 

    I'll see what I come up with and post my results.


  • 09-07-2008 11:26 PM In reply to

    Re: How can I draw decals to complex geometry?

    shawn would you mind showing a quick example as how to display the decals through the stencil buffer? i can understand that i would have to distiguish an area where the piece of the projection would be displayed, but i have no idea how i would be able to establish this area, and i it is done this way, wouldnt u need to re render the scene for every decal projection?
  • 09-08-2008 6:27 PM In reply to

    Re: How can I draw decals to complex geometry?

    qc zackf:
    I am trying to implement the drawing of decals on to complex geometry. For example, I want to draw a circle below an object, but the object is on stairs. How would I place the decal to wrap to the stairs below the object? Here is a picture of what I hope to achieve. Also I think that this is possible but I'm not sure, how would I use an animated sprite or texture as a decal? Would I have to apply a new decal, deleting the previous one, for each frame of the animation? I have just one more question that I am not a hundred percent on. Can I apply an effect or a shader to the decal, or would this only work if the decal is a geometry object and not a sprite or texture?

    You could try something like this.  Pass the coordinates of the center of the decal in the world to the shader.  In the pixel shader simply convert each point to a distance from that center point and put them in the 0-1 range clamp(distance/desired radius,0,1), and make them all whatever color you want.   That will give you a sphere of decal, but only things intersecting the sphere will be colored, making for a round dot applied over any geometry passing through.     With a little fiddling, you could turn that distance & center point into a texture coordinate to apply a different texture on the dot.

     

    Best,

    Byron

    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 09-12-2008 12:21 AM In reply to

    Re: How can I draw decals to complex geometry?

    I was talking more on the lines of getting the decal to be shown up properly. I understand how to do the volumes, but i cant seem to get the projection of the decal down right. whenever i try to scle it by moving the "projector" distance the texture begins to repeat. What ive turned to is a simple system of rendering 3d quads against the surface, getting that texture, and using it to project the quads texture onto the surface, much like with the shadow volumes. so basically just the projection is the problem for me.
  • 09-16-2008 12:54 AM In reply to

    Re: How can I draw decals to complex geometry?

    You're complicating the issue. Pass the horizontal position of where you want the circle to show up (in world coordinates) to the shader. Then use the x and z coordinates (if y is up) (aka horizontal world position) of the vertices of the stairs as "UV coordinates" to slap your circular texture. You will probably need a "UV multiplier" for the circular texture to show up at a certain size. The "UV offset" will be the world position of the circular texture. I hope that wasn't too confusing.
  • 10-15-2008 4:24 PM In reply to

    Re: How can I draw decals to complex geometry?

    Abuzer, drawing a decal like you say it's not difficult, I think you have expressed it right. But how would you draw 100 decals with that method? Most common shaders (2_0) are not going to let you do a for loop with the coordinates of those 100 decals, because it admits few (I have compilation errors with 2 nested for, each of them going from 0 to 4, I think that 64 arithmetic operations is the top).

    Anyone has an idea to draw 100+ decals?

Page 1 of 1 (11 items) Previous Next