C、添加渲染器。渲染器是用来设定渲染的结果会在页面的什么元素上面呈现,以及按什么规则来渲染。这里使用的是CSS3DRenderer渲染器。
- /*
- * 添加渲染器
- */
- renderer = new THREE.CSS3DRenderer(); // 定义渲染器
- renderer.setSize( window.innerWidth, window.innerHeight ); // 定义尺寸
- document.body.appendChild( renderer.domElement ); // 将场景到加入页面中
D、创建正方体。立方体全景图有6个面,我们需要定义每个面贴图的背景图片,3D位置,旋转角度(默认的6个面都是朝着我们的,我们需要定义朝坐标轴的各个方向做90度的旋转,才可以搭建成一个立方体)。
- /**
- *正方体的6个面的资源及相关(坐标、旋转等)设置
- */
- var flipAngle = Math.PI, // 180度
- rightAngle = flipAngle / 2, // 90度
- tileWidth = 512;
- var sides = [{
- url: "images/panorama.right.jpg", //right
- position: [-tileWidth, 0, 0],
- rotation: [0, rightAngle, 0]
- }, {
- url: "images/panorama.left.jpg", //left
- position: [tileWidth, 0, 0],
- rotation: [0, -rightAngle, 0]
- }, {
- url: "images/panorama.top.jpg", //top
- position: [0, tileWidth, 0],
- rotation: [rightAngle, 0, Math.PI]
- }, {
- url: "images/panorama.bottom.jpg", //bottom
- position: [0, -tileWidth, 0],
- rotation: [-rightAngle, 0, Math.PI]
- }, {
- url: "images/panorama.front.jpg", //front
- position: [0, 0, tileWidth],
- rotation: [0, Math.PI, 0]
- }, {
- url: "images/panorama.back.jpg", //back
- position: [0, 0, -tileWidth],
- rotation: [0, 0, 0]
- }];
- for ( var i = 0; i < sides.length; i ++ ) {
- var side = sides[ i ];
- var element = document.getElementById("bg_section_"+i);
- element.width = 1026;
- element.height = 1026; // 2 pixels extra to close the gap.
- // 添加一个渲染器
- var object = new THREE.CSS3DObject( element );
- object.position.fromArray( side.position );
- object.rotation.fromArray( side.rotation );
- scene.add( object );
- }
E、实时渲染函数。这里我们用的是Threejs的 实时渲染 :就是需要不停的对画面进行渲染,即使画面中什么也没有改变,也需要重新渲染。
其中一个重要的函数是requestAnimationFrame,这个函数就是让浏览器去执行一次参数中的函数,这样通过上面animate中调用requestAnimationFrame()函数,requestAnimationFrame()函数又让animate()再执行一次,就形成了我们通常所说的渲染循环了。
- /**
- * 实时渲染函数
- */
- function animate() {
- requestAnimationFrame(animate);
- // lon = Math.max(-180, Math.min(180, lon));//限制固定角度内旋转
- // lon += 0.1;//自动旋转
- lat = Math.max(-85, Math.min(85, lat)); //限制固定角度内旋转
- phi = THREE.Math.degToRad(85 - lat);
- theta = THREE.Math.degToRad(lon+180);
- target.x = Math.sin(phi) * Math.cos(theta);
- target.y = Math.cos(phi);
- target.z = Math.sin(phi) * Math.sin(theta);
- camera.lookAt( target );
- camera.updateProjectionMatrix();
- isDeviceing == false ? initMouseControl() : deviceControl.update();
- renderer.render(scene, camera);
- }
F、窗口改变时对camera焦点的处理。
- /**
- * 窗体大小改变
- */
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
G、相机焦点跟着鼠标或手指的操作移动。
- /*
- 相机焦点跟着鼠标或手指的操作移动
- */
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- }
- function onDocumentMouseMove( event ) {
- var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
- var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
- lon -= movementX * 0.1;
- lat += movementY * 0.1;
- }
- function onDocumentMouseUp( event ) {
- document.removeEventListener( 'mousemove', onDocumentMouseMove );
- document.removeEventListener( 'mouseup', onDocumentMouseUp );
- }
- /**
- * 鼠标滚轮改变相机焦距
- */
- function onDocumentMouseWheel( event ) {
- camera.fov += event.deltaY * 0.05;
- camera.updateProjectionMatrix();
- }
- function onDocumentTouchStart( event ) {
- event.preventDefault();
- var touch = event.touches[ 0 ];
- touchX = touch.screenX;
- touchY = touch.screenY;
- }
- function onDocumentTouchMove( event ) {
- event.preventDefault();
- var touch = event.touches[ 0 ];
- lon -= ( touch.screenX - touchX ) * 0.1;
- lat += ( touch.screenY - touchY ) * 0.1;
- touchX = touch.screenX;
- touchY = touch.screenY;
- }