首页 > 解决方案 > 带助手的threejs中的定向/聚光灯

问题描述

我正在按照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)

我以完美的方式看到了灯光的位置,当添加灯光时,助手消失(在其他灯光下没有发生)并且灯光按照她的意愿移动。

有人可以帮助我吗?

标签: javascriptthree.js

解决方案


助手必须是场景的父级(或至少 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>


推荐阅读