#author("2026-03-12T09:34:36+09:00","default:admin","admin")
*Shader Documentation [#zac52db8]
#contents

**Location of the Created Shader [#qbd0b7ef]
- ''Folder for Storing Shaders''
To apply the created shader to Bakin, place the following files in the \bin\bakin\lib\sysresource\shader\Builtin folder under the directory where Bakin is installed.
The “xxx” sections in the table represent shader names.
|xxx.cg|Shader itself|
|xxx.txt|Resources > Material Properties: shader description field and property parameter names.|

- ''Text for Description Field and Property Field''
This text is required when registering a shader in the Resources > Particle Properties section.
By including a language code, you can set text tailored to the language settings when using Bakin.
When creating it, please refer to the .txt file of an existing shader.

**Shader Documentation [#y848575e]
**Declaration [#x0a3f2ea]

-&size(16){''Version''};

--''Example''
 //_VERSION(3)

--''Description''
The current version is 3.
#br
#br
-&size(16){''Shader Type''};
--''Example''
 //_SHADER_TYPE(surface)

--''Description''
Declares the shader to be a surface type (using a common vertex shader) shader.
---''surface'': A shader that uses the common vertex shader.
---''screen'': A shader intended for post effects and similar purposes (used internally).
---''ui'': A shader for UI rendering (used internally).
---If you use your own vertex shaders, do not declare the shader type.
#br
#br
-&size(16){''Custom Pass''};
--''Example''
 //_CUSTOM_PASS(outline,shadow,pickup,hidden_area)

--''Description''
Bakin renders the same model several times with different shaders when rendering a single screen.
For example, rendering when creating a shadow map. These additional passes are as follows:

---''Shadow''
---''Outline''
---''Pickup'': Hit test on the editor screen
---''HiddenArea'': When the main cast members are hidden behind buildings during the game execution
#br
Although these shaders are provided in common, there are cases where you may want to add individual shaders.
In doing so, use the _CUSTOM_PASS declaration to inform Bakin which passes to customize.
Cases you may want to handle individually are, for example, when a texture (with discard) makes some polygons transparent, or when the polygons are swaying in the wind.
#br
#br
-&size(16){''GUID''};
--''Example''
 //_GUID(0123456789ABCDEF0123456789ABCDEF)

--''Description''
Describe the non-hyphenated GUID for each shader.
Used to distinguish between shader files with the same name.
GUIDs can be created on websites, GUIDGen in VisualStudio, etc.
#br
#br
-&size(16){''Vertex Shader Input''};
--''Example''
 //_VIN(position)

--''Description''
Specifies elements to be input to the vertex shader.

---''position''
---''texCoord0'' Texture coordinate 0
---''texCoord1'' Texture coordinate 1
---''normal''
---''tangent''
---''color''
#br
The above can be specified.
#br
#br
-&size(16){''Vertex Shader Output''};
--''Example''
 //_VOUT(normal)

--''Description''
Specifies the elements to be output from the vertex shader to the fragment shader.

---''texCoord0'' Texture coordinate 0
---''texCoord1'' Texture coordinate 1
---''normal''
---''tangent''
---''binormal''
---''color''
#br
The above are supported.
#br
#br
-&size(16){''2D Texture''};
--''Example''
 //_DECL_TEXTURE_2D(name)

--''Description''
Declares a 2D texture called name.
#br
#br
-&size(16){''Cube Texture''};
--''Example''
 //_DECL_TEXTURE_CUBE(name)

--''Description''
Declares a cube texture called name.
#br
#br
-&size(16){''3D Texture''};
--''Example''
 //_DECL_TEXTURE_3D(name)

--''Description''
Declares a 3D texture called name.
---The maximum number of user-defined textures is 10.
#br
#br
-&size(16){''RGB Color''};
--''Example''
 //_DECL_COLOR3(name)

--''Description''
Declares a color parameter called name.
#br
#br
-&size(16){''RGBA Color''};
--''Example''
 //_DECL_COLOR4(name)

--''Description''
Declares a color parameter called name.
#br
#br
-&size(16){''float Parameter''};
--''Example''
 //_DECL_FLOAT(name)
 //_DECL_FLOAT2(name)
 //_DECL_FLOAT3(name)
 //_DECL_FLOAT4(name)

--''Description''
Declares a froat parameter called name.
#br
#br
-&size(16){''Integer Parameter''};
--''Example''
 //_DECL_INT(name)
 //_DECL_INT2(name)
 //_DECL_INT3(name)
 //_DECL_INT4(name)

--''Description''
Declares an integer parameter called name.
#br
#br
-&size(16){''BOOL Parameter''};
--''Example''
 //_DECL_BOOL(name)

--''Description''
Declares a BOOL parameter called name.
#br
#br
-&size(16){''MATRIX Parameter''};
--''Example''
 //_DECL_MATRIX(name)

--''Description''
Declares a 4x4 matrix called name.
---Matrix cannot be edited from the Material Editor.
#br
#br
-&size(16){''ENUM Parameter''};
--''Example''
 //_DECL_ENUM(name);//enum:Red Green Blue

--''Description''
Declares an ENUM parameter called name.
After the declaration, declare the name to be enumerated as enum:. In the example, the variable will be able to choose Red Green Blue.



**Declaration 2 [#wb2ba2bd]

-&size(16){''Main Drawing Pass''};
--''Example''
 //_DRAW_PASS(opaque)

--''Description''
Specifies when the main pass will be drawn.

---''opaque''
---''unlit'': Opaque drawing after 'opaque' pass
---''skybox'': After unlit, drawing the sky sphere, etc.
---''translucent''
#br
You can choose from the above.

#br
#br
-&size(16){''Blend''};
--''Example''
 //_BLEND(true)

--''Description''
This setting determines whether or not blending is enabled on the main pass.
You can choose true/false.
Valid when using translucent pass.

#br
#br
-&size(16){''Blend Settings''};
--''Example''
#br
 //_BLEND_SRC_COLOR(one);
 //_BLEND_DST_COLOR(one);
 //_BLEND_SRC_ALPHA(one);
 //_BLEND_DST_ALPHA(one);


--''Description''
Specifies the blending method when blending is enabled.
SRCCOLOR/DSTCOLOR sets the color weighting SRCALPHA/DSTALPHA sets the alpha weighting.

---''zero''
---''one''
---''srccolor''
---''rsrccolor''
---''dstcolor''
---''rdstcolor''
---''srcalpha''
---''rsrcalpha''
---''dstalpha''
---''rdstalpha''
#br
You can choose from the above.

#br
#br
-&size(16){''Color Write''};
--''Example''
 @//_COLOR_WRITE(true true true true)

--''Description''
Configure whether to write to each RGBA channel.
If not declared, writing occurs to all channels.
#br
#br
#br
#br
-&size(16){''Depth Test''};
--''Example''
 //_DEPTH_TEST(false)

--''Description''
Sets whether or not to use depth testing on the main pass.
#br
#br
-&size(16){''Depth Write''};
--''Example''
 //_DEPTH_WRITE(true)

--''Description''
Sets whether or not the depth is written in the main pass.
#br
#br
-&size(16){''Stencil Ref''};
--''Example''
 //_STENCIL_REF(1)

--''Description''
Sets the reference value for the stencil test for the main pass.
#br
#br
-&size(16){''Stencil Mask''};
--''Example''
 //_STENCIL_MASK(1)

--''Description''
Sets the stencil test mask value for the main pass.
#br
#br
-&size(16){''Stencil Test''};
--''Example''
 //_STEINCIL_ENABLE(true)

--''Description''
Specifies whether stencil testing is enabled on the main pass. Default is disabled.
#br
#br
-&size(16){''Stencil Op''};
--''Example''
 //_STENCIL_OPPASS()
 //_STENCIL_OPDEPTHFAIL()
 //_STENCIL_OPFAIL()


--''Description''
Sets how the stencil value will be used in the test results when the stencil test is enabled in the main pass.

---''keep''
---''replace'': Replace by Ref value
---''inc'': Increase by 1
---''dec'': Decrease by 1
#br
You can choose from the above.
#br
#br
-&size(16){''Test Func''};
--''Example''
 //_DEPTH_FUNC(lequal)
 //_STENCIL_FUNC(noteqal)


--''Description''
You can specify acceptable/failure types for the main pass depth test and the stencil test, respectively.

---''never'': Always failed.
---''less'': If less than, then acceptable.
---''equal'': If equal, then acceptable.
---''lequal'': If less than or equal to, then acceptable.
---''greater'': If greater than, then acceptable.
---''notequal'': If not equal, then acceptable.
---''gequal'': If greater than or equal to, then acceptable.
---''always'': Always acceptable.
#br
You can choose from the above.

#br
#br
-&size(16){''Dither Translucent''};
--''Example''
 @//_DITHER_TRANSLUCENT(false)

--''Description''
Disable the use of dithered transparency in opaque path shaders.

#br
#br
-&size(16){''Skin''};
--''Example''
 @//_SKIN(false)

--''Description''
Declare that skinning (bone-based deformation) is not performed.
Include this declaration whenever possible to reduce shader generation processing load.

#br
#br
-&size(16){''Morph''};
--''Example''
 @//_MORPH(false)

--''Description''
Declare that no morphing (shape animation) will be performed.
Include this declaration whenever possible to reduce shader generation processing load.

#br
#br
-&size(16){''ZPrepass''};
--''Example''
 @//_ZPREPASS(false)

--''Description''
Disable rendering to the Z-prepass in the opaque path shader.

#br
#br
-&size(16){''Forward Fog''};
--''Example''
 @//_FORWARD_FOG(true)

--''Description''
Add fog effects to the rendering of translucent paths.

#br
#br
-&size(16){''Outline Pass''};
--''Example''
 @//_NO_OUTLINE_PASS(true)

--''Description''
Disable the generation of outline rendering shaders.

#br
#br
-&size(16){''Hidden Area Pass''};
--''Example''
 @//_NO_HIDDEN_AREA_PASS(true)

--''Description''
Disable shader generation for rendering player character parts hidden behind buildings.

#br
#br
-&size(16){''Cast Shadow''};
--''Example''
 @//_CAST_SHADOW(false)

--''Description''
Disable shadow rendering.


**Parameters (Uniform) [#v231a222]

This section explains the parameters that are passed from Bakin to a shader.


-&size(16){''SYSTEMCOMMONBLOCK''};

--''Description''
It contains information about the scene.
Data can be accessed with its variable name.
#br
 	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

#br
#br
-&size(16){''SYSTEMMATERIALBLOCK''};
#br
Material parameters assigned by the system.
Accessed by SVAL(name) macro.
 SVAL(emissionMultiply)
The above and more.
#br
 	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)

#br
#br
-&size(16){''LOCALLIGHTLISTBLOCK''};
#br
Local light information is stored in an array.
You can find the index of the array based on the localLightCount and localLightIndex in the SYSTEMMATERIALBLOCK.
#br
 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];

#br
#br
-&size(16){''USERMATERIAL''};
#br
A parameter declared in the shader, accessed using the UVAL macro.
 //DECL_COLOR3(color)
If you declare something like above,
 vec3 tmp = UVAL(color);
can be used.



**Compile Condition [#s7714dfd]

-&size(16){''Vertex Shader''};
FLAG_VP is defined during compilation.
#br
 #ifdef FLAG_VP
 /*Describe vertex shader*/
 #endif
#br
If you bundle as above, that part will be enabled when compiling vertex shader.
#br
#br
-&size(16){''Fragment Shader''};
FLAG_FP is defined during compilation.
#br
 #ifdef FLAG_FP
 /*Describe fragment shader*/
 #endif
If you bundle as above, that part will be enabled when compiling the fragment shader.
#br
#br
-&size(16){''Pass''};
Bakin creates several passages from a single shader.
#br
--''Main'' The pass for normal rendering.
--''Shadow'' The pass for rendering shadows.
--''Outline'' The pass for rendering outlines.
--''Pickup'' This is the pass used to test mouse hits in the editor.
--''HiddenArea'' The pass is used to render the area that will be highlighted when the player hides in the shadow of buildings.
#br
Each is defined at compilation time as follows:
#br
--Main : _MAIN_PASS_COMPILE
--Shadow : _SHADOW_PASS_COMPILE
--Outline : _OUTLINE_PASS_COMPILE
--Pickup : _PICKUP_PASS_COMPILE
--HiddenArea : _HIDDEN_AREA_PASS_COMPILE
#br
For example, the Shadow pass is as follows:
#br
 #ifdef _SHADOW_PASS_COMPILE
 /*Description for shadow pass case*/
 #endif
#br
You can describe the behavior of the Shadow pass by bundling it as above.
#br
''If a pass other than Main is not described, a common source will be used.''
''If you want to describe a pass, declare _CUSTOM_PASS and then describe the operation of the appropriate pass.''
#br
As an example of describing multiple passes, refer to the following function description method when describing the main pass and shadow pass separately in the vertex shader.
#br
 #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
#br

**User Main Functions [#f9edf0bb]

''The main functions of vertex shader and fragment shader should be declared with the name usermain.''

-&size(16){''Vertex Shader''};
Input is done with a structure called VertexInStruct and output is done with a structure called VertexoutStruct.

#br
 VertexOutStruct usermain(VertexInStruct vin)
 {
 	VertexOutStruct vout
 	vout.position = calcPositionWorldToScreen(calcPositionLocalToWorld(vin.position));
 	vout.texCoord0.xy = vin.uv0.xy;
 	return vout;
 }

#br
#br
-&size(16){''Fragment Shader''};
FragmentInStruct is used for input.
#br
For opaque rendering, returns PBRMaterialResult and the caller does the lighting.

#br
 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;
 }
#br
The members of PBRMaterialResult are as follows:
#br
--''baseColor''
--''normal''
--''roughness''
--''metallic''
--''specular''
--''emissiveColor''
--''worldPosition'': World coordinate system position
--''opacity'': Used for dither translucent processing
--''sssColor'': SSS color and applicability
#br
Otherwise, Unlit and Translucent return vec4 colors.
Example:
#br
 vec4 usermain(FragmentInStruct fin)
 {
 	return SAMPLING2D( DifMap, fin.texCoord0.xy ) * fin.color;
 }


**Function [#oceb0148]

-&size(16){''Matrix''};
For compatibility, please use the mul macro for matrix multiplication.

-&size(16){''Samplers''};
Samplers should use the following macros for compatibility.
#br
--''SAMPLING2D(name, uv)'': 2D texture sampling, uv is vec2
--''SAMPLING2DLOD(name, uv)'': LOD sampling of 2D textures, uv is vec3
--''SAMPLING3D(name, uvw)'': 3D texture sampling, uvw is vec3
--''SAMPLING3DLOD(name, uvw)'': LOD sampling of 3D textures, uvw is vec4
--''SAMPLINGCUBE(name, uv)'': Cube map sampling, uv is vec3	
--''SAMPLINGCUBELOD(name, uv)'': Cube map sampling, uv is vec4
#br
#br
-&size(16){''Vertex Shader Functions''};

--''World Position''
 vec3 calcPositionLocalToWorld(vec3 vtx)
--- ''Description''
Passing a world vertex position vtx returns vec3 converted to world coordinates using either a skeletal or static transformation matrix.

--''View Position''
 vec3 calcPositionWorldToView(vec3 vtx)
--- ''Description''
Passing a world vertex position vtx returns vec3 converted to the view coordinate system.

--''Screen Position''
 vec4 calcPositionWorldToScreen(vec3 vtx)
--- ''Description''
Passing a world vertex vtx returns vec4 in the clipping coordinate system, no division by w.

--''Screen Position 2''
 vec4 calcPositionViewToScreen(vec3 vtx)
--- ''Description''
Passing view vertex vtx returns vec4 in the clipping coordinate system, no division by w.

--''World Normal Calculation''
 vec3 calcNormalLocalToWorld(vec3 normal)
--- ''Description''
Passing a local normal will convert it to a world normal and return it.

--''World Tangent Calculation''
 vec4 calcTangentLocalToWorld(vec4 tangent)
--- ''Description''
Passing a local tangent is converted to a world tangent and return it.

--''Binormal Calculation''
 vec3 calcBinormal(vec3 normal, vec4 tangent)
--- ''Description''
Finds binormal from the outer product of the normal and tangent.

--''Outline Calculation''
 vec calcOutlineVertex(vec3 vtx, vec3 normal)
--- ''Description''
Returns the position of the vertex after translating it along the normal direction to reflect the material's outline width setting.

--''World Matrix''
 vec3 getLocalToWorldMatrix()
--- ''Description''
Returns the matrix for transforming a model's vertices from local coordinates to world coordinates.

--''Outline Width''
 @float getOutlineWidth()
--- ''Description''
Returns the width of the outline set in the material.
#br
#br

-&size(16){''Fragment Shader Functions''};

--''World Position''
 vec3 CalcWorldPosition()
--- ''Description''
Converts from the current fragment position to world coordinates. Some error will occur.

--''View Coordinate Position''
 vec3 CalcViewPosition()
--- ''Description''
Converts from the current fragment position to the coordinate system of the view (camera).

--''Directional Light Brightness''
 float directionalLightAttenuation(vec3 wposition)
--- ''Description''
Obtains the state of hiding by shadow of a directional light.

--''Local Light Brightness''
 float localLightAttenuation(int index, vec3 wposition)
--- ''Description''
Obtains the influence of a local light.

--''Number of Local Lights''
 int getLocalLightCount()
--- ''Description''
Returns the number of local lights.

--''Local Light Position''
 vec3 getLocalLightPosition(int index)
--- ''Description''
Returns the position of the index-th local light.

--''Local Light Color''
 getLocalLightColor(int index)
--- ''Description''
Returns the color of the index-th local light.

--''Fog Calculation''
 vec4 calcFog(vec3 wposition)
--- ''Description''
Calculates fog for a specified world position.

--''Environment Map''
 vec3 GetReflectionImg(vec3 wposition, vec3 normal, float level)
--- ''Description''
Returns the environment map at the specified position and normal.
Specify the MipMap level derived from roughness and other factors in level.
The maximum MipMap level for the environment map can be obtained via SCNVAL(reflectionMipCount).

--''IBL''
 vec3 GetIBL( vec3 normal, float level)
--- ''Description''
Returns the IBL for the specified normal direction. The IBL uses a portion of the environment map.
level specifies the MipMap level of the environment map (e.g., SCNVAL).

--''Outline Color''
 vec4 fpGetOutlineColor()
--- ''Description''
Returns the outline color considering linear lighting.

--''PBR Lighting''
 PBRLightingResult PbrSSSWithWorldPosition(vec3 albedo, vec3 normal, vec3 emissive, float roughness, float metallic, float specular, float opacity, vec4 subsurface, vec3 worldPosition, bool frontFacing)
--- ''Description''
Providing PBR parameters returns the lighting result.

--''Unlit''
 vec4 Unlit(vec3 emissive, float opacity, vec3 worldPosition)
--- ''Description''
Performs the necessary calculations for Unlit materials and returns the color.

--''Unlit with Reflection''
 vec4 UnlitWithReflection(vec3 albedo, vec3 normal, vec3 emissive, float roughness, float metallic, float opacity, vec3 worldPosition, bool frontFacing)
--- ''Description''
Returns a color that reflects the environment map without applying glass-like lighting effects.

--''Translucent''
 void Translucent(vec4 value, vec3 worldPosition)
--- ''Description''
Returns a color for translucent rendering, taking fog and other effects into account.

Front page   Edit Diff History Attach Copy Rename Reload   New Page list Search Recent changes   Help   RSS of recent changes