See camera camera target point: because the screen shows the view of the camera vertebral visual range, and the camera lookAt method is an observation camera target point, it can be drawn:
LookAt camera display certain point in the middle of the screen: the use of this point, we can achieve the object moves, we can always track the object, so the object is always in the center of the screen: what is the code to achieve
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>shape对象的研究</title>
<style>
body{
margin:0;
}
</style>
</head>
<script src="../js/three.js"></script>
<script>
function init() {
createScene();
buildAuxSystem();
addBox();
loop();
}
var scene,camera,renderer,width = window.innerWidth,height = window.innerHeight,controls;
var box,x =0 ,z = 0;
function createScene() {
scene = new THREE.Scene;
camera = new THREE.PerspectiveCamera(45,width/height,1,1000);
camera.position.set(200,200,200);
camera.lookAt(scene.position);
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x333333),1);
renderer.setSize(width,height);
document.body.appendChild(renderer.domElement);
document.addEventListener("keydown",handleKey,false);
window.addEventListener("resize",handleWindowResize,false)
}
function loop() {
renderer.render(scene,camera);
box.position.set(x,5,z);
camera.lookAt(box.position);
requestAnimationFrame(loop);
}
function handleWindowResize() {
width = window.innerWidth;
height = window.innerHeight;
renderer.setSize(width,height);
camera.aspect = width/height;
camera.updateProjectionMatrix();
}
// 建立辅助设备系统
function buildAuxSystem(){
var gridHelper = new THREE.GridHelper(320,32);
scene.add(gridHelper);
var axesHelper = new THREE.AxesHelper(320);
scene.add(axesHelper);
}
function addBox() {
var geometry = new THREE.CubeGeometry(10,10,10);
box = new THREE.Mesh(geometry,new THREE.MeshBasicMaterial({color:0x00ff00}));
box.position.set(x,5,z);
scene.add(box);
}
function handleKey(e) {
if(e.keyCode == 38){
z -= 10;
}else if(e.keyCode == 39){
x += 10;
}else{
return;
}
}
</script>
<body onload ="init()">
</body>
</html>