Like I said before, I hate just posting alot of code, but here it is...

It's nowhere near finished, but it works...


WDL: MLTITerrain.wdl
Code:

/////////////////////////////////////////
// terrain Multi-Texture shader

string fxMLTITex = "MLTITex.fx";

function mtl_terrainmulti3_event();


MATERIAL mtl_terrainmulti3
{
event = mtl_terrainmulti3_event;
flags = tangent;
effect = fxMLTITex;
}

function mtl_terrainmulti3_event()
{
mtl.skin1 = bmap_for_entity(my, 5);
mtl.skin2 = bmap_for_entity(my, 6);
mtl.skin3 = bmap_for_entity(my, 7);
mtl.skin4 = bmap_for_entity(my, 8);
}


starter FXLoad
{
effect_load(mtl_terrainmulti3, fxMLTITex);
}



FX: MLTITex.fx
Code:

//
float4x4 matWorldViewProj;
float4x4 matWorldInv;
float4x4 matWorld;
float4 vecViewPos;
float4 vecFog;
float3 vecSunDir;
float3 vecSunPos;

Texture entSkin1; // mask texture
Texture entSkin2; // Red texture
Texture entSkin3; // Green texture
Texture entSkin4; // Blue texture
Texture mtlSkin1; // overlay normal
Texture mtlSkin2; // red normal
Texture mtlSkin3; // green noemal
Texture mtlSkin4; // blue normal

float4 vecSunDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};


sampler mapMask = sampler_state
{
Texture = <entSkin1>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapNormal = sampler_state
{
Texture = <mtlSkin1>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapRC = sampler_state
{
Texture = <entSkin2>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapGC = sampler_state
{
Texture = <entSkin3>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapBC = sampler_state
{
Texture = <entSkin4>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapRN = sampler_state
{
Texture = <mtlSkin2>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapGN = sampler_state
{
Texture = <mtlSkin3>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler mapBN = sampler_state
{
Texture = <mtlSkin4>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

//////////////////////////////////////////////////////////////////////

float DoFog(float3 WPos)
{
return(1 - (distance(WPos, vecViewPos) - vecFog.x) * (vecFog.z));
}

//////////////////////////////////////////////////////////////////////
struct TMULTI_VS_OUT
{
float4 Pos : POSITION;
float2 tCoord : TEXCOORD0;
float3 Sun : TEXCOORD1;
float3 View : TEXCOORD2;
float Fog : FOG;
};

struct TMULTI_PS_IN
{
float4 Pos : POSITION;
float2 tCoord : TEXCOORD0;
float3 Sun : TEXCOORD1;
float3 View : TEXCOORD2;
};


TMULTI_VS_OUT TMulti_VS(
float4 inPos : POSITION,
float3 inNormal : NORMAL,
float3 inTangent : TEXCOORD2,
float2 inTCoord : TEXCOORD0)
{
TMULTI_VS_OUT Out;

float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(inTangent, matWorld);
worldToTangentSpace[1] = mul(cross(inTangent, inNormal), matWorld);
worldToTangentSpace[2] = mul(inNormal, matWorld);

Out.Pos = mul(inPos, matWorldViewProj);
//float3 N = normalize(mul(inNormal, matWorldInv));
float3 P = mul(inPos, matWorld);

Out.Sun = mul(worldToTangentSpace, -vecSunDir);

float3 Viewer = P - vecViewPos;
Out.View = mul(worldToTangentSpace, -Viewer);

Out.Fog = DoFog(P);

Out.tCoord = inTCoord.xy;
return Out;
}


float4 TMulti_PS(TMULTI_PS_IN In): COLOR
{
float4 MaskColor = tex2D(mapMask, In.tCoord);
float4 NormalColor = tex2D(mapNormal, In.tCoord);
float4 RColor = tex2D(mapRC, In.tCoord * 10);
float4 GColor = tex2D(mapGC, In.tCoord * 10);
float4 BColor = tex2D(mapBC, In.tCoord * 10);
float4 RNormal = 2 * tex2D(mapRN, In.tCoord * 10) - 1;
float4 GNormal = 2 * tex2D(mapGN, In.tCoord * 10) - 1;
float4 BNormal = 2 * tex2D(mapBN, In.tCoord * 10) - 1;

float3 ViewDir = normalize(In.View);

float4 outColor = 0.5f;
outColor = lerp(outColor, RColor, MaskColor.r);
outColor = lerp(outColor, GColor, MaskColor.g);
outColor = lerp(outColor, BColor, MaskColor.b);

float3 outNormal = 2*NormalColor.rgb - 1;
outNormal = lerp(outNormal, RNormal, MaskColor.r);
outNormal = lerp(outNormal, GNormal, MaskColor.g);
outNormal = lerp(outNormal, BNormal, MaskColor.b);

float3 sunDir = In.Sun;
float4 sunDiff = saturate(dot(outNormal, sunDir));
float4 sunShadow = saturate(4 * sunDiff);
float3 sunReflect = normalize(2 * sunDiff * outNormal - sunDir);
float4 sunSpec = pow(saturate(dot(sunReflect, ViewDir)), 15);

float4 sunOut = (sunDiff*outColor + (NormalColor.a)*sunSpec) * sunShadow * vecSunDiffuse;
sunOut.a = 1.0f;

return sunOut;
}


technique tmulti3
{
pass one
{
alphaBlendEnable = true;
VertexShader = compile vs_2_0 TMulti_VS();
PixelShader = compile ps_2_0 TMulti_PS();
}
}




xXxGuitar511
- Programmer