In this example we'll explain how to create an image texture in Lua. At the end you should end up with a nice gradient texture. Let's have a look at (some of) the attributes of the image texture node (octane.NT_TEX_IMAGE):
Code: Select all
A_BUFFER
• Description : Raw byte uncompressed data for the image.
• Type : AT_BYTE
• Array : true
A_SIZE
• Description : Size of the image in pixels.
• Type : AT_INT2
• Array : false
• Default Value : 0 0
A_TYPE
• Description : The image type, i.e. the data format used in A_BUFFER. Must be of type ImageType.
• Type : AT_INT
• Array : false
• Default Value : 0
encounter a new line in the image we recalculate the interpolation factor (it's a bit a waste of time to calculate the colour for each pixel but hey it's an example

Code: Select all
-- Creates an LDR gradient image (4 bytes/pixel).
--
-- @param[in] x
-- horizontail size of the image
-- @param[in] y
-- vertical size of the image
-- @param[in] c1
-- start colour for the gradient.
-- @param[in] c2
-- end colour for the gradient.
-- @return
-- image buffer with a top to bottom gradient c1 > c2
local function createGradient(x, y, c1, c2)
-- create a data structure for the image
local image = {}
image.size = { x, y }
image.type = octane.image.type.LDR_RGBA
image.buffer = {}
-- fill in the buffer with a gradient
t = 0
for i = 1, x*y do
-- interpolate between start & end colour
local c = octane.vec.lerp(c2, c1, t)
table.insert(image.buffer, c[1])
table.insert(image.buffer, c[2])
table.insert(image.buffer, c[3])
table.insert(image.buffer, 255) -- alpha
-- on a new line, modify the interpolation factor
if (i % x == 0) then t = t + 1/y end
end
return image
end
Code: Select all
-- start with a clean slate
octane.project.reset()
-- create a gradient (500x500, red -> yellow)
gradient = createGradient(500, 500, { 255, 0, 0 }, { 255, 255, 0 })
Code: Select all
-- create an image texture node
texNode = octane.node.create{ type=octane.NT_TEX_IMAGE, name="Gradient Texture" }
-- set up the gradient in the attributes
texNode:setAttribute(octane.A_BUFFER , gradient.buffer , false)
texNode:setAttribute(octane.A_SIZE , gradient.size , false)
texNode:setAttribute(octane.A_TYPE , gradient.type , false)
-- evaluate the texture node
texNode:evaluate()
Code: Select all
-- save out the image texture as a png
directory = "/tmp/lua"
-- this will export to "/tmp/lua/textures/Gradient Texture.png"
texNode:exportToFile(directory)
Code: Select all
-- Roll our own image textures
-- Creates an LDR gradient image (4 bytes/pixel).
--
-- @param[in] x
-- horizontail size of the image
-- @param[in] y
-- vertical size of the image
-- @param[in] c1
-- start colour for the gradient.
-- @param[in] c2
-- end colour for the gradient.
-- @return
-- image buffer with a top to bottom gradient c1 > c2
local function createGradient(x, y, c1, c2)
-- create a data structure for the image
local image = {}
image.size = { x, y }
image.type = octane.image.type.LDR_RGBA
image.buffer = {}
-- fill in the buffer with a gradient
t = 0
for i = 1, x*y do
-- interpolate between start & end colour
local c = octane.vec.lerp(c2, c1, t)
table.insert(image.buffer, c[1])
table.insert(image.buffer, c[2])
table.insert(image.buffer, c[3])
table.insert(image.buffer, 255) -- alpha
-- on a new line, modify the interpolation factor
if (i % x == 0) then t = t + 1/y end
end
return image
end
-- start with a clean slate
octane.project.reset()
-- create a gradient (500x500, red -> yellow)
gradient = createGradient(500, 500, { 255, 0, 0 }, { 255, 255, 0 })
-- create an image texture node
texNode = octane.node.create{ type=octane.NT_TEX_IMAGE, name="Gradient Texture" }
-- set up the gradient in the attributes
texNode:setAttribute(octane.A_BUFFER , gradient.buffer , false)
texNode:setAttribute(octane.A_SIZE , gradient.size , false)
texNode:setAttribute(octane.A_TYPE , gradient.type , false)
-- evaluate the texture node
texNode:evaluate()
-- save out the image texture as a png
directory = "/tmp/lua"
-- this will export to "/tmp/lua/textures/Gradient Texture.png"
texNode:exportToFile(directory)
Thomas