当前位置: 首页 > 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.jsqmd.com/news/1110948/

相关文章:

  • Obsidian系列7:如何管理笔记1-文件夹 标签 书签的使用
  • Java 类加载机制:双亲委派、打破与热替换的实战
  • 干细胞研究获新突破 新规促规范
  • 451. Java 正则表达式 - Matcher 的 start(), end(), matches() 和 lookingAt()
  • 如何解决区域创新部门在政策资金投放中的“撒胡椒面”问题?
  • 港股AI新股成“韭菜镰刀”:上市拉高、配股、入港股通后暴跌,散户成最终买单者
  • 彻底解决 OpenClaw 杀毒拦截、路径报错、网关离线全套方案(含安装包)
  • 如何快速获取网盘直链:LinkSwift网盘下载助手完整指南
  • 从吃灰到生产力:用Armbian让旧电视盒子重获新生
  • GPT-4稀疏激活真相:万亿参数如何实现2%动态路由
  • Dify实战指南:从AI应用编排到企业级部署的30+核心模式解析
  • LU,大鼠脑定位仪 小鼠脑定位仪 大动物定位仪 小动物脑定位仪
  • 为什么专业机房都离不开防火门?一文讲透它的重要性
  • 大模型辅助搭建生产制造型企业排单助手
  • 经济周期与服饰品类匹配程序,区分繁荣期奢品,下行期平价服饰最优备货比例。
  • 索尼 PS6 将用 Zen 6 LP 低功耗核心,专为后台闲置工作降能耗
  • 分享:一站式 AI 工具全栈实验室|Chaos AI 研究室
  • 【Java课程设计/毕业设计】基于 SpringBoot 的智能瑜伽健身服务管理系统的设计与实现 基于 SpringBoot 的普拉提会馆会员权益与课程管理系统【附源码、数据库、万字文档】
  • A 股上市公司高管数字背景数据集
  • Whisky:在macOS上重构Windows应用运行边界的架构革命
  • 2026AI论文工具红黑榜出炉!教你选对工具,写作不踩坑
  • 67|技能治理:版本、禁用回滚与共享策略
  • TikTokDownload Cookie自动获取:告别手动烦恼的10分钟终极指南
  • 2026 年居家高温灼伤护理科普:热水烫伤应急处理与避坑实操指南
  • 面向对象设计在Java开发中的核心作用
  • AI教材写作必备:低查重AI工具,为教材编写保驾护航!
  • Artillery性能测试实战:从脚本编写到结果分析全流程指南
  • 69_Python时间日期处理
  • 全球公司集体反省:从“Token管够”到“小模型经济学”,省钱风潮来袭!
  • 如何3分钟搞定QQ空间数据备份:GetQzonehistory智能导出工具完整指南