Threejs - 如何在网格中进行光线投射并获得三角形的重心坐标?

时间:2015-09-30 07:04:29

标签: javascript three.js

我使用r71,我需要点击光线投射以根据用户上传的照片缩放THREE.Geometry平面,并获得网格上点击三角形内点的重心坐标。 我知道有一个THREE.Triangle.barycoordFromPoint (point, optionalTarget)函数,但我猜不到所需的结果,因为网格是根据上传的图像缩放的。

var scaleRatio = 0.1;

var loader = new THREE.OBJLoader();

loader.load( 'obj/plano_tri.obj', function(object) {

    var scaleTexture = 1;

    scaleGeometryX = texture.image.width / texture.image.height ;

     var desiredHeight = 144 * 0.08;

     if (texture.image.height > texture.image.width) {

          scaleTexture = desiredHeight / texture.image.height;

     }

      var plane = object;

      plane.scale.setX( scaleRatio * scaleGeometryX );

      plane.scale.setY( scaleRatio );

      scene.add( plane );

   });

我的代码尝试将三维光线投影中获得的Face3索引转换为三角形,然后使用来自光线投射的点调用该函数:

raycaster.setFromCamera( mouse, camera );

intersects = raycaster.intersectObject( plane, true );

var face = plane.geometry.faces[ intersects[0].faceIndex ];

var faceX = plane.geometry.vertices[ face.a ];

var faceY = plane.geometry.vertices[ face.b ];

var faceZ = plane.geometry.vertices[ face.c ];

var triangle = new THREE.Triangle ( faceX, faceY, faceZ );

var bary = triangle.barycoordFromPoint( raycastLast[0].point ) ;

console.log( bary )输出T…E.Vector3 {x: 11.585726082148518, y: 27.99652418990989, z: -38.58225027205841}似乎不正确,因为值非常大。

如何从缩放网格的三角形内的交叉点获取正确的重心坐标?

谢谢

1 个答案:

答案 0 :(得分:1)

我设法了解如何获得正确的值。 三角形UV也需要缩放:

var faceX = new THREE.Vector3( plane.geometry.vertices[faceId.a].x  / ( 1/scaleRatio ) * scaleGeometryX , plane.geometry.vertices[faceId.a].y  / (1/scaleRatio ) , 0    );

var faceY = new THREE.Vector3( plane.geometry.vertices[faceId.b].x  / ( 1/scaleRatio ) * scaleGeometryX , plane.geometry.vertices[faceId.b].y  / (1/scaleRatio ) , 0    );

var faceZ = new THREE.Vector3( plane.geometry.vertices[faceId.c].x  / ( 1/scaleRatio ) * scaleGeometryX , plane.geometry.vertices[faceId.c].y  / (1/scaleRatio ) , 0   );

var triangle = new THREE.Triangle( faceX , faceY , faceZ );

var bary = triangleBar.barycoordFromPoint (intersects[ 0 ].point);