Skip to content

Commit

Permalink
Add support of the new render.draw's options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
aglitchman committed Mar 10, 2022
1 parent 840676d commit 00cc91b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
12 changes: 12 additions & 0 deletions scene3d/render/render3d.lua
Expand Up @@ -124,4 +124,16 @@ function M.debug_log(t)
M.debug_text = M.debug_text .. tostring(t) .. " "
end

local ENGINE_VERSION = {}
for num in sys.get_engine_info().version:gmatch("%d+") do
table.insert(ENGINE_VERSION, tonumber(num))
end

-- Returns true if `major.minor.patch` >= the engine version
function M.engine_version(major, minor, patch)
return ENGINE_VERSION[1] > major or
(ENGINE_VERSION[1] == major and ENGINE_VERSION[2] > minor) or
(ENGINE_VERSION[1] == major and ENGINE_VERSION[2] == minor and ENGINE_VERSION[3] >= patch)
end

return M
37 changes: 24 additions & 13 deletions scene3d/render/render3d.render_script
@@ -1,5 +1,8 @@
local render3d = require("scene3d.render.render3d")

-- Defold 1.3.1 or newer: render.draw(predicate, [options]).
local HAS_DRAW_OPTS = render3d.engine_version(1, 3, 1)

function init(self)
-- GUI predicates
self.gui_pred = render.predicate({"gui"})
Expand All @@ -22,18 +25,25 @@ function update(self)
local window_width = math.max(1, render.get_window_width())
local window_height = math.max(1, render.get_window_height())

local CONSTANTS = render.constant_buffer()
local constants = render.constant_buffer()
local DRAW_OPTS = constants
if HAS_DRAW_OPTS then
DRAW_OPTS = {
frustum = nil,
constants = constants
}
end

local color = render3d.light_ambient_color
local intensity = render3d.light_ambient_intensity
CONSTANTS.light_ambient = vmath.vector4(color.x, color.y, color.z, intensity)
constants.light_ambient = vmath.vector4(color.x, color.y, color.z, intensity)

local dir = render3d.light_directional_direction
local intensity = render3d.light_directional_intensity
CONSTANTS.light_directional = vmath.vector4(-dir.x, -dir.y, -dir.z, intensity)
constants.light_directional = vmath.vector4(-dir.x, -dir.y, -dir.z, intensity)

CONSTANTS.fog = vmath.vector4(render3d.fog_range_from, render3d.fog_range_to, 0, render3d.fog_intensity)
CONSTANTS.fog_color = vmath.vector4(render3d.fog_color.x, render3d.fog_color.y, render3d.fog_color.z, 1.0)
constants.fog = vmath.vector4(render3d.fog_range_from, render3d.fog_range_to, 0, render3d.fog_intensity)
constants.fog_color = vmath.vector4(render3d.fog_color.x, render3d.fog_color.y, render3d.fog_color.z, 1.0)

render.set_depth_mask(true)
render.set_stencil_mask(0xff)
Expand All @@ -52,30 +62,31 @@ function update(self)
render.set_view(mat_view)
local mat_proj = render3d.camera_perspective()
render.set_projection(mat_proj)
local pv = mat_proj * mat_view
scene3d.frustum_set(pv)
local mat_frustum = mat_proj * mat_view
scene3d.frustum_set(mat_frustum)
if HAS_DRAW_OPTS then
DRAW_OPTS.frustum = mat_frustum
end
render.set_depth_mask(true)
render.enable_state(render.STATE_DEPTH_TEST)
render.disable_state(render.STATE_STENCIL_TEST)
render.disable_state(render.STATE_BLEND)
render.enable_state(render.STATE_CULL_FACE)
render.set_blend_func(render.BLEND_ONE, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.draw(self.model_pred, CONSTANTS)
render.draw(self.model_pred, DRAW_OPTS)
-- The sky is rendered last as a workaround for the lack of front-to-back sorting.
-- https://github.com/defold/defold/issues/5168
render.set_blend_func(render.BLEND_ONE, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.draw(self.sky_pred, CONSTANTS)
render.draw(self.sky_pred, DRAW_OPTS)

-- Non-opaque, i.e. alpha-blended sprites
render.enable_state(render.STATE_BLEND)
render.set_projection(mat_proj)
-- TODO: Render sprite-based shadows
-- render.draw(self.shadow_pred, CONSTANTS)
render.disable_state(render.STATE_CULL_FACE)
render.set_blend_func(render.BLEND_ONE, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.set_depth_mask(false)
render.draw(self.tile_pred)
render.draw(self.particle_pred)
render.draw(self.tile_pred, DRAW_OPTS)
render.draw(self.particle_pred, DRAW_OPTS)
render.draw_debug3d()
end

Expand Down

0 comments on commit 00cc91b

Please sign in to comment.