好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#使用Shader实现夜幕降临倒计时的效果

最近火爆全球的pc游戏battlerite(战争仪式)在倒计时的会生成一种类似夜幕降临的效果,会以战场中心为圆心,某个长度为半径的范围外是暗的,而这个半径会逐渐缩小,而圆之外的阴暗部分是附着地形的,本文就尝试使用屏幕后处理的手段来实现这种效果。

(暂时缺少battlerite的截图,稍后会补上)

首先看效果图:

注:本文参考了tasharen fog of war插件

创建一个c#脚本,命名为nightfall.cs,为nightfall类创建一些公共变量(nightcolor,center和radius),另外还需要一个nightfall.shader。

首先,我们要确定这个效果是在场景渲染之后还未送到屏幕显示之前的实现的,所以,nightfall脚本是要挂载到主camera上的(添加特性[requirecomponent(typeof(camera))]),并要实现onrenderimage方法。

其次,在onrenderimage方法里,我们最终需要调用graphics.blit方法,而这个方法的第三个参数是material类型,所以我们需要在代码里创建一个临时材质,这个材质使用了nightfall.shader。

再次,我们需要在shader里面将屏幕坐标转换为世界坐标,来计算与世界中心的坐标,所以我们需要mvp的逆矩阵(参考shader山下(十六)坐标空间与转换矩阵)。

最后,为了附着地形,我们需要在shader计算深度,也就是坐标点与摄像机的相对距离,所以需要摄像机的位置。

c#的代码:

using unityengine;

?

[requirecomponent( typeof (camera))]

public class nightfall : monobehaviour

{

public shader shader;

public color nightcolor = new color(0.05f, 0.05f, 0.05f, 0.5f);

public vector3 center = vector3.zero;

public float radius = 10;

camera mcam;

matrix4x4 minversemvp;

material mmat;

/// the camera we're working with needs depth.

void onenable ()

{

mcam = getcomponent<camera>();

mcam.depthtexturemode = depthtexturemode.depth;

if (shader == null ) shader = shader.find( "image effects/nightfall" );

}

/// destroy the material when disabled.

void ondisable () { if (mmat) destroyimmediate(mmat); }

/// automatically disable the effect if the shaders don't support it.

void start ()

{

if (!systeminfo.supportsimageeffects || !shader || !shader.issupported)

{

enabled = false ;

}

}

// called by camera to apply image effect

void onrenderimage (rendertexture source, rendertexture destination)

{

print (nightcolor);

print (destination);

// calculate the inverse modelview-projection matrix to convert screen coordinates to world coordinates

minversemvp = (mcam.projectionmatrix * mcam.worldtocameramatrix).inverse;

if (mmat == null )

{

mmat = new material(shader);

mmat.hideflags = hideflags.hideanddontsave;

}

vector4 campos = mcam.transform.position;

// this accounts for anti-aliasing on windows flipping the depth uv coordinates.

// despite the official documentation, the following approach simply doesn't work:

// http://docs.unity3d.com/documentation/components/sl-platformdifferences.html

if (qualitysettings.antialiasing > 0)

{

runtimeplatform pl = application.platform;

if (pl == runtimeplatform.windowseditor ||

pl == runtimeplatform.windowsplayer ||

pl == runtimeplatform.windowswebplayer)

{

campos.w = 1f;

}

}

mmat.setvector( "_campos" , campos);

mmat.setmatrix( "_inversemvp" , minversemvp);

mmat.setcolor( "_nightcolor" , nightcolor);

mmat.setvector ( "_center" , center);

mmat.setfloat ( "_radius" , radius);

graphics.blit(source, destination, mmat);

}

}

shader代码:

?

shader "image effects/nightfall"

{

properties

{

_nightcolor ( "night color" , color) = (0.05, 0.05, 0.05, 0.05)

_center ( "center" , vector) = (0,0,0,0)

_radius ( "radius" , float ) = 10

}

subshader

{

pass

{

ztest always

cull off

zwrite off

fog { mode off }

blend srcalpha oneminussrcalpha

cgprogram

#pragma vertex vert_img

#pragma fragment frag vertex:vert

#pragma fragmentoption arb_precision_hint_fastest

#include "unitycg.cginc"

sampler2d _cameradepthtexture;

uniform float4x4 _inversemvp;

uniform float4 _campos;

uniform half4 _nightcolor;

uniform half4 _center;

uniform half _radius;

struct input

{

float4 position : position;

float2 uv : texcoord0;

};

void vert (inout appdata_full v, out input o)

{

o.position = mul(unity_matrix_mvp, v.vertex);

o.uv = v.texcoord.xy;

}

float3 camtoworld ( in float2 uv, in float depth)

{

float4 pos = float4(uv.x, uv.y, depth, 1.0);

pos.xyz = pos.xyz * 2.0 - 1.0;

pos = mul(_inversemvp, pos);

return pos.xyz / pos.w;

}

fixed4 frag (input i) : color

{

#if shader_api_d3d9 || shader_api_d3d11

float2 depthuv = i.uv;

depthuv.y = lerp(depthuv.y, 1.0 - depthuv.y, _campos.w);

float depth = unity_sample_depth(tex2d(_cameradepthtexture, depthuv));

float3 pos = camtoworld(depthuv, depth);

#else

float depth = unity_sample_depth(tex2d(_cameradepthtexture, i.uv));

float3 pos = camtoworld(i.uv, depth);

#endif

// limit to sea level

if (pos.y < 0.0)

{

// this is a simplified version of the ray-plane intersection formula: t = -( n.o + d ) / ( n.d )

float3 dir = normalize(pos - _campos.xyz);

pos = _campos.xyz - dir * (_campos.y / dir.y);

}

half4 col;

float dis = length(pos.xz - _center.xz);

if (dis < _radius)

{

col = fixed4(0,0,0,0);

}

else

{

col = _nightcolor;

}

return col;

}

endcg

}

}

fallback off

}

需要说明的几个点:

1、因为平台差异性,为了兼容direct3d,所以在c#和shader里通过campos(_campos)的w分量来调整uv坐标。

2、这里虽然没有声明_maintex,但是_maintex实际上就是即将成像的屏幕图像,所以这里的i.uv也就是指屏幕图像的纹理坐标。

3、_cameradepthtexture是摄像机的深度纹理,通过unity_sample_depth方法获取深度。

4、camtoworld里面,先是根据uv坐标和深度depth创建了一个float4的坐标值pos,然后对pos乘2减1是将这个坐标范围从[0,1]转换到了[-1,1],对应世界坐标。然后使用传入的mvp逆矩阵_inversemvp乘以这个坐标值,就得到了屏幕点的世界坐标。最后将pos的xyz分量除以w分量,这里w分量表示因为远近而产生的缩放值。

5、在计算过世界坐标之后,对于y小于0的坐标要做一下处理,将效果限制在海平面(sea level)之上,使用射线平面相交方程(ray-plane intersection formula)的简化版本来处理。

6、最后根据距离返回色彩值。

如果要实现夜幕降临倒计时的效果,只需要在控制脚本(c#)中获取camera上的nightfall组件,根据时间修改radius变量即可。

以上所述是小编给大家介绍的c#使用shader实现夜幕降临倒计时的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/ecidevilin/article/details/52930701

dy("nrwz");

查看更多关于C#使用Shader实现夜幕降临倒计时的效果的详细内容...

  阅读:64次