Libs3D Library ShapesV2: Difference between revisions

From GiderosMobile
wip
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:




  '''<big>Please note: there is [[Libs3D Library ShapesV2|a Shapes Library V2]]. We only keep this one for backward compatibility 🤷‍♂️</big>'''
  '''<big>This is Shapes Library V2</big>''' 💪
*dimensions where 1 unit is indeed 1 unit
*added normal mapping
*added a pyramid shape
 
'''[[Media:3DShapesV2.lua]]''' '''(tip: right click and save link as)'''. Add it to the '''3dbase''' folder and ''exclude from execution'', ''3DShapes.lua''


== I am ready ==
== I am ready ==
Line 12: Line 17:
* some common 3D shapes (plane, cube, ...)
* some common 3D shapes (plane, cube, ...)
* light and shadows
* light and shadows
* playground
* extra (fog mode)
* the code


Copy the following two '''Library''' folders in your project '''''assets''''' folder:
Copy the following two '''Library''' folders in your project '''''assets''''' folder:
Line 31: Line 37:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- we position the view (camera) and set its target
-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.5*8,0.5*8, 0*8,0*8,0*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])
view:lookAt(0.4*8,0.3*8,0.3*8, 0*8,0*8,-0.1*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])
</syntaxhighlight>
</syntaxhighlight>


Line 45: Line 51:
-- we will add 3D shapes and models to a scene
-- we will add 3D shapes and models to a scene
local scene = view:getScene()
local scene = view:getScene()
scene:setAutoSort(true) -- enables depth sorting for the children of a Sprite (from farthest to nearest)
</syntaxhighlight>
</syntaxhighlight>


== Our first shape: a Plane ==
== Our first shape: a Plane ==
We are going to use the Plane shape (helper file ''3DShapes.lua''), this will be our ground.
We are going to use the Plane shape (helper file ''3DShapesV2.lua''), this will be our ground.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- our first shape, a plane for the ground
-- our first shape, a plane for the ground
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
local ground = D3.PlaneV2.new(1.2*8, 1.2*8) -- (width,depth)
-- a color
-- a color
ground:mapColor(0x9d7878) -- (color,[alpha=1])
ground:mapColor(0x55ffff) -- (color,[alpha=1])
</syntaxhighlight>
</syntaxhighlight>


