游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1781|回复: 2

How to calculate view transform in shader code

[复制链接]

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
发表于 2009-2-9 15:19:00 | 显示全部楼层 |阅读模式
How to calculate view transform in shader code?
I have a special requirement to calculate the view tranformation in Exponential Fog. I want to implement the pixel fog into shader code, becuase the objects in my scene all are used effect file. So I cannot use SetRenderState to set the pixel fog.

I saw the Fog Formulas (Direct3D 9). This part describes as below:


  1. For range based fog, the value for d is the distance between the camera position and a vertex.
  2. For non-range based fog, the value for d is the absolute value of the Z-coordinate in camera space.
复制代码


I have a confusion in the word of "the absolute value of the Z-coordinate in camera space". How do I calculate the value in shader code if the camera always is at the skydorme's top? And 3ds max DierctX Shader material will show the result synchonizately.(If I can calculate the view matrix from skydrome'top in shader code)

There are my shader code to calculate the fog as below:


  1. //vertex shader
  2. v2f av(a2v In, uniform float3 lightPosition)
  3. {
  4.         v2f Out = (v2f)0;
  5.         Out.position = mul(In.position, wvp);                                //transform vert position to homogeneous clip space

  6.         //this code was added by the IN UV Node
  7.         Out.texCoord = In.texCoord;                                                //pass through texture coordinates from channel 1

  8.         //FOG_NONE
  9.         if (UIConst_FogType==0)
  10.         {
  11.                 Out.fogLerpParam=0.0f;
  12.         }
  13.         else
  14.         {
  15.                 float4 tmPos;

  16.                 if (UIConst_FogType==1)//FOG_EXP1
  17.                 {
  18.                         tmPos=mul(In.position,world*view);

  19.                         float dist;
  20.                        
  21.                         #ifdef _3DSMAX_
  22.                                 dist=UIConst_FogRadius-tmPos.z;
  23.                         #else
  24.                                 dist=UIConst_FogRadius-tmPos.y;
  25.                         #endif
  26.                        
  27.                         Out.fogLerpParam = 1/exp(dist*UIConst_FogDensity);
  28.                 }
  29.                 else if (UIConst_FogType==2)//FOG_EXP2
  30.                 {       
  31.                         tmPos=mul(In.position,world*view);

  32.                         float dist;
  33.                        
  34.                         #ifdef _3DSMAX_
  35.                                 dist=UIConst_FogRadius-tmPos.z;
  36.                         #else
  37.                                 dist=UIConst_FogRadius-tmPos.y;
  38.                         #endif

  39.                         Out.fogLerpParam = 1/exp(pow(dist*UIConst_FogDensity,2));
  40.                 }
  41.                 else if (UIConst_FogType==3)//FOG_LINEAR
  42.                 {
  43.                         tmPos=mul(In.position,world);

  44.                         float gFogRange=UIConst_FogEnd-UIConst_FogStart;

  45.                         float dist;

  46.                         #ifdef _3DSMAX_
  47.                                 dist=UIConst_FogRadius-tmPos.z;
  48.                         #else
  49.                                 dist=UIConst_FogRadius-tmPos.y;
  50.                         #endif

  51.                         Out.fogLerpParam = saturate((dist - UIConst_FogStart) / gFogRange);
  52.                 }
  53.         }

  54.     return Out;
  55. }

  56. //pixel shader
  57. float4 af(v2f In, uniform float4 lightColor) : COLOR
  58. { ......
  59.         float4 ret =  float4(0,0,0,1);
  60.         ret = float4(input1, 1);

  61.         if (UIConst_FogType==0)
  62.         {
  63.        
  64.         }
  65.         else
  66.         {       
  67.                 float3 final = lerp(ret.rgb , UIColor_FogColor, In.fogLerpParam);
  68.                 ret=float4(final,1.0f);
  69.         }
  70.          
  71.         return ret;
  72. }
复制代码


PS:UIConst_XX is the UI widget showed in 3ds max's DirectX Shader material.

19

主题

638

帖子

638

积分

高级会员

Rank: 4

积分
638
发表于 2009-2-9 19:58:00 | 显示全部楼层

Re:How to calculate view transform in shader code

pass in the true view matrix through another uniform.

But seriously, you don't need to apply fog to the skydome.

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 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).


  1. //vertex shader
  2. v2f av(a2v In, uniform float3 lightPosition)
  3. {
  4.         v2f Out = (v2f)0;
  5.         Out.position = mul(In.position, wvp);                                //transform vert position to homogeneous clip space

  6.         //this code was added by the IN UV Node
  7.         Out.texCoord = In.texCoord;                                                //pass through texture coordinates from channel 1

  8.         //FOG_NONE
  9.         if (UIConst_FogType==0)
  10.         {
  11.                 Out.fogLerpParam=0.0f;
  12.         }
  13.         else
  14.         {
  15.                 float4 tmPos;
  16.                 float dist;

  17.                 float4 upW;

  18.                 #ifdef _3DSMAX_
  19.                         upW=float4(0.0f,0.0f,UIConst_FogRadius,1.0f);
  20.                 #else
  21.                         upW=float4(0.0f,UIConst_FogRadius,0.0f,1.0f);
  22.                 #endif

  23.                 tmPos=mul(In.position,world);

  24.                 if (UIConst_FogType==1)//FOG_EXP1
  25.                 {
  26.                         dist=distance(upW,tmPos)/UIConst_FogEnd*4.0f;
  27.                         Out.fogLerpParam = exp(-dist*UIConst_FogDensity);
  28.                 }
  29.                 else if (UIConst_FogType==2)//FOG_EXP2
  30.                 {       
  31.                         dist=distance(upW,tmPos)/UIConst_FogEnd*4.0f;
  32.                         Out.fogLerpParam = exp(-pow(dist*UIConst_FogDensity,2));
  33.                 }
  34.                 else if (UIConst_FogType==3)//FOG_LINEAR
  35.                 {
  36.                         float gFogRange=UIConst_FogEnd-UIConst_FogStart;
  37.                         dist=distance(upW,tmPos);
  38.                         Out.fogLerpParam = saturate((dist - UIConst_FogStart) / gFogRange);
  39.                 }
  40.         }

  41.     return Out;
  42. }

  43. //pixel shader
  44. float4 af(v2f In, uniform float4 lightColor) : COLOR
  45. { ......
  46.         float4 ret =  float4(0,0,0,1);
  47.         ret = float4(input1, 1);

  48.         if (UIConst_FogType==0)
  49.         {
  50.        
  51.         }
  52.         else
  53.         {
  54.                 if (UIConst_FogType==1 || UIConst_FogType==2)
  55.                 {
  56.                         float3 final = lerp(UIColor_FogColor.rgb , ret.rgb, In.fogLerpParam);
  57.                         ret=float4(final,1.0f);       
  58.                 }
  59.                 else if (UIConst_FogType==3)
  60.                 {
  61.                         float3 final = lerp(ret.rgb , UIColor_FogColor.rgb, In.fogLerpParam);
  62.                         ret=float4(final,1.0f);
  63.                 }       

  64.                 //ret=float4(UIColor_FogColor.rgb,1.0f);
  65.         }
  66.          
  67.         return ret;
  68. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 07:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表