Why faking 3D nowadays?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Party member
Posts: 934
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia

Re: Why faking 3D nowadays?

Post by pgimeno » Tue Mar 13, 2018 11:50 pm

4aiman wrote:
Wed Feb 28, 2018 4:32 pm
Thanks a lot for your answers!

I guess I should've been more specific, though.
Love3d and other projects I saw here (love2d forums & github) use shaders and/or ffi in order to achieve proper 3d representation. Heck, even raycasters tend to be hardware-dependent (can't find a single raycaster that have textured floor/ceiling AND works on Android).

My interest lies not in love3d (or any other specific lib for that matter) and certainly not in raycasting.
I'm just wondering if there some newly-introduced built-in features of love2d 0.11.x to make perspective textured planes in 3d.

Something like love.graphics.rectangle(x,y,z,width,heigth, length,...) is a distant dream, but is it possible to go 3d without ffi and shaders as of now? Note that I'm talking about a cross-platform solution.
Without shaders, no. Shaders are at the core of Löve. They are always running, even if you don't define any.

To work in 3D with OpenGL, you need a vertex shader. I've looked more into love3d now, and it's now in my to-do list to try and make a raycasting-looking demo using actual GL 3D rendering. The key to obtain it is love.graphics.newMesh and an appropriate vertex and fragment ("pixel") shader, but especially vertex.

Now, take a look at this love3d demo: https://github.com/excessive/love3d-dem ... orward.lua

Pay special attention to how little it uses "l3d" and for what purpose. In that demo, it is used ONLY for two purposes: 1) to provide a depth buffer so that only the closest pixels are drawn, and 2) to not draw polygons that don't point to the camera. The rest is done with pure Löve. For something as simple as the raycasting-like demo I've mentioned earlier, you can do without both, and then you don't need love3d, just pure Löve 0.10. The love3d demo uses the IQM library, which in turn uses functions love.graphics.newMesh and Mesh:setVertexMap to create the mesh. IQM uses FFI to speed up loading of the file format it supports, but according to what I have seen so far, it seems to me that that loading could have been implemented in pure Lua with no problems. I am still studying it.

As I noted in a previous post, Löve 0.11 provides depth buffers, and that makes (1) unnecessary. It also introduces love.graphics.setMeshCullMode, which makes (2) unnecessary. This means that with 0.11, you don't need love3d to do real 3D like in that demo.

Have I answered your question now?

Edit: Simple proof of concept follows. I see no reason for it not to work in mobile. Note it does not use any libraries or FFI, but it uses a (trivial) vertex shader.

Code: Select all

local img, sb

-- Vertex shader code
local shader = [[
extern mat4 view;

vec4 position(mat4 transform, vec4 vertex) {
  // ignore love's transform which is only for 2d and use our own matrix
  return view * vertex;

function love.load(args)
  img = love.graphics.newImage('image1.jpg')

  -- A spritebatch is a mesh, so let's use that for simplicity
  sb = love.graphics.newSpriteBatch(img, 64)
  -- Generate 8x8 quads (image is 640x480 which means each quad is 80x60)
  local quads = {}
  for y = 0, 7 do for x = 0, 7 do
    quads[y * 8 + x + 1] = love.graphics.newQuad(x*80, y*60, 80, 60, 640, 480)
  end end
  -- Add some quads at random
  for i = 1, 60 do
    sb:add(quads[love.math.random(1, 64)],
           love.math.random(0, 7) * 80,
           love.math.random(0, 7) * 60)

  -- Compile the shader in place
  shader = love.graphics.newShader(shader)

local t = 0
function love.update(dt)
  t = t + dt

function love.draw()
  -- Use a preset matrix for demo purposes. A real matrix can be generated
  -- with a 3D math library like CPML.
  shader:send('view', {
     1.3,    0,  0,  0,
       0,    0,  1,  1,
       0, -1.7,  0,  0,
    -450, -120, 50, 50,

  -- Toggle vertex shader on/off every second
  if t >= 2 then
    t = 0
  elseif t >= 1 then

  -- Draw the spritebatch
Edit2: Oops, wrong image. The new one is 640x480.
(68.95 KiB) Downloaded 8 times
(118.14 KiB) Downloaded 13 times

User avatar
Party member
Posts: 934
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia

Re: Why faking 3D nowadays?

Post by pgimeno » Sun Mar 18, 2018 10:31 pm

Here's something that is a bit more than the simple proof of concept above, but it's still little more than a mini-demo and not usable for anything serious yet, because it doesn't do any sorting: everything is rendered in the order it's created.

It allows you to draw 3D rectangles (actually rhomboids) given three 3D vertices: top left, top right, and bottom left (the bottom right one is calculated) and the UV corners. Everything is done via "the standard way" of using GPU rendering. By that I mean that it uses the same 3D projection method that actual 3D engines use. You don't have to worry about compatibility; the vertex shader is so very close to the Löve default that I'd say it's guaranteed to work on any platform where Löve works.

I've added on-screen buttons for testing in a mobile phone. I've tested in mine and it has no problems with it at all.
(72.03 KiB) Downloaded 13 times

Posts: 3
Joined: Sat Mar 18, 2017 3:45 pm

Re: Why faking 3D nowadays?

Post by Cruhan » Mon Mar 19, 2018 2:05 am

interesting, it's a shame no raycaster engine gets really finished hehe.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests