Model

internal constructor new Cesium.Model()

要构造 Model,请调用 Model.fromGltfAsync。不要直接调用构造函数。
A 3D model based on glTF, the runtime asset format for WebGL, OpenGL ES, and OpenGL.

Cesium supports glTF assets with the following extensions:

Note: for models with compressed textures using the KHR_texture_basisu extension, we recommend power of 2 textures in both dimensions for maximum compatibility. This is because some samplers require power of 2 textures (Using textures in WebGL) and KHR_texture_basisu requires multiple of 4 dimensions (KHR_texture_basisu additional requirements).

Demo:
See:

Members

当前正在播放的 glTF 动画。

backFaceCulling : boolean

是否剔除背面几何体。为 true 时,背面剔除由材质的 doubleSided 属性决定;为 false 时,禁用背面剔除。如果 Model#color 是半透明的,或 Model#silhouetteSize 大于 0.0,则不会剔除背面。
Default Value: true
获取模型在世界空间中的包围球。这不考虑 glTF 动画、skins 或 morph targets。它也不考虑 Model#minimumPixelSize

clampAnimations : boolean

决定在没有指定关键帧的帧上,模型的动画是否应保持一个姿态。
Default Value: true
获取模型的分类类型。这决定了地形、 3D Tiles 或两者是否会被此模型分类。

此外,还有一些要求/限制:

  • glTF 不能包含 morph targets、skins 或 animations。
  • glTF 不能包含 EXT_mesh_gpu_instancing 扩展。
  • 只有使用 TRIANGLES 的网格可用于对其他资源进行分类。
  • 网格必须是水密的(watertight)。
  • POSITION 属性是必需的。
  • 如果 feature IDs 和索引缓冲区同时存在,则具有相同 feature id 的所有索引必须占据索引缓冲区的连续区段。
  • 如果存在 feature IDs 但没有索引缓冲区,则具有相同 feature id 的所有位置必须占据位置缓冲区的连续区段。

接收分类的 3D Tiles 或地形必须是非透明的(opaque)。

Default Value: undefined
Experimental

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.

用于有选择地禁用模型渲染的 ClippingPlaneCollection
用于有选择地禁用模型渲染的 ClippingPolygonCollection
与模型渲染颜色混合的颜色。
Default Value: undefined

colorBlendAmount : number

colorBlendModeMIX 时,用于确定颜色强度的数值。值为 0.0 时呈现模型的渲染颜色,值为 1.0 时呈现纯色,介于两者之间的任何值都会产生两者的混合。
Default Value: 0.5
定义颜色如何与模型混合。
Default Value: ColorBlendMode.HIGHLIGHT
获取将为模型显示的 credit(版权信息)。
模型的自定义着色器(如果存在)。将自定义着色器与 Cesium3DTileStyle 一起使用可能会导致未定义行为。
Experimental

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.

debugShowBoundingVolume : boolean

此属性仅用于调试;不用于生产环境,也未经过优化。

绘制模型中每个绘制命令的包围球。

Default Value: false

debugWireframe : boolean

此属性仅用于调试;不用于生产环境,也未经过优化。

以线框模式绘制模型。

Default Value: false
获取或设置距离显示条件,指定模型将在距相机多远处显示。
Default Value: undefined
控制来自 EXT_mesh_primitive_edge_visibility glTF 扩展的边相对于表面几何体的渲染方式。未声明该扩展的图元不受此设置影响。
Default Value: EdgeDisplayMode.SURFACES_ONLY
Experimental

This feature is using part of the glTF spec that is not yet final and is subject to change without Cesium's standard deprecation policy.

enableVerticalExaggeration : boolean

如果为 true,当 Scene.verticalExaggeration 被设置为 1.0 以外的值时,模型会沿椭球法线方向被放大。
Default Value: true
Example:
// Exaggerate terrain by a factor of 2, but prevent model exaggeration
scene.verticalExaggeration = 2.0;
model.enableVerticalExaggeration = false;
用于管理此模型上动态环境贴图的属性。影响光照。
Example:
// Change the ground color used for a model's environment map to a forest green
const environmentMapManager = model.environmentMapManager;
environmentMapManager.groundColor = Cesium.Color.fromCssColorString("#203b34");
获取当模型遇到异步渲染错误时触发的事件。通过订阅该事件,你将收到错误通知,并有可能从中恢复。事件监听器会收到一个 ModelError 实例。

featureIdLabel : string

