Created
January 5, 2019 22:29
-
-
Save sebastien/abb5d14adb18dbc44e31b4ed8d94827b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A study of rendering varyingly shaped points with randomized positions | |
// with a discrete gradient o density. | |
// | |
// Ref: https://www.tylerlhobbs.com/works/item/st-c | |
// | |
// It is an opportunity to study randomness (or pseudo-randomness) in curv, | |
// as well as how to define varying densities of points. | |
let | |
black = [0.051,0.051,0.051]; | |
white = [0.963,0.961,0942]; | |
blue = [181,235,235]/256; | |
// That's an infinite volume that we use to give a color to the paper sheet. | |
paper = make_shape { | |
dist (x,y,z,t) = 0; | |
is_2d = true; | |
is_3d = true; | |
}; | |
// The grid is useful for debugging | |
grid_x = make_shape { dist (x,y,z,t) = frac(x) ; is_2d = true ; is_3d = true } | |
>> offset 0.05; | |
grid_y = make_shape { dist (x,y,z,t) = frac(y) ; is_2d = true ; is_3d = true } | |
>> offset 0.05; | |
// We're going to introduce some level of variations for the dots: | |
// - some are going to be squarish, some are going to be roundish | |
// - some are going to be longer, some are going to be larger | |
dot (w,k) = make_shape { | |
dist (x,y,z,t) = | |
let x_ = x; y_ = y; in | |
// Min is union, max is intersection, lerp works to interpolate | |
// the shapes. | |
lerp( | |
// This is the distance function for a square. | |
max (abs(x_,y_) - w/2), | |
// That's the distance function for a circle. | |
(x_*x_ + y_*y_ + z*z - w/2), | |
k | |
) | |
; | |
is_2d = true; | |
is_3d = true; | |
}; | |
// Here we're creating a grid, we know that the dots are 1 unit wide | |
// (by default). We apply the `mod` function to *repeat* the shape | |
// along the X and Y axes. The step of the `mod` must be at least the | |
// width and the shifting should be the width. | |
dots shape = make_shape { | |
dist (x,y,z,t) = shape.dist( | |
mod(x,2) - 1, | |
mod(y,2) - 1, | |
z,t | |
); | |
is_2d = true; | |
is_3d = true; | |
}; | |
// Now for every dot we'll need to hide some of them. As each dot fits within | |
// a 2 x 2 cells. | |
nsin x = (sin (x) + 1) / 2; | |
roundby (a,b) = round(a/b) * b; | |
// A random function from the Book of Shaders https://thebookofshaders.com/10/ | |
rand x = frac(sin(x)*1000000.0); | |
mask shape = make_shape { | |
dist (x,y,z,t) = | |
max( | |
round(nsin (roundby(x,4)*10000)) * 1,//round(mod(x,4)/4) * round(mod(y,4)/4), | |
shape.dist(x,y,z,t) | |
); | |
is_2d = true; | |
is_3d = true; | |
} | |
in union [ | |
paper >> colour (white), | |
grid_x >> colour (blue), | |
grid_y >> colour (blue), | |
dot (1,0.5) >> dots >> mask >> colour (black) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great article, thanks, for the information, I love finding interesting information, and I love it when it is freely shared. Therefore, when I need to complete some kind of project in college, I browse this site and also look for information on similar sites.