Threejs:如何使用GLTFExporter导出具有绘制范围的索引几何?

问题描述 投票:1回答:2

我有一个特定的问题,我想导出一个具有绘图范围的索引几何。使用GLTFExporter,在遇到打字稿集成问题(显然是已知问题)之后,我运气不好发现导出器中没有实现:

// @TODO Indexed buffer geometry with drawRange not supported yet

https://github.com/mrdoob/three.js/blob/master/examples/js/exporters/GLTFExporter.js第564行

检查提交历史记录显示我最近的更新是3个月前,我认为这不会很快到来。我试图删除索引缓冲区并根据我的绘制范围重写我的位置bufferattribute数组,但我必须做错了,因为它不起作用,它只是打破了我的几何。你们有没有为我做过工作或者有关如何进行几何学的一些解释?

先感谢您。

编辑:

我目前的解决方法是为导出“去索引”我的几何体并保留drawRange,这种情况由导出器处理。它并不理想,它迫使我用新的BufferAttributes重新创建一个全新的几何体。但由于此操作仅用于导出,我甚至可以以异步方式进行此过程。我希望有更好的方法。

javascript typescript three.js gltf
2个回答
0
投票

关于这两个变量,我将在下面的PR中解决这个问题,使其成为WebGLUtils的一部分并导入它。每个需要这些常数的人每次都需要重新定义它们是没有意义的。


0
投票

正如我在编辑中所提到的,我通过对几何体进行去索引来绕过我的问题,它不是最好的解决方案,但由于我只需要它用于此导出,所以我继续这样做:

// original attributes
const vertices  = geometryTmp.getAttribute("position");
const normals  = geometryTmp.getAttribute("normal");
const uv  = geometryTmp.getAttribute("uv");

// new buffer arrays
let verticesTmp = new Float32Array(3 * geometryTmp.index.array.length);
let normalTmp = new Float32Array(3 * geometryTmp.index.array.length);
let uvTmp = new Float32Array(2 * geometryTmp.index.array.length);


let j = 0;
for(let i = 0; i < verticesTmp.length; i += 3) {
    let index = geometryTmp.index.array[j];
    verticesTmp[i] = vertices.getX(index);
    verticesTmp[i+1] = vertices.getY(index);
    verticesTmp[i+2] = vertices.getZ(index);

    normalTmp[i] = normals.getX(index);
    normalTmp[i+1] = normals.getY(index);
    normalTmp[i+2] = normals.getZ(index);
    j++;

}

j = 0;
for(let i = 0; i < uvTmp.length; i += 2) {
    let index = geometryTmp.index.array[j];
    uvTmp[i] = uv.getX(index);
    uvTmp[i+1] = uv.getY(index);
    j++;
}

let newGeomtry = new THREE.BufferGeometry();
newGeomtry.addAttribute( 'position', new THREE.BufferAttribute( verticesTmp, 3 ) );
newGeomtry.addAttribute( 'normal', new THREE.BufferAttribute( normalTmp, 3 ) );
newGeomtry.addAttribute( 'uv', new THREE.BufferAttribute( uvTmp, 2 ) );

newGeomtry.drawRange = geometryTmp.drawRange;
mesh.geometry = newGeomtry;  

// After I do that to all the meshes I need, them to a new THREE.Scene that will be given to the exporter with truncateDrawRange = true

我希望它也可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.