@@ -8,6 +8,7 @@ import { parse } from 'acorn';
88import { ancestor } from 'acorn-walk' ;
99import escodegen from 'escodegen' ;
1010import noiseGLSL from './shaders/functions/noiseGLSL.glsl' ;
11+ import noise3DGLSL from './shaders/functions/noise3DGLSL.glsl' ;
1112
1213function shadergenerator ( p5 , fn ) {
1314
@@ -1663,23 +1664,34 @@ function shadergenerator(p5, fn) {
16631664 const originalNoise = fn . noise ;
16641665 fn . noise = function ( ...args ) {
16651666 if ( ! GLOBAL_SHADER ?. isGenerating ) {
1666- return originalNoise . apply ( this , args ) ; // fallback to regular p5.js noise
1667+ return originalNoise . apply ( this , args ) ; // fallback to p5.js noise
16671668 }
1668-
1669- GLOBAL_SHADER . output . vertexDeclarations . add ( noiseGLSL ) ;
1670- GLOBAL_SHADER . output . fragmentDeclarations . add ( noiseGLSL ) ;
1671- // Handle noise(x, y) as noise(vec2)
1669+
16721670 let nodeArgs ;
1673- if ( args . length === 2 ) {
1671+
1672+ if ( args . length === 3 ) {
1673+ // GLSL vec3 noise(x, y, z)
1674+ nodeArgs = [ fn . vec3 ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ] ;
1675+ GLOBAL_SHADER . output . vertexDeclarations . add ( noise3DGLSL ) ;
1676+ GLOBAL_SHADER . output . fragmentDeclarations . add ( noise3DGLSL ) ;
1677+
1678+ return fnNodeConstructor ( 'noise' , nodeArgs , {
1679+ args : [ 'vec3' ] ,
1680+ returnType : 'float' ,
1681+ } ) ;
1682+ } else if ( args . length === 2 ) {
1683+ // GLSL vec2 noise(x, y)
16741684 nodeArgs = [ fn . vec2 ( args [ 0 ] , args [ 1 ] ) ] ;
1675- } else {
1676- nodeArgs = args ;
1685+ GLOBAL_SHADER . output . vertexDeclarations . add ( noiseGLSL ) ;
1686+ GLOBAL_SHADER . output . fragmentDeclarations . add ( noiseGLSL ) ;
1687+
1688+ return fnNodeConstructor ( 'noise' , nodeArgs , {
1689+ args : [ 'vec2' ] ,
1690+ returnType : 'float' ,
1691+ } ) ;
16771692 }
16781693
1679- return fnNodeConstructor ( 'noise' , nodeArgs , {
1680- args : [ 'vec2' ] ,
1681- returnType : 'float'
1682- } ) ;
1694+ return originalNoise . apply ( this , args ) ; // fallback again if unexpected
16831695 } ;
16841696
16851697}
0 commit comments