用于拾取和样式设置的 feature ID 集合的标签。

对于 EXT_mesh_features,这是 feature ID 的 label 属性;若未指定,则为 "featureId_N"(其中 N 为 featureIds 数组中的索引)。EXT_feature_metadata 没有 label 字段,因此 此类 feature ID 集合总是标记为 "featureId_N",其中 N 为 所有 feature Id 列表中的索引,feature ID 属性排在 feature ID 纹理之前。

如果 featureIdLabel 被设为整数 N,它会自动转换为 字符串 "featureId_N"。如果同时存在逐图元(per-primitive)和逐实例(per-instance)的 feature IDs,则实例 feature IDs 优先。

Experimental

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.

模型的高度参考,决定模型相对于地形的绘制方式。
Default Value: {HeightReference.NONE}
当模型被拾取时返回的用户自定义对象。
Default Value: undefined
See:
用于管理此模型上基于图像的照明的属性。

instanceFeatureIdLabel : string

用于拾取和样式设置的实例 feature ID 集合的标签。

如果 instanceFeatureIdLabel 被设为整数 N,它会自动转换为 字符串 "instanceFeatureId_N"。如果同时存在逐图元和逐实例的 feature IDs,则 实例 feature IDs 优先。

Experimental

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.

对模型着色时的方向光颜色。当为 undefined 时,改为使用场景的光照颜色。

通过将 model.imageBasedLighting.imageBasedLightingFactor = new Cartesian2(0.0, 0.0) 设为该值来禁用额外光源会使模型变暗很多。此时,提高光源强度会使模型变亮。

Default Value: undefined
模型的最大缩放尺寸。这可用于为 Model#minimumPixelSize 设定上限,确保模型的缩放尺度始终合理。

minimumPixelSize : number

模型近似的最小像素尺寸,与缩放无关。 这可用于确保即使 viewer 缩小,模型也可见。当为 0.0 时,不强制最小尺寸。
Default Value: 0.0
将模型从模型坐标变换到世界坐标的 4x4 变换矩阵。 当其为单位矩阵时,模型在世界坐标(即地球的 Cartesian WGS84 坐标)中绘制。通过提供不同的变换矩阵(例如 Transforms.eastNorthUpToFixedFrame 返回的矩阵)可以使用局部参考系。
Default Value: Matrix4.IDENTITY
Example:
const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
m.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
渲染轮廓时使用的颜色。
Default Value: Color.BLACK
用于控制点云衰减和光照的点云着色设置。 对于 3D Tiles,此设置继承自 Cesium3DTileset

readonly ready : boolean

true 时,此模型已准备好渲染,即外部二进制、图像 和着色器文件已下载,且 WebGL 资源已创建。
Default Value: false
获取当模型加载完成并准备好渲染时(即外部资源已下载且 WebGL 资源已创建)触发的事件。事件监听器会收到一个 Model 实例。

如果 Model.incrementallyLoadTextures 为 true,则在所有纹理加载并准备好渲染之前就会触发此事件。订阅 Model.texturesReadyEvent 可在纹理准备好时收到通知。

Model#modelMatrix 之前应用于此模型的均匀缩放。 大于 1.0 的值会增大模型尺寸;小于 1.0 的值会减小。
Default Value: 1.0
决定模型是否投射或接收来自光源的阴影。
Default Value: ShadowMode.ENABLED
是否渲染此模型。
Default Value: true

showCreditsOnScreen : boolean

获取或设置模型的 credits(版权信息)是否显示在屏幕上。
Default Value: false
是否显示使用 CESIUM_primitive_outline 扩展的模型的轮廓。为 true 时显示轮廓,为 false 时不显示。
Default Value: true
轮廓(silhouette)的颜色。
Default Value: Color.RED

silhouetteSize : number

轮廓的像素大小。
Default Value: 0.0
要应用于此模型的 SplitDirection
Default Value: SplitDirection.NONE
要应用于模型中要素的样式。如果同时应用了 CustomShader,则无法应用样式。

readonly texturesReadyEvent : Event

获取一个事件:如果 Model.incrementallyLoadTextures 为 true,则当模型纹理加载完成并准备好渲染时(即外部资源已下载且 WebGL 资源已创建)触发。事件监听器会收到一个 Model 实例。

Methods

static Cesium.Model.fromGltfAsync(options)Promise.<Model>

