简体   繁体   English

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

[英]Threejs: How can one export with GLTFExporter an indexed geometry with draw range?

I have a specific problem, I would like to export an indexed geomtry that has a drawrange. 我有一个特定的问题,我想导出一个带有drawrange的索引几何体。 Using the GLTFExporter, after having faced the issue with typescript integration (known issue apparently), I had the bad luck to discover that this was not implemented in the exporter: 使用GLTFExporter,面对打字稿集成问题(显然是已知问题)后,我很不幸地发现导出器中未实现此问题:

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

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

Checking the commit history showed me that the last update was 3 months ago and I don't think this is gonna come any time soon. 查看提交历史记录后,我发现最近一次更新是在3个月前,而且我认为这不会很快到来。 I tried to remove the index buffer and rewrite my position bufferattribute array base on my draw range but I must do something wrong because it does not work, it simply breaks my geometry. 我试图删除索引缓冲区并根据我的绘制范围重写位置缓冲区属性数组,但是我必须做一些错误的事情,因为它不起作用,只会破坏几何形状。 Would any of you have a work around for me or some explainations on how to proceed with my geometry? 你们中的任何人都可以为我解决一下问题,或者对如何进行几何图形进行一些解释吗?

Thank you in advance. 先感谢您。

EDIT: 编辑:

My current work-around is to "de-index" my geometry for the export and keep the drawRange, this case is handled by the exporter. 我当前的解决方法是为导出几何图形“取消索引” ,并保持drawRange,这种情况由导出程序处理。 It is not ideal, and it forces me to recreate a full new geometry with new BufferAttributes. 这不是理想的选择,它迫使我使用新的BufferAttributes重新创建一个全新的几何。 But since this operation is only done for the export, I can even have this process happening in an asynchronous way. 但是由于此操作仅针对导出完成,所以我什至可以使该过程以异步方式发生。 I wish there was a better way. 我希望有更好的方法。

Regarding the two variables, this is something I'm going to address in the following PR to make it part of the WebGLUtils and just import it. 关于这两个变量,我将在下面的PR中解决此问题,以使其成为WebGLUtils的一部分并仅将其导入。 It doesn't make sense that each one that needs these constants need to redefine them again every time. 每个需要这些常量的人每次都需要重新定义它们是没有意义的。

As mentionned in my edit, I bypassed my problem by de-indexing of my geometry, it is not the best solution, but since I only need it for this export, here is how I proceeded: 正如我在编辑中提到的那样,我通过取消几何索引来绕过了我的问题,这不是最好的解决方案,但是由于我只需要进行导出,因此按以下步骤进行:

// 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

I hope it helps someone too. 我希望它也能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM