<style> body { margin: 0; } canvas { width: 100%; height: 100% } </style> </head> <body> <canvas id="surface"></canvas> <script src="/javascripts/clock.js"></script> <script> var renderer; var stats; function initThree() { renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setClearColor(0xFFFFFF, 1.0); document.body.appendChild( renderer.domElement );
stats = new Stats(); //stats.setMode(1); // 0: fps, 1: ms stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px'; document.body.appendChild(stats.domElement); } var camera; function initCamera() { camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 ); camera.position.x = 600 ; camera.position.y = 0; camera.position.z = 600; camera.lookAt(new THREE.Vector3(0,0,0));
} function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize( window.innerWidth, window.innerHeight ); } var scene; function initScene() { scene = new THREE.Scene(); }
var light; function initLight() { // 方向光 /* light = new THREE.DirectionalLight(0xFF0000); light.position.set(0, 0,1); scene.add(light); light = new THREE.PointLight(0x00FF00); light.position.set(0, 0,25); scene.add(light);*/ }
var cube,mesh,texture; function initObject() { //加载纹理 texture = new THREE.Texture(); /*//加载canvas texture.image = canvas.toDataURL("image/png"); texture.needsUpdate = true; */ var geometry = new THREE.CubeGeometry(150, 150, 150);
var imgLoader = new THREE.ImageLoader(); imgLoader.load('images/timg.png',function(img) { //将图片值赋于纹理 texture.image = img; texture.needsUpdate = true; });
//texture = new THREE.Texture(canvas);
// 将纹理应用于材质 var material = new THREE.MeshBasicMaterial({map:texture}); mesh = new THREE.Mesh( geometry,material ); scene.add(mesh); window.addEventListener( 'resize', onWindowResize, false ); } function threeStart() { //clock(); initThree(); initCamera(); initScene(); initLight(); initObject(); animation(); }
function animation() { texture.needsUpdate = true; mesh.rotation.y -= 0.01; mesh.rotation.x -= 0.01; requestAnimationFrame( animation ); renderer.render( scene, camera ); }