@@ -2,26 +2,9 @@ import React, { useEffect, useRef } from 'react';
2
2
import { RgbColor } from '../../common/colorUtils' ;
3
3
import { initArrayBuffer , initProgram , initTexture , setAttributeToArrayBuffer , setTexture } from '../../common/webglUtils' ;
4
4
import { ColorMetricType , getFragmentShader , RenderedColorReducerType } from './utils' ;
5
+ import vertexShaderSource from './shaders/vertex-shader.glsl' ;
5
6
import mixbox from 'mixbox' ;
6
7
7
- const VERTEX_SHADER = `
8
- attribute vec2 a_position;
9
- attribute vec2 a_texCoord;
10
-
11
- uniform vec2 u_resolution;
12
-
13
- varying vec2 v_texCoord;
14
-
15
- void main() {
16
- // remap positions to be in the [-1, 1] range
17
- vec2 clipSpace = ((a_position / u_resolution) * 2.0) - 1.0;
18
-
19
- gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
20
-
21
- v_texCoord = a_texCoord;
22
- }
23
- `
24
-
25
8
interface WebGlCanvasProps {
26
9
image : HTMLImageElement | undefined ;
27
10
palette : RgbColor [ ] ;
@@ -45,7 +28,7 @@ const WebGlCanvas: React.FC<WebGlCanvasProps> = ({
45
28
return ;
46
29
}
47
30
48
- const program = initProgram ( gl , VERTEX_SHADER , getFragmentShader ( colorMetric , colorReducer ) ) ;
31
+ const program = initProgram ( gl , vertexShaderSource , getFragmentShader ( colorMetric , colorReducer ) ) ;
49
32
if ( program === undefined ) {
50
33
console . error ( 'Program could not be initialized.' ) ;
51
34
return ;
@@ -86,11 +69,11 @@ const WebGlCanvas: React.FC<WebGlCanvasProps> = ({
86
69
87
70
const texture = initTexture ( gl ) ;
88
71
if ( texture === undefined ) {
89
- console . error ( 'Texture could not be initialized.' )
72
+ console . error ( 'Texture could not be initialized.' ) ;
90
73
return ;
91
74
}
92
75
93
- setTexture ( gl , texture , image )
76
+ setTexture ( gl , texture , image ) ;
94
77
95
78
gl . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
96
79
gl . useProgram ( program ) ;
@@ -99,14 +82,14 @@ const WebGlCanvas: React.FC<WebGlCanvasProps> = ({
99
82
gl . uniform2f ( resolutionLocation , gl . canvas . width , gl . canvas . height ) ;
100
83
101
84
const paletteBufferData = palette . flatMap (
102
- ( color ) => [ color . red / 255 , color . green / 255 , color . blue / 255 , 1 ]
85
+ ( color ) => [ color . red / 255 , color . green / 255 , color . blue / 255 , 1 ] ,
103
86
) ;
104
- paletteBufferData . push ( ...( ( new Array ( ( 64 - palette . length ) * 4 ) ) . fill ( 0 ) ) ) // pad with 0s
87
+ paletteBufferData . push ( ...( ( new Array ( ( 64 - palette . length ) * 4 ) ) . fill ( 0 ) ) ) ; // pad with 0s
105
88
const paletteLocation = gl . getUniformLocation ( program , 'u_palette' ) ;
106
89
gl . uniform4fv ( paletteLocation , paletteBufferData ) ;
107
90
108
91
const paletteSizeLocation = gl . getUniformLocation ( program , 'u_paletteSize' ) ;
109
- gl . uniform1i ( paletteSizeLocation , palette . length )
92
+ gl . uniform1i ( paletteSizeLocation , palette . length ) ;
110
93
111
94
const positionLocation = gl . getAttribLocation ( program , 'a_position' ) ;
112
95
setAttributeToArrayBuffer ( gl , positionBuffer , positionLocation ) ;
@@ -121,17 +104,17 @@ const WebGlCanvas: React.FC<WebGlCanvasProps> = ({
121
104
}
122
105
123
106
gl . drawArrays ( gl . TRIANGLES , 0 , 6 ) ;
124
- } , [ image , palette , colorMetric , colorReducer ] )
107
+ } , [ image , palette , colorMetric , colorReducer ] ) ;
125
108
126
109
if ( image === undefined ) {
127
110
return (
128
111
< > </ >
129
- )
112
+ ) ;
130
113
}
131
114
132
115
return (
133
116
< canvas ref = { canvasRef } />
134
- )
135
- }
117
+ ) ;
118
+ } ;
136
119
137
120
export default WebGlCanvas ;
0 commit comments