three.js光照与阴影

在这里插入图片描述

<template>
  <div id="webgl"></div>
</template>

<script setup>
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

const scene = new THREE.Scene();

// 目标:灯光与阴影
// 1、材质要满足能够对光照有反应
// 2、设置渲染器开启阴影的计算 renderer.shadowMap.enabled = true
// 3、设置光照投射阴影 directionalLight.castShadow = true
// 4、设置物体投射阴影 sphere.castShadow = true
// 5、设置物体接收阴影 plane.receiveShadow = true

const sphereGemometry = new THREE.SphereGeometry(1, 20, 20) // 球体
const material = new THREE.MeshStandardMaterial()
const sphere = new THREE.Mesh(sphereGemometry, material)
sphere.castShadow = true
scene.add(sphere)

// 创建平面
const planeGeometry = new THREE.PlaneGeometry(10, 10)
const plane = new THREE.Mesh(planeGeometry, material)
plane.position.set(0, -1, 0)
plane.rotation.x = -Math.PI / 2
// 接收阴影
plane.receiveShadow = true
scene.add(plane)

// 灯光
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.9)
directionalLight.position.set(5, 5, 5)
directionalLight.castShadow = true
scene.add(directionalLight)

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 10);
// scene.add(camera);

const axesHelper = new THREE.AxesHelper(5); // 长度
scene.add(axesHelper);

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight); //设置three.js渲染区域的尺寸(像素px)
renderer.shadowMap.enabled = true
document.body.appendChild(renderer.domElement);
renderer.render(scene, camera);

// 相机围绕目标进行轨道运动
const controls = new OrbitControls(camera, renderer.domElement);

function render(time) {
  renderer.render(scene, camera);
  requestAnimationFrame(render);
}
render()
</script>

相关推荐

  1. 第六章 Three.js 光照

    2024-05-14 08:38:10       27 阅读
  2. 探索光影魔法:WebKit中的CSS文本阴影效果

    2024-05-14 08:38:10       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-14 08:38:10       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-14 08:38:10       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-14 08:38:10       87 阅读
  4. Python语言-面向对象

    2024-05-14 08:38:10       96 阅读

热门阅读

  1. Elasticsearch映射定义

    2024-05-14 08:38:10       31 阅读
  2. RSA非对称加密解密,前端公钥加密后端私钥解密

    2024-05-14 08:38:10       31 阅读
  3. Spring Cloud 架构技术版本选型

    2024-05-14 08:38:10       37 阅读
  4. 优化运行效率

    2024-05-14 08:38:10       32 阅读
  5. Git检测和处理版本冲突的原理

    2024-05-14 08:38:10       33 阅读
  6. Linux 里的tmp目录

    2024-05-14 08:38:10       27 阅读
  7. echo服务器--聊天版

    2024-05-14 08:38:10       34 阅读
  8. vue项目中vue.config.js中配置webpack详解

    2024-05-14 08:38:10       31 阅读
  9. web安全学习笔记(14)

    2024-05-14 08:38:10       36 阅读