如何使用Object3D.lookAt()函数使ExtrudeGeometry面向对象?

时间:2019-05-27 15:25:30

标签: javascript three.js aframe

我需要制作一个面向另一个对象的3D多边形。有什么好办法吗?我考虑过使用ExtrudeGeometry,但我不知道如何将Object3D.lookat()函数应用于该函数。我将不胜感激:)

这是我现在拥有的代码:

  var position = new THREE.Vector3(0,0,0);

  var polyhedronPts = [];
    polyhedronPts.push(new THREE.Vector2(-100, 600));
    polyhedronPts.push(new THREE.Vector2(300, 600));
    polyhedronPts.push(new THREE.Vector2(600, -100));
    polyhedronPts.push(new THREE.Vector2(200, 100));
    polyhedronPts.push(new THREE.Vector2(400, 200));

    polyhedronShape = new THREE.Shape(polyhedronPts);
    var extrudeSettings = {
      amount: 5
    }; 
    var geometry = new THREE.ExtrudeGeometry(polyhedronShape, extrudeSettings);

    geometry.lookAt(position); 

    var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
    mesh = new THREE.Mesh( geometry, material ) ;

    sceneEl.object3D.add(mesh);

geometry.lookAt(sphereposition);线应使对象面向[0,0,0] 但事实并非如此。您知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:1)

.lookAt()Object3D函数reference

这意味着您需要在网格对象而不是几何体上使用它。

要将对象向targetObject旋转,可以使用object.lookAt( targetObject.position )。这将检测到targetObject的当前位置,并相应地旋转它。

我已经设置了一个交互式JSFiddle。您可以拖动立方体,圆锥体将自动指向它。