three.js - Shadow

2024-01-07  本文已影响0人  闪电西兰花
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
// 导入gui
import * as dat from 'dat.gui'

/*
  * scene
*/
const scene = new THREE.Scene()

/*
  * Objects
*/
// MeshStandardMaterial
const material = new THREE.MeshStandardMaterial()
material.roughness = 0.4

// 球体
const sphere = new THREE.Mesh(
  new THREE.SphereGeometry(0.5, 32, 32),
  material
)

// 平面
const plane = new THREE.Mesh(
  new THREE.PlaneGeometry(5, 5),
  material
)
plane.rotation.x = - Math.PI * 0.5
plane.position.y = - 0.65

scene.add(sphere, plane)

/*
  * size
*/
const sizes = {
  width: window.innerWidth,
  height: window.innerHeight
}

window.addEventListener('resize', () => {
  sizes.width = window.innerWidth
  sizes.height = window.innerHeight
})

/*
  * camera
*/
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 1, 1000) 
camera.position.z = 3
scene.add(camera) 

/*
  * render
*/
const renderer = new THREE.WebGLRenderer()
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio))
renderer.shadowMap.enabled = true
document.body.appendChild(renderer.domElement)

/*
  * controls
*/
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

/*
  *Animations
*/
const clock = new THREE.Clock()
const tick = () => {
  let elapsedTime = clock.getElapsedTime()
  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(tick)
}
tick()
/*
  * Lights
*/
// AmbientLight
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
scene.add(ambientLight)

// DirectionalLight
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
directionalLight.position.set(2, 2, -1)
scene.add(directionalLight)
  /**
   * 3. 创建渲染器
  **/
  ...
  ...
  renderer.shadowMap.enabled = true  // 开启场景中的阴影贴图

  /*
    * 4. 创建几何体
  */
  ...
  ...
  sphere.castShadow = true  // sphere 投射阴影
  ...
  ...
  plane.receiveShadow = true    // 平面接收阴影
/*
  * Lights
*/
...
...
// DirectionalLight
...
directionalLight.castShadow = true

directionalLight.shadow.mapSize.set(1024, 1024) // 阴影贴图分辨率,值为2的幂次方,默认512 * 512

// 观察光源的相机对象,从光的角度来看
directionalLight.shadow.camera.near = 1 
directionalLight.shadow.camera.far = 6
directionalLight.shadow.camera.top = 5
directionalLight.shadow.camera.bottom = -5
directionalLight.shadow.camera.left = -5
directionalLight.shadow.camera.right = 5

directionalLight.shadow.radius = 10 // 阴影边缘模糊度
  // SpotLight (与上面的DirectionalLight可对比观察)
  const spotLight = new THREE.SpotLight(0x78ff00, 1, 10, Math.PI * 0.1, 0.25, 1)
  spotLight.position.set(1.5, 1.5, 1.5);
  spotLight.castShadow = true
  spotLight.target = sphere // 聚光灯目标,移动物体时,灯光跟随物体
  spotLight.angle = Math.PI / 6 // 聚光灯角度,一般不超过90°
  spotLight.distance = 5 // 聚光灯光源发出的距离
  spotLight.penumbra = 0.5 // 聚光灯半影衰减,数值越接近1时,灯光边缘越模糊

  // 阴影边缘模糊度 
  spotLight.shadow.radius = 20
  
  // 阴影贴图分辨率
  spotLight.shadow.mapSize.set(4096, 4096)
const directionalLightCameraHelper = new THREE.CameraHelper(directionalLight.shadow.camera)
scene.add(directionalLightCameraHelper)
image.png
上一篇下一篇

猜你喜欢

热点阅读