Depth testing problem when drawing to quad (SOLVED)

Hi,

So I wanted to draw everything from the scene to render target and then on a quad to apply some post-processing, but it turns out 3D is not as simple as 2D, so I run into what I think is depth testing issue:

Render script:

local IDENTITY_MATRIX = vmath.matrix4()

local function create_render_target(name)
  local color_params = {
    format = render.FORMAT_RGBA,
    width = render.get_window_width(),
    height = render.get_window_height(),
    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 = render.get_window_width(),
    height = render.get_window_height(),
    u_wrap = render.WRAP_CLAMP_TO_EDGE,
    v_wrap = render.WRAP_CLAMP_TO_EDGE
  }
  return render.render_target(name, {[render.BUFFER_COLOR_BIT] = color_params })
end

function init(self)
  self.clear_color = vmath.vector4(0,0,0,1)

  self.scene = {
    predicate = render.predicate({ 'model' }),
    view = vmath.matrix4(),
    projection = vmath.matrix4()
  }

  self.gui = {
    predicate = render.predicate({ 'gui' }),
    view = vmath.matrix4()
  }

  self.text = {
    predicate = render.predicate({ 'text' })
  }

  self.sky_pred = render.predicate({"sky"})


  self.quad_pred = render.predicate({"quad"})

  self.all_rt = create_render_target("all")

  self.clear_options = {
    [render.BUFFER_COLOR_BIT] = self.clear_color,
    [render.BUFFER_DEPTH_BIT] = 1,
    [render.BUFFER_STENCIL_BIT] = 0
  }
end

function update(self)
  self.window = {
    width = render.get_window_width(),
    height = render.get_window_height()
  }
  self.all_rt = create_render_target("all")

  -- render everything to rt
  render.enable_render_target(self.all_rt)

    render.clear(self.clear_options)
    render.set_viewport(0, 0, self.window.width, self.window.height)

    -- scene
    render.set_view(self.scene.view)
    render.set_projection(self.scene.projection)
    render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
    render.enable_state(render.STATE_BLEND)
    render.enable_state(render.STATE_CULL_FACE)
    render.enable_state(render.STATE_DEPTH_TEST)
    render.set_depth_mask(true)

    render.draw(self.sky_pred)
    render.draw(self.scene.predicate)

  render.disable_render_target(self.all_rt)

  -- render to quad
  render.set_view(IDENTITY_MATRIX)
  render.set_projection(IDENTITY_MATRIX)

  render.enable_material(hash("quad"))
    render.enable_texture(0, self.all_rt, render.BUFFER_COLOR0_BIT)

      render.clear(self.clear_options)

      render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
      render.enable_state(render.STATE_BLEND)
      render.enable_state(render.STATE_CULL_FACE)
      render.enable_state(render.STATE_DEPTH_TEST)
      render.set_depth_mask(true)
    
      render.draw(self.quad_pred)

    render.disable_texture(0)
  render.disable_material()

  -- debug
  render.disable_state(render.STATE_DEPTH_TEST)
  render.disable_state(render.STATE_CULL_FACE)
  render.draw_debug3d()

  -- gui
  render.set_view(self.gui.view)
  render.set_projection(vmath.matrix4_orthographic(0, self.window.width, 0, self.window.height, -1, 1))
  render.enable_state(render.STATE_STENCIL_TEST)
  render.draw(self.gui.predicate)
  render.draw(self.text.predicate)
end

function on_message(self, message_id, message)
  if message_id == hash 'clear_color' then
    self.clear_color = message.color
  elseif message_id == hash 'set_view_projection' then
    self.scene.view = message.view
    self.scene.projection = message.projection
  end
end

Quad.fp just draws given textures for now:

varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
varying mediump vec4 var_light;

uniform lowp sampler2D tex0;
uniform lowp vec4 tint;

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    vec4 color = texture2D(tex0, var_texcoord0.xy) * tint_pm;

    gl_FragColor = color;
}

I believe the issue is with render script, but I don’t know where, so do you maybe spot the issue there?

In the create_render_target function you have defined a local depth_params that is never used in the render script only returning color_bit, no depth is used. I created an example here using depth with RT in a 3d scene.

3 Likes

Thank you so much! This solved the issue :slight_smile:

-- added depth_params to render target creation:  
return render.render_target(name, {[render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params })
2 Likes