Appkit: /world_wrapper.lua code sample - Stingray Lua API Reference
--[[
The `WorldWrapper` manages an engine `World` object and
<ul>
<li>Manages a list of enabled cameras</li>
<li>References a ShadingEnvironment for rendering</li>
<li>Renders each frame with enabled cameras and its shading environment</li>
<li>Stores and updates an Appkit.DebugScrollbox for debug printing to screen</li>
</ul>
The `WorldWrapper` is used together with `LevelWrapper` and `CameraWrapper` to
support the Editor flow nodes to get and set enabled cameras using cameras and
flow from the Level Editor.
]]--
require 'core/appkit/lua/class'
local DebugScrollbox = require 'core/appkit/lua/debug_scrollbox'
Appkit.WorldWrapper = Appkit.class(Appkit.WorldWrapper)
local WorldWrapper = Appkit.WorldWrapper
local World = stingray.World
local Application = stingray.Application
local ShadingEnvironment = stingray.ShadingEnvironment
-------------------------------------------------------------------------------
-- External Interface
-- The Unit is required because at the moment the unit is necessary for things
-- like Camera Flow Nodes so we need to track and make it accessible.
-- unit parameter is optional when disabling camera.
function WorldWrapper:set_camera_enabled(camera, unit, enabled)
if not camera then return end
if enabled == true then
self.enabled_cameras[camera] = unit
else
self.enabled_cameras[camera] = nil
end
end
-- Returns two values: camera, camera_unit
-- Todo: improve. If there are multiple enabled cameras then this arbitrarily
-- returns one of them.
function WorldWrapper:get_enabled_camera()
return next(self.enabled_cameras)
end
-- Set the shading environment used to render the currently enabled cameras
function WorldWrapper:set_shading_environment(env, name)
World.set_shading_environment(self.world, env, name)
self.shading_env = env
end
-- Draw debug text to the screen. label and color are optional
function WorldWrapper:debug_display_text(text, label, color)
self.debug_scrollbox:add_line(text, label, color)
end
-------------------------------------------------------------------------------
-- Internal Appkit Interface
-- If is_linked is not supplied then will link by default.
function WorldWrapper:init(world, viewport)
self.world = world
self.enabled_cameras = {}
self.shading_env = nil
self.debug_scrollbox = DebugScrollbox(world)
-- you can pass an optional viewport template parameter used for this world when creating the viewport, will use "default" otherwise
self.viewport_name = viewport or "default"
self.viewport = Application.create_viewport(world, self.viewport_name)
self.gui = stingray.World.create_screen_gui(self.world, "immediate")
-- is this stuff needed anymore? etc
-- stingray.BakedLighting.map(world, resource_name)
end
function WorldWrapper:update(dt)
local world = self.world
self.debug_scrollbox:update(dt)
world:update(dt)
end
function WorldWrapper:change_viewport_template(template)
if self.viewport_name ~= template then
Application.destroy_viewport(self.world, self.viewport)
self.viewport = Application.create_viewport(self.world, template)
self.viewport_name = template
end
end
function WorldWrapper:render(window)
local shading_env = self.shading_env
if not shading_env then
if not WorldWrapper.did_render_fail then
WorldWrapper.did_render_fail = true
print "Appkit WorldWrapper: Render failed. No shading environment."
end
return
end
ShadingEnvironment.apply(shading_env)
local world = self.world
local viewport = self.viewport
if #self.enabled_cameras == 0 then
WorldWrapper.did_warn_no_cameras = true
if not WorldWrapper.did_warn_no_cameras then
print "WorldWrapper:render. No cameras to render."
end
end
for camera, _ in pairs(self.enabled_cameras) do
if window ~= nil then
Application.render_world(world, camera, viewport, shading_env, window)
else
Application.render_world(world, camera, viewport, shading_env)
end
end
end
function WorldWrapper:shutdown()
end
return WorldWrapper