home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 February / Gamestar_81_2006-02_dvd.iso / Dema / roboblitz_demo.msi / disk1.cab / UserShaderTemplate.hlsl < prev    next >
Text File  |  2005-03-31  |  8KB  |  252 lines

  1. /*
  2.     Defined by the C++ code:
  3.         SHADERBLENDING_SOLID
  4.         SHADERBLENDING_MASKED
  5.         SHADERBLENDING_TRANSLUCENT
  6.         SHADERBLENDING_ADDITIVE
  7.  
  8.         SHADER_TWOSIDED
  9.         SHADER_LIGHTINGMODEL_PHONG
  10.         SHADER_LIGHTINGMODEL_NONDIRECTIONAL
  11.         SHADER_LIGHTINGMODEL_UNLIT
  12.         SHADER_LIGHTINGMODEL_SHPRT
  13.         SHADER_LIGHTINGMODEL_CUSTOM
  14. */
  15.  
  16. #define NUM_USER_TEXTURES %u
  17. #define NUM_USER_TEXCOORDS %u
  18. #define NUM_USER_VECTOR_INPUTS %u
  19. #define NUM_USER_SCALAR_INPUTS %u
  20.  
  21. #if NUM_USER_VECTOR_INPUTS
  22. float4    UserVectorInputs[NUM_USER_VECTOR_INPUTS];
  23. #endif
  24.  
  25. #if NUM_USER_SCALAR_INPUTS
  26. float4    UserScalarInputs[(NUM_USER_SCALAR_INPUTS + 3) / 4];
  27. #endif
  28.  
  29. #if NUM_USER_TEXTURES
  30. sampler UserTextures[NUM_USER_TEXTURES];
  31. float4    UserTexturesMins[NUM_USER_TEXTURES];
  32. float4    UserTexturesMaxMinusMins[NUM_USER_TEXTURES];
  33. float4    UserTextureOffset;
  34. float4    UserTextureScale;
  35. #endif
  36.  
  37. float    SceneTime;
  38.  
  39. float4    UserMeshEmitterVertexColor;
  40.  
  41. struct userShaderInput
  42. {
  43. #if NUM_USER_TEXCOORDS
  44.     float2 UserTexCoords[NUM_USER_TEXCOORDS];
  45. #endif
  46.     float4    UserVertexColor;
  47.     float3    TangentNormal,
  48.             TangentReflectionVector,
  49.             TangentCameraVector;
  50.     half3    TangentLightVector;
  51.     float4    ScreenPosition;
  52. };
  53.  
  54. half3 userNormal(userShaderInput Input)
  55. {
  56.     return %s;
  57. }
  58.  
  59. half3 userEmissive(userShaderInput Input)
  60. {
  61.     return %s;
  62. }
  63.  
  64. half3 userDiffuseColor(userShaderInput Input)
  65. {
  66.     return %s;
  67. }
  68.  
  69. half3 userSpecularColor(userShaderInput Input)
  70. {
  71.     return %s;
  72. }
  73.  
  74. half userSpecularPower(userShaderInput Input)
  75. {
  76.     return %s;
  77. }
  78.  
  79. half userOpacity(userShaderInput Input)
  80. {
  81.     return %s;
  82. }
  83.  
  84. #if SHADERBLENDING_MASKED
  85. float userMask(userShaderInput Input)
  86. {
  87.     return %s - %s;
  88. }
  89. #endif
  90.  
  91. float2 userDistortion(userShaderInput Input)
  92. {
  93.     return %s;
  94. }
  95.  
  96. float3 userTwoSidedLightingMask(userShaderInput Input)
  97. {
  98.     return %s;
  99. }
  100.  
  101. #if SHADER_LIGHTINGMODEL_SHPRT
  102. float4 userSHM(userShaderInput Input)
  103. {
  104.     return %s;
  105. }
  106. #endif
  107.  
  108. #if SHADER_LIGHTINGMODEL_CUSTOM
  109. float3 userCustomLighting(userShaderInput Input)
  110. {
  111.     return %s;
  112. }
  113. #endif
  114.  
  115. float3 userPointLightTransfer(userShaderInput Input,float3 WorldLightVector)
  116. {
  117.     float3    TwoSidedLighting = 0;
  118.     float3    TwoSidedLightingMask = 0;
  119.     TwoSidedLightingMask = userTwoSidedLightingMask(Input);
  120.     TwoSidedLighting = TwoSidedLightingMask * userDiffuseColor(Input) * radialAttenuation(WorldLightVector);
  121.  
  122.     float3    Lighting = 0;
  123. #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL
  124.     Lighting = userDiffuseColor(Input) * radialAttenuation(WorldLightVector);;
  125. #elif SHADER_LIGHTINGMODEL_SHPRT
  126.     Lighting = pointLightSHM(
  127.             userDiffuseColor(Input),
  128.             userSpecularColor(Input),
  129.             userSpecularPower(Input),
  130.             userSHM(Input),
  131.             Input.TangentLightVector,
  132.             Input.TangentCameraVector,
  133.             Input.TangentNormal,
  134.             Input.TangentReflectionVector
  135.             ) *
  136.         radialAttenuation(WorldLightVector);
  137. #elif SHADER_LIGHTINGMODEL_PHONG
  138.     Lighting = pointLightPhong(
  139.         userDiffuseColor(Input),
  140.         userSpecularColor(Input),
  141.         userSpecularPower(Input),
  142.         Input.TangentLightVector,
  143.         Input.TangentCameraVector,
  144.         Input.TangentNormal,
  145.         Input.TangentReflectionVector
  146.         ) *
  147.         radialAttenuation(WorldLightVector);
  148. #elif SHADER_LIGHTINGMODEL_CUSTOM
  149.     Lighting = userCustomLighting(Input);
  150. #endif
  151.  
  152.     return lerp(Lighting,TwoSidedLighting,TwoSidedLightingMask);
  153. }
  154.  
  155. float3 userDirectionalLightTransfer(userShaderInput Input)
  156. {
  157.     float3    TwoSidedLighting = 0;
  158.     float3    TwoSidedLightingMask = 0;
  159.     TwoSidedLightingMask = userTwoSidedLightingMask(Input);
  160.     TwoSidedLighting = TwoSidedLightingMask * userDiffuseColor(Input);
  161.  
  162.     float3    Lighting = 0;
  163. #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL
  164.     Lighting = userDiffuseColor(Input);
  165. #elif SHADER_LIGHTINGMODEL_SHPRT
  166.     Lighting = pointLightSHM(
  167.             userDiffuseColor(Input),
  168.             userSpecularColor(Input),
  169.             userSpecularPower(Input),
  170.             userSHM(Input),
  171.             Input.TangentLightVector,
  172.             Input.TangentCameraVector,
  173.             Input.TangentNormal,
  174.             Input.TangentReflectionVector
  175.             );
  176. #elif SHADER_LIGHTINGMODEL_PHONG
  177.     Lighting = pointLightPhong(
  178.         userDiffuseColor(Input),
  179.         userSpecularColor(Input),
  180.         userSpecularPower(Input),
  181.         Input.TangentLightVector,
  182.         Input.TangentCameraVector,
  183.         Input.TangentNormal,
  184.         Input.TangentReflectionVector
  185.         );
  186. #elif SHADER_LIGHTINGMODEL_CUSTOM
  187.     Lighting = userCustomLighting(Input);
  188. #endif
  189.  
  190.     return lerp(Lighting,TwoSidedLighting,TwoSidedLightingMask);
  191. }
  192.  
  193. float3 userHemisphereLightTransfer(userShaderInput Input,float3 SkyVector)
  194. {
  195.     float3    TwoSidedLighting = 0;
  196.     float3    TwoSidedLightingMask = 0;
  197.     TwoSidedLightingMask = userTwoSidedLightingMask(Input);
  198.     TwoSidedLighting = TwoSidedLightingMask * userDiffuseColor(Input);
  199.  
  200.     float3    Lighting = 0;
  201. #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL
  202.     Lighting = userDiffuseColor(Input);
  203. #elif SHADER_LIGHTINGMODEL_SHPRT
  204.     Lighting = hemisphereLightSHM(userDiffuseColor(Input),userSHM(Input),SkyVector);
  205. #elif SHADER_LIGHTINGMODEL_PHONG
  206.     Lighting = hemisphereLightPhong(userDiffuseColor(Input),SkyVector,Input.TangentNormal);
  207. #elif SHADER_LIGHTINGMODEL_CUSTOM
  208.     Lighting = 0;//userCustomLighting(Input);
  209. #endif
  210.  
  211.     return lerp(Lighting,TwoSidedLighting,TwoSidedLightingMask);
  212. }
  213.  
  214. #if SHADERBLENDING_TRANSLUCENT
  215.     void userClipping(userShaderInput Input) { clip(userOpacity(Input) - 1.0 / 255.0); }
  216.     #if OPAQUELAYER
  217.         float4 userBlendBase(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendBase(ScreenPosition,Color); }
  218.         float4 userBlendAdd(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendAdd(ScreenPosition,Color); }
  219.     #else
  220.         float4 userBlendBase(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return translucentBlendBase(ScreenPosition,Color,userDistortion(Input),userOpacity(Input)); }
  221.         float4 userBlendAdd(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return translucentBlendAdd(ScreenPosition,Color,userOpacity(Input)); }
  222.     #endif
  223. #elif SHADERBLENDING_ADDITIVE
  224.     void userClipping(userShaderInput Input) { clip(userOpacity(Input) - 1.0 / 255.0); }
  225.     #if OPAQUELAYER
  226.         float4 userBlendBase(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendBase(ScreenPosition,Color); }
  227.         float4 userBlendAdd(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendAdd(ScreenPosition,Color); }
  228.     #else
  229.         float4 userBlendBase(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return additiveBlendBase(ScreenPosition,Color,userOpacity(Input)); }
  230.         float4 userBlendAdd(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return additiveBlendAdd(ScreenPosition,Color,userOpacity(Input)); }
  231.     #endif
  232. #else
  233.     #if SHADERBLENDING_MASKED
  234.         void userClipping(userShaderInput Input) { clip(userMask(Input)); }
  235.     #else
  236.         void userClipping(userShaderInput Input) { return; }
  237.     #endif
  238.     float4 userBlendBase(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendBase(ScreenPosition,Color); }
  239.     float4 userBlendAdd(userShaderInput Input,float4 ScreenPosition,float3 Color) { userClipping(Input); return opaqueBlendAdd(ScreenPosition,Color); }
  240. #endif
  241.  
  242. void calcUserVectors(in out userShaderInput UserShaderInput,float3 CameraVector,float4 ScreenPosition,half3 LightVector = 0)
  243. {
  244.     UserShaderInput.ScreenPosition = ScreenPosition;
  245.     UserShaderInput.TangentCameraVector = normalize(CameraVector);
  246.     UserShaderInput.TangentLightVector = normalize((half3)LightVector);
  247.     UserShaderInput.TangentNormal = normalize(userNormal(UserShaderInput));
  248. #if SHADER_TWOSIDED
  249.     UserShaderInput.TangentNormal *= TwoSidedSign;
  250. #endif
  251.     UserShaderInput.TangentReflectionVector = -UserShaderInput.TangentCameraVector + UserShaderInput.TangentNormal * dot(UserShaderInput.TangentNormal,UserShaderInput.TangentCameraVector) * 2.0;
  252. }