异步地从 glTF 资源创建模型。该函数返回一个 promise,在模型准备好渲染时(即外部二进制、图像 和着色器文件已下载且 WebGL 资源已创建)resolve。

该模型可以是带有 .gltf 扩展名的传统 glTF 资源,也可以是使用 .glb 扩展名的二进制 glTF。

Name Type Description
options object Object with the following properties:
Name Type Default Description
url string | Resource 指向 .gltf 或 .glb 文件的 url。
basePath string | Resource '' optional glTF JSON 中路径所相对的基础路径。
show boolean true optional 是否渲染此模型。
modelMatrix Matrix4 Matrix4.IDENTITY optional 将模型从模型坐标变换到世界坐标的 4x4 变换矩阵。
scale number 1.0 optional 应用于此模型的均匀缩放。
enableVerticalExaggeration boolean true optional 如果为 true,当 Scene.verticalExaggeration 被设置为 1.0 以外的值时,模型会沿椭球法线方向被放大。
minimumPixelSize number 0.0 optional 模型近似的最小像素尺寸,与缩放无关。
maximumScale number optional 模型的最大缩放尺寸。minimumPixelSize 的上限。
id object optional 当模型被 Scene#pick 拾取时返回的用户自定义对象。
allowPicking boolean true optionaltrue 时,每个图元都可通过 Scene#pick 拾取。
incrementallyLoadTextures boolean true optional 确定纹理在模型加载后是否可继续流入。
asynchronous boolean true optional 决定模型的 WebGL 资源创建是分散到若干帧进行,还是在全部 glTF 文件加载完成后阻塞直到完成。
clampAnimations boolean true optional 决定在没有指定关键帧的帧上,模型的动画是否应保持一个姿态。
shadows ShadowMode ShadowMode.ENABLED optional 决定模型是否投射或接收来自光源的阴影。
releaseGltfJson boolean false optional 为 true 时,glTF 加载完成后即释放 glTF JSON。这对于 3D Tiles 等场景特别有用,因为每个 .gltf 模型都是唯一的,缓存 glTF JSON 无效。
debugShowBoundingVolume boolean false optional 仅用于调试。绘制模型中每个绘制命令的包围球。
enableDebugWireframe boolean false optional 仅用于调试。要让 debugWireframe 在 WebGL1 中生效,必须将其设为 true。此属性在模型加载后无法再设置。
debugWireframe boolean false optional 仅用于调试。以线框模式绘制模型。仅在 enableDebugWireframe 设为 true 时,才能在 WebGL1 中生效。
cull boolean true optional 是否使用视锥/地平线剔除来剔除模型。如果模型是 3D Tiles 瓦片集的一部分,此属性始终为 false,因为会使用 3D Tiles 的剔除系统。
opaquePass boolean Pass.OPAQUE optionalDrawCommand 中用于模型不透明部分的 pass。
upAxis Axis Axis.Y optional glTF 模型的向上轴。
forwardAxis Axis Axis.Z optional glTF 模型的前向轴。
customShader CustomShader optional 一个自定义着色器。这会向顶点和片元着色器添加用户自定义的 GLSL 代码。将自定义着色器与 Cesium3DTileStyle 一起使用可能会导致未定义行为。
content Cesium3DTileContent optional 此模型所属的瓦片内容。如果模型不是作为瓦片集的一部分加载的,此属性为 undefined。
heightReference HeightReference HeightReference.NONE optional 决定模型相对于地形的绘制方式。
scene Scene optional 对于使用高度参考属性的模型,必须传入此参数。
distanceDisplayCondition DistanceDisplayCondition optional 指定模型将在距相机多远处显示的条件。
color Color optional 与模型渲染颜色混合的颜色。
colorBlendMode ColorBlendMode ColorBlendMode.HIGHLIGHT optional 定义颜色如何与模型混合。
colorBlendAmount number 0.5 optionalcolorBlendModeMIX 时,用于确定颜色强度的数值。值为 0.0 时呈现模型的渲染颜色,值为 1.0 时呈现纯色,介于两者之间的任何值都会产生两者的混合。
edgeDisplayMode EdgeDisplayMode EdgeDisplayMode.SURFACES_ONLY optional 控制来自 EXT_mesh_primitive_edge_visibility 扩展的边相对于表面几何体的渲染方式。
silhouetteColor Color Color.RED optional 轮廓的颜色。如果超过 256 个模型启用了轮廓,重叠的模型可能会出现轻微的伪影。
silhouetteSize number 0.0 optional 轮廓的像素大小。
enableShowOutline boolean true optional 是否对使用 CESIUM_primitive_outline 扩展的模型启用轮廓。可将其设为 false 以避免在加载时进行几何后处理。为 false 时,showOutlines 和 outlineColor 选项将被忽略。
showOutline boolean true optional 是否显示使用 CESIUM_primitive_outline 扩展的模型的轮廓。为 true 时显示轮廓,为 false 时不显示。
outlineColor Color Color.BLACK optional 渲染轮廓时使用的颜色。
clippingPlanes ClippingPlaneCollection optional 用于有选择地禁用模型渲染的 ClippingPlaneCollection
clippingPolygons ClippingPolygonCollection optional 用于有选择地禁用模型渲染的 ClippingPolygonCollection
lightColor Cartesian3 optional 对模型着色时的光照颜色。当为 undefined 时,改为使用场景的光照颜色。
imageBasedLighting ImageBasedLighting optional 用于管理此模型上基于图像的照明的属性。
environmentMapOptions DynamicEnvironmentMapManager.ConstructorOptions optional 用于管理此模型上动态环境贴图的属性。
backFaceCulling boolean true optional 是否剔除背面几何体。为 true 时,背面剔除由材质的 doubleSided 属性决定;为 false 时,禁用背面剔除。如果模型的颜色是半透明的,则不会剔除背面。
credit Credit | string optional 数据源的 credit(版权信息),显示在画布上。
showCreditsOnScreen boolean false optional 是否在屏幕上显示此模型的 credits(版权信息)。
splitDirection SplitDirection SplitDirection.NONE optional 要应用于此模型的 SplitDirection 拆分。
projectTo2D boolean false optional 是否在 2D 中精确投影模型的位置。如果为 true,模型将被精确投影到 2D,但会占用更多内存。如果为 false,模型会占用更少内存,并且仍可在 2D / CV 模式下渲染,但其位置可能不准确。这会禁用 minimumPixelSize,并阻止后续对模型矩阵进行修改。此属性在模型加载后也无法再设置。
enablePick boolean false optional 在未使用 WebGL 2 或更高版本时,是否允许通过 pick 进行 CPU 拾取。如果使用 WebGL 2 或更高版本,此选项将被忽略。如果使用 WebGL 1 且此选项为 true,pick 操作将正常工作,但会占用更多内存。如果在 WebGL 1 下此选项为 false,模型会占用更少内存,但 pick 将始终返回 undefined。此属性在模型加载后无法再设置。
featureIdLabel string | number "featureId_0" optional 用于拾取和样式设置的 feature ID 集合的标签。对于 EXT_mesh_features,这是 feature ID 的 label 属性;若未指定,则为 "featureId_N"(其中 N 为 featureIds 数组中的索引)。EXT_feature_metadata 没有 label 字段,因此此类 feature ID 集合总是标记为 "featureId_N",其中 N 为所有 feature Id 列表中的索引,feature ID 属性排在 feature ID 纹理之前。如果 featureIdLabel 为整数 N,它会自动转换为字符串 "featureId_N"。如果同时存在逐图元和逐实例的 feature IDs,则实例 feature IDs 优先。
instanceFeatureIdLabel string | number "instanceFeatureId_0" optional 用于拾取和样式设置的实例 feature ID 集合的标签。如果 instanceFeatureIdLabel 为整数 N,它会自动转换为字符串 "instanceFeatureId_N"。如果同时存在逐图元和逐实例的 feature IDs,则实例 feature IDs 优先。
pointCloudShading object optional 用于构造 PointCloudShading 对象以控制点云衰减和光照的选项。
classificationType ClassificationType optional 决定地形、3D Tiles 或两者是否会被此模型分类。此属性在模型加载后无法再设置。
gltfCallback Model.GltfCallback optional 在加载完成后,使用已加载的 gltf 对象调用的函数。
Returns:
一个 promise,在创建的模型准备好渲染时 resolve(解析)为模型本身。
Throws:
Examples:
// Load a model and add it to the scene
try {
 const model = await Cesium.Model.fromGltfAsync({
   url: "../../SampleData/models/CesiumMan/Cesium_Man.glb"
 });
 viewer.scene.primitives.add(model);
} catch (error) {
 console.log(`Failed to load model. ${error}`);
}
// Position a model with modelMatrix and display it with a minimum size of 128 pixels
const position = Cesium.Cartesian3.fromDegrees(
  -123.0744619,
  44.0503706,
  5000.0
);
const headingPositionRoll = new Cesium.HeadingPitchRoll();
const fixedFrameTransform = Cesium.Transforms.localFrameToFixedFrameGenerator(
  "north",
  "west"
);
try {
 const model = await Cesium.Model.fromGltfAsync({
   url: "../../SampleData/models/CesiumAir/Cesium_Air.glb",
   modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
     position,
     headingPositionRoll,
     Cesium.Ellipsoid.WGS84,
     fixedFrameTransform
   ),
   minimumPixelSize: 128,
 });
 viewer.scene.primitives.add(model);
} catch (error) {
 console.log(`Failed to load model. ${error}`);
}
// Load a model and play the last animation at half speed
let animations;
try {
 const model = await Cesium.Model.fromGltfAsync({
   url: "../../SampleData/models/CesiumMan/Cesium_Man.glb",
   gltfCallback: gltf => {
     animations = gltf.animations
   }
 });
 viewer.scene.primitives.add(model);
 model.readyEvent.addEventListener(() => {
   model.activeAnimations.add({
     index: animations.length - 1,
     loop: Cesium.ModelAnimationLoop.REPEAT,
     multiplier: 0.5,
   });
 });
} catch (error) {
 console.log(`Failed to load model. ${error}`);
}
将任意已修改的关节(articulation)阶段应用到 参与任何关节的每个节点的矩阵上。注意,这会覆盖参与节点上的 任意节点变换。
Throws:
  • DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 变为 true。
