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

📅 2026/6/30 16:53:48
Three.js 三维转屏幕坐标教程
三维转屏幕坐标 ·World to Screen· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么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 threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.jsconst 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 truescene.add(new THREE.AxesHelper(50))const R () Math.random() * 10 - 5const 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 divlist.push(mesh)}function updateCSS2DVisibility() {list.forEach(mesh {const worldPosition mesh.getWorldPosition(new THREE.Vector3())worldPosition.project(camera);const width renderer.domElement.clientWidthconst height renderer.domElement.clientHeightconst screenX (worldPosition.x 1) / 2 * widthconst screenY (-worldPosition.y 1) / 2 * heightmesh.div.style.left screenX pxmesh.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.clientHeightcamera.updateProjectionMatrix()}// 创建dom function createDom(text) {const div document.createElement(div)div.style.position absoluteconst img document.createElement(img)img.src HOST /files/author/KallkaGo.jpgimg.style.width 50pximg.style.height 50pxdiv.appendChild(img)div.innerHTML textdiv.style.color whitedocument.body.appendChild(div)return div}完整源码GitHub小结本文提供三维转屏幕坐标完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库