【问题标题】:moving particles on face of imported *.obj in threejs在threejs中导入的* .obj面上移动粒子
【发布时间】:2016-07-18 06:53:29
【问题描述】:

我正在通过 dat.gui 上传具有 8 个顶点和 6 个面(顶点索引)的 3D *.obj(例如立方体)。虽然我可以成功地将我的点移动到顶点,但我无法将它们均匀地分布在模型的面上。

这是我到目前为止的过程:

(1) 加载对象文件

*.obj 的例子很简单:

v 5.526871 -3.827843 1.523720
v 5.526871 -1.827843 1.523720
v 5.526871 -3.827843 -0.476280
v 5.526871 -1.827843 -0.476280
v 7.526871 -3.827843 1.523720
v 7.526871 -1.827843 1.523720
v 7.526871 -3.827843 -0.476280
v 7.526871 -1.827843 -0.476280
s off
f 2 4 3 1
f 4 8 7 3
f 8 6 5 7
f 6 2 1 5
f 1 3 7 5
f 6 8 4 2

(2) 修剪其顶点和面

我正在使用 RegEx 模式来修剪顶点和面,然后将它们推送到我的几何体。

var faces_pattern1 = /f( +[\d]+)( [\d]+)( [\d]+)( [\d]+)?/g; // f vertex vertex vertex ...
if ( (result = faces_pattern1.exec(line) ) !== null ) {
   faces_pattern1.exec(line); 
   if ( result[ 4 ] === undefined ) {
        faces1.push( [
            parseInt( result[ 1 ] ) - 1,
            parseInt( result[ 2 ] ) - 1,
            parseInt( result[ 3 ] ) - 1
        ] );
    } else {
        faces1.push( [
            parseInt( result[ 1 ] ) - 1,
            parseInt( result[ 2 ] ) - 1,
            parseInt( result[ 3 ] ) - 1,
            parseInt( result[ 4 ] ) - 1
         ] );
    }
}
// push faces to geometry
for (var i = 0; i < faces1.length; i++) {
    this.renderer.geometry.faces.push( new THREE.Face3( faces1[i][0], faces1[i][1], faces1[i][2], faces1[i][3] ) );
}

(3) 在顶点上移动粒子

我有许多粒子,我将它们定位到顶点。这很好用。

var lg = allParticle.length;
for( var i = 0; i < lg; i++ ){
    var p = allParticle[i];
    p.diffX = ( vertices[h][0] * scale - p.x );
    p.diffY = ( -vertices[h][1] * scale - p.y );
    p.diffZ = ( -vertices[h][2] * scale - p.z );
    h += 1;
    if( h > nbVertices - 1 ) h = 0;
}

(4) 在脸上散布颗粒

我现在有一个切换开关,我想在立方体的表面上均匀分布相同的粒子。我尝试使用 GeometryUtils.randomPointsInGeometry 来做到这一点

var randomPointPositions = THREE.GeometryUtils.randomPointsInGeometry( this.renderer.geometry, lg  );
this.renderer.geometry.computeBoundingBox();
for( var i = 0; i < randomPointPositions.length; i++ ){

  var p = allParticle[i];
  p.diffX = randomPointPositions[i].x * scale ;
  p.diffY = randomPointPositions[i].y * scale;
  p.diffZ = randomPointPositions[i].z * scale ;
}

这仅在 x 轴上分布点,而不是在面部区域上均匀分布。有什么线索吗?

-- 这些是我得到的面孔 (THREE.Face3):

{a: 1, b: 3, c: 2, normal: T…E.Vector3, vertexNormals: Array[0]…}
{a: 3, b: 7, c: 6, normal: T…E.Vector3, vertexNormals: Array[0]…}
{a: 7, b: 5, c: 4, normal: T…E.Vector3, vertexNormals: Array[0]…}
{a: 5, b: 1, c: 0, normal: T…E.Vector3, vertexNormals: Array[0]…}
{a: 0, b: 2, c: 6, normal: T…E.Vector3, vertexNormals: Array[0]…}
{a: 5, b: 7, c: 3, normal: T…E.Vector3, vertexNormals: Array[0]…}

他们计算的 _area 总是为零。

类似的问题: THREE.js - position particles evenly on objects faces rather than verticies

【问题讨论】:

  • 我记得 three.js 有一个 .obj 解析器,所以您不需要重复这项工作。加载网格后,您可以选择不渲染它,但仍然可以访问顶点/面/计算法线/等。
  • 谢谢!我实际上是在不渲染它的情况下加载对象,而只是读取它的顶点/面值。这很好用;我被困在第四步了。
  • 我也尝试了 OBJLoader 方法。网格正确加载,然后我这样做: object.traverse( function ( child ) { if ( child instanceof THREE.Mesh ) { var randomPointPositions = THREE.GeometryUtils.randomPointsInBufferGeometry( child, 5000 ); } 但得到“无法读取属性'位置' 未定义”在 GeometryUtils.js:190 (vertices = geometry.attributes.position.array)
  • 罢工之前的 cmets -- 愚蠢的错字 » 应该是 child.geometry

标签: javascript 3d three.js


【解决方案1】:

好的 - 我花了一段时间,但我解决了它。

我尝试了 G.Profenza 建议的 OBJLoader 方法。

基本上加载对象,获取它的网格,使用 GeometryUtils.randomPointsinBufferGeometry 然后将粒子移动到你得到的vector3:

OBJLoader: function() {
        var manager = new THREE.LoadingManager();
        manager.onProgress = function ( item, loaded, total ) {
            console.log( item, loaded, total );
        };

        var onProgress = function ( xhr ) {
            if ( xhr.lengthComputable ) {
                var percentComplete = xhr.loaded / xhr.total * 100;
                console.log( Math.round(percentComplete, 2) + '% downloaded' );
            }
        };

        var onError = function ( xhr ) {
            console.log ("ERROR", xhr);
        };

        var loader = new THREE.OBJLoader( manager );
        var allParticle = this.scene.getParticles();
        var lg = allParticle.length;
        var scale = this.renderer.scale;    
        loader.load( '/data/cube_02.obj', function ( object ) {
            object.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                console.log (" === CHILD === ");
                console.log (child.geometry);
                var randomPointPositions = THREE.GeometryUtils.randomPointsInBufferGeometry( child.geometry, lg  );


                console.log (randomPointPositions[0].x, randomPointPositions[0].y, randomPointPositions[0].z );


                for( var i = 0; i < randomPointPositions.length; i++ ){

                   var p = allParticle[i];
                   p.diffX = randomPointPositions[i].x * scale -p.x ;
                   p.diffY = randomPointPositions[i].y * scale -p.y;
                   p.diffZ = randomPointPositions[i].z * scale -p.z;
                }

            }

        } );


        //object.position.y = - 95;
        //this.renderer.sceneGL.add(object);

        }, onProgress, onError );



    }

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多