Furious Furball – MoleHill/Stage3D experiment with Source

Click any of the above images to see an interactive mouse reactive live version – Flash Player 11+ is required.

Recently I had the opportunity to experiment with the new Stage3D api for Flash. Not having done much work with Flash in a while, and having spent a fair bit of time with WebGL recently, it was interesting to test this api. Working with meshes was super easy, if not as flexible as webGL. Working with shaders was done the hard way, with AGAL, it would have been easier with pixelbender 3d. WebGL certainly makes working with point sprites a whole lot easier. In this example I incorporated a textured quad technique similar to that mentioned in this article on particles. The audio is an edit from Boucepholous Bouncing Ball by Aphex Twin.

The source from this experiment can be downloaded on an as is basis.

WebGL particles and pointsprites experiment

This webGL demo uses 50000 particles, rendered as pointsprites influenced by samples from the image below. The z position of each particle is influenced by the luminosity of each sample and a sine wave. You can rotate the particles with the mouse.

The image used for this demo. I'd provide an attribution but I can't remember where I got it from!

WebGL – using framebuffers and shaders to create a bloom effect

This webGL demo creates a glow effect using following steps

  • 1) Render an opaque (in this case yellow on white) silhouette of the model as a texture attached to an offscreen framebuffer
  • 2) Render the texture to another framebuffer, blurring the image with the fragment shader
  • 3) Repeat step 2 for further blur, but targetting the framebuffer set up for step 1
  • 4) Render the blurred image to the screen and blend it with a rendering of the original model

Meet the Pyro – WebGL JSON model with normal and specular mapping

Here’s a WebGL model of the Pyro’s mask from Team Fortress 2. I started with a reference model from the steam workshop and used the following tools and techniques to get it in the browser -

  • Blender – for modelling, UV unwrapping, texturing, and creating normal maps from a hi-res version.
  • Crazy Bump – for help with normal maps.
  • Photoshop – for texturing
  • Y.A.W.G.L.E Blender add-on, for exporting the vertices, normals, and texture coordinates in JSON format.
  • (Note I could only get this to work in Blender 2.57)

Shaders and Lighting

The vertex shader deals with hemispheric lighting, while the fragment shader deals with specular and diffuse light and also the normal map. The model uses three textures. The base texture which is a straight color map, a normal map which adds some detail to the mask (such as the grill at the front) without adding geometry, and a specular map which alters both the color and the shininess of different parts of the mask.

WebGL Particle Effects


Hereis a WebGL demo that features a particle effect with animation controlled by the vertex shader.

Techniques used in this demo include

  • Using simplex noise to generate the color distribution for particles randomly distributed around a sphere.
  • Using the vertex shader to calculate the orbital position for each particle based on elapsed time and velocity. Velocity is determined by vertex colour.

Strange Attractors and WebGL

lorenz strange attractor

Last year I did a number of experiments using WebGL, the recent (and still experimental) standard for rendering hardware accelerated graphics in the browser. This experiment is a Lorenz strange attractor. It’s a bit bleeding edge, so you’ll need an up to date version of Google Chrome to view it. It may work in other browsers, but at the moment I’m concentrating on Chrome to keep things simpler, and even then Chrome will sometimes update itself and break WebGL content by introducing regression bugs into the implementation.

View the demo.

Minimal Mandelbrot Fractal in Javascript

The purpose of this post is to show how to plot the Mandelbrot Set using javascript and the canvas API. We’ve seen the Mandelbrot fractal image a million times, and there are many examples around of fractal explorers that let you visualise fractals, alter parameters, and zoom infinately far into their complex shapes. The code in this example strips things back to show how the basic map is plotted so that the algorithm is clear. It is based heavily on the excellent tutorial and C++ example found here, and is a reasonably close port to javascript. The code is written for clarity rather than optimization.

I recommend reading the tutorial and getting a general idea of how the algorithm works, having some knowledge of what complex and imaginary numbers are helps too. The javascript version of the code plots the image seen above, but can be easily altered to plot different patterns using a Julia Set. All you need to do is change the cReal and cImaginary variables to be constant values, as described in the tutorial.

Changing the real and imaginary limit variables will change the scaling of the set, and changing the numIterations variable will alter the level of detail (increasing it won’t change much of what you can see but will slow things down).

The Mandelbrot Set consists of the points in black, rather than the coloured points. This example using a similar method to the C++ version to colour the pixels, however it would be easy to change that part of the code to use other colouring methods.

With a fair understanding of how the code works it could be expanded to make Mandelbrot or Julia Set explorers such as this, this, this or this (yes they are popular), or just generate your own interesting fractal images.

Meanwhile I’m looking forward to what the Fractal.io people do next.


window.addEventListener("load",App,false);

function App() {
    var theCanvas = document.getElementById("canvas1");
    var context = theCanvas.getContext("2d");
    drawMandelbrot(context);
}

