• Create Account

### #Actualcodejockey

Posted 23 October 2012 - 05:26 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only concern I have with it is that it is increadibly slow unless you have multiple channels for your video card. (check out bindless textures on the NVidia video cards http://www.geforce.c...paper-FINAL.pdf)
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128. So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

Oh and one final note, you don't have to do just textures for a terrain. You can use a normal map and do per pixel bump mapping for dynamic lighting.

final = tex2D(texture01Sampler, input.uv1);
normalMap = tex2D(texture02Sampler, input.uv1);
NdL = dot(lightDir, normalMap);
final *= NdL;

let's not forget to make the alpha channel of the pixel 1 so it does't blend with itself.

final.a = 1.0;

And a really, really good texture artist is a must for making a real nice looking terrain so you don't get the fish net looking tiling from a semi-seemless texture pack. Way to go Id Software. http://www.moddb.com/games/enemy-territory-quake-wars/addons/mega-texture-media-pack

and voila! Happy terraining!

### #8codejockey

Posted 23 October 2012 - 05:08 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only concern I have with it is that it is increadibly slow unless you have multiple channels for your video card. (check out bindless textures on the NVidia video cards http://www.geforce.com/Active/en_US/en_US/pdf/GeForce-GTX-680-Whitepaper-FINAL.pdf)
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128. So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

Oh and one final note, you don't have to do just textures for a terrain. You can use a normal map and do per pixel bump mapping for dynamic lighting.

final = tex2D(texture01Sampler, input.uv1);
normalMap = tex2D(texture02Sampler, input.uv1);
NdL = dot(lightDir, normalMap);
final *= NdL;

let's not forget to make the alpha channel of the pixel 1 so it does't blend with itself.

final.a = 1.0;

and voila! Happy terraining!

### #7codejockey

Posted 23 October 2012 - 05:01 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only concern I have with it is that it is increadibly slow unless you have multiple channels for your video card.
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128. So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

Oh and one final note, you don't have to do just textures for a terrain. You can use a normal map and do per pixel bump mapping for dynamic lighting.

final = tex2D(texture01Sampler, input.uv1);
normalMap = tex2D(texture02Sampler, input.uv1);
NdL = dot(lightDir, normalMap);
final *= NdL;

let's not forget to make the alpha channel of the pixel 1 so it does't blend with itself.

final.a = 1.0;

and voila! Happy terraining!

### #6codejockey

Posted 23 October 2012 - 04:56 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only problem with it on my machine is that it is increadibly slow.
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128 and any bigger than that won't run on my card.
So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

Oh and one final note, you don't have to do just textures for a terrain. You can use a normal map and do per pixel bump mapping for dynamic lighting.

final = tex2D(texture01Sampler, input.uv1);
normalMap = tex2D(texture02Sampler, input.uv1);
NdL = dot(lightDir, normalMap);
final *= NdL;

let's not forget to make the alpha channel of the pixel 1 so it does't blend with itself.

final.a = 1.0;

and voila! Happy terraining!

### #5codejockey

Posted 23 October 2012 - 04:21 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only problem with it on my machine is that it is increadibly slow.
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128 and any bigger than that won't run on my card.
So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

### #4codejockey

Posted 23 October 2012 - 04:19 PM

Ok, I'm going to be quite generous with this post as I have seen this terrain blending question being asked repeatedly over and over and over.
I found a good tutorial (http://www.innovativ...proved-terrain/) and I'm going to describe to you folks how to do it.
It uses a pixel shader to do the texture blending.
Only problem with it on my machine is that it is increadibly slow.
However with the cards that run DX11 these days you'll have to tell me if its quicker because I get around 8 FPS.
With that in mind here's what I have completed that works.
First off your terrain model has to define its UV's to be from 0 to 1.
This is accomplished by dividing the number of vertexes across by 1 and number of vertexes down by 1.
My terrain is 128x128 and any bigger than that won't run on my card.
So we get 0.0078125 and if we loop across starting at 0 when whe arrive at the 128th vertex we'll be at 1.
We can take advantage of this in the pixel shader. Here's how:

Set up a float value for how many times you want the texture to tile across the terrain.

float uvScale01 = 1.0;

in your program you'll have a handle set to this variable:

and you can set the variable from your program:

This is a mask in mine so I have it stretched across the whole terrain for the blending but if it were a texture you could make the scale something like 32 and it would tile across the whole terrain 32 times.
I'll show you how in a little bit.
Here is the sampler definition for the mask and a texture is just the same.
texture texture01;
sampler2D texture01Sampler = sampler_state
{
texture = texture01;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
{
float4 pos : POSITION;
float3 norm : NORMAL;
float4 diffuse : COLOR;
float4 specular : COLOR1;
float2 uv1 : TEXCOORD;
float Depth : TEXCOORD1;
float3 vpos : TEXCOORD2;
};
Not going to go into much detail here but this is how you pass the pixel from the primitive as it relates to the FVF to the pixel shader.
Grabbing the elements to pass to the PS:
{
// directional lighting
lightDir = normalize(lightDir);
output.pos = mul(input.pos, WorldViewProj);
output.vpos = input.pos;
// lighting
output.norm = normalize(mul(input.norm, World));
float d = dot(output.norm, lightDir);
d = clamp(d, ambient, 1.0);
output.diffuse = lightMaterial * d;
output.diffuse.a = 1.0;
output.specular = specularMaterial * d;
output.specular.a = 1.0;
// texture
output.uv1 = input.uv1;
// fog
output.Depth = output.pos.z;
return output;
}
Notice there are normals (vertex normal) and depth and things like that for more than just blending.
Now for the actual blend:
{
float4 final = {0, 0, 0, 0};
float4 lightMap;
float b1;
float b2;
float4 ts;
// =================================================================================
// Base Terrain Layer 1

final = tex2D(texture16Sampler, input.uv1 * uvScale16);
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 2

maskMap = tex2D(texture03Sampler, input.uv1 * uvScale03);
{
b2 = 1.0 - b1;
ts = tex2D(texture04Sampler, input.uv1 * uvScale04);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 3

maskMap = tex2D(texture05Sampler, input.uv1 * uvScale05);
{
b2 = 1.0 - b1;
ts = tex2D(texture06Sampler, input.uv1 * uvScale06);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 4

maskMap = tex2D(texture07Sampler, input.uv1 * uvScale07);
{
b2 = 1.0 - b1;
ts = tex2D(texture08Sampler, input.uv1 * uvScale08);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 5

maskMap = tex2D(texture09Sampler, input.uv1 * uvScale09);
{
b2 = 1.0 - b1;
ts = tex2D(texture10Sampler, input.uv1 * uvScale10);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 6

maskMap = tex2D(texture11Sampler, input.uv1 * uvScale11);
{
b2 = 1.0 - b1;
ts = tex2D(texture12Sampler, input.uv1 * uvScale12);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Terrain Layer 7

maskMap = tex2D(texture13Sampler, input.uv1 * uvScale13);
{
b2 = 1.0 - b1;
ts = tex2D(texture14Sampler, input.uv1 * uvScale14);
final = (final * b1) + (ts * b2);
}
// =================================================================================
// Lightmap
// no scaling needed here as it goes across the whole terrain

lightMap = tex2D(texture15Sampler, input.uv1);
final *= lightMap;
// =================================================================================
// Fog
#include "fog.fxh"
// =================================================================================
// Return Color
final.a = 1.0;
return final;
}
technique terrainBlend
{
pass
{
#include "samplerStates.fxh"
}
}
Yup 7 layers of textures and a lightmap to boot. Ok looks a little daunting at first but here is what it is doing.
final is the returned pixel color and texture16 (0-15 samplers) is scaled (tiled) across the terrrain at scale16 times.
This is the base texture for the whole terrain.
final = tex2D(texture16Sampler, input.uv1 * uvScale16);
Then blend texture02Sampler with a bitmap mask or whatever mask you have defined. The mask just needs to be 24 bit and I use bmp's for the mask even though I uploaded a jpg. IE just doesn't want to display a bmp.
// =================================================================================
// Terrain Layer 1

maskMap = tex2D(texture01Sampler, input.uv1 * uvScale01);
{
b2 = 1.0 - b1;
ts = tex2D(texture02Sampler, input.uv1 * uvScale02);
final = (final * b1) + (ts * b2);
}

Here's the code to get the shader script executed:
{
r = graphics->device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbrVert, 0, count);

}
I used an indexed list of vertexes but you can use drawprimitive if you like. Not sure what you application is.

And thats all there is to it. It looks real nice when you get the shader script working and you don't need 12x128x2 triangles to do the blend.
You can actually do it on 1 triangle so you should be able to apply this to any form of texture blending that you may need.

PS. Fog way to cool.

float fogStart = 256.0;
float fogEnd = 4096.0;
float fogDensity = 0;
float minFog = 0.0;
float maxFog = 1.0;
float3 fogColor = {0.85, 0.85, 0.85};
if (fogDensity > 0)
{
float fog = (input.Depth - fogStart) / (fogEnd - fogStart);
fog *= fogDensity;
fog = clamp(fog, minFog, maxFog);
final = lerp(final, float4(fogColor, 1.0), fog);
}

PARTNERS