簡體   English   中英

Threejs:如何使用GLTFExporter導出具有繪制范圍的索引幾何圖形?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM