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

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

一、方法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;
相关推荐
gameckisme4 天前
Selfloss,官方中文,解压即玩,
游戏·unity·游戏程序·图形渲染·cocos2d·贴图·游戏策划
refineiks5 天前
three.js使用3DTilesRendererJS加载3d tiles数据
前端·3d·图形渲染·webgl
Padid5 天前
文章-深入GPU硬件架构及运行机制 学习后记
笔记·学习·硬件架构·图形渲染·着色器
Padid8 天前
OpenGL GLFW OIT 实现
c++·笔记·学习·图形渲染·着色器
玖er悠8 天前
Unity Shader实现简单的各向异性渲染(采用各向异性形式的GGX分布)
unity·图形渲染
米芝鱼11 天前
UnityShader自定义属性特性
开发语言·游戏·unity·游戏引擎·图形渲染·着色器
米芝鱼12 天前
UnityShaderGraph 卡通水面效果
游戏·unity·游戏引擎·图形渲染·着色器
知心宝贝12 天前
【小程序 - 大智慧】深入微信小程序的核心原理
开发语言·javascript·算法·微信小程序·小程序·前端框架·图形渲染
2401_8566522112 天前
高清无损!探索PDF转JPG的最佳实践工具
图像处理·学习·自然语言处理·pdf·图形渲染
是jin奥15 天前
Ubuntu 搭建 GLFW 环境及其相关测试 demo
linux·ubuntu·图形渲染