I’m trying to create a depth buffer for a shadow map. I created a render target:
local depth_params = {
format = render.FORMAT_DEPTH,
width = render.get_window_width(),
height = render.get_window_height(),
min_filter = render.FILTER_NEAREST,
mag_filter = render.FILTER_NEAREST,
u_wrap = render.WRAP_CLAMP_TO_EDGE,
v_wrap = render.WRAP_CLAMP_TO_EDGE
}
local parameters = {
[render.BUFFER_DEPTH_BIT] = depth_params
}
self.render_target_shadow_map = render.render_target("shadow_map", parameters)
In render script update I draw in a Render Pass for Shadow Map the data to this render target.
-- Enable render target
HELPER.to_render_target(self.render_target_shadow_map, function()
-- Clear screen
HELPER.clear_all_buffers(clear_buffers)
-- Use virtual light camera (I'm trying now with a single light source)
local camera_light = cameras.light
render.set_view(camera_light.view) -- Use the light camera's view matrix
render.set_projection(camera_light.proj) -- Use the light camera's projection matrix
-- Calculate light space matrix for given light camera and set it for Lighting Pass constants
self.constants.u_light_space_matrix = camera_light.proj * camera_light.view
-- Set state
render.set_depth_mask(true)
render.set_depth_func(render.COMPARE_FUNC_LEQUAL)
render.enable_state(render.STATE_DEPTH_TEST)
render.disable_state(render.STATE_BLEND)
-- Draw to render target
render.draw(predicates.model) -- Draw all objects that cast shadows
end)
HELPER here is just a simple stateless wrapper for frequent calls, e.g. clearing, enabling render targets and then disabling it after whole function in callback is finished, same for enabling textures.
And then there is G-Buffer pass and then I want to use it in the Lighting Pass like this:
HELPER.with_texture(0, gbuffer_rt, render.BUFFER_COLOR0_BIT, function() -- POSITIONS RGBA32F
HELPER.with_texture(1, gbuffer_rt, render.BUFFER_COLOR1_BIT, function() -- NORMALS RGBA32F
HELPER.with_texture(2, gbuffer_rt, render.BUFFER_COLOR2_BIT, function() -- ALBEDO RGBA
assert(self.render_target_shadow_map, "Shadow map render target is nil or not initialized.")
HELPER.with_texture(3, self.render_target_shadow_map, render.BUFFER_DEPTH_BIT, function() -- SHADOW MAP
render.draw(self.predicates.light, { frustum = camera_world.frustum.frustum, constants = self.constants })
end)
end)
end)
end)
But I have an error:
ERROR:SCRIPT: render/helper.lua:189: Render target '(asset 524294 type=ASSET_TYPE_RENDER_TARGET)' does not have a texture for the specified buffer type (type=BUFFER_TYPE_DEPTH_BIT).
stack traceback:
[C]:-1: in function enable_texture
render/helper.lua:189: in function with_texture
render/cute.render_script:199: in function draw_function
render/helper.lua:190: in function with_texture
render/cute.render_script:197: in function draw_function
render/helper.lua:190: in function with_texture
render/cute.render_script:196: in function draw_function
render/helper.lua:190: in function with_texture
render/cute.render_script:195: in function <render/cute.render_script:74>
And the effect is like this:
Without shadows:
Assert is not triggered.
The data passed looks correct:
DEBUG:SCRIPT: 3, <-- this is unit binding
8590458886, <--- looks like a correct render target handle
16 <--- buffer type - DEPTH
What might be the cause of error?
I want to first solve the issue with the render target buffer (and then most probably with proper camera creation for this light, because from the output it “looks” like depth from the perspective of the light in the middle is incorrect, if it’s depth representation even)
Full project:
MWDJ_2024_3D.zip (323.4 KB)