news 2026/7/6 21:34:37

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 开源仓库
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/3 18:42:59

2026年7月GPT充值困局解析深度指南

国内用户在2026年充值GPT时面临支付障碍&#xff0c;核心原因在于合规支付渠道的缺失与非官方充值渠道的稳定性风险&#xff0c;这直接影响了服务的连续性和使用体验。 一、核心支付障碍 障碍类别具体表现影响人群支付工具限制缺乏有效的海外信用卡&#xff08;Visa/MasterCa…

作者头像 李华
网站建设 2026/7/5 19:25:29

靠谱的螺旋风管节能型厂家

在工业和建筑领域&#xff0c;螺旋风管的应用十分广泛&#xff0c;它不仅能保障空气流通&#xff0c;还在节能方面发挥着重要作用。选择一家靠谱的螺旋风管节能型厂家至关重要。今天就为大家推荐佛山市南海开顺螺旋风管厂&#xff08;简称开顺风管&#xff09;&#xff0c;同时…

作者头像 李华
网站建设 2026/7/3 2:52:13

鸿蒙 ArkTS 两大基础事件简单说明

一、onClick 点击事件触发条件&#xff1a;手指点击、鼠标点击组件时立刻执行&#xff0c;几乎所有可视化组件都能绑定。用途&#xff1a;处理点击交互&#xff0c;比如按钮弹窗、页面跳转、确认操作、切换显示隐藏等。特点&#xff1a;只要产生点击动作就会触发&#xff0c;和…

作者头像 李华
网站建设 2026/7/6 12:00:57

Windows系统文件ActionQueue.dll丢失找不到问题解决

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华