Here’s the entire render script.
local camera = require "orthographic.camera"
-- Check if 'shared_state' setting is on
-- From https://github.com/rgrams/rendercam/blob/master/rendercam/rendercam.lua#L4-L7
if sys.get_config("script.shared_state") ~= "1" then
error("ERROR - camera - 'shared_state' setting in game.project must be enabled for camera to work.")
end
local high_dpi = sys.get_config("display.high_dpi", "0") == "1"
local WIDTH, HEIGHT = camera.get_window_size()
local CLEAR_COLOR = hash("clear_color")
local SET_VIEW_PROJECTION = hash("set_view_projection")
local SET_CAMERA_OFFSET = hash("set_camera_offset")
local IDENTITY_MATRIX = vmath.matrix4()
local DISPLAY_WIDTH = tonumber(sys.get_config("display.width"))
local DISPLAY_HEIGHT = tonumber(sys.get_config("display.height"))
local function create_render_targets(self)
local color_params = {
format = render.FORMAT_RGBA,
width = WIDTH,
height = (HEIGHT/1.3),
min_filter = render.FILTER_LINEAR,
mag_filter = render.FILTER_LINEAR,
u_wrap = render.WRAP_CLAMP_TO_EDGE,
v_wrap = render.WRAP_CLAMP_TO_EDGE
}
local depth_params = {
format = render.FORMAT_DEPTH,
width = WIDTH*2,
height = (HEIGHT/1.3),
u_wrap = render.WRAP_CLAMP_TO_EDGE,
v_wrap = render.WRAP_CLAMP_TO_EDGE
}
self.base_target = render.render_target("base_target", {[render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params })
self.lights_target = render.render_target("lights_target", {[render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params })
end
function init(self)
self.tile_pred = render.predicate({"tile"})
self.gui_pred = render.predicate({"gui"})
self.text_pred = render.predicate({"text"})
self.particle_pred = render.predicate({"particle"})
self.lights_pred = render.predicate({"lights"})
self.render_target_pred = render.predicate({"render target"})
self.clear_pred = render.predicate({"clear"})
self.ambient_light = vmath.vector4(0, 0, 0, 1)
self.clear_color = vmath.vector4(1, 1, 1, 1)
self.clear_color.x = sys.get_config("render.clear_color_red", 0)
self.clear_color.y = sys.get_config("render.clear_color_green", 0)
self.clear_color.z = sys.get_config("render.clear_color_blue", 0)
self.clear_color.w = sys.get_config("render.clear_color_alpha", 0)
self.world_view = vmath.matrix4()
self.world_projection = vmath.matrix4()
self.screen_view = vmath.matrix4()
self.window_width = nil
self.window_height = nil
create_render_targets(self)
end
function update(self)
local window_width = render.get_window_width()
local window_height = render.get_window_height()
if self.window_width ~= window_width or self.window_height ~= window_height then
self.window_width = window_width
self.window_height = window_height
-- update window width/height for camera (used by the projections)
if high_dpi then
camera.set_window_size(window_width / 2, window_height /2)
else
camera.set_window_size(window_width, window_height)
end
end
-- clear color
render.set_depth_mask(true)
render.clear({[render.BUFFER_COLOR_BIT] = self.clear_color, [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
-- setup the viewport
render.set_viewport(0, 0, window_width, window_height)
-- draw world space using projection received from the camera in on_message
render.set_view(self.world_view)
render.set_projection(self.world_projection)
render.set_depth_mask(false)
render.disable_state(render.STATE_DEPTH_TEST)
render.disable_state(render.STATE_STENCIL_TEST)
render.enable_state(render.STATE_BLEND)
render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.disable_state(render.STATE_CULL_FACE)
render.enable_render_target(self.base_target)
render.clear({[render.BUFFER_COLOR_BIT] = self.clear_color, [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
render.draw(self.tile_pred)
render.draw(self.particle_pred)
render.draw_debug3d()
render.disable_render_target(self.base_target)
--LIGHTING STUFF
render.enable_render_target(self.lights_target)
-- Clear with ambient light color
render.clear({[render.BUFFER_COLOR_BIT] = self.ambient_light, [render.BUFFER_DEPTH_BIT] = 1, [render.BUFFER_STENCIL_BIT] = 0})
render.draw(self.lights_pred)
render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.disable_render_target(self.lights_target)
--PROJECTION
render.set_view(IDENTITY_MATRIX) -- set view and projection so quad fills the viewport
render.set_projection(IDENTITY_MATRIX)
render.enable_texture(0, self.lights_target, render.BUFFER_COLOR_BIT)
render.enable_texture(1, self.base_target, render.BUFFER_COLOR_BIT)
render.draw(self.render_target_pred)
render.disable_texture(0, self.lights_target)
render.disable_texture(1, self.base_target)
render.set_viewport(0, 0, window_width, window_height)
-- draw world space using projection received from the camera in on_message
render.set_view(self.world_view)
render.set_projection(self.world_projection)
-- draw gui in screen space using an orthographic projection
render.set_view(self.screen_view)
if self.camera_offset then
local m4 = vmath.matrix4_orthographic(
self.camera_offset.x, window_width + self.camera_offset.x,
self.camera_offset.y, window_height + self.camera_offset.y,
-1, 1)
render.set_projection(m4)
else
local m4 = vmath.matrix4_orthographic(0, window_width, 0, window_height, -1, 1 )
render.set_projection(m4)
end
render.enable_state(render.STATE_STENCIL_TEST)
render.draw(self.gui_pred)
render.draw(self.text_pred)
render.draw(self.clear_pred)
render.disable_state(render.STATE_STENCIL_TEST)
render.set_depth_mask(false)
render.draw_debug2d()
end
function on_message(self, message_id, message)
if message_id == CLEAR_COLOR then
self.clear_color = message.color
elseif message_id == SET_VIEW_PROJECTION then
self.world_view = message.view
self.world_projection = message.projection
elseif message_id == SET_CAMERA_OFFSET then
self.camera_offset = message.offset
end
end
this is basically just @britzl 's orhographic cam render script, in which I added the render lighting part. I just stretched the height part to fit the entire thing on the whole screen.
Yes, I think that is the case, but how can I properly set the matrix. Plz help…