为什么合并后的 Geometry 中的顶点数与从中获得的 BufferedGeometry 中的顶点数不同? [英] Why the number of vertices in a merged Geometry differs from the number of vertices in the BufferedGeometry obtained from it?

查看:28
本文介绍了为什么合并后的 Geometry 中的顶点数与从中获得的 BufferedGeometry 中的顶点数不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将更多对象合并在一起以创建一个 BufferGeometry.在合并期间,我想填充一个缓冲区,稍后我将使用该缓冲区向我的 BufferGeometry 添加属性.因此,我需要在创建 BufferedGeometry 之前知道缓冲区的大小.

I'm merging more objects together to create a BufferGeometry. During the merging, i want to fullfil a buffer that i will use later to add an attribute to my BufferGeometry. Therefore, I need to know the size of the buffer before the BufferedGeometry is created.

为了计算缓冲区的大小,我计算了该几何体中的顶点数加上面数乘以 3.正如这段代码所说 https://github.com/mrdoob/three.js/blob/master/src/core/DirectGeometry.js#L148 并且正如这个答案所暗示的 在 Three.js 中将 Geometry 转换为 BufferGeometry 会增加顶点数吗?

To calculate the size of the buffer I'm counting the number of vertices in that geometry added to the number of the faces multiplied by 3. As this code says https://github.com/mrdoob/three.js/blob/master/src/core/DirectGeometry.js#L148 and as this answer suggests Does converting a Geometry to a BufferGeometry in Three.js increase the number of vertices?

但是这样做,缓冲区几何中的顶点数是 57600,我计算出来的顶点数是 67400.我做错了什么,但我不明白到底是什么.

But doing like this, the number of vertices in the buffer geometry is 57600, that one in calculated by me is 67400. I'm doing something wrong but I do not understand what exactly.

这是我正在使用的代码:

This is the code that I'm using:

let tot_objects = 100;
let material = new THREE.MeshStandardMaterial( {color: 0x00ff00} );
let geometry = new THREE.BoxGeometry(5, 5, 5, 4, 4, 4);
let objs = populateGroup(geometry, material, tot_objects);

//let's merge all the objects in one geometry
let mergedGeometry = new THREE.Geometry();
for (let i = 0; i < objs.length; i++){
    let mesh = objs[i];
    mesh.updateMatrix();
    mergedGeometry.merge(mesh.geometry, mesh.matrix);
}

let bufGeometry = new THREE.BufferGeometry().fromGeometry(mergedGeometry);

let totVerticesMergedGeometry = (mergedGeometry.vertices.length ) + (mergedGeometry.faces.length * 3);
console.log(bufGeometry.attributes.position.count); // 57600
console.log(totVerticesMergedGeometry); // it returns 67400 !!!
scene.add(new THREE.Mesh(bufGeometry, material));

function populateGroup(selected_geometry, selected_material, tot_objects) {
    let objects = [];
    for (var i = 0; i< tot_objects; i++) {
        let coord = {x:i, y:i, z:i};
        let object = new THREE.Mesh(selected_geometry, selected_material);
        object.position.set(coord.x, coord.y, coord.z);
        object.rotateY( (90 + 40 + i * 100/tot_objects) * -Math.PI/180.0 );
        objects.push(object);
    }
    return objects;
}

推荐答案

在非索引的 BufferGeometry 中,position 属性数组的长度总是 numberOfFaces * 3 * 3(每个面 3 个顶点,每个顶点 3 个值).这也是您指向的 DirectGeometry 中的代码所做的:它遍历面并将每个面的三个顶点推送到顶点数组.

In a non-indexed BufferGeometry the length of the position attribute-array is always numberOfFaces * 3 * 3 (3 vertices per face, 3 values per vertex). That is also what the code from DirectGeometry you pointed to does: it iterates over the faces and pushes three vertices per face to the vertices-array.

一个在所有方向上有 4 个线段的盒子几何体有 192 个面:每边 4 * 4 = 16 个线段,每条线段有 2 个三角形使得 16 *每边 2 = 32 个面.而 32 * 6 = 192.你有 100 个这样的盒子几何图形,所以总共有 19200 个面.第 3 次我们得到 57600 个顶点.

A box-geometry with 4 segments in all directions has 192 faces: 4 * 4 = 16 segments per side with 2 triangles per segment makes 16 * 2 = 32 faces per side. And 32 * 6 = 192. You have 100 of these box-geometries so there are 19200 faces in total. Times 3 we get 57600 vertices.

这篇关于为什么合并后的 Geometry 中的顶点数与从中获得的 BufferedGeometry 中的顶点数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