如今的显卡对雾已经有了硬件级别的支持,所以性能上的损耗非常小。通常硬件都会提供几种基础的雾的实现。
float4x4 view_proj_matrix; struct VS_OUTPUT { float4 Pos: POSITION; float2 Txr1: TEXCOORD0; float1 Fog: FOG; }; VS_OUTPUT vs_main( float4 inPos: POSITION, float2 Txr1: TEXCOORD0 ) { VS_OUTPUT Out; float4 Pos = mul(view_proj_matrix, inPos); Out.Pos = Pos; Out.Txr1 = Txr1; // Set the fog based on a fixed end distance Out.Fog = pow(1-((Pos.z)/650),4); return Out; }
sampler Texture0; float4 ps_main( float4 inDiffuse: COLOR0, float2 inTxr1: TEXCOORD0 ) : COLOR0 { return tex2D(Texture0,inTxr1); }
垂直雾在现实生活中就像下面这种情况一样
在计算的时候只要根据y值来设置雾的浓度就好了
float4x4 view_proj_matrix; struct VS_OUTPUT { float4 Pos: POSITION; float2 Txr1: TEXCOORD0; float1 Fog: FOG; }; VS_OUTPUT vs_main( float4 inPos: POSITION, float2 Txr1: TEXCOORD0 ) { VS_OUTPUT Out; float4 Pos = mul(view_proj_matrix, inPos); Out.Pos = Pos; Out.Txr1 = Txr1; // Set the fog proportional to the Y height. // With a vertex shader, the fog can be set to // any value you wish. Out.Fog = (2*Pos.y/Pos.w)+1; return Out; }
vs如下
Out.Fog = 1-sqrt(dot(Pos.xy/Pos.w,Pos.xy/Pos.w)); float4x4 view_proj_matrix; struct VS_OUTPUT { float4 Pos: POSITION; float2 Txr1: TEXCOORD0; float1 Fog: FOG; }; VS_OUTPUT vs_main( float4 inPos: POSITION, float2 Txr1: TEXCOORD0 ) { VS_OUTPUT Out; float4 Pos = mul(view_proj_matrix, inPos); Out.Pos = Pos; Out.Txr1 = Txr1; // Set the fog proportional to the Y height. // With a vertex shader, the fog can be set to // any value you wish. Out.Fog = 1-sqrt(dot(Pos.xy/Pos.w,Pos.xy/Pos.w)); return Out; }
float4x4 view_proj_matrix; struct VS_OUTPUT { float4 Pos: POSITION; float Depth: TEXCOORD0; }; VS_OUTPUT vs_main(float4 inPos: POSITION) { VS_OUTPUT Out; float4 Pos = mul(view_proj_matrix, inPos); Out.Pos = Pos; Out.Depth= (Pos.z/800); return Out; }
sampler Texture0; float4 ps_main( float Depth: TEXCOORD0 ) : COLOR0 { return Depth; }
sampler Front; sampler Back; const float off = 1.0 / 128.0; float4 ps_main( float2 TexCoord : TEXCOORD0 ) : COLOR { float4 F = tex2D(Front,TexCoord); float4 B = tex2D(Back,TexCoord); return (F-B)*16; }