JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
JavaScript
// 1owk3y "Direction of arrow seems to
// roam around in unpredictable directions"
const makeArrowLookAt_1 = function(obj, position){
const posVector = new THREE.Vector3().copy(position);
const localVector = obj.worldToLocal(posVector);
localVector.normalize();
obj.children[0].rotation.x = localVector.x;
obj.children[0].rotation.y = localVector.y;
obj.children[0].rotation.z = localVector.z;
}
// Mugen87 "causes arrow to stare at
// different unrelated direction"
const makeArrowLookAt_2 = function(obj, position){
obj.children[0].lookAt(position);
}
var camera, scene, renderer;
var geometry, material, boxFrom, boxTo;
init();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
material = new THREE.MeshNormalMaterial();
boxFrom = new THREE.Mesh( geometry, material );
boxFrom.position.set(-0.5, 0.2, -0.4);
scene.add( boxFrom );
boxTo = new THREE.Mesh( geometry, material );
boxTo.position.set(0.5, -0.2, -0.4);
scene.add( boxTo );
const dir = new THREE.Vector3( 0, 0, 0 );
const arrowPosition = new THREE.Vector3( 0, 0, 0 );
const length = 0.4;
const hex = 0xffff00;
arrowHelper = new THREE.ArrowHelper( dir, arrowPosition, length, hex );
boxFrom.add(arrowHelper);
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
boxFrom.rotation.x += 0.01;
boxFrom.rotation.y += 0.02;
boxTo.rotation.x -= 0.01;
boxTo.rotation.y -= 0.02;
//makeArrowLookAt_1(boxFrom, boxTo.position)
makeArrowLookAt_2(boxFrom, boxTo.position)
renderer.render( scene, camera );
}
animate();