天天看点

HTML5 Three.js 3D特效

一、Three.js基本介绍

Three.js是JavaScript编写的WebGL第三方库。提供了非常多的3D显示功能。Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机、光影、材质等各种对象。你可以在它的主页上看到许多精采的演示。不过,这款引擎目前还处在比较不成熟的开发阶段,其不够丰富的 API 以及匮乏的文档增加了初学者的学习难度(尤其是文档的匮乏,基本没有中文的)Three.js的代码托管在github上面。

点击查看最终效果

二、基本 Demo

1.最基本的Hello World:http://stemkoski.github.io/Three.js/HelloWorld.html

2.在网页上调用摄像头:http://stemkoski.github.io/Three.js/Webcam-Texture.html

3.体感操作:http://stemkoski.github.io/Three.js/Webcam-Motion-Detection-Texture.html

4.支持游戏手柄:http://stemkoski.github.io/Three.js/Mesh-Movement-Gamepad.html

5.3D建模和方向键控制移动方向:http://stemkoski.github.io/Three.js/Model-Animation-Control.html

6.SkyBox和三个气泡的渲染:http://stemkoski.github.io/Three.js/Metabubbles.html

7.3D红蓝偏光的名车展:http://threejs.org/examples/webgl_materials_cars_anaglyph.html

8.跑车游戏:http://hexgl.bkcore.com/

三、Three.js编写环境准备

1.Three.js库文件下载:https://github.com/mrdoob/three.js/

2.已安装IIS或Tomcat或Apache,这些例子必须挂到服务器上才能正常运行,本地打开会出现各种无法理解的问题。

四、动手编写第一个 Demo

