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加载后的字体对象

相关推荐
Irene19914 分钟前
JavaScript 字符串和数组方法总结(默写版:同9则6 Str21 Arr27)
javascript·字符串·数组·方法总结
runepic4 分钟前
Vue3 + Element Plus 实现PDF附件上传下载
前端·pdf·vue
可触的未来,发芽的智生6 分钟前
新奇特:象棋与麻将,解析生成大模型的两种哲学
javascript·人工智能·python·程序人生·自然语言处理
程序员修心18 分钟前
CSS 盒子模型与布局核心知识点总结
开发语言·前端·javascript
Cshaosun20 分钟前
阿里云宝塔面板部署vue+nodejs项目并实现https访问操作流程
vue.js·阿里云·https·node.js·宝塔·文件下载
elangyipi12323 分钟前
前端面试题:CSS BFC
前端·css·面试
程序员龙语24 分钟前
CSS 核心基础 —— 长度单位、颜色表示与字体样式
前端·css
IT古董25 分钟前
企业级官网全栈(React·Next.js·Tailwind·Axios·Headless UI·RHF·i18n)实战教程-第五篇:登录态与权限控制
javascript·react.js·ui
shuishen4929 分钟前
视频尾帧提取功能实现详解 - 纯前端Canvas API实现
前端·音视频·尾帧·末帧
IT_陈寒29 分钟前
Python性能调优实战:5个不报错但拖慢代码300%的隐藏陷阱(附解决方案)
前端·人工智能·后端