Libs3D Library Shapes: Difference between revisions

From GiderosMobile
Created page with "__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. I will try to impress you with 3D..."
ย 
No edit summary
ย 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
__TOC__
__TOC__
'''<big>Please note: there is [[Libs3D Library ShapesV2|a Shapes Library V2]]. We only keep this one for backward compatibility ๐Ÿคทโ€โ™‚๏ธ</big>'''


== I am ready ==
== I am ready ==
As we have seen, to make more advanced 3D, Gideros has a bunch of helper files packed in the '''Library''' folder.
As we have seen, to make more advanced 3D, Gideros has a bunch of helper files packed in the '''Library''' folder.
I will try to impress you with 3D Gideros โœจ.


Here is the plan:
Here is the plan:
* start with shapes
* start with a camera
* add some .obj models
** a world to populate (scene)
* add some .fbx animations
* some common 3D shapes (plane, cube, ...)
* light and shadows
* playground


Please create a new Gideros project and copy the two following '''Library''' folders in the '''''assets''''' folder:
Copy the following two '''Library''' folders in your project '''''assets''''' folder:
*'''3dbase'''
*'''3dbase'''
*'''luashader'''
*'''luashader'''


Create a '''models''' folder, and in this folder an '''objs''' folder. I also add a '''textures''' folder. Your file structure should look something like this:
== The Camera ==
*'''YourProjectName'''
The camera is a Viewport with a perspective projection Matrix (helper file ''3DView.lua''). Let's add one:
**Plugins
<syntaxhighlight lang="lua">
**Files
application:setBackgroundColor(0x454545)
***3dbase
***luashader
***models
****objs
****textures
***''main''.lua


You can run your project, you should have a blank screen.
-- 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>


== .obj Rulez ==
You can position the view (camera) and set its target using the Viewport:lookAt function.
When you work with 3D, you need to follow some "engine" rulez. In Gideros, for the '''.obj''' file format, those are:
<syntaxhighlight lang="lua">
*'''currently Gideros supports ''.png'' and ''.jpg'' image formats'''
-- we position the view (camera) and set its target
*'''currently Gideros supports ''Base Color'', ''Texture map'' and ''Normal Map'''''
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])
*'''the textures shouldn't be embedded in the models. In Blender, use ''unpack resources''''':
</syntaxhighlight>
[[File:Blender_unpack_resources.png|414px]]


*'''your Blender BSDF shaders should look something like''':
In order to display the content of the Viewport (camera) to the screen, we add it to Gideros '''stage'''.
[[File:Blender_shading_tab.png|558px]]
<syntaxhighlight lang="lua">
-- we add the view (camera) to Gideros stage
stage:addChild(view)
</syntaxhighlight>


*'''Gideros will import the position of the models, so make sure your models are at location 0, 0, 0'''
=== 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>


*'''I believe Gideros also imports the origin of the models, so place the origins accordingly'''
== 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">
-- our first shape, a plane for the ground
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
-- a color
ground:mapColor(0x9d7878) -- (color,[alpha=1])
</syntaxhighlight>


'''I personally use this Blender add-on: https://extensions.blender.org/add-ons/act-game-asset-creation-toolset/'''
Let's texture our plane, I picked this one: https://polyhaven.com/a/forrest_ground_01, '''Diffuse''', '''1K''', '''JPG'''


Please note, you can still have the ''Metallic'', ''Roughness'' and other textures attached in your Blender BSDF shaders, Gideros will just ignore them.
[[File:3d_plane_polyhaven_forrest_ground.png|480px]]


== The assets: '''.obj''' ==
<syntaxhighlight lang="lua">
I have chosen this cc0 asset pack: https://roadtovostok.itch.io/road-to-vostok-assets-vol1.
-- a texture (diffuse)
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
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
</syntaxhighlight>


In 3D, the assets you download from the internet, often need some adjustement to follow your "engine" rulez ๐Ÿคทโ€โ™‚๏ธ. I already made those adjustements and you can find the adjusted asset pack here: '''https://mokatunprod.itch.io/3dpixel-obj-packs'''.
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>


Please copy the '''roadtovostok''' asset pack (or your own models) in the ''objs'' folder:
And we add the ground to the world (scene).
***models
<syntaxhighlight lang="lua">
****objs
-- we add the shape to the scene (world)
*****roadtovostok
scene:addChild(ground)
</syntaxhighlight>


