分享

HTML5 画布上的 Three.js 环境灯光(HTML5 Canvas Three.js Ambient Lighting)

 pengphie 2016-12-21

HTML5 画布上的 Three.js 环境灯光(Html5 Canvas Three.js Ambient Lighting)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOSAndroid、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


HTML5 画布上的 Three.js 环境灯光
HTML5 Canvas Three.js Ambient Lighting


[javascript] view plain copy
print?
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.   <head>  
  4.     <style>  
  5.       body {  
  6.         margin: 0px;  
  7.         padding: 0px;  
  8.       }  
  9.     </style>  
  10.   </head>  
  11.   <body>  
  12.     <div id="container"></div>  
  13.     <script src="http://www./libraries/three.min.js"></script>  
  14.     <script defer="defer">  
  15.       // revolutions per second  
  16.       var angularSpeed = 0.2;   
  17.       var lastTime = 0;  
  18.    
  19.       // this function is executed on each animation frame  
  20.       function animate(){  
  21.         // update  
  22.         var time = (new Date()).getTime();  
  23.         var timeDiff = time - lastTime;  
  24.         var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;  
  25.         cube.rotation.y += angleChange;  
  26.         lastTime = time;  
  27.    
  28.         // render  
  29.         renderer.render(scene, camera);  
  30.    
  31.         // request new frame  
  32.         requestAnimationFrame(function(){  
  33.             animate();  
  34.         });  
  35.       }  
  36.    
  37.       // renderer  
  38.       var renderer = new THREE.WebGLRenderer();  
  39.       renderer.setSize(window.innerWidth, window.innerHeight);  
  40.       document.body.appendChild(renderer.domElement);  
  41.    
  42.       // camera  
  43.       var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);  
  44.       camera.position.z = 500;  
  45.    
  46.       // scene  
  47.       var scene = new THREE.Scene();  
  48.                   
  49.       // cube  
  50.       var cube = new THREE.Mesh(new THREE.CubeGeometry(200, 200, 200), new THREE.MeshLambertMaterial({  
  51.         color: 'blue'   
  52.       }));  
  53.       cube.overdraw = true;  
  54.       cube.rotation.x = Math.PI * 0.1;  
  55.       scene.add(cube);  
  56.         
  57.       // add subtle blue ambient lighting  
  58.       var ambientLight = new THREE.AmbientLight(0x000044);  
  59.       scene.add(ambientLight);  
  60.         
  61.       // directional lighting  
  62.       var directionalLight = new THREE.DirectionalLight(0xffffff);  
  63.       directionalLight.position.set(1, 1, 1).normalize();  
  64.       scene.add(directionalLight);  
  65.    
  66.       // start animation  
  67.       animate();  
  68.     </script>  
  69.   </body>  
  70. </html>        

讨论
Discussion

要使用 Three.js 创建环境灯光,我们可以实例化一个 AmbientLight 对象,然后把它添加到场景中。环境灯光需要一个颜色来定义。环境灯光照亮整个场景,可用于柔化位置灯光,诸如 directional 灯光。

To create ambient lighting with Three.js, we can instantiate an AmbientLight object and then add it to the scene.  AmbientLight requires is defined with a color.  Ambient lights illuminate the entire scene and can be used to soften positional lights such as directional lights.



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多