当前位置: 首页 > 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.gsyq.cn/news/1622923.html

相关文章:

  • 港股AI新股成“韭菜镰刀”:上市拉高、配股、入港股通后暴跌,散户成最终买单者
  • 大模型辅助搭建生产制造型企业排单助手
  • 2026AI论文工具红黑榜出炉!教你选对工具,写作不踩坑
  • 67|技能治理:版本、禁用回滚与共享策略
  • 2026 年居家高温灼伤护理科普:热水烫伤应急处理与避坑实操指南
  • Artillery性能测试实战:从脚本编写到结果分析全流程指南
  • 全球公司集体反省:从“Token管够”到“小模型经济学”,省钱风潮来袭!
  • 2026论文写作新利器!5款AI论文软件实测,从框架到内容一步到位
  • AI教材写作必备:低查重AI工具,为教材编写保驾护航!
  • SpaceX收购后Cursor推iOS版应用,可语音启动Agent但遭用户吐槽Bug多
  • Claude归零层解析:语义保真度校验环的工程消除与能力密度跃升
  • YOLOv8工业视觉实战:从模型优化到RK3588边缘部署全解析
  • 经典蓝牙技术综述
  • 无限维系统模型降阶:从插值投影到H2最优逼近的工程实践
  • 2026年构建 AI 交易机器人的最佳加密APIs
  • 工程办公管理软件如何破解成本失控与回款扯皮?三个落地切口
  • Agent Runtime:AI 应用的“操作系统时刻”已到来
  • 经济模型预测控制在周期性最优运行中的稳定性与性能分析
  • 良率工程实战:从72%到89%的完整爬坡路径
  • 从Samba漏洞到Jenkins沦陷:CVE-2017-7494攻击链深度剖析与防御实践
  • 3步掌握安卓应用管理神器:APKMirror安卓客户端终极指南
  • 微信小程序抓包实战:从原理到工具配置与安全分析
  • 企业AI提效五大实操场景:本地化、零API、合规落地
  • 暑期旅游邮件营销深度拆解:你的促销邮件为什么没人看?
  • 112、hypothesis 属性测试:让机器自动生成测试用例,发现你从未想过的边界
  • 大语言模型如何理解表格数据:表示学习与检索增强生成实践
  • BiliDownloader终极指南:简单快速免费下载B站视频的完整教程
  • 帐号注册与帐号登陆互联
  • 终极指南:3分钟掌握中国科学技术大学学位论文LaTeX模板
  • 掌握AI教材编写技巧,低查重AI工具让教材生成不再难!