I have started with three.js and I trying to move a object randomly around. But I am facing a problem of detecting that when does my 3d model moves outside of the visible area of the scene. I don't want the 3d model to move outside the scene. When I logged the position of the 3d model and the window.innerwidth I found that there was a huge difference in them even when the model is at the corner of the visible area. Can anyone help me out here?
Also if there are any suggestions on how can I achieve the bigger objective of random movement of the 3d object, please do share.
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xff0000, .5);
document.body.appendChild(renderer.domElement);
var geometry = new THREE.BoxGeometry();
var material = new THREE.MeshBasicMaterial({
color: 0x00ff00
});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
let pos = new THREE.Vector3();
pos = pos.setFromMatrixPosition(cube.matrixWorld);
pos.project(camera);
let widthHalf = window.innerWidth / 2;
let heightHalf = window.innerHeight / 2;
pos.x = (pos.x * widthHalf) + widthHalf;
pos.y = - (pos.y * heightHalf) + heightHalf;
pos.z = 0;
var numFlag = 0,
value = 0.01;
function animate() {
requestAnimationFrame(animate);
firstQuadrant();
renderer.render(scene, camera);
}
animate();
function firstQuadrant() {
if (numFlag < 300) {
cube.position.x += value;
cube.position.y += value;
} else {
cube.position.x -= value;
cube.position.y -= value;
}
console.log(cube.position.x, window.innerHeight,pos.x);
numFlag = numFlag > 600 ? 0 : numFlag + 1;
}
body {
margin: 0;
}
canvas {
display: block;
}
Update
When I try to change world coordinates to screen coordinates, by adding foloowing lines
let pos = new THREE.Vector3();
pos = pos.setFromMatrixPosition(cube.matrixWorld);
pos.project(camera);
let widthHalf = window.innerWidth / 2;
let heightHalf = window.innerHeight / 2;
pos.x = (pos.x * widthHalf) + widthHalf;
pos.y = - (pos.y * heightHalf) + heightHalf;
pos.z = 0;
then I get the pos.x
value as NaN
. What did I miss/went wrong?