threejs特殊几何体(一:文字几何体对象)

threejs中文字几何体通过newTextGeometry()生成,它被单独作为一个类存在于threejs中const txtGeo = new TextGeometry("threejs", { ...opts, font: font });

我们先看效果:

复制代码
<template>
    <div>
    </div>
</template>
<script  setup>
import { ref } from "vue";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry";
import { FontLoader } from "three/examples/jsm/loaders/FontLoader";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
// const font = new THREE.Font("Arial");
camera.position.set(0, 0, 10);

let opts = {
  size: 3,
  height: 5,
  //   fontWeight: "bold",
  bevelSize: 3,
  bevelEnabled: false,
  steps: 7,
  font: "",
  bevelThickness: 1
};

const ftloader = new FontLoader();
ftloader.load("/src/assets/fonts/helvetiker_regular.typeface.json", font => {
  const txtGeo = new TextGeometry("sean", { ...opts, font: font });
  const mesh = new THREE.Mesh(
    txtGeo,
    new THREE.MeshBasicMaterial({ color: "#e6c" })
  );
  scene.add(mesh);
});

const renderder = new THREE.WebGLRenderer();
renderder.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderder.domElement);
renderder.setClearColor("#eee");
const control = new OrbitControls(camera, renderder.domElement);
const render = () => {
  requestAnimationFrame(render);
  renderder.render(scene, camera);
};
render();
</script>
<style scoped>
/* body {
  font-family: Arial, Helvetica, sans-serif;
} */
</style>

这里特别要注意的是:

注意点:1 文字几何体font必须要引入。可以使用three库默认的字体。该字体位于three/example/fonts/文件夹下,默认字体采用了json文件格式

2 给TextGeometry添加配资的时候,font选项不是普通的字体对象而是three里面的字体对象。也就是font是 new FontLoader()。load加载后的字体对象

相关推荐
bmxy小明同学38 分钟前
2026-10-11-幻觉治理
服务器·前端·javascript
Coder_张三1 小时前
iOS 26 适配
前端·ios·swift
m0_587383001 小时前
无人自助拼豆系统落地开发实战指南 核心逻辑实现与部署经验分享
前端·javascript·vue.js·人工智能·经验分享·需求分析
zhaiyy1 小时前
HTML Popover API 实践
前端·javascript·html
OpsEye1 小时前
企业 AI 管理新需求:不仅要接入模型,还要看懂每一次调用
javascript·ai编程
牧艺1 小时前
SPA 也能像原生 App:View Transitions API 从原理到可上线实战
前端·javascript·css
Dovis(誓平步青云)1 小时前
现场问题先关闭还是先验收,建筑任务怎样处理依赖
前端·javascript·数据库·mysql·音视频
用户84298142418102 小时前
压缩 ≠ 加密:前端代码保护的三层实践与踩坑记录
前端·javascript
前端一课2 小时前
别把人生过成一场等待退休的倒计时
前端
.Hypocritical.2 小时前
SpringBoot+Vue 前后端分离项目部署到云服务器
服务器·vue.js·spring boot