1 registered members (TipmyPip),
18,618
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Normalmapping on terrain
[Re: PHeMoX]
#122346
04/10/07 21:43
04/10/07 21:43
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
Yep, I shouldn't have doubted the manual...
It works. I'll have to update the NM shader I posted, see if it makes any difference...
xXxGuitar511 - Programmer
|
|
|
Re: Normalmapping on terrain
[Re: xXxGuitar511]
#122347
04/11/07 04:45
04/11/07 04:45
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
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.wdlCode:
///////////////////////////////////////// // 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.fxCode:
// 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
|
|
|
Re: Normalmapping on terrain
[Re: Puppeteer]
#122349
04/12/07 13:03
04/12/07 13:03
|
Joined: Oct 2003
Posts: 4,131
Matt_Aufderheide
Expert
|
Expert
Joined: Oct 2003
Posts: 4,131
|
Quote:
alphaBlendEnable = true;
Why do you set this render state? Probabaly not a good idea here as it will disable correct z sorting.
instead you should set these render states:
alphablendenable = false; zenable=true; zwriteenable=true;
|
|
|
Re: Normalmapping on terrain
[Re: Matt_Aufderheide]
#122350
04/12/07 15:53
04/12/07 15:53
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
@derOmega: Thankz man  @Matt: Umm.. I didn't know you could do it that way, but now it's too late to edit my post... I was using the terrain with a complex-object-placement script I'm working on, and the ent's fade away as the get to a certain distance, so i thought I needed to enableAlphaBlend.
xXxGuitar511 - Programmer
|
|
|
Re: Normalmapping on terrain
[Re: xXxGuitar511]
#122351
04/12/07 19:48
04/12/07 19:48
|
Joined: Mar 2006
Posts: 2,503 SC, United States
xXxGuitar511
Expert
|
Expert
Joined: Mar 2006
Posts: 2,503
SC, United States
|
NOTE:
Skin1: Color Mask (RGB) Skin2: Red Diffuse Skin3: Green Diffuse Skin4: Blue Diffuse Skin5: Overlay Normal Skin6: Red Normal Skin7: Green Normal Skin8: Blue Normal
Alpha channel of normal maps is the specular channel.
xXxGuitar511 - Programmer
|
|
|
|