Skip to content

引擎内置的ShaderAPI参考

类型

虽然可以直接使用GLSL类型(vec4或mat4),但我们建议使用以下类型别名:

NameGLSL typeDescription
bool2bvec2A vector of 2 booleans
bool3bvec3A vector of 3 booleans
bool4bvec4A vector of 4 booleans
int2ivec2A vector of 2 integers
int3ivec3A vector of 3 integers
int4ivec4A vector of 4 integers
uint2uvec2A vector of 2 unsigned integers
uint3uvec3A vector of 3 unsigned integers
uint4uvec4A vector of 4 unsigned integers
float2float2A vector of 2 floats
float3float3A vector of 3 floats
float4float4A vector of 4 floats
float4x4mat4A 4x4 float matrix
float3x3mat3A 3x3 float matrix

数学相关

NameTypeDescription
PIfloatA constant that represent $\pi$
HALF_PIfloatA constant that represent $\frac{\pi}{2}$
saturate(float x)floatClamps the specified value between 0.0 and 1.0
pow5(float x)floatComputes $x^5$
sq(float x)floatComputes $x^2$
max3(float3 v)floatReturns the maximum value of the specified float3
mulMat4x4Float3(float4x4 m, float3 v)float4Returns $m * v$
mulMat3x3Float3(float4x4 m, float3 v)float4Returns $m * v$

矩阵相关

NameTypeDescription
getViewFromWorldMatrix()float4x4Matrix that converts from world space to view/eye space
getWorldFromViewMatrix()float4x4Matrix that converts from view/eye space to world space
getClipFromViewMatrix()float4x4Matrix that converts from view/eye space to clip (NDC) space
getViewFromClipMatrix()float4x4Matrix that converts from clip (NDC) space to view/eye space
getClipFromWorldMatrix()float4x4Matrix that converts from world to clip (NDC) space
getWorldFromClipMatrix()float4x4Matrix that converts from clip

帧数据常量

NameTypeDescription
getResolution()float4Dimensions of the view's effective (physical) viewport in pixels: width, height, 1 / width, 1 / height. This might be different from View::getViewport() for instance because of added rendering guard-bands.
getWorldCameraPosition()float3Position of the camera/eye in world space (see note below)
getWorldOffset()float3[deprecated] The shift required to obtain API-level world space. Use getUserWorldPosition() instead
getUserWorldFromWorldMatrix()float4x4Matrix that converts from world space to API-level (user) world space.
getTime()floatCurrent time as a remainder of 1 second. Yields a value between 0 and 1
getUserTime()float4Current time in seconds: time, (double)time - time, 0, 0
getUserTimeMod(float m)floatCurrent time modulo m in seconds
getExposure()floatPhotometric exposure of the camera
getEV100()floatExposure value at ISO 100 of the camera

仅顶点着色器可访问

NameTypeDescription
getPosition()float4Vertex position in the domain defined by the material (default: object/model space)
getCustom0() to getCustom7()float4Custom vertex attribute
getWorldFromModelMatrix()float4x4Matrix that converts from model (object) space to world space
getWorldFromModelNormalMatrix()float3x3Matrix that converts normals from model (object) space to world space
getVertexIndex()intIndex of the current vertex

仅像素着色器可访问

NameTypeDescription
getWorldTangentFrame()float3x3Matrix containing in each column the tangent (frame[0]), bi-tangent (frame[1]) and normal (frame[2]) of the vertex in world space. If the material does not compute a tangent space normal for bump mapping or if the shading is not anisotropic, only the normal is valid in this matrix.
getWorldPosition()float3Position of the fragment in world space (see note below about world-space)
getUserWorldPosition()float3Position of the fragment in API-level (user) world-space (see note below about world-space)
getWorldViewVector()float3Normalized vector in world space from the fragment position to the eye
getWorldNormalVector()float3Normalized normal in world space, after bump mapping (must be used after prepareMaterial())
getWorldGeometricNormalVector()float3Normalized normal in world space, before bump mapping (can be used before prepareMaterial())
getWorldReflectedVector()float3Reflection of the view vector about the normal (must be used after prepareMaterial())
getNormalizedViewportCoord()float3Normalized user viewport position (i.e. NDC coordinates normalized to [0, 1] for the position, [1, 0] for the depth), can be used before prepareMaterial()). Because the user viewport is smaller than the actual physical viewport, these coordinates can be negative or superior to 1 in the non-visible area of the physical viewport.
getNdotV()floatThe result of dot(normal, view), always strictly greater than 0 (must be used after prepareMaterial())
getColor()float4Interpolated color of the fragment, if the color attribute is required
getUV0()float2First interpolated set of UV coordinates, only available if the uv0 attribute is required
getUV1()float2First interpolated set of UV coordinates, only available if the uv1 attribute is required
getMaskThreshold()floatReturns the mask threshold, only available when blending is set to masked
inverseTonemap(float3)float3Applies the inverse tone mapping operator to the specified linear sRGB color and returns a linear sRGB color. This operation may be an approximation and works best with the "Filmic" tone mapping operator
inverseTonemapSRGB(float3)float3Applies the inverse tone mapping operator to the specified non-linear sRGB color and returns a linear sRGB color. This operation may be an approximation and works best with the "Filmic" tone mapping operator
luminance(float3)floatComputes the luminance of the specified linear sRGB color
ycbcrToRgb(float, float2)float3Converts a luminance and CbCr pair to a sRGB color
uvToRenderTargetUV(float2)float2Transforms a UV coordinate to allow sampling from a RenderTarget attachment