销毁此对象持有的 WebGL 资源。销毁对象可以实现 WebGL 资源的确定性 释放,而不是依赖垃圾回收器来销毁此对象。

对象一旦被销毁,就不应再使用;调用除 isDestroyed 以外的任何函数都会导致 DeveloperError 异常。因此, 应像示例中那样将返回值(undefined)赋给该对象。
Throws:
Example:
model = model && model.destroy();
See:

getExtension(extensionName)object|undefined

返回为给定扩展创建的对象。 给定的名称可以是 glTF 扩展的名称,例如 `"EXT_example_extension"`。 如果指定的扩展存在于底层 glTF 资源的根中, 并且该扩展的加载器已处理其扩展数据,则 此方法将返回该扩展的模型表示。
Name Type Description
extensionName string 扩展的名称
Returns:
该对象;如果不存在则为 `undefined`
Throws:
  • DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 变为 true。
Experimental

This feature is not final and is subject to change without Cesium's standard deprecation policy.

返回 glTF 中具有给定 name 的节点。这用于 为用户自定义动画修改节点的变换。
Name Type Description
name string glTF 中节点的名称。
Returns:
该节点;如果不存在具有该 name 的节点,则为 undefined
Throws:
  • DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 变为 true。
Example:
// Apply non-uniform scale to node "Hand"
const node = model.getNode("Hand");
node.matrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(5.0, 1.0, 1.0), node.matrix);

