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
  • stringworld can be 2ds, 2dw or 3d.

  • numberx position (optional).

  • numbery position (optional).

  • numberz 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
  • numberwidth texture (optional).

  • numberheight texture (optional).

  • booleanalpha channel (optional).

  • stringnick 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
_images/render_2_texture_example_1.png

Figure 11.1 Texture object being renderized in the render2texture object

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
_images/render_2_texture_example_2.gif

Figure 11.2 render2texture being used as dynamic texture.

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

booleanenable 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
_images/render_2_texture_example_3.png

Figure 11.3 adding object to render2texture

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

stringworld 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
_images/render_2_texture_example_4.gif

Figure 11.4 Accessing the camera from render_2_texture object

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

renderizablemesh 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

renderizablemesh 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
  • numberr red color, range 0 to 1.

  • numberg green color, range 0 to 1.

  • numberb blue color, range 0 to 1.

  • numbera 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 supplied x and y will be zero.
If not supplied w and h will be the size of the back buffer.
Parameters
  • stringfile name of texture to be saved.

  • numberx origin to rect the texture.

  • numbery origin to rect the texture.

  • numberwidth of texture to be saved.

  • numberheight 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

If the texture was never render, then when you save it will be just empty texture.
That is why we wait for the first cycle of render to save the texture.