从加载GLTF中提取全局顶点位置的问题

从 gltf 文件中提取顶点和面索引,这样我就能把它们作为碰撞网格输入物理引擎。大多数 gltf 文件都能正常工作,但有些文件就不行了。调试时我重建了一个新的网格,这样问题就显现出来了。

以下是要重现的代码:

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Debug Mesh Reconstruction</title>
    <script type="importmap">
{
  "imports": {
    "three": "https://cdn.jsdelivr.net/npm/three@v0.183.0/build/three.module.js",
    "three/addons/": "https://cdn.jsdelivr.net/npm/three@v0.183.0/examples/jsm/"
  }
}
</script>
</head>

<body style="margin:0; overflow:hidden;">
    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

        const scene = new THREE.Scene();

        const camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 1000);
        camera.position.set(2, 2, 2);

        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(innerWidth, innerHeight);
        document.body.appendChild(renderer.domElement);

        const controls = new OrbitControls(camera, renderer.domElement);

        const loader = new GLTFLoader();
        loader.load('Menger Sponge.glb', gltf => {
            const original = gltf.scene;
            scene.add(original);

            const originalData = [];

            original.updateWorldMatrix(true, true);
            original.traverse(child => {
                if (!child.isMesh) return;

                child.material = new THREE.MeshNormalMaterial()

                const geom = child.geometry;
                const posAttr = geom.attributes.position;
                const indexAttr = geom.index;

                const worldMatrix = child.matrixWorld;
                const v = new THREE.Vector3();

                const vertices = new Float32Array(posAttr.count * 3);
                for (let i = 0; i < posAttr.count; i++) {
                    v.fromBufferAttribute(posAttr, i).applyMatrix4(worldMatrix);
                    vertices[i * 3 + 0] = v.x;
                    vertices[i * 3 + 1] = v.y;
                    vertices[i * 3 + 2] = v.z;
                }
                const faces = new Uint16Array(indexAttr.array);

                originalData.push({ vertices, faces });
            });

            const reconstructed = new THREE.Group();
            for (const data of originalData) {
                const geometry = new THREE.BufferGeometry();
                geometry.setAttribute('position', new THREE.BufferAttribute(data.vertices, 3));
                geometry.setIndex(new THREE.BufferAttribute(data.faces, 1));
                geometry.computeVertexNormals();

                const mesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial());
                reconstructed.add(mesh);
            }
            scene.add(reconstructed);
            reconstructed.position.x = 3
        });

        function animate() {
            requestAnimationFrame(animate);
            controls.update();
            renderer.render(scene, camera);
        }
        animate();
    </script>
</body>

</html>

具体情况如下:

左边是原件,右边是重建版。

相关推荐
没有梦想的咸鱼185-1037-166312 小时前
北斗高精度数据解算:破解城市峡谷/长基线/无网区难题,从毫米级定位到自动化交付——(GAMIT/GLOBK底层核心解算技术方法)
运维·arcgis·数据分析·自动化
吴佳浩 Alben1 天前
Claude Code 源码泄露事件深度剖析
人工智能·arcgis·语言模型·自然语言处理·npm·node.js
城数派2 天前
2025年全国地铁线路及站点数据(ShapeFile文件/表格文件)
arcgis·信息可视化·数据分析
@高蕊2 天前
首创光储协同算法!iSolarBP破解分布式光伏市场化评估难题
大数据·人工智能·算法·arcgis·无人机
城数派2 天前
1985-2024年各省市县不同土地覆盖类型的土地面积(Excel)
数据库·arcgis·信息可视化·数据分析·excel
杨超越luckly2 天前
HTML应用指南:利用GET请求获取中国生活垃圾焚烧发电厂位置信息
python·arcgis·html·数据可视化·生活垃圾焚烧发电厂
GIS阵地3 天前
QgsProviderMetadata 详解(基于 QGIS 3.40.13 API)
数据库·qt·arcgis·oracle·gis·开源软件·qgis
城数派4 天前
2000-2024年1km精度人口分布栅格数据(全球/全国/分省/分市)
arcgis·信息可视化·数据分析·excel
城数派4 天前
1984-2024年中国10米分辨率城市土地利用栅格数据(商业、公服、居住等9类)
arcgis·信息可视化·数据分析·excel
城数派4 天前
2015-2024年我国1km分辨率逐日地表温度(LST)栅格数据
数据库·arcgis·信息可视化·数据分析·excel