home *** CD-ROM | disk | FTP | other *** search
-
- #include "Common.cryFX"
-
- /// Un-Tweakables //////////////////////
- float4x4 vpMatrix : ViewProjection; // View*Projection
- float4x4 ObjToCubeSpace : InvObjMatrix;
-
- float3 viewPos : OSCameraPos;
- float4 Ambient : WorldObjColor;
-
- // Tweakables /////////////////
- float ReflectBumpScale
- <
- vsregister = c4.x;
- string UIWidget = "slider";
- float UIMin = 0.0;
- float UIMax = 2.0;
- float UIStep = 0.1;
- > = 0.5;
-
- float RefractBumpScale
- <
- vsregister = c4.x;
- string UIWidget = "slider";
- float UIMin = 0.0;
- float UIMax = 2.0;
- float UIStep = 0.1;
- > = 0.5;
-
- float Refraction
- <
- vsregister = c4.y;
- string UIWidget = "slider";
- float UIMin = 0.0;
- float UIMax = 3.0;
- float UIStep = 0.1;
- > = 1.2;
-
- float ReflectAmount
- <
- psregister = c0.w;
- string UIWidget = "slider";
- float UIMin = 0.0;
- float UIMax = 1.0;
- float UIStep = 0.1;
- > = 0.3;
-
- float RefractAmount
- <
- psregister = c0.w;
- string UIWidget = "slider";
- float UIMin = 0.0;
- float UIMax = 1.0;
- float UIStep = 0.1;
- > = 0.7;
-
- ///////////////////
-
- texture normalMap : NORMAL
- <
- string ResourceType = "2D";
- >;
-
- texture cubeMap : ENVIRONMENT
- <
- string ResourceType = "Cube";
- >;
-
- sampler2D normalMapSampler = sampler_state
- {
- Texture = <normalMap>;
- MinFilter = Linear;
- MagFilter = Linear;
- MipFilter = Linear;
- };
-
- samplerCUBE envMapSampler = sampler_state
- {
- Texture = <cubeMap>;
- MinFilter = Linear;
- MagFilter = Linear;
- MipFilter = Linear;
- };
-
- /////////////////////////////
-
- struct a2v
- {
- float4 Position : POSITION; //in object space
- float4 TexCoord : TEXCOORD0;
- IN_TANG
- float4 Color : COLOR0;
- };
-
- struct v2f
- {
- float4 Position : POSITION; //in projection space
- float4 TexCoord0 : TEXCOORD0;
- float4 TexCoord1 : TEXCOORD1; //first row of the 3x3 transform from tangent to cube space
- float4 TexCoord2 : TEXCOORD2; //second row of the 3x3 transform from tangent to cube space
- float4 TexCoord3 : TEXCOORD3; //third row of the 3x3 transform from tangent to cube space
- float4 Color : COLOR0;
- float4 Color1 : COLOR1;
- # ifdef _FOG
- float FogC : FOG; //Fog factor
- # endif
- };
-
- #ifdef __REFLECT
-
- ///////////////// vertex shader //////////////////
- v2f BumpReflectVS(a2v IN)
- {
- v2f OUT = (v2f)0;
-
- // Position in screen space.
- float4 vPos = IN.Position;
- OUT.Position = _pos_Common(vpMatrix, vPos);
-
- // pass texture coordinates for fetching the normal map
- OUT.TexCoord0 = IN.TexCoord;
-
- // compute the 3x3 tranform from tangent space to object space
- float3x3 objToTangentSpace;
- // first rows are the tangent and binormal scaled by the bump scale
- objToTangentSpace[0] = ReflectBumpScale * IN.Tangent;
- objToTangentSpace[1] = ReflectBumpScale * IN.Binormal;
- objToTangentSpace[2] = IN.TNormal;
-
- // compute the 3x3 transform from tangent space to cube space:
- // TangentToCubeSpace = object2cube * tangent2object
- // = object2cube * transpose(objToTangentSpace) (since the inverse of a rotation is its transpose)
- // so a row of TangentToCubeSpace is the transform by objToTangentSpace of the corresponding row of ObjToCubeSpace
- OUT.TexCoord1.xyz = mul(objToTangentSpace, ObjToCubeSpace[0].xyz);
- OUT.TexCoord2.xyz = mul(objToTangentSpace, ObjToCubeSpace[1].xyz);
- OUT.TexCoord3.xyz = mul(objToTangentSpace, ObjToCubeSpace[2].xyz);
-
- // compute the eye vector (going from shaded point to eye) in cube space
- float3 eyeVector = viewPos.xyz - vPos.xyz;
- OUT.TexCoord1.w = dot(eyeVector, ObjToCubeSpace[0].xyz);
- OUT.TexCoord2.w = dot(eyeVector, ObjToCubeSpace[1].xyz);
- OUT.TexCoord3.w = dot(eyeVector, ObjToCubeSpace[2].xyz);
-
- OUT.Color = IN.Color;
-
- # ifdef _HDR
- OUT.Color1.xyzw = CalcFog(vpMatrix, vPos);;
- # endif
-
- # ifdef _FOG
- OUT.FogC = CalcFog(vpMatrix, vPos);
- # endif
- return OUT;
- }
-
- ///////////////// pixel shader //////////////////
-
- pixout BumpReflectPS(v2f IN)
- {
- pixout OUT;
-
- // load the decal
- float4 bumpNormal = tex2D(normalMapSampler, IN.TexCoord0.xy);
- # ifdef _PS_1_1
- float4 env = texCUBE_reflect_dp3x3(envMapSampler, IN.TexCoord3, IN.TexCoord1, IN.TexCoord2, bumpNormal);
- # else
- float3 E = float3(IN.TexCoord1.w, IN.TexCoord2.w, IN.TexCoord3.w);
- float3 N = float3(dot(IN.TexCoord1.xyz, bumpNormal.xyz),
- dot(IN.TexCoord2.xyz, bumpNormal.xyz),
- dot(IN.TexCoord3.xyz, bumpNormal.xyz));
- float4 env = texCUBE(envMapSampler, 2*(dot(N, E)/dot(N, N))*N - E);
- # endif
-
- #ifdef __REFRACT
- float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz * ReflectAmount);
- #else
- float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz);
- #endif
- OUT.Color.xyz = HDRFogBlend(vColor.xyz, IN.Color1.w, GetFogColor());
- OUT.Color.a = IN.Color.a * Ambient.a;
-
- return OUT;
- }
- #endif
-
-
- #ifdef __REFRACT
- ///////////////// vertex shader //////////////////
- v2f BumpRefractVS(a2v IN)
- {
- v2f OUT = (v2f)0;
-
- // Position in screen space.
- float4 vPos = IN.Position;
- OUT.Position = _pos_Common(vpMatrix, vPos);
-
- // pass texture coordinates for fetching the normal map
- OUT.TexCoord0 = IN.TexCoord;
-
- float3 eyeVector = viewPos.xyz - vPos.xyz;
-
- TANG_MATR
-
- float3 trEyeVector = mul(objToTangentSpace, eyeVector);
-
- // fTemp = sqrt(n^2*|EYE|^2 - (EYE.x^2 + EYE.y^2)) - EYE.z
- float3 eye2D = trEyeVector;
- eye2D.z = 0;
- float fTemp = sqrt(Refraction*Refraction*dot(trEyeVector, trEyeVector) - dot(eye2D, eye2D)) - trEyeVector.z;
-
- // Set the refraction matrix
- // | REFRACT_0 | | -TEMP.w 0 EYE.x*TEMP.w |
- // | REFRACT_1 | = | 0 -TEMP.w EYE.y*TEMP.w |
- // | REFRACT_2 | | -EYE.x -EYE.y -EYE.z |
- float3x3 refrMatr;
- refrMatr[0] = float3(-fTemp, 0, fTemp * trEyeVector.x);
- refrMatr[1] = float3(0, -fTemp, fTemp * trEyeVector.y);
- refrMatr[2] = -trEyeVector;
-
- refrMatr[0] = refrMatr[0] * RefractBumpScale;
- refrMatr[1] = refrMatr[1] * RefractBumpScale;
-
- // Calculate the 3x3 matrix for the pixel shader texm3x3tex
- float3 vTemp0 = mul(objToTangentSpace, ObjToCubeSpace[0].xyz);
- float3 vTemp1 = mul(objToTangentSpace, ObjToCubeSpace[1].xyz);
- float3 vTemp2 = mul(objToTangentSpace, ObjToCubeSpace[2].xyz);
-
- OUT.TexCoord1.xyz = mul(refrMatr, vTemp0);
- OUT.TexCoord2.xyz = mul(refrMatr, vTemp1);
- OUT.TexCoord3.xyz = mul(refrMatr, vTemp2);
-
- OUT.Color = IN.Color;
-
- # ifdef _HDR
- OUT.Color1.xyzw = CalcFog(vpMatrix, vPos);;
- # endif
-
- # ifdef _FOG
- OUT.FogC = CalcFog(vpMatrix, vPos);
- # endif
- return OUT;
- }
-
- ///////////////// pixel shader //////////////////
-
- pixout BumpRefractPS(v2f IN)
- {
- pixout OUT;
-
- // load the decal
- float4 bumpNormal = tex2D(normalMapSampler, IN.TexCoord0.xy);
- # ifdef _PS_1_1
- float4 env = texCUBE_dp3x3(envMapSampler, IN.TexCoord3.xyz, IN.TexCoord1, IN.TexCoord2, bumpNormal);
- # else
- float3 newst = float3(dot(IN.TexCoord1.xyz, bumpNormal.xyz),
- dot(IN.TexCoord2.xyz, bumpNormal.xyz),
- dot(IN.TexCoord3.xyz, bumpNormal.xyz));
- float4 env = texCUBE(envMapSampler, newst);
- # endif
- float3 vColor = HDREncodeAmb(env.xyz * IN.Color.xyz * RefractAmount);
- OUT.Color.xyz = HDRFogBlend(vColor.xyz, IN.Color1.w, GetFogColor());
- OUT.Color.a = IN.Color.a * Ambient.a;
-
- return OUT;
- }
- #endif
-
- //////////////////////////////// technique ////////////////
-
- technique Glass
- {
- #ifdef __REFRACT
- pass p0
- {
- VertexShader = compile vs_1_1 BumpRefractVS() 0x3;
-
- Zenable = true;
- ZWriteEnable = true;
- CullMode = Back;
-
- PixelShader = compile ps_1_1 BumpRefractPS() 0x3;
- }
- #endif
- #ifdef __REFLECT
- pass p1
- {
- VertexShader = compile vs_1_1 BumpReflectVS() 0x3;
-
- Zenable = true;
- ZWriteEnable = true;
- CullMode = Back;
-
- SECOND_STATE
-
- PixelShader = compile ps_1_1 BumpReflectPS() 0x3;
- }
- #endif
- }
-
- /////////////////////// eof ///
-