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

Quaternion.setFromRotationMatrix()

Quaternion.setFromRotationMatrix(matrix: Matrix4): Quaternion

该方法将矩阵转化为四元数。

参数

  • matrixMatrix4类型的矩阵,用于计算四元数。

返回值

返回一个Quaternion类型的对象。

示例

const matrix = new THREE.Matrix4().makeRotationAxis( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
const quaternion = new THREE.Quaternion().setFromRotationMatrix( matrix );

实现原理

通过将旋转矩阵转化为四元数,将三维旋转问题转换为四元数旋转问题,其核心算法如下:

假设旋转矩阵为 matrix,构造四元数:

const quaternion = new THREE.Quaternion();

根据旋转矩阵中每个元素的值计算四元数的各个分量:

const te = matrix.elements;
const x = te[ 9 ] - te[ 6 ];
const y = te[ 2 ] - te[ 8 ];
const z = te[ 4 ] - te[ 1 ];
const w = Math.sqrt( 1.0 + te[ 0 ] + te[ 5 ] + te[ 10 ] ) / 2.0;

将上述分量设置到四元数中:

quaternion.set( x, y, z, w );

最后,将四元数归一化:

quaternion.normalize();

实际上,在计算过程中存在多种实现方式,以上仅为一种简单实现。