Table of Contents
11. Table render2texture¶
A table render2texture create a dynamic texture, inherit from renderizable and normally have one animation. Also the animation has a shader table by default which can be accessed by getShader method.
A dynamic texture can be interpreted as a personal view and can be applied to any component through setTexture.
Also can be rendered in a single frame using the method enableFrame as true
(default).
11.1. render2texture methods¶
11.1.1. render2texture new¶
- new(string * world, number * x, number * y, number * z)¶
Create a new instance of a render2texture passing the world desired (detail) and position.
- Parameters
string – world can be
2ds
,2dw
or3d
.number – x position (optional).
number – y position (optional).
number – z position (optional).
- Returns
render2texture table.
Example:
tRender = render2texture:new('2dw') --note that render2texture inherits from renderizable
11.1.2. render2texture create¶
- create(number * width, number * height, boolean * alpha, string * nickName)¶
- Create a dynamic texture properly.
Width is the texture’s width and should not be major than native width. if it is then the native width will be used.
Height is the texture’s height and should not be major than native height. if it is then the native height will be used.
Alpha indicate if the texture will have alpha channel.
NickName is the texture name. It generate automatically if not supplied.
- Parameters
number – width texture (optional).
number – height texture (optional).
boolean – alpha channel (optional).
string – nick name from texture generated.
- Returns
boolean
- result,string
- texture name,number
- texture id
Example:
tRender = render2texture:new('2dw') --note that render2texture inherits from renderizable
local ret, nickname, id = tRender:create()
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(true) -- we are enabling render to frame to show our texture
tex = texture:new('2dw')
tex:load('#0000ff') --Blue texture
tex:setSize(500,500) -- resize the frame
tRender:add(tex)
else
print('Failed to create a dynamic texture')
end
Following other example doing the opposite way. Setting the background color and applying it to the texture object:
tRender = render2texture:new('2dw') --note that render2texture inherits from renderizable
local ret, nickname, id = tRender:create()
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(false) -- we are disabling render to frame
tex = texture:new('2dw')
tex:load('#0000ff') --Blue texture
tex:setSize(500,500) -- resize the frame
tRender:setColor(1,0,0) -- set the background color of render2texture to red
tex:setTexture(nickname) -- will render the dynamic texture instead of blue because we just set
--however anytime that we change background color of render2texture will be applied to the texture
--we will do it in the timer following
tTimer = timer:new(
function (self)
local r = math.random(0,1)
local g = math.random(0,1)
local b = math.random(0,1)
tRender:setColor(r,g,b)
end,
0.2
)
else
print('Failed to create a dynamic texture')
end
11.1.3. render2texture enableFrame¶
- enableFrame(boolean enable)¶
Enable the render2texture to render in a frame. The frame will have the same size as the texture. The frame is initially enabled.
- Parameters
boolean – enable render the frame.
Example:
myMesh = mesh:new('2dw')-- create a object as 2d
if myMesh:load('crate.msh') then
tRender = render2texture:new('2dw')
local ret, nickname, id = tRender:create(300,250) --similar to ratio current
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(true) -- enable render to frame to show our object inside the frame
tRender:setColor(0,1,0) --green color
tRender:add(myMesh) -- add the object to our dynamic texture
else
print('Failed to create a dynamic texture')
end
else
print('Failed to load crate.msh')
end
11.1.4. render2texture getCamera¶
- getCamera(string * world)¶
Get a camera which gives you the control to the scene in the dynamic texture. See camera for more information.
- Parameters
string – world can be
'2d'
or'3d'
. default is'2d'
.- Returns
camera table reference.
Example:
myMesh = mesh:new('3d')-- create a object as 3d
if myMesh:load('crate.msh') then
tRender = render2texture:new('2dw')
local ret, nickname, id = tRender:create(300,250) --similar to ration current
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(true) -- enable render to frame to show our any object inside the frame
tRender:setColor(0,1,0) --green color
tRender:add(myMesh) -- add the object to our dynamic texture
camera3d = tRender:getCamera('3d')
camera3d:setPos(0,0,-500)
--using a timer to do a random movement using the camera
tTimer = timer:new(
function (self)
local p = camera3d:getPos()
p.x = p.x + 10
p.y = p.y + 20
p.z = p.z - 1.5
if p.x > 300 then
p.x = -300
end
if p.y > 600 then
p.y = -600
end
if p.z > 500 then
p.z = -500
end
end,
0.02
)
else
print('Failed to create a dynamic texture')
end
else
print('Failed to load crate.msh')
end
Important
The camera method is supposed to be a normal camera but not all methods are implemented. TODO :)
11.1.5. render2texture add¶
- add(renderizable mesh)¶
Add a renderizable to render in the dynamic texture.
- Parameters
renderizable – mesh to render in the frame.
Example:
myMesh = mesh:new('2dw')-- create a object as 2d
if myMesh:load('crate.msh') then
tRender = render2texture:new('2dw')
local ret, nickname, id = tRender:create(300,250) --similar to ratio current
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(true) -- enable render to frame to show our object inside the frame
tRender:setColor(0,1,0) --green color
tRender:add(myMesh) -- add the object to our dynamic texture
else
print('Failed to create a dynamic texture')
end
else
print('Failed to load crate.msh')
end
11.1.6. render2texture remove¶
- remove(renderizable mesh)¶
Remove a renderizable from render 2 texture list.
- Parameters
renderizable – mesh to remove from list.
- Returns
boolean
found - it was found to be removed?.
11.1.7. render2texture clear¶
- clear¶
Remove all renderizable from render 2 texture list.
11.1.8. render2texture release¶
- release¶
Remove all renderizable from render 2 texture list and destroy the dynamic texture created. The method
create
might be used again.
11.1.9. render2texture setColor¶
Change the background color of dynamic texture.
- setColor(number r, number g, number b, number a)¶
- Parameters
number – r red color, range 0 to 1.
number – g green color, range 0 to 1.
number – b blue color, range 0 to 1.
number – a alpha color, range 0 to 1.
11.1.10. render2texture save¶
- save(string file_name, number * x, number * y, number * w, number * h)¶
- Save the texture as
png
file.If not suppliedx
andy
will be zero.If not suppliedw
andh
will be the size of the back buffer.- Parameters
string – file name of texture to be saved.
number – x origin to rect the texture.
number – y origin to rect the texture.
number – width of texture to be saved.
number – height of texture to be saved.
- Returns
boolean
result of operation.
Example:
myMesh = mesh:new('2dw')-- create a object as 2d
if myMesh:load('crate.msh') then
tRender = render2texture:new('2dw')
local ret, nickname, id = tRender:create(300,250) --similar to ratio current
if ret then
print('Dynamic texture created successfully')
tRender:enableFrame(true) -- enable render to frame to show our object inside the frame
tRender:setColor(0,1,0) --green color
tRender:add(myMesh) -- add the object to our dynamic texture
else
print('Failed to create a dynamic texture')
end
else
print('Failed to load crate.msh')
end
times_render = 0
function loop(delta)
if times_render == 1 then --we wait for the first render otherwise the texture will be empty
if tRender:save('my_dynamic_texture.png') then
print('Texture saved successfully ...')
else
print('Failed to save texture')
end
end
times_render = times_render + 1
end
Important