2013-02-11 60 views
0

我對three.js相當陌生。我用r55把幾件東西放在一起,並想添加燈光。根據Mrdoob的文檔,我一直試圖添加DirectionalLight - 沒有運氣...我嘗試添加的燈光沒有出現 - 添加了shadowCameraVisible進行調試,但它不會顯示出來......任何人都知道什麼是什麼我的代碼可能是錯誤的?我感謝任何輸入!directionallight three js

function init() { 
    container = document.createElement('div'); 
    document.body.appendChild(container); 

    scene = new THREE.Scene(); 
    camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); 

    renderer = new THREE.WebGLRenderer(); 
    renderer.setSize(window.innerWidth, window.innerHeight); 
    container.appendChild(renderer.domElement); 

    //CUBE 
    var material = new THREE.MeshBasicMaterial({ 
     color: 0xff0000 
    }); 
    var geometry = new THREE.CubeGeometry(x, y, z); 
    cube = new THREE.Mesh(geometry, material); 
    cube.position.z = z/2; 
    scene.add(cube); 

    //FLOOR 
    //floor color: 
    var floorMaterial = new THREE.MeshBasicMaterial({ 
     color: 0xcccccc 
    }); 
    //floor size: 
    var floorGeometry = new THREE.PlaneGeometry(20, 20, 1, 1); 
    var floor = new THREE.Mesh(floorGeometry, floorMaterial); 
    floor.position.y = -0.5; 
    floor.doubleSided = true; 
    scene.add(floor); 

    //LIGHTS 
    var dLight = new THREE.DirectionalLight(0xffffff); 
    dLight.position.set = (0, 0, 1); 
    dLight.shadowCameraVisible = true; 
    dLight.shadowCameraNear = 1; 
    dLight.shadowCameraFar = 150; 
    dLight.castshadow = true; 
    scene.add(dLight); 

    //CAMERA POSITION 
    camera.position.z = 50; 
    controls = new THREE.TrackballControls(camera); 

    renderer.shadowMapEnabled = true; 
    renderer.shadowMapType = THREE.PCFShadowMap; 

} 

回答

0
dLight.position.set = (0,0,1); 

糟糕,不看的權利......這開始運行,該光源的位置將不會是一個三維向量,這將產生從與光源的位置做任何數學NaN

dLight.position.set(0,0,1); 
// or 
dLight.position = new THREE.Vector3(0,0,1); 
+0

確實,這看起來不正確......但是,改變之後,沒有燈光顯示出來......也許是渲染器設置的東西? – NGA 2013-02-11 20:28:53

1

您可以設置通過

light.position.set (0,0,1); 

光明,但我覺得你光是幾何內。 看看http://jsfiddle.net/aSt8c/。它有一個帶陰影的工作方向燈。

+0

謝謝!我看着小提琴,也發現了這一個http://jsfiddle.net/Rhtr2/58/,但即使改變了周圍的位置,我似乎也不能使燈光顯示出來...... – NGA 2013-02-12 07:24:10

0

重點是,您的對象的材質必須是MeshLambertMaterial或MeshPhongMaterial。其他材料無法反射光線。

//CUBE var material = new THREE.MeshPhongMaterial({ color: 0xff0000 }); OR var material = new THREE.MeshLambertMaterial({ color: 0xff0000 });

MeshPhongMaterial可用於有光澤,MeshLambertMaterial非發亮(朗伯)表面。