Skip to content

Commit

Permalink
Changes in turbulence (#57)
Browse files Browse the repository at this point in the history
* input api

* change input to be not a uniform

* change input to be not a uniform

* rename and expose changes

* add doc

* Update src/effects/turbulence.js

---------

Co-authored-by: Yehonatan Daniv <[email protected]>
  • Loading branch information
marine-bre and ydaniv authored Dec 16, 2024
1 parent e0861a4 commit edfa10f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/effects/turbulence.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @param {number} [params.octaves=1] initial number of octaves to use for turbulence noise generation.
* @param {boolean} [params.isFractal=false] initial number of octaves to use for turbulence noise generation.
* @param {number} [params.time=0] initial time for controlling initial noise value.
* @param {string} [params.input] how to define `turbulenceSeed`. Defaults to `turbulence.FRAGCOORD_XY_TIME`.
* @returns {turbulenceEffect}
*
* @example turbulence({noise: kampos.noise.simplex, output: turbulence.COLOR, octaves: 4, isFractal: true})
Expand All @@ -20,6 +21,7 @@ function turbulence({
octaves = 1,
isFractal = false,
time = 0.0,
input = INPUT_TYPES.FRAGCOORD_XY_TIME,
}) {
const { x: fx, y: fy } = frequency || { x: 0.0, y: 0.0 };

Expand Down Expand Up @@ -82,10 +84,10 @@ float turbulence (vec3 seed, vec2 frequency, int numOctaves, bool isFractal) {
return clamp(sum, 0.0, 1.0);
}`,
source: `
vec3 turbulenceSeed = vec3(gl_FragCoord.xy, u_time * 0.0001);
${input || ''}
float turbulenceValue = turbulence(turbulenceSeed, u_turbulenceFrequency, u_turbulenceOctaves, u_isFractal);`,
main: `
${output || ''}`
${output || ''}`,
},
get frequency() {
const [x, y] = this.uniforms[0].data;
Expand Down Expand Up @@ -143,7 +145,21 @@ const OUTPUT_TYPES = {
ALPHA: 'alpha = turbulenceValue;',
};

const INPUT_TYPES = {
FRAGCOORD_XY_TIME:
'vec3 turbulenceSeed = vec3(gl_FragCoord.xy, u_time * 0.0001)',
FRAGCOORD_XYZ: 'vec3 turbulenceSeed = vec3(gl_FragCoord.xyz)',
FRAGCOORD_XY_MOUSE_TIME:
'vec3 turbulenceSeed = vec3(gl_FragCoord.x + u_mouse.x * u_resolution.x * -1.0, gl_FragCoord.y + u_mouse.y * u_resolution.y, u_time * 0.0001);',
FRAGCOORD_XY_MOUSE_Z:
'vec3 turbulenceSeed = vec3(gl_FragCoord.x + u_mouse.x * u_resolution.x * -1.0, gl_FragCoord.y + u_mouse.y * u_resolution.y);',
};

turbulence.COLOR = OUTPUT_TYPES.COLOR;
turbulence.ALPHA = OUTPUT_TYPES.ALPHA;
turbulence.FRAGCOORD_XY_TIME = INPUT_TYPES.FRAGCOORD_XY_TIME;
turbulence.FRAGCOORD_XYZ = INPUT_TYPES.FRAGCOORD_XYZ;
turbulence.FRAGCOORD_XY_MOUSE_TIME = INPUT_TYPES.FRAGCOORD_XY_MOUSE_TIME;
turbulence.FRAGCOORD_XY_MOUSE_Z = INPUT_TYPES.FRAGCOORD_XY_MOUSE_Z;

export default turbulence;

0 comments on commit edfa10f

Please sign in to comment.