2016-03-25 99 views
1

如何在Three.js中啓用陰影?如何在Three.js中啓用陰影

不同的例子表明它應該像指定THREE.FlatShading一樣簡單,但是當我這樣做時,我的形狀根本沒有陰影。

我的JS:

CANVAS_WIDTH = 200, 
CANVAS_HEIGHT = 200; 

var camera, scene, renderer; 
var mesh; 
init(); 
animate(); 
function init() { 
    camera = new THREE.PerspectiveCamera(70, CANVAS_WIDTH/CANVAS_HEIGHT, 1, 1000); 
    camera.position.z = 400; 
    scene = new THREE.Scene(); 

    var geometry = new THREE.BoxBufferGeometry(200, 200, 200); 

    var material = new THREE.MeshBasicMaterial({color: 0xff0000}); 
    material.shading = THREE.FlatShading; 
    for(var i=0; i<material.length; i++){ 
     material[i].shading = THREE.FlatShading; 
    } 

    mesh = new THREE.Mesh(geometry, material); 
    scene.add(mesh); 

    // light 
    //scene.add(new THREE.AmbientLight(0x404040)); 
    light = new THREE.DirectionalLight(0xffffff); 
    light.position.set(400, 400, 400); 
    scene.add(light); 

    renderer = new THREE.WebGLRenderer(); 
    renderer.setPixelRatio(window.devicePixelRatio); 
    renderer.setSize(CANVAS_WIDTH, CANVAS_HEIGHT); 
    container = document.getElementById('accelgyro_canvas'); 
    container.appendChild(renderer.domElement); 

    window.addEventListener('resize', onWindowResize, false); 
} 
function onWindowResize() { 
    camera.aspect = window.innerWidth/window.innerHeight; 
    camera.updateProjectionMatrix(); 
    renderer.setSize(window.innerWidth, window.innerHeight); 
} 
function animate() { 
console.log('animate') 
    requestAnimationFrame(animate); 
    mesh.rotation.x += 0.005; 
    mesh.rotation.y += 0.01; 
    renderer.render(scene, camera); 
} 

這使得由立方體爲:

enter image description here

我試圖調整光的角度,添加/移除的環境光,改變哪個對象予加FlatShading和似乎沒有任何幫助。

我該如何解決這個問題?

+0

你是在陰影興趣(如在http://threejs.org/examples/#webgl_shadowmap_viewer),或在着色(http://threejs.org/例子/#canvas_materials_normal)?問題的標題與內容不匹配 –

回答

-1

Flat Shading

是你在尋找這樣的結果。

附加的參考代碼相同

 var camera, scene, renderer; 
     var mesh; 

     init(); 
     animate(); 

     function init() { 

      camera = new THREE.PerspectiveCamera(70, window.innerWidth/window.innerHeight, 1, 1000); 
      camera.position.z = 400; 

      scene = new THREE.Scene(); 
      scene.add(new THREE.HemisphereLight(0xffffff,0xffffff,1.0)) 


      var geometry = new THREE.BoxGeometry(200, 200, 200); 
      for (var i = 0; i < geometry.faces.length; i++) 
      { 

       geometry.faces[ i ].color.setRGB(1.0 * Math.random()+0.2 , 0, 0);  
      } 
      var material = new THREE.MeshBasicMaterial({ color:0xff0000,vertexColors: THREE.FaceColors }); 

      mesh = new THREE.Mesh(geometry, material); 
      //mesh.drawMode =THREE.TriangleStripDrawMode; 
      scene.add(mesh); 

      renderer = new THREE.WebGLRenderer(); 
      renderer.setPixelRatio(window.devicePixelRatio); 
      renderer.setSize(window.innerWidth, window.innerHeight); 
      document.body.appendChild(renderer.domElement); 

      // 

      window.addEventListener('resize', onWindowResize, false); 

     } 

     function onWindowResize() { 

      camera.aspect = window.innerWidth/window.innerHeight; 
      camera.updateProjectionMatrix(); 

      renderer.setSize(window.innerWidth, window.innerHeight); 

     } 

     function animate() { 

      requestAnimationFrame(animate); 

      mesh.rotation.x += 0.005; 
      mesh.rotation.y += 0.01; 

      renderer.render(scene, camera); 

     } 
1

MeshBasicMaterial不響應燈。使用MeshLambertMaterial,MeshPhongMaterialMeshStandardMaterial

另外,MeshLambertMaterial沒有shading屬性。如果您需要FlatShading,則需要使用MeshPhongMaterialMeshStandardMaterial

three.js所r.75

+0

有意義,除非我不明白爲什麼此jsfiddle似乎使用MeshBasicMaterial進行着色? http://jsfiddle.net/wilt/w67tzfhx/81/ –