美文网首页Three.js学习
Three.js实现光照阴影

Three.js实现光照阴影

作者: travelclover | 来源:发表于2020-03-23 17:52 被阅读0次

Three.js实现光照阴影

在Three.js中,物体可以形成阴影投影效果,但是由于渲染阴影需要消耗计算机大量资源,所以Three.js在默认情况下是不会渲染阴影的。 为了实现渲染阴影效果,我们还需要完成以下四个部分的设置。

1. renderer设置

首先我们需要告诉渲染器我们需要阴影效果:

renderer.shadowMap.enabled = true;

更多详细内容可以查看WebGLRenderershadowMap属性。

2. 光源设置

然后我们需要定义能够产生阴影的光源:

light.castShadow = true;

注意: 不是所有的光源都能够产生阴影,只有一部分光源可以,例如通过THREE.PointLight(点光源)、THREE.SpotLight(聚光源)和THREE.DirectionalLight(平行光光源)定义的光源是能够产生阴影的。

3. 指定物体投射阴影

接下来我们需要指定场景中的哪些物体能够投射阴影,能够产生阴影的物体需要设置以下代码:

const geometry = new THREE.BoxBufferGeometry(4, 4, 4); // 生成几何体
const material = new THREE.MeshLambertMaterial({ // 生成材质
    color: 0x00ff00,
});
const mesh = new THREE.Mesh(geometry, material); // 生成网格
mesh.castShadow = true; // 对象是否渲染到阴影贴图中,默认值为false

注意: 只有castShadow属性为true的物体才会产生阴影。

4. 指定物体接受阴影

最后我们还需要指定哪些物体能够接受阴影,这样才能够看出阴影效果。

const planeGeometry = new THREE.PlaneGeometry(300, 300); // 生成平面几何
const planeMaterial = new THREE.MeshLambertMaterial({ // 生成材质
  color: 0xcccccc,
});
const planeMesh = new THREE.Mesh(planeGeometry, planeMaterial); // 生成平面网格
planeMesh.receiveShadow = true; // 设置平面网格为接受阴影的投影面

注意: 只有receiveShadow属性为true的物体才能够接受阴影。


全部代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Three.js实现光照阴影</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <script type="module">
      import * as THREE from 'https://threejs.org/build/three.module.js';
      import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
      import { OrbitControls } from 'https://threejs.org/examples/jsm/controls/OrbitControls.js';

      let stats = new Stats(); // 性能监控器,用来查看Three.js渲染帧率

      // 创建div
      let container = document.createElement('div');
      document.body.appendChild(container);

      // 创建场景
      let scene = new THREE.Scene();

      // 创建相机
      let camera = new THREE.PerspectiveCamera( // 透视投影相机
        40, // 视场,表示能够看到的角度范围
        window.innerWidth / window.innerHeight, // 渲染窗口的长宽比,设置为浏览器窗口的长宽比
        0.1, // 从距离相机多远的位置开始渲染
        2000 // 距离相机多远的位置截止渲染
      );
      camera.position.set(-10, 10, 40); // 设置相机位置

      // 创建渲染器
      let renderer = new THREE.WebGLRenderer({
        antialias: true, // 是否执行抗锯齿
      });
      renderer.setPixelRatio(window.devicePixelRatio); // 设置设备像素比率。通常用于HiDPI设备,以防止输出画布模糊。
      renderer.setSize(window.innerWidth, window.innerHeight); // 设置渲染器大小
      renderer.shadowMap.enabled = true;
      container.appendChild(renderer.domElement);

      // 创建控制器
      let controls = new OrbitControls(camera, renderer.domElement);

      // 创建物体
      const geometry = new THREE.BoxBufferGeometry(4, 4, 4); // 生成几何体
      const material = new THREE.MeshLambertMaterial({
        // 生成材质
        color: 0x00ff00,
      });
      const mesh = new THREE.Mesh(geometry, material); // 生成网格
      mesh.castShadow = true; // 对象是否渲染到阴影贴图中,默认值为false
      mesh.position.set(0, 3, 0); // 设置物体位置
      scene.add(mesh); // 添加到场景中

      // 创建平面
      const planeGeometry = new THREE.PlaneGeometry(300, 300); // 生成平面几何
      const planeMaterial = new THREE.MeshLambertMaterial({
        // 生成材质
        color: 0xcccccc,
      });
      const planeMesh = new THREE.Mesh(planeGeometry, planeMaterial); // 生成平面网格
      planeMesh.receiveShadow = true; // 设置平面网格为接受阴影的投影面
      planeMesh.rotation.x = -Math.PI / 2; //绕X轴旋转90度
      scene.add(planeMesh); // 添加到场景中

      // 创建平行光源
      const light = new THREE.DirectionalLight(0xffffff, 1); // 平行光,颜色为白色,强度为1
      light.position.set(-40, 40, 20); // 设置灯源位置
      light.castShadow = true; // 允许生成阴影
      light.target = mesh;
      scene.add(light); // 添加到场景中

      animate();

      function animate() {
        requestAnimationFrame(animate);
        stats.begin();
        renderer.render(scene, camera);
        stats.end();
      }
    </script>
  </body>
</html>

相关文章

  • Three.js实现光照阴影

    Three.js实现光照阴影 在Three.js中,物体可以形成阴影投影效果,但是由于渲染阴影需要消耗计算机大量资...

  • 光照阴影

    1、摄像机离植被太远可能会导致阴影丢失。会有一层特别明显的分割线。并不好看。指令中输入 r.Raytracing....

  • Unity无光照假阴影Shader实现及常见问题总结

    效果可点击放大查看 游戏实现阴影的常见处理方式 (动态人或物,非烘焙) 1.实时光照实时光照属于真阴影,一般来说效...

  • 光照和阴影

    静态光照属于预渲染的部分 LightMass负责预先计算的光照,将结果储存在光照贴图中。可以使用LightMass...

  • [Unity] 使用Unity进行增强现实中的光照和阴影的渲染

    我们曾经为大家介绍过Unity中光照和阴影有关的内容,比如Unity实时阴影实现图解。近几年,增强现实应用开发者越...

  • 体积的使用 阴影控制

    简介: Unity HDRP 通过体积框架来实现了一些光照和阴影的控制。我们这里先介绍一下能对几个阴影的控制功能进...

  • 简单流程图

    区域光配合光照强度,可以制作不同的阴影。 同样的光照效果下,区域光越大,所需光照强度越低,阴影发虚。 ...

  • Three.js 实现俄罗斯方块

    最近学了一段时间three.js ,想到之前一直没能实现的俄罗斯方块,于是用three.js想办法实现了,其中图形...

  • shader光照和阴影

    一.标准光照模型OpenGL与Direct3D提供了几乎相同的固定功能光照模型。什么是固定功能光照模型?在过去只有...

  • WebGL光照阴影映射

      原文地址:WebGL光照阴影映射  经过前面的学习,webgl的基本功能都已经掌握了,我们不仅掌握了着色器的编...

网友评论

    本文标题:Three.js实现光照阴影

    本文链接:https://www.haomeiwen.com/subject/klpwyhtx.html