-
Notifications
You must be signed in to change notification settings - Fork 1
/
d2c.lua
186 lines (159 loc) · 4.05 KB
/
d2c.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
local xml2lua = require("dae2collision.xml2lua.xml2lua")
local xmltree = require("dae2collision.xml2lua.tree")
local M = {}
-- Split a string into an array by spaces
local function split(s)
local t = {}
for w in s:gmatch("[^%s]+") do
table.insert(t, w)
end
return t
end
local function find_by_attribute(array, attr_name, value)
for i,v in ipairs(array) do
if v._attr[attr_name] == value then
return v
end
end
end
-- We don't have vmath.* when running from an editor script
local function vector3(x, y, z) return {x = x, y = y, z = z} end
local function build_verts(positions, up_axis)
local verts = {}
for i = 1,#positions,3 do
if up_axis == "Z_UP" then
-- x, z, -y
table.insert(verts, vector3(positions[i+0], positions[i+2], -positions[i+1]))
elseif up_axis == "X_UP" then
-- -y, x, z
table.insert(verts, vector3(-positions[i+1], positions[i+0], positions[i+2]))
else
-- x, y, z
table.insert(verts, vector3(positions[i+0], positions[i+1], positions[i+2]))
end
end
return verts
end
local function build_tris(p)
local tris = {}
for i = 1,#p,9 do
table.insert(tris, {p[i]+1, p[i+3]+1, p[i+6]+1})
end
return tris
end
local function build_convexshape(verts)
local out = "shape_type: TYPE_HULL"
for _,vert in ipairs(verts) do
out = out .. ("\ndata: %f data: %f data: %f"):format(vert.x, vert.y, vert.z)
end
return out
end
local function parse_xml(s)
local handler = xmltree:new()
local parser = xml2lua.parser(handler)
parser:parse(s)
return handler
end
function M.shapes_from_string(s)
local xml = parse_xml(s)
local geometries = xml.root.COLLADA.library_geometries.geometry
local up_axis = xml.root.COLLADA.asset.up_axis
-- Make sure geometries is an array
if geometries[1] == nil then
geometries = {geometries}
end
local shapes = {}
for i,geometry in ipairs(geometries) do
local mesh = geometry.mesh
if mesh.triangles then
local positions_id = mesh.vertices.input._attr.source:sub(2)
-- An array of vertex positions in the format: {x1, y1, z1, x2, y2, z2, ...}
local positions = split(find_by_attribute(mesh.source, "id", positions_id).float_array[1])
local verts = build_verts(positions, up_axis)
table.insert(shapes, build_convexshape(verts))
else
print("No triangles found for " .. geometry._attr.name .. ", skipping...")
end
end
return shapes
end
-- Editor script helpers --
local function read(path)
local file = assert(io.open(path, "r"))
local data = file:read("*a")
file:close()
return data
end
local function write(path, data)
local file = assert(io.open(path, "w"))
file:write(data)
file:close()
end
--[[
`properties` example:
{
type = "COLLISION_OBJECT_TYPE_STATIC",
mass = 1,
friction = 0.1,
restitution = 0.5,
linear_damping = 0,
angular_damping = 0,
locked_rotation = false,
bullet = false,
group = "world",
mask = "player,enemies"
}
]]--
function M.generate_collision(dae_path, properties, convexshape_path_format)
local shapes = M.shapes_from_string(read(dae_path))
local digits = #tostring(#shapes)
local go = ""
local shape_paths = {}
for i,shape in ipairs(shapes) do
-- Write convex shape
local shape_path = convexshape_path_format:format(("%0" .. digits .. "i"):format(i))
shape_paths[shape_path] = shape
-- Add collision object to game object
go = go .. ([[embedded_components {
id: "collisionobject]] .. i .. [["
type: "collisionobject"
data: "collision_shape: \"/]] .. shape_path .. [[\"\n"
"type: %s\n"
"mass: %f\n"
"friction: %f\n"
"restitution: %f\n"
"group: \"%s\"\n"
"mask: \"%s\"\n"
"linear_damping: %f\n"
"angular_damping: %f\n"
"locked_rotation: %s\n"
"bullet: %s\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
}
]]):format(
properties.type,
properties.mass,
properties.friction,
properties.restitution,
properties.group,
properties.mask,
properties.linear_damping,
properties.angular_damping,
properties.locked_rotation and "true" or "false",
properties.bullet and "true" or "false"
)
end
return go, shape_paths
end
return M