Libs3D Library Model Import

From GiderosMobile
Revision as of 00:18, 9 August 2026 by MoKaLux (talk | contribs) (wip)

I am ready

Next step is to import 3D models. Let's start with some static models (.obj, .vox, ...). As with the shapes we have seen before, models will react to lighting, shadow, normal map, ... .

Gideros rulez

When you work with 3D, you need to follow some "engine" rulez. In Gideros those are:

  • currently Gideros supports .png and .jpg image formats
  • currently Gideros supports Base Color, Texture map and Normal Map
  • the textures shouldn't be embedded in the models. In Blender, use unpack resources:

  • your Blender BSDF shaders should look something like:

please note, you can still have the Metallic, Roughness and other textures attached in your Blender BSDF shaders, Gideros will just ignore them

  • Gideros will import the position of the models
  • I believe Gideros also imports the origin of the models, so place the origins accordingly
I personally use this Blender add-on: https://extensions.blender.org/add-ons/act-game-asset-creation-toolset/

.obj format

Loading .obj files is as simple as

local obj = loadObj("path/to/model/folder", "model.obj")

You can set the model position, rotation and scale however you like.

Some sample code, pic above (you may have to provide your own assets).

application:setBackgroundColor(0x5500ff)

-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)

-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.25*8,0.5*8, 0*8,0*8,-0.1*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])

-- we add the view (camera) to Gideros stage
stage:addChild(view)

-- we will add 3D shapes and models to a scene
local scene = view:getScene()

-- a shape
local ground = D3.PlaneV2.new(2*8, 2*8) -- (width,depth)
ground:mapColor(0xefbebe) -- (color,[alpha=1])
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local normal = Texture.new("textures/forrest_ground_01_nor_gl_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local texsx, texsy = 0.15*8, 0.15*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)

-- loading obj files
local obj001 = loadObj("models/objs", "Limousine.obj") -- ("path/to/model/folder", "modelname.obj")
local obj002 = loadObj("models/objs", "Monster Truck.obj") -- ("path/to/model/folder", "modelname.obj")
local obj003 = loadObj("models/objs", "Muscle 2.obj") -- ("path/to/model/folder", "modelname.obj")
-- position
obj001:setPosition(0.1*8, 0*8, -0.25*8)
obj002:setPosition(-0.35*8, 0*8, -0.0*8)
obj003:setPosition(0.3*8, 0*8, 0.05*8)
-- rotation
obj001:setRotationY(-3*15)
obj002:setRotationY(2*15)
obj003:setRotationY(-4*15)
-- scale
local scale = 1
obj001:setScale(scale,scale,scale)
obj002:setScale(scale,scale,scale)
obj003:setScale(scale,scale,scale)
-- order
scene:addChild(ground)
scene:addChild(obj001)
scene:addChild(obj002)
scene:addChild(obj003)

-- light setup
local lsx, lsy, lsz = 0.0*8, 0.8*8, -0.8*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.3) -- (x,y,z, ambient)
local pd, fov = 6*8, 5*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)

-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
	scene:setRotationY(scene:getRotationY()+0.17)
	Lighting.computeShadows(scene)
end)

.vox format

Loading .vox files is a little bit more involved

  • first we read the .vox file info using MagicaVoxel.new(path) available in magicavoxel.lua
  • then we extract the model data using MagicaVoxel:getModel(optimize)
  • finally we build the model using G3DFormat.buildG3D(vox) available in G3DFormat.lua
local voxA = MagicaVoxel.new("path/to/vox/file.vox") -- MagicaVoxel:init(path)
local voxB = voxA:getModel(true) -- optimize set to true
local vox = G3DFormat.buildG3D(voxB) -- the final .vox model

Some sample code (you may have to provide your own assets).

application:setBackgroundColor(0x5500ff)

-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)

-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.25*8,0.5*8, 0*8,0*8,-0.1*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])

-- we add the view (camera) to Gideros stage
stage:addChild(view)

-- we will add 3D shapes and models to a scene
local scene = view:getScene()

