Appearance
KBE3D / KBCore / Cesium / CustomShader
类: CustomShader
A user defined GLSL shader used with Model as well as Cesium3DTileset. <p> If texture uniforms are used, additional resource management must be done: </p> <ul> <li> The <code>update</code> function must be called each frame. When a custom shader is passed to a Model or a Cesium3DTileset, this step is handled automaticaly </li> <li> CustomShader#destroy must be called when the custom shader is no longer needed to clean up GPU resources properly. The application is responsible for calling this method. </li> </ul> <p> See the Custom Shader Guide for more detailed documentation. </p>
示例
ts
const customShader = new CustomShader({
uniforms: {
u_colorIndex: {
type: Cesium.UniformType.FLOAT,
value: 1.0
},
u_normalMap: {
type: Cesium.UniformType.SAMPLER_2D,
value: new Cesium.TextureUniform({
url: "http://example.com/normal.png"
})
}
},
varyings: {
v_selectedColor: Cesium.VaryingType.VEC3
},
vertexShaderText: `
void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput) {
v_selectedColor = mix(vsInput.attributes.color_0, vsInput.attributes.color_1, u_colorIndex);
vsOutput.positionMC += 0.1 * vsInput.attributes.normal;
}
`,
fragmentShaderText: `
void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
material.normal = texture(u_normalMap, fsInput.attributes.texCoord_0);
material.diffuse = v_selectedColor;
}
`
});参数
An object with the following options
参数
The custom shader mode, which determines how the custom shader code is inserted into the fragment shader.
参数
The lighting model (e.g. PBR or unlit). If present, this overrides the default lighting for the model.
参数
The translucency mode, which determines how the custom shader will be applied. If the value is CustomShaderTransulcencyMode.OPAQUE or CustomShaderTransulcencyMode.TRANSLUCENT, the custom shader will override settings from the model's material. If the value is CustomShaderTransulcencyMode.INHERIT, the custom shader will render as either opaque or translucent depending on the primitive's material settings.
参数
A dictionary for user-defined uniforms. The key is the uniform name that will appear in the GLSL code. The value is an object that describes the uniform type and initial value
参数
A dictionary for declaring additional GLSL varyings used in the shader. The key is the varying name that will appear in the GLSL code. The value is the data type of the varying. For each varying, the declaration will be added to the top of the shader automatically. The caller is responsible for assigning a value in the vertex shader and using the value in the fragment shader.
参数
The custom vertex shader as a string of GLSL code. It must include a GLSL function called vertexMain. See the example for the expected signature. If not specified, the custom vertex shader step will be skipped in the computed vertex shader.
参数
The custom fragment shader as a string of GLSL code. It must include a GLSL function called fragmentMain. See the example for the expected signature. If not specified, the custom fragment shader step will be skipped in the computed fragment shader.
属性
mode
readonlymode:CustomShaderMode
A value determining how the custom shader interacts with the overall fragment shader. This is used by CustomShaderPipelineStage
lightingModel
readonlylightingModel:LightingModel
The lighting model to use when using the custom shader. This is used by CustomShaderPipelineStage
uniforms
readonlyuniforms: {[key:string]:UniformSpecifier; }
Additional uniforms as declared by the user.
索引签名
[key: string]: UniformSpecifier
varyings
readonlyvaryings: {[key:string]:VaryingType; }
Additional varyings as declared by the user. This is used by CustomShaderPipelineStage
索引签名
[key: string]: VaryingType
vertexShaderText
readonlyvertexShaderText:string
The user-defined GLSL code for the vertex shader
fragmentShaderText
readonlyfragmentShaderText:string
The user-defined GLSL code for the fragment shader
translucencyMode
readonlytranslucencyMode:CustomShaderTranslucencyMode
The translucency mode, which determines how the custom shader will be applied. If the value is CustomShaderTransulcencyMode.OPAQUE or CustomShaderTransulcencyMode.TRANSLUCENT, the custom shader will override settings from the model's material. If the value isCustomShaderTransulcencyMode.INHERIT, the custom shader will render as either opaque or translucent depending on the primitive's material settings.
方法
setUniform()
setUniform(
uniformName:string,value:string|number|boolean|Cartesian3|Matrix4|Cartesian2|Cartesian4|Resource|Matrix3|Matrix2|TextureUniform):void
Update the value of a uniform declared in the shader
参数
uniformName
string
The GLSL name of the uniform. This must match one of the uniforms declared in the constructor
value
The new value of the uniform.
string | number | boolean | Cartesian3 | Matrix4 | Cartesian2 | Cartesian4 | Resource | Matrix3 | Matrix2 | TextureUniform
返回
void
isDestroyed()
isDestroyed():
boolean
Returns true if this object was destroyed; otherwise, false. <br /><br /> If this object was destroyed, it should not be used; calling any function other than <code>isDestroyed</code> will result in a DeveloperError exception.
返回
boolean
True if this object was destroyed; otherwise, false.
destroy()
destroy():
void
Destroys the WebGL resources held by this object. Destroying an object allows for deterministic release of WebGL resources, instead of relying on the garbage collector to destroy this object. <br /><br /> Once an object is destroyed, it should not be used; calling any function other than <code>isDestroyed</code> will result in a DeveloperError exception. Therefore, assign the return value (<code>undefined</code>) to the object as done in the example.
返回
void
示例
ts
customShader = customShader && customShader.destroy();构造函数
构造函数
new CustomShader(
options: {mode?:CustomShaderMode;lightingModel?:LightingModel;translucencyMode?:CustomShaderTranslucencyMode;uniforms?: {[key:string]:UniformSpecifier; };varyings?: {[key:string]:VaryingType; };vertexShaderText?:string;fragmentShaderText?:string; }):CustomShader
参数
options
mode?
lightingModel?
translucencyMode?
uniforms?
{[key: string]: UniformSpecifier; }
varyings?
{[key: string]: VaryingType; }
vertexShaderText?
string
fragmentShaderText?
string
返回
CustomShader
