Super shader

From Bohemia Interactive Community
Jump to navigation Jump to search


Motivation, goal

Super shader is shader which contains all our standard shader techniques (such as normal, specular, ambient, detail maps etc.) a furthermore it has on input fresnel map defining course of specularity similarly to shader Glass.

Goal is to start use this shader for all usual objects which we see ingame.For such objects which haven't defined some maps may be used default, procedurally generated textures.

Definition of individual stages in material

In frames are quoted default values in case we want that stage ignored.

1. Normal map

Normal map of type _NO or _NOHQ.

Normal mapping is a technique used to fake the lighting of surface bumps and dents. It is used to enhance the appearance of low polygon models. The R,G,B values for each pixel in the map correspond to the X,Y,Z angle of a surface normal at that pixel.

texture="#(argb,8,8,3)color(0.5,0.5,1,1)";

2. Detail map

Detail map of type _DT.

Detail maps add to diffusion map inside one stage another repeating texture to reach greater fineness for close view onto big surfaces which would too complicated to cover with oversized texture resolution.

WARNING: DetailTexture (_DT) has painting saved inside alpha-channel (RGB components are ignored while this texture is converted from TGA to PAA). Because shader in a way multiplying detail texture with colored diffusion component, it is necessary that average color of detail texture should be 50% grey. Otherwise if material with detail texture is cutoff inside lower LOD then it may result into visible change of texture brightness (MipMapping is applied with distance and angle).

texture="#(argb,8,8,3)color(0.5,0.5,0.5,0)";

3. Macro map

Macro map _MC.

Macro texture contains alternative data to basic texture. Representation of basic texture and macro texture for given place is selected by macrotexture alpha channel (1 means only macro texture , 0 means only basic texture). RGB is calculated as LERP with basic map. Detail map is applied on the end (last) thus overlay original and macro map.

texture="#(argb,8,8,3)color(0,0,0,0)";

4. AmbientShadow map

Ambient shadow map _AS.

Ambient shadow texture contains stored information "how much ambient light is in given place'. White color means full ambient lighting. 0 means none.In fact only channel which is taken advantage of is G - it's thus important that information is correctly in him. Rest of channels doesn't matter and anything can be placed there (may be same as in G).

texture="#(argb,8,8,3)color(0,1,1,1)";

5. Specular map

