home *** CD-ROM | disk | FTP | other *** search
/ Geek Games #12 / GEGA012.iso / eroticos / dreamstrippergdidemoinstall.exe / DreamStripper.msi / _8A941BDB14F779B8105FDFAA799C6946 / _8C0B520907B14ACDA0FC7848EF03590F < prev    next >
Text File  |  2005-05-20  |  31KB  |  1,055 lines

  1. /*********************************************************************
  2. //*
  3. //*Copyright (c) 2001-2005, Ensign Games Ltd.  All rights reserved.
  4. //*    
  5. //*********************************************************************/
  6. //vertex shader consants
  7. float4x4 WorldViewProj        : WORLDVIEWPROJ        : register(vs, c0);
  8. float4x4 World                : WORLD                : register(vs, c4);
  9. float4 MorphWeights            : MORPHWEIGHTS        : register(vs, c8);
  10. float3 ModelSpaceCamera        : MODELSPACECAMERA    : register(vs, c9);
  11. float3 ModelSpaceLight1        : MODELSPACELIGHT1    : register(vs, c10);
  12. float3 ModelSpaceLight2        : MODELSPACELIGHT2    : register(vs, c11);
  13. float2 InverseLightRange1    : INVERSELIGHTRANGE1: register(vs, c12);
  14. float2 InverseLightRange2    : INVERSELIGHTRANGE2: register(vs, c13);
  15. float4x4 ShadowMapTexProj1    : SHADOWMAPTEXPROJ1    : register(vs, c14);//used to project the shadow map
  16. float4x4 ShadowMapTexProj2    : SHADOWMAPTEXPROJ2    : register(vs, c18);//used to project the shadow map
  17. float4x4 LightMapTexProj1    : LIGHTMAPTEXPROJ1    : register(vs, c22);//used to project the light map
  18. float4x4 LightMapTexProj2    : LIGHTMAPTEXPROJ2    : register(vs, c26);//used to project the light map
  19. float ShadowMapDepthBias    : SHADOWMAPDEPTHBIAS: register(vs, c30);
  20. float3 LightDirection1        : LIGHTDIRECTION1    : register(vs, c31);
  21. float3 LightDirection2        : LIGHTDIRECTION2    : register(vs, c32);
  22. #if defined(GPU_SKINNING)
  23.     float4x4 WorldPoseArray[BONE_ARRAY_SIZE]    : WORLDPOSEARRAY    : register(vs, c40);
  24. #endif
  25.  
  26. //pixel shader constants
  27. float4 DiffuseMaterial        : DIFFUSE            : register(ps, c0);
  28. float4 SpecularMaterial        : SPECULAR            : register(ps, c1);
  29. float4 AmbientLight            : AMBIENT            : register(ps, c2);
  30. float  Fade                    : FADE                : register(ps, c3);
  31. float4 LightColor1            : LIGHTCOLOR1        : register(ps, c4);
  32. float4 LightColor2            : LIGHTCOLOR2        : register(ps, c5);
  33. float  SpecularPower        : SPECULARPOWER        : register(ps, c6);
  34. float  SpecularScale        : SPECULARSCALE        : register(ps, c7);
  35. float2 KajiyaKayPowers        : KAJIYAKAYPOWERS    : register(ps, c8);
  36. float2 KajiyaKayOffsets        : KAJIYAKAYOFFSETS    : register(ps, c9);
  37. float4 KajiyaKayColor1        : KAJIYAKAYCOLOR1    : register(ps, c10);
  38. float4 KajiyaKayColor2        : KAJIYAKAYCOLOR2    : register(ps, c11);
  39. float  PassMultiplier        : PASSMULTIPLIER    : register(ps, c12);
  40.  
  41. //constants for both
  42. float BumpFactor            : BUMPFACTOR        : register(vs, c33) : register(ps, c13);
  43.  
  44. sampler DiffuseMap    : register(s0);
  45. sampler LightMap    : register(s1);
  46. sampler NormalMap    : register(s2);
  47. sampler ShadowMap    : register(s3);
  48. sampler ShadowMap2    : register(s4);
  49. #define BlurMap ShadowMap
  50.  
  51. //for shadow mapping
  52. #define ShadowLookupMap NormalMap
  53.  
  54. const bool g_bUseNormalMap = true;
  55. const bool g_bSpecular = true;
  56. const bool g_bShadowed = true;
  57. const bool g_bKajiyaKay = false;
  58. const float g_fShadowMapResolution = 2.0f;
  59.  
  60.  
  61. //standard input for this effect
  62. struct VS_INPUT
  63. {
  64.     float3    Position        : POSITION;
  65. #if defined(GPU_SKINNING)
  66.     float4    BlendWeight        : BLENDWEIGHT;
  67.     #if defined(INDEXED_GPU_SKINNING) == false
  68.         int4    BlendIndices    : BLENDINDICES;
  69.     #endif
  70. #endif
  71.     float3    Normal            : NORMAL;
  72. #if defined (NORMAL_MAP)    
  73.     float3    Tangent            : TANGENT;
  74. #endif
  75. #if defined (VERTEX_COLORS)
  76.     float4 Color0            : COLOR0;
  77. #endif
  78.     float2    TexCoord        : TEXCOORD0;
  79. #if defined(SECOND_TEXCOORDS)
  80.     float2  TexCoord1        : TEXCOORD1;
  81. #endif
  82. #if defined(INDEXED_GPU_SKINNING)
  83.     int4    BlendIndices    : BLENDINDICES;
  84. #endif
  85. #if MORPH_TARGET >= 1
  86.     float3 Position1        : POSITION1;
  87.     float3 Normal1            : NORMAL1;
  88. #endif
  89. #if MORPH_TARGET >= 2
  90.     float3 Position2        : POSITION2;
  91.     float3 Normal2            : NORMAL2;
  92. #endif
  93. #if MORPH_TARGET >= 3    
  94.     float3 Position3        : POSITION3;
  95.     float3 Normal3            : NORMAL3;
  96. #endif
  97. #if MORPH_TARGET >= 4    
  98.     float3 Position4        : POSITION4;
  99.     float3 Normal4            : NORMAL4;
  100. #endif
  101. };
  102.  
  103. //declaration to be read by the engine
  104. VS_INPUT VertexDeclaration;
  105. struct VS_OUTPUT_LIGHT
  106. {    
  107.     float4 Position                : POSITION;
  108.     float4 TexCoord                : TEXCOORD0;//xy is pass through, zw is transformed by uv stuff
  109.     float4 VertexToCamera        : TEXCOORD1;
  110.     float4 LightMapTexCoord        : TEXCOORD2;//holds light map texcoords
  111.     float4 ShadowMapTexCoord    : TEXCOORD3;//holds light map and shadow map texcoords
  112.     float3x3 TangentToModelSpace: TEXCOORD4;
  113.     float4 VertexToLight1        : TEXCOORD7;//4th component holds depth
  114.     float4 VertexToLight2        : COLOR0;//4th component holds depth
  115.     float4 Halfway                : COLOR1;//4th component holds VdotH
  116.     //major packing:
  117.         //Halfway2 is in VertexToCamera.w, LightMapTexCoord.z, ShadowMapTexCoord.z
  118. };
  119.  
  120. ////////////////////////////////////////////////////////////////
  121. //VERTEX SHADER helper functions
  122. ////////////////////////////////////////////////////////////////
  123. #if defined(GPU_SKINNING)
  124. float4 vsDoSkinning(in VS_INPUT In)
  125. {
  126.     float4 Position =  mul(float4(In.Position,1), WorldPoseArray[In.BlendIndices.x]) * In.BlendWeight.x;
  127.     Position += mul(float4(In.Position,1), WorldPoseArray[In.BlendIndices.y]) * In.BlendWeight.y;
  128.     Position += mul(float4(In.Position,1), WorldPoseArray[In.BlendIndices.z]) * In.BlendWeight.z;
  129.     Position += mul(float4(In.Position,1), WorldPoseArray[In.BlendIndices.w]) * In.BlendWeight.w;
  130.     Position /= Position.w;
  131.     return Position;
  132. }
  133. #endif
  134.  
  135. float4 vsGetLocalPosition(in VS_INPUT In)
  136. {
  137. #if MORPH_TARGET >= 1
  138.     In.Position += In.Position1 * MorphWeights.x;
  139. #endif    
  140. #if MORPH_TARGET >= 2    
  141.     In.Position += In.Position2 * MorphWeights.y;
  142. #endif    
  143. #if MORPH_TARGET >= 3    
  144.     In.Position += In.Position3 * MorphWeights.z;
  145. #endif    
  146. #if MORPH_TARGET >= 4    
  147.     In.Position += In.Position4 * MorphWeights.w;
  148. #endif    
  149.     
  150.     //transform to clip space and calculate world space position
  151. #if defined(GPU_SKINNING)
  152.     return vsDoSkinning(In);
  153. #else
  154.     return float4(In.Position,1);
  155. #endif
  156.  
  157. }
  158. float4 vsDoPositionTransform(in VS_INPUT In, out float4 Position, uniform bool bCalculateLocalPosition = false, uniform bool bCalculateWorldPosition = false)
  159. {
  160.     float4 LocalPosition = 0;
  161.  
  162. #if MORPH_TARGET >= 1
  163.     In.Position += In.Position1 * MorphWeights.x;
  164. #endif    
  165. #if MORPH_TARGET >= 2    
  166.     In.Position += In.Position2 * MorphWeights.y;
  167. #endif    
  168. #if MORPH_TARGET >= 3    
  169.     In.Position += In.Position3 * MorphWeights.z;
  170. #endif    
  171. #if MORPH_TARGET >= 4    
  172.     In.Position += In.Position4 * MorphWeights.w;
  173. #endif    
  174.     
  175.     //transform to clip space and calculate world space position
  176. #if defined(GPU_SKINNING)
  177.     Position = vsDoSkinning(In);
  178.         
  179.     if(bCalculateLocalPosition)
  180.         LocalPosition = Position;
  181.         
  182.     if(bCalculateWorldPosition)
  183.         LocalPosition = mul(Position, World);
  184.         
  185.     Position = mul(Position, WorldViewProj);
  186. #else
  187.     Position = mul(float4(In.Position,1), WorldViewProj);
  188.     
  189.     if(bCalculateLocalPosition)
  190.         LocalPosition = float4(In.Position,1);
  191.         
  192.     if(bCalculateWorldPosition)
  193.         LocalPosition = mul(float4(In.Position,1), World);
  194.     
  195. #endif
  196.  
  197.     return LocalPosition;
  198. }
  199.  
  200. void vsGetTangentSpace(in VS_INPUT In, out float3x3 TSMatrix)
  201. {
  202. #if MORPH_TARGET >= 1
  203.     In.Normal += In.Normal1 * MorphWeights.x;
  204. #endif    
  205. #if MORPH_TARGET >= 2    
  206.     In.Normal += In.Normal2 * MorphWeights.y;
  207. #endif    
  208. #if MORPH_TARGET >= 3    
  209.     In.Normal += In.Normal3 * MorphWeights.z;
  210. #endif    
  211. #if MORPH_TARGET >= 4    
  212.     In.Normal += In.Normal4 * MorphWeights.w;
  213. #endif    
  214.  
  215. #if defined(NORMAL_MAP)
  216.     #if defined(GPU_SKINNING)
  217.         TSMatrix[2] = mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.x]).xyz * In.BlendWeight.x;
  218.         TSMatrix[2] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.y]).xyz * In.BlendWeight.y;
  219.         TSMatrix[2] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.z]).xyz * In.BlendWeight.z;
  220.         TSMatrix[2] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.w]).xyz * In.BlendWeight.w;
  221.         TSMatrix[2] = normalize(TSMatrix[2]);
  222.  
  223.         #if MORPH_TARGET >= 1
  224.             In.Tangent = In.Tangent - dot(In.Tangent, In.Normal)*In.Normal;
  225.         #endif
  226.  
  227.         TSMatrix[0] = mul(float4(In.Tangent,0), WorldPoseArray[In.BlendIndices.x]).xyz * In.BlendWeight.x;
  228.         TSMatrix[0] += mul(float4(In.Tangent,0), WorldPoseArray[In.BlendIndices.y]).xyz * In.BlendWeight.y;
  229.         TSMatrix[0] += mul(float4(In.Tangent,0), WorldPoseArray[In.BlendIndices.z]).xyz * In.BlendWeight.z;
  230.         TSMatrix[0] += mul(float4(In.Tangent,0), WorldPoseArray[In.BlendIndices.w]).xyz * In.BlendWeight.w;
  231.         TSMatrix[0] = normalize(TSMatrix[0]);
  232.         
  233.         TSMatrix[1] = normalize(cross(TSMatrix[0], TSMatrix[2]));
  234.         
  235.     #else
  236.         TSMatrix[2] = In.Normal;
  237.         TSMatrix[0] = In.Tangent;
  238.         TSMatrix[1] = normalize(cross(TSMatrix[0], TSMatrix[2]));
  239.     #endif
  240.     
  241.     TSMatrix[0] *= BumpFactor;
  242.     TSMatrix[1] *= BumpFactor;
  243.     
  244. #else
  245.     #if defined(GPU_SKINNING)
  246.         TSMatrix[0] = mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.x]).xyz * In.BlendWeight.x;
  247.         TSMatrix[0] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.y]).xyz * In.BlendWeight.y;
  248.         TSMatrix[0] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.z]).xyz * In.BlendWeight.z;
  249.         TSMatrix[0] += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.w]).xyz * In.BlendWeight.w;
  250.         TSMatrix[0] = normalize(TSMatrix[0]);
  251.     #else
  252.         TSMatrix[0] = In.Normal;
  253.     #endif
  254.     
  255.     TSMatrix[1] = 0;
  256.     TSMatrix[2] = 0;
  257. #endif
  258. }
  259.  
  260. float3 vsGetNormal(in VS_INPUT In)
  261. {
  262.     float3 Normal;
  263.  
  264. #if MORPH_TARGET >= 1
  265.     In.Normal += In.Normal1 * MorphWeights.x;
  266. #endif    
  267. #if MORPH_TARGET >= 2    
  268.     In.Normal += In.Normal2 * MorphWeights.y;
  269. #endif    
  270. #if MORPH_TARGET >= 3    
  271.     In.Normal += In.Normal3 * MorphWeights.z;
  272. #endif    
  273. #if MORPH_TARGET >= 4    
  274.     In.Normal += In.Normal4 * MorphWeights.w;
  275. #endif    
  276.  
  277. #if defined(GPU_SKINNING)
  278.  
  279.     Normal = mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.x]).xyz * In.BlendWeight.x;
  280.     Normal += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.y]).xyz * In.BlendWeight.y;
  281.     Normal += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.z]).xyz * In.BlendWeight.z;
  282.     Normal += mul(float4(In.Normal,0), WorldPoseArray[In.BlendIndices.w]).xyz * In.BlendWeight.w;
  283.     Normal = normalize(Normal);
  284. #else
  285.     Normal = In.Normal;
  286. #endif
  287.     return Normal;
  288. }
  289.  
  290. ////////////////////////////////////////////////////////////////
  291. //PIXEL SHADER helper functions
  292. ////////////////////////////////////////////////////////////////
  293. float psGetShadowCoefficient(uniform sampler ShadowMapSampler, in float fDepth, in float4 ShadowTexCoord, uniform float2 texmapscale, uniform bool bLessSamples = false)
  294. {
  295.     float fShadowCoefficient = 0.0f;
  296.     
  297. #if defined(FLOATING_POINT_SHADOW_MAP)
  298.     float x,y;
  299.     
  300.     float2 Center = ShadowTexCoord.xy / ShadowTexCoord.w;
  301.  
  302.     for(y = -0.5f; y <= 0.6f; y += 1.0f)
  303.     {
  304.         for(x = -0.5f; x <= 0.6f; x += 1.0f)
  305.         {
  306.             float fShadowDepth = tex2D(ShadowMapSampler, Center + float2(x,y)*texmapscale);
  307.             if(fDepth <= fShadowDepth)//the pixel is closest to the light
  308.                 fShadowCoefficient += 0.25f;
  309.         }
  310.     }
  311.     
  312.     
  313. #elif defined(SHADOW_MAP_LOOKUP)
  314.  
  315.     const float2 unpack= float2(0.125f, 1.0f);
  316.     float x,y;
  317.     float2 Center = ShadowTexCoord.xy / ShadowTexCoord.w;
  318.     
  319.     float2 fShadowDepthPacked = tex2D(ShadowMapSampler, Center + float2(0.0f,0.5f)*texmapscale).rg;
  320.     float fShadowDepth = dot(fShadowDepthPacked.rg, unpack);
  321.     if(fDepth <= fShadowDepth)//the pixel is closest to the light
  322.         fShadowCoefficient += 0.333333333f;
  323.     
  324.     fShadowDepthPacked = tex2D(ShadowMapSampler, Center + float2(-0.4f,-0.5f)*texmapscale).rg;
  325.     fShadowDepth = dot(fShadowDepthPacked.rg, unpack);
  326.     if(fDepth <= fShadowDepth)//the pixel is closest to the light
  327.         fShadowCoefficient += 0.333333333f;
  328.     
  329.     fShadowDepthPacked = tex2D(ShadowMapSampler, Center + float2(0.4f,-0.5f)*texmapscale).rg;
  330.     fShadowDepth = dot(fShadowDepthPacked.rg, unpack);
  331.     if(fDepth <= fShadowDepth)//the pixel is closest to the light
  332.         fShadowCoefficient += 0.333333333f;
  333. #endif
  334.     return fShadowCoefficient;
  335. }
  336.  
  337.  
  338. ////////////////////////////////////////////////////////////////
  339. //Ambient pass shaders
  340. ////////////////////////////////////////////////////////////////
  341. struct VS_OUTPUT_AMBIENT
  342. {
  343.     float4 Position                : POSITION;
  344.     float2 TexCoord                : TEXCOORD0;
  345. #if defined (VERTEX_COLORS)
  346.     float4 Color0                : COLOR0;
  347. #endif    
  348.     
  349. };
  350.  
  351. VS_OUTPUT_AMBIENT VS(VS_INPUT In)
  352. {
  353.     VS_OUTPUT_AMBIENT Out = (VS_OUTPUT_AMBIENT)0;
  354.     
  355.     vsDoPositionTransform(In, Out.Position);
  356.  
  357.     Out.TexCoord.xy = In.TexCoord;
  358.     
  359. #if defined (VERTEX_COLORS)
  360.     Out.Color0 = In.Color0;
  361. #endif
  362.     return Out;
  363. }
  364.  
  365. float4 PS(VS_OUTPUT_AMBIENT In) : COLOR
  366. {
  367.     float4 Out = DiffuseMaterial;
  368. #if defined(DIFFUSE_MAP)
  369.     Out = tex2D(DiffuseMap, In.TexCoord.xy);
  370. #endif
  371. #if defined (VERTEX_COLORS)
  372.     Out.xyz *= AmbientLight.xyz + In.Color0.xyz;
  373. #else
  374.     Out.xyz *= AmbientLight;
  375. #endif    
  376.  
  377.     
  378.     
  379.     return Out;
  380. }
  381.  
  382. //////////////////////////////////////////////////////////////////
  383. //Depth Encoding shaders
  384. //////////////////////////////////////////////////////////////////
  385. struct VS_OUTPUT_SHADOWMAP
  386. {
  387.     float4 Position                : POSITION;
  388.     float2 Depth                : TEXCOORD0;
  389. };
  390.  
  391. VS_OUTPUT_SHADOWMAP vs_shadowmap(VS_INPUT In)
  392. {
  393.     VS_OUTPUT_SHADOWMAP Out = (VS_OUTPUT_SHADOWMAP)0;
  394.     
  395.     float4 LocalPosition = vsDoPositionTransform(In, Out.Position, true);
  396.     float3 VertexToLight = ModelSpaceCamera - LocalPosition.xyz;
  397.     Out.Depth = (length(VertexToLight) - InverseLightRange1.y) * InverseLightRange1.x;
  398.  
  399.     return Out;
  400. }
  401.  
  402. float4 ps_shadowmap(VS_OUTPUT_SHADOWMAP In) : COLOR
  403. {
  404.     #if defined(SHADOW_MAP_LOOKUP)
  405.         float4 Out = 1.0f;
  406.         Out.xyz = tex1D(ShadowLookupMap, In.Depth);
  407.         return Out;
  408.     #else
  409.         return In.Depth.x;
  410.     #endif
  411. }
  412.  
  413. //////////////////////////////////////////////////////////////////
  414. //light with spotlight
  415. //////////////////////////////////////////////////////////////////
  416. VS_OUTPUT_LIGHT vs_spotlight(VS_INPUT In,
  417.                                     uniform bool bOutputTextureSpace,
  418.                                     uniform bool bUseTangentSpace,
  419.                                     uniform bool bShadowed,
  420.                                     uniform bool bTwoLights = false)
  421. {
  422.     VS_OUTPUT_LIGHT Out = (VS_OUTPUT_LIGHT)0;
  423.     
  424.     float4 LocalPosition;
  425.     
  426.     #if defined(BLUR_SHADOWS) && defined(SECOND_TEXCOORDS)
  427.         Out.TexCoord.zw = In.TexCoord1.xy;
  428.     #else
  429.         Out.TexCoord.zw = In.TexCoord.xy;
  430.     #endif
  431.     
  432.     if(bOutputTextureSpace)
  433.     {
  434.         Out.Position.xy = 1.0f - Out.TexCoord.zw * 2.0f;
  435.         Out.Position.x *= -1.0f;
  436.         Out.Position.z = 0.0f;
  437.         Out.Position.w = 1.0f;
  438.  
  439.         //calculate world position
  440.         LocalPosition = vsGetLocalPosition(In);
  441.     }
  442.     else
  443.     {
  444.         LocalPosition = vsDoPositionTransform(In, Out.Position, true);
  445.     }
  446.     
  447.     Out.TexCoord.xy = In.TexCoord;
  448.     
  449.     //calculate the VertexToLight vector
  450.     Out.VertexToLight1.xyz = ModelSpaceLight1 - LocalPosition.xyz;
  451.     if(bTwoLights) 
  452.         Out.VertexToLight2.xyz = ModelSpaceLight2 - LocalPosition.xyz;
  453.     
  454.     float4 WorldPosition = mul(LocalPosition, World);
  455.     
  456.     Out.VertexToLight1.w = (length(Out.VertexToLight1.xyz) - InverseLightRange1.y) * InverseLightRange1.x;
  457.     Out.VertexToLight1.w += ShadowMapDepthBias;
  458.     Out.VertexToLight1.w = min(0.98f, Out.VertexToLight1.w);
  459.     if(bTwoLights)
  460.         Out.VertexToLight2.w = (length(Out.VertexToLight2.xyz) - InverseLightRange2.y) * InverseLightRange2.x;
  461.             
  462.     if(bShadowed)
  463.     {
  464.         //Calculate Shadow map Texture Coordinates
  465.         Out.ShadowMapTexCoord = mul(WorldPosition,ShadowMapTexProj1);
  466.         
  467.         if(bOutputTextureSpace && bTwoLights)
  468.         {
  469.             Out.LightMapTexCoord = mul(WorldPosition,ShadowMapTexProj2);
  470.         }
  471.     }
  472.     
  473.     if(!bOutputTextureSpace)
  474.     {
  475.         //calculate light map texture coordinates
  476.         Out.LightMapTexCoord = mul(WorldPosition,LightMapTexProj1);
  477.         if(bTwoLights)
  478.             Out.ShadowMapTexCoord = mul(WorldPosition,LightMapTexProj2);
  479.     }
  480.     
  481.     Out.VertexToCamera.xyz = normalize(ModelSpaceCamera - LocalPosition.xyz);
  482.     
  483.     if(bUseTangentSpace)
  484.         vsGetTangentSpace(In, Out.TangentToModelSpace);
  485.     else
  486.         Out.TangentToModelSpace[0] = vsGetNormal(In);
  487.     
  488.     Out.VertexToLight1.xyz = normalize(Out.VertexToLight1.xyz);
  489.     if(dot(LightDirection1, -Out.VertexToLight1.xyz) < 0.5f)
  490.         Out.VertexToLight1.w = 10.0f;
  491.         
  492.     if(bTwoLights)
  493.         Out.VertexToLight2.xyz = normalize(Out.VertexToLight2.xyz);
  494.     
  495.     Out.Halfway.xyz = normalize(Out.VertexToLight1.xyz + Out.VertexToCamera.xyz);    
  496.     
  497.     Out.Halfway.w = dot(Out.Halfway.xyz, Out.VertexToCamera.xyz);
  498.     
  499.     Out.Halfway += 1.0f;
  500.     Out.Halfway *= 0.5f;
  501.     
  502.     if(bTwoLights)
  503.     {
  504.         //packing hack
  505.         //Halfway2 is in VertexToCamera.w, LightMapTexCoord.z, ShadowMapTexCoord.z
  506.         float3 Halfway2 = normalize(Out.VertexToLight2.xyz + Out.VertexToCamera.xyz);    
  507.         Out.VertexToCamera.w = Halfway2.x;
  508.         Out.LightMapTexCoord.z = Halfway2.y;
  509.         Out.ShadowMapTexCoord.z = Halfway2.z;
  510.         
  511.         Out.VertexToLight2.xyz += 1.0f;
  512.         Out.VertexToLight2.xyz *= 0.5f;
  513.     }    
  514.     
  515. #if defined (VERTEX_COLORS)
  516.     Out.VertexToLight2 = In.Color0;
  517. #endif    
  518.     
  519.     return Out;
  520. }
  521.  
  522.  
  523. float4 ps_spotlight(VS_OUTPUT_LIGHT In, 
  524.                                     uniform bool bLight,
  525.                                     uniform bool bReadTextureSpaceLight,
  526.                                     uniform bool bShadow,
  527.                                     uniform bool bReadTextureSpaceShadow,
  528.                                     uniform bool bNormalMapped,
  529.                                     uniform bool bUseDiffuseColor,
  530.                                     uniform bool bSpecular,
  531.                                     uniform bool bKajiyaKay = false,
  532.                                     uniform bool bOutputTextureSpace = false,
  533.                                     uniform bool bTwoLights = false
  534.                                     ) : COLOR
  535. {
  536.     float4 Out = 1;
  537.     float4 Diffuse1 = 1.0f;
  538.     float4 Diffuse2 = 0.0f;
  539.     float4 Specular1 = 0.0f;
  540.     float4 Specular2 = 0.0f;
  541.     float3 Ambient = AmbientLight.xyz;
  542.     float3 VertexColor = PassMultiplier;
  543.  
  544.     if(bUseDiffuseColor)
  545.     {
  546.         #if defined(DIFFUSE_MAP)
  547.             Out = tex2D(DiffuseMap, In.TexCoord);
  548.         #else
  549.             Out = DiffuseMaterial;    
  550.         #endif
  551.         
  552.         Ambient *= Out.xyz;
  553.         
  554. #if defined (VERTEX_COLORS)
  555.         VertexColor *= Out.xyz * In.VertexToLight2.xyz;
  556. #endif    
  557.         
  558.     }
  559.  
  560.     float NdotL = 0;
  561.     float3 Normal = 0;
  562.     float3 Light1 = 1.0f;
  563.     float3 Light2 = 0.0f;
  564.     
  565.     if(bLight)
  566.     {
  567.         if(bTwoLights)
  568.         {
  569.             In.VertexToLight2.xyz *= 2.0f;
  570.             In.VertexToLight2.xyz -= 1.0f;
  571.         }
  572.         
  573.         #if defined (NORMAL_MAP)
  574.             if(bNormalMapped)
  575.             {
  576.                 Normal = 2.0f * tex2D(NormalMap, In.TexCoord) - 1.0f;
  577.                 Normal = mul(Normal, In.TangentToModelSpace);
  578.             }
  579.             else
  580.             {
  581.                 Normal = In.TangentToModelSpace[0];
  582.             }
  583.         #else
  584.             Normal = In.TangentToModelSpace[0];
  585.         #endif
  586.         
  587.         Normal = normalize(Normal);
  588.     
  589.         //Diffuse Lighting
  590.         if(bReadTextureSpaceLight)
  591.         {
  592.             float4 TextureSpace = tex2D(BlurMap, In.TexCoord.zw);
  593.             if(bTwoLights)
  594.             {
  595.                 Diffuse1.xyz = TextureSpace.y;
  596.                 Diffuse2.xyz = TextureSpace.z;
  597.             }
  598.             else
  599.             {
  600.                 Diffuse1.xyz = TextureSpace.x;
  601.             }
  602.         }
  603.         else
  604.         {
  605.             Diffuse1.xyz = saturate(dot(In.VertexToLight1.xyz, Normal));
  606.             
  607.             if(bTwoLights)
  608.             {
  609.                 Diffuse2.xyz = saturate(dot(In.VertexToLight2.xyz, Normal));
  610.             }
  611.         }
  612.     
  613.         if(!bOutputTextureSpace)
  614.         {
  615.             Light1 = tex2Dproj(LightMap, In.LightMapTexCoord);
  616.             Diffuse1.xyz *= Light1;
  617.             
  618.             if(bTwoLights)
  619.             {
  620.                 Light2 = tex2Dproj(LightMap, In.ShadowMapTexCoord);
  621.                 Diffuse2.xyz *= Light2;
  622.             }
  623.         }
  624.         
  625.         //Specular Ligthing
  626.         if(bSpecular)
  627.         {
  628.             float SpecularMultiplier = SpecularScale;
  629.             #if defined(NORMAL_MAP)
  630.                 SpecularMultiplier *= tex2D(NormalMap, In.TexCoord).a;
  631.             #else
  632.                 SpecularMultiplier *= SpecularMaterial;
  633.             #endif
  634.             
  635.             if(bTwoLights)//only happens with the blurred ones
  636.             {
  637.                 float2 NdotLs = saturate(dot(Normal, In.VertexToLight1.xyz));
  638.                 NdotLs.y = dot(Normal, In.VertexToLight2.xyz);
  639.                 float3 ReflectedLight = 2 * NdotLs.x * Normal - In.VertexToLight1.xyz;
  640.                 float2 RdotVs = saturate(dot(ReflectedLight,In.VertexToCamera.xyz));
  641.                 ReflectedLight = 2 * NdotLs.y * Normal - In.VertexToLight2.xyz;
  642.                 RdotVs.y = saturate(dot(ReflectedLight,In.VertexToCamera.xyz));
  643.                 RdotVs = SpecularMultiplier* saturate(pow(RdotVs, SpecularPower));
  644.                 Specular1 = RdotVs.x;
  645.                 Specular2 = RdotVs.y;
  646.  
  647.             }        
  648.             else if(bKajiyaKay)
  649.             {
  650.                 float3 Halfway = In.Halfway * 2.0f - 1.0f;
  651.                 //In.Halfway -= 1.0f;
  652.                 float3 Binormal1 = In.TangentToModelSpace[1]/BumpFactor + Normal * KajiyaKayOffsets.x*tex2D(NormalMap, In.TexCoord).a;
  653.                 float3 Binormal2 = In.TangentToModelSpace[1]/BumpFactor + Normal * KajiyaKayOffsets.y*tex2D(NormalMap, In.TexCoord).a;
  654.                 float2 BdotHs = float2(dot(Binormal1, Halfway),dot(Binormal2, Halfway));
  655.                 float2 sinBHs = sqrt(1.0f - BdotHs*BdotHs);
  656.                 float2 dirAttens = smoothstep(-1.0f, 0.0f, BdotHs);
  657.                 float2 Speculars = dirAttens * pow(sinBHs, KajiyaKayPowers);
  658.                 Specular1 = Speculars.x * KajiyaKayColor1;
  659.                 Specular1 += Speculars.y * KajiyaKayColor2;
  660.                 Specular1 *= Diffuse1;
  661.             }
  662.             else
  663.             {
  664.                 float3 ReflectedLight = 2 * dot(Normal, In.VertexToLight1.xyz) * Normal - In.VertexToLight1.xyz;
  665.                 float RdotV = saturate(dot(ReflectedLight,In.VertexToCamera.xyz));
  666.                 
  667.                 RdotV = SpecularMultiplier* saturate(pow(RdotV, SpecularPower));
  668.                 Specular1 = RdotV;
  669.             }
  670.         
  671.             if(bReadTextureSpaceLight)
  672.             {
  673.                 Specular1.xyz *= Diffuse1.xyz;
  674.                 if(bTwoLights)
  675.                     Specular2.xyz *= Diffuse2.xyz;
  676.             }
  677.             else if(!bKajiyaKay)
  678.             {
  679.                 Specular1.xyz *= Light1;
  680.                 if(bTwoLights)
  681.                     Specular2.xyz *= Light2;
  682.             }
  683.         }
  684.     }
  685.  
  686.     if(bShadow)
  687.     {
  688.         if(bReadTextureSpaceShadow)//shadow is blurred
  689.         {
  690.             float4 Shadow = tex2D(BlurMap, In.TexCoord.zw);
  691.             
  692.             if(bTwoLights)
  693.             {
  694.                 Diffuse1.xyz *= Shadow.y;
  695.                 Specular1.xyz *= Shadow.y;
  696.                 Diffuse2.xyz *= Shadow.z;
  697.                 Specular2.xyz *= Shadow.z;
  698.             }
  699.             else
  700.             {
  701.                 Diffuse1.xyz *= Shadow.x;
  702.                 Specular1.xyz *= Shadow.x;
  703.             }
  704.         }
  705.         else
  706.         {
  707.             float2 texmapscale = (1.0f/g_fShadowMapResolution, 1.0f/g_fShadowMapResolution);    
  708.         
  709.             if(bOutputTextureSpace)
  710.             {
  711.             #if defined(BLUR_DIFFUSE)
  712.                 Out.xyz = 0.0f;
  713.                 if(bTwoLights)
  714.                 {
  715.                     Out.y = Diffuse1.x * psGetShadowCoefficient(ShadowMap, In.VertexToLight1.w, In.ShadowMapTexCoord, texmapscale, bSpecular);
  716.                     Out.z = Diffuse2.x * psGetShadowCoefficient(ShadowMap2, In.VertexToLight2.w, In.LightMapTexCoord, texmapscale, bSpecular);
  717.                 }
  718.                 else
  719.                 {
  720.                     Out.x = Diffuse1.x * psGetShadowCoefficient(ShadowMap, In.VertexToLight1.w, In.ShadowMapTexCoord, texmapscale, bSpecular);                
  721.                 }
  722.                 Out.a = 1.0f;
  723.             #elif defined(BLUR_SHADOWS)
  724.                 Out.xyz = 0.0f;
  725.                 if(bTwoLights)
  726.                 {
  727.                     Out.y = psGetShadowCoefficient(ShadowMap, In.VertexToLight1.w, In.ShadowMapTexCoord, texmapscale, bSpecular);
  728.                     Out.z = psGetShadowCoefficient(ShadowMap2, In.VertexToLight2.w, In.LightMapTexCoord, texmapscale, bSpecular);
  729.                 }
  730.                 else
  731.                 {
  732.                     Out.x = psGetShadowCoefficient(ShadowMap, In.VertexToLight1.w, In.ShadowMapTexCoord, texmapscale, bSpecular);
  733.                 }
  734.                 Out.a = 1.0f;
  735.             #endif
  736.             }
  737.             else
  738.             {
  739.                 float Shadow = psGetShadowCoefficient(ShadowMap, In.VertexToLight1.w, In.ShadowMapTexCoord, texmapscale, bSpecular);
  740.                     
  741.                 Diffuse1.xyz *= Shadow;
  742.                 Specular1 *= Shadow;
  743.             }
  744.         }
  745.     }
  746.     else
  747.     {
  748.         if(bOutputTextureSpace)
  749.         {
  750.         #if defined(BLUR_DIFFUSE)
  751.             Out.xyz = 0.0f;
  752.             if(bTwoLights)
  753.             {
  754.                 Out.y = Diffuse1.x;
  755.                 Out.z = Diffuse2.x;
  756.             }
  757.             else
  758.             {
  759.                 Out.x = Diffuse1.x;                
  760.             }
  761.             Out.a = 1.0f;
  762.         #endif
  763.         }
  764.         Out.a += 0.00001f;
  765.     }
  766.     
  767.     if(bLight && bOutputTextureSpace == false)
  768.     {
  769.         if(bTwoLights)
  770.         {
  771.             Out *= Diffuse1 * LightColor1 + Diffuse2 * LightColor2;
  772.             if(bSpecular)
  773.             {
  774.                 Out += Specular1 + Specular2;
  775.             }
  776.         }
  777.         else
  778.         {
  779.             Out *= Diffuse1 * LightColor1;
  780.             if(bSpecular)
  781.             {
  782.                 Out += Specular1;
  783.             }
  784.         }
  785.         
  786.         Out.xyz += Ambient;
  787. #if defined (VERTEX_COLORS)
  788.         Out.xyz += VertexColor;
  789. #endif    
  790.         if(!bKajiyaKay)
  791.         {
  792.             Out.a *= Fade;
  793.         }
  794.     }
  795.     
  796.     
  797.     return Out;
  798. }
  799.  
  800. ///////////////////////////////////////////////////////////////////
  801. //Techniques
  802. ///////////////////////////////////////////////////////////////////
  803. technique Normal
  804. {
  805.     pass Ambient
  806.     {
  807.         VertexShader = compile vs_1_1 VS();
  808.         PixelShader = compile ps_2_0 PS();
  809.         
  810.         MagFilter[0] = Linear;
  811.         MinFilter[0] = Linear;
  812.         MipFilter[0] = Linear;
  813.         
  814.         FillMode = Solid;
  815.     }    
  816.     pass DepthEncode
  817.     {
  818.         VertexShader = compile vs_1_1 vs_shadowmap();
  819.         PixelShader = compile ps_2_0 ps_shadowmap();
  820.         
  821.         AddressU[2] = clamp;//shadowlookup
  822.         AddressV[2] = clamp;
  823.         MagFilter[2] = Linear;
  824.         MinFilter[2] = Linear;
  825.         MipFilter[2] = Linear;
  826.  
  827.         FillMode = Solid;
  828.         AlphaTestEnable = false;
  829.         AlphaFunc = Always;
  830.         AlphaBlendEnable = false;
  831.         SrcBlend = One;
  832.         DestBlend = Zero;
  833.         BlendOp = Add;
  834.     }
  835.     pass SpotLight
  836.     {
  837.         VertexShader = compile vs_2_0 vs_spotlight(false, g_bUseNormalMap, true);
  838.         PixelShader = compile ps_2_0 ps_spotlight(true, false,  g_bShadowed, false, g_bUseNormalMap, true, g_bSpecular, g_bKajiyaKay);
  839.         
  840.         MagFilter[0] = Linear;//diffuse map
  841.         MinFilter[0] = Linear;
  842.         MipFilter[0] = Linear;
  843.         MagFilter[1] = Linear;//light map
  844.         MinFilter[1] = Linear;
  845.         MipFilter[1] = Linear;
  846.         MagFilter[2] = Linear;//normal map
  847.         MinFilter[2] = Linear;
  848.         MipFilter[2] = Linear;
  849.         MagFilter[3] = Linear;//shadow map
  850.         MinFilter[3] = Linear;
  851.         MipFilter[3] = Linear;
  852.         MagFilter[4] = Linear;//shadow map
  853.         MinFilter[4] = Linear;
  854.         MipFilter[4] = Linear;
  855.         
  856.         AddressU[1] = clamp;
  857.         AddressV[1] = clamp;
  858.         AddressU[3] = clamp;
  859.         AddressV[3] = clamp;
  860.         AddressU[4] = clamp;
  861.         AddressV[4] = clamp;
  862.  
  863.         FillMode = solid;
  864.     }            
  865.     #if defined (BLUR_DIFFUSE) || defined (BLUR_SHADOWS)
  866.     pass SpotLightBlur
  867.     {
  868.         #if defined (BLUR_DIFFUSE)
  869.             //already have texture space map
  870.             //only need the texture space if doing specular
  871.             VertexShader = compile vs_2_0 vs_spotlight(false, g_bSpecular, true);
  872.         #else
  873.             VertexShader = compile vs_2_0 vs_spotlight(false, g_bUseNormalMap, true);
  874.         #endif
  875.         
  876.         #if defined (BLUR_DIFFUSE)
  877.             PixelShader = compile ps_2_0 ps_spotlight(true, true,  false, false, g_bUseNormalMap, true, g_bSpecular, false);
  878.         #elif defined (BLUR_SHADOWS)
  879.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  true, true, g_bUseNormalMap, true, g_bSpecular, false);
  880.         #else
  881.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  g_bShadowed, false, g_bUseNormalMap, true, g_bSpecular, g_bKajiyaKay);
  882.         #endif        
  883.         
  884.         MagFilter[0] = Linear;//diffuse map
  885.         MinFilter[0] = Linear;
  886.         MipFilter[0] = Linear;
  887.         MagFilter[1] = Linear;//light map
  888.         MinFilter[1] = Linear;
  889.         MipFilter[1] = Linear;
  890.         MagFilter[2] = Linear;//normal map
  891.         MinFilter[2] = Linear;
  892.         MipFilter[2] = Linear;
  893.         MagFilter[3] = Linear;//shadow map
  894.         MinFilter[3] = Linear;
  895.         MipFilter[3] = Linear;
  896.         MagFilter[4] = Linear;//shadow map
  897.         MinFilter[4] = Linear;
  898.         MipFilter[4] = Linear;
  899.         
  900.         AddressU[1] = clamp;
  901.         AddressV[1] = clamp;
  902.         AddressU[3] = clamp;
  903.         AddressV[3] = clamp;
  904.         AddressU[4] = clamp;
  905.         AddressV[4] = clamp;
  906.  
  907.         FillMode = solid;
  908.     }    
  909.     pass SpotLightBlur2
  910.     {
  911.         #if defined (BLUR_DIFFUSE)
  912.             //already have texture space map
  913.             //only need the texture space if doing specular
  914.             VertexShader = compile vs_2_0 vs_spotlight(false, g_bSpecular, true, true);
  915.         #else
  916.             VertexShader = compile vs_2_0 vs_spotlight(false, g_bUseNormalMap, true, true);
  917.         #endif
  918.         
  919.         #if defined (BLUR_DIFFUSE)
  920.             PixelShader = compile ps_2_0 ps_spotlight(true, true,  false, false, g_bUseNormalMap, true, g_bSpecular, g_bKajiyaKay, false, true);
  921.         #elif defined (BLUR_SHADOWS)
  922.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  true, true, g_bUseNormalMap, true, g_bSpecular, g_bKajiyaKay, false, true);
  923.         #else
  924.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  g_bShadowed, false, g_bUseNormalMap, true, g_bSpecular, g_bKajiyaKay);
  925.         #endif        
  926.         
  927.         MagFilter[0] = Linear;//diffuse map
  928.         MinFilter[0] = Linear;
  929.         MipFilter[0] = Linear;
  930.         MagFilter[1] = Linear;//light map
  931.         MinFilter[1] = Linear;
  932.         MipFilter[1] = Linear;
  933.         MagFilter[2] = Linear;//normal map
  934.         MinFilter[2] = Linear;
  935.         MipFilter[2] = Linear;
  936.         MagFilter[3] = Linear;//shadow map
  937.         MinFilter[3] = Linear;
  938.         MipFilter[3] = Linear;
  939.         MagFilter[4] = Linear;//shadow map
  940.         MinFilter[4] = Linear;
  941.         MipFilter[4] = Linear;
  942.         
  943.         AddressU[1] = clamp;
  944.         AddressV[1] = clamp;
  945.         AddressU[3] = clamp;
  946.         AddressV[3] = clamp;
  947.         AddressU[4] = clamp;
  948.         AddressV[4] = clamp;
  949.         
  950.  
  951.         FillMode = Solid;
  952.     }            
  953.     pass TextureSpaceLighting
  954.     {
  955.         VertexShader = compile vs_2_0 vs_spotlight(true, false, true);
  956.         
  957.         #if defined (BLUR_DIFFUSE)
  958.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  g_bShadowed, false,   false, false, false, false, true);
  959.         #else
  960.             PixelShader = compile ps_2_0 ps_spotlight(false, false,  true, false, false, false, false,false, true);
  961.         #endif
  962.         
  963.         MagFilter[0] = Linear;//diffuse map
  964.         MinFilter[0] = Linear;
  965.         MipFilter[0] = Linear;
  966.         
  967.         MagFilter[1] = Linear;//light map
  968.         MinFilter[1] = Linear;
  969.         MipFilter[1] = Linear;
  970.  
  971.         AddressU[1] = Clamp;
  972.         AddressV[1] = Clamp;
  973.         
  974.         MagFilter[2] = Linear;//normal map
  975.         MinFilter[2] = Linear;
  976.         MipFilter[2] = Linear;
  977.         
  978.         MagFilter[3] = Linear;//shadow map
  979.         MinFilter[3] = Linear;
  980.         MipFilter[3] = Linear;
  981.  
  982.         AddressU[3] = Clamp;
  983.         AddressV[3] = Clamp;
  984.         
  985.         MagFilter[4] = Linear;//blurred map
  986.         MinFilter[4] = Linear;
  987.         MipFilter[4] = Linear;
  988.         
  989.         AddressU[4] = Clamp;
  990.         AddressV[4] = Clamp;
  991.         
  992.         ZEnable = false;
  993.         ZFunc = Always;
  994.         FillMode = Solid;
  995.         ZWriteEnable = false;
  996.         AlphaTestEnable = false;
  997.         AlphaFunc = Always;
  998.         AlphaBlendEnable = true;
  999.         SrcBlend = One;
  1000.         DestBlend = One;
  1001.         BlendOp = Add;
  1002.     }
  1003.     pass TextureSpaceLighting2
  1004.     {
  1005.         VertexShader = compile vs_2_0 vs_spotlight(true, false, true, true);
  1006.         
  1007.         #if defined (BLUR_DIFFUSE)
  1008.             PixelShader = compile ps_2_0 ps_spotlight(true, false,  g_bShadowed, false, false, false, false,false, true, true);
  1009.         #else
  1010.             PixelShader = compile ps_2_0 ps_spotlight(false, false,  true, false, false, false, false,false, true, true);
  1011.         #endif
  1012.         
  1013.         MagFilter[0] = Linear;//diffuse map
  1014.         MinFilter[0] = Linear;
  1015.         MipFilter[0] = Linear;
  1016.         
  1017.         MagFilter[1] = Linear;//light map
  1018.         MinFilter[1] = Linear;
  1019.         MipFilter[1] = Linear;
  1020.  
  1021.         AddressU[1] = Clamp;
  1022.         AddressV[1] = Clamp;
  1023.         
  1024.         MagFilter[2] = Linear;//normal map
  1025.         MinFilter[2] = Linear;
  1026.         MipFilter[2] = Linear;
  1027.  
  1028.         MagFilter[3] = Linear;//shadow map
  1029.         MinFilter[3] = Linear;
  1030.         MipFilter[3] = Linear;
  1031.  
  1032.  
  1033.         AddressU[3] = Clamp;
  1034.         AddressV[3] = Clamp;
  1035.  
  1036.         MagFilter[4] = Linear;//shadow map
  1037.         MinFilter[4] = Linear;
  1038.         MipFilter[4] = Linear;
  1039.         
  1040.         AddressU[4] = Clamp;
  1041.         AddressV[4] = Clamp;
  1042.         
  1043.         ZEnable = false;
  1044.         ZFunc = Always;
  1045.         FillMode = Solid;
  1046.         ZWriteEnable = false;
  1047.         AlphaTestEnable = false;
  1048.         AlphaFunc = Always;
  1049.         AlphaBlendEnable = true;
  1050.         SrcBlend = One;
  1051.         DestBlend = One;
  1052.         BlendOp = Add;
  1053.     }    
  1054.     #endif
  1055. }