terrain.fx
来自「这也是一个游戏编程基础入门的事例!它是通过鼠标的移动改变照相机的仰角」· FX 代码 · 共 116 行
FX
116 行
//=====================================================================================================
//地形shader
//2009.3.16
//ne0
struct VSOut
{
float4 Pos: POSITION0;
float2 tex0: TEXCOORD0;
float2 texBlend: TEXCOORD1;
float shader: TEXCOORD2;
float fogLerp: TEXCOORD3;
};
float3 g_lightDir;
float4x4 g_matrixVP;
float3 g_EyePosW;
//纹理
texture g_texGrass; //草
texture g_texStone; //山
texture g_texPath; //路
texture g_texBlend; //混合通道纹理
//设置雾化参数
static float3 g_FogColor = {0.5f, 0.5f, 0.5f};
static float g_FogStart = 1000.0f;
static float g_FogRange = 1000.0f;
sampler Tex_Grass = sampler_state
{
Texture = <g_texGrass>;
MagFilter = LINEAR;
MinFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler Tex_Stone = sampler_state
{
Texture = <g_texStone>;
MagFilter = LINEAR;
MinFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler Tex_Path = sampler_state
{
Texture = <g_texPath>;
MagFilter = LINEAR;
MinFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler Tex_Blend = sampler_state
{
Texture = <g_texBlend>;
MagFilter = LINEAR;
MinFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
VSOut VS(float3 PosW:POSITION0,float3 NormalW: NORMAL0,float2 tex0: TEXCOORD0)
{
VSOut V = (VSOut)0;
float3 normal = normalize(NormalW);
float3 vecToLight = normalize(-g_lightDir);
V.shader = saturate(max(dot(normal,vecToLight),0)+0.25f);
V.tex0 = tex0 * 100.0f;
V.texBlend = tex0;
V.Pos = mul(float4(PosW,1.0f),g_matrixVP);
float dist = distance(PosW, g_EyePosW);
V.fogLerp = saturate((dist - g_FogStart) / g_FogRange);
return V;
}
float4 PS(float2 tex0: TEXCOORD0,float2 texBlend: TEXCOORD1,float shader: TEXCOORD2,float fogLerp: TEXCOORD3):COLOR
{
float3 grass = tex2D(Tex_Grass,tex0).rgb;
float3 stone = tex2D(Tex_Stone,tex0).rgb;
float3 path = tex2D(Tex_Path,tex0).rgb;
float3 blend = tex2D(Tex_Blend,texBlend).rgb;
float w = 1.0f/(blend.r + blend.g + blend.b);
float3 texColor = shader * w*(blend.r * grass + blend.g * path + blend.b * stone);
float3 finalColor = lerp(texColor,g_FogColor,fogLerp);
return float4(finalColor,1.0f);
}
technique Tech
{
pass P0
{
vertexShader = compile vs_2_0 VS();
pixelShader = compile ps_2_0 PS();
}
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?