1- import { useTexture } from '@react-three/drei'
21import { extend , useFrame } from '@react-three/fiber'
32import { MeshLineGeometry , MeshLineMaterial } from 'meshline'
4- import { useEffect , useMemo , useState } from 'react'
5- import { DataTexture , DoubleSide , LinearFilter , Texture , Uniform , Vector2 } from 'three'
6- import { createConfig , GlyphConfig , MsdfFontJson } from '../../sdk/utils/glyphs'
7- import { get } from '../../storybook/dependencies/api'
3+ import { useContext , useEffect , useMemo } from 'react'
4+ import { DataTexture , DoubleSide , Texture , Uniform , Vector2 } from 'three'
5+ import { GlyphsContext } from '../../main'
86import fragmentShader from './shaders/fragment.glsl'
97import vertexShader from './shaders/vertex.glsl'
108
@@ -24,25 +22,10 @@ type Props = {
2422 horizontalAlign ?: number
2523}
2624
27- const fileName = 'OpenSans-Regular'
28-
2925
3026export const SDFTest = ( { text, inBias = 0 , outBias = 0 , fontSize = 32 , rotation = 0 , spacing = 0 , verticalAlign = 0.0 , horizontalAlign = 0.0 } : Props ) => {
3127
32- const glyphAtlas = useTexture ( `./glyphs/${ fileName } .png` , ( tex : Texture ) => {
33- tex . generateMipmaps = false
34- tex . magFilter = LinearFilter
35- tex . minFilter = LinearFilter
36- tex . flipY = true
37- } )
38-
39- const [ glyphConfig , setGlyphConfig ] = useState < GlyphConfig | null > ( )
40-
41- useEffect ( ( ) => {
42- get ( `./glyphs/${ fileName } .json` ) . then ( ( json : MsdfFontJson ) => {
43- setGlyphConfig ( createConfig ( json ) )
44- } ) . catch ( ( ) => setGlyphConfig ( null ) )
45- } , [ ] )
28+ const glyphContext = useContext ( GlyphsContext )
4629
4730 const uniforms = useMemo ( ( ) => {
4831 return {
@@ -64,40 +47,46 @@ export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation
6447 } , [ ] )
6548
6649 useEffect ( ( ) => {
67- if ( glyphConfig ) {
50+ if ( glyphContext ) {
51+ uniforms . glyphAtlas . value = glyphContext . glyphAtlas
52+ }
53+ } , [ uniforms , glyphContext ] )
54+
55+ useEffect ( ( ) => {
56+ if ( glyphContext ) {
6857 if ( uniforms . textTexture . value ) {
6958 uniforms . textTexture . value . dispose ( )
7059 }
71- const { texture, textPointersOffset, textPointersCount } = glyphConfig . encodeTextTexture ( text . split ( '\n' ) )
60+ const { texture, textPointersOffset, textPointersCount } = glyphContext . encodeTextTexture ( text . split ( '\n' ) )
7261 uniforms . textTexture . value = texture
7362 uniforms . textPointersOffset . value = textPointersOffset
7463 uniforms . textPointersCount . value = textPointersCount
75- uniforms . digits . value = [ ...glyphConfig . encodeText ( '0123456789.-' ) . indices ]
64+ uniforms . digits . value = [ ...glyphContext . encodeText ( '0123456789.-' ) . indices ]
7665 }
7766
7867 return ( ) => {
79- if ( glyphConfig ) {
80- glyphConfig . dispose ( )
68+ if ( glyphContext ) {
69+ glyphContext . dispose ( )
8170 }
8271 }
83- } , [ uniforms , glyphConfig , text ] )
72+ } , [ uniforms , glyphContext , text ] )
73+
8474
8575 useEffect ( ( ) => {
86- uniforms . glyphAtlas . value = glyphAtlas
8776 uniforms . in_bias . value = inBias
8877 uniforms . out_bias . value = outBias
8978 uniforms . fontSize . value = fontSize
9079 uniforms . rotation . value = rotation
9180 uniforms . spacing . value = spacing
9281 uniforms . verticalAlign . value = verticalAlign
9382 uniforms . horizontalAlign . value = horizontalAlign
94- } , [ uniforms , glyphAtlas , glyphConfig , inBias , outBias , fontSize , rotation , spacing , verticalAlign , horizontalAlign ] )
83+ } , [ uniforms , inBias , outBias , fontSize , rotation , spacing , verticalAlign , horizontalAlign ] )
9584
9685 useFrame ( ( { clock } ) => {
9786 uniforms . time . value = clock . elapsedTime
9887 } )
9988
100- if ( ! glyphConfig ) return null
89+ if ( ! glyphContext ) return null
10190
10291 return (
10392 < group >
@@ -107,10 +96,10 @@ export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation
10796 {/* <icosahedronGeometry args={[WIDTH, 32]} /> */ }
10897 < shaderMaterial
10998 defines = { {
110- GLYPHS_LENGTH : glyphConfig . glyphsCount ,
99+ GLYPHS_LENGTH : glyphContext . glyphsCount ,
111100 } }
112101 uniforms = { uniforms }
113- uniformsGroups = { [ glyphConfig . glyphData ] }
102+ uniformsGroups = { [ glyphContext . glyphData ] }
114103 vertexShader = { vertexShader }
115104 fragmentShader = { fragmentShader }
116105 side = { DoubleSide }
0 commit comments