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

Moving texture with non-moving alpha channel

Last post 7/5/2009 3:01 PM by NightCreature. 6 replies.
  • 7/4/2009 5:11 AM

    Moving texture with non-moving alpha channel

    I've just started learning 3D in XNA after about 6 months of using it for 2D development.
    I've created a model that I am trying to make it so that my mesh is transparent at the top edge using one texture but has a color map moving vertically along it constantly. I already have alpha blending working and most of the textures, it's just a matter of figuring out how to either move the texture or move the vertex texture positions, and figuring out how to use multiple textures for alpha and color, since the alpha texture will not be moving.

    I haven't gotten into the HLSL stuff yet so is there a way to do this with the BasicEffect? If not, what would be the easiest way to do this with a custom effect?

    Thanks.
  • 7/4/2009 11:01 AM In reply to

    Re: Moving texture with non-moving alpha channel

    You would need to use two texture coordinates they can have the same values, one is used for the alpha texture and stay statics. The second set is used to display your color map and is animated with a texture coordinate transform matrix, this is just like a world matrix except it's only applied to the texture coordinates and not to the vertices or normals.

    If you are going to write a custom shader for this you can get away with one texture coordinate set in the vertex declaration, in the vertex shader you would output two sets of texcoord sets. The first one is the unchanged one and is just a pass through the second set is multiplied with the texture coordinate transform matrix. In the pixel shader you index the alpha texture with the first set and the color texture with the second set, then multiply the results and output that.
  • 7/4/2009 7:54 PM In reply to

    Re: Moving texture with non-moving alpha channel

    That still doesn't help me much as I still can't figure out how in code to access the texture coordinates for the mesh so that I can transform them, how to duplicate them for the second texture, or how to render the mesh with two textures without creating a new effect.

    All the tutorials I've found through hours of searching all use hard coded triangles and texture coordinates and I need to know how to access the texture coordinates for a loaded model.
  • 7/5/2009 1:45 AM In reply to

    Re: Moving texture with non-moving alpha channel

    Because XNA doesn't exposed the fixed-function pipeline, you'll have to write a shader to handle this the way you want.

    It may be helpful to review Chapter 5. Modeling and Chapter 11. Basic Texturing from my book The Direct3D Graphics Pipeline to make sure you understand how vertex data is supplied to the pipeline and how the fixed-function pipeline processes texture coordinates. I know XNA doesn't use the fixed-function pipeline, but its easier to write a shader when you understand how the FFP does things.

  • 7/5/2009 2:04 AM In reply to

    Re: Moving texture with non-moving alpha channel

    I found a very different solution than the one I was looking for.

    I wound up pulling this off by using a rendertarget with a surface format that allowed alpha transparency. I used spritebatch to draw my color texture twice (for the top and bottom half) on top of the alpha texture, moving down the render target using gameTime, and used GraphicsDevice blend mode settings so that the minimum alpha value would be used. I saved the render target result in a texture and applied that on the model, and voilà, a moving texture with a static alpha channel! Now I just need to sort out some problems with depth buffering because the transparent parts of the object now obscure the rest of the object when they pass in front of it.

    Here was my inspiration for the alpha blending stuff:
    http://www.gamedev.net/community/forums/topic.asp?topic_id=532775

    And I didn't even have to change from using the BasicEffect!

    Legalize, I found another link to Chapter 11 of your book in another thread when I was trying to solve this! It wasn't much help to me in this case since it isn't XNA-specific and I was still looking for the specific XNA code for accessing the texture coordinates so i could transform them. Sorry!
  • 7/5/2009 5:04 AM In reply to

    Re: Moving texture with non-moving alpha channel

    Yes, my book doesn't use the XNA API for the code examples and details, but all the explanations of concepts still apply.

    Glad you got it to work.

  • 7/5/2009 3:01 PM In reply to

    Re: Moving texture with non-moving alpha channel

    phort99:
    I found a very different solution than the one I was looking for.

    I wound up pulling this off by using a rendertarget with a surface format that allowed alpha transparency. I used spritebatch to draw my color texture twice (for the top and bottom half) on top of the alpha texture, moving down the render target using gameTime, and used GraphicsDevice blend mode settings so that the minimum alpha value would be used. I saved the render target result in a texture and applied that on the model, and voilà, a moving texture with a static alpha channel! Now I just need to sort out some problems with depth buffering because the transparent parts of the object now obscure the rest of the object when they pass in front of it.

    Here was my inspiration for the alpha blending stuff:
    http://www.gamedev.net/community/forums/topic.asp?topic_id=532775

    And I didn't even have to change from using the BasicEffect!

    Legalize, I found another link to Chapter 11 of your book in another thread when I was trying to solve this! It wasn't much help to me in this case since it isn't XNA-specific and I was still looking for the specific XNA code for accessing the texture coordinates so i could transform them. Sorry!

    You don't want to access your texture coordinates on the CPU side, you want to do this on the GPU side it's much faster (orders of magnitude faster). For that you either have to write your own shader like this

    Vertex Shader:
    float4x4 matViewProjection; 
    float time; 
     
    struct VS_INPUT  
       float4 Position : POSITION0; 
       float2 Texcoord : TEXCOORD0; 
    }; 
     
    struct VS_OUTPUT  
       float4 Position  : POSITION0; 
       float2 Texcoord  : TEXCOORD0; 
       float2 Texcoord2 : TEXCOORD1; 
    }; 
     
    VS_OUTPUT vs_main( VS_INPUT Input ) 
       VS_OUTPUT Output; 
     
       Output.Position = mul( Input.Position, matViewProjection ); 
       Output.Texcoord = Input.Texcoord; 
       Output.Texcoord2 = Input.Texcoord;
       Output.Texcoord2.x += time; 
     
       return( Output ); 
     

    Pixel Shader:
    sampler2D baseMap; 
    sampler2D staticAlpha; 
     
    struct PS_INPUT  
       float2 Texcoord  : TEXCOORD0; 
       float2 Texcoord2 : TEXCOORD1; 
    }; 
     
    float4 ps_main( PS_INPUT Input ) : COLOR0 
       return tex2D( baseMap, Input.Texcoord2 ) * tex2D( staticAlpha, Input.Texcoord ).a; 
        

    The fixed function way of doing it would have been:

    D3DXMATRIX16A textureTransform;
    //Seeing we are in an update loop with a gameTime variable
    D3DXCreateMatrixTranslation(&textureTransform, gameTime, 0.0f, 0.0f);
    m_device->SetTransform(D3DST_TEXTURE0, &textureTransform);

    //yourColorTexture defined elsewhere
    m_device->SetTexture(0, &yourColorTexture );
    //yourAlphaTexture defined elsewhere
    m_device->SetTexture(1, &yourAlphaTexture );

    //Set up blending stuff
    ...

    //Draw polygon
    ...

    results are:
    Screen shot 1
    Screen shot 2

    Seeing that the basic effect doesn't support setting texture matrices you will need to write your own shader most of the time.
Page 1 of 1 (7 items) Previous Next