具有辅助功能的Three.js中的定向/聚光灯

时间:2019-06-08 08:22:24

标签: javascript three.js

我遵循本指南Light in Threejs 我已经在场景中添加了一些灯光。

现在,我尝试为游戏的角色添加一个亮点,但仍然无法正常工作。 我使用指南的相同代码,只是更改了position.set

 const color = 0xFFFFFF;
 const intensity = 1;

 const light2 = new THREE.SpotLight(color, intensity,0,Math.PI/3);
 light2.position.set(100,-5000,1000);

 light2.target = lightTarget;
 light2.castShadow = true;
 const helper2 = new THREE.SpotLightHelper(light2);

然后我以这种方式添加到角色

       self.flame.add( helper2 );
       self.flame.add(lightTarget);
       self.flame.add(light2);

我也添加了一个帮助器,但是如果我只在场景中使用该帮助器,那么如果添加注释

  self.flame.add(light2)

我以完美的方式看到了灯光的位置,添加灯光时,助手会消失(在其他灯光下则不会发生),灯光会按照自己的意愿移动。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

帮助程序必须作为场景的父项(或者至少是SpotLightHelper如此)。您可能希望也可能不想将目标作为场景的父对象。

您还需要为每个助手呼叫helper.update

'use strict';

/* global THREE, dat */

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({canvas});

  const fov = 45;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 100;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.set(0, 10, 20);

  const controls = new THREE.OrbitControls(camera, canvas);
  controls.target.set(0, 5, 0);
  controls.update();

  const scene = new THREE.Scene();
  scene.background = new THREE.Color('black');

  {
    const planeSize = 40;

    const loader = new THREE.TextureLoader();
    const texture = loader.load('https://threejsfundamentals.org/threejs/resources/images/checker.png');
    texture.wrapS = THREE.RepeatWrapping;
    texture.wrapT = THREE.RepeatWrapping;
    texture.magFilter = THREE.NearestFilter;
    const repeats = planeSize / 2;
    texture.repeat.set(repeats, repeats);

    const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
    const planeMat = new THREE.MeshPhongMaterial({
      map: texture,
      side: THREE.DoubleSide,
    });
    const mesh = new THREE.Mesh(planeGeo, planeMat);
    mesh.rotation.x = Math.PI * -.5;
    scene.add(mesh);
  }
  const cubes = [];
  let parent = scene;
  {
    const cubeSize = 1;
    const cubeGeo = new THREE.BoxBufferGeometry(cubeSize, cubeSize, cubeSize);
    const cubeMat = new THREE.MeshPhongMaterial({
     color: '#8AC',
     emissive: '#333',
    });
    for (let i = 0; i < 6; ++i) {
      const mesh = new THREE.Mesh(cubeGeo, cubeMat);
      mesh.position.set(1, 0, 0);
      parent.add(mesh);
      cubes.push(mesh);
      parent = mesh;
    }
  }
  cubes[0].position.set(-3, 7, 0);
  
  const color = 0xFFFFFF;
  const intensity = 1;
  const light = new THREE.SpotLight(color, intensity);
  light.position.set(0, 0, 0);
  light.target.position.set(0, -1, 0);
  parent.add(light);
  parent.add(light.target);
  //scene.add(light.target);

  const helper = new THREE.SpotLightHelper(light);
  scene.add(helper);

  function resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      renderer.setSize(width, height, false);
    }
    return needResize;
  }

  function render(time) {
    time *= 0.001;

    if (resizeRendererToDisplaySize(renderer)) {
      const canvas = renderer.domElement;
      camera.aspect = canvas.clientWidth / canvas.clientHeight;
      camera.updateProjectionMatrix();
    }
    
    for (const cube of cubes) {
      cube.rotation.z = Math.sin(time) * .4;
    }
    light.angle = THREE.Math.lerp(
       THREE.Math.degToRad(20), 
       THREE.Math.degToRad(80),
       Math.sin(time * 0.77) * 0.5 + 0.5);
    
    helper.update();
    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
}

main();
html, body {
  margin: 0;
  height: 100%;
}
#c {
  width: 100%;
  height: 100%;
  display: block;
}
<canvas id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r105/three.min.js"></script>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r105/js/controls/OrbitControls.js"></script>