function drawMandelbrot(context) {
    // Create a canvas imagedata object to draw on
    var width = 800, height = 600;
    var img = context.createImageData(width,height)

    //real number limits (represented on the x axis)
    var minReal = -2.0;
    var maxReal = 1.0;
    //imaginary number limits (represented on the y axis)
    var minImaginary =  -1.2;
    var maxImaginary = minImaginary + (maxReal-minReal) * height/width; 

    // maximum number of iterations influences level of detail
    var numIterations = 50;

    // used for converting between pixel resolution and the complex number plane limits
    var realFactor = (maxReal-minReal)/(width-1);
    var imaginaryFactor = (maxImaginary-minImaginary)/(height-1);

    // used to calculate whether a point lies within the mandelbrot set
    var cImaginary,cReal;
    var zReal,zImaginary;
    var zReal2,zImaginary2;
    // a boolean to indicate whether the point was within the mandelbrot set
    var insideSet;

    //calculate rows (y/imaginary axis)
    for (var y = 0;y < height;y++) {
        cImaginary = maxImaginary - y*imaginaryFactor;
        //calculate columns (x/real axis);
        for (var x = 0;x < width;x++) {
           cReal = minReal + x*realFactor;
           zReal = cReal;
           zImaginary = cImaginary;
           //assume the value is within set
           inside = true;
           //iterate
           for (var i=0;i<numIterations;i++) {
                zReal2 = zReal*zReal;
                zImaginary2 = zImaginary*zImaginary;

                //check to see if Z will tend to infinity (not inside set)
                if (zReal2 + zImaginary2 > 4) {
                    inside = false;
                    break;  // if not inside set then exit the for loop
                }
                zImaginary = 2*zReal*zImaginary + cImaginary;
                zReal = zReal2 - zImaginary2 + cReal;
            }

            if (inside) {
                // points inside the set are set to black
                setPixel(img,x,y,0,0,0,255);
            } else {
                // points outside the set are colored black to red and red to white
                if (i < numIterations /2 -1) {
                    setPixel(img,x,y,i*10,0,0,255);
                } else {
                    setPixel(img,x,y,i*10,255,255,255);
                }
            }
        }
    }
    context.putImageData(img,0,0) 
}

// writes rgba values to a pixel in the image data pixel array
function setPixel(imgData, x, y, r, g, b, a) {
    offset = (x + y * imgData.width) * 4;
    imgData.data[offset+0] = r;
    imgData.data[offset+1] = g;
    imgData.data[offset+2] = b;
    imgData.data[offset+3] = a;
}

A rollercoaster, a serpent, a rope and a slinky

 

These are all structures built from points and sticks that are constrained using verlet integration, the system which is often used in ragdoll physics in gaming.

Speaking of game physics, Seth Ladd has published a truly excellent series of articles on a javascript port of box2d (the physics engine orginally written by Erin Catto).

I’m using up to 1200 particles in these demos, with little optimization, and there is still headroom. Javascript has definitely come a long way when it comes to speed.

A list of books useful for creative coding and generative art

I’ll continue to add to this list as I find more books. I’d be interested in hearing about more books that have some kind of relationship to creative coding, but aren’t about code itself, such as the science books listed at the bottom.

General

Digital by Design: Crafting Technology for Products and Environments Conny Freyer, Sebastien Noel, Eva Rucki and Paola Antonelli

Handbook of regular patterns Peter S. Stevens

A Touch of Code: Interactive Installations and Experiences R. Klanten (Author, Editor), S. Ehmann (Editor), L. Feireiss (Editor)

Form+Code in Design, Art, and Architecture Casey Reas and Chandler McWilliams
Generative Art Matt Pearson

Written Images

Processing

Programming Interactivity: A Designer’s Guide to Processing, Arduino, and Openframeworks Joshua Noble

Processing: A Programming Handbook for Visual Designers and Artists Ben Fry, John Maeda

Getting Started With Processing Casey Reas, Ben Fry

Learning Processing: A Beginner’s Guide to Programming Images, Animation, and Interaction Daniel Shiffman

Processing for Visual Artists: How to Create Expressive Images and Interactive Art Andrew Glassner

Processing: Creative Coding and Computational Art Ira Greenberg

Canvas/Javascript

HTML5 Canvas Steve Fulton, Jeff Fulton

Science and Mathematics

These books contain great information about how nature actually works, and provide a lot of inspiration for creative code projects.

Shapes: Nature’s Patterns: A Tapestry in Three Parts Philip Ball

Branches: Nature’s Patterns: A Tapestry in Three Parts Philip Ball

Flow: Nature’s Patterns: A Tapestry in Three Parts Philip Ball

Chaos: Making a New Science James Gleick

Three Interactive Trees

Here are three trees created and animated using recursion. Click one of the images to use the interactive version. Each one has controls that will alter aspects of the animation. These require a canvas enabled browser, such as Chrome or Firefox.

Bending Tree 1

Bending Tree 2

Bending Tree 2

Bending Tree 3

Bending Tree 3