Specular map of _SMDI type (_SM isn't supported). _SMDI maps are bit depth optimized specular maps. Each channel in the 24 bit (RGB) SMDI map has a different function. The R channel should be set to 1 for all pixels, the G channel functions as a specular map, and the B channel functions as a specular power map (also known as gloss map).

6. Fresnel function

Fresnel function is entered as procedural texture in following form: texture="#(ai,64,64,1)fresnel(N,K)";. N and K are inputs for fresnel equation that's on theirs basics and for given angle able determine how much reflects under given angle. N and K are constant for given material and are known for individual elements. Following table include roundup of basic materials and theirs settings. In table are shown values for individual wavelenghts which was possible found. Visible spectrum is approx 400µm-800µm. Interesting are values around middle of this inerval (if available).

Material wavelength N K
Aluminum 600 1.3 7
Cobalt 600 0.2 3
Copper 850 2.08 7.15
Gold 600 0.3 3
Iron 886 3.12 3.87
Lead 850 1.44 4.35
Molybdenum 954 2.77 3.74
Nickel 855 2.59 4.55
Palladium 827 2.17 5.22
Platinum 827 2.92 5.07
Silver 600 0.2 3
Titanium 821 3.21 4.01
Vanadium 984 2.94 3.50
Tungsten 827 3.48 2.79


Additional sources where You can find N and K values: http://en.wikipedia.org/wiki/List_of_refractive_indices

Other values must must be found e.g. empirically. For that may help following program which can inside TexView visualize course of fresnel function for given N and K (on left is normal line to line of sight, on right directly against line of sight, the more is value closer 1 the higher reflection is):

N = 0.969770;
K = 0.0118000;
X = u * 0.5 + 0.5;
S = acos(X);
AA = sqrt((sqrt((N^2-K^2-sin(S)^2)^2 + 4*N^2*K^2)+(N^2-K^2-sin(S)^2))/2);
BB = sqrt((sqrt((N^2-K^2-sin(S)^2)^2 + 4*N^2*K^2)-(N^2-K^2-sin(S)^2))/2);
FS = (AA^2+BB^2-2*AA*cos(S) + cos(S)^2)/(AA^2+BB^2+2*AA*cos(S) + cos(S)^2);
FP = FS*(AA^2+BB^2-2*AA*sin(S)*tan(S)+sin(S)^2*tan(S)^2)/(AA^2+BB^2+2*AA*sin(S)*tan(S)+sin(S)^2*tan(S)^2);
r=1;
g=1;
b=1;
a=(FS+FP)/2;

7. Environmental map

Environmental map.

This technology allows show environmental reflections on materials. The reflected environment is static and only changes it's lighting accordingly to time of day. Environment is characterized by texture which is called environmental map. In our case environmental map presents hemisphere projection into plane.

Example

ambient[]={1.000000,1.000000,1.000000,1.000000};
diffuse[]={1.000000,1.000000,1.000000,1.000000};
forcedDiffuse[]={0.000000,0.000000,0.000000,0.000000};
emmisive[]={0.000000,0.000000,0.000000,1.000000};
specular[]={0.700000,0.700000,0.700000,1.000000};
specularPower=180.000000;
PixelShaderID="Super";
VertexShaderID="Super";
class Stage1
{
	texture="ca\weapons\data\aimpoint_NOHQ.paa";
	uvSource="tex";
};
class Stage2
{
	texture="#(argb,8,8,3)color(0.5,0.5,0.5,0)";
	uvSource="tex";
};
class Stage3
{
       texture="#(argb,8,8,3)color(0,0,0,0)";
	uvSource="tex";
};
class Stage4
{
	texture="#(argb,8,8,3)color(0,1,1,1)";
	uvSource="tex";
};
class Stage5
{
	texture="ca\weapons\data\aimpoint_SMDI.paa";
	uvSource="tex";
};
class Stage6
{
	texture="#(ai,64,64,1)fresnel(1.3,7)";
	uvSource="none";
};
class Stage7
{
	texture="ca\air\data\env_co.paa";
	uvSource="none";
};


Super shader inside CgFX for Autodesk Maya

Shader usage

In software Maya use shader as material type "Cgfx Shader". Then is required set path to file "Super_01.cgfx" in parameter "CgFX File" in group "CgFX Shader". In same directory with file "Super_01.cgfx" must be also header file "headMAYA.fxh". After loading cgfx file appears material paramaters in options group "Super_01.cgfx Parameters" (see appended picture).


Vertex Data

For correct function of material is needed rightly set "Vertex Data" for particular model.

pos - vertex position

tc0 - texture coordinates for first "wrap" (for textures CO, NO, DT, SMDI)

tc1 - texture coordinates for second "wrap" (for textures AS, MC)

norm - vertex normal line

tanX, tanY - tangent-space for first "wrap" (for example of settings see interface picture below)


Material paramaters (Super_01.cgfx Parameters)

show final color of material - shows unlit resulting color of material

show world-space normals - shows reverse normal map

show fresnel - at model it shows intensity of reflections in gray scale according to set 'fresnel' parameters


use sRGB conversions - on input are textures converted into linear space, in which are carried calculations and result is again converted to sRGB

use texture coords 2 - for selected textures (MC, AS) is used second mapping

ambient, diffuse, forced diffuse, specular - colors of material components; they have same meaning as in material editor Objektiv2

specular power - specular strenght


TEXTURES

(COLOR, NORMAL, DETAIL, MACRO, AMBIENT, SPECULAR, ENVIRONMENTAL)

For each texture there is parameter fader which blends betweensubstitute color and texture. This solution replaces procedural textures. Fader setting to 0 means that it will use only substitute color while setting mean it will use only texture. For NORMAL map there is no substitute color setup as it's hardset to RGB=(0.5, 0.5, 1.0), which is default normal line direction (upright to surface). With fader it's possible test various "strenght" of normal map (values higher than 1 increasing influence while smaller decreasing influence) and then eventually alter texture. For DETAIL map the situation is similar, substitute color is hardset to RGB=(0.5, 0.5, 1.0) and you can define by fader different strenght.

Example setting up procedural color map "#(argb,8,8,3)color(0.5,0.5,0.5,1,CO)": "COLOR MAP fader" set to 0, "color" set to 50% šedou and "(alpha)" to 1

environ. map multiplier - environmental map multiplier (tuning parameter related to lighting)


FRESNEL

Fresnel calculation with help of coefficients N and K is not suitable for intgration into CgFX shaderu therefore it's replaced by simpler variants which fails to naturally represent curve for metal materials. fresnel R0 - specular strenght on surface upright to direction of sight fresnel grade - curve "steepness"

detailTcMulX, detailTcMulY - mapping multiplier for detail texture


Illumination model

ambient lighting ON, diffuse lighting ON, specular lighting ON, specular environmental lighting ON - tuning switches of lighting model's individual components

global light multiplier - multiplier of all lights


HEMISPHERICAL LIGHT

hemispheric light ON - light switch

sky light - color and intensity of light from top hemisphere

ground light - color and intensity of light from top hemisphere


DIRECTIONAL LIGHT

dir light ON - light switch

bi-dir light ON - contrejour light switch (something like reflection)

dir light direction - link up directional light in scene (right mouse button click and choose correct light)

dir light color - light color and intensity

bi-dir color - color of material from which contrejour light reflects

dir light multiplier - directional light multiplier


POINT LIGHT

point light ON - light switch

point light per-pixel - enable more precise lighting calculation (for testing only; it's not part of super shader)

point light position - link up point light in scene (right mouse button click and choose correct light)

point light color - light color and intensity ?of directional light component?

point light ambient - ambient component color and intensity (depends only on distance not direction)

point light multiplier - light intensity multiplier

point light attenuation - coefficient for intensity decline with distance (more precisely reverse value - the higher value, the bigger afterglow)



Picture with interface of super-shader material inside Maya:
SuperShaderMaya1.jpg




Super Shader FX for Autodesk 3DS Max

Shader usage

Display driver for 3DS Max must be set to DirectX 9.0 to be able use shader. To create new material dot his: Within Material editor choose free slot and set material type to "DirectX 9 Shader" (instead default "Standard"). Then is needed set path to file "SuperShader.fx" inside tab "DirectX 9 Shader". When file is loaded it will show material paramaters within tab "SuperShader.fx Parameters" (see appended picture).


Picture with interface of super-shader material inside 3DS Max:
SuperShaderFX01.jpg

"Technique"

Each fx-file may include multiple techniques. For alpha blending i use standalone technique because it's not possible do with just checkbox field.

SuperShader_Default - for opacity geometry

SuperShader_AlphaBlend - for transparent geometry; often hard to sort and nothing can be done about it

SuperShader_MapTest - with help of paramaters "map test CO alpha" till "map test ENVIR" shows on model particular texture or it's component (it's used only downmost checked option - doesn't go other way at present time; options "map test" works only in this technique

Illumination model

Material can be assigned one directional and one point lighting (paramaters "dir light direction" and "point light position"). Color and intensity is taken directly from light.


rest is nearly same as here: [[1]]


How is gained fresnel from procedural texture

Procedural texture for fresnel function contains single-dimensional conversion table (visually it's sort of brightness gradient ) from which reads value on position (U,V). Value U is calculated as scalar product between normal line and direction to the camera (both vertors must be normalized)) in each surface point of model (this value also represents cosine of angle between vectors). Value V is always 0.5, therefore it's unneeded that texture height would be different than 1. For example: function with parameters N = 1.0 and K = 0.6 should be entered with width 64 and height 1: "#(ai,64,1,1)fresnel(1.0,0.6)". Size of texture is then only 64 bytes instead 4096 in case if the height is 64. When we realize that such texture is generated for every material with fresnel inside scene, then saving of memory and performance is indispensable!


Picture with visual representation of obtaining fresnel value from procedural texture:
FresnelLUT.jpg