three.js - Animations

2023-09-21  本文已影响0人  闪电西兰花
import * as THREE from "three"

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

// Cube
const geometry = new THREE.BoxGeometry(1,1,1) // 1表示一个单位
const material = new THREE.MeshBasicMaterial({
  color: '#ff0000'
})
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)

// Sizes
const sizes = {
  width: 800,
  height: 600
}

// Camera
// 垂直视野,纵横比
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height) 
// 修改camera位置,默认所有东西都在场景中心,也就是修改位置前camera位于立方体的内部中心
camera.position.set(0, 0, 10)
scene.add(camera) 

// Render
const renderer = new THREE.WebGLRenderer({})
renderer.setSize(sizes.width, sizes.height)
document.body.appendChild(renderer.domElement)


// Animations
const tick = () => {
  // Update objects
  mesh.rotation.y += 0.01
  // mesh.position.x += 0.01

  // Render
  renderer.render(scene, camera)

  window.requestAnimationFrame(tick) // 仅传递方法,不调用,会在下一帧调用
}
tick()
// Time
let time = Date.now()

// Animations
const tick = () => {
  // Time
  const currentTime = Date.now()
  const deltaTime = currentTime - time // 帧间隔时长,单位毫秒,高刷新率的时长更短
  time = currentTime
  // console.log('deltaTime',deltaTime)

  // Update objects
  // mesh.position.x += 0.001 * deltaTime
  mesh.rotation.y += 0.001 * deltaTime

  // Render
  renderer.render(scene, camera)

  window.requestAnimationFrame(tick) // 仅传递方法,不调用,会在下一帧调用
}
tick()
// Clock
const clock = new THREE.Clock() // 启动Clock

// Animations
const tick = () => {
  // Clock
  let elapsedTime = clock.getElapsedTime() // 获取自Clock启动后的秒数
  // console.log('elapsedTime',elapsedTime)

  // Update objects
  // mesh.rotation.y = elapsedTime

  // mesh.rotation.y = elapsedTime * Math.PI * 2 // 每秒转一圈

  // mesh.position.y = Math.sin(elapsedTime)
  // mesh.position.x = Math.cos(elapsedTime)

  // camera.position.y = Math.sin(elapsedTime)
  // camera.position.x = Math.cos(elapsedTime)
  // camera.lookAt(mesh.position)

  // Render
  renderer.render(scene, camera)

  window.requestAnimationFrame(tick) // 仅传递方法,不调用,会在下一帧调用
}
tick()
上一篇 下一篇

猜你喜欢

热点阅读