Textures are blended by thier alpha channel, just give the terrain 4 skins, and and then make sure youn define the detail texture somewhere and assign it to material skin 1.
Code:
//fixed function terrain shader, blends 4 color texture based on thier alpha channels,
// and blends a detail texture over that
////////////////////////////////////////////////////
// Assign the tile size of the Textures.
//
float Skin_Size1 = 60;
float Skin_Size2 = 60;
float Skin_Size3 = 50;
float Skin_Size4 = 40;
//detail texture
float Skin_Size5 = 400;
float4 vecLight;
/////////////////////////////////////////////////////////////////////////////////////
// Let's allow for adjusting the intensity of the ambient light color on the passes.
//
float ambientLightFactor = 0.5;
////////////////////////////////////////////////////////////////////
// Here we just create and plug some values into a matrix for
// use in multiplying during the stages.
//
float4x4 matTerrainTexSize = {1.0, 0.0, 0.0 ,0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
};
float4x4 matAlphaMapSize = {1.0, 0.0, 0.0 ,0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
};
Texture mtlSkin1; //detail texture
Texture entSkin1; //1st tile tex RGB
Texture entSkin2; //2nd tile tex RGB and blender in A
Texture entSkin3; //3nd tile tex RGB and blender in A
Texture entSkin4; //4nd tile tex RGB and blender in A
sampler s_ShaleTex = sampler_state
{
Texture = (entSkin1);
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipMapLodBias=-0.2;
};
sampler s_SandTex = sampler_state
{
Texture = (entSkin2);
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipMapLodBias=-0.2;
};
sampler s_GrassTex = sampler_state
{
Texture = (entSkin3);
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipMapLodBias=-0.1;
};
sampler s_RoadTex = sampler_state
{
Texture = (entSkin4);
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipMapLodBias=0;
};
sampler grass_detail = sampler_state
{
Texture = (mtlSkin1);
MipFilter = Linear;
MinFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipMapLodBias=0;
};
technique technique_one
{
pass p0
{
Sampler[0] = (s_ShaleTex);
AlphaBlendEnable = False;
AlphaTestEnable = False;
DitherEnable = True;
zWriteEnable = True;
zEnable = True;
Lighting=True;
Ambient = mul(vecLight, ambientLightFactor);
TextureTransformFlags[0] = Count2;
TexCoordIndex[0] = 1;
TextureTransform[0]= mul(matTerrainTexSize,Skin_Size1);
ColorOp[0] = Modulate2x;
ColorArg1[0] = Texture;
ColorArg2[0] = Diffuse;
AlphaOp[0] = Disable;
}
pass p1
{
Sampler[0] = (s_SandTex); // Blendmap is Alpha
Sampler[1] = (s_SandTex); // The Colormap
Lighting = True;
AlphaBlendEnable = True;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
zWriteEnable = True;
zEnable = True;
Ambient = mul(vecLight, ambientLightFactor);
TextureTransformFlags[0] = Count2;
TextureTransform[0]= (matAlphaMapSize);
texcoordindex[0]=0;
ColorArg1[0] = Texture;
ColorOp[0] = SelectArg1;
AlphaArg1[0] = Texture;
AlphaOp[0] = SelectArg1;
TextureTransformFlags[1] = Count2;
TextureTransform[1]= mul(matTerrainTexSize, Skin_Size2);
texcoordindex[1]=1;
ColorArg1[1] = Texture;
ColorArg2[1]= Diffuse;
ColorOp[1] =Modulate2x;
alphaarg1[1] = current;
alphaop[1]=Disable;
}
pass p2
{
Sampler[0] = (s_GrassTex); // Blendmap is Alpha
Sampler[1] = (s_GrassTex); // The Colormap
DitherEnable=True;
AlphaBlendEnable = True;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
DitherEnable = True;
zWriteEnable = True;
zEnable = True;
Ambient = mul(vecLight, ambientLightFactor);
TextureTransformFlags[0] = Count2;
TextureTransform[0]= (matAlphaMapSize);
texcoordindex[0]=0;
ColorArg1[0] = Texture;
ColorOp[0] = SelectArg1;
AlphaArg1[0] = Texture;
AlphaOp[0] = SelectArg1;
TextureTransformFlags[1] = Count2;
TextureTransform[1]= mul(matTerrainTexSize, Skin_Size3);
texcoordindex[1]=1;
ColorArg1[1] = Texture;
ColorArg2[1]= Diffuse;
ColorOp[1] =Modulate2x;
alphaarg1[1] = current;
alphaop[1]=Disable;
}
pass p3
{
Sampler[0] = (s_RoadTex); // Blendmap is Alpha
Sampler[1] = (s_RoadTex); // The Colormap
Lighting = True;
AlphaBlendEnable = True;
SrcBlend = srcAlpha;
DestBlend = InvSrcAlpha;
Ambient = mul(vecLight, ambientLightFactor);
TextureTransformFlags[0] = Count2;
TextureTransform[0]= (matAlphaMapSize);
texcoordindex[0]=0;
ColorArg1[0] = Texture;
ColorOp[0] = SelectArg1;
AlphaArg1[0] = Texture;
AlphaOp[0] = SelectArg1;
TextureTransformFlags[1] = Count2;
TextureTransform[1]= mul(matTerrainTexSize, Skin_Size4);
texcoordindex[1]=1;
ColorArg1[1] = Texture;
ColorArg2[1]= Diffuse;
ColorOp[1] =Modulate2x;
alphaarg1[1] = current;
alphaop[1]=Disable;
}
//detail texture pass
pass p4
{
Sampler[0] = (grass_detail); // Blendmap is Alpha
Sampler[1] = (grass_detail);
AlphaBlendEnable = true;
fogcolor={1.0,1.0,1.0,1.0};
fogstart = 100.0f;
fogend = 500.0f;
Ambient = mul(vecLight, ambientLightFactor);
//multiply blending
srcblend=zero;
destblend=srccolor;
lighting=false;
zWriteEnable = True;
zEnable = True;
TextureTransformFlags[1] = Count2;
TextureTransform[1]= mul(matTerrainTexSize, Skin_Size5);
texcoordindex[1]=1;
ColorOp[1] = modulate;
ColorArg1[1] = Texture;
ColorArg2[1] = diffuse;
AlphaOp[0] = Disable;
}
}