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

Three.js 精灵标签教程

精灵标签 ·Sprite Label· ▶ 在线运行案例

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

你将学到什么

  • Canvas 2D动态画标签(图 + 字)
  • CanvasTexture转 Three.js 纹理
  • sprite.center锚点、devicePixelRatio高清

效果说明

5 个 Sprite 沿对角线排列,每个显示头像 +「测试文本」,纹理来自运行时 canvas 绘制。

核心概念

const canvas = document.createElement('canvas');

const ctx = canvas.getContext('2d'); // 高 DPI canvas.width = logicalWidth * devicePixelRatio; ctx.scale(devicePixelRatio, devicePixelRatio); ctx.drawImage(img, ...); ctx.fillText(text, ...);

const texture = new THREE.CanvasTexture(canvas); texture.colorSpace = THREE.SRGBColorSpace;

const sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map: texture })); sprite.center.set(0.5, 0); // 底边中心锚点,适合「桩子上的牌」

改 canvas 内容后需texture.needsUpdate = true

代码要点

import * as THREE from 'three'

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

const box = document.getElementById('box')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 1000)

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

const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })

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

box.appendChild(renderer.domElement)

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

controls.enableDamping = true

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

animate()

function animate() {

requestAnimationFrame(animate)

controls.update()

renderer.render(scene, camera)

}

window.onresize = () => {

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

camera.aspect = box.clientWidth / box.clientHeight

camera.updateProjectionMatrix()

}

const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d');

const devicePixelRatio = window.devicePixelRatio * 4; const logicalWidth = 124; const logicalHeight = 164;

canvas.width = logicalWidth * devicePixelRatio; canvas.height = logicalHeight * devicePixelRatio; canvas.style.width =${logicalWidth}px; canvas.style.height =${logicalHeight}px;

ctx.scale(devicePixelRatio, devicePixelRatio);

const img = new Image(); img.src = HOST + 'files/author/z2586300277.png';

const text = '测试文本';

const setText = () => {

// 画图 const imglong = 124; const left = (logicalWidth - imglong) / 2; ctx.drawImage(img, left, 40, imglong, imglong); // 向下移动图片

// 写字 ctx.fillStyle = '#fff'; ctx.font = 'Bold 30px Arial';

const textWidth = ctx.measureText(text).width; ctx.fillText(text, (logicalWidth - textWidth) / 2, 30); // 放在图片上方

// 纹理图 const texture = new THREE.CanvasTexture(canvas); texture.colorSpace = THREE.SRGBColorSpace; texture.needsUpdate = true;

// 纹理生成材质 const material = new THREE.SpriteMaterial({ map: texture });

// 创建精灵几何体 const sprite = new THREE.Sprite(material); sprite.center = new THREE.Vector2(0.5, 0);

return sprite;

}

img.onload = () => {

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

const sprite = setText()

sprite.position.set(i, i, i)

scene.add(sprite)

}

}

完整源码:GitHub

小结

  • 本文提供精灵标签完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
  • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.cnnetsun.cn/news/3107693.html

相关文章:

  • 如何3分钟搞定QQ空间数据备份:GetQzonehistory智能导出工具完整指南
  • Three.js 变换 Box3教程
  • 基于Agentic AI的降维算法自动化调优与可视化评估实践
  • TELAPA框架:基于策略档案与共享潜空间的持续强化学习实践
  • 8位MCU安全连接云端:PIC18F2620与A5000加密芯片实践
  • Anthropic Claude‘归零层’技术解析:语义校验环的架构级移除
  • GPT-4的‘2%激活‘真相:MoE稀疏推理原理与工程实践
  • 期权量化交易系列教程(四):经典方向性策略——备兑、保护性看跌与价差组合
  • Angular端到端测试实战:用TestCafe替代Protractor
  • Ubuntu 14.04下MongoDB备份恢复与迁移实战指南
  • Flask生产部署:Gunicorn+Nginx在Ubuntu 20.04上的完整实践
  • 告别手动抢购烦恼:Campus-iMaoTai智能茅台预约系统完全指南
  • 语义一致性仲裁系统:ADK契约引擎+Agent SDK协同验证
  • Debian 8 上安全部署 Django 1.11 的完整实践指南
  • PCF8591与PIC18LF25J11的硬件协同设计与信号处理优化
  • 从简历到offer:Java面试的全流程攻略
  • Mythos动态闸门:Claude 3.5的语义栅栏与可信推理机制
  • Gemini 3.0全家桶如何重塑前端开发工作流
  • MuleSoft如何实现企业级AI编排:LLM与业务系统的语义融合
  • 医院智慧后勤数字化建设技术方案
  • Claude语义保真度校验环归零:确定性推理架构解析
  • 2026必看:两款主流AI编程工具深度实测对比
  • Transformer词嵌入层深度解剖:语义校准、位置耦合与梯度调控
  • Mac发烫如何解决?智能温控系统实现设备性能优化与硬件保护
  • Java核心考点:final/finally/finalize与对象4种引用全解析
  • Anthropic新架构:认知链路压缩为原子操作
  • 终极Windows风扇控制指南:如何用FanControl实现智能散热与静音平衡
  • AI数学家:数学实践范式的迁移与可验证工作流
  • 【CSDN首发】PTC加热器医疗应用技术指南:原理、选型与工程实践
  • Semantic Kernel+Neo4j轻量级知识问答系统实战