Getting started - Introduction - Installing Defold - The editor - Glossary - Defold for Flash users - Getting help Project setup - Creating a project - Project settings - Libraries Core concepts - Building blocks - Addressing - Message passing - Application lifecycle Assets and resources - Importing assets - Importing 2D graphics - Importing 3D models - Adapting to different screen sizes - Live update - Animation - Atlas - Font - Resource management - Tile source - Texture filtering - Texture profiles Components - Overview - Collection factory - Collection proxy - Collision object - Camera - Factory - Label - Mesh - Model - Particle FX - Sound - Spine animation - Spine model - Sprite - Tilemap Gui - GUI overview - Box nodes - Text nodes - Pie nodes - Spine nodes - ParticleFX nodes - Template nodes - Scripts - Clipping - Layouts Physics - Physics overview - Collision objects - Collision shapes - Collision groups - Collision messages - Resolving collisions - Ray casts - Joints and constraints Game logic - Scripts - Properties - Script properties - Lua in Defold - Input - Modules - Debugging Saving and loading files - Working with files Networking - Overview - HTTP Requests - Socket connections - WebSocket connections - Online services Rendering - Render - Material - Shader - Texture filtering Workflow - Adapting to different screen sizes - Bundling an application - Bob the builder - Hot reloading - Optimizing an application - Profiling - Refactoring - The mobile dev app - Version control - Writing code - Working offline Debugging - Debugging game logic - Debugging native code - Debugging native code on Android - Debugging native code on iOS - Reading game and system logs - Profiling Platforms - Android - iOS - Nintendo Switch - Linux - HTML5 - macOS - Windows Monetization - Ads - In-app purchases - Web Monetization Extensions - Advertising Info - Camera - Facebook - Facebook Instant Games - Firebase Analytics - Google Play Game Services - Google Play Instant - In-app purchases - Inter-app communication - Push notifications - App review and ratings - WebMonetization - WebSocket - Webview Native extensions - Introduction - Details - Defold SDK - Adding auto-complete definition - Best Practices - Debugging - Build Variants - Manifest Merging Editor extensions - Editor scripts
Read this manual in English
Model 组件
Defold 核心是3D的. 就算只有2D材质的话渲染也是3D的, 只不过是映射到平面屏幕上而已. Defold 允许在集合中使用 3D 资源, 或者称作 模型 . 你可以用Defold制作全3D的, 或者2D和3D混合的游戏.
创建 model 组件
Model 组件和其他游戏对象组件一样, 两种创建办法:
Assets 浏览器里 右键点击 选择 New... ▸ Model 创建 Model 文件 .
直接在 Outline 视图的游戏对象上 右键点击 然后选择 Add Component ▸ Model .
模型组件需要设置一些属性:
模型属性
除了常见的 Id , Position 和 Rotation 属性, 模型组件还有如下特有属性:
Mesh
这个属性指定 Collada .dae 文件的模型网格. 如果文件包含多组网格, 只读取第一个.
Material
添加合适的材质. 一开始可以使用默认的 model.material 材质.
Texture
指定适当的纹理.
Skeleton
指定 Collada .dae 文件里的骨骼. 注意Defold只支持一个骨骼树.
Animations
指定模型的 动画集文件 .
Default Animation
指定自动播放的默认动画 (从动画集之中) .
编辑时操作
有了模型组件就可以使用随意使用组件功能同时可以使用 Scene Editor 工具移动, 旋转和缩放模型游戏对象了.
运行时操作
有一套用于在运行时修改模型的方法和属性 (参见 API文档 ).
运行时动画
Defold 提供了强大的运行时动画控制方法:
local play_properties = { blend_duration = 0 . 1 }
spine . play_anim ( "#model" , "jump" , go . PLAYBACK_ONCE_FORWARD , play_properties )
可以手动播放动画甚至使用属性动画系统控制播放头:
-- 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 )
修改属性
使用 go.get()
和 go.set()
方法可以修改模型的属性:
animation
当前动画 (hash
) (只读). 使用 model.play_anim()
方法来更改播放动画 (见上文).
cursor
标准化动画头 (number
).
material
Spine模型材质 (hash
). 可使用 go.set()
修改. 参见 这个例子的 API 用法 .
playback_rate
动画播放速率 (number
).
textureN
模型材质. 其中 N 的范围是 0-7 (hash
). 可使用 go.set()
修改. 参见 这个例子的 API 用法 .
材质
3D 一半都有给网格赋予材质的功能, 比如颜色和贴图. 在 Collada .dae 文件输出时, 这些信息被写入文件. 应该基于游戏需要选择或者建造 高性能 材质. 材质由 着色器程序 及其若干相关属性组成.
在内置材质文件夹里有一个3D模型材质. 如果需要自定义材质, 请参考 材质文档 . 着色器教程 介绍了着色器程序的工作方式.
材质常量
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
3D网格颜色 (vector4
). 四元数 x, y, z, 和 w 分别对应红, 绿, 蓝和不透明度.
渲染
默认的渲染脚本是为2D游戏而不是3D模型制作的. 但是你可以把默认渲染脚本拷贝出来, 自己加几行代码就能用来渲染模型. 比如:
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
关于渲染脚本详情请见 Render documentation .