当前位置: 首页 > news >正文

Three.js 三维转屏幕坐标教程

三维转屏幕坐标 ·World to Screen· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • vector.project(camera)世界坐标 → NDC → 屏幕像素
  • 不用 CSS2DRenderer 的手动 DOM 跟随写法
  • 每帧在 rAF 里更新标签位置

效果说明

30 个小立方体,每个上方有一个绝对定位的 DOM(图片 + 文字 D0~D29),随立方体在屏幕上移动而移动,像简易版 3D 标牌。

核心概念

project 管线

世界坐标 (World)

↓ matrixWorld × projectionMatrix NDC 归一化设备坐标 (-1 ~ 1) ↓ 视口变换 屏幕像素 (px)

const worldPosition = mesh.getWorldPosition(new THREE.Vector3());

worldPosition.project(camera);

const screenX = (worldPosition.x + 1) / 2 * width; const screenY = (-worldPosition.y + 1) / 2 * height;

div.style.left = screenX + 'px'; div.style.top = screenY + 'px';

注意Y 轴翻转:NDC 的 y 向上,屏幕 CSS 的 y 向下,故screenY取负。

与 CSS2DRenderer 对比

| 方式 | 本案例 | cssElement | |------|--------|-----------------------------------------------| | 实现 | 手算 project + DOM | CSS2DRenderer 自动投影 | | 深度遮挡 | 无,DOM 总在最上层 | 可选 | | 适用 | 理解原理、轻量标签 | 生产推荐 |

代码要点

import * as THREE from 'three'

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const DOM = document.getElementById('box')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(75, DOM.clientWidth / DOM.clientHeight, 0.1, 1000)

camera.position.set(10, 10, 10)

const renderer = new THREE.WebGLRenderer()

renderer.setSize(DOM.clientWidth, DOM.clientHeight)

DOM.appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)

controls.enableDamping = true

scene.add(new THREE.AxesHelper(50))

const R = () => Math.random() * 10 - 5

const list = []

for (let i = 0; i < 30; i++) {

const div = createDom('D' + i)

const mesh = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.3, 0.3), new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff }))

mesh.position.set(R(), R(), R())

scene.add(mesh)

mesh.div = div

list.push(mesh)

}

function updateCSS2DVisibility() {

list.forEach(mesh => {

const worldPosition = mesh.getWorldPosition(new THREE.Vector3())

worldPosition.project(camera);

const width = renderer.domElement.clientWidth

const height = renderer.domElement.clientHeight

const screenX = (worldPosition.x + 1) / 2 * width

const screenY = (-worldPosition.y + 1) / 2 * height

mesh.div.style.left = screenX + 'px'

mesh.div.style.top = screenY + 'px' })

}

animate()

function animate() {

requestAnimationFrame(animate)

updateCSS2DVisibility()

controls.update()

renderer.render(scene, camera)

}

window.onresize = () => {

renderer.setSize(box.clientWidth, box.clientHeight)

camera.aspect = box.clientWidth / box.clientHeight

camera.updateProjectionMatrix()

}

// 创建dom function createDom(text) {

const div = document.createElement('div')

div.style.position = 'absolute'

const img = document.createElement('img')

img.src = HOST + '/files/author/KallkaGo.jpg'

img.style.width = '50px'

img.style.height = '50px'

div.appendChild(img)

div.innerHTML += text

div.style.color = 'white'

document.body.appendChild(div)

return div

}

完整源码:GitHub

小结

  • 本文提供三维转屏幕坐标完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
  • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.jsqmd.com/news/1097119/

相关文章:

  • 《从CAPM到Barra:多因子模型的演进与基于AmazingData的实战》
  • 告别TrackBar!用这个开源控件5分钟搞定C# WinForm酷炫仪表盘
  • 竞争存在论:运动三连续统——时空动力学的统一生成理论
  • GoB插件:5分钟实现Blender与ZBrush无缝3D数据交换的高效方案
  • 保姆级教程:用Frida-Dexdump一键脱掉360加固的壳(附最新脚本)
  • 会小汪观察|第44届康博会圆满收官,重塑西部康养产业新格局
  • 技术实践:通过AI聚合平台统一调用文生图、视频生成和数字人API
  • 如何3步完成Nintendo Switch大气层自定义固件安装:新手终极教程
  • 别再手动调参了!用ModelScope的pipeline,5行代码搞定图像风格迁移与视频修复
  • 学机器视觉,别先纠结 OpenCV、HALCON 还是深度学习
  • 工信局如何识别产业链中的断点与卡脖子环节?
  • 保姆级教程:用R语言mediation包搞定NHANES数据的中介效应分析(附完整代码)
  • 靠谱的儿童近视配镜
  • 鸿蒙 ArkTS 基础组件与通用样式学习笔记
  • 实测对比:DECIMER、Img2Mol、MolScribe,哪个化学结构识别工具更靠谱?
  • 参数引发的复制中断:max_binlog_cache_size 导致 SQL 线程异常的复现与分析
  • VR-Reversal终极指南:免费将3D VR视频转为2D的完整教程
  • 达梦DMRMAN备份集校验:别等数据丢了才检查!手把手教你用CHECK命令给备份上个‘保险’
  • 如何让高校科研成果更有效地对接市场需求?
  • 5分钟掌握ComfyUI中文工作流:从新手到AI绘画高手的完整指南
  • 岁月从不败美人:会保养的女人与科学养生之道
  • 长链非编码RNA Gm10451(P10451)在干细胞分化与糖尿病治疗中的关键作用
  • SAP顾问必看:手把手教你用SNOTE打补丁,从下载SAR文件到撤回Note全流程避坑
  • 2026图片去水印方法:手机电脑免费工具、PS详细步骤、在线网站推荐
  • 【小白向】虾壳云一键部署完整实操,低配电脑也能流畅运行 OpenClaw v2.7.9 数字员工(最新安装包)
  • 江苏蔡司3D扫描仪定制厂家:为什么越来越多企业开始重视全尺寸检测?
  • AI建站工具零基础极速上手:10分钟生成你的第一个网站
  • Windows系统文件ActivationClient.dll丢失找不到问题解决
  • Three.js 3D饼图教程
  • 电池回收真的还能闭环吗? - 蓝色星球