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

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

在这里插入图片描述

一、方法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 );

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
// 计算视图矩阵的逆矩阵
mat4 inverseViewMatrix = inverse(viewMatrix);

// 提取相机在世界空间中的位置
vec3 cameraPosition = inverseViewMatrix[3].xyz;

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 21:04:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 21:04:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 21:04:01       45 阅读
  4. Python语言-面向对象

    2024-07-12 21:04:01       55 阅读

热门阅读

  1. PyTorch 1-深度学习

    2024-07-12 21:04:01       19 阅读
  2. pip install selenium异常

    2024-07-12 21:04:01       16 阅读
  3. PostgreSQL 导入 .gz 备份文件

    2024-07-12 21:04:01       17 阅读
  4. 力扣 225题 用队列实现栈 记录

    2024-07-12 21:04:01       19 阅读
  5. python爬虫js逆向入门

    2024-07-12 21:04:01       21 阅读
  6. vue3+ts 引入 json-editor-vue3 报错

    2024-07-12 21:04:01       17 阅读
  7. jar服务注册为windows的服务

    2024-07-12 21:04:01       13 阅读
  8. C++:创建线程

    2024-07-12 21:04:01       21 阅读
  9. python 知识点累积

    2024-07-12 21:04:01       19 阅读
  10. C语言——循环结构:while、do...while、for

    2024-07-12 21:04:01       19 阅读
  11. 简单有效防止CDN被盗刷流量

    2024-07-12 21:04:01       15 阅读