home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 January / Gamestar_69_2005-01_dvd.iso / Dema / theprojectv1-0.exe / FCData / Shaders.pak / Shaders / HWScripts / CryFX / GlassCM.cryFX < prev   
Encoding:
Text File  |  2004-10-27  |  7.9 KB  |  301 lines

  1.  
  2. #include "Common.cryFX"
  3.  
  4. /// Un-Tweakables //////////////////////
  5. float4x4 vpMatrix  : ViewProjection; // View*Projection
  6. float4x4 ObjToCubeSpace : InvObjMatrix;
  7.  
  8. float3 viewPos : OSCameraPos;
  9. float4 Ambient : WorldObjColor;
  10.  
  11. // Tweakables /////////////////
  12. float ReflectBumpScale
  13. <
  14.   vsregister = c4.x;
  15.   string UIWidget = "slider";
  16.   float UIMin = 0.0;
  17.   float UIMax = 2.0;
  18.   float UIStep = 0.1;
  19. > = 0.5;
  20.  
  21. float RefractBumpScale
  22. <
  23.   vsregister = c4.x;
  24.   string UIWidget = "slider";
  25.   float UIMin = 0.0;
  26.   float UIMax = 2.0;
  27.   float UIStep = 0.1;
  28. > = 0.5;
  29.  
  30. float Refraction
  31. <
  32.   vsregister = c4.y;
  33.   string UIWidget = "slider";
  34.   float UIMin = 0.0;
  35.   float UIMax = 3.0;
  36.   float UIStep = 0.1;
  37. > = 1.2;
  38.  
  39. float ReflectAmount
  40. <
  41.   psregister = c0.w;
  42.   string UIWidget = "slider";
  43.   float UIMin = 0.0;
  44.   float UIMax = 1.0;
  45.   float UIStep = 0.1;
  46. > = 0.3;
  47.  
  48. float RefractAmount
  49. <
  50.   psregister = c0.w;
  51.   string UIWidget = "slider";
  52.   float UIMin = 0.0;
  53.   float UIMax = 1.0;
  54.   float UIStep = 0.1;
  55. > = 0.7;
  56.  
  57. ///////////////////
  58.  
  59. texture normalMap : NORMAL
  60. <
  61.   string ResourceType = "2D";
  62. >;
  63.  
  64. texture cubeMap : ENVIRONMENT
  65. <
  66.   string ResourceType = "Cube";
  67. >;
  68.  
  69. sampler2D normalMapSampler = sampler_state
  70. {
  71.   Texture = <normalMap>;
  72.   MinFilter = Linear;
  73.   MagFilter = Linear;
  74.   MipFilter = Linear;
  75. };
  76.  
  77. samplerCUBE envMapSampler = sampler_state
  78. {
  79.   Texture = <cubeMap>;
  80.   MinFilter = Linear;
  81.   MagFilter = Linear;
  82.   MipFilter = Linear;
  83. };
  84.  
  85. /////////////////////////////
  86.  
  87. struct a2v
  88. {
  89.   float4 Position : POSITION; //in object space
  90.   float4 TexCoord : TEXCOORD0;
  91.   IN_TANG
  92.   float4 Color    : COLOR0; 
  93. };
  94.  
  95. struct v2f
  96. {
  97.   float4 Position  : POSITION;  //in projection space
  98.   float4 TexCoord0 : TEXCOORD0;
  99.   float4 TexCoord1 : TEXCOORD1; //first row of the 3x3 transform from tangent to cube space
  100.   float4 TexCoord2 : TEXCOORD2; //second row of the 3x3 transform from tangent to cube space
  101.   float4 TexCoord3 : TEXCOORD3; //third row of the 3x3 transform from tangent to cube space
  102.   float4 Color     : COLOR0;
  103.   float4 Color1    : COLOR1;
  104. # ifdef _FOG
  105.   float FogC       : FOG;       //Fog factor
  106. # endif
  107. };
  108.  
  109. #ifdef __REFLECT
  110.  
  111. ///////////////// vertex shader //////////////////
  112. v2f BumpReflectVS(a2v IN)
  113. {
  114.   v2f OUT = (v2f)0; 
  115.  
  116.   // Position in screen space.
  117.   float4 vPos = IN.Position;
  118.   OUT.Position = _pos_Common(vpMatrix, vPos);
  119.   
  120.   // pass texture coordinates for fetching the normal map
  121.   OUT.TexCoord0 = IN.TexCoord;
  122.  
  123.   // compute the 3x3 tranform from tangent space to object space
  124.   float3x3 objToTangentSpace;
  125.   // first rows are the tangent and binormal scaled by the bump scale
  126.   objToTangentSpace[0] = ReflectBumpScale * IN.Tangent;
  127.   objToTangentSpace[1] = ReflectBumpScale * IN.Binormal;
  128.   objToTangentSpace[2] = IN.TNormal;
  129.  
  130.   // compute the 3x3 transform from tangent space to cube space:
  131.   // TangentToCubeSpace = object2cube * tangent2object
  132.   //              = object2cube * transpose(objToTangentSpace) (since the inverse of a rotation is its transpose)
  133.   // so a row of TangentToCubeSpace is the transform by objToTangentSpace of the corresponding row of ObjToCubeSpace
  134.   OUT.TexCoord1.xyz = mul(objToTangentSpace, ObjToCubeSpace[0].xyz);
  135.   OUT.TexCoord2.xyz = mul(objToTangentSpace, ObjToCubeSpace[1].xyz);
  136.   OUT.TexCoord3.xyz = mul(objToTangentSpace, ObjToCubeSpace[2].xyz);
  137.  
  138.   // compute the eye vector (going from shaded point to eye) in cube space
  139.   float3 eyeVector = viewPos.xyz - vPos.xyz;
  140.   OUT.TexCoord1.w = dot(eyeVector, ObjToCubeSpace[0].xyz);
  141.   OUT.TexCoord2.w = dot(eyeVector, ObjToCubeSpace[1].xyz);
  142.   OUT.TexCoord3.w = dot(eyeVector, ObjToCubeSpace[2].xyz);
  143.   
  144.   OUT.Color = IN.Color;
  145.  
  146. # ifdef _HDR
  147.   OUT.Color1.xyzw = CalcFog(vpMatrix, vPos);;
  148. # endif        
  149.  
  150. # ifdef _FOG
  151.   OUT.FogC = CalcFog(vpMatrix, vPos);
  152. # endif
  153.   return OUT;
  154. }
  155.  
  156. ///////////////// pixel shader //////////////////
  157.  
  158. pixout BumpReflectPS(v2f IN)
  159. {
  160.   pixout OUT;
  161.  
  162.   // load the decal
  163.   float4 bumpNormal = tex2D(normalMapSampler, IN.TexCoord0.xy);
  164. # ifdef _PS_1_1
  165.   float4 env = texCUBE_reflect_dp3x3(envMapSampler, IN.TexCoord3, IN.TexCoord1, IN.TexCoord2, bumpNormal);
  166. # else
  167.   float3 E = float3(IN.TexCoord1.w, IN.TexCoord2.w, IN.TexCoord3.w);
  168.   float3 N = float3(dot(IN.TexCoord1.xyz, bumpNormal.xyz),
  169.                     dot(IN.TexCoord2.xyz, bumpNormal.xyz),
  170.                     dot(IN.TexCoord3.xyz, bumpNormal.xyz));
  171.   float4 env = texCUBE(envMapSampler, 2*(dot(N, E)/dot(N, N))*N - E);
  172. # endif
  173.  
  174. #ifdef __REFRACT
  175.   float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz * ReflectAmount);
  176. #else  
  177.   float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz);
  178. #endif
  179.   OUT.Color.xyz = HDRFogBlend(vColor.xyz, IN.Color1.w, GetFogColor());
  180.   OUT.Color.a = IN.Color.a * Ambient.a;
  181.  
  182.   return OUT;
  183. }
  184. #endif
  185.  
  186.  
  187. #ifdef __REFRACT
  188. ///////////////// vertex shader //////////////////
  189. v2f BumpRefractVS(a2v IN)
  190. {
  191.   v2f OUT = (v2f)0; 
  192.  
  193.   // Position in screen space.
  194.   float4 vPos = IN.Position;
  195.   OUT.Position = _pos_Common(vpMatrix, vPos);
  196.  
  197.   // pass texture coordinates for fetching the normal map
  198.   OUT.TexCoord0 = IN.TexCoord;
  199.   
  200.   float3 eyeVector = viewPos.xyz - vPos.xyz;
  201.  
  202.   TANG_MATR
  203.  
  204.   float3 trEyeVector = mul(objToTangentSpace, eyeVector);
  205.   
  206.   // fTemp = sqrt(n^2*|EYE|^2 - (EYE.x^2 + EYE.y^2)) - EYE.z
  207.   float3 eye2D = trEyeVector;
  208.   eye2D.z = 0;
  209.   float fTemp = sqrt(Refraction*Refraction*dot(trEyeVector, trEyeVector) - dot(eye2D, eye2D)) - trEyeVector.z;
  210.  
  211.   // Set the refraction matrix
  212.   // | REFRACT_0 |   | -TEMP.w   0       EYE.x*TEMP.w |
  213.   // | REFRACT_1 | = |  0       -TEMP.w  EYE.y*TEMP.w |
  214.   // | REFRACT_2 |   | -EYE.x   -EYE.y   -EYE.z       |
  215.   float3x3 refrMatr;
  216.   refrMatr[0] = float3(-fTemp, 0, fTemp * trEyeVector.x);
  217.   refrMatr[1] = float3(0, -fTemp, fTemp * trEyeVector.y);
  218.   refrMatr[2] = -trEyeVector;
  219.   
  220.   refrMatr[0] = refrMatr[0] * RefractBumpScale;
  221.   refrMatr[1] = refrMatr[1] * RefractBumpScale;
  222.   
  223.   // Calculate the 3x3 matrix for the pixel shader texm3x3tex
  224.   float3 vTemp0 = mul(objToTangentSpace, ObjToCubeSpace[0].xyz);
  225.   float3 vTemp1 = mul(objToTangentSpace, ObjToCubeSpace[1].xyz);
  226.   float3 vTemp2 = mul(objToTangentSpace, ObjToCubeSpace[2].xyz);
  227.  
  228.   OUT.TexCoord1.xyz = mul(refrMatr, vTemp0);
  229.   OUT.TexCoord2.xyz = mul(refrMatr, vTemp1);
  230.   OUT.TexCoord3.xyz = mul(refrMatr, vTemp2);
  231.   
  232.   OUT.Color = IN.Color;
  233.  
  234. # ifdef _HDR
  235.   OUT.Color1.xyzw = CalcFog(vpMatrix, vPos);;
  236. # endif        
  237.  
  238. # ifdef _FOG
  239.   OUT.FogC = CalcFog(vpMatrix, vPos);
  240. # endif
  241.   return OUT;
  242. }
  243.  
  244. ///////////////// pixel shader //////////////////
  245.  
  246. pixout BumpRefractPS(v2f IN)
  247. {
  248.   pixout OUT;
  249.  
  250.   // load the decal
  251.   float4 bumpNormal = tex2D(normalMapSampler, IN.TexCoord0.xy);
  252. # ifdef _PS_1_1
  253.   float4 env = texCUBE_dp3x3(envMapSampler, IN.TexCoord3.xyz, IN.TexCoord1, IN.TexCoord2, bumpNormal);
  254. # else
  255.   float3 newst = float3(dot(IN.TexCoord1.xyz, bumpNormal.xyz),
  256.                         dot(IN.TexCoord2.xyz, bumpNormal.xyz),
  257.                         dot(IN.TexCoord3.xyz, bumpNormal.xyz));
  258.   float4 env = texCUBE(envMapSampler, newst);
  259. # endif                
  260.   float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz * RefractAmount);
  261.   OUT.Color.xyz = HDRFogBlend(vColor.xyz, IN.Color1.w, GetFogColor());
  262.   OUT.Color.a = IN.Color.a * Ambient.a;
  263.  
  264.   return OUT;
  265. }
  266. #endif
  267.  
  268. //////////////////////////////// technique ////////////////
  269.  
  270. technique Glass
  271. {
  272. #ifdef __REFRACT
  273.   pass p0
  274.   {
  275.     VertexShader = compile vs_1_1 BumpRefractVS() 0x3;
  276.     
  277.     Zenable = true;
  278.     ZWriteEnable = true;
  279.     CullMode = Back;
  280.     
  281.     PixelShader = compile ps_1_1 BumpRefractPS() 0x3;
  282.   }
  283. #endif  
  284. #ifdef __REFLECT
  285.   pass p1
  286.   {
  287.     VertexShader = compile vs_1_1 BumpReflectVS() 0x3;
  288.     
  289.     Zenable = true;
  290.     ZWriteEnable = true;
  291.     CullMode = Back;
  292.  
  293.     SECOND_STATE
  294.  
  295.     PixelShader = compile ps_1_1 BumpReflectPS() 0x3;
  296.   }
  297. #endif  
  298. }
  299.  
  300. /////////////////////// eof ///
  301.