Page 1 of 1

### Generating a Julia texture in Lua

Posted: Sun Jan 05, 2014 5:12 am
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 squaredlocal RADIUS2        = 4-- max iterations before we stoplocal MAX_ITERATIONS = 255-- real and imaginary part of the constantlocal 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   end   return math.ceil((r+m)*256),math.ceil((g+m)*256),math.ceil((b+m)*256)end-- 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       end        -- calculate the values for the next iteration      zr = zr2 - zi2 + cr      zi = ci + zrzi2   end    -- after MAX_ITERATIONS, we're pretty confident that the point is in the julia set.    return true, MAX_ITERATIONS end-- 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            else                -- points outside are coloured depending on how quickly they                -- converged.                r, g, b = HSL(iterations, 127, 127)            end            -- colour pixel            table.insert(image.buffer, r)            table.insert(image.buffer, g)            table.insert(image.buffer, b)            table.insert(image.buffer, 255)      end   end    return imageend-- dimensions of the generated texturelocal WIDTH, HEIGHT = 500, 500-- actually create the julia texturelocal tex = genJuliaTex(WIDTH, HEIGHT)assert(#tex.buffer == 4 * WIDTH * HEIGHT, "invalid buffer length")-- create an image texture nodetexNode = octane.node.create{ type=octane.NT_TEX_IMAGE, name="Julia Set Texture" }-- set up the gradient in the attributestexNode: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 nodetexNode:evaluate()`

Here's a picture of the generated texture:

The julia set generated in Lua

cheers,
Thomas

### Re: Generating a Julia texture in Lua

Posted: Sun Jan 05, 2014 5:31 am
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

Posted: Sun Jan 05, 2014 7:28 am
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: http://www.amazon.com/Texturing-Modeling-Third-Edition-Procedural/dp/1558608486 (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...

cheers,
Thomas

### Re: Generating a Julia texture in Lua

Posted: Sun Jan 05, 2014 8:20 am
Ah yes, I see, ok so a lot of potential. Animated procedural displacement sounds, well...exciting even
I looked here for some noise examples - http://www.neilblevins.com/cg_education ... 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...
later..

### Re: Generating a Julia texture in Lua

Posted: Sun Jan 05, 2014 10:18 am
wow, this is great
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
here is julia in action

ciao beppe

### Re: Generating a Julia texture in Lua

Posted: Sun Jan 05, 2014 7:28 pm
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

cheers,
Thomas