unit.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. -- Copyright (c) 2012-2025 Daniele Bartolini et al.
  2. -- SPDX-License-Identifier: MIT
  3. require "core/lua/class"
  4. UnitUtils = UnitUtils or {}
  5. function UnitUtils.collect_children(scene_graph, unit_id, children)
  6. local transform = SceneGraph.instance(scene_graph, unit_id)
  7. if transform == nil then return end
  8. local cur_child = SceneGraph.first_child(scene_graph, transform)
  9. while cur_child ~= nil do
  10. local child_id = SceneGraph.owner(scene_graph, cur_child)
  11. UnitUtils.collect_children(scene_graph, child_id, children)
  12. cur_child = SceneGraph.next_sibling(scene_graph, cur_child)
  13. end
  14. table.insert(children, unit_id)
  15. end
  16. function UnitUtils.freeze(world, unit_id)
  17. local physics_world = World.physics_world(world)
  18. local scene_graph = World.scene_graph(world)
  19. local children = {}
  20. UnitUtils.collect_children(scene_graph, unit_id, children)
  21. for _, child_id in ipairs(children) do
  22. local actor = PhysicsWorld.actor_instance(physics_world, child_id)
  23. if actor ~= nil then
  24. PhysicsWorld.actor_set_kinematic(physics_world, actor, true)
  25. PhysicsWorld.actor_disable_collision(physics_world, actor)
  26. end
  27. end
  28. end
  29. function UnitUtils.destroy_tree(world, unit_id)
  30. local scene_graph = World.scene_graph(world)
  31. local children = {}
  32. UnitUtils.collect_children(scene_graph, unit_id, children)
  33. for _, child_id in ipairs(children) do
  34. World.destroy_unit(world, child_id)
  35. end
  36. end
  37. UnitBox = class(UnitBox)
  38. function UnitBox:init(world, id, unit_id, prefab)
  39. self._world = world
  40. self._rw = World.render_world(world)
  41. self._id = id
  42. self._unit_id = unit_id
  43. self._prefab = prefab
  44. self._sg = World.scene_graph(world)
  45. self._selected = false
  46. self._obb = { pose = Matrix4x4Box(), half_extents = Vector3Box(), dirty = true }
  47. self:freeze()
  48. end
  49. function UnitBox:freeze()
  50. UnitUtils.freeze(self._world, self._unit_id)
  51. end
  52. function UnitBox:id()
  53. return self._id
  54. end
  55. function UnitBox:prefab()
  56. return self._prefab
  57. end
  58. function UnitBox:unit_id()
  59. return self._unit_id
  60. end
  61. function UnitBox:destroy()
  62. UnitUtils.destroy_tree(self._world, self._unit_id)
  63. end
  64. function UnitBox:is_spatial()
  65. return SceneGraph.instance(self._sg, self._unit_id) ~= nil
  66. end
  67. function UnitBox:local_position()
  68. local tr = SceneGraph.instance(self._sg, self._unit_id)
  69. return tr and SceneGraph.local_position(self._sg, tr) or Vector3.zero()
  70. end
  71. function UnitBox:local_rotation()
  72. local tr = SceneGraph.instance(self._sg, self._unit_id)
  73. return tr and SceneGraph.local_rotation(self._sg, tr) or Quaternion.identity()
  74. end
  75. function UnitBox:local_scale()
  76. local tr = SceneGraph.instance(self._sg, self._unit_id)
  77. return tr and SceneGraph.local_scale(self._sg, tr) or Vector3(1, 1, 1)
  78. end
  79. function UnitBox:local_pose()
  80. local tr = SceneGraph.instance(self._sg, self._unit_id)
  81. return tr and SceneGraph.local_pose(self._sg, tr) or Matrix4x4.identity()
  82. end
  83. function UnitBox:world_position()
  84. local tr = SceneGraph.instance(self._sg, self._unit_id)
  85. return tr and SceneGraph.world_position(self._sg, tr) or Vector3.zero()
  86. end
  87. function UnitBox:world_rotation()
  88. local tr = SceneGraph.instance(self._sg, self._unit_id)
  89. return tr and SceneGraph.world_rotation(self._sg, tr) or Quaternion.identity()
  90. end
  91. function UnitBox:world_scale()
  92. return Vector3(1, 1, 1)
  93. end
  94. function UnitBox:world_pose()
  95. local tr = SceneGraph.instance(self._sg, self._unit_id)
  96. return tr and SceneGraph.world_pose(self._sg, tr) or Matrix4x4.identity()
  97. end
  98. function UnitBox:set_local_position(pos)
  99. local tr = SceneGraph.instance(self._sg, self._unit_id)
  100. if tr then
  101. SceneGraph.set_local_position(self._sg, tr, pos)
  102. self._obb.dirty = true
  103. end
  104. end
  105. function UnitBox:set_local_rotation(rot)
  106. local tr = SceneGraph.instance(self._sg, self._unit_id)
  107. if tr then
  108. SceneGraph.set_local_rotation(self._sg, tr, rot)
  109. self._obb.dirty = true
  110. end
  111. end
  112. function UnitBox:set_local_scale(scale)
  113. local tr = SceneGraph.instance(self._sg, self._unit_id)
  114. if tr then
  115. SceneGraph.set_local_scale(self._sg, tr, scale)
  116. self._obb.dirty = true
  117. end
  118. end
  119. function UnitBox:set_local_pose(pose)
  120. local tr = SceneGraph.instance(self._sg, self._unit_id)
  121. if tr then
  122. SceneGraph.set_local_pose(self._sg, tr, pose)
  123. self._obb.dirty = true
  124. end
  125. end
  126. function UnitBox:on_selected(selected)
  127. local scene_graph = self._sg
  128. local unit_id = self._unit_id
  129. local children = {}
  130. UnitUtils.collect_children(scene_graph, unit_id, children)
  131. for _, child_id in ipairs(children) do
  132. RenderWorld.selection(self._rw, child_id, selected);
  133. end
  134. self._selected = selected
  135. end
  136. function UnitBox:mesh_tree_obb()
  137. local scene_graph = self._sg
  138. local unit_id = self._unit_id
  139. local obb_tm = self:local_pose()
  140. local obb_he = Vector3(0.01, 0.01, 0.01)
  141. local children = {}
  142. UnitUtils.collect_children(scene_graph, unit_id, children)
  143. for _, child_id in ipairs(children) do
  144. local mesh = RenderWorld.mesh_instance(self._rw, child_id)
  145. if mesh then
  146. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, RenderWorld.mesh_obb(self._rw, mesh))
  147. end
  148. end
  149. return obb_tm, obb_he
  150. end
  151. function UnitBox:sprite_tree_obb()
  152. local scene_graph = self._sg
  153. local unit_id = self._unit_id
  154. local obb_tm = self:local_pose()
  155. local obb_he = Vector3(0.01, 0.01, 0.01)
  156. local children = {}
  157. UnitUtils.collect_children(scene_graph, unit_id, children)
  158. for _, child_id in ipairs(children) do
  159. local sprite = RenderWorld.sprite_instance(self._rw, child_id)
  160. if sprite then
  161. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, RenderWorld.sprite_obb(self._rw, sprite))
  162. end
  163. end
  164. return obb_tm, obb_he
  165. end
  166. -- Returns the Oriented Bounding-Box of the unit.
  167. function UnitBox:obb()
  168. if self._obb.dirty then
  169. local obb_tm, obb_he = self:mesh_tree_obb()
  170. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, self:sprite_tree_obb())
  171. self._obb.pose:store(obb_tm)
  172. self._obb.half_extents:store(obb_he)
  173. self._obb.dirty = false
  174. end
  175. return self._obb.pose:unbox(), self._obb.half_extents:unbox()
  176. end
  177. function UnitBox:raycast_mesh_tree(pos, dir)
  178. local scene_graph = self._sg
  179. local unit_id = self._unit_id
  180. local t_min = math.huge
  181. local children = {}
  182. UnitUtils.collect_children(scene_graph, unit_id, children)
  183. for _, child_id in ipairs(children) do
  184. local mesh = RenderWorld.mesh_instance(self._rw, child_id)
  185. if mesh then
  186. local t = RenderWorld.mesh_cast_ray(self._rw, mesh, pos, dir)
  187. if t ~= -1.0 then
  188. t_min = math.min(t, t_min)
  189. end
  190. end
  191. end
  192. return t_min == math.huge and -1.0 or t_min
  193. end
  194. function UnitBox:raycast_sprite_tree(pos, dir)
  195. local scene_graph = self._sg
  196. local unit_id = self._unit_id
  197. local t_min = math.huge
  198. local children = {}
  199. UnitUtils.collect_children(scene_graph, unit_id, children)
  200. for _, child_id in ipairs(children) do
  201. local sprite = RenderWorld.sprite_instance(self._rw, child_id)
  202. if sprite then
  203. local t = RenderWorld.sprite_cast_ray(self._rw, sprite, pos, dir)
  204. if t ~= -1.0 then
  205. t_min = math.min(t, t_min)
  206. end
  207. end
  208. end
  209. return t_min == math.huge and -1.0 or t_min
  210. end
  211. function UnitBox:raycast(pos, dir)
  212. local obb_tm, obb_he = self:obb()
  213. if Math.ray_obb_intersection(pos, dir, obb_tm, obb_he) == -1.0 then
  214. return -1.0
  215. end
  216. local t = self:raycast_mesh_tree(pos, dir)
  217. if t ~= -1.0 then
  218. return t
  219. end
  220. return self:raycast_sprite_tree(pos, dir)
  221. end
  222. function UnitBox:draw()
  223. if not self._selected then
  224. return
  225. end
  226. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  227. if light then
  228. RenderWorld.light_debug_draw(self._rw, light, LevelEditor._lines)
  229. end
  230. end
  231. function UnitBox:set_light(type, range, intensity, angle, color, bias, cast_shadows)
  232. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  233. if light then
  234. RenderWorld.light_set_type(self._rw, light, type)
  235. RenderWorld.light_set_color(self._rw, light, color)
  236. RenderWorld.light_set_range(self._rw, light, range)
  237. RenderWorld.light_set_intensity(self._rw, light, intensity)
  238. RenderWorld.light_set_spot_angle(self._rw, light, angle)
  239. RenderWorld.light_set_shadow_bias(self._rw, light, bias)
  240. RenderWorld.light_set_cast_shadows(self._rw, light, cast_shadows)
  241. end
  242. end
  243. function UnitBox:set_fog(color, density, range_min, range_max, sun_blend, enabled)
  244. local fog = RenderWorld.fog_instance(self._rw, self._unit_id)
  245. if fog then
  246. RenderWorld.fog_set_color(self._rw, fog, color)
  247. RenderWorld.fog_set_density(self._rw, fog, density)
  248. RenderWorld.fog_set_range_min(self._rw, fog, range_min)
  249. RenderWorld.fog_set_range_max(self._rw, fog, range_max)
  250. RenderWorld.fog_set_sun_blend(self._rw, fog, sun_blend)
  251. RenderWorld.fog_set_enabled(self._rw, fog, enabled)
  252. end
  253. end
  254. function UnitBox:set_global_lighting(skydome_map, skydome_intensity, ambient_color)
  255. local gl = RenderWorld.global_lighting_instance(self._rw, self._unit_id)
  256. if gl then
  257. RenderWorld.global_lighting_set_skydome_map(self._rw, skydome_map)
  258. RenderWorld.global_lighting_set_skydome_intensity(self._rw, skydome_intensity)
  259. RenderWorld.global_lighting_set_ambient_color(self._rw, ambient_color)
  260. end
  261. end
  262. function UnitBox:set_bloom(enabled, threshold, weight, intensity)
  263. local bloom = RenderWorld.bloom_instance(self._rw, self._unit_id)
  264. if bloom then
  265. RenderWorld.bloom_set_enabled(self._rw, enabled)
  266. RenderWorld.bloom_set_threshold(self._rw, threshold)
  267. RenderWorld.bloom_set_weight(self._rw, weight)
  268. RenderWorld.bloom_set_intensity(self._rw, intensity)
  269. end
  270. end
  271. function UnitBox:set_tonemap(type)
  272. local tonemap = RenderWorld.tonemap_instance(self._rw, self._unit_id)
  273. if tonemap then
  274. RenderWorld.tonemap_set_type(self._rw, type)
  275. end
  276. end
  277. function UnitBox:set_mesh(mesh_resource, geometry, material, visible, cast_shadows)
  278. local mesh = RenderWorld.mesh_instance(self._rw, self._unit_id)
  279. if mesh then
  280. RenderWorld.mesh_set_geometry(self._rw, mesh, mesh_resource, geometry)
  281. RenderWorld.mesh_set_material(self._rw, mesh, material)
  282. RenderWorld.mesh_set_visible(self._rw, mesh, visible)
  283. RenderWorld.mesh_set_cast_shadows(self._rw, mesh, cast_shadows)
  284. self._obb.dirty = true
  285. end
  286. end
  287. function UnitBox:set_sprite(sprite_resource_name, material, layer, depth, visible, flip_x, flip_y)
  288. local sprite = RenderWorld.sprite_instance(self._rw, self._unit_id)
  289. if sprite then
  290. RenderWorld.sprite_set_sprite(self._rw, sprite, sprite_resource_name)
  291. RenderWorld.sprite_set_material(self._rw, sprite, material)
  292. RenderWorld.sprite_set_layer(self._rw, sprite, layer)
  293. RenderWorld.sprite_set_depth(self._rw, sprite, depth)
  294. RenderWorld.sprite_set_visible(self._rw, sprite, visible)
  295. RenderWorld.sprite_flip_x(self._rw, sprite, flip_x)
  296. RenderWorld.sprite_flip_y(self._rw, sprite, flip_y)
  297. self._obb.dirty = true
  298. end
  299. end
  300. function UnitBox:set_camera(projection, fov, near_range, far_range)
  301. local camera = World.camera_instance(self._world, self._unit_id)
  302. if camera then
  303. World.camera_set_projection_type(self._world, camera, projection)
  304. World.camera_set_fov(self._world, camera, fov)
  305. World.camera_set_near_clip_distance(self._world, camera, near_range)
  306. World.camera_set_far_clip_distance(self._world, camera, far_range)
  307. end
  308. end
  309. function UnitBox:send()
  310. Device.console_send { type = "unit_spawned"
  311. , id = self._id
  312. , name = self:prefab()
  313. , position = self:local_position()
  314. , rotation = self:local_rotation()
  315. , scale = self:local_scale()
  316. }
  317. end