-- a shape
local ground = D3.PlaneV2.new(3*8, 3*8) -- (width,depth)
ground:mapColor(0xefbebe) -- (color,[alpha=1])
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local normal = Texture.new("textures/forrest_ground_01_nor_gl_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local texsx, texsy = 0.15*8, 0.15*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)

-- loading vox files (cc0)
-- https://xeeh.itch.io/farm-set
-- https://voxelbytebot.itch.io/voxel-gaming-room-furniture
local voxA001 = MagicaVoxel.new("models/vox/Flooring.vox") -- MagicaVoxel:init(path)
local voxA002 = MagicaVoxel.new("models/vox/GamingChair.vox") -- MagicaVoxel:init(path)
local voxA003 = MagicaVoxel.new("models/vox/PC.vox") -- MagicaVoxel:init(path)
local voxB001 = voxA001:getModel(true)
local voxB002 = voxA002:getModel(true)
local voxB003 = voxA003:getModel(true)
local vox001 = G3DFormat.buildG3D(voxB001)
local vox002 = G3DFormat.buildG3D(voxB002)
local vox003 = G3DFormat.buildG3D(voxB003)

-- position
vox001:setPosition(-0.2*8, 0*8, 0.0*8)
vox002:setPosition(0.05*8, 0.0*8, 0.2*8)
vox003:setPosition(0.0*8, 0*8, 0.25*8)
-- rotation
vox001:setRotationY(0*15)
vox002:setRotationY(0*15)
vox003:setRotationY(0*15)
-- scale
local scale = 0.025
vox001:setScale(scale,scale,scale)
vox002:setScale(scale,scale,scale)
vox003:setScale(scale,scale,scale)
-- order
scene:addChild(ground)
scene:addChild(vox001)
scene:addChild(vox002)
scene:addChild(vox003)

-- light setup
local lsx, lsy, lsz = 0.0*8, 0.8*8, -0.8*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.3) -- (x,y,z, ambient)
local pd, fov = 6*8, 5*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)

-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
	scene:setRotationY(scene:getRotationY()+0.17)
	Lighting.computeShadows(scene)
end)

.glb format

Almost same as .vox above

local glbA = Glb.new("path", "file.glb")
local glbB = glbA:getScene()
local glb = G3DFormat.buildG3D(glbB)

Some sample code (you may have to provide your own assets).

application:setBackgroundColor(0x2e2e2e)

-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local sw, sh = application:getContentWidth(), application:getContentHeight()
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (screen w,screen h, fov, near plane,far plane)

-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.25*8,0.5*8, 0*8,0*8,-0.1*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])

-- we add the view (camera) to Gideros stage
stage:addChild(view)

-- we will add 3D shapes and models to a scene
local scene = view:getScene()

-- loading models
local glbA001 = Glb.new("models/glb", "peach_1.glb") -- Glb.new("path", "file")
local glbA002 = Glb.new("models/glb", "peach_2.glb") -- Glb.new("path", "file")
local glbB001 = glbA001:getScene()
local glbB002 = glbA002:getScene()
local glb001 = G3DFormat.buildG3D(glbB001)
local glb002 = G3DFormat.buildG3D(glbB002)

-- position
glb001:setPosition(-0.2*8, 0*8, 0.0*8)
glb002:setPosition(0.05*8, 0.0*8, 0.2*8)
-- rotation
glb001:setRotationY(0*15)
glb002:setRotationY(-3*15)
-- scale
local scale = 24
glb001:setScale(scale,scale,scale)
glb002:setScale(scale,scale,scale)
-- order
scene:addChild(glb001)
scene:addChild(glb002)

-- light setup
local lsx, lsy, lsz = 1.0*8, 1.0*8, 1.0*8 -- light source position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.5) -- (x,y,z, ambient)
local pd, fov = 6*8, 7*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)

-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
	scene:setRotationY(scene:getRotationY()+0.17)
	Lighting.computeShadows(scene)
end)


...TO BE CONTINUED...


I am out 3

You are strongly encouraged to mess with the code.

Bye for now!


Libs3D