A downloadable project

pd3d is a tiny Lua library for Playdate that can load and render .obj 3D models.

The library is completely free, licensed under CC0.

What it can do?

  • Load .obj models together with their .mtl material libraries
  • Load raw bitmap textures (RGB8)
  • Render orthographic projection
  • Render solid color polygons (dithered)
  • Render single model inside a viewport
  • Render and transform low poly models (~300 tris) at a reasonable framerate

What it can't do?

  • Load .png,. bmp, .jpg, etc. complex texture files
  • Read advanced or extended .obj instructions
  • Render textures
  • Perspective
  • Scenes (multiple objects in same 3D space)
  • C performance

Quick start

import "pd3d"
-- Load model
local model = pd3d.loadModel("tree.obj")
function playdate.update()
   -- Scale and rotate
   pd3d.transform(model, 0, 0, 1.5, 180, -45, 0)
   --- Draw
   pd3d.draw(model, 200, 120, 80, 80)
end

API

The best way to explore the API is using LuaLS extension in your favorite editor. Here's a quick reference: 

pd3d.loadModel(path)

Load model from .obj file

pd3d.setRenderer(name)

Sets rendering mode ("vertex"|"flat"|"shader"|"wire")

pd3d.createPrimitive(type)

Create primitive mesh ("cube"|"icosahedron") 

pd3d.transform(model, offsetX, offsetY, scale, rotationY, rotationX, rotationZ)

Change model scale, rotation or offset within its space

pd3d.applyTransform(model)

Make current transform permanent by applying it to the model

pd3d.drawModel(model, x, y, w, h)

Draw model on screen

pd3d.setLight(y)

Set light source position. Currently the light is always on the Y axis. This setting only affects the "shader" renderer.

pd3d.setAmbientLight(intensity)

Set ambient light luminosity. This setting only affects the "shader" renderer.

Tech

pd3d relies heavily on integer math and precalculated values.

While the model is drawn every frame, changes take effect after a short delay. Here's how it happens (frames):

  1. Transforms are applied, vertices are recalculated and polygon visibility resolved (backface culling)
  2. Polygons are sorted by z and rendered to a buffer (back-to-front)
  3. Rendered image is drawn on screen
Published 11 hours ago
StatusReleased
CategoryOther
AuthorOutgunned Games
ContentNo generative AI was used

Download

Download
pd3d.lua 37 kB
Download
Demo (pd3d.pdx.zip) 55 kB
Download
Source code (source.zip) 9.1 kB