@@ -1067,6 +1067,108 @@ suite('p5.Shader', function() {
10671067 } ) ;
10681068 } ) ;
10691069
1070+ suite ( 'passing data between shaders' , ( ) => {
1071+ test ( 'handle passing a value from a vertex hook to a fragment hook' , ( ) => {
1072+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1073+
1074+ const testShader = myp5 . baseMaterialShader ( ) . modify ( ( ) => {
1075+ let worldPos = myp5 . varyingVec3 ( ) ;
1076+ myp5 . getWorldInputs ( ( inputs ) => {
1077+ worldPos = inputs . position . xyz ;
1078+ return inputs ;
1079+ } ) ;
1080+ myp5 . getFinalColor ( ( c ) => {
1081+ return [ myp5 . abs ( worldPos / 25 ) , 1 ] ;
1082+ } ) ;
1083+ } , { myp5 } ) ;
1084+
1085+ myp5 . background ( 0 , 0 , 255 ) ; // Make the background blue to tell it apart
1086+ myp5 . noStroke ( ) ;
1087+ myp5 . shader ( testShader ) ;
1088+ myp5 . plane ( myp5 . width , myp5 . height ) ;
1089+
1090+ // The middle should have position 0,0 which translates to black
1091+ const midColor = myp5 . get ( 25 , 25 ) ;
1092+ assert . approximately ( midColor [ 0 ] , 0 , 5 ) ;
1093+ assert . approximately ( midColor [ 1 ] , 0 , 5 ) ;
1094+ assert . approximately ( midColor [ 2 ] , 0 , 5 ) ;
1095+
1096+ // The corner should have position 1,1 which translates to yellow
1097+ const cornerColor = myp5 . get ( 0 , 0 ) ;
1098+ assert . approximately ( cornerColor [ 0 ] , 255 , 5 ) ;
1099+ assert . approximately ( cornerColor [ 1 ] , 255 , 5 ) ;
1100+ assert . approximately ( cornerColor [ 2 ] , 0 , 5 ) ;
1101+ } ) ;
1102+
1103+ test . only ( 'handle passing a value from a vertex hook to a fragment hook' , ( ) => {
1104+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1105+
1106+ const testShader = myp5 . baseMaterialShader ( ) . modify ( ( ) => {
1107+ let worldPos = myp5 . varyingVec3 ( ) ;
1108+ myp5 . getWorldInputs ( ( inputs ) => {
1109+ worldPos . xyz = inputs . position . xyz ;
1110+ return inputs ;
1111+ } ) ;
1112+ myp5 . getFinalColor ( ( c ) => {
1113+ return [ myp5 . abs ( worldPos / 25 ) , 1 ] ;
1114+ } ) ;
1115+ } , { myp5 } ) ;
1116+
1117+ console . log ( 'VERTEX SHADER OUTPUT:' ) ;
1118+ console . log ( testShader . vertSrc ( ) ) ;
1119+
1120+ myp5 . background ( 0 , 0 , 255 ) ; // Make the background blue to tell it apart
1121+ myp5 . noStroke ( ) ;
1122+ myp5 . shader ( testShader ) ;
1123+ myp5 . plane ( myp5 . width , myp5 . height ) ;
1124+
1125+ // The middle should have position 0,0 which translates to black
1126+ const midColor = myp5 . get ( 25 , 25 ) ;
1127+ assert . approximately ( midColor [ 0 ] , 0 , 5 ) ;
1128+ assert . approximately ( midColor [ 1 ] , 0 , 5 ) ;
1129+ assert . approximately ( midColor [ 2 ] , 0 , 5 ) ;
1130+
1131+ // The corner should have position 1,1 which translates to yellow
1132+ const cornerColor = myp5 . get ( 0 , 0 ) ;
1133+ assert . approximately ( cornerColor [ 0 ] , 255 , 5 ) ;
1134+ assert . approximately ( cornerColor [ 1 ] , 255 , 5 ) ;
1135+ assert . approximately ( cornerColor [ 2 ] , 0 , 5 ) ;
1136+ } ) ;
1137+
1138+ test ( 'handle passing a value from a vertex hook to a fragment hook as part of hook output' , ( ) => {
1139+ myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
1140+
1141+ const testShader = myp5 . baseMaterialShader ( ) . modify ( ( ) => {
1142+ let worldPos = myp5 . varyingVec3 ( ) ;
1143+ myp5 . getWorldInputs ( ( inputs ) => {
1144+ worldPos = inputs . position . xyz ;
1145+ inputs . position . xyz = worldPos + [ 25 , 25 , 0 ] ;
1146+ return inputs ;
1147+ } ) ;
1148+ myp5 . getFinalColor ( ( c ) => {
1149+ return [ myp5 . abs ( worldPos / 25 ) , 1 ] ;
1150+ } ) ;
1151+ } , { myp5 } ) ;
1152+
1153+ myp5 . background ( 0 , 0 , 255 ) ; // Make the background blue to tell it apart
1154+ myp5 . noStroke ( ) ;
1155+ myp5 . shader ( testShader ) ;
1156+ myp5 . plane ( myp5 . width , myp5 . height ) ;
1157+
1158+ // The middle (shifted +25,25) should have position 0,0 which translates to black
1159+ const midColor = myp5 . get ( 49 , 49 ) ;
1160+ assert . approximately ( midColor [ 0 ] , 0 , 5 ) ;
1161+ assert . approximately ( midColor [ 1 ] , 0 , 5 ) ;
1162+ assert . approximately ( midColor [ 2 ] , 0 , 5 ) ;
1163+
1164+ // The corner (shifted +25,25) should have position 1,1 which translates to yellow
1165+ const cornerColor = myp5 . get ( 25 , 25 ) ;
1166+ assert . approximately ( cornerColor [ 0 ] , 255 , 5 ) ;
1167+ assert . approximately ( cornerColor [ 1 ] , 255 , 5 ) ;
1168+ assert . approximately ( cornerColor [ 2 ] , 0 , 5 ) ;
1169+ } ) ;
1170+ } ) ;
1171+
10701172 suite ( 'filter shader hooks' , ( ) => {
10711173 test ( 'handle getColor hook with non-struct return type' , ( ) => {
10721174 myp5 . createCanvas ( 50 , 50 , myp5 . WEBGL ) ;
0 commit comments