11// From: https://github.com/emilwidlund/ASCII
22// https://twitter.com/emilwidlund/status/1652386482420609024
33
4- import { forwardRef , useMemo } from 'react'
5- import { CanvasTexture , Color , NearestFilter , RepeatWrapping , Texture , Uniform } from 'three'
64import { Effect } from 'postprocessing'
5+ import { Ref , useMemo } from 'react'
6+ import { CanvasTexture , Color , NearestFilter , RepeatWrapping , Texture , Uniform } from 'three'
7+ import { useDispose } from '../util'
78
8- const fragment = `
9- uniform sampler2D uCharacters;
10- uniform float uCharactersCount;
11- uniform float uCellSize;
12- uniform bool uInvert;
13- uniform vec3 uColor;
9+ const fragment = /* glsl */ `
10+ uniform sampler2D uCharacters;
11+ uniform float uCharactersCount;
12+ uniform float uCellSize;
13+ uniform bool uInvert;
14+ uniform vec3 uColor;
1415
15- const vec2 SIZE = vec2(16.);
16+ const vec2 SIZE = vec2(16.);
1617
17- vec3 greyscale(vec3 color, float strength) {
18+ vec3 greyscale(vec3 color, float strength) {
1819 float g = dot(color, vec3(0.299, 0.587, 0.114));
1920 return mix(color, vec3(g), strength);
20- }
21+ }
2122
22- vec3 greyscale(vec3 color) {
23+ vec3 greyscale(vec3 color) {
2324 return greyscale(color, 1.0);
24- }
25+ }
2526
26- void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
27+ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
2728 vec2 cell = resolution / uCellSize;
2829 vec2 grid = 1.0 / cell;
2930 vec2 pixelizedUV = grid * (0.5 + floor(uv / grid));
@@ -43,7 +44,7 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)
4344 asciiCharacter.rgb = uColor * asciiCharacter.r;
4445 asciiCharacter.a = pixelized.a;
4546 outputColor = asciiCharacter;
46- }
47+ }
4748`
4849
4950interface IASCIIEffectProps {
@@ -53,6 +54,7 @@ interface IASCIIEffectProps {
5354 cellSize ?: number
5455 color ?: string
5556 invert ?: boolean
57+ ref ?: Ref < ASCIIEffect >
5658}
5759
5860class ASCIIEffect extends Effect {
@@ -63,7 +65,7 @@ class ASCIIEffect extends Effect {
6365 cellSize = 16 ,
6466 color = '#ffffff' ,
6567 invert = false ,
66- } : IASCIIEffectProps = { } ) {
68+ } : Omit < IASCIIEffectProps , 'ref' > = { } ) {
6769 const uniforms = new Map < string , Uniform > ( [
6870 [ 'uCharacters' , new Uniform ( new Texture ( ) ) ] ,
6971 [ 'uCellSize' , new Uniform ( cellSize ) ] ,
@@ -114,22 +116,21 @@ class ASCIIEffect extends Effect {
114116 }
115117}
116118
117- export const ASCII = /* @__PURE__ */ forwardRef < ASCIIEffect , IASCIIEffectProps > (
118- (
119- {
120- font = 'arial' ,
121- characters = ` .:,'-^=*+?!|0#X%WM@` ,
122- fontSize = 54 ,
123- cellSize = 16 ,
124- color = '#ffffff' ,
125- invert = false ,
126- } ,
127- fref
128- ) => {
129- const effect = useMemo (
130- ( ) => new ASCIIEffect ( { characters, font, fontSize, cellSize, color, invert } ) ,
131- [ characters , fontSize , cellSize , color , invert , font ]
132- )
133- return < primitive ref = { fref } object = { effect } />
134- }
135- )
119+ export function ASCII ( {
120+ font = 'arial' ,
121+ characters = ` .:,'-^=*+?!|0#X%WM@` ,
122+ fontSize = 54 ,
123+ cellSize = 16 ,
124+ color = '#ffffff' ,
125+ invert = false ,
126+ ref,
127+ } : IASCIIEffectProps ) {
128+ const effect = useMemo (
129+ ( ) => new ASCIIEffect ( { characters, font, fontSize, cellSize, color, invert } ) ,
130+ [ characters , fontSize , cellSize , color , invert , font ]
131+ )
132+
133+ useDispose ( effect )
134+
135+ return < primitive ref = { ref } object = { effect } />
136+ }
0 commit comments