Defold is at its core a 3D engine. Even when you work with 2D material only all rendering is done in 3D, but orthographically projected onto the screen. Defold allows you to utilize full 3D content by including 3D assets, or Models into your collections. You can build games in strictly 3D with only 3D assets, or you can mix 3D and 2D content as you wish.
Model components are created just like any other game object component. You can do it two ways:
With the model created you need to specify a number of properties:
Apart from the properties Id, Position and Rotation the following component specific properties exist:
With the model component in place you are free to edit and manipulate the component and/or the encapsulating game object with the regular Scene Editor tools to move, rotate and scale the model to your liking.
You can manipulate models in runtime through a number of different functions and properties (refer to the API docs for usage).
Defold provides powerful support for controlling animation in runtime. More in the model animation manual:
local play_properties = { blend_duration = 0.1 }
model.play_anim("#model", "jump", go.PLAYBACK_ONCE_FORWARD, play_properties)
The animation playback cursor can be animated either by hand or through the property animation system:
-- set the run animation
model.play_anim("#model", "run", go.PLAYBACK_NONE)
-- animate the cursor
go.animate("#model", "cursor", go.PLAYBACK_LOOP_PINGPONG, 1, go.EASING_LINEAR, 10)
A model also has a number of different properties that can be manipulated using go.get()
and go.set()
:
animation
hash
) (READ ONLY). You change animation using model.play_anim()
(see above).cursor
number
).material
hash
). You can change this using a material resource property and go.set()
. Refer to the API reference for an example.playback_rate
number
).textureN
hash
). You can change this using a texture resource property and go.set()
. Refer to the API reference for an example.3D software commonly allows you to set properties on your object vertices, like coloring and texturing. This information goes into the glTF .gltf or Collada .dae file that you export from your 3D software. Depending on the requirements of your game you will have to select and/or create appropriate and performant materials for your objects. A material combines shader programs with a set of parameters for rendering of the object.
There is a simple 3D model material available in the built-in materials folder. If you need to create custom materials for your models, see the Material documentation for information. The Shader manual contains information on how shader programs work.
The default model material has the following constants that can be changed using go.set() or go.animate() (refer to the Material manual for more details). Examples:
go.set("#model", "tint", vmath.vector4(1,0,0,1))
go.animate("#model", "tint", go.PLAYBACK_LOOP_PINGPONG, vmath.vector4(1,0,0,1), go.EASING_LINEAR, 2)
tint
vector4
). The vector4 is used to represent the tint with x, y, z, and w corresponding to the red, green, blue and alpha tint.The default render script is tailor made for 2D games and does not work with 3D models. But by copying the default render script and adding a handful of lines of code to the render script you can enable rendering of your models. For instance:
function init(self)
self.model_pred = render.predicate({"model"})
...
end
function update()
...
render.set_depth_mask(true)
render.enable_state(render.STATE_DEPTH_TEST)
render.set_projection(stretch_projection(-1000, 1000)) -- orthographic
render.draw(self.model_pred)
render.set_depth_mask(false)
...
end
See the Render documentation for details on how render scripts work.
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB