Advertisement
Guest User

Untitled

a guest
Oct 12th, 2010
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////////////////////
  2. // Includes
  3. /////////////////////////////////////////////////////////////////////
  4.  
  5. #include "Common.cfi" // Common contains general codes and information.
  6.  
  7. // Shader global descriptions
  8. float Script : STANDARDSGLOBAL
  9. <
  10.   string Script =          
  11.            "Public;"
  12.            "SupportsAttrInstancing;"
  13.            "ShaderDrawType = Light;"
  14.            "ShaderType = General;"
  15. >;
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////// Custom shading pass structure /////////////////////////////////////
  19.  
  20. /////////////////////////////////////////////////////////////////////
  21. // Custom definitions.
  22. // Custom definitions are used to pass data that we want to use in more than a single portion of the code.
  23. /////////////////////////////////////////////////////////////////////
  24.  
  25. struct fragPassCustom
  26. {            
  27.     //Example:
  28.     half3 cExample;
  29.  
  30. };
  31. /////////////////////////////////////////////////////////////////////
  32. // Custom light definitions. Same as above but for light pass (not usually used)
  33. /////////////////////////////////////////////////////////////////////
  34.  
  35. struct fragLightPassCustom
  36. {
  37. };
  38.  
  39. /////////////////////////////////////////////////////////////////////
  40. // Include shadeLib, which contains general shading functions, like specular models and color effects, amongst others.
  41. /////////////////////////////////////////////////////////////////////
  42.  
  43. #include "ShadeLib.cfi"
  44.  
  45. /////////////////////////////////////////////////////////////////////
  46. // Some constants for use in the shader. Some functions can be found in FXConstantDefs.cfi for an idea of what you can use.
  47. /////////////////////////////////////////////////////////////////////
  48.  
  49. // Un-Tweakables /////////////////
  50. float4x4 mCamera      : PB_CameraMatrix;
  51. float4 AmbientObjectCol : PI_ObjectAmbColComp;//x=Ambient.w, y=ObjColor.w, w = Obj Rend quality
  52.  
  53. /////////////////////////////////////////////////////////////////////
  54. // These samples are defined in FXSamplerDefs.cfi
  55. // We simply place the defined name (#SUBSURFACEMAP as example) and you get access to the sampler.
  56. /////////////////////////////////////////////////////////////////////
  57.  
  58. //////////////////////////////// Samplers ////////////////
  59. SUBSURFACEMAP
  60. //DECALMAP
  61. OPACITYMAP
  62. ENVIRONMENTMAP
  63. ENVIRONMENTCUBEMAP
  64.  
  65. //////////////////////////////// Common vertex shader ////////////////
  66.  
  67. /////////////////////////////////////////////////////////////////////
  68. // Include vertexLib, which contains the global/generic vertex shader functions.
  69. /////////////////////////////////////////////////////////////////////
  70.  
  71. #include "VertexLib.cfi"
  72.  
  73. /////////////////////////////////////////////////////////////////////
  74. // Here's where we define material properties.
  75. // The material editor reads these and uses them as tweakable values.
  76. /////////////////////////////////////////////////////////////////////
  77.  
  78. // Tweakables /////////////////
  79.  
  80. float AmbientMultiplier // Name of the variable to be used by the shader.
  81. <
  82.   /////////////////////////////////////////////////////////////////////
  83.   // The registry of the slider. Make sure these don't overlap or the shader won't work.
  84.   // You generally start with 3.x
  85.   // The name tells you where you can use the variable (because it needs to be converted)
  86.   // vsregister is for vertex shader, psregister is for pixel shader. You can use both, but need at least one.
  87.   /////////////////////////////////////////////////////////////////////
  88.  
  89.   psregister = PS_REG_PM_3.x; // Example of change: PS_REG_PM_5.y (x,y,z,w, like everything else)
  90.    
  91.   string UIHelp = "Set alpha glow multiplier";  // Text/description to be displayed at the bottom of the material editor.                  
  92.   string UIName = "Alpha glow multiplier";   // Name of the material property.
  93.  
  94.   string UIWidget = "slider"; // Type. This one's a slider.
  95.   float UIMin = 0.0; // Minimum slider value.
  96.   float UIMax = 32.0; // Maximum slider value.
  97.   float UIStep = 0.1; // Slider step.
  98.  
  99. > = 1.0; // Initial value for unedited materials (when you assign the new material shader).
  100.  
  101. /////////////////////////////////////////////////////////////////////
  102. // Here's another, but it's a 4-value float instead of a single float.
  103. // Note that there's no .x(yzw) at the end of the register because it's using the entire register (all 4 values)
  104. // Instead of a slider, it's a color selection.
  105. /////////////////////////////////////////////////////////////////////
  106. float4 ColorExample
  107. <  
  108.   vsregister = VS_REG_PM_4;
  109.   psregister = PS_REG_PM_3.y;
  110.   string UIName = "Vertex color modifier";
  111.   string UIWidget = "color";  
  112. > = {0.45, 0.62, 0.54, 1.0}; //4 values instead of 1.
  113.  
  114. ///////////////// Vertex shader //////////////////
  115.  
  116. /////////////////////////////////////////////////////////////////////
  117. // Here's the structure for the vertex shader. You can define custom outputs here.
  118. // These are just outputs. Inputs are in vertexlib.cfi - for custom inputs, make a new input structure and use that instead of the usual app2VertGeneral.
  119. /////////////////////////////////////////////////////////////////////
  120.  
  121. struct vert2FragBase
  122. {  
  123.   // Position. Can also be "float4 Position : POSITION", but we use OUT_P to get the general info from another shader (ModificatorVT.cfi I think).
  124.   OUT_P
  125.  
  126.   // Texture coordinates.
  127.   float4 baseTC     : TEXCOORDN;
  128.  
  129.   // tangent
  130.   float4 vTangent  : TEXCOORDN;
  131.  
  132.   // Binormal
  133.   float4 vBinormal : TEXCOORDN;
  134.  
  135.   // Camera vector. Used for specularity and view-dependent effects (fresnel, rim lighting)
  136.   float4 vView     : TEXCOORDN;
  137.  
  138.   // Screen-space coordinates
  139.   float4 screenProj : TEXCOORDN_centroid;   //z used for instanced alpha test value
  140.  
  141.   // If the model is being lit by a projector light, we pass through the projections coordinates.
  142. #if %_LT_LIGHTS && %_LT_HASPROJ  
  143.   float4 projTC     : TEXCOORDN;
  144. #endif    
  145.  
  146.     // Color for ambient lighting.
  147.     float4 Ambient    : TEXCOORDN;
  148.  
  149.     // If there's fog, we pass through the fog information.
  150. #if %_RT_FOG
  151.   float4 AvgFogVolumeContrib : TEXCOORDN;
  152. #endif
  153.  
  154.     //D3D10's alphatesting works different, so we need to pass some info through.
  155. #if D3D10
  156.  #if %_RT_NOZPASS && %_RT_ALPHATEST
  157.   float4 AlphaTest : TEXCOORDN;
  158.  #endif
  159. #endif
  160.  
  161.   // Pass the vertex color data.
  162.   float4 Color      : COLOR0;
  163.  
  164. };
  165.  
  166. /////////////////////////////////////////////////////////////////////
  167. // Actual vertex shader.
  168. // This is where you add all vertex-processing code, like per-vertex lighting, etc.
  169. // It's much cheaper than the pixel shader as this is only run for each vertex on the model, and not each pixel.
  170. // Anything that doesn't need textures and can be done in the vertex shader, should be.
  171. /////////////////////////////////////////////////////////////////////
  172.  
  173. vert2FragBase BaseVS(app2vertGeneral IN)
  174. {
  175.   vert2FragBase OUT;
  176.  
  177.   //  This is just here for documentation. It doesn't really work or anything heh.
  178. #ifndef OPENGL
  179.   OUT = (vert2FragBase) 0;
  180. #endif
  181.  
  182.   // Calls the generic vertex shader from vertexLib.cfi.
  183.   vs_shared_output( IN, OUT, false );
  184.  
  185.   //Place custom codes after it, or replace it entirely. You wouldn't usually replace it unless making a completely new shader that requires special tweaks (vegetation)
  186.   // Example: Multiply the vertex colors by the ColorExample values chosen in the material editor.
  187.   OUT.Color *= ColorExample.
  188.  
  189.   return OUT;
  190. }
  191.  
  192. ////////////////////////////////////////////////////////////////////////////////////////////////////
  193. ////////////////////////////////////////////////////////////////////////////////////////////////////
  194.  
  195. /////////////////////////////////////////////////////////////////////
  196. // Here's where we define our boolean values to select features we want from fragLib.cfi
  197. // This is where the pixel shader starts. From here on out it's all pixel shader processing.
  198. /////////////////////////////////////////////////////////////////////
  199.  
  200. void frag_unify_parameters( inout fragPass pPass )
  201. {
  202.  
  203.   // Renormalize the normal map
  204.   pPass.bRenormalizeNormal = true;
  205.  
  206.   // Enable hemisphere lighting.
  207.   pPass.bHemisphereLighting = true;
  208.  
  209.   // If we turned on vertex colors in the material editor (via the checkbox, defined in .ext), then enable vertex color output.
  210. #if %VERTCOLORS
  211.   pPass.bVertexColors = true;
  212. #endif
  213.  
  214.   // If we checked "Glow in diffuse alpha" in the material editor, enable alpha glow.
  215. #if %ALPHAGLOW
  216.   pPass.bAlphaGlow = true;
  217.   pPass.fAlphaGlow_Multiplier = AmbientMultiplier;
  218. #endif
  219.  
  220.   // %BUMP_DIFFUSE is set when a texture is placed in the bump/normal map slot. When it's active, enable bump-mapping.
  221. #if %BUMP_DIFFUSE
  222.   pPass.bDiffuseBump = true;
  223. #endif
  224.  
  225. }
  226.  
  227. ////////////////////////////////////////////////////////////////////////////////////////////////////
  228. ////////////////////////////////////////////////////////////////////////////////////////////////////
  229.  
  230. /////////////////////////////////////////////////////////////////////
  231. // Here's where the begining of our custom code begins. We can setup things for our custom definitions here.
  232. // Some things that vary from material to material are also processed here, like the way specular textures are handled.
  233. // The commented "alu" bits are just notes on how expensive parts of the code are. When you figure out how much things cost, it's a good way to keep track of your shaders overall cost.
  234. /////////////////////////////////////////////////////////////////////
  235.  
  236. void frag_custom_begin(inout fragPass pPass)
  237. {
  238.   // Set opacity, gloss-map and per pixel shininess
  239.   pPass.fAlpha = pPass.cDiffuseMap.w * pPass.IN.Ambient.w;                                              // 1 alu    
  240.  
  241.   pPass.cGlossMap = pPass.cDiffuseMap.w;
  242. #if !%GLOSS_DIFFUSEALPHA // If there's a specular texture in the slot, use it instead of the diffuse maps alpha channel.
  243.   pPass.cGlossMap = tex2D(glossMapSampler, pPass.IN.baseTC.xy);    
  244. #endif  
  245.  
  246.   // Multiply the specular power by the glossmaps alpha. This allows for different glossiness on a single texture.
  247.   pPass.fSpecPow *= pPass.cGlossMap.w;                                                              // 1 alu
  248.  
  249.   // Here we assign our custom definition a value. In this case, we'll use the opposite of ColorExample
  250.   pPass.pCustom.cExample = 1-ColorExample;
  251.  
  252.   /////////////////////////////////////////////////////////////////////
  253.   // Other things you can find in pPass are found in fragLib.
  254.   // A list of constants and their descriptions are at the top of shadelib.cfi and fraglib.cfi. So if you need something, check to see if it's there first.
  255.   /////////////////////////////////////////////////////////////////////
  256.  
  257. }
  258.  
  259. ////////////////////////////////////////////////////////////////////////////////////////////////////
  260. ////////////////////////////////////////////////////////////////////////////////////////////////////
  261.  
  262. /////////////////////////////////////////////////////////////////////
  263. // Lighting. This code is processed once PER LIGHT. So make sure it's not expensive or complex.
  264. /////////////////////////////////////////////////////////////////////
  265.  
  266. void frag_custom_per_light(inout fragPass pPass, inout fragLightPass pLight)
  267. {
  268.   // If we're using bump-mapping, calculate the dot product of the bump-mapped normals and light
  269.   // Read up on standard lambertian lighting if you want a decent explanation of this.
  270.   if ( pPass.bDiffuseBump )
  271.   {
  272.     pLight.fNdotL = dot(pPass.vNormalDiffuse.xyz, pLight.vLight.xyz);                               // 1 alu
  273.   }
  274.  
  275.   // saturate the N dot L so that it fits within 0-1 range (so we don't get ugly artifacts).
  276.   // Again, read up on standard lambertian lighting if you're not quite sure.
  277.   pLight.fNdotL = saturate( pLight.fNdotL );                       // 2 alu  
  278.  
  279.   // Assign the NdotL value for diffuse lighting and multiply it by the lights intensity.
  280.   half3 cDiffuse = pLight.cDiffuse.xyz * pLight.fNdotL;                                            // 1 alu
  281.  
  282.   // Calculate our specular BRDF (read up on BRDFs if you don't know what they are).
  283.   // Typical shaders use Phong for it's simplicity.
  284.   // pPass.vReflVec is the reflection vector, pLight.vLight is the light position, and pPass.fSpecPow is specular info, like glossiness and intensity
  285.   half fSpec = Phong(pPass.vReflVec, pLight.vLight,  pPass.fSpecPow); // 4 alu
  286.  
  287.   // Final specular term
  288.   // Multiply by the specular intensity and the BRDF.
  289.   half3 cSpecular = pLight.cSpecular.xyz * fSpec;                                             // 1 alu
  290.    
  291.   // This contains other light info, like shadows, the lights fall off, and the projective texture.
  292.   half3 cK = pLight.fOcclShadow * pLight.fFallOff * pLight.cFilter;                                // 2 alu
  293.  
  294.   // Add the calculated light information to the final results in pPass and multiply them by the light info above
  295.   pPass.cDiffuseAcc.xyz += cDiffuse.xyz * cK.xyz;                                                   // 1 alu
  296.   pPass.cSpecularAcc.xyz += cSpecular.xyz * cK.xyz;                                                 // 1 alu
  297. }
  298.  
  299. ////////////////////////////////////////////////////////////////////////////////////////////////////
  300. ////////////////////////////////////////////////////////////////////////////////////////////////////
  301.  
  302. /////////////////////////////////////////////////////////////////////
  303. // You can modify the ambient lighting here, though generally you don't need to.
  304. /////////////////////////////////////////////////////////////////////
  305.  
  306. void frag_custom_ambient(inout fragPass pPass, inout half3 cAmbient)
  307. {
  308.  
  309.   // Very simple ambient-occlusion for normals.        
  310.   if( pPass.bDiffuseBump )
  311.   {
  312.     // darken ambient if there is unoccluded area direction
  313.     cAmbient.xyz *= saturate( dot(pPass.vNormalDiffuse.xyz, pPass.vNormal.xyz) );
  314.   }
  315.  
  316.   // Add the ambient to the final shader.
  317.   pPass.cAmbientAcc.xyz += cAmbient.xyz;
  318. }
  319.  
  320. ////////////////////////////////////////////////////////////////////////////////////////////////////
  321. ////////////////////////////////////////////////////////////////////////////////////////////////////
  322.  
  323. void frag_custom_end(in fragPass pPass, inout half3 cFinal)
  324. {
  325.     /////////////////////////////////////////////////////////////////////
  326.     // This is the final step of the fragLib stuff.
  327.     // You can put any final codes to modify the final result of the shader here.
  328.     // Things like color-correction or saturation and such can go here.
  329.     // if "bCustomComposition" is active, you'll need to place the final composition of the shader here (it's in fragLib if you need to know how to do it).
  330.     // Generally you don't need to make a custom composition unless the shader is really complex.
  331.     /////////////////////////////////////////////////////////////////////
  332.  
  333.     // We'll just multiply the final result with cExample, which is the inverse of ColorExample.
  334.     cFinal.xyz *= pPass.pCustom.cExample;
  335.  
  336. }
  337.  
  338. ////////////////////////////////////////////////////////////////////////////////////////////////////
  339. ////////////////////////////////////////////////////////////////////////////////////////////////////
  340.  
  341. /////////////////////////////////////////////////////////////////////
  342. // Include fragLib here because we need it to run the common shader structure.
  343. /////////////////////////////////////////////////////////////////////
  344.  
  345. #include "fragLib.cfi"
  346.  
  347. /////////////////////////////////////////////////////////////////////
  348. // The actual pixel shader.
  349. // You don't need to stick to fragLib, you can easily write your own shader in the pixel shader without any fraglib info.
  350. // It's just easier to use fraglib as it does half the work for you.
  351. // Shaders that don't use fraglib are usually effects, like water, fog, etc.
  352. /////////////////////////////////////////////////////////////////////
  353.  
  354. ///////////////// pixel shader //////////////////
  355.  
  356. pixout BasePS(vert2FragGeneral IN)
  357. {
  358.   pixout OUT = (pixout) 0;  
  359.    
  360.   // Initialize fragPass structure
  361.   fragPass pPass = (fragPass) 0;
  362.   frag_unify(pPass, IN);
  363.  
  364.   // Run all the fragPass/fragLib code (including the code we wrote above in the custom fragPass functions).
  365.   half4 cFinal = frag_shared_output(pPass);
  366.              
  367.   //Output the final result.
  368.   HDROutput(OUT, cFinal, 1);
  369.    
  370.   return OUT;
  371. }
  372.  
  373. /////////////////////////////////////////////////////////////////////
  374. // Shader technique. This is where the shader gets called.
  375. /////////////////////////////////////////////////////////////////////
  376.  
  377. //////////////////////////////// technique ////////////////
  378.  
  379. technique General
  380. <
  381.     /////////////////////////////////////////////////////////////////////
  382.     // These are other passes used by the engine. Things like glow, motion blur, shadows, etc are run in other shaders by using them as extra passes.
  383.     /////////////////////////////////////////////////////////////////////
  384.  
  385.   string Script =
  386.         "TechniqueZ=ZPass;"
  387.         "TechniqueGlow=GlowPass;"        
  388.         "TechniqueMotionBlur=MotionBlurPass;"        
  389.         "TechniqueDetail=DetailPass;"        
  390.         "TechniqueCaustics=CausticsPass;"
  391.         "TechniqueCustomRender=CustomRenderPass;"
  392. #ifndef %DISABLE_RAIN_PASS // Disable rain if checkbox is active.
  393.         "TechniqueRainPass=RainPass;"
  394. #endif
  395.         "TechniqueShadowGen=ShadowGen;"
  396. #ifdef D3D10
  397.         "TechniqueShadowGenDX10=ShadowGenGS;"
  398. #endif
  399.         "TechniqueShadowPass=ShadowPass;"
  400. {
  401.  
  402.   // First pass. You can have more than 1 pass, allowing you to layer effects using alpha blending. See cloth/glass/hair shaders for an example.
  403.   pass p0
  404.   {
  405.  
  406.     /////////////////////////////////////////////////////////////////////
  407.     // Note the "GeneralVS" and "GeneralPS" after the vertex and pixel shader declarations.
  408.     // These change throughout the shaders, depending on which one. You can't use a custom one, so use GeneralPS/GeneralVS.
  409.     // it's not really important and I honestly don't really know why it's there yet, but use it just in-case.
  410.     // I'll eventually find out why they're here. :P
  411.     /////////////////////////////////////////////////////////////////////
  412.  
  413. #if %DYN_BRANCHING
  414.     // If we're using dynamic branching (a cvar enables this in-engine), use pixel shader 3 (because dynamic branching only works on shader model 3 and above)
  415.  
  416.     VertexShader = compile vs_3_0 BaseVS() GeneralVS;
  417.     PixelShader = compile ps_3_0 BasePS() GeneralPS;
  418. #else
  419.     // Otherwise, use the default (which is 2_x or 2_0, I forget which one (cba to check)).
  420.    
  421.     VertexShader = compile vs_Auto BaseVS() GeneralVS;
  422.     PixelShader = compile ps_Auto BasePS() GeneralPS;
  423. #endif    
  424.  
  425.     // Explanations for these are easily available online.
  426.     ZEnable = true;
  427.     ZWriteEnable = true;
  428.     CullMode = Back;
  429.   }  
  430.    
  431. }
  432.  
  433. /////////////////////////////////////////////////////////////////////
  434. // Below we include the common pass shader files so that the passes listed above in the shader technique can run.
  435. /////////////////////////////////////////////////////////////////////
  436.  
  437. //////////////////////////////// Common techniques ////////////////
  438.  
  439. #include "CommonZPass.cfi"
  440. #include "CommonGlowPass.cfi"
  441. #include "CommonMotionBlurPass.cfi"
  442. #include "CommonDetailPass.cfi"
  443. #include "CommonCausticsPass.cfi"
  444. #include "CommonViewsPass.cfi"
  445. #ifndef %DISABLE_RAIN_PASS
  446.   #include "CommonRainPass.cfi"
  447. #endif
  448. #include "ShadowCommon.cfi"
  449. #include "CommonShadowGenPass.cfi"
  450. #ifdef D3D10
  451.     #include "CommonShadowGenPassGS.cfi"
  452. #endif
  453. #include "CommonShadowPass.cfi"
  454.  
  455. /////////////////////// eof ///
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement