1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00
This commit is contained in:
Kevin Levron 2021-04-21 21:05:17 +02:00
parent 04890fc2fb
commit 86c51ef9bd

85
src/meshes/Text.ts Normal file
View File

@ -0,0 +1,85 @@
import { defineComponent, watch } from 'vue'
import { Font, FontLoader, TextGeometry } from 'three'
import Mesh, { MeshSetupInterface } from './Mesh'
import { object3DSetup } from '../core/Object3D'
interface TextSetupInterface extends MeshSetupInterface {
geometry?: TextGeometry
font?: Font
}
const props = {
text: { type: String, required: true, default: 'Text' },
fontSrc: { type: String, required: true },
size: { type: Number, default: 80 },
height: { type: Number, default: 5 },
depth: { type: Number, default: 1 },
curveSegments: { type: Number, default: 12 },
bevelEnabled: { type: Boolean, default: false },
bevelThickness: { type: Number, default: 10 },
bevelSize: { type: Number, default: 8 },
bevelOffset: { type: Number, default: 0 },
bevelSegments: { type: Number, default: 5 },
align: { type: [Boolean, String], default: false },
}
export default defineComponent({
extends: Mesh,
props,
setup(): TextSetupInterface {
return object3DSetup()
},
created() {
if (!this.fontSrc) {
console.error('Missing required prop: "font-src"')
return
}
// if (!this.text) {
// console.error('Missing required prop: "text"')
// return
// }
// add watchers
const watchProps = [
'text', 'size', 'height', 'curveSegments',
'bevelEnabled', 'bevelThickness', 'bevelSize', 'bevelOffset', 'bevelSegments',
'align',
]
watchProps.forEach(p => {
// @ts-ignore
watch(() => this[p], () => {
if (this.font) this.refreshGeometry()
})
})
const loader = new FontLoader()
this.loading = true
loader.load(this.fontSrc, (font) => {
this.loading = false
this.font = font
this.createGeometry()
this.initMesh()
})
},
methods: {
createGeometry() {
this.geometry = new TextGeometry(this.text, {
// @ts-ignore
font: this.font,
size: this.size,
height: this.height,
depth: this.depth,
curveSegments: this.curveSegments,
bevelEnabled: this.bevelEnabled,
bevelThickness: this.bevelThickness,
bevelSize: this.bevelSize,
bevelOffset: this.bevelOffset,
bevelSegments: this.bevelSegments,
})
if (this.align === 'center') {
this.geometry.center()
}
},
},
})