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

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

一、方法1

glsl 实现:

js 复制代码
// 从本地局部坐标系(相机空间) 到 世界空间的旋转变换
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 );

js 实现:

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);
二、方法2
js 复制代码
// 计算视图矩阵的逆矩阵
mat4 inverseViewMatrix = inverse(viewMatrix);

// 提取相机在世界空间中的位置
vec3 cameraPosition = inverseViewMatrix[3].xyz;
相关推荐
还下着雨ZG1 天前
VTK基础(01):VTK中的基本概念
图形渲染
和光同尘 、Y_____4 天前
BRepMesh_IncrementalMesh 重构生效问题
c++·算法·图形渲染
郝学胜-神的一滴7 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
玖釉-11 天前
OpenGL视图变换矩阵详解:从理论推导到实战应用
c++·图形渲染
元让_vincent13 天前
论文Review 3DGS HAC | ECCV2024 上海交大 莫纳什大学| 数10倍的3DGS模型压缩
3d·图形渲染·模型压缩·3dgs
郝学胜-神的一滴22 天前
Horse3D游戏引擎研发笔记(七):在QtOpenGL环境下,使用改进的Uniform变量管理方式绘制多彩四边形
c++·3d·unity·游戏引擎·图形渲染·虚幻·unreal engine
Duo1J22 天前
【OpenGL】LearnOpenGL学习笔记15 - 面剔除
笔记·学习·图形渲染
头发掉光的程序员23 天前
第七章 利用Direct3D绘制几何体
c++·windows·图形渲染·direct12
Jiezcode24 天前
Unreal Engine ClassName Rule
c++·游戏·图形渲染·虚幻引擎
郝学胜-神的一滴24 天前
深度解析游戏引擎中的相机:视图矩阵
程序人生·unity·矩阵·游戏引擎·godot·图形渲染·虚幻