2013-04-10 43 views
-1

我使用threejs与我的colladaLoader,我的模型显示很好,除了环境光或有关的东西有时会使光线变暗..光线进出。用下面的代码,我如何禁用这种行为,并使灯光恒定而明亮?three.js奇特的光行为

car 1 car 2

CODE:(?)

if (! Detector.webgl) Detector.addGetWebGLMessage(); 

var container, stats; 
var camera, scene, renderer, objects; 
var particleLight, pointLight; 
var skin; 

function load_model(el,model_url,type) { 

      window.loader = new THREE.ColladaLoader(); 
      window.loader.options.convertUpAxis = true; 

      window.imageReplace = [{"name":"stock.jpg","new_image":"colors/generated_color.png"}]; 

      window.loader.load(model_url, imageReplace, function (collada) { 
       //console.log(collada); 
       dae = collada.scene; 
       skin = collada.skins[ 0 ]; 

       dae.scale.x = dae.scale.y = dae.scale.z = 0.040; 
       dae.updateMatrix(); 

       //setMaterial(dae, new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture("/media/images/stock.jpg") })); 
       window.init(el); 
       window.animate(); 

      }); 

      window.init = function(el) { 

       container = document.createElement('div'); 
       el.append(container); 

       camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 2000); 
       camera.position.set(2, 2, 3); 

       scene = new THREE.Scene(); 

       // Add the COLLADA 

       scene.add(dae); 

       particleLight = new THREE.Mesh(new THREE.SphereGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff })); 
       scene.add(particleLight); 

       // Lights 

       // scene.add(new THREE.AmbientLight(0xFFFFFF)); 

       var directionalLight = new THREE.DirectionalLight(/*Math.random() * 0xffffff*/0xeeeeee); 
       directionalLight.position.x = Math.random() - 0.5; 
       directionalLight.position.y = Math.random() - 0.5; 
       directionalLight.position.z = Math.random() - 0.5; 
       directionalLight.position.normalize(); 
       scene.add(directionalLight); 

       // pointLight = new THREE.PointLight(0xffffff, 4); 
       // pointLight.position = particleLight.position; 
       // scene.add(pointLight); 

       renderer = new THREE.WebGLRenderer(); 
       if (type =='edit') 
        renderer.setSize(window.innerWidth/2, window.innerHeight/2); 
       else 
        renderer.setSize(window.innerWidth/3, window.innerHeight/3); 

       container.appendChild(renderer.domElement); 

       // stats = new Stats(); 
       // stats.domElement.style.position = 'absolute'; 
       // stats.domElement.style.top = '0px'; 
       // container.appendChild(stats.domElement); 

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

      } 

      function onWindowResize() { 

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

       if (type =='edit') 
        renderer.setSize(window.innerWidth/2, window.innerHeight/2); 
       else 
        renderer.setSize(window.innerWidth/3, window.innerHeight/3); 

      } 

      // 

      var t = 0; 
      var clock = new THREE.Clock(); 

      window.animate = function() { 

       var delta = clock.getDelta(); 

       requestAnimationFrame(animate); 

       if (t > 1) t = 0; 

       if (skin) { 

        // guess this can be done smarter... 

        // (Indeed, there are way more frames than needed and interpolation is not used at all 
        // could be something like - one morph per each skinning pose keyframe, or even less, 
        // animation could be resampled, morphing interpolation handles sparse keyframes quite well. 
        // Simple animation cycles like this look ok with 10-15 frames instead of 100 ;) 

        for (var i = 0; i < skin.morphTargetInfluences.length; i++) { 

         skin.morphTargetInfluences[ i ] = 0; 

        } 

        skin.morphTargetInfluences[ Math.floor(t * 30) ] = 1; 

        t += delta; 

       } 

       window.render(); 
       //stats.update(); 

      } 

      window.render = function() { 

       var timer = Date.now() * 0.0005; 

       camera.position.x = Math.cos(timer) * 10; 
       camera.position.y = 2; 
       camera.position.z = Math.sin(timer) * 10; 

       camera.lookAt(scene.position); 

       particleLight.position.x = Math.sin(timer * 4) * 3009; 
       particleLight.position.y = Math.cos(timer * 5) * 4000; 
       particleLight.position.z = Math.cos(timer * 4) * 3009; 

       renderer.render(scene, camera); 

      } 


} 

回答

0

你的主要光源位置被定义为随机这样的:

  directionalLight.position.x = Math.random() - 0.5; 
      directionalLight.position.y = Math.random() - 0.5; 
      directionalLight.position.z = Math.random() - 0.5; 

只需使用硬编码的,恒定的位置在那里。

也可以使用环境光线,根本不需要位置。如果你愿意,你可以从代码中取消注释。

+0

如果我从我的代码中取消注释环境光线并评论方向灯,它只是保持太亮而且汽车的某些部分变得不可见。我只是想看看我的模型是这样的...不需要任何灯光效果..我只是不知道该怎么做..如果我尝试了另一种固定不变位置的方法,它会变得更好,但仍然有一些黑斑(也许是因为我不明白哪个是正确的值) – psychok7 2013-04-11 10:34:23

+0

如果我做var directionalLight = new THREE.DirectionalLight(0xffffff); scene.add(DirectionalLight的);是最接近我想要的。只需要它更亮一点 – psychok7 2013-04-11 10:44:29

+0

您可以在创建灯光时用第二个参数控制方向光强度:'new THREE.DirectionalLight(0xffffff,2.0);'默认强度为1.0。您还可以通过将颜色更改为某种灰度而不是白色来更改方向和环境光强度。 (例子:0x333333差不多黑/ 0x666666/0x999999/0xcccccc/0xeeeeee差不多白) – yaku 2013-04-11 10:54:04