== Gideros 3D ==
== More shapes ==
For 3D, we use a Viewport with a 3D perspective projection Matrix. Everything is already setup and ready to use in the helper file ''3DView.lua''.
The Mesh3D class (''3DShapes.lua'') has other shapes you can use: '''D3.Cube''', '''D3.Sphere''', '''D3.Cylinder'''.
<syntaxhighlight lang="lua">
-- more shapes
local cube = D3.Cube.new(0.15*8, 0.17*8, 0.15*8)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)


We set up a fullscreen 3D Viewport. For the perspective projection Matrix, we need a field of view (fov), a clipping near plane distance and a clipping far plane distance.
local sphere = D3.Sphere.new(2*8, 0.12*8) -- (steps,radius)
<syntaxhighlight lang="lua">
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
application:setBackgroundColor(0x454545)
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy)
sphere:setPosition(0.1*8, 0.12*8, 0.07*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)


-- we set up a fullscreen 3D viewport (fov: consoles 60, pc 90)
local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
local sw, sh = application:getContentWidth(), application:getContentHeight()
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
local view = D3.View.new(sw,sh, 90, 0.1,1*1024) -- (sw,sh, fov, near,far)
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)
</syntaxhighlight>
</syntaxhighlight>


'''For consoles a good fov angle should be around 60 and for pc around 90'''
== Lighting ==
The final touch is lighting (''3DLighting.lua'').


<youtube>blZUao2jTGA</youtube>
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 view is a Viewport with a perspective projection. You can see it as a "camera" you can position and set a target to look at.
The light source position and ambient light.
<syntaxhighlight lang="lua">
-- light setup
local lsx, lsy, lsz = 0.4*8, 0.8*8, 0.4*8 -- light source position
Lighting.setLight(lsx,lsy,lsz, 0.35) -- (x,y,z, ambient)
</syntaxhighlight>


Finally, we will add all our 3D models to a scene. The scene is a simple Sprite.
The light target has a position and a projection distance plus a field of view (fov) angle.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- we will add 3D shapes and models to a scene
local ltx, lty, ltz = 0.0*8, 0.0*8, 0.0*8 -- light target position
local scene = view:getScene()
local pd, fov = 3*8, 7*8
Lighting.setLightTarget(ltx,lty,ltz, pd,fov) -- (x,y,z, projection distance,fov)
</syntaxhighlight>
</syntaxhighlight>


== A shape for the Ground ==
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.
The asset pack doesn't come with some kind of ground, so let's build our own.
ย 
For the ground, we are going to use the Plane shape available in the helper file ''3DShapes.lua''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- a shape
Lighting.computeShadows(scene)
local ground = D3.Plane.new(0.6*8, nil, 0.6*8) -- (w,h,d)
</syntaxhighlight>
</syntaxhighlight>


'''By definition a plane is flat, that's why we can set the height (h) as nil'''
'''And voilร '''!
ย 
[[File:ss_3DShapes.png]]
ย 
== Playground ==
'''The full code'''
<syntaxhighlight lang="lua">
application:setBackgroundColor(0x454545)


We add some image texture to it. Please pick any ground texture you like from any site (eg. https://polyhaven.com/textures) and put it in the '''textures''' folder. I picked this one: https://polyhaven.com/a/forrest_ground_01 '''1K JPG'''.
-- 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)


[[File:3d_plane_polyhaven_forrest_ground.png|480px]]
-- 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 lang="lua">
-- we add the view (camera) to Gideros stage
-- a shape
stage:addChild(view)
local ground = D3.Plane.new(0.6*8, nil, 0.6*8) -- (w,h,d)
-- and its texture
local tex = Texture.new("models/textures/forrest_ground_01_diff_1k.jpg", nil, { wrap=Texture.REPEAT, } )
local texsx, texsy = 0.17*8, 0.17*8 -- texture scaling
ground:mapTexture(tex, texsx, texsy)
</syntaxhighlight>


'''Scale the texture according to your texture size using texture scale x (tessx) and texture scale y (texsy)'''
-- we will add 3D shapes and models to a scene
local scene = view:getScene()
--scene:setAutoSort(true)


