skydome.fx
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
- float4x4 mViewProj;
- float4x4 mInvViewProj;
- float4x4 mInvView;
- float4 view_position;
- float sun_alfa, sun_theta, sun_shininess, sun_strength;
- float intensity;
- float2 newuv;
- float4 skyinfo = float4(0.5, 0.7, 0.9, 0.15);
- float radius = 5000;
- float t ;
- float cloudiness = 1;
- float night_day;
- float fog;
- struct VS_INPUT
- {
- float3 Pos : POSITION;//顶点坐标
- };
- struct VS_OUTPUT
- {
- float4 Pos : POSITION;//顶点的投影坐标
- float2 v : TEXCOORD1;//云的纹理采样坐标
- float3 sun : TEXCOORD2;//太阳的位置
- float3 vvv : TEXCOORD4;//保存顶点坐标的变量
- };
- //云的纹理
- texture Clouds;
- //纹理采样器
- sampler2D clouds = sampler_state
- {
- texture = <Clouds>;
- MinFilter = LINEAR;
- MagFilter = LINEAR;
- AddressU = WRAP;
- AddressV = WRAP;
- };
- VS_OUTPUT VShader(VS_INPUT i)
- {
- VS_OUTPUT o;
-
- //保存顶点坐标以在像素着色器中用以计算太阳的色彩
- o.vvv = i.Pos ;
- o.Pos = mul(float4(radius*(i.Pos - float3(0, 0, 0)),1), mViewProj);
-
- //计算云的纹理坐标
- newuv.x = (acos(i.Pos.x) * 1.0 + 1.0 ) / 2;
- newuv.y = (acos(i.Pos.z) * 1.0 + 1.0 ) / 2;
-
- newuv.xy *= 1.5;
-
- //令云的纹理坐标随时间变化以使云达到运动的效果
- o.v.x = newuv.x + t * 0.266;
- o.v.y = newuv.y + t * 0.033;
-
- //对太阳高度角进行变换以实现太阳升落的效果
- sun_theta = sun_theta * abs(night_day * 2 - 1) - 0.1;
- //计算太阳的位置
- o.sun.y = sin(sun_theta);
- o.sun.x = cos(sun_theta)*sin(sun_alfa);
- o.sun.z = cos(sun_theta)*cos(sun_alfa);
-
- //白天和黑夜时分别让太阳(月亮)在不同的位置(相对)
- if(night_day < 0.5)
- {
- o.sun.x = -o.sun.x;
- o.sun.z = -o.sun.z;
- }
- return o;
- }
- float4 PShader(VS_OUTPUT i) : COLOR
- {
- float3 sunlight;
- float4 ut;
- //判断当前点是否在海面以下,若是则不计算
- if(i.vvv.y < -1)
- ut.rgba = 0;
- else
- {
- //计算当前处理的顶点的太阳的色彩
- //为太阳月亮赋予不同色彩
- if(night_day > 0.5)
- sunlight = (sun_strength*pow(saturate(dot(normalize(i.vvv), i.sun)),sun_shininess)*float3(1.0, 0.4, 0.1)) * night_day;
- else
- sunlight = (sun_strength*pow(saturate(dot(normalize(i.vvv), i.sun)),sun_shininess)*float3(0.7, 0.7, 0.8)) * (1 - night_day);
-
- //读取云的纹理
- float4 noise1 = tex2D(clouds, float2(i.v.x, i.v.y));
- //计算云的色彩
- intensity = 0.65;
- float4 cloudFrag = noise1 * intensity;
- float4 cloudColor = float4((1.0 - intensity)*skyinfo.x, (1.0 - intensity)*skyinfo.y,intensity*skyinfo.z, 0.0);
- cloudColor = cloudColor * (1 - 0.9 * cloudiness) + float4(0.9, 0.9, 0.9, 1) * 0.7 *cloudiness;
-
- ut.a = 1;
-
- float night_day_sqr = night_day * night_day ;
-
- //计算天空色彩
- ut = (
- ( cloudColor * (1.0 - cloudFrag.x ) + cloudFrag ) //* ( 1 - cloudiness )
- ) * night_day_sqr * pow(i.vvv.y , 1.5)
- + (1 - pow(i.vvv.y, 1.5) ) * float4(0.8, 0.8, 0.9, 1) * night_day_sqr * fog
- + float4(sunlight, 1) ;
- }
- return ut;
- }
- technique T0
- {
- pass P0
- {
- pixelshader = compile ps_2_0 PShader();
- vertexshader = compile vs_1_1 VShader();
- }
- }