11import * as constants from './constants' ;
22
33/*
4- * Creates p5.strands filter shaders for cross-platform compatibility
4+ * Creates p5.strands filter shaders for cross-platform compatibility.
5+ *
6+ * NOTE: These work a little differently than p5.js web editor shaders work!
7+ * Firstly, it uses instance mode, so we have to explicitly pass in context
8+ * variables in an argument to your callback and as a second argument to `modify`.
9+ * Secondly, always manually specify uniform names, as variable names will change
10+ * in minified builds.
511 */
612export function makeFilterShader ( renderer , operation , p5 ) {
713 switch ( operation ) {
814 case constants . GRAY :
9- return renderer . baseFilterShader ( ) . modify ( ( ) => {
15+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
1016 p5 . getColor ( ( inputs , canvasContent ) => {
1117 const tex = p5 . getTexture ( canvasContent , inputs . texCoord ) ;
1218 // weighted grayscale with luminance values
@@ -16,7 +22,7 @@ export function makeFilterShader(renderer, operation, p5) {
1622 } , { p5 } ) ;
1723
1824 case constants . INVERT :
19- return renderer . baseFilterShader ( ) . modify ( ( ) => {
25+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
2026 p5 . getColor ( ( inputs , canvasContent ) => {
2127 const color = p5 . getTexture ( canvasContent , inputs . texCoord ) ;
2228 const invertedColor = p5 . vec3 ( 1.0 ) - color . rgb ;
@@ -25,8 +31,8 @@ export function makeFilterShader(renderer, operation, p5) {
2531 } , { p5 } ) ;
2632
2733 case constants . THRESHOLD :
28- return renderer . baseFilterShader ( ) . modify ( ( ) => {
29- const filterParameter = p5 . uniformFloat ( ) ;
34+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
35+ const filterParameter = p5 . uniformFloat ( 'filterParameter' ) ;
3036 p5 . getColor ( ( inputs , canvasContent ) => {
3137 const color = p5 . getTexture ( canvasContent , inputs . texCoord ) ;
3238 // weighted grayscale with luminance values
@@ -38,8 +44,8 @@ export function makeFilterShader(renderer, operation, p5) {
3844 } , { p5 } ) ;
3945
4046 case constants . POSTERIZE :
41- return renderer . baseFilterShader ( ) . modify ( ( ) => {
42- const filterParameter = p5 . uniformFloat ( ) ;
47+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
48+ const filterParameter = p5 . uniformFloat ( 'filterParameter' ) ;
4349 const quantize = ( color , n ) => {
4450 // restrict values to N options/bins
4551 // and floor each channel to nearest value
@@ -60,9 +66,9 @@ export function makeFilterShader(renderer, operation, p5) {
6066 } , { p5 } ) ;
6167
6268 case constants . BLUR :
63- return renderer . baseFilterShader ( ) . modify ( ( ) => {
64- const radius = p5 . uniformFloat ( ) ;
65- const direction = p5 . uniformVec2 ( ) ;
69+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
70+ const radius = p5 . uniformFloat ( 'radius' ) ;
71+ const direction = p5 . uniformVec2 ( 'direction' ) ;
6672
6773 // This isn't a real Gaussian weight, it's a quadratic weight
6874 const quadWeight = ( x , e ) => {
@@ -120,7 +126,7 @@ export function makeFilterShader(renderer, operation, p5) {
120126 } , { p5 } ) ;
121127
122128 case constants . ERODE :
123- return renderer . baseFilterShader ( ) . modify ( ( ) => {
129+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
124130 const luma = ( color ) => {
125131 return p5 . dot ( color . rgb , p5 . vec3 ( 0.2126 , 0.7152 , 0.0722 ) ) ;
126132 } ;
@@ -150,7 +156,7 @@ export function makeFilterShader(renderer, operation, p5) {
150156 } , { p5 } ) ;
151157
152158 case constants . DILATE :
153- return renderer . baseFilterShader ( ) . modify ( ( ) => {
159+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
154160 const luma = ( color ) => {
155161 return p5 . dot ( color . rgb , p5 . vec3 ( 0.2126 , 0.7152 , 0.0722 ) ) ;
156162 } ;
@@ -180,7 +186,7 @@ export function makeFilterShader(renderer, operation, p5) {
180186 } , { p5 } ) ;
181187
182188 case constants . OPAQUE :
183- return renderer . baseFilterShader ( ) . modify ( ( ) => {
189+ return renderer . baseFilterShader ( ) . modify ( ( { p5 } ) => {
184190 p5 . getColor ( ( inputs , canvasContent ) => {
185191 const color = p5 . getTexture ( canvasContent , inputs . texCoord ) ;
186192 return p5 . vec4 ( color . rgb , 1.0 ) ;
0 commit comments