`
18211103738
  • 浏览: 82695 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

ThreeJS系列教程-Lesson4

阅读更多

效果:

 

代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<title>Three.js tutorial - Lesson 04</title>
		<style>body{background:#000000;overflow:hidden;}</style>
		<script src="js/r69/three.js"></script> 
        <script src="js/r69/Detector.js"></script> 
        <script src="js/r69/CanvasRenderer.js"></script> 
        <script src="js/r69/Projector.js"></script>
	</head>
	<body>
		<script>
			var scene,camera,mesh,renderer;
			
			if(Detector.webgl){
				renderer = new THREE.WebGLRenderer({antialias:true});
			}else{
				renderer = new THREE.CanvasRenderer();
			}
			renderer.setSize(window.innerWidth, window.innerHeight);
			renderer.setClearColor(0x000000, 1);
			document.body.appendChild(renderer.domElement);
			
			scene = new THREE.Scene();
			
			camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 1000);
			camera.position.set(0, 0, 10);
			camera.lookAt(scene.position);	//相机聚焦于场景中心
			//scene.add(camera);
			
			var boxGeometry = new THREE.BoxGeometry(2.0, 2.0, 2.0);
			
			var texture = new THREE.ImageUtils.loadTexture("images/NeHe.jpg");	//创建一个图片纹理
			var boxMaterial = new THREE.MeshBasicMaterial({map:texture});		//用纹理创建一个材质
			
			var mesh = new THREE.Mesh(boxGeometry, boxMaterial);
			mesh.position.set(0, 0, 4);
			scene.add(mesh);
			
			var xRotation = 0;
			var yRotation = 0;
			var zRotation = 0;
			function render(){
				xRotation += 0.03;
				yRotation += 0.02;
				zRotation += 0.04;
				mesh.rotation.set(xRotation, yRotation, zRotation);
				
				renderer.render(scene, camera);
				
				requestAnimationFrame(render);
			}
			render();
		</script>
	</body>
</html>

 附注:当前笔者使用的three.js版本是r69

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics