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

相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code3 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luoluoal4 小时前
基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
vue.js·spring boot·宠物
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲5 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome