-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add GLSL-based noise(vec2) function to p5.strands (#7897) #7921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
b924d1c
9cf4424
4cbf98f
6f66939
3b70b71
09f307d
df4c546
0cd9448
fd13238
27ebbe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import { parse } from 'acorn'; | ||
import { ancestor } from 'acorn-walk'; | ||
import escodegen from 'escodegen'; | ||
import noiseGLSL from './shaders/functions/noiseGLSL.glsl'; | ||
|
||
function shadergenerator(p5, fn) { | ||
|
||
|
@@ -1578,6 +1579,7 @@ function shadergenerator(p5, fn) { | |
], | ||
'sqrt': { args: ['genType'], returnType: 'genType', isp5Function: true}, | ||
'step': { args: ['genType', 'genType'], returnType: 'genType', isp5Function: false}, | ||
'noise': { args: ['vec2'], returnType: 'float', isp5Function: false }, | ||
'trunc': { args: ['genType'], returnType: 'genType', isp5Function: false}, | ||
|
||
////////// Vector ////////// | ||
|
@@ -1629,10 +1631,21 @@ function shadergenerator(p5, fn) { | |
return originalLerp.apply(this, args); // Fallback to normal p5.js lerp | ||
} | ||
}; | ||
fn.noise = function (...args) { | ||
if (!GLOBAL_SHADER?.isGenerating) { | ||
p5._friendlyError( | ||
`It looks like you've called noise() outside of a shader's modify() function.` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
return; | ||
} | ||
|
||
GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL); | ||
GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL); | ||
return fnNodeConstructor('noise', args, { args: ['vec2'], returnType: 'float' }); | ||
}; | ||
} | ||
|
||
|
||
|
||
export default shadergenerator; | ||
|
||
if (typeof p5 !== 'undefined') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
float baseNoise(vec2 st) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to have more such functions in the future like noise? I am not sure if we should create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. off the top of my head I think we were considering at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @LalitNarayanYadav while I tested it locally, I found that the things are little jittery and we want the noise to be smoother, maybe we just need a different basis for each octave. e.g. something like the examples here, but, maybe finding where they came from to double check the licenses, or doing something similar but different https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
perminder-17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fract(sin(dot(st.xy ,vec2(12.9898,78.233))) * 43758.5453123); | ||
} | ||
|
||
float noise(vec2 st) { | ||
float result = 0.0; | ||
for (int i = 0; i < 3; i++) { | ||
float freq = pow(2.0, float(i)); | ||
float amp = pow(0.5, float(i)); | ||
result += amp * baseNoise(st * freq); | ||
} | ||
return result; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in the glsl code, we have a
baseNoise()
function returning float. So, either you have to rename at the js file or in the glsl file. Probably changing frombaseNoise()
tonoise()
in the glsl is what I would prefer.