Skip to content

Instantly share code, notes, and snippets.

@prakharaditya
prakharaditya / GLSL-Noise.md
Created January 15, 2022 12:02 — forked from patriciogonzalezvivo/GLSL-Noise.md
GLSL Noise Algorithms

Generic 1,2,3 Noise

float rand(float n){return fract(sin(n) * 43758.5453123);}

float noise(float p){
	float fl = floor(p);
  float fc = fract(p);
	return mix(rand(fl), rand(fl + 1.0), fc);
}
@prakharaditya
prakharaditya / ordered_dithering.txt
Created December 30, 2021 18:51 — forked from MehdiNS/ordered_dithering.txt
Ordered dithering explanation
(Written as a way to stop forgetting these things.
May be wrong sometimes, as it's just the result of my research here and there.
If it helps you, give me a shout!)
Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image
----------------------------------------------------------------
INTRODUCTION
// http://www.iquilezles.org/www/articles/palettes/palettes.htm
// to see this function graphed out go to: https://www.desmos.com/calculator/18rq4ybrru
vec3 cosPalette( float t , vec3 brightness, vec3 contrast, vec3 osc, vec3 phase)
{
return brightness + contrast*cos( 6.28318*(osc*t+phase) );
}
vec3 hsv2rgb(vec3 c)
{
$red: #FF0000
$green: #00FF00
$blue: #0000FF
$black: #000000
$white: #FFFFFF
$gray: #808080
$cyan: #00FFFF
$magenta: #FF00FF
$yellow: #FFFF00
$brown: #996633

Generator Functions

What are generator functions?

Generator functions are basically functions that you can pause at some point. You can define a generator function with function*:

function* Generator() {
  // code goes here
}

I like to named my generator functions with a capital, for reasons that will become apparent later, but generally they are named with a lowercase letter.

  • map => Takes an array, and then applies a function to every element of that array.
  • filter => Takes an array, and passes every element through a function. It only keeps the element if the function returns true.
  • sort => By default, it sorts the elements of an array in alphabetical order. But, if you give it a function, it will use it to sort the array in the following way:
    • The function takes two arguments to compare, a and b.
    • The function returns a negative number if a is lower than b.
    • The function returns zero if a has the same value as b.
    • The function returns a positive number is a is higher than b.
  • reduce => Starts with an accumulator. It starts with the first element of the array. Then it goes through the rest of the elements, and for each one, it applies a function to the accumulator and the element and puts the result back into the accumulator. The function returns what the accumulator is at the end.

Examples

WEBGL // p5 WEBGL rendering mode.
createCanvas(w, h, renderer) // Creates a 3D canvas (if renderer is WEBGL).
// Primitives
plane(width, height) // Creates a plane in 3D space. Equivalent to rect() in the default rendering mode.
plane(width, height, detailX, detailY) // Creates a plane in 3D space with the number of triangle subdivisions specified.
box(width) // Creates a cube in 3D space.
box(width, height, depth) // Creates a cuboid in 3D space.
box(width, height, depth, detailX, detailY) // Creates a cuboid in 3D space with triangle subdivisions.
sphere(radius) // Creates a sphere in 3D space.