Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////
- // Includes
- /////////////////////////////////////////////////////////////////////
- #include "Common.cfi" // Common contains general codes and information.
- // Shader global descriptions
- float Script : STANDARDSGLOBAL
- <
- string Script =
- "Public;"
- "SupportsAttrInstancing;"
- "ShaderDrawType = Light;"
- "ShaderType = General;"
- >;
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////// Custom shading pass structure /////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // Custom definitions.
- // Custom definitions are used to pass data that we want to use in more than a single portion of the code.
- /////////////////////////////////////////////////////////////////////
- struct fragPassCustom
- {
- //Example:
- half3 cExample;
- };
- /////////////////////////////////////////////////////////////////////
- // Custom light definitions. Same as above but for light pass (not usually used)
- /////////////////////////////////////////////////////////////////////
- struct fragLightPassCustom
- {
- };
- /////////////////////////////////////////////////////////////////////
- // Include shadeLib, which contains general shading functions, like specular models and color effects, amongst others.
- /////////////////////////////////////////////////////////////////////
- #include "ShadeLib.cfi"
- /////////////////////////////////////////////////////////////////////
- // Some constants for use in the shader. Some functions can be found in FXConstantDefs.cfi for an idea of what you can use.
- /////////////////////////////////////////////////////////////////////
- // Un-Tweakables /////////////////
- float4x4 mCamera : PB_CameraMatrix;
- float4 AmbientObjectCol : PI_ObjectAmbColComp;//x=Ambient.w, y=ObjColor.w, w = Obj Rend quality
- /////////////////////////////////////////////////////////////////////
- // These samples are defined in FXSamplerDefs.cfi
- // We simply place the defined name (#SUBSURFACEMAP as example) and you get access to the sampler.
- /////////////////////////////////////////////////////////////////////
- //////////////////////////////// Samplers ////////////////
- SUBSURFACEMAP
- //DECALMAP
- OPACITYMAP
- ENVIRONMENTMAP
- ENVIRONMENTCUBEMAP
- //////////////////////////////// Common vertex shader ////////////////
- /////////////////////////////////////////////////////////////////////
- // Include vertexLib, which contains the global/generic vertex shader functions.
- /////////////////////////////////////////////////////////////////////
- #include "VertexLib.cfi"
- /////////////////////////////////////////////////////////////////////
- // Here's where we define material properties.
- // The material editor reads these and uses them as tweakable values.
- /////////////////////////////////////////////////////////////////////
- // Tweakables /////////////////
- float AmbientMultiplier // Name of the variable to be used by the shader.
- <
- /////////////////////////////////////////////////////////////////////
- // The registry of the slider. Make sure these don't overlap or the shader won't work.
- // You generally start with 3.x
- // The name tells you where you can use the variable (because it needs to be converted)
- // vsregister is for vertex shader, psregister is for pixel shader. You can use both, but need at least one.
- /////////////////////////////////////////////////////////////////////
- psregister = PS_REG_PM_3.x; // Example of change: PS_REG_PM_5.y (x,y,z,w, like everything else)
- string UIHelp = "Set alpha glow multiplier"; // Text/description to be displayed at the bottom of the material editor.
- string UIName = "Alpha glow multiplier"; // Name of the material property.
- string UIWidget = "slider"; // Type. This one's a slider.
- float UIMin = 0.0; // Minimum slider value.
- float UIMax = 32.0; // Maximum slider value.
- float UIStep = 0.1; // Slider step.
- > = 1.0; // Initial value for unedited materials (when you assign the new material shader).
- /////////////////////////////////////////////////////////////////////
- // Here's another, but it's a 4-value float instead of a single float.
- // Note that there's no .x(yzw) at the end of the register because it's using the entire register (all 4 values)
- // Instead of a slider, it's a color selection.
- /////////////////////////////////////////////////////////////////////
- float4 ColorExample
- <
- vsregister = VS_REG_PM_4;
- psregister = PS_REG_PM_3.y;
- string UIName = "Vertex color modifier";
- string UIWidget = "color";
- > = {0.45, 0.62, 0.54, 1.0}; //4 values instead of 1.
- ///////////////// Vertex shader //////////////////
- /////////////////////////////////////////////////////////////////////
- // Here's the structure for the vertex shader. You can define custom outputs here.
- // 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.
- /////////////////////////////////////////////////////////////////////
- struct vert2FragBase
- {
- // Position. Can also be "float4 Position : POSITION", but we use OUT_P to get the general info from another shader (ModificatorVT.cfi I think).
- OUT_P
- // Texture coordinates.
- float4 baseTC : TEXCOORDN;
- // tangent
- float4 vTangent : TEXCOORDN;
- // Binormal
- float4 vBinormal : TEXCOORDN;
- // Camera vector. Used for specularity and view-dependent effects (fresnel, rim lighting)
- float4 vView : TEXCOORDN;
- // Screen-space coordinates
- float4 screenProj : TEXCOORDN_centroid; //z used for instanced alpha test value
- // If the model is being lit by a projector light, we pass through the projections coordinates.
- #if %_LT_LIGHTS && %_LT_HASPROJ
- float4 projTC : TEXCOORDN;
- #endif
- // Color for ambient lighting.
- float4 Ambient : TEXCOORDN;
- // If there's fog, we pass through the fog information.
- #if %_RT_FOG
- float4 AvgFogVolumeContrib : TEXCOORDN;
- #endif
- //D3D10's alphatesting works different, so we need to pass some info through.
- #if D3D10
- #if %_RT_NOZPASS && %_RT_ALPHATEST
- float4 AlphaTest : TEXCOORDN;
- #endif
- #endif
- // Pass the vertex color data.
- float4 Color : COLOR0;
- };
- /////////////////////////////////////////////////////////////////////
- // Actual vertex shader.
- // This is where you add all vertex-processing code, like per-vertex lighting, etc.
- // It's much cheaper than the pixel shader as this is only run for each vertex on the model, and not each pixel.
- // Anything that doesn't need textures and can be done in the vertex shader, should be.
- /////////////////////////////////////////////////////////////////////
- vert2FragBase BaseVS(app2vertGeneral IN)
- {
- vert2FragBase OUT;
- // This is just here for documentation. It doesn't really work or anything heh.
- #ifndef OPENGL
- OUT = (vert2FragBase) 0;
- #endif
- // Calls the generic vertex shader from vertexLib.cfi.
- vs_shared_output( IN, OUT, false );
- //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)
- // Example: Multiply the vertex colors by the ColorExample values chosen in the material editor.
- OUT.Color *= ColorExample.
- return OUT;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // Here's where we define our boolean values to select features we want from fragLib.cfi
- // This is where the pixel shader starts. From here on out it's all pixel shader processing.
- /////////////////////////////////////////////////////////////////////
- void frag_unify_parameters( inout fragPass pPass )
- {
- // Renormalize the normal map
- pPass.bRenormalizeNormal = true;
- // Enable hemisphere lighting.
- pPass.bHemisphereLighting = true;
- // If we turned on vertex colors in the material editor (via the checkbox, defined in .ext), then enable vertex color output.
- #if %VERTCOLORS
- pPass.bVertexColors = true;
- #endif
- // If we checked "Glow in diffuse alpha" in the material editor, enable alpha glow.
- #if %ALPHAGLOW
- pPass.bAlphaGlow = true;
- pPass.fAlphaGlow_Multiplier = AmbientMultiplier;
- #endif
- // %BUMP_DIFFUSE is set when a texture is placed in the bump/normal map slot. When it's active, enable bump-mapping.
- #if %BUMP_DIFFUSE
- pPass.bDiffuseBump = true;
- #endif
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // Here's where the begining of our custom code begins. We can setup things for our custom definitions here.
- // Some things that vary from material to material are also processed here, like the way specular textures are handled.
- // 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.
- /////////////////////////////////////////////////////////////////////
- void frag_custom_begin(inout fragPass pPass)
- {
- // Set opacity, gloss-map and per pixel shininess
- pPass.fAlpha = pPass.cDiffuseMap.w * pPass.IN.Ambient.w; // 1 alu
- pPass.cGlossMap = pPass.cDiffuseMap.w;
- #if !%GLOSS_DIFFUSEALPHA // If there's a specular texture in the slot, use it instead of the diffuse maps alpha channel.
- pPass.cGlossMap = tex2D(glossMapSampler, pPass.IN.baseTC.xy);
- #endif
- // Multiply the specular power by the glossmaps alpha. This allows for different glossiness on a single texture.
- pPass.fSpecPow *= pPass.cGlossMap.w; // 1 alu
- // Here we assign our custom definition a value. In this case, we'll use the opposite of ColorExample
- pPass.pCustom.cExample = 1-ColorExample;
- /////////////////////////////////////////////////////////////////////
- // Other things you can find in pPass are found in fragLib.
- // 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.
- /////////////////////////////////////////////////////////////////////
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // Lighting. This code is processed once PER LIGHT. So make sure it's not expensive or complex.
- /////////////////////////////////////////////////////////////////////
- void frag_custom_per_light(inout fragPass pPass, inout fragLightPass pLight)
- {
- // If we're using bump-mapping, calculate the dot product of the bump-mapped normals and light
- // Read up on standard lambertian lighting if you want a decent explanation of this.
- if ( pPass.bDiffuseBump )
- {
- pLight.fNdotL = dot(pPass.vNormalDiffuse.xyz, pLight.vLight.xyz); // 1 alu
- }
- // saturate the N dot L so that it fits within 0-1 range (so we don't get ugly artifacts).
- // Again, read up on standard lambertian lighting if you're not quite sure.
- pLight.fNdotL = saturate( pLight.fNdotL ); // 2 alu
- // Assign the NdotL value for diffuse lighting and multiply it by the lights intensity.
- half3 cDiffuse = pLight.cDiffuse.xyz * pLight.fNdotL; // 1 alu
- // Calculate our specular BRDF (read up on BRDFs if you don't know what they are).
- // Typical shaders use Phong for it's simplicity.
- // pPass.vReflVec is the reflection vector, pLight.vLight is the light position, and pPass.fSpecPow is specular info, like glossiness and intensity
- half fSpec = Phong(pPass.vReflVec, pLight.vLight, pPass.fSpecPow); // 4 alu
- // Final specular term
- // Multiply by the specular intensity and the BRDF.
- half3 cSpecular = pLight.cSpecular.xyz * fSpec; // 1 alu
- // This contains other light info, like shadows, the lights fall off, and the projective texture.
- half3 cK = pLight.fOcclShadow * pLight.fFallOff * pLight.cFilter; // 2 alu
- // Add the calculated light information to the final results in pPass and multiply them by the light info above
- pPass.cDiffuseAcc.xyz += cDiffuse.xyz * cK.xyz; // 1 alu
- pPass.cSpecularAcc.xyz += cSpecular.xyz * cK.xyz; // 1 alu
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // You can modify the ambient lighting here, though generally you don't need to.
- /////////////////////////////////////////////////////////////////////
- void frag_custom_ambient(inout fragPass pPass, inout half3 cAmbient)
- {
- // Very simple ambient-occlusion for normals.
- if( pPass.bDiffuseBump )
- {
- // darken ambient if there is unoccluded area direction
- cAmbient.xyz *= saturate( dot(pPass.vNormalDiffuse.xyz, pPass.vNormal.xyz) );
- }
- // Add the ambient to the final shader.
- pPass.cAmbientAcc.xyz += cAmbient.xyz;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void frag_custom_end(in fragPass pPass, inout half3 cFinal)
- {
- /////////////////////////////////////////////////////////////////////
- // This is the final step of the fragLib stuff.
- // You can put any final codes to modify the final result of the shader here.
- // Things like color-correction or saturation and such can go here.
- // 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).
- // Generally you don't need to make a custom composition unless the shader is really complex.
- /////////////////////////////////////////////////////////////////////
- // We'll just multiply the final result with cExample, which is the inverse of ColorExample.
- cFinal.xyz *= pPass.pCustom.cExample;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////
- // Include fragLib here because we need it to run the common shader structure.
- /////////////////////////////////////////////////////////////////////
- #include "fragLib.cfi"
- /////////////////////////////////////////////////////////////////////
- // The actual pixel shader.
- // You don't need to stick to fragLib, you can easily write your own shader in the pixel shader without any fraglib info.
- // It's just easier to use fraglib as it does half the work for you.
- // Shaders that don't use fraglib are usually effects, like water, fog, etc.
- /////////////////////////////////////////////////////////////////////
- ///////////////// pixel shader //////////////////
- pixout BasePS(vert2FragGeneral IN)
- {
- pixout OUT = (pixout) 0;
- // Initialize fragPass structure
- fragPass pPass = (fragPass) 0;
- frag_unify(pPass, IN);
- // Run all the fragPass/fragLib code (including the code we wrote above in the custom fragPass functions).
- half4 cFinal = frag_shared_output(pPass);
- //Output the final result.
- HDROutput(OUT, cFinal, 1);
- return OUT;
- }
- /////////////////////////////////////////////////////////////////////
- // Shader technique. This is where the shader gets called.
- /////////////////////////////////////////////////////////////////////
- //////////////////////////////// technique ////////////////
- technique General
- <
- /////////////////////////////////////////////////////////////////////
- // 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.
- /////////////////////////////////////////////////////////////////////
- string Script =
- "TechniqueZ=ZPass;"
- "TechniqueGlow=GlowPass;"
- "TechniqueMotionBlur=MotionBlurPass;"
- "TechniqueDetail=DetailPass;"
- "TechniqueCaustics=CausticsPass;"
- "TechniqueCustomRender=CustomRenderPass;"
- #ifndef %DISABLE_RAIN_PASS // Disable rain if checkbox is active.
- "TechniqueRainPass=RainPass;"
- #endif
- "TechniqueShadowGen=ShadowGen;"
- #ifdef D3D10
- "TechniqueShadowGenDX10=ShadowGenGS;"
- #endif
- "TechniqueShadowPass=ShadowPass;"
- {
- // 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.
- pass p0
- {
- /////////////////////////////////////////////////////////////////////
- // Note the "GeneralVS" and "GeneralPS" after the vertex and pixel shader declarations.
- // These change throughout the shaders, depending on which one. You can't use a custom one, so use GeneralPS/GeneralVS.
- // it's not really important and I honestly don't really know why it's there yet, but use it just in-case.
- // I'll eventually find out why they're here. :P
- /////////////////////////////////////////////////////////////////////
- #if %DYN_BRANCHING
- // 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)
- VertexShader = compile vs_3_0 BaseVS() GeneralVS;
- PixelShader = compile ps_3_0 BasePS() GeneralPS;
- #else
- // Otherwise, use the default (which is 2_x or 2_0, I forget which one (cba to check)).
- VertexShader = compile vs_Auto BaseVS() GeneralVS;
- PixelShader = compile ps_Auto BasePS() GeneralPS;
- #endif
- // Explanations for these are easily available online.
- ZEnable = true;
- ZWriteEnable = true;
- CullMode = Back;
- }
- }
- /////////////////////////////////////////////////////////////////////
- // Below we include the common pass shader files so that the passes listed above in the shader technique can run.
- /////////////////////////////////////////////////////////////////////
- //////////////////////////////// Common techniques ////////////////
- #include "CommonZPass.cfi"
- #include "CommonGlowPass.cfi"
- #include "CommonMotionBlurPass.cfi"
- #include "CommonDetailPass.cfi"
- #include "CommonCausticsPass.cfi"
- #include "CommonViewsPass.cfi"
- #ifndef %DISABLE_RAIN_PASS
- #include "CommonRainPass.cfi"
- #endif
- #include "ShadowCommon.cfi"
- #include "CommonShadowGenPass.cfi"
- #ifdef D3D10
- #include "CommonShadowGenPassGS.cfi"
- #endif
- #include "CommonShadowPass.cfi"
- /////////////////////// eof ///
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement