当前位置:   article > 正文

根据视图矩阵, 恢复相机的世界空间的位置

根据视图矩阵, 恢复相机的世界空间的位置

根据视图矩阵, 恢复相机的世界空间的位置

在这里插入图片描述

一、方法1

glsl 实现:

// 从本地局部坐标系(相机空间) 到 世界空间的旋转变换
mat3 getLocal2WorldRotation() {
	mat3 world2localRotation = mat3(
		viewMatrix[0].xyz,
		viewMatrix[1].xyz,
		viewMatrix[2].xyz
	);

	return inverse(world2localRotation);
}

vec3 getCameraPos( in mat3 rotation ) {
    // 相机没有旋转时的世界坐标系下的位置
    const posWCNoRotation = - viewMatrix[3].xyz;
	return rotation * posWCNoRotation;
}

mat3 local2worldRotation = getLocal2WorldRotation();

// 世界坐标系的相机位置
vec3 camPositionWC = getCameraPos( local2worldRotation );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

js 实现:

const viewMat = camera.matrixWorldInverse.elements;

// 旋转矩阵, 方法1
const viewMat = camera.matrixWorldInverse.elements;
const viewRotation = new THREE.Matrix4();
  viewRotation.set(
    viewMat[0], viewMat[4], viewMat[8], 0,
    viewMat[1], viewMat[5], viewMat[9], 0,
    viewMat[2], viewMat[6], viewMat[10], 0,
    0, 0, 0, 1,
  );
viewRotation.invert();

// 旋转矩阵, 方法2
const viewRotation = new THREE.Matrix4().makeRotationFromQuaternion(camera.quaternion);

const pos = new THREE.Vector3(-viewMat[12], -viewMat[13], -viewMat[14]);
pos.applyMatrix4(viewRotation);
console.log(pos);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
二、方法2
// 计算视图矩阵的逆矩阵
mat4 inverseViewMatrix = inverse(viewMatrix);

// 提取相机在世界空间中的位置
vec3 cameraPosition = inverseViewMatrix[3].xyz;
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/847386
推荐阅读
相关标签
  

闽ICP备14008679号