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

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

一、方法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;
相关推荐
拿我格子衫来5 天前
图形编辑器基于Paper.js教程27:对图像描摹的功能实现,以及参数调整
开发语言·前端·javascript·图像处理·编辑器·图形渲染
程序员茶馆6 天前
【unity】Vulkan模式下部分Android机型使用VideoPlayer组件播放视频异常问题
游戏·unity·游戏引擎·图形渲染·unity3d·游戏开发
头发掉光的程序员7 天前
CPU与GPU之间的交互
c++·图形渲染·direct12
与火星的孩子对话9 天前
Unity进阶课程【四】Recorder 插件的使用 - 录制游戏画面、音频、动画、图片、无水印
游戏·unity·图形渲染·开源软件
踢足球的程序员·9 天前
OpenGL学习笔记(几何着色器、实例化、抗锯齿)
笔记·学习·图形渲染
踢足球的程序员·11 天前
OpenGL学习笔记(立方体贴图、高级数据、高级GLSL)
笔记·学习·图形渲染
GOTXX11 天前
【Qt】Qt Creator开发基础:项目创建、界面解析与核心概念入门
开发语言·数据库·c++·qt·图形渲染·图形化界面·qt新手入门
头发掉光的程序员14 天前
Raymarching Textures In Depth
ue5·游戏引擎·图形渲染
踢足球的程序员·21 天前
图形渲染: tinyrenderer 实现笔记(Lesson 5 - 7)
笔记·图形渲染