order | category | name | sourcePath | type | componentSignature | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2.5 |
@threlte/extras |
<Text3DGeometry> |
packages/extras/src/lib/components/Text3DGeometry/Text3DGeometry.svelte |
component |
|
Render 3D text as a geometry using threejs's TextGeometry.
<script lang="ts">
import { T } from '@threlte/core'
import { Text3DGeometry } from '@threlte/extras'
</script>
<T.Mesh>
<Text3DGeometry text={'Hello World'} />
<T.MeshStandardMaterial />
</T.Mesh>
If no font
property is provided, the default font "Helvetiker" will be used
and loaded from the CDN
JSDeliver.
If you want to use a custom font, you can generate a font using
typeface.js. Provide the path to the
resulting JSON file using the prop font
.
The component <Text3DGeometry>
is suspense-ready. Using it in a
<Suspense>
boundary will suspend
rendering until the provided font is loaded:
<script lang="ts">
import { T } from '@threlte/core'
import { Text3DGeometry, Suspense } from '@threlte/extras'
import Fallback from './Fallback.svelte'
</script>
<Suspense>
<T.Mesh>
<Text3DGeometry
font={'path-to-your-font'}
text={'Hello World'}
/>
<T.MeshStandardMaterial />
</T.Mesh>
<Fallback slot="fallback" />
</Suspense>
You can also load the font yourself and pass it to the component, like so:
<script lang="ts">
import { T, useLoader } from '@threlte/core'
import { Text3DGeometry } from '@threlte/extras'
import { FontLoader } from 'three/examples/jsm/loaders/FontLoader.js'
let font = useLoader(FontLoader).load('path-to-your-font')
</script>
{#if $font}
<T.Mesh>
<Text3DGeometry
font={$font}
text={'Hello World'}
/>
<T.MeshStandardMaterial />
</T.Mesh>
{/await}
You can center a text by using the <Align>
component and calling the align slot prop when the text geometry is created.
<Align let:align>
<T.Mesh>
<Text3DGeometry
font={'path-to-your-font'}
text={`Hello!`}
on:create={align}
/>
<T.MeshStandardMaterial />
</T.Mesh>
</Align>
You can smooth the text by setting the smooth
prop to a value above 0 to smooth all edges where the angle between faces is less than the smooth
value.