主题
高级特性:创建可变形组件
在蛋仔编辑器的基础组件中,可以创建可变形的组件。例如带倒角的方块,可定制边数和内外径的多边形环(可变形圆环),以及自定义曲线/曲面。这些组件通过参数化的配置,可以实现丰富的定制效果。 您可以将编辑好的可变形组件,保存为组件预设,接着在游戏运行时,通过GameAPI.create_obstacle()来创建。但是,这种方式创建的组件,其参数已经在编辑时确定,无法动态指定。
蛋仔在Lua中提供了更加强大的运行时创建可变形组件的API,您现在可以在游戏运行时,动态地创建可变形组件。例如,您可以根据玩家在游戏中的操作/轨迹来创建自定义曲面,实现更有趣的地图交互。
创建可变形组件分两个步骤:
- 注册可变形组件的几何体形状
- 创建可变形组件
第一步,调用GameAPI.register_geometry_*()系列API来注册可变形组件的几何体形状。这一步传入可变形组件的参数,并生成对应的几何体形状“预设”,并不实际创建组件。
以创建自定义曲面/曲线为例:
lua
local Vector3 = math.Vector3
local positions = {
Vector3(0, 0, 0),
Vector3(10, 0, 10),
Vector3(15, 0, 5),
Vector3(15, 0, -5),
Vector3(12.5, 0, 3),
Vector3(10, 0, 0),
Vector3(12.5, 0, -3),
Vector3(10, 0, -10),
}
local normals = {
Vector3(0, 1, 0),
Vector3(0, 0, -1),
Vector3(-1, 0, 0),
Vector3(0, 0, 1),
Vector3(0, 0, 1),
Vector3(-1, 0, 0),
Vector3(-1, 1, 0),
Vector3(0, 1, 0),
}
local radius = {
1, 2, 5, 2, 2, 5, 1, 2
}
local path = GameAPI.register_geometry_spline(false, positions, normals, radius, 0.2, 0.05, 0.5, {})
local path2 = GameAPI.register_geometry_spline(true, positions, normals, radius, 0.2, 0.05, 0.5, {})GameAPI.register_geometry_spline接收七个参数,分别为类型(true为曲线,false为曲面)、位置列表、法线列表、半径列表、距离精度、角度精度、厚度(仅对于曲面)、附加参数(暂时无用)。
这个API返回一个字符串形式的ID,用来表示这个几何体。这里我们以同样的顶点配置,分别创建一个曲线,一个曲面。
第二步,调用GameAPI.create_obstacle_from_geometry()来用刚刚得到的几何体创建可变形的组件:
lua
for i = 1, 2 do
GameAPI.create_obstacle_from_geometry(1073819754, Vector3(-30 * i + 30, 15, 5), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, i % 2 == 0 and path or path2)
endGameAPI.create_obstacle_from_geometry()接收六个参数,分别为参考预设ID、位置、旋转、缩放、所有者和前一步得到的几何体ID。
由于几何体本身仅仅是一个形状描述,并没有指定皮肤、物理配置等属性,因此在创建组件时,需要传入一个参考预设ID,除了几何形状和物理形状以外,其他的属性都将从这个预设中继承。
上面的示例代码中,1073819754是演示用的预设ID,请替换为你自己的预设ID。
除了自定义曲面以外,目前蛋仔还支持了创建可变形圆环、圆台、方块,每种类型所支持的参数各不相同,请参照API文档。
lua
-- splines
do
local Vector3 = math.Vector3
local positions = {
Vector3(0, 0, 0),
Vector3(10, 0, 10),
Vector3(15, 0, 5),
Vector3(15, 0, -5),
Vector3(12.5, 0, 3),
Vector3(10, 0, 0),
Vector3(12.5, 0, -3),
Vector3(10, 0, -10),
}
local normals = {
Vector3(0, 1, 0),
Vector3(0, 0, -1),
Vector3(-1, 0, 0),
Vector3(0, 0, 1),
Vector3(0, 0, 1),
Vector3(-1, 0, 0),
Vector3(-1, 1, 0),
Vector3(0, 1, 0),
}
local radius = {
1, 2, 5, 2, 2, 5, 1, 2
}
local path = GameAPI.register_geometry_spline(false, positions, normals, radius, 0.2, 0.05, 0.5, {})
local path2 = GameAPI.register_geometry_spline(true, positions, normals, radius, 0.2, 0.05, 0.5, {})
-- 注意,这里预设ID(第一个参数)请替换为你自己的预设ID,下同
for i = 1, 2 do
GameAPI.create_obstacle_from_geometry(i % 2 == 0 and 1073819754 or 1073815637, Vector3(-30 * i + 30, 15, 5), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, i % 2 == 0 and path or path2)
end
end
-- ring
do
local path = GameAPI.register_geometry_ring(2.0, 3.2, 4.0, 5, 8, 0.25, 270.0, {})
GameAPI.create_obstacle_from_geometry(1073811526, Vector3(-4, 20, 15), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, path)
end
-- frustum
do
local path = GameAPI.register_geometry_frustum(6.0, 3.2, 4.0, 8, 10, 0.25, 225.0, 8, 0.25, {})
GameAPI.create_obstacle_from_geometry(1073811526, Vector3(-24, 20, 15), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, path)
end
-- box
do
local path = GameAPI.register_geometry_box(3.5, 0.5, 2, false, {})
GameAPI.create_obstacle_from_geometry(1073807375, Vector3(-14, 25, 15), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, path)
end
-- box还支持在生成时用平面对模型进行切分
do
-- 在postSplitPlanes可选参数内,每10个定点数描述一个平面,其中前三个元素为切分平面的朝向,第四个元素为切分平面的截距;
-- 其余六个参数为截面UV的旋转和偏移,默认情况下保持如下设置即可。
-- 切角立方体
local path = GameAPI.register_geometry_box(3.5, 0.5, 2, false, { postSplitPlanes = { { 1.0, -1.0, 1.0, -4.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0 } } })
GameAPI.create_obstacle_from_geometry(1073807375, Vector3(-14, 25, 15), Quaternion(0.0, 1.57, 0.0), Vector3(1, 1, 1), nil, path)
end
