Shaders for Game Programmers and Artists(8) - Fog

简介

在水汽充足、微风及大气层稳定的情况下,气温接近零点,相对湿度达到100%时,空气中的水汽便会凝结成细微的水滴悬浮于空中,使地面水平的能见度下降,这种天气现象称为雾。
光线在照射到这些小颗粒的时候,方向发生改变,一些光被挡掉了,所有光的强度发生了改变,另一方面本不应该射向观察者的光线射向了观察者。

线性雾/指数雾/对数雾


如今的显卡对雾已经有了硬件级别的支持,所以性能上的损耗非常小。通常硬件都会提供几种基础的雾的实现。




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;
}




圆形雾

还是计算fog值
Out.Fog = 1-sqrt(dot(Pos.xy/Pos.w,Pos.xy/Pos.w));

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;
}



体积雾

基于顶点的雾效,由于计算的雾色最终是叠加到模型上渲染出来,无法表达出体积感。所以需要一种新的可以表达体积的雾效,我们把它称之为体积雾,能够控制位置,形状,密度。

实现体积雾需要用到深度信息。在计算某一点fog浓度的时候,需要两个pass,两个RT,一个pass绘制正面的深度,另一个pass绘制背面的深度,fog的浓度等于这两个深度值相减。

想绘制模型的背面只需要将RenderState中CULLMODE换成CW就可以了。
深度绘制的vs

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;
}



深度绘制的ps
sampler Texture0;
float4 ps_main( 
   float Depth: TEXCOORD0
) : COLOR0
{
   return Depth;
}



最后一个pass采样之前绘制的两个pass,将深度值相减,得到雾的厚度
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;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Program 3D Games in C++: The #1 Language at Top Game Studios Worldwide C++ remains the key language at many leading game development studios. Since it’s used throughout their enormous code bases, studios use it to maintain and improve their games, and look for it constantly when hiring new developers. Game Programming in C++ is a practical, hands-on approach to programming 3D video games in C++. Modeled on Sanjay Madhav’s game programming courses at USC, it’s fun, easy, practical, hands-on, and complete. Step by step, you’ll learn to use C++ in all facets of real-world game programming, including 2D and 3D graphics, physics, AI, audio, user interfaces, and much more. You’ll hone real-world skills through practical exercises, and deepen your expertise through start-to-finish projects that grow in complexity as you build your skills. Throughout, Madhav pays special attention to demystifying the math that all professional game developers need to know. Set up your C++ development tools quickly, and get started Implement basic 2D graphics, game updates, vectors, and game physics Build more intelligent games with widely used AI algorithms Implement 3D graphics with OpenGL, shaders, matrices, and transformations Integrate and mix audio, including 3D positional audio Detect collisions of objects in a 3D environment Efficiently respond to player input Build user interfaces, including Head-Up Displays (HUDs) Improve graphics quality with anisotropic filtering and deferred shading Load and save levels and binary game data Whether you’re a working developer or a student with prior knowledge of C++ and data structures, Game Programming in C++ will prepare you to solve real problems with C++ in roles throughout the game development lifecycle. You’ll master the language that top studios are hiring for—and that’s a proven route to success. Table of Contents Chapter 1: Game Programming Overview Chapter 2: Game Objects and 2D Graphics Chapter 3: Vectors and Basic Physics Chapter 4: A

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值