![](/img/trans.png)
[英]How to draw a geometry shape exactly where mouse is clicked in ThreeJS
[英]Threejs: How can one export with GLTFExporter an indexed geometry with draw range?
我有一个特定的问题,我想导出一个带有drawrange的索引几何体。 使用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个月前,而且我认为这不会很快到来。 我试图删除索引缓冲区并根据我的绘制范围重写位置缓冲区属性数组,但是我必须做一些错误的事情,因为它不起作用,只会破坏几何形状。 你们中的任何人都可以为我解决一下问题,或者对如何进行几何图形进行一些解释吗?
先感谢您。
编辑:
我当前的解决方法是为导出几何图形“取消索引” ,并保持drawRange,这种情况由导出程序处理。 这不是理想的选择,它迫使我使用新的BufferAttributes重新创建一个全新的几何。 但是由于此操作仅针对导出完成,所以我什至可以使该过程以异步方式发生。 我希望有更好的方法。
关于这两个变量,我将在下面的PR中解决此问题,以使其成为WebGLUtils的一部分并仅将其导入。 每个需要这些常量的人每次都需要重新定义它们是没有意义的。
正如我在编辑中提到的那样,我通过取消几何索引来绕过了我的问题,这不是最好的解决方案,但是由于我只需要进行导出,因此按以下步骤进行:
// 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
我希望它也能帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.