-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 6 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,20 @@ function shadergenerator(p5, fn) { | |
| return originalLerp.apply(this, args); // Fallback to normal p5.js lerp | ||
| } | ||
| }; | ||
| fn.noise = function (...args) { | ||
| if (GLOBAL_SHADER?.isGenerating) { | ||
|
||
| GLOBAL_SHADER.output.vertexDeclarations.add(noiseGLSL); | ||
| GLOBAL_SHADER.output.fragmentDeclarations.add(noiseGLSL); | ||
LalitNarayanYadav marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return fnNodeConstructor('noise', args, { args: ['vec2'], returnType: 'float' }); | ||
| } else { | ||
| p5._friendlyError( | ||
| `It looks like you've called noise() outside of a shader's modify() function.` | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| 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) { | ||
|
||
| 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.