首页 > 解决方案 > 在 Cesium 中提取 glTF 矩形模型的中心坐标

问题描述

我有一个描述矩形的简单 glTF 文件。

我需要在铯中检索那个矩形的中心。

一种方法是“读取”glTF 文件本身中的一些值。我手动完成了它并且有效。就是这样。

这是 glTF 文件开头的内容:

{
    "accessors": {
        "accessor_15": {
            "bufferView": "bufferView_21",
            "byteOffset": 0,
            "byteStride": 0,
            "componentType": 5123,
            "count": 6,
            "type": "SCALAR"
        },
        "accessor_17": {
            "bufferView": "bufferView_22",
            "byteOffset": 0,
            "byteStride": 12,
            "componentType": 5126,
            "count": 4,
            "max": [
                50,
                148,
                4
            ],
            "min": [
                -52,
                140,
                -64
            ],
            "type": "VEC3"
        },
        "accessor_19": {
            "bufferView": "bufferView_22",
            "byteOffset": 48,
            "byteStride": 8,
            "componentType": 5126,
            "count": 4,
            "max": [
                0,
                1
            ],
            "min": [
                -1,
                0
            ],
            "type": "VEC2"
        }
    },

我注意到该accessor_17元素实际上包含沿每个轴的最小值和最大值:x、y 和 z。

如果我对每个取平均值,这显然给了我(-1, 144, -30)矩形的中心,如果我在 Cesium 中绘制一个点viewer.entities.add(),它会显示在预期的位置。

现在,我怎样才能以编程方式做到这一点?例如,我想到的方法是在加载 glTF 之后在我的 Cesium 代码中提取 x、y 和 z 的这些最小值和最大值。这可能以简单的方式吗?或者您是否在 JavaScript 代码中看到任何其他优雅的方式来做到这一点?

这是我加载 glTF 文件的方式:

const modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(position);
const model = await scene.primitives.add(
  Cesium.Model.fromGltf({
    id: 1,
    url: `./gltf/my_model.gltf`,
    modelMatrix: modelMatrix,
    scale: 1,
    show: true
  })
);

标签: cesiumgltf

解决方案


推荐阅读