We tell the shape to receive light and shadows.
-- our first shape, a plane for the ground
<syntaxhighlight lang="lua">
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
ground:mapColor(0x9d7878) -- (color,[alpha=1])
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
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
-- 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)
</syntaxhighlight>
Finally, we add the ground to the scene and the view to Gideros '''stage'''. The view (camera) has a position and a target to look at.
<syntaxhighlight lang="lua">
-- we add the shape to the scene
-- we add the shape to the scene
scene:addChild(ground)
scene:addChild(ground)
-- and the scene to stage
stage:addChild(view)


-- we position the view (camera) and its target
-- more shapes
view:lookAt(0*8,0.5*8,1*8, 0*8,0*8,0*8) -- (eyex,eyey,eyez, targetx,targety,targetz, upx,upy,upz)
local cube = D3.Cube.new(0.15*8, 0.17*8, 0.15*8)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)
ย 
local sphere = D3.Sphere.new(2*8, 0.12*8) -- (steps,radius)
sphere:mapColor(0x55ff00) -- (color,[alpha=1])
texsx, texsy = 0.01*8, 0.01*8 -- texture scaling
sphere:mapTexture(tex, texsx, texsy)
sphere:setPosition(0.1*8, 0.12*8, 0.07*8)
sphere:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(sphere)
ย 
local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)
ย 
-- light setup
local lsx, lsy, lsz = 0.4*8, 0.8*8, 0.4*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.35) -- (x,y,z, ambient)
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.computeShadows(scene)
ย 
-- 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)
Lighting.computeShadows(scene)
end)
</syntaxhighlight>
</syntaxhighlight>


== I am out 2 ==
== I am out 2 ==
To do.
Please have a look at [[Libs3D Library ShapesV2|the Shapes Library V2]].


You are strongly encouraged to mess with the code.
You are strongly encouraged to mess with the code.

Latest revision as of 03:58, 1 August 2026


Please note: there is a Shapes Library V2. We only keep this one for backward compatibility ๐Ÿคทโ€โ™‚๏ธ

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:

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.25*8,0.5*8,0.5*8, 0*8,0*8,0*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()

Our first shape: a Plane

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

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

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

-- a texture (diffuse)
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
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)

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.Cube, D3.Sphere, D3.Cylinder.

-- more shapes
local cube = D3.Cube.new(0.15*8, 0.17*8, 0.15*8)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)

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

local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)

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.4*8, 0.8*8, 0.4*8 -- light source position
Lighting.setLight(lsx,lsy,lsz, 0.35) -- (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 = 3*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)

And voilร !

Playground

The full 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.25*8,0.5*8,0.5*8, 0*8,0*8,0*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)

-- our first shape, a plane for the ground
local ground = D3.Plane.new(1*8, 1*8) -- (width,depth)
ground:mapColor(0x9d7878) -- (color,[alpha=1])
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
ground:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
-- 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.Cube.new(0.15*8, 0.17*8, 0.15*8)
cube:mapColor(0xb3ffb8) -- (color,[alpha=1])
texsx, texsy = 0.03*8, 0.03*8 -- texture scaling
cube:mapTexture(tex, texsx, texsy) -- (texture,sw,sh)
cube:setPosition(-0.2*8, 0.17*8, -0.05*8)
cube:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cube)

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

local cylinder = D3.Cylinder.new(2*8, 0.1*8, 0.25*8) -- (steps,radius,height)
cylinder:mapColor(0xffaa00) -- (color,[alpha=1])
texsx, texsy = 0.05*8, 0.05*8 -- texture scaling
cylinder:mapTexture(tex, texsx, texsy)
cylinder:setPosition(0.3*8, 0.2, 0.15*8)
cylinder:updateMode(D3.Mesh.MODE_LIGHTING + D3.Mesh.MODE_SHADOW)
scene:addChild(cylinder)

-- light setup
local lsx, lsy, lsz = 0.4*8, 0.8*8, 0.4*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.35) -- (x,y,z, ambient)
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.computeShadows(scene)

-- 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)
	Lighting.computeShadows(scene)
end)

I am out 2

Please have a look at the Shapes Library V2.

You are strongly encouraged to mess with the code.

Bye for now!


Libs3D