hjin-me / blog

This is my blog
http://huangj.in
Other
9 stars 2 forks source link

ThreeJS 使用总结 FAQ #16

Open hjin-me opened 7 years ago

hjin-me commented 7 years ago

如何获取 global position,而不是 local position

parent.updateMatrixWorld(false)
let vector = new Vector3()
vector.setFromMatrixPosition(child.matrixWorld)

https://stackoverflow.com/questions/15098479/how-to-get-the-global-world-position-of-a-child-object

空间坐标基于透视镜头转换为屏幕坐标

  let v = new Vector3(x, y, z)
  let vp = v.project(camera)
  return {
    x: (vp.x + 1) * viewWidth / 2,
    y: (-vp.y + 1) * viewHeight / 2,
  }

https://stackoverflow.com/questions/27409074/converting-3d-position-to-2d-screen-position-r69

屏幕坐标基于透视镜头转换为空间坐标

function screenToZ(camera: Camera, width: number, height: number, left: number, top: number, targetZ: number = 0): Vector3 {
  let x = (left / width) * 2 - 1
    , y = -(top / height) * 2 + 1
    , z = -1 // -1透视镜头近面,1透视镜头远面
  let v = new Vector3(x, y, z).unproject(camera)
  v.x = (camera.position.z - targetZ) / (camera.position.z - v.z) * v.x
  v.y = (camera.position.z - targetZ) / (camera.position.z - v.z) * v.y
  v.z = targetZ
  return v
}