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

效果图:






官网:https://threejs.org/

GitHub:https://github.com/mrdoob/three.js/




  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值