From d84c416951b276e145a2c3ffeda7dd9e88ff07bd Mon Sep 17 00:00:00 2001 From: kjerandp Date: Tue, 17 Jun 2025 11:38:12 +0200 Subject: [PATCH] fix: use glyph provider in sdfTest component and fix parameter qualifiers in glyph shaders --- src/components/SDFTest/SDFTest.stories.tsx | 2 + src/components/SDFTest/SDFTest.tsx | 53 ++++++++----------- src/components/SDFTest/shaders/fragment.glsl | 8 +-- src/sdk/materials/shaderLib/glyphs.glsl | 2 +- .../materials/shaderLib/render-number.glsl | 2 +- src/sdk/materials/shaderLib/render-text.glsl | 3 +- src/storybook/decorators/glyphs-decorator.tsx | 9 ++++ 7 files changed, 39 insertions(+), 40 deletions(-) create mode 100644 src/storybook/decorators/glyphs-decorator.tsx diff --git a/src/components/SDFTest/SDFTest.stories.tsx b/src/components/SDFTest/SDFTest.stories.tsx index 9233a8a..9a31dda 100644 --- a/src/components/SDFTest/SDFTest.stories.tsx +++ b/src/components/SDFTest/SDFTest.stories.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryObj } from '@storybook/react' import { Canvas3dDecorator } from '../../storybook/decorators/canvas-3d-decorator' +import { GlyphsDecorator } from '../../storybook/decorators/glyphs-decorator' import { PerformanceDecorator } from '../../storybook/decorators/performance-decorator' import { SDFTest } from './SDFTest' @@ -7,6 +8,7 @@ const meta = { title: 'Components/Misc/SDFTest', component: SDFTest, decorators: [ + GlyphsDecorator, PerformanceDecorator, Canvas3dDecorator, ], diff --git a/src/components/SDFTest/SDFTest.tsx b/src/components/SDFTest/SDFTest.tsx index cb333be..31539fd 100644 --- a/src/components/SDFTest/SDFTest.tsx +++ b/src/components/SDFTest/SDFTest.tsx @@ -1,10 +1,8 @@ -import { useTexture } from '@react-three/drei' import { extend, useFrame } from '@react-three/fiber' import { MeshLineGeometry, MeshLineMaterial } from 'meshline' -import { useEffect, useMemo, useState } from 'react' -import { DataTexture, DoubleSide, LinearFilter, Texture, Uniform, Vector2 } from 'three' -import { createConfig, GlyphConfig, MsdfFontJson } from '../../sdk/utils/glyphs' -import { get } from '../../storybook/dependencies/api' +import { useContext, useEffect, useMemo } from 'react' +import { DataTexture, DoubleSide, Texture, Uniform, Vector2 } from 'three' +import { GlyphsContext } from '../../main' import fragmentShader from './shaders/fragment.glsl' import vertexShader from './shaders/vertex.glsl' @@ -24,25 +22,10 @@ type Props = { horizontalAlign?: number } -const fileName = 'OpenSans-Regular' - export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation = 0, spacing = 0, verticalAlign = 0.0, horizontalAlign = 0.0 }: Props) => { - const glyphAtlas = useTexture(`./glyphs/${fileName}.png`, (tex: Texture) => { - tex.generateMipmaps = false - tex.magFilter = LinearFilter - tex.minFilter = LinearFilter - tex.flipY = true - }) - - const [glyphConfig, setGlyphConfig] = useState() - - useEffect(() => { - get(`./glyphs/${fileName}.json`).then((json: MsdfFontJson) => { - setGlyphConfig(createConfig(json)) - }).catch(() => setGlyphConfig(null)) - }, []) + const glyphContext = useContext(GlyphsContext) const uniforms = useMemo(() => { return { @@ -64,26 +47,32 @@ export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation }, []) useEffect(() => { - if (glyphConfig) { + if (glyphContext) { + uniforms.glyphAtlas.value = glyphContext.glyphAtlas + } + }, [uniforms, glyphContext]) + + useEffect(() => { + if (glyphContext) { if (uniforms.textTexture.value) { uniforms.textTexture.value.dispose() } - const { texture, textPointersOffset, textPointersCount } = glyphConfig.encodeTextTexture(text.split('\n')) + const { texture, textPointersOffset, textPointersCount } = glyphContext.encodeTextTexture(text.split('\n')) uniforms.textTexture.value = texture uniforms.textPointersOffset.value = textPointersOffset uniforms.textPointersCount.value = textPointersCount - uniforms.digits.value = [...glyphConfig.encodeText('0123456789.-').indices] + uniforms.digits.value = [...glyphContext.encodeText('0123456789.-').indices] } return () => { - if (glyphConfig) { - glyphConfig.dispose() + if (glyphContext) { + glyphContext.dispose() } } - }, [uniforms, glyphConfig, text]) + }, [uniforms, glyphContext, text]) + useEffect(() => { - uniforms.glyphAtlas.value = glyphAtlas uniforms.in_bias.value = inBias uniforms.out_bias.value = outBias uniforms.fontSize.value = fontSize @@ -91,13 +80,13 @@ export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation uniforms.spacing.value = spacing uniforms.verticalAlign.value = verticalAlign uniforms.horizontalAlign.value = horizontalAlign - }, [uniforms, glyphAtlas, glyphConfig, inBias, outBias, fontSize, rotation, spacing, verticalAlign, horizontalAlign]) + }, [uniforms, inBias, outBias, fontSize, rotation, spacing, verticalAlign, horizontalAlign]) useFrame(({ clock }) => { uniforms.time.value = clock.elapsedTime }) - if (!glyphConfig) return null + if (!glyphContext) return null return ( @@ -107,10 +96,10 @@ export const SDFTest = ({ text, inBias = 0, outBias = 0, fontSize = 32, rotation {/* */} ( + + + +) + +