[html]  view plain copy

  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4. <title>Template (Three.js)</title>  
  5. <meta charset="utf-8">  
  6. <meta name="viewport"  
  7.     content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">  
  8. <link rel=stylesheet href="css/base.css" />  
  9. </head>  
  10. <body>  
  11.     <script src="../js/Three.js"></script> <!-- 这个是Three.js引擎的js -->  
  12.     <script src="../js/Detector.js"></script>  
  13.     <script src="../js/Stats.js"></script>  
  14.     <script src="../js/OrbitControls.js"></script>  
  15.     <script src="../js/THREEx.KeyboardState.js"></script>  
  16.     <script src="../js/THREEx.FullScreen.js"></script>  
  17.     <script src="../js/THREEx.WindowResize.js"></script>  
  18.     <script src="../js/Texture.js"></script> <!-- 一些js工具类,现在不深究 -->  
  19.     <div id="ThreeJS"  
  20.         style="z-index: 1; position: absolute; left: 0px; top: 0px"></div> <!-- 这个div就是整个画布 -->  
  21.     <script>  
  22.         //      
  23.         // MAIN //  
  24.         //  
  25.         // standard global variables  
  26.         var container, scene, camera, renderer, controls, stats; // 几个变量代表的含义:容器、场景、摄像机(视角)、渲染器、控制装置  
  27.         var keyboard = new THREEx.KeyboardState();  
  28.         var clock = new THREE.Clock();  
  29.         // custom global variables  
  30.         var cube;  
  31.         // initialization  
  32.         init();  
  33.         // animation loop / game loop  
  34.         animate();  
  35.         ///  
  36.         // FUNCTIONS //  
  37.         ///  
  38.         function init() { // 初始化  
  39.             ///  
  40.             // SCENE //  
  41.             ///  
  42.             scene = new THREE.Scene(); // 定义场景  
  43.             // CAMERA //  
  44.             // set the view size in pixels (custom or according to window size)  
  45.             // var SCREEN_WIDTH = 400, SCREEN_HEIGHT = 300;  
  46.             var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;  
  47.             // camera attributes  
  48.             var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;  
  49.             // set up camera  
  50.             camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR); // 定义视角  
  51.             // add the camera to the scene  
  52.             scene.add(camera);  
  53.             // the camera defaults to position (0,0,0)  
  54.             // so pull it back (z = 400) and up (y = 100) and set the angle towards the scene origin  
  55.             camera.position.set(-400, 150, 200); // 视角的位置  
  56.             camera.lookAt(scene.position);  
  57.             //  
  58.             // RENDERER //  
  59.             //  
  60.             // create and start the renderer; choose antialias setting.  
  61.             if (Detector.webgl)  
  62.                 renderer = new THREE.WebGLRenderer({  
  63.                     antialias : true  
  64.                 });  
  65.             else  
  66.                 renderer = new THREE.CanvasRenderer();  
  67.             renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);  
  68.             // attach div element to variable to contain the renderer  
  69.             container = document.getElementById('ThreeJS');  
  70.             // alternatively: to create the div at runtime, use:  
  71.             // container = document.createElement( 'div' );  
  72.             // document.body.appendChild( container );  
  73.             // attach renderer to the container div  
  74.             container.appendChild(renderer.domElement);  
  75.             // EVENTS //  
  76.             // automatically resize renderer  
  77.             THREEx.WindowResize(renderer, camera);  
  78.             // toggle full-screen on given key press  
  79.             THREEx.FullScreen.bindKey({  
  80.                 charCode : 'm'.charCodeAt(0)  
  81.             });  
  82.             //  
  83.             // CONTROLS //  
  84.             //  
  85.             // move mouse and: left   click to rotate,   
  86.             //                 middle click to zoom,   
  87.             //                 right  click to pan  
  88.             controls = new THREE.OrbitControls(camera, renderer.domElement); // 设置控制,这里只有鼠标操作  
  89.             ///  
  90.             // STATS //  
  91.             ///  
  92.             // displays current and past frames per second attained by scene  
  93.             stats = new Stats();  
  94.             stats.domElement.style.position = 'absolute';  
  95.             stats.domElement.style.bottom = '0px';  
  96.             stats.domElement.style.zIndex = 100;  
  97.             container.appendChild(stats.domElement);  
  98.             ///  
  99.             // LIGHT //  
  100.             ///  
  101.             // create a light  
  102.             var light = new THREE.PointLight(0xffffff); // 设置光源  
  103.             light.position.set(0, 250, 0);  
  104.             scene.add(light);  
  105.             // CUBE  
  106.             var cubeGeometry = new THREE.CubeGeometry(260, 35, 185, 1, 1, 1); // 定义一个立方体,就是那本书的模型  
  107.             var cubeMaterialArray = [];  
  108.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  109.                 map : new THREE.ImageUtils.loadTexture('img/side-up.png') // 给每一面上贴图,下同  
  110.             }));  
  111.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  112.                 map : new THREE.ImageUtils.loadTexture('img/side-up.png')  
  113.             }));  
  114.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  115.                 map : new THREE.ImageUtils.loadTexture('img/up.png')  
  116.             }));  
  117.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  118.                 map : new THREE.ImageUtils.loadTexture('img/down.png')  
  119.             }));  
  120.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  121.                 map : new THREE.ImageUtils.loadTexture('img/side-right.png')  
  122.             }));  
  123.             cubeMaterialArray.push(new THREE.MeshBasicMaterial({  
  124.                 map : new THREE.ImageUtils.loadTexture('img/side-left.png')  
  125.             }));  
  126.             var cubeMaterials = new THREE.MeshFaceMaterial(cubeMaterialArray);  
  127.             cube = new THREE.Mesh(cubeGeometry, cubeMaterials);  
  128.             cube.position.set(0, 0, 0); // 立方体放置的位置  
  129.             scene.add(cube);  
  130.         }  
  131.         function animate() {  
  132.             requestAnimationFrame(animate);  
  133.             render();  
  134.             update();  
  135.         }  
  136.         function update() {  
  137.             // delta = change in time since last call (in seconds)  
  138.             var delta = clock.getDelta();  
  139.             controls.update();  
  140.             stats.update();  
  141.         }  
  142.         function render() {  
  143.             renderer.render(scene, camera);  
  144.         }  
  145.     </script>  
  146. </body>  
  147. </html>  

效果图:

HTML5 Three.js 3D特效

继续阅读