@@ -1066,5 +1066,79 @@ suite('p5.Shader', function() {
10661066 assert . approximately ( pixelColor [ 0 ] , 127 , 5 ) ; // 0.5 * 255 ≈ 127
10671067 } ) ;
10681068 } ) ;
1069+
1070+ suite ( 'filter shader hooks' , ( ) => {
1071+ test ( 'handle getColor hook with non-struct return type' , ( ) => {
1072+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1073+
1074+ const testShader = myp5 . baseFilterShader ( ) . modify ( ( ) => {
1075+ myp5 . getColor ( ( inputs , canvasContent ) => {
1076+ // Simple test - just return a constant color
1077+ return [ 1.0 , 0.5 , 0.0 , 1.0 ] ; // Orange color
1078+ } ) ;
1079+ } , { myp5 } ) ;
1080+
1081+ // Create a simple scene to filter
1082+ myp5 . background ( 0 , 0 , 255 ) ; // Blue background
1083+
1084+ // Apply the filter
1085+ myp5 . filter ( testShader ) ;
1086+
1087+ // Check that the filter was applied (should be orange)
1088+ const pixelColor = myp5 . get ( 25 , 25 ) ;
1089+ assert . approximately ( pixelColor [ 0 ] , 255 , 5 ) ; // Red channel should be 255
1090+ assert . approximately ( pixelColor [ 1 ] , 127 , 5 ) ; // Green channel should be ~127
1091+ assert . approximately ( pixelColor [ 2 ] , 0 , 5 ) ; // Blue channel should be 0
1092+ } ) ;
1093+
1094+ test ( 'simple vector multiplication in filter shader' , ( ) => {
1095+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1096+
1097+ const testShader = myp5 . baseFilterShader ( ) . modify ( ( ) => {
1098+ myp5 . getColor ( ( inputs , canvasContent ) => {
1099+ // Test simple scalar * vector operation
1100+ const scalar = 0.5 ;
1101+ const vector = [ 1 , 2 ] ;
1102+ const result = scalar * vector ;
1103+ return [ result . x , result . y , 0 , 1 ] ;
1104+ } ) ;
1105+ } , { myp5 } ) ;
1106+ } ) ;
1107+
1108+ test ( 'handle complex filter shader with for loop and vector operations' , ( ) => {
1109+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1110+
1111+ const testShader = myp5 . baseFilterShader ( ) . modify ( ( ) => {
1112+ const r = myp5 . uniformFloat ( ( ) => 3 ) ; // Small value for testing
1113+ myp5 . getColor ( ( inputs , canvasContent ) => {
1114+ let sum = [ 0 , 0 , 0 , 0 ] ;
1115+ let samples = 1 ;
1116+
1117+ for ( let i = 0 ; i < r ; i ++ ) {
1118+ samples ++ ;
1119+ sum += myp5 . texture ( canvasContent , inputs . texCoord + ( i / r ) * [
1120+ myp5 . sin ( 4 * myp5 . PI * i / r ) ,
1121+ myp5 . cos ( 4 * myp5 . PI * i / r )
1122+ ] ) ;
1123+ }
1124+
1125+ return sum / samples ;
1126+ } ) ;
1127+ } , { myp5 } ) ;
1128+
1129+ // Create a simple scene to filter
1130+ myp5 . background ( 255 , 0 , 0 ) ; // Red background
1131+
1132+ // Apply the filter
1133+ myp5 . filter ( testShader ) ;
1134+
1135+ // The result should be some variation of the red background
1136+ const pixelColor = myp5 . get ( 25 , 25 ) ;
1137+ // Just verify it ran without crashing - exact color will depend on sampling
1138+ assert . isNumber ( pixelColor [ 0 ] ) ;
1139+ assert . isNumber ( pixelColor [ 1 ] ) ;
1140+ assert . isNumber ( pixelColor [ 2 ] ) ;
1141+ } ) ;
1142+ } ) ;
10691143 } ) ;
10701144} ) ;
0 commit comments