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

Advanced Terrain Texturing

Last post 09-05-2008 8:54 PM by Lord Ikon. 5 replies.
  • 09-02-2008 12:16 PM

    Advanced Terrain Texturing

    Right now I am using some simple color blending on my terrain using color maps and another map that tells the shader how much color data to pull from each color map, but there is a huge problem. Each terrain block size is limited to the size of the texture because each vertex in the terrain relates 1:1 to the texture. That is the upper left vertex is 0,0 on the texture and the bottom right is 1,1 on the texture. While this works well, I would like to be able to splat textures with independent sizes. How do I do this?


    Here is an example of what I have right now. This terrain is made up of four blocks with the same color mapping.

    Foxhorn Terrain Texturing
    John Sedlak Xna/DirectX MVP
    Focused Games | My Blog
  • 09-02-2008 3:46 PM In reply to

    Re: Advanced Terrain Texturing

    Use a texturesampler in your shader to read the texture information in, and then you can use it on either a per vertex or per pixel basis and can scale it to your needs. The will allow you to change the LOD of the terrain (if you ever need to) without losing texture information. Engine in my signiture uses this if you'd like to see the code.

    Edit: If you'd also like to splat textures onto the terrain, that may need to be stored separately and applied separately as well, as in, different shader, and in a different step then the terrain multitexturing.

    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 09-02-2008 10:30 PM In reply to

    Re: Advanced Terrain Texturing

    I used a single set of UV co-ordinates (0.0-1.0 like you mentioned), but divided them by a 'textureScale' value in the pixel shader.

    tex2D(Sampler, input.TextureUV/diffuseScale); 

    Give the shader a couple of different scales (I used globalScale for the blend, diffuseScale for the colour and detailScale for the detailo map) and voila!

  • 09-03-2008 2:38 PM In reply to

    Re: Advanced Terrain Texturing

    Hmmm. Not exactly as advanced as I am looking for. I have implemented blending of multiple color maps and will implement normal maps soon, but I am still looking for the next step. The problem is that each texture is still defined by some scale on each block which requires some really good tileable textures.

     

    Terrain

    Here is the shader:

    1 uniform extern float4x4 WorldViewProj : WORLDVIEWPROJECTION;  
    2 uniform extern float4x4 World;  
    3 uniform extern float3 LightPosition;  
    4 uniform extern float3 LightDirection;  
    5 uniform extern bool UseLighting;  
    6  
    7 uniform extern float4 LightColor = float4(1, 1, 1, 1); //half4(0.8945f, 0.8867f, 0.4453f, 1.f);  
    8 uniform extern float4 AmbientColor = float4(0.25, 0.25, 0.25, 1); //half4(0.375f, 0.379f, 0.391f, 1.f);  
    9  
    10 uniform extern texture ColorTexture;  
    11 uniform extern texture ColorDataTexture0;  
    12 uniform extern texture ColorDataTexture1;  
    13 uniform extern texture ColorDataTexture2;  
    14  
    15 struct VS_INPUT  
    16 {  
    17     float4 Pos  : POSITION;  
    18     float4 TexCoord : TEXCOORD0;  
    19     float3 Normal : NORMAL;  
    20 };  
    21  
    22 struct VS_OUTPUT  
    23 {  
    24     float4 Pos  : POSITION;  
    25     float4 TexCoord : TEXCOORD0;  
    26     float3 Normal : TEXCOORD1;  
    27     float3 LightDirection : TEXCOORD2;  
    28 };  
    29  
    30 sampler colorSampler = sampler_state  
    31 {  
    32     Texture = <ColorTexture>;  
    33     mipfilter = LINEAR;  
    34     minfilter = LINEAR;  
    35     magfilter = LINEAR;  
    36 };  
    37  
    38 sampler colorDataSampler0 = sampler_state  
    39 {  
    40     Texture = <ColorDataTexture0>;  
    41     mipfilter = LINEAR;  
    42     minfilter = LINEAR;  
    43     magfilter = LINEAR;  
    44 };  
    45  
    46 sampler colorDataSampler1 = sampler_state  
    47 {  
    48     Texture = <ColorDataTexture1>;  
    49     mipfilter = LINEAR;  
    50     minfilter = LINEAR;  
    51     magfilter = LINEAR;  
    52 };  
    53  
    54 sampler colorDataSampler2 = sampler_state  
    55 {  
    56     Texture = <ColorDataTexture2>;  
    57     mipfilter = LINEAR;  
    58     minfilter = LINEAR;  
    59     magfilter = LINEAR;  
    60 };  
    61    
    62 VS_OUTPUT Transform(VS_INPUT In)  
    63 {  
    64     VS_OUTPUT Out = (VS_OUTPUT)0;  
    65  
    66     Out.Pos = mul(In.Pos, WorldViewProj);  
    67     Out.TexCoord = In.TexCoord;  
    68     Out.Normal = In.Normal; /* = mul(In.Normal, World);*/ 
    69       
    70     Out.LightDirection = LightPosition - In.Pos; /* - mul(In.Pos, World);*/ 
    71  
    72     return Out;  
    73 }  
    74  
    75 float4 ApplyTexture(VS_OUTPUT Input) : COLOR  
    76 {  
    77     float4 TextureColor = tex2D(colorSampler, Input.TexCoord).rgba;  
    78       
    79     float4 FinalColor;  
    80       
    81     FinalColor = saturate(  
    82                  tex2D(colorDataSampler0, Input.TexCoord * 8) * TextureColor.r +  
    83                  tex2D(colorDataSampler1, Input.TexCoord * 8) * TextureColor.g +  
    84                  tex2D(colorDataSampler2, Input.TexCoord * 8) * TextureColor.b  
    85                  );  
    86                    
    87     if(UseLighting)  
    88     {  
    89         float3 LightDirection = normalize(Input.LightDirection);  
    90           
    91         float3 Normal = normalize(Input.Normal);  
    92         float NDotL = dot(LightDirection, Normal);  
    93           
    94         float4 DiffuseAverage = float4(1, 1, 1, 1);  
    95         float4 TotalDiffuse = DiffuseAverage * saturate(NDotL);  
    96               
    97         //FinalColor = saturate(FinalColor * TotalDiffuse + FinalColor * 0.25);  
    98           
    99         //FinalColor = saturate((Ambient + TotalDiffuse) * FinalColor);  
    100           
    101         FinalColor = (AmbientColor + (LightColor * max(0.f, NDotL))) * FinalColor;  
    102     }  
    103     else 
    104         FinalColor = TextureColor;  
    105       
    106     return FinalColor;  
    107 }  
    108  
    109 technique TransformAndTexture  
    110 {  
    111     pass P0  
    112     {  
    113         ZEnable = true;  
    114         ZWriteEnable = true;  
    115         vertexShader = compile vs_2_0 Transform();  
    116         pixelShader  = compile ps_2_0 ApplyTexture();  
    117     }  
    118
    John Sedlak Xna/DirectX MVP
    Focused Games | My Blog
  • 09-05-2008 8:52 PM In reply to

    Re: Advanced Terrain Texturing

    Have you turned on mip-mapping for all of your terrain textures? It will really improve the graininess of the more distant textures
  • 09-05-2008 8:54 PM In reply to

    Re: Advanced Terrain Texturing

    Mip-mapping could easily help framerate as well.
    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
Page 1 of 1 (6 items) Previous Next