Skip to content

Commit 0338924

Browse files
authored
fix: use glyph provider in sdfTest component and fix parameter qualifiers in glyph shaders (#18)
1 parent fd02cf0 commit 0338924

File tree

7 files changed

+39
-40
lines changed

7 files changed

+39
-40
lines changed

src/components/SDFTest/SDFTest.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { Meta, StoryObj } from '@storybook/react'
22
import { Canvas3dDecorator } from '../../storybook/decorators/canvas-3d-decorator'
3+
import { GlyphsDecorator } from '../../storybook/decorators/glyphs-decorator'
34
import { PerformanceDecorator } from '../../storybook/decorators/performance-decorator'
45
import { SDFTest } from './SDFTest'
56

67
const meta = {
78
title: 'Components/Misc/SDFTest',
89
component: SDFTest,
910
decorators: [
11+
GlyphsDecorator,
1012
PerformanceDecorator,
1113
Canvas3dDecorator,
1214
],

src/components/SDFTest/SDFTest.tsx

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { useTexture } from '@react-three/drei'
21
import { extend, useFrame } from '@react-three/fiber'
32
import { 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'
86
import fragmentShader from './shaders/fragment.glsl'
97
import vertexShader from './shaders/vertex.glsl'
108

@@ -24,25 +22,10 @@ type Props = {
2422
horizontalAlign?: number
2523
}
2624

27-
const fileName = 'OpenSans-Regular'
28-
2925

3026
export 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}

src/components/SDFTest/shaders/fragment.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ uniform float horizontalAlign;
1717
#include ../../../sdk/materials/shaderLib/sdf-functions.glsl
1818

1919
// Debug
20-
void textGuides(out vec3 outColor, vec2 position) {
20+
void textGuides(inout vec3 outColor, vec2 position) {
2121
float helper;
2222

2323
// helper = sdfBox(position - vec2(0.0, 8.5), vec2(1000.0, glyphFontSize / 2.0));
@@ -40,7 +40,7 @@ void textGuides(out vec3 outColor, vec2 position) {
4040
}
4141

4242
// Examples
43-
void example1(out vec3 color, vec2 pixelCoords) {
43+
void example1(inout vec3 color, vec2 pixelCoords) {
4444
// the scale we need for a specific font size
4545
float scale = glyphFontSize / fontSize;
4646

@@ -74,7 +74,7 @@ void example1(out vec3 color, vec2 pixelCoords) {
7474
renderText(color, pixelCoords, textPointer, verticalAlign, horizontalAlign, vec3(0.09, 0.74, 0.51), spacing, scale);
7575
}
7676

77-
void example2(out vec3 color, vec2 pixelCoords) {
77+
void example2(inout vec3 color, vec2 pixelCoords) {
7878
uint i = 0u;
7979

8080
uvec3 textPointer = readTextPointerFromTexture(i);
@@ -101,7 +101,7 @@ void example2(out vec3 color, vec2 pixelCoords) {
101101
renderText(color, pixelCoords, textPointer, verticalAlign, horizontalAlign, textColor, spacing, scale);
102102
}
103103

104-
void exmaple3(out vec3 color, vec2 pixelCoords) {
104+
void exmaple3(inout vec3 color, vec2 pixelCoords) {
105105
float scale = glyphFontSize / fontSize;
106106

107107
float number = time;//-495.549221;

src/sdk/materials/shaderLib/glyphs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ float _sdfGlyph(vec2 p, uint glyphId) {
6868
return sigDist;
6969
}
7070

71-
void renderGlyph(out vec3 outColor, vec2 position, uint glyphId, vec3 glyphColor, float pxRange) {
71+
void renderGlyph(inout vec3 outColor, vec2 position, uint glyphId, vec3 glyphColor, float pxRange) {
7272
float dist = _sdfGlyph(position, glyphId);
7373
float e = pxRange * (dist - 0.5 + in_bias) + 0.5 + out_bias;
7474

src/sdk/materials/shaderLib/render-number.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
uniform uint digits[12];
22

33
float renderNumber(
4-
out vec3 outColor,
4+
inout vec3 outColor,
55
vec2 position,
66
float number,
77
uint decimals,

src/sdk/materials/shaderLib/render-text.glsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ uvec3 readTextPointerFromTexture(uint index) {
3737
}
3838

3939
void renderText(
40-
out vec3 outColor,
40+
inout vec3 outColor,
4141
vec2 position,
4242
uvec3 textPointer,
4343
float verticalAlign,
@@ -46,7 +46,6 @@ void renderText(
4646
float spacing,
4747
float scale
4848
) {
49-
5049
// do nothing if the text widht is 0
5150
if(textPointer.z == 0u)
5251
return;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { GlyphsProvider } from '../../main'
2+
3+
export const GlyphsDecorator = (Story: any) => (
4+
<GlyphsProvider fontAtlasUrl='glyphs/OpenSans-Regular.png' fontConfigUrl='glyphs/OpenSans-Regular.json'>
5+
<Story />
6+
</GlyphsProvider>
7+
)
8+
9+

0 commit comments

Comments
 (0)