Let's texture our plane, I picked this one: https://polyhaven.com/a/forrest_ground_01, '''Diffuse''', '''1K''', '''JPG'''
Let's texture our plane, I picked this one: https://polyhaven.com/a/forrest_ground_01, '''Diffuse''' + '''Normal (GL)''', '''1K''', '''JPG'''


[[File:3d_plane_polyhaven_forrest_ground.png|480px]]
[[File:3d_plane_polyhaven_forrest_ground.png|480px]]


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- a texture (diffuse)
-- texture (diffuse) and normal map
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local texsx, texsy = 0.1*8, 0.1*8 -- texture scaling
local normal = Texture.new("textures/forrest_ground_01_nor_gl_1k.jpg", nil, { wrap=Texture.REPEAT, } )
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
local texsx, texsy = 0.09*8, 0.09*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)
</syntaxhighlight>
</syntaxhighlight>


Line 80: Line 88:


== More shapes ==
== More shapes ==
The Mesh3D class (''3DShapes.lua'') has other shapes you can use: '''D3.Cube''', '''D3.Sphere''', '''D3.Cylinder'''.
The Mesh3D class (''3DShapes.lua'') has other shapes you can use: '''D3.CubeV2''', '''D3.SphereV2''', '''D3.CylinderV2''', '''D3.PyramidV2'''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- more shapes
-- more shapes
local cube = D3.Cube.new(0.15*8, 0.17*8, 0.15*8)
local cube = D3.CubeV2.new(0.1*8, 0.2*8, 0.1*8) -- (w,h,d)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
cube:mapColor(0xffff00) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
texsx, texsy = 0.007*8, 0.007*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:mapTexture(tex, texsx, texsy, normal)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:setPosition(-0.1*8, cube.dims.h/2, 0.0*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)
scene:addChild(cube)


local sphere = D3.Sphere.new(2*8, 0.12*8) -- (steps,radius)
local sphere = D3.SphereV2.new(2*8, 0.2*8) -- (steps,diameter)
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy)
sphere:mapTexture(tex, texsx, texsy, normal)
sphere:setPosition(0.1*8, 0.12*8, 0.07*8)
sphere:setPosition(-0.3*8, sphere.dims.r, 0.0*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)
scene:addChild(sphere)


local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
local cylinder = D3.CylinderV2.new(2*8, 0.2*8, 0.2*8) -- (steps,diameter,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
texsx, texsy = 0.025*8, 0.025*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:mapTexture(tex, texsx, texsy, normal)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:setPosition(0.1*8, cylinder.dims.h/2, 0.0*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)
scene:addChild(cylinder)
local pyramid = D3.PyramidV2.new(0.15*8, 0.2*8) -- (diameter,height)
pyramid:mapColor(0xff5500) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
pyramid:mapTexture(tex, texsx, texsy, normal)
pyramid:setPosition(0.3*8, pyramid.dims.h/2, 0.0*8)
pyramid:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(pyramid)
</syntaxhighlight>
</syntaxhighlight>


Line 121: Line 137:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- light setup
-- light setup
local lsx, lsy, lsz = 0.4*8, 0.8*8, 0.4*8 -- light source position
local lsx, lsy, lsz = 0.5*8, 0.55*8, 0.2*8 -- light source position
Lighting.setLight(lsx,lsy,lsz, 0.35) -- (x,y,z, ambient)
Lighting.setLight(lsx,lsy,lsz, 0.3) -- (x,y,z, ambient)
</syntaxhighlight>
</syntaxhighlight>


Line 128: Line 144:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
local pd, fov = 3*8, 7*8
local pd, fov = 4*8, 7*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
</syntaxhighlight>
</syntaxhighlight>
Line 135: Line 151:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Lighting.computeShadows(scene)
Lighting.computeShadows(scene)
</syntaxhighlight>
== Extra ==
The ''3DShapes.lua'' file comes with many modes: ''Mesh3D.MODE_TEXTURE=1'', ''Mesh3D.MODE_LIGHTING=2'', ''Mesh3D.MODE_BUMP=4'', ''Mesh3D.MODE_SHADOW=8'', ''Mesh3D.MODE_ANIMATED=16'', ''Mesh3D.MODE_INSTANCED=32'', ''Mesh3D.MODE_COLORED=64'', ''Mesh3D.MODE_VOXEL=128'', ''Mesh3D.MODE_OCCLUSION=256'', ''Mesh3D.MODE_DEPTHMASK=512'', ''Mesh3D.MODE_BRICKS=1024'', ''Mesh3D.MODE_FOG=2048''
So far we have used:
*Mesh3D.MODE_TEXTURE=1
*Mesh3D.MODE_LIGHTING=2 --ie normals
*Mesh3D.MODE_BUMP=4
*Mesh3D.MODE_SHADOW=8
*Mesh3D.MODE_COLORED=64
Let's see fog mode: '''Mesh3D.MODE_FOG=2048'''. First, we tell which shapes will be in the fog.
<syntaxhighlight lang="lua">
-- fog
ground:updateMode( D3.Mesh.MODE_FOG )
cube:updateMode( D3.Mesh.MODE_FOG )
sphere:updateMode( D3.Mesh.MODE_FOG )
cylinder:updateMode( D3.Mesh.MODE_FOG )
pyramid:updateMode( D3.Mesh.MODE_FOG )
</syntaxhighlight>
Then we set the fog parameters (''3DLighting.lua''):
*''fogDistance'': with a minimum distance and a distance range
*''fogColor'': r,g,b,a with values between 0 and 1 (values can be greater than 1 too 😉)
<syntaxhighlight lang="lua">
Lighting.setFog(0.25*8,1*8, 0.1,0.1,0.1,2) -- (dmin,drange, r,g,b,a)
</syntaxhighlight>
</syntaxhighlight>


'''And voilà'''!
'''And voilà'''!


[[File:ss_3DShapes.png]]
[[File:ss_3DShapesV2.png]]


== Playground ==
== The code ==
'''The full code'''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
application:setBackgroundColor(0x454545)
application:setBackgroundColor(0x454545)
Line 151: Line 193:


-- we position the view (camera) and set its target
-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.5*8,0.5*8, 0*8,0*8,0*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])
view:lookAt(0.4*8,0.3*8,0.3*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
-- we add the view (camera) to Gideros stage
Line 158: Line 200:
-- we will add 3D shapes and models to a scene
-- we will add 3D shapes and models to a scene
local scene = view:getScene()
local scene = view:getScene()
--scene:setAutoSort(true)
scene:setAutoSort(true) -- enables depth sorting for the children of a Sprite (from farthest to nearest)


-- our first shape, a plane for the ground
-- a shape
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
local ground = D3.PlaneV2.new(1.2*8, 1.2*8) -- (width,depth)
ground:mapColor(0x9d7878) -- (color,[alpha=1])
ground:mapColor(0x55ffff) -- (color,[alpha=1])
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local tex = Texture.new("textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local texsx, texsy = 0.1*8, 0.1*8 -- texture scaling
local normal = Texture.new("textures/forrest_ground_01_nor_gl_1k.jpg", nil, { wrap=Texture.REPEAT, } )
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
local texsx, texsy = 0.09*8, 0.09*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)
-- we tell the shape to receive light and shadows
-- we tell the shape to receive light and shadows
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
Line 172: Line 215:


-- more shapes
-- more shapes
local cube = D3.Cube.new(0.15*8, 0.17*8, 0.15*8)
local cube = D3.CubeV2.new(0.1*8, 0.2*8, 0.1*8) -- (w,h,d)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
cube:mapColor(0xffff00) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
texsx, texsy = 0.007*8, 0.007*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:mapTexture(tex, texsx, texsy, normal)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:setPosition(-0.1*8, cube.dims.h/2, 0.0*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)
scene:addChild(cube)


local sphere = D3.Sphere.new(2*8, 0.12*8) -- (steps,radius)
local sphere = D3.SphereV2.new(2*8, 0.2*8) -- (steps,diameter)
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy)
sphere:mapTexture(tex, texsx, texsy, normal)
sphere:setPosition(0.1*8, 0.12*8, 0.07*8)
sphere:setPosition(-0.3*8, sphere.dims.r, 0.0*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)
scene:addChild(sphere)


local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
local cylinder = D3.CylinderV2.new(2*8, 0.2*8, 0.2*8) -- (steps,diameter,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
texsx, texsy = 0.025*8, 0.025*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:mapTexture(tex, texsx, texsy, normal)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:setPosition(0.1*8, cylinder.dims.h/2, 0.0*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)
scene:addChild(cylinder)
local pyramid = D3.PyramidV2.new(0.15*8, 0.2*8) -- (diameter,height)
pyramid:mapColor(0xff5500) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
pyramid:mapTexture(tex, texsx, texsy, normal)
pyramid:setPosition(0.3*8, pyramid.dims.h/2, 0.0*8)
pyramid:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(pyramid)


-- light setup
-- light setup
local lsx, lsy, lsz = 0.4*8, 0.8*8, 0.4*8 -- light source position
local lsx, lsy, lsz = 0.5*8, 0.55*8, 0.2*8 -- light source position
Lighting.setLight(lsx,lsy,lsz, 0.3) -- (x,y,z, ambient)
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
Lighting.setLight(lsx,lsy,lsz, 0.35) -- (x,y,z, ambient)
local pd, fov = 4*8, 7*8
local pd, fov = 3*8, 7*8 -- 5*8, 6*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
Lighting.computeShadows(scene)
Lighting.computeShadows(scene)
-- fog
ground:updateMode( D3.Mesh.MODE_FOG )
cube:updateMode( D3.Mesh.MODE_FOG )
sphere:updateMode( D3.Mesh.MODE_FOG )
cylinder:updateMode( D3.Mesh.MODE_FOG )
pyramid:updateMode( D3.Mesh.MODE_FOG )
Lighting.setFog(0.05*8, 1*8, 0.1,0.1,0.1,3) -- (dmin,drange,r,g,b,a)


-- game loop
-- game loop
Line 209: Line 268:
sphere:setRotationY(sphere:getRotationY()+9*8*e.deltaTime)
sphere:setRotationY(sphere:getRotationY()+9*8*e.deltaTime)
cylinder:setRotationY(cylinder:getRotationY()+10*8*e.deltaTime)
cylinder:setRotationY(cylinder:getRotationY()+10*8*e.deltaTime)
pyramid:setRotationX(pyramid:getRotationX()+3*8*e.deltaTime)
Lighting.computeShadows(scene)
Lighting.computeShadows(scene)
end)
end)
</syntaxhighlight>
</syntaxhighlight>


== I am out 2 ==
== I am out V2 ==
Please have a look at [[Libs3D Library ShapesV2|the Shapes Library V2]].
 
You are strongly encouraged to mess with the code.
 
'''Bye for now'''!
 
----
'''[[Libs3D]]'''
{{GIDEROS IMPORTANT LINKS}}
 
 
 
 
 
 
 
 
 
 
 
__TOC__
 
== I am ready ==
As we have seen, to make more advanced 3D, Gideros has a bunch of helper files packed in the '''Library''' folder.
 
Here is the plan:
* start with a camera
* a world to populate (scene)
* some common 3D shapes (plane, cube, ...)
* light and shadows
* playground
 
Copy the following two '''Library''' folders in your project '''''assets''''' folder:
*'''3dbase'''
*'''luashader'''
 
== The Camera ==
The camera is a Viewport with a perspective projection Matrix (helper file ''3DView.lua''). Let's add one:
<syntaxhighlight lang="lua">
application:setBackgroundColor(0x454545)
 
-- 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)
</syntaxhighlight>
 
You can position the view (camera) and set its target using the Viewport:lookAt function.
<syntaxhighlight lang="lua">
-- we position the view (camera) and set its target
view:lookAt(0.25*8,0.5*8,0.5*8, 0*8,0*8,0*8) -- (eyex,eyey,eyez, targetx,targety,targetz[, upx,upy,upz])
</syntaxhighlight>
 
In order to display the content of the Viewport (camera) to the screen, we add it to Gideros '''stage'''.
<syntaxhighlight lang="lua">
-- we add the view (camera) to Gideros stage
stage:addChild(view)
</syntaxhighlight>
 
=== The World (scene) ===
We will add all our 3D models to a scene. The scene is a simple ''Sprite'' and is part of the camera.
<syntaxhighlight lang="lua">
-- we will add 3D shapes and models to a scene
local scene = view:getScene()
</syntaxhighlight>
 
== Our first shape: a Plane ==
We are going to use the Plane shape (helper file ''3DShapes.lua''), this will be our ground.
<syntaxhighlight lang="lua">
-- a shape
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
-- a color
ground:mapColor(0x554333, 0.5) -- (color,[alpha=1])
</syntaxhighlight>
 
Let's texture our plane, I picked this one: https://polyhaven.com/a/forrest_ground_01, '''Diffuse''' +  '''Normal (GL)''', '''1K''', '''JPG'''
 
[[File:3d_plane_polyhaven_forrest_ground.png|480px]]
 
<syntaxhighlight lang="lua">
-- a shape
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
-- a color
--ground:mapColor(0x554333, 0.5) -- (color,[alpha=1])
-- a texture (diffuse + normal)
local tex = Texture.new("models/textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local normal = Texture.new("models/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)
</syntaxhighlight>
 
We tell the shape to receive light and shadows.
<syntaxhighlight lang="lua">
-- we tell the shape to receive light and shadows
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
</syntaxhighlight>
 
And we add the ground to the world (scene).
<syntaxhighlight lang="lua">
-- we add the shape to the scene (world)
scene:addChild(ground)
</syntaxhighlight>
 
 
'''...TO BE CONTINUED...'''
 
 
== I am out 2 ==
...
 
You are strongly encouraged to mess with the code.
You are strongly encouraged to mess with the code.



Latest revision as of 17:01, 3 August 2026


This is Shapes Library V2 💪
  • dimensions where 1 unit is indeed 1 unit
  • added normal mapping
  • added a pyramid shape

Media:3DShapesV2.lua (tip: right click and save link as). Add it to the 3dbase folder and exclude from execution, 3DShapes.lua

I am ready

As we have seen, to make more advanced 3D, Gideros has a bunch of helper files packed in the Library folder.

Here is the plan:

  • start with a camera
    • a world to populate (scene)
  • some common 3D shapes (plane, cube, ...)
  • light and shadows
  • extra (fog mode)
  • the code

Copy the following two Library folders in your project assets folder:

  • 3dbase
  • luashader

The Camera

The camera is a Viewport with a perspective projection Matrix (helper file 3DView.lua). Let's add one:

application:setBackgroundColor(0x454545)

-- 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)

You can position the view (camera) and set its target using the Viewport:lookAt function.

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

In order to display the content of the Viewport (camera) to the screen, we add it to Gideros stage.

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

The World (scene)

We will add all our 3D models to a scene. The scene is a simple Sprite and is part of the camera.

-- we will add 3D shapes and models to a scene
local scene = view:getScene()
scene:setAutoSort(true) -- enables depth sorting for the children of a Sprite (from farthest to nearest)

Our first shape: a Plane

We are going to use the Plane shape (helper file 3DShapesV2.lua), this will be our ground.

-- our first shape, a plane for the ground
local ground = D3.PlaneV2.new(1.2*8, 1.2*8) -- (width,depth)
-- a color
ground:mapColor(0x55ffff) -- (color,[alpha=1])

Let's texture our plane, I picked this one: https://polyhaven.com/a/forrest_ground_01, Diffuse + Normal (GL), 1K, JPG

-- texture (diffuse) and normal map
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.09*8, 0.09*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)

We tell the shape to receive light and shadows.

-- we tell the shape to receive light and shadows
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)

And we add the ground to the world (scene).

-- we add the shape to the scene (world)
scene:addChild(ground)

More shapes

The Mesh3D class (3DShapes.lua) has other shapes you can use: D3.CubeV2, D3.SphereV2, D3.CylinderV2, D3.PyramidV2.

-- more shapes
local cube = D3.CubeV2.new(0.1*8, 0.2*8, 0.1*8) -- (w,h,d)
cube:mapColor(0xffff00) -- (color,[alpha=1])
texsx, texsy = 0.007*8, 0.007*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy, normal)
cube:setPosition(-0.1*8, cube.dims.h/2, 0.0*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)

local sphere = D3.SphereV2.new(2*8, 0.2*8) -- (steps,diameter)
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy, normal)
sphere:setPosition(-0.3*8, sphere.dims.r, 0.0*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)

local cylinder = D3.CylinderV2.new(2*8, 0.2*8, 0.2*8) -- (steps,diameter,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.025*8, 0.025*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy, normal)
cylinder:setPosition(0.1*8, cylinder.dims.h/2, 0.0*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)

local pyramid = D3.PyramidV2.new(0.15*8, 0.2*8) -- (diameter,height)
pyramid:mapColor(0xff5500) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
pyramid:mapTexture(tex, texsx, texsy, normal)
pyramid:setPosition(0.3*8, pyramid.dims.h/2, 0.0*8)
pyramid:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(pyramid)

Lighting

The final touch is lighting (3DLighting.lua).

About lighting:

  • Lighting.setLight(x,y,z,a) sets the position of the light source (spotlight) and the amount of ambient light
  • Lighting.setLightTarget(x,y,z,d,f) sets the target of the light source. x,y,z is the position where the light is pointing at, d determines the projection distance when computing shadows (d gives the far plane, and the near plane is computed as d/16 internally). f is the FoV angle (actually the half angle), it determines the opening of your spotlight cone, and has a consequence on the shadow precision
  • Only objects between d and d/16 in the light cone will cast shadows
  • Select d wisely as it has an impact on shadow precision
  • the lib uses a 1024x1024 shadow map

The light source position and ambient light.

-- light setup
local lsx, lsy, lsz = 0.5*8, 0.55*8, 0.2*8 -- light source position
Lighting.setLight(lsx,lsy,lsz, 0.3) -- (x,y,z, ambient)

The light target has a position and a projection distance plus a field of view (fov) angle.

local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
local pd, fov = 4*8, 7*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)

When we added the shapes to the world, we gave the shapes the ability to receive light and shadows: D3.Mesh.MODE_LIGHTING, D3.Mesh.MODE_SHADOW. We need to compute the shadow map.

Lighting.computeShadows(scene)

Extra

The 3DShapes.lua file comes with many modes: Mesh3D.MODE_TEXTURE=1, Mesh3D.MODE_LIGHTING=2, Mesh3D.MODE_BUMP=4, Mesh3D.MODE_SHADOW=8, Mesh3D.MODE_ANIMATED=16, Mesh3D.MODE_INSTANCED=32, Mesh3D.MODE_COLORED=64, Mesh3D.MODE_VOXEL=128, Mesh3D.MODE_OCCLUSION=256, Mesh3D.MODE_DEPTHMASK=512, Mesh3D.MODE_BRICKS=1024, Mesh3D.MODE_FOG=2048

So far we have used:

  • Mesh3D.MODE_TEXTURE=1
  • Mesh3D.MODE_LIGHTING=2 --ie normals
  • Mesh3D.MODE_BUMP=4
  • Mesh3D.MODE_SHADOW=8
  • Mesh3D.MODE_COLORED=64

Let's see fog mode: Mesh3D.MODE_FOG=2048. First, we tell which shapes will be in the fog.

-- fog
ground:updateMode( D3.Mesh.MODE_FOG )
cube:updateMode( D3.Mesh.MODE_FOG )
sphere:updateMode( D3.Mesh.MODE_FOG )
cylinder:updateMode( D3.Mesh.MODE_FOG )
pyramid:updateMode( D3.Mesh.MODE_FOG )

Then we set the fog parameters (3DLighting.lua):

  • fogDistance: with a minimum distance and a distance range
  • fogColor: r,g,b,a with values between 0 and 1 (values can be greater than 1 too 😉)
Lighting.setFog(0.25*8,1*8, 0.1,0.1,0.1,2) -- (dmin,drange, r,g,b,a)

And voilà!

The code

application:setBackgroundColor(0x454545)

-- 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.4*8,0.3*8,0.3*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()
scene:setAutoSort(true) -- enables depth sorting for the children of a Sprite (from farthest to nearest)

-- a shape
local ground = D3.PlaneV2.new(1.2*8, 1.2*8) -- (width,depth)
ground:mapColor(0x55ffff) -- (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.09*8, 0.09*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy, normal)
-- we tell the shape to receive light and shadows
ground:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
-- we add the shape to the scene
scene:addChild(ground)

-- more shapes
local cube = D3.CubeV2.new(0.1*8, 0.2*8, 0.1*8) -- (w,h,d)
cube:mapColor(0xffff00) -- (color,[alpha=1])
texsx, texsy = 0.007*8, 0.007*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy, normal)
cube:setPosition(-0.1*8, cube.dims.h/2, 0.0*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)

local sphere = D3.SphereV2.new(2*8, 0.2*8) -- (steps,diameter)
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy, normal)
sphere:setPosition(-0.3*8, sphere.dims.r, 0.0*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)

local cylinder = D3.CylinderV2.new(2*8, 0.2*8, 0.2*8) -- (steps,diameter,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.025*8, 0.025*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy, normal)
cylinder:setPosition(0.1*8, cylinder.dims.h/2, 0.0*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)

local pyramid = D3.PyramidV2.new(0.15*8, 0.2*8) -- (diameter,height)
pyramid:mapColor(0xff5500) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
pyramid:mapTexture(tex, texsx, texsy, normal)
pyramid:setPosition(0.3*8, pyramid.dims.h/2, 0.0*8)
pyramid:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(pyramid)

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

-- fog
ground:updateMode( D3.Mesh.MODE_FOG )
cube:updateMode( D3.Mesh.MODE_FOG )
sphere:updateMode( D3.Mesh.MODE_FOG )
cylinder:updateMode( D3.Mesh.MODE_FOG )
pyramid:updateMode( D3.Mesh.MODE_FOG )
Lighting.setFog(0.05*8, 1*8, 0.1,0.1,0.1,3) -- (dmin,drange,r,g,b,a)

-- game loop
stage:addEventListener(Event.ENTER_FRAME, function(e)
	cube:setRotationX(cube:getRotationX()+8*8*e.deltaTime)
	sphere:setRotationY(sphere:getRotationY()+9*8*e.deltaTime)
	cylinder:setRotationY(cylinder:getRotationY()+10*8*e.deltaTime)
	pyramid:setRotationX(pyramid:getRotationX()+3*8*e.deltaTime)
	Lighting.computeShadows(scene)
end)

I am out V2

You are strongly encouraged to mess with the code.

Bye for now!


Libs3D