three.js - Transform objects

2023-09-02  本文已影响0人  闪电西兰花
关于物体位移、旋转、缩放等属性可参考此文档 Object3D
// Cube
const geometry = new THREE.BoxGeometry(1,1,1) // 1表示一个单位
const material = new THREE.MeshBasicMaterial({
  color: '#ff0000'
})
const mesh = new THREE.Mesh(geometry, material)

// 修改position写法1:
mesh.position.x = 0.7
mesh.position.y = -0.6
mesh.position.z = 8

// 修改position写法2:
// mesh.position.set(0.7,-0.6,8)

scene.add(mesh)

// 4. mesh.position.normalize() // 转换为单位向量

// 1. 场景中心(0,0,0)和物体之间的距离
// normalize后场景中心与物体间的距离为1
console.log(mesh.position.length())

// 2. 物体至某向量之间的距离,distanceTo方法接受一个三维向量
console.log(mesh.position.distanceTo(new THREE.Vector3(0.7,-0.6,8)))

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

// Camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height) 
camera.position.set(0, 0, 10)
scene.add(camera) 

// 3. 物体与camera之间的距离
console.log(mesh.position.distanceTo(camera.position))
  // 创建几何体(父子共用)
  const geometry = new THREE.BoxGeometry(1, 1, 1) 

  // 子几何体
  const material = new THREE.MeshBasicMaterial({ 
    color: 0x00ff00
  })
  const cube = new THREE.Mesh(geometry, material) 
  cube.position.x = 3 // 修改子几何体位置

  // 父几何体
  const parentMaterial = new THREE.MeshBasicMaterial({ 
    color: 0xff0000
  })
  const parentCube = new THREE.Mesh(geometry, parentMaterial)
  parentCube.add(cube) // 将几何体添加进父几何体
  parentCube.position.set(-3, 0 ,0) // 修改父几何体位置
  
  // 将父几何体添加进场景
  scene.add(parentCube) 
// Axes helper
const axesHelper = new THREE.AxesHelper(6) // 坐标轴线段长度
scene.add(axesHelper)
// 修改scale写法1:
// mesh.scale.x = 1.4
// mesh.scale.y = 0.5
// mesh.scale.z = 2

// 修改scale写法2:
mesh.scale.set(1.4, 0.5, 2)
cube.scale.set(2, 2, 2) // 子几何体
parentCube.scale.set(2, 2, 2) // 父几何体
// 默认旋转顺序:XYZ

// 修改rotation写法1:
// mesh.rotation.reorder('YXZ')
// mesh.rotation.x = Math.PI / 4
// mesh.rotation.y = Math.PI / 4

// 修改rotation写法2:
mesh.rotation.set(Math.PI / 4, Math.PI / 4, 0, 'YXZ')
  cube.rotation.x = Math.PI / 4 // 子元素
  parentCube.rotation.x = Math.PI / 4 // 父元素
camera.lookAt(mesh.position) // 相机看向物体
// camera.lookAt(new THREE.Vector3(0,1,0))
import * as THREE from "three"

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

// group
const group = new THREE.Group()
group.position.y = 1
group.scale.x = 2
group.rotation.x = Math.PI / 4
scene.add(group)

const cube1 = new THREE.Mesh(
  new THREE.BoxGeometry(1,1,1),
  new THREE.MeshBasicMaterial({color: '#ff0000'})
)
cube1.position.x = 4

group.add(cube1)

const cube2 = new THREE.Mesh(
  new THREE.BoxGeometry(1,1,1),
  new THREE.MeshBasicMaterial({color: '#00ff00'})
)
cube2.position.set(0,4,0)

group.add(cube2)

const cube3 = new THREE.Mesh(
  new THREE.BoxGeometry(1,1,1),
  new THREE.MeshBasicMaterial({color: '#0000ff'})
)
cube3.position.set(0,0,4)

group.add(cube3)

// Axes helper
const axesHelper = new THREE.AxesHelper(6) // 坐标轴长度
scene.add(axesHelper)

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

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

// 物体与camera之间的距离
// console.log(mesh.position.distanceTo(camera.position))

// Render
const renderer = new THREE.WebGLRenderer({})
renderer.setSize(sizes.width, sizes.height)
document.body.appendChild(renderer.domElement)
renderer.render(scene, camera)
上一篇下一篇

猜你喜欢

热点阅读