news 2026/7/2 19:54:47

Three.js 精灵标签教程

作者头像

张小明

前端开发工程师

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

如何3分钟搞定QQ空间数据备份:GetQzonehistory智能导出工具完整指南

如何3分钟搞定QQ空间数据备份&#xff1a;GetQzonehistory智能导出工具完整指南 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 您是否曾经想过&#xff0c;那些记录青春岁月的QQ空间说…

作者头像 李华
网站建设 2026/7/2 19:48:38

Three.js 变换 Box3教程

变换 Box3 Box3 Helper ▶ 在线运行案例 案例合集&#xff1a; 三维可视化功能案例&#xff08;threehub.cn&#xff09;开源仓库github地址&#xff1a; https://github.com/z2586300277/three-cesium-examples400个案例代码: 网盘链接 你将学到什么 Box3.setFromObject …

作者头像 李华
网站建设 2026/7/2 19:46:24

基于Agentic AI的降维算法自动化调优与可视化评估实践

1. 项目概述&#xff1a;当降维算法遇上“智能体”在数据科学和机器学习的日常工作中&#xff0c;降维算法是我们处理高维数据、进行特征工程和可视化探索的“瑞士军刀”。无论是经典的PCA&#xff08;主成分分析&#xff09;、t-SNE&#xff0c;还是UMAP&#xff0c;我们都曾花…

作者头像 李华
网站建设 2026/7/2 19:42:54

TELAPA框架:基于策略档案与共享潜空间的持续强化学习实践

1. 从“灾难性遗忘”到“终身学习”&#xff1a;为什么我们需要TELAPA&#xff1f;如果你在强化学习领域摸爬滚打过一段时间&#xff0c;尤其是尝试过让一个智能体去学习一系列连续但不同的任务&#xff0c;那你大概率遇到过这个令人头疼的问题&#xff1a;智能体学会了新任务&…

作者头像 李华
网站建设 2026/7/2 19:39:33

8位MCU安全连接云端:PIC18F2620与A5000加密芯片实践

1. 项目背景与核心挑战在工业物联网和嵌入式设备领域&#xff0c;安全连接云端服务一直是个棘手问题。我最近用Microchip的PIC18F2620微控制器和A5000加密芯片做了个有意思的项目——让这个8位MCU也能安全地连接公共/私有云。这听起来可能有些反直觉&#xff0c;毕竟PIC18F2620…

作者头像 李华
网站建设 2026/7/2 19:36:38

Anthropic Claude‘归零层’技术解析:语义校验环的架构级移除

1. 项目概述&#xff1a;这不是一次普通更新&#xff0c;而是模型能力边界的悄然坍缩 “Anthropic Just Shipped the Layer That’s Already Going to Zero”——这个标题乍看像一句技术圈的黑色幽默&#xff0c;甚至带点玄学意味。但作为连续跟踪Claude系列模型迭代三年、亲手…

作者头像 李华