Page 1 of 1

Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 5:12 am
by stratified
Hi everyone,

Now that we generated the Mandelbrot set, here's a script to generate a Julia set. The principle is the same. The only thing to note here is that there's a function to convert from the HSL colour model to the RGB model. The HSL model (or HSV) comes in very handy when you'd like to generate a full colour spectrum from a single value. Here is the code:

Code: Select all
-- @description Creates a Julia set texture node.
-- @author      Stratified
-- @version     0.1

-- radius 2 squared
local RADIUS2        = 4
-- max iterations before we stop
local MAX_ITERATIONS = 255
-- real and imaginary part of the constant
local C = { -0.7, 0.27015 }

-- Converts HSL to RGB (input and output range: 0 - 255)
function HSL(h, s, l)
   if s == 0 then return l,l,l end
   h, s, l = h/256*6, s/255, l/255
   local c = (1-math.abs(2*l-1))*s
   local x = (1-math.abs(h%2-1))*c
   local m,r,g,b = (l-.5*c), 0,0,0
   if h < 1     then r,g,b = c,x,0
   elseif h < 2 then r,g,b = x,c,0
   elseif h < 3 then r,g,b = 0,c,x
   elseif h < 4 then r,g,b = 0,x,c
   elseif h < 5 then r,g,b = x,0,c
   else              r,g,b = c,0,x
   return math.ceil((r+m)*256),math.ceil((g+m)*256),math.ceil((b+m)*256)

-- Checks if a pixel in the image plane is in the julia set.
-- Coordinates (x, y) need to be in the range [-1, 1]. Returns if the point is in the set and
-- the number of iterations to figure that out.
local function julia(x, y)

   local zr, zi = x, y
    local cr, ci = C[1], C[2]
   for k=1,MAX_ITERATIONS do

        -- calculate z(n+1) from z(n)
      local zrzi2 = zr * zi * 2
      local zr2   = zr * zr
      local zi2   = zi * zi

        -- check if we're still in the circle with radius 2, if not the point isn't
        -- in the Julia set anymore.
      if (zi2 + zr2 > RADIUS2) then
         return false, k

        -- calculate the values for the next iteration
      zr = zr2 - zi2 + cr
      zi = ci + zrzi2

    -- after MAX_ITERATIONS, we're pretty confident that the point is in the julia set.
    return true, MAX_ITERATIONS

-- Generates a Julia set centered around the centre of the image.
function genJuliaTex(w, h)

    -- create a data structure for the image
    local image  = {}
    image.size   = { w, h }
    image.type   = octane.image.type.LDR_RGBA
    image.buffer = {}

    count = 0

    -- see which pixels are in the Julia set
   for y = 0, h-1 do
      for x = 0, w-1 do
            -- transform x so it's in the range [-1, 1]
            local sx = 2 * (x / w) - 1
            -- transform y so it's in the range [-1, 1]
            local sy = 2 * (y / h) - 1

            local inSet, iterations = julia(sx, sy)
            local r, g, b = 0, 0, 0
            if inSet then
                -- point in the set are red
                r, g, b = 255, 0, 0
                -- points outside are coloured depending on how quickly they
                -- converged.
                r, g, b = HSL(iterations, 127, 127)

            -- colour pixel
            table.insert(image.buffer, r)
            table.insert(image.buffer, g)
            table.insert(image.buffer, b)
            table.insert(image.buffer, 255)

    return image

-- dimensions of the generated texture
local WIDTH, HEIGHT = 500, 500

-- actually create the julia texture
local tex = genJuliaTex(WIDTH, HEIGHT)
assert(#tex.buffer == 4 * WIDTH * HEIGHT, "invalid buffer length")

-- create an image texture node
texNode = octane.node.create{ type=octane.NT_TEX_IMAGE, name="Julia Set Texture" }
-- set up the gradient in the attributes
texNode:setAttribute(octane.A_BUFFER , tex.buffer , false)
texNode:setAttribute(octane.A_SIZE   , tex.size   , false)
texNode:setAttribute(octane.A_TYPE   , tex.type   , false)
-- evaluate the texture node

Here's a picture of the generated texture:

The julia set generated in Lua


Re: Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 5:31 am
by pixelrush
These are quite cool manifestations ;)
Not to cramp your style but can you show me a more mundane use for the creation of textures in Lua please.
What type of thing can I do that I can make use of in a regular scene?

Re: Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 7:28 am
by stratified
Julia textures are nice to look at and it gives me something better to do than watch NZ's got talent on a Sunday evening (Can't do that anyway since they pulled the plug on 1 Dec ;)

Generating procedural textures gives you full control and more flexibility. You could for example generate a procedural bump map and use this to animate something. In the future, generate a procedural displacement map for an animation, ...

Procedural noise is just everywhere in CG, there are gazillions of noise functions that Octane doesn't support.

I bet there's in each big CG studio a big pile of code just generating procedural stuff. People even write a books about it: (One of the authors is Ken Perlin of Perlin noise fame)

If all else fails, you could save out the Julia texture and use it as a painting in your interior scenes. It looks sophisticated...


Re: Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 8:20 am
by pixelrush
Ah yes, I see, ok so a lot of potential. Animated procedural displacement sounds, well...exciting even ;)
I looked here for some noise examples - ... noise.html

OT: I tend to shop around the net for mindless entertainment.
Some NZers appear in Australian shows too.
Some amazing singing talent out there from all manner of places. Philippines, Ukraine, China...

Re: Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 10:18 am
by bepeg4d
wow, this is great :D
for me is a jump back to the past :) the first 3d application that i have learned, some years ago, was Infini-D and there were only two kind of procedurals, mandelbrot and julia, no turbulance or other kind of noise. and i always used julia the most, so, thanks again thomas ;)
it remember me Kei's Power Tools by Kei Krause and Bryce 1.0... if someone will create an interface for fractals generation in lua, we will have the power of bryce inside octane when the displacement will arrive :shock:
here is julia in action ;)


ciao beppe

Re: Generating a Julia texture in Lua

PostPosted: Sun Jan 05, 2014 7:28 pm
by stratified
Hi Bepe,

Those materials are awesome! I always find the Julia set so mesmerizing to look at, it's just stunning.

If I find some free time, I'll try to create some tool around it. It's going to be a hobby project so I don't know when of even if ;)