|
|

楼主 |
发表于 2009-2-17 14:52:00
|
显示全部楼层
Re:How to calculate view transform in shader code
I had solved this problem by reference the book "ShaderX2 Introdutuion & Tutorials with DirectX 9"(Introduction to Different Fog Effects).
- //vertex shader
- v2f av(a2v In, uniform float3 lightPosition)
- {
- v2f Out = (v2f)0;
- Out.position = mul(In.position, wvp); //transform vert position to homogeneous clip space
- //this code was added by the IN UV Node
- Out.texCoord = In.texCoord; //pass through texture coordinates from channel 1
- //FOG_NONE
- if (UIConst_FogType==0)
- {
- Out.fogLerpParam=0.0f;
- }
- else
- {
- float4 tmPos;
- float dist;
- float4 upW;
- #ifdef _3DSMAX_
- upW=float4(0.0f,0.0f,UIConst_FogRadius,1.0f);
- #else
- upW=float4(0.0f,UIConst_FogRadius,0.0f,1.0f);
- #endif
- tmPos=mul(In.position,world);
- if (UIConst_FogType==1)//FOG_EXP1
- {
- dist=distance(upW,tmPos)/UIConst_FogEnd*4.0f;
- Out.fogLerpParam = exp(-dist*UIConst_FogDensity);
- }
- else if (UIConst_FogType==2)//FOG_EXP2
- {
- dist=distance(upW,tmPos)/UIConst_FogEnd*4.0f;
- Out.fogLerpParam = exp(-pow(dist*UIConst_FogDensity,2));
- }
- else if (UIConst_FogType==3)//FOG_LINEAR
- {
- float gFogRange=UIConst_FogEnd-UIConst_FogStart;
- dist=distance(upW,tmPos);
- Out.fogLerpParam = saturate((dist - UIConst_FogStart) / gFogRange);
- }
- }
- return Out;
- }
- //pixel shader
- float4 af(v2f In, uniform float4 lightColor) : COLOR
- { ......
- float4 ret = float4(0,0,0,1);
- ret = float4(input1, 1);
- if (UIConst_FogType==0)
- {
-
- }
- else
- {
- if (UIConst_FogType==1 || UIConst_FogType==2)
- {
- float3 final = lerp(UIColor_FogColor.rgb , ret.rgb, In.fogLerpParam);
- ret=float4(final,1.0f);
- }
- else if (UIConst_FogType==3)
- {
- float3 final = lerp(ret.rgb , UIColor_FogColor.rgb, In.fogLerpParam);
- ret=float4(final,1.0f);
- }
- //ret=float4(UIColor_FogColor.rgb,1.0f);
- }
-
- return ret;
- }
复制代码 |
|