Skip to content

Instantly share code, notes, and snippets.

@andyli
Created April 10, 2011 19:54
Show Gist options
  • Select an option

  • Save andyli/912662 to your computer and use it in GitHub Desktop.

Select an option

Save andyli/912662 to your computer and use it in GitHub Desktop.
onthewings.stuffs.stuff12
/**
* Some of the renderings can be found at
* http://www.flickr.com/photos/andy-li/sets/72157625719497466/
*
*
* Copyright (c) 2011, Andy Li http://www.onthewings.net/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package onthewings.stuffs.stuff12;
import cpp.Sys;
import hxColorToolkit.spaces.RGB;
import hxColorToolkit.spaces.HSL;
import nme.geom.Point;
import nme.geom.Vector3D;
import org.casalib.util.NumberUtil;
import of.Context;
using of.Context.Functions;
using Std;
using Math;
using Lambda;
using DateTools;
using StringTools;
using nme.geom.Point;
using org.casalib.util.ArrayUtil;
using org.casalib.util.NumberUtil;
using org.casalib.util.GeomUtil;
using org.casalib.util.ConversionUtil;
using hxColorToolkit.ColorToolkit;
//Park-Miller-Carta Pseudo-Random Number Generator
//https://github.com/pnitsch/BitmapData.js/blob/master/js/BitmapData.js
class PRNG {
public var seed:Float;
public function new():Void {
seed = 1;
}
inline public function next(){
return gen() / 2147483647;
}
inline public function nextRange(min:Float, max:Float){
return min + ((max - min) * next());
}
inline public function gen(){
return seed = (seed * 16807) % 2147483647;
}
}
//https://github.com/pnitsch/BitmapData.js/blob/master/js/Simplex.js
class SimplexNoise {
var rand:PRNG;
static var grad3:Array<Array<Int>> =
[
[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],
[1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],
[0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]
];
var p:Array<Int>;
var perm:Array<Int>;
public function new(seed:Float):Void {
rand = new PRNG();
setSeed(seed);
}
public function setSeed(seed:Float):Void {
this.p = [];
this.rand.seed = seed;
for (i in 0...256) {
this.p[i] = Std.int(this.rand.nextRange(0, 255));
}
this.perm = [];
for(i in 0...512) {
this.perm[i]=this.p[i & 255];
}
}
inline static function dot(g:Array<Int>, x:Float, y:Float):Float {
return g[0]*x + g[1]*y;
}
public function noise(xin:Float, yin:Float):Float {
var n0, n1, n2;
var F2 = 0.5*(Math.sqrt(3.0)-1.0);
var s = (xin+yin)*F2;
var i = Std.int(xin+s);
var j = Std.int(yin+s);
var G2 = (3.0-Math.sqrt(3.0))/6.0;
var t = (i+j)*G2;
var X0 = i-t;
var Y0 = j-t;
var x0 = xin-X0;
var y0 = yin-Y0;
var i1, j1;
if(x0>y0) {i1=1; j1=0;}
else {i1=0; j1=1;}
var x1 = x0 - i1 + G2;
var y1 = y0 - j1 + G2;
var x2 = x0 - 1.0 + 2.0 * G2;
var y2 = y0 - 1.0 + 2.0 * G2;
var ii = i & 255;
var jj = j & 255;
var gi0 = this.perm[ii+this.perm[jj]] % 12;
var gi1 = this.perm[ii+i1+this.perm[jj+j1]] % 12;
var gi2 = this.perm[ii+1+this.perm[jj+1]] % 12;
var t0 = 0.5 - x0*x0-y0*y0;
if(t0<0) n0 = 0.0;
else {
t0 *= t0;
n0 = t0 * t0 * dot(grad3[gi0], x0, y0);
}
var t1 = 0.5 - x1*x1-y1*y1;
if(t1<0) n1 = 0.0;
else {
t1 *= t1;
n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
}
var t2 = 0.5 - x2*x2-y2*y2;
if(t2<0) n2 = 0.0;
else {
t2 *= t2;
n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
}
return 70.0 * (n0 + n1 + n2);
}
}
class Main extends BaseApp
{
var screenCap:Image;
var width:Int;
var height:Int;
inline static var subsample = 1;
override public function setup():Void {
setFrameRate(1);
enableAlphaBlending();
setCircleResolution(40);
enableSmoothing();
background(174, 232, 251);
width = getWidth();
height = getHeight();
screenCap = new Image();
screenCap.allocate(width, height, Constants.OF_IMAGE_COLOR);
var p = screenCap.getPixels();
for (i in 0...p.length) {
p[i] = cast 255;
}
screenCap.update();
}
override public function draw():Void {
var noiseImg = new Image();
noiseImg.allocate(width*subsample, height*subsample, Constants.OF_IMAGE_COLOR_ALPHA);
//perlinNoise modified from https://github.com/pnitsch/BitmapData.js/blob/master/js/BitmapData.js
var randomSeed = Date.now().getTime();
var imData:Array<Float> = [];
var simplexes = [];
for (i in 0...9) {
simplexes.push(new SimplexNoise(randomSeed++));
}
var pos = 0;
var tmp;
var max_a = Math.NEGATIVE_INFINITY;
for(y in 0...height*subsample) {
for(x in 0...width*subsample) {
var p1 = (simplexes[0].noise(x/250/subsample, y/250/subsample)+1)*0.5;/*
(
(simplexes[0].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[1].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[2].noise(x/250/subsample, y/250/subsample)+1)*0.5
)/3;*/
var p2 = (simplexes[3].noise(x/250/subsample, y/250/subsample)+1)*0.5;/*
(
(simplexes[3].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[4].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[5].noise(x/250/subsample, y/250/subsample)+1)*0.5
)/3;*/
var p3 = (simplexes[6].noise(x/250/subsample, y/250/subsample)+1)*0.5;/*
(
(simplexes[6].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[7].noise(x/250/subsample, y/250/subsample)+1)*0.5 +
(simplexes[8].noise(x/250/subsample, y/250/subsample)+1)*0.5
)/3;*/
var yuv = new hxColorToolkit.spaces.YUV(
p1.map(0,1,0,255),
255*0.5,
255*0.5
);
var xyz = yuv.toXYZ();
xyz.y += p2.map(0,1,0,100-xyz.y);
var hsl = xyz.toHSL();
hsl.lightness += p3.map(0,1,0,100-hsl.lightness);
var rgb = hsl.toRGB();
imData[pos++] = rgb.red.round();
imData[pos++] = rgb.green.round();
imData[pos++] = rgb.blue.round();
tmp = imData[pos++] = (p1 + p2 + p3) * 255;
if (max_a < tmp) max_a = tmp;
}
}
var p = noiseImg.getPixels();
var i = 0;
while (i < p.length){
p[i] = cast imData[i++];
p[i] = cast imData[i++];
p[i] = cast imData[i++];
p[i++] = cast 255;//imData[i++].constrain(max_a*0.6, max_a).map(max_a*0.6, max_a, 0, 255);
}
noiseImg.update();
noiseImg.resize(width, height);
setColor(255, 255, 255, 255);
noiseImg.draw(0,0);
}
override public function keyPressed(key:Int):Void {
if (key == 's'.charCodeAt(0)){
screenCap.grabScreen(0, 0, width, height);
screenCap.saveImage(Date.now().format("%Y%m%d_%H%M%S") + ".png");
} else if (key == 'a'.charCodeAt(0)) {
setup();
}
}
public static function main():Void {
//AppRunner.setupOpenGL(new AppGlutWindow(), 1680, 1050, Constants.OF_FULLSCREEN);
AppRunner.setupOpenGL(new AppGlutWindow(), 1024, 768, Constants.OF_WINDOW);
AppRunner.runApp(new Main());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment