Model.fromGltfAsync。不要直接调用构造函数。
Cesium 支持带有以下扩展的 glTF 资产:
- AGI_articulations
- CESIUM_primitive_outline
- CESIUM_RTC
- EXT_instance_features
- EXT_mesh_features
- EXT_mesh_gpu_instancing
- EXT_mesh_primitive_restart
- EXT_meshopt_compression
- EXT_structural_metadata
- EXT_texture_webp
- KHR_draco_mesh_compression
- KHR_techniques_webgl
- KHR_materials_common
- KHR_materials_pbrSpecularGlossiness
- KHR_materials_unlit
- KHR_mesh_quantization
- KHR_texture_basisu
- KHR_texture_transform
- WEB3D_quantized_attributes
- NGA_gpm_local (实验性)
注意:对于使用 KHR_texture_basisu 扩展压缩纹理的模型,我们建议在两个维度上使用 2 的幂次方的纹理,以获得最大的兼容性。这是因为某些采样器需要 2 的幂次方的纹理(在 WebGL 中使用纹理),并且 KHR_texture_basisu 需要 4 的倍数维度(KHR_texture_basisu 附加要求)。
Demo:
See:
Members
readonly activeAnimations : ModelAnimationCollection
Model#color
是半透明的或 Model#silhouetteSize 大于 0.0,则背面不会被剔除。
-
Default Value:
true
readonly boundingSphere : BoundingSphere
Model#minimumPixelSize.
-
Default Value:
true
readonly classificationType : ClassificationType
此外,还有一些要求/限制:
- glTF 不能包含变形目标、骨骼或动画。
- glTF 不能包含
EXT_mesh_gpu_instancing扩展。 - 只有具有 TRIANGLES 的网格可用于对其他资产进行分类。
- 网格必须是水密的(无缝隙)。
- 需要 POSITION 属性。
- 如果同时存在特征 ID 和索引缓冲区,则具有相同特征 ID 的所有索引必须占据索引缓冲区的连续部分。
- 如果存在特征 ID 但没有索引缓冲区,则具有相同特征 ID 的所有位置必须占据位置缓冲区的连续部分。
接收分类的 3D Tiles 或地形必须是不透明的。
-
Default Value:
undefined
Experimental
此功能使用的 3D Tiles 规范部分尚未定稿,可能会更改,且无需遵循 Cesium 的标准弃用策略。
clippingPlanes : ClippingPlaneCollection
ClippingPlaneCollection。
clippingPolygons : ClippingPolygonCollection
ClippingPolygonCollection。
-
Default Value:
undefined
colorBlendMode 为 MIX 时用于确定颜色强度的值。值为 0.0 时结果为模型的渲染颜色,值为 1.0 时结果为纯色,中间的任何值则为两者的混合。
-
Default Value:
0.5
-
Default Value:
ColorBlendMode.HIGHLIGHT
readonly credit : Credit
customShader : CustomShader
Cesium3DTileStyle 一起使用
可能会导致未定义的行为。
Experimental
此功能使用的 3D Tiles 规范部分尚未定稿,可能会更改,且无需遵循 Cesium 的标准弃用策略。
绘制模型中每个绘制命令的包围球。
-
Default Value:
false
以线框模式绘制模型。
-
Default Value:
false
distanceDisplayCondition : DistanceDisplayCondition
-
Default Value:
undefined
true,当 Scene.verticalExaggeration 设置为 1.0 以外的值时,模型会沿椭球法线方向夸张。
-
Default Value:
true
Example:
// 将地形夸张 2 倍,但防止模型夸张
scene.verticalExaggeration = 2.0;
model.enableVerticalExaggeration = false;
readonly environmentMapManager : DynamicEnvironmentMapManager
Example:
// 将用于模型环境贴图的地面颜色更改为森林绿
const environmentMapManager = model.environmentMapManager;
environmentMapManager.groundColor = Cesium.Color.fromCssColorString("#203b34");
readonly errorEvent : Event
ModelError 的实例。
对于 EXT_mesh_features,这是特征 ID 的 label 属性,如果未 指定,则为 "featureId_N"(其中 N 是 featureIds 数组中的索引)。EXT_feature_metadata 没有 label 字段,因此此类 特征 ID 集始终标记为 "featureId_N",其中 N 是所有特征 ID 列表中的索引,特征 ID 属性列在特征 ID 纹理之前。
如果 featureIdLabel 设置为整数 N,它将自动转换为 字符串 "featureId_N"。如果同时存在每个图元和每个实例的 特征 ID,则实例特征 ID 优先。
Experimental
此功能使用的 3D Tiles 规范部分尚未定稿,可能会更改,且无需遵循 Cesium 的标准弃用策略。
heightReference : HeightReference
-
Default Value:
{HeightReference.NONE}
-
Default Value:
undefined
See:
imageBasedLighting : ImageBasedLighting
如果 instanceFeatureIdLabel 设置为整数 N,它将自动转换为 字符串 "instanceFeatureId_N"。 如果同时存在每个图元和每个实例的特征 ID,则实例特征 ID 优先。
Experimental
此功能使用的 3D Tiles 规范部分尚未定稿,可能会更改,且无需遵循 Cesium 的标准弃用策略。
lightColor : Cartesian3
undefined 时,将使用场景的光照颜色。
通过设置
model.imageBasedLighting.imageBasedLightingFactor = new Cartesian2(0.0, 0.0)
禁用其他光源会使模型变暗很多。此时,增加光源的强度会使模型变亮。
-
Default Value:
undefined
Model#minimumPixelSize 设置上限,确保模型
永远不会处于不合理的缩放比例。
0.0 时,不强制执行最小大小。
-
Default Value:
0.0
modelMatrix : Matrix4
Transforms.eastNorthUpToFixedFrame 返回的矩阵。
-
Default Value:
Matrix4.IDENTITY
Example:
const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
m.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
outlineColor : Color
-
Default Value:
Color.BLACK
pointCloudShading : PointCloudShading
Cesium3DTileset 的。
true 时,此模型已准备好渲染,即外部二进制文件、图像
和着色器文件已下载,并且 WebGL 资源已创建。
-
Default Value:
false
readonly readyEvent : Event
Model 的实例。
如果 Model.incrementallyLoadTextures 为 true,此事件将在所有纹理加载完毕并准备好渲染之前引发。订阅 Model.texturesReadyEvent 以在纹理准备好时收到通知。
-
Default Value:
1.0
-
Default Value:
ShadowMode.ENABLED
-
Default Value:
true
-
Default Value:
false
-
Default Value:
true
silhouetteColor : Color
-
Default Value:
Color.RED
-
Default Value:
0.0
splitDirection : SplitDirection
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 资源已创建时)该 promise 会resolved。
该模型可以是具有 .gltf 扩展名的传统 glTF 资产,也可以是使用 .glb 扩展名的二进制 glTF。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
具有以下属性的对象:
|
Returns:
Throws:
-
RuntimeError : 模型加载失败。
-
RuntimeError : 不支持的 glTF 版本。
-
RuntimeError : 不支持的 glTF 扩展
Examples:
// 加载模型并将其添加到场景中
try {
const model = await Cesium.Model.fromGltfAsync({
url: "../../SampleData/models/CesiumMan/Cesium_Man.glb"
});
viewer.scene.primitives.add(model);
} catch (error) {
console.log(`加载模型失败。${error}`);
}
// 使用 modelMatrix 定位模型并以 128 像素的最小尺寸显示它
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(`加载模型失败。${error}`);
}
// 加载模型并以半速播放最后一个动画
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(`加载模型失败。${error}`);
}
Throws:
-
DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 为 true。
对象一旦销毁,就不应再使用;调用除
isDestroyed 之外的任何函数都会导致 DeveloperError 异常。因此,
如示例所示,将返回值(undefined)赋给该对象。
Throws:
-
DeveloperError : 此对象已被销毁,即调用了 destroy()。
- Model#isDestroyed
Example:
model = model && model.destroy();
See:
| Name | Type | Description |
|---|---|---|
extensionName |
string | 扩展的名称 |
Returns:
Throws:
-
DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 为 true。
Experimental
此功能尚未定稿,可能会更改,且无需遵循 Cesium 的标准弃用策略。
getNode(name) → ModelNode
name in the glTF. This is used to
modify a node's transform for user-defined animation.
| Name | Type | Description |
|---|---|---|
name |
string | The name of the node in the glTF. |
Returns:
undefined if no node with the name exists.
Throws:
-
DeveloperError : The model is not loaded. Use Model.readyEvent or wait for Model.ready to be 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);
getNode(name) → ModelNode
| Name | Type | Description |
|---|---|---|
name |
string | glTF 中节点的名称。 |
Returns:
Throws:
-
DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 为 true。
Example:
// 对节点 "Hand" 应用非均匀缩放
const node = model.getNode("Hand");
node.matrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(5.0, 1.0, 1.0), node.matrix);
Model#style 标记为脏,这将强制所有特征在模型可见的下一帧重新评估样式。
| Name | Type | Description |
|---|---|---|
articulationStageKey |
string | 铰接的名称、一个空格以及阶段的名称。 |
value |
number | 此铰接阶段的数值。 |
Throws:
-
DeveloperError : 模型未加载。请使用 Model.readyEvent 或等待 Model.ready 为 true。
Example:
// 设置属于名为 "SampleArticulation" 的铰接的名为 "MoveX" 的阶段的值
model.setArticulationStage("SampleArticulation MoveX", 50.0);
See:
Throws:
-
RuntimeError : 加载外部引用失败。
Type Definitions
| Name | Type | Description |
|---|---|---|
gltf |
object | gltf 对象 |
