<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>three.js从入门到精通系列教程047 - 创建一个可旋转的立体椅子</title>
<script src="ThreeJS/three.js"></script>
<script src="ThreeJS/OrbitControls.js"></script>
</head>
<body>
<script>
var myRenderer, myCamera, myScene, myOrbitControls;
//创建渲染器
function initRender() {
myRenderer = new THREE.WebGLRenderer({ antialias: true });
myRenderer.setSize(window.innerWidth, window.innerHeight);
myRenderer.setClearColor(0xffffff);
document.body.appendChild(myRenderer.domElement);
myCamera = new THREE.PerspectiveCamera(45,
window.innerWidth / window.innerHeight, 0.1, 1000);
myCamera.position.set(0, 4, 5);
myCamera.lookAt(new THREE.Vector3(0, 0, 0));
myScene = new THREE.Scene();
myScene.add(new THREE.AmbientLight(0x444444));
var myLight = new THREE.PointLight(0xffffff);
myLight.position.set(0, 10, 10);
myScene.add(myLight);
}
//加载并创建Blender生成的JSON格式的模型
function initModel() {
var myObjectLoader = new THREE.ObjectLoader();
myObjectLoader.load("Data/misc_chair01.json", function (obj) {
obj.position.y = -1.25;
obj.scale.set(1.5, 1.5, 1.5);
myScene.add(obj);
});
}
//创建轨道控制器(允许自动旋转)
function initControls() {
myOrbitControls = new THREE.OrbitControls(myCamera);
myOrbitControls.autoRotate = true;
myOrbitControls.autoRotateSpeed = 3.5;
}
//渲染模型
function animate() {
myRenderer.render(myScene, myCamera);
myOrbitControls.update();
requestAnimationFrame(animate);
}
initRender();
initModel();
initControls();
animate();
</script>
</body>
</html>