English中文

CustomShader

new Cesium.CustomShader(options)

用户定义的 GLSL 着色器与 A user defined GLSL shader used with Model 以及 as well as Cesium3DTileset.

如果使用纹理统一,则必须进行额外的资源管理: If texture uniforms are used, additional resource management must be done:

  • The update 必须每帧调用函数。当自定义着色器传递给 function must be called each frame. When a custom shader is passed to a Model 或一个 or a Cesium3DTileset,这一步是自动处理的 , this step is handled automaticaly
  • CustomShader#destroy 当不再需要自定义着色器时必须调用以正确清理 GPU 资源。应用程序负责调用此方法。 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.

请参阅 See the Custom Shader Guide 以获得更详细的文档。 for more detailed documentation.

名称 Name 类型 Type 说明 Description
options object 具有以下选项的对象 An object with the following options
名称 Name 类型 Type 默认值 Default 说明 Description
mode CustomShaderMode CustomShaderMode.MODIFY_MATERIAL 可选 optional 自定义着色器模式,决定自定义着色器代码如何插入到片段着色器中。 The custom shader mode, which determines how the custom shader code is inserted into the fragment shader.
lightingModel LightingModel 可选 optional 光照模型(例如 PBR 或未光照)。如果存在,它将覆盖模型的默认照明。 The lighting model (e.g. PBR or unlit). If present, this overrides the default lighting for the model.
translucencyMode CustomShaderTranslucencyMode CustomShaderTranslucencyMode.INHERIT 可选 optional 半透明模式,决定如何应用自定义着色器。如果值为 CustomShaderTransulcencyMode.OPAQUE 或 CustomShaderTransulcencyMode.TRANSLUCENT,自定义着色器将覆盖模型材质的设置。如果值为 CustomShaderTransulcencyMode.INHERIT,则自定义着色器将根据图元的材质设置渲染为不透明或半透明。 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.
uniforms Object.<string, UniformSpecifier> 可选 optional 用户定义制服的字典。关键是 GLSL 代码中将出现的统一名称。 value是一个对象,描述统一类型和初始值 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
varyings Object.<string, VaryingType> 可选 optional 用于声明着色器中使用的附加 GLSL 变量的字典。关键是 GLSL 代码中出现的不同名称。该值是变化的数据类型。对于每个变化,声明将自动添加到着色器的顶部。调用者负责在顶点着色器中分配一个值并在片段着色器中使用该值。 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.
vertexShaderText string 可选 optional 作为 GLSL 代码字符串的自定义顶点着色器。它必须包含一个名为 vertexMain 的 GLSL 函数。请参阅预期签名的示例。如果未指定,则在计算顶点着色器中将跳过自定义顶点着色器步骤。 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.
fragmentShaderText string 可选 optional 作为 GLSL 代码字符串的自定义片段着色器。它必须包含一个名为fragmentMain 的GLSL 函数。请参阅预期签名的示例。如果未指定,则在计算片段着色器中将跳过自定义片段着色器步骤。 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.
示例: Example:
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;
  }
  `
});
实验性 Experimental

此功能使用的是 3D Tiles 规范的一部分,该规范不是最终版本,并且可能会在没有 Cesium 的标准弃用政策的情况下进行更改。 This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.

成员 Members

readonly fragmentShaderText : string

片段着色器的用户定义 GLSL 代码 The user-defined GLSL code for the fragment shader
使用自定义着色器时要使用的光照模型。这是由 The lighting model to use when using the custom shader. This is used by CustomShaderPipelineStage
确定自定义着色器如何与整体片段着色器交互的值。这是由 A value determining how the custom shader interacts with the overall fragment shader. This is used by CustomShaderPipelineStage
半透明模式,决定如何应用自定义着色器。如果值为 CustomShaderTransulcencyMode.OPAQUE 或 CustomShaderTransulcencyMode.TRANSLUCENT,自定义着色器将覆盖模型材质的设置。如果值为 CustomShaderTransulcencyMode.INHERIT,则自定义着色器将根据图元的材质设置渲染为不透明或半透明。 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.
默认值: Default Value: CustomShaderTranslucencyMode.INHERIT
用户声明的其他制服。 Additional uniforms as declared by the user.

readonly varyings : Object.<string, VaryingType>

用户声明的其他变化。这是由 Additional varyings as declared by the user. This is used by CustomShaderPipelineStage

readonly vertexShaderText : string

顶点着色器的用户定义 GLSL 代码 The user-defined GLSL code for the vertex shader

方法 Methods

销毁该对象持有的 WebGL 资源。销毁对象可以确定性地释放 WebGL 资源,而不是依赖垃圾收集器来销毁该对象。 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.

一旦对象被销毁,就不应再使用;调用除 Once an object is destroyed, it should not be used; calling any function other than isDestroyed 将导致 will result in a DeveloperError 例外。因此,分配返回值( exception. Therefore, assign the return value (undefined) 到对象,如示例中所做的那样。 ) to the object as done in the example.
抛出: Throws:
  • DeveloperError : This object was destroyed, i.e., destroy() was called.
示例: Example:
customShader = customShader && customShader.destroy();
另见: See:
如果该对象被销毁则返回 true;否则为假。 Returns true if this object was destroyed; otherwise, false.

如果该对象被破坏,则不应使用它;调用除 If this object was destroyed, it should not be used; calling any function other than isDestroyed 将导致 will result in a DeveloperError exception.
返回: Returns:
如果该对象被销毁,则为 True;否则为假。 True if this object was destroyed; otherwise, false.
另见: See:

setUniform(uniformName, value)

更新着色器中声明的统一值 Update the value of a uniform declared in the shader
名称 Name 类型 Type 说明 Description
uniformName string 制服的 GLSL 名称。这必须与构造函数中声明的制服之一匹配 The GLSL name of the uniform. This must match one of the uniforms declared in the constructor
value boolean | number | Cartesian2 | Cartesian3 | Cartesian4 | Matrix2 | Matrix3 | Matrix4 | string | Resource | TextureUniform 制服的新价值。 The new value of the uniform.
需要帮助吗?获得答案的最快方法是从社区和团队那里获得答案 Need help? The fastest way to get answers is from the community and team on the Cesium Forum.