Three.js-阴影投射异常

Three.js - anomaly with shadow casting

本文关键字:异常 阴影 js- Three      更新时间:2023-09-26

我有这个代码:

  var other_phongMaterial   = new THREE.MeshPhongMaterial({ color: 0xf455f4 });      
  var other_sphere = new THREE.Mesh(new THREE.SphereGeometry(50, 50, 50), other_phongMaterial );
  var other_cube = new THREE.Mesh(new THREE.CubeGeometry(200, 200, 200), other_phongMaterial );
  var phongMaterial = new THREE.MeshPhongMaterial({ color: 0xcccccc });
  var sphere = new THREE.Mesh(new THREE.SphereGeometry(50, 50, 50), phongMaterial );
  var cube = new THREE.Mesh(new THREE.CubeGeometry(200, 200, 200), phongMaterial );

  sphere.castShadow = true;
  cube.receiveShadow = true;
  other_sphere.castShadow = true;
  other_cube.receiveShadow = true;
  sphere.position.set(0,250,-130);
  other_cube.position.set(0,50,-150);
  other_sphere.position.set(0,250,130);
  cube.position.set(0,50,150);
  scene.add(cube);
  scene.add(sphere);
  scene.add(other_sphere);
  scene.add(other_cube);    
  var spotLight = new THREE.SpotLight( 0xffffff );
  spotLight.position.set( 100, 1000, 100 );
  spotLight.castShadow = true;
  spotLight.shadowMapWidth = 1024;
  spotLight.shadowMapHeight = 1024;
  spotLight.shadowCameraNear = 500;
  spotLight.shadowCameraFar = 4000;
  spotLight.shadowCameraFov = 30;
  scene.add( spotLight );

而且,我不明白为什么阴影投射只对一个有效。我注意到以下几点:
-如果我尝试交换立方体,阴影会投射到其中一个上,但仍然不会投射到另一个上
-如果我使用与other_cube相同的材料,它会起作用,这意味着正如现在所写的那样,它对我有效,相反,如果我使用两种不同的材料,那么它只会投射在other_cubes上
但是这些形状的设置是完全相同的!所以…我想不通,真的。。提前感谢!

编辑:可能还有其他我不记得放的异常,所以请告诉我每种情况。

也许你的另一个立方体不在阴影相机截锥体内。您应该能够通过设置spotLight.shadowCameraVisible = true;来查看阴影摄影机的尺寸。只有该圆锥体内的对象才会计算阴影。然后移动立方体或相应地调整阴影摄影机属性。阴影可能有点棘手,问题可能完全是另一回事。但我会先检查一下。