isDestroyed()boolean

如果此对象已被销毁则返回 true;否则返回 false。

如果此对象已被销毁,则不应再使用它;调用除 isDestroyed 以外的任何函数都会导致 DeveloperError 异常。
Returns:
如果此对象已被销毁则为 true;否则为 false
See:
将模型的 Model#style 标记为脏(dirty),这会强制所有要素 在模型可见的下一帧重新评估样式。

setArticulationStage(articulationStageKey, value)

设置某个关节(articulation)阶段的当前值。在设置一个或多个 阶段值之后,调用 Model.applyArticulations() 以 使节点矩阵重新计算。
Name Type Description
articulationStageKey string 关节名称、一个空格以及阶段的名称。
value number 此关节阶段的数值。
Throws:
  • DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 变为 true。
Example:
// Sets the value of the stage named "MoveX" belonging to the articulation named "SampleArticulation"
model.setArticulationStage("SampleArticulation MoveX", 50.0);
See:
ViewerCesiumWidget 渲染场景时调用,以 获取渲染此图元所需的绘制命令。

不要直接调用此函数。这里对其进行说明只是为了 列出场景渲染时可能传播出的异常:

Throws:

Type Definitions

Cesium.Model.GltfCallback(gltf)

在加载完成后,使用已加载的 gltf 对象调用的函数的接口。
Name Type Description
gltf object gltf 对象
需要帮助?获取答案的最快方式是在 Cesium 论坛 上向社区和团队提问。