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

相关推荐
WEI_Gaot几秒前
React 19 Props 和 react-icons 和 事件处理函数
前端·react.js
10年前端老司机几秒前
微信小程序模板语法和事件
前端·javascript·微信小程序
龙在天几秒前
Promise.withResolvers() vs 传统 Promise:谁更胜一筹?
前端
xinhong1 分钟前
Vue3 学习笔记
前端
Evenknow2 分钟前
将"修改源码"改为更专业的"二次开发",体现技术深度
前端·github
khalil2 分钟前
浅析TS枚举与位运算的结合
前端·typescript
yanglei2 分钟前
electron-updater 核心源码解析
前端
攻城狮的大师兄2 分钟前
红宝书(第四版)通读之查漏补缺
javascript
神仙别闹5 分钟前
基于Java+MySQL 实现(Web)日程管理系统
java·前端·mysql
布列瑟农的星空7 分钟前
webworker 实践:外部依赖引入和打包问题
前端·低代码