HTML5畫布上的Three.js環境燈光(HTML5CanvasThree.jsAmbientLighting)

悟靜發表於2014-08-03

HTML5 畫布上的 Three.js 環境燈光(HTML5 Canvas Three.js Ambient Lighting)

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

本文遵循“署名-非商業用途-保持一致”創作公用協議

轉載請保留此句:太陽火神的美麗人生 –  本部落格專注於 敏捷開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。

HTML5 畫布上的 Three.js 環境燈光
HTML5 Canvas Three.js Ambient Lighting

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
    <script defer="defer">
      // revolutions per second
      var angularSpeed = 0.2; 
      var lastTime = 0;
 
      // this function is executed on each animation frame
      function animate(){
        // update
        var time = (new Date()).getTime();
        var timeDiff = time - lastTime;
        var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
        cube.rotation.y += angleChange;
        lastTime = time;
 
        // render
        renderer.render(scene, camera);
 
        // request new frame
        requestAnimationFrame(function(){
            animate();
        });
      }
 
      // renderer
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);
 
      // camera
      var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
      camera.position.z = 500;
 
      // scene
      var scene = new THREE.Scene();
                
      // cube
      var cube = new THREE.Mesh(new THREE.CubeGeometry(200, 200, 200), new THREE.MeshLambertMaterial({
        color: `blue` 
      }));
      cube.overdraw = true;
      cube.rotation.x = Math.PI * 0.1;
      scene.add(cube);
      
      // add subtle blue ambient lighting
      var ambientLight = new THREE.AmbientLight(0x000044);
      scene.add(ambientLight);
      
      // directional lighting
      var directionalLight = new THREE.DirectionalLight(0xffffff);
      directionalLight.position.set(1, 1, 1).normalize();
      scene.add(directionalLight);
 
      // start animation
      animate();
    </script>
  </body>
</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.


相關文章