BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

Matrix4.lookAt()

Matrix4.lookAt()是three.js中用于创建视图矩阵的方法之一。该方法主要用于将一个3D物体定位到摄像机中心位置,并使其朝向摄像机的方向。

方法原型

Matrix4.lookAt(eye, target, up = new Vector3(0, 1, 0))

参数说明

  • eye:Vector3类型,表示摄像机的位置(即视点)。
  • target:Vector3类型,表示摄像机所要对准的点(即视线方向)。
  • up:Vector3类型,表示摄像机上方的方向。默认值为(0, 1, 0)

返回值

返回一个Matrix4类型的视图矩阵,用于将3D物体定位到摄像机中心位置。

示例

// 创建一个摄像机
const camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

// 将摄像机定位到(0, 0, 5),对准原点(0, 0, 0),上方朝向正Y轴
camera.matrix.lookAt(
    new Vector3(0, 0, 5),
    new Vector3(),
    new Vector3(0, 1, 0)
);

注意事项

  • lookAt()方法的调用者是Matrix4实例对象,而非Vector3实例对象。
  • 摄像机的位置、对准点和上方方向值均为Vector3类型。
  • 在three.js中,一般使用camera.matrix.lookAt()方法对摄像机进行定位,而不是直接调用camera.lookAt()方法。后者是基于欧拉角计算的,可能不够精确,而前者是基于矩阵计算的,更加准确。
  • up参数的默认值为(0, 1, 0),即默认的向上方向为正Y轴,因为在three.js中,大多数场景中都是以Y轴为垂直方向的,因此这个默认值可适用于绝大多数情况下。但是,当我们需要将物体定位到与其他方向上的摄像机中心位置时,需要显式地指定up参数的方向值。