three.js - 矢量项目 THREE.ArrayCamera
问题描述
我正在尝试获取球体在屏幕上的位置并在其附近显示注释。我尝试了几种方法,但都没有给出正确的坐标。相机是THREE.ArrayCamera,我觉得是主要问题,我也试过vector.project(camera.cameras[3]),还是不行。CSS2DRenderer 也不适用于 arrayCamera。JSFIDDLE:https ://jsfiddle.net/cjuL4qx7/
const canvas = renderer.domElement; // `renderer` is a THREE.WebGLRenderer
var sphere = spheres_array[0];
sphere.material.color.setHex("0x0000FF")
var camera_for_label = camera;
camera_for_label.updateMatrixWorld();
camera_for_label.updateProjectionMatrix();
var vector = sphere.getWorldPosition().clone();
// map to normalized device coordinate (NDC) space
vector.project( camera_for_label );
// map to 2D screen space
vector.x = Math.round( ( vector.x + 1 ) * canvas.width / 2 ),
vector.y = Math.round( ( - vector.y + 1 ) * canvas.height / 2 );
vector.z = 0;
var annotation = document.querySelector('.annotation');
annotation.style.top = `${vector.y}px`;
annotation.style.left = `${vector.x}px`;
解决方案
推荐阅读
- laravel - 如何摆脱我的 Laravel 网站上的 aeR4Choc(恶意代码)?
- c++ - 从 Arduino 中的函数返回初始未知长度的 char 数组
- php - 如何在 Laravel 中拥有不同的 robots.txt 文件子域和主域?
- javascript - Docusign:如何让信封收件人在不同时间签署两个文件?
- php - 如何使用 PHP 在 netsuite api 中通过销售代表获取销售订单项目?
- python - Python - 从字典类型列熊猫数据框中获取前 5 项
- c - 为什么 Newlib C 标准库实现的 strtod() 和 strtof() 使用动态内存分配?
- node.js - Mongoose:如果它不是 null 或空字符串,则为唯一字段
- css - 如何在 React.js Formik(输入)中的输入字段后设置标签?
- javascript - 用 JavaScript 替换 return() 中的图标