| xxx.cg | Shader itself |
| xxx.txt | Resources > Material Properties: shader description field and property parameter names. |
//_VERSION(3)
//_SHADER_TYPE(surface)
//_CUSTOM_PASS(outline,shadow,pickup,hidden_area)
//_GUID(0123456789ABCDEF0123456789ABCDEF)
//_VIN(position)
//_VOUT(normal)
//_DECL_TEXTURE_2D(name)
//_DECL_TEXTURE_CUBE(name)
//_DECL_TEXTURE_3D(name)
//_DECL_COLOR3(name)
//_DECL_COLOR4(name)
//_DECL_FLOAT(name) //_DECL_FLOAT2(name) //_DECL_FLOAT3(name) //_DECL_FLOAT4(name)
//_DECL_INT(name) //_DECL_INT2(name) //_DECL_INT3(name) //_DECL_INT4(name)
//_DECL_BOOL(name)
//_DECL_MATRIX(name)
//_DECL_ENUM(name);//enum:Red Green Blue
//_DRAW_PASS(opaque)
//_BLEND(true)
//_BLEND_SRC_COLOR(one); //_BLEND_DST_COLOR(one); //_BLEND_SRC_ALPHA(one); //_BLEND_DST_ALPHA(one);
@//_COLOR_WRITE(true true true true)
//_DEPTH_TEST(false)
//_DEPTH_WRITE(true)
//_STENCIL_REF(1)
//_STENCIL_MASK(1)
//_STEINCIL_ENABLE(true)
//_STENCIL_OPPASS() //_STENCIL_OPDEPTHFAIL() //_STENCIL_OPFAIL()
//_DEPTH_FUNC(lequal) //_STENCIL_FUNC(noteqal)
@//_DITHER_TRANSLUCENT(false)
@//_SKIN(false)
@//_MORPH(false)
@//_ZPREPASS(false)
@//_FORWARD_FOG(true)
@//_NO_OUTLINE_PASS(true)
@//_NO_HIDDEN_AREA_PASS(true)
@//_CAST_SHADOW(false)
This section explains the parameters that are passed from Bakin to a shader.
vec3 eyePosition; //Camera position vec3 eyeDirection; //Camera orientation (-Z) mat4 projToWorld; //Projection to World matrix mat4 worldToProj; //World to Projection matrix mat4 projToView; //Projection To View(Camera) matrix mat4 viewToProj; //View(Camera) To Projection matrix mat4 worldToView; //World To View matrix float drawTime; //Elapsed time (sec) int drawCount; //Number of elapsed frames float reflectionIntensity; //Environment Map Intensity float reflectionMipCount; //Number of environment map mipmaps float IBLMipIndex; //Environment map mipmap index for IBL float IBLIntensity; //IBL Intensity vec4 fogColor; //fog(See editor's Rendering Settings) float fogStart; float fogDepthDensity; float fogHeightFallOff; vec2 _3DScreenSize; //Viewport size for 3D rendering vec2 _3DScreenScale; //Scale to frame buffer during 3D rendering vec2 _DefaultSurfaceSize; //Frame buffer size for 3D rendering vec2 _GameViewScreenSize; //Game Resolution mat4 shadowMatrix[4]; //Shadow Matrix int cascadeCount; //Shadow cascade count float cascadeVisualize; //For Development float shadowBias; //Shadow Bias float shadowDistance; //Shadow effective distance float shadowmapScale; //Dynamic resolution of shadow map //One Directional(With Shadow) vec3 directionalLightDir; //Parallel light source direction vec3 directionalLightColor; //Parallel light source color vec3 directionalLightShadowColor; //Unused //Ambient vec3 ambientLightColor; //Ambient light color float buildingLightBrightness; //Brightness of building lighting float ssrScale; //Unused float billboardLightModulation; //Billboard Light Intensity vec4 hiddenAreaColor; //Color of Hidden Area float distanceFadeBias; //Distance Fade Parameter float distanceFadeStart; float distanceFadeNearClip; // float deltaTime; //Elapsed time from previous frame
SVAL(emissionMultiply)The above and more.
int localLightCount; //Number of allocated local lights int localLightIndex[LOCALLIGHTASSIGNLIMIT]; //Index of allocated local lights float emissionMultiply; //Emissive Strength float opacityMultiplier; //Opacity Strength vec4 overrideColor; //Override Color float outlineWidth; //Outline Width vec4 outlineColor; //Outline Color float pickupID; //ID for the editor float distanceFadeBias; //Distance fade bias float buildingLightMask; //Whether it is affected by the building's lighting settings vec2 uvofs; //Built-in UV Offset vec2 uvscl; //Built-in UV Scale mat3x4 localToWorldMatrix; //local to world matrix (typical value) mat3x4 worldToLocalMatrix; //world to local matrix (typical value) mat3x4 localToWorldAxis; //local to world matrix (typical value) mat3x4 worldToLocalAxis; //world to local matrix (typical value)
struct LocalLight{
int type; //Light Type
float radius; //Radius
float specularScale; //Specular strength
vec3 color; //Color
vec3 position; //Position
float innerAngle; //Internal angle of spotlight
vec3 direction; //Spotlight Direction
float outerAngle; //External angle of spotlight
};
LocalLight _localLights[LOCAL_LIGHT_BLOCK_ARRAYSIZE];//DECL_COLOR3(color)If you declare something like above,
vec3 tmp = UVAL(color);can be used.
#ifdef FLAG_VP /*Describe vertex shader*/ #endif
#ifdef FLAG_FP /*Describe fragment shader*/ #endifIf you bundle as above, that part will be enabled when compiling the fragment shader.
#ifdef _SHADOW_PASS_COMPILE /*Description for shadow pass case*/ #endif
#ifdef FLAG_VP
#ifdef _SHADOW_PASS_COMPILE
VertexOutStruct usermain(VertexInStruct vin)
{
/*Vertex shader code for shadow pass*/
}
#endif
#ifdef _MAIN_PASS_COMPILE
VertexOutStruct usermain(VertexInStruct vin)
{
/*Vertex shader code for the main pass*/
}
#endif
#endif
The main functions of vertex shader and fragment shader should be declared with the name usermain.
VertexOutStruct usermain(VertexInStruct vin)
{
VertexOutStruct vout
vout.position = calcPositionWorldToScreen(calcPositionLocalToWorld(vin.position));
vout.texCoord0.xy = vin.uv0.xy;
return vout;
}PBRMaterialResult usermain(FragmentInStruct fin)
{
vec4 t = SAMPLING2D( AMap, fin.texCoord0.xy );
if(t.w < UVAL(discard_threshold))discard;
vec4 nt = SAMPLING2D( NMap, fin.texCoord0.xy );
nt.xyz = decompressNormal(nt.xy);
vec3 n = nt.y * fin.binormal * UVAL(normalscl) + nt.x * fin.tangent * UVAL(normalscl) + nt.z * fin.normal;
vec4 rmt = SAMPLING2D( RMMap, fin.texCoord0.xy );
PBRMaterialResult fout;
fout.baseColor = t.xyz * fin.color.xyz * UVAL(color);
fout.normal = normalize(n.xyz);
fout.roughness = rmt.g * UVAL(roughness);
fout.metallic = rmt.b * UVAL(metallic);
fout.specular= rmt.a * UVAL(specular);
fout.emissiveColor = UVAL(emissive_color) * rmt.r * UVAL(emissive_strength);
fout.worldPosition = CalcWorldPosition();
fout.opacity = 1.0f;
fout.sssColor = vec4(0,0,0,0);
return fout;
}
vec4 usermain(FragmentInStruct fin)
{
return SAMPLING2D( DifMap, fin.texCoord0.xy ) * fin.color;
}vec3 calcPositionLocalToWorld(vec3 vtx)
vec3 calcPositionWorldToView(vec3 vtx)
vec4 calcPositionWorldToScreen(vec3 vtx)
vec4 calcPositionViewToScreen(vec3 vtx)
vec3 calcNormalLocalToWorld(vec3 normal)
vec4 calcTangentLocalToWorld(vec4 tangent)
vec3 calcBinormal(vec3 normal, vec4 tangent)
vec calcOutlineVertex(vec3 vtx, vec3 normal)
vec3 getLocalToWorldMatrix()
@float getOutlineWidth()
vec3 CalcWorldPosition()
vec3 CalcViewPosition()
float directionalLightAttenuation(vec3 wposition)
float localLightAttenuation(int index, vec3 wposition)
int getLocalLightCount()
vec3 getLocalLightPosition(int index)
getLocalLightColor(int index)
vec4 calcFog(vec3 wposition)
vec3 GetReflectionImg(vec3 wposition, vec3 normal, float level)
vec3 GetIBL( vec3 normal, float level)
vec4 fpGetOutlineColor()
PBRLightingResult PbrSSSWithWorldPosition(vec3 albedo, vec3 normal, vec3 emissive, float roughness, float metallic, float specular, float opacity, vec4 subsurface, vec3 worldPosition, bool frontFacing)
vec4 Unlit(vec3 emissive, float opacity, vec3 worldPosition)
vec4 UnlitWithReflection(vec3 albedo, vec3 normal, vec3 emissive, float roughness, float metallic, float opacity, vec3 worldPosition, bool frontFacing)
void Translucent(vec4 value, vec3 worldPosition)