如今的显卡对雾已经有了硬件级别的支持,所以性能上的损耗非常小。通常硬件都会提供几种基础的雾的实现。
1意味着没有fog,0表示全是fog 雾的实现也可分为per vertex的和per pixel的。Per vertex的只计算顶点的fog,其他的地方用插值来处理,perpixel每个pixel都计算一次,稍微耗一些。 After you determine the regular rendering result of the object, the fog thickness is determined and used to blend the object color with the fog color based on the fog progression mode chosen 使用fog的话也不用写什么shader,只要设置对应api的相关的state就可以了。 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; } Ps中并没有什么特殊处理,因为直接交给硬件处理了。 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; }