Three.js光源、阴影

几何本身的阴影效果

首先我们要确认几何的材质是否能产生阴影、比如基础网格材质(MeshBasicMaterial)没有阴影效果,建议使用Lambert网格材质(MeshLambertMaterial)

在three.js添加光源和阴影,我们需要先创建一个聚光灯,可以理解为一个小太阳

//创建一个聚光灯
var spotLight = new THREE.SpotLight(0xFFFFFF);

然后我们需要设置小太阳的位置、设置 小太阳是否能产生阴影、小太阳产生阴影的精细程度、阴影的投射远点和投射近点在后在场景中添加小太阳

//设置小太阳的位置 
 spotLight.position.set(-50, 50, 20);
//设置小太阳是否能产生阴影
  spotLight.castShadow = true;
//设置小太阳的阴影精细程度
  spotLight.shadow.mapSize = new THREE.Vector2(5000, 5000);
//设置物体阴影的远点
  spotLight.shadow.camera.far = 120;
//设置物体阴影的近点
  spotLight.shadow.camera.near = 40;

  scene.add(spotLight);

我们还可以通过设置AmbientLight来生成环境光

环境光会均匀的照亮场景中的所有物体。环境光不能用来投射阴影,因为它没有方向。

//创建一个环境光并添加到场景中  
var ambienLight = new THREE.AmbientLight(0x353535);
scene.add(ambienLight);

然后我们需要让第一节我们所创建的每一个几何物体都会生成阴影

 
//开启立方体、圆、平面开启阴影效果
 cube.castShadow = true;
 sphere.castShadow = true;
 plane.receiveShadow = true;

由于渲染阴影需要耗费大量的计算资源,所以渲染器默认是不渲染的,我们需要手动开启renderer的阴影效果

//开启renderer的阴影效果  
renderer.shadowMap.enabled = true;

全部代码

function init() {
  // create a scene, that will hold all our elements such as objects, cameras and lights.
  var scene = new THREE.Scene();
  const axesHelper = new THREE.AxesHelper( 200 );
  scene.add( axesHelper );
  // create a camera, which defines where we're looking at.
  var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

  // create a render and configure it with shadows
  var renderer = new THREE.WebGLRenderer();
  renderer.setClearColor(new THREE.Color(0x000000));
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.shadowMap.enabled = true;

   // create a cube
  var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
  var cubeMaterial = new THREE.MeshLambertMaterial({
    color: 0xFF0000
  });
  var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  cube.castShadow = true;

  // position the cube
  cube.position.x = -4;
  cube.position.y = 2;
  cube.position.z = 0;

  // add the cube to the scene

  var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
  var sphereMaterial = new THREE.MeshLambertMaterial({
    color: 0x7777ff
  });
  var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);

  // position the sphere
  sphere.position.x = 20;
  sphere.position.y = 4;
  sphere.position.z = 2;
  sphere.castShadow = true;

  // create the ground plane
  var planeGeometry = new THREE.PlaneGeometry(60, 20);
  var planeMaterial = new THREE.MeshLambertMaterial({
    color: 0xAAAAAA
  });
  var plane = new THREE.Mesh(planeGeometry, planeMaterial);

  // rotate and position the plane
  plane.rotation.x = -0.5 * Math.PI;
  plane.position.set(15, 0, 0);
  plane.receiveShadow = true;

  // add the objects
  scene.add(cube);
  scene.add(sphere);
  scene.add(plane);

  // position and point the camera to the center of the scene
  camera.position.x = 0;
  camera.position.y = 70;
  camera.position.z = 20;
  camera.lookAt(scene.position);

  // add spotlight for the shadows
  var spotLight = new THREE.SpotLight(0xFFFFFF);
  spotLight.position.set(-50, 50, 20);
  spotLight.castShadow = true;
  spotLight.shadow.mapSize = new THREE.Vector2(5000, 5000);
  spotLight.shadow.camera.far = 120;
  spotLight.shadow.camera.near = 40;
  scene.add(spotLight);


  var ambienLight = new THREE.AmbientLight(0x353535);
  scene.add(ambienLight);

  // add the output of the renderer to the html element
  document.getElementById("webgl-output").appendChild(renderer.domElement);

  // call the render function
  renderer.render(scene, camera);
}

猜你喜欢

转载自blog.csdn.net/shmilynn_/article/details/131264784