unit.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. UnitUtils.freeze(world, unit_id)
  48. end
  49. function UnitBox:id()
  50. return self._id
  51. end
  52. function UnitBox:prefab()
  53. return self._prefab
  54. end
  55. function UnitBox:unit_id()
  56. return self._unit_id
  57. end
  58. function UnitBox:destroy()
  59. UnitUtils.destroy_tree(self._world, self._unit_id)
  60. end
  61. function UnitBox:local_position()
  62. local tr = SceneGraph.instance(self._sg, self._unit_id)
  63. return tr and SceneGraph.local_position(self._sg, tr) or Vector3.zero()
  64. end
  65. function UnitBox:local_rotation()
  66. local tr = SceneGraph.instance(self._sg, self._unit_id)
  67. return tr and SceneGraph.local_rotation(self._sg, tr) or Quaternion.identity()
  68. end
  69. function UnitBox:local_scale()
  70. local tr = SceneGraph.instance(self._sg, self._unit_id)
  71. return tr and SceneGraph.local_scale(self._sg, tr) or Vector3(1, 1, 1)
  72. end
  73. function UnitBox:local_pose()
  74. local tr = SceneGraph.instance(self._sg, self._unit_id)
  75. return tr and SceneGraph.local_pose(self._sg, tr) or Matrix4x4.identity()
  76. end
  77. function UnitBox:world_position()
  78. local tr = SceneGraph.instance(self._sg, self._unit_id)
  79. return tr and SceneGraph.world_position(self._sg, tr) or Vector3.zero()
  80. end
  81. function UnitBox:world_rotation()
  82. local tr = SceneGraph.instance(self._sg, self._unit_id)
  83. return tr and SceneGraph.world_rotation(self._sg, tr) or Quaternion.identity()
  84. end
  85. function UnitBox:world_scale()
  86. return Vector3(1, 1, 1)
  87. end
  88. function UnitBox:world_pose()
  89. local tr = SceneGraph.instance(self._sg, self._unit_id)
  90. return tr and SceneGraph.world_pose(self._sg, tr) or Matrix4x4.identity()
  91. end
  92. function UnitBox:set_local_position(pos)
  93. local tr = SceneGraph.instance(self._sg, self._unit_id)
  94. if tr then
  95. SceneGraph.set_local_position(self._sg, tr, pos)
  96. self._obb.dirty = true
  97. end
  98. end
  99. function UnitBox:set_local_rotation(rot)
  100. local tr = SceneGraph.instance(self._sg, self._unit_id)
  101. if tr then
  102. SceneGraph.set_local_rotation(self._sg, tr, rot)
  103. self._obb.dirty = true
  104. end
  105. end
  106. function UnitBox:set_local_scale(scale)
  107. local tr = SceneGraph.instance(self._sg, self._unit_id)
  108. if tr then
  109. SceneGraph.set_local_scale(self._sg, tr, scale)
  110. self._obb.dirty = true
  111. end
  112. end
  113. function UnitBox:set_local_pose(pose)
  114. local tr = SceneGraph.instance(self._sg, self._unit_id)
  115. if tr then
  116. SceneGraph.set_local_pose(self._sg, tr, pose)
  117. self._obb.dirty = true
  118. end
  119. end
  120. function UnitBox:on_selected(selected)
  121. local scene_graph = self._sg
  122. local unit_id = self._unit_id
  123. local children = {}
  124. UnitUtils.collect_children(scene_graph, unit_id, children)
  125. for _, child_id in ipairs(children) do
  126. RenderWorld.selection(self._rw, child_id, selected);
  127. end
  128. self._selected = selected
  129. end
  130. function UnitBox:mesh_tree_obb()
  131. local scene_graph = self._sg
  132. local unit_id = self._unit_id
  133. local obb_tm = self:local_pose()
  134. local obb_he = Vector3(0.01, 0.01, 0.01)
  135. local children = {}
  136. UnitUtils.collect_children(scene_graph, unit_id, children)
  137. for _, child_id in ipairs(children) do
  138. local mesh = RenderWorld.mesh_instance(self._rw, child_id)
  139. if mesh then
  140. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, RenderWorld.mesh_obb(self._rw, mesh))
  141. end
  142. end
  143. return obb_tm, obb_he
  144. end
  145. function UnitBox:sprite_tree_obb()
  146. local scene_graph = self._sg
  147. local unit_id = self._unit_id
  148. local obb_tm = self:local_pose()
  149. local obb_he = Vector3(0.01, 0.01, 0.01)
  150. local children = {}
  151. UnitUtils.collect_children(scene_graph, unit_id, children)
  152. for _, child_id in ipairs(children) do
  153. local sprite = RenderWorld.sprite_instance(self._rw, child_id)
  154. if sprite then
  155. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, RenderWorld.sprite_obb(self._rw, sprite))
  156. end
  157. end
  158. return obb_tm, obb_he
  159. end
  160. -- Returns the Oriented Bounding-Box of the unit.
  161. function UnitBox:obb()
  162. if self._obb.dirty then
  163. local obb_tm, obb_he = self:mesh_tree_obb()
  164. obb_tm, obb_he = Math.obb_merge(obb_tm, obb_he, self:sprite_tree_obb())
  165. self._obb.pose:store(obb_tm)
  166. self._obb.half_extents:store(obb_he)
  167. self._obb.dirty = false
  168. end
  169. return self._obb.pose:unbox(), self._obb.half_extents:unbox()
  170. end
  171. function UnitBox:raycast_mesh_tree(pos, dir)
  172. local scene_graph = self._sg
  173. local unit_id = self._unit_id
  174. local t_min = math.huge
  175. local children = {}
  176. UnitUtils.collect_children(scene_graph, unit_id, children)
  177. for _, child_id in ipairs(children) do
  178. local mesh = RenderWorld.mesh_instance(self._rw, child_id)
  179. if mesh then
  180. local t = RenderWorld.mesh_cast_ray(self._rw, mesh, pos, dir)
  181. if t ~= -1.0 then
  182. t_min = math.min(t, t_min)
  183. end
  184. end
  185. end
  186. return t_min == math.huge and -1.0 or t_min
  187. end
  188. function UnitBox:raycast_sprite_tree(pos, dir)
  189. local scene_graph = self._sg
  190. local unit_id = self._unit_id
  191. local t_min = math.huge
  192. local children = {}
  193. UnitUtils.collect_children(scene_graph, unit_id, children)
  194. for _, child_id in ipairs(children) do
  195. local sprite = RenderWorld.sprite_instance(self._rw, child_id)
  196. if sprite then
  197. local t = RenderWorld.sprite_cast_ray(self._rw, sprite, pos, dir)
  198. if t ~= -1.0 then
  199. t_min = math.min(t, t_min)
  200. end
  201. end
  202. end
  203. return t_min == math.huge and -1.0 or t_min
  204. end
  205. function UnitBox:raycast(pos, dir)
  206. local obb_tm, obb_he = self:obb()
  207. if Math.ray_obb_intersection(pos, dir, obb_tm, obb_he) == -1.0 then
  208. return -1.0
  209. end
  210. local t = self:raycast_mesh_tree(pos, dir)
  211. if t ~= -1.0 then
  212. return t
  213. end
  214. return self:raycast_sprite_tree(pos, dir)
  215. end
  216. function UnitBox:draw()
  217. if not self._selected then
  218. return
  219. end
  220. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  221. if light then
  222. RenderWorld.light_debug_draw(self._rw, light, LevelEditor._lines)
  223. end
  224. end
  225. function UnitBox:set_light(type, range, intensity, angle, color, bias, cast_shadows)
  226. local light = RenderWorld.light_instance(self._rw, self._unit_id)
  227. if light then
  228. RenderWorld.light_set_type(self._rw, light, type)
  229. RenderWorld.light_set_color(self._rw, light, color)
  230. RenderWorld.light_set_range(self._rw, light, range)
  231. RenderWorld.light_set_intensity(self._rw, light, intensity)
  232. RenderWorld.light_set_spot_angle(self._rw, light, angle)
  233. RenderWorld.light_set_shadow_bias(self._rw, light, bias)
  234. RenderWorld.light_set_cast_shadows(self._rw, light, cast_shadows)
  235. end
  236. end
  237. function UnitBox:set_mesh(mesh_resource, geometry, material, visible, cast_shadows)
  238. local mesh = RenderWorld.mesh_instance(self._rw, self._unit_id)
  239. if mesh then
  240. RenderWorld.mesh_set_geometry(self._rw, mesh, mesh_resource, geometry)
  241. RenderWorld.mesh_set_material(self._rw, mesh, material)
  242. RenderWorld.mesh_set_visible(self._rw, mesh, visible)
  243. RenderWorld.mesh_set_cast_shadows(self._rw, mesh, cast_shadows)
  244. self._obb.dirty = true
  245. end
  246. end
  247. function UnitBox:set_sprite(sprite_resource_name, material, layer, depth, visible)
  248. local sprite = RenderWorld.sprite_instance(self._rw, self._unit_id)
  249. if sprite then
  250. RenderWorld.sprite_set_sprite(self._rw, sprite, sprite_resource_name)
  251. RenderWorld.sprite_set_material(self._rw, sprite, material)
  252. RenderWorld.sprite_set_layer(self._rw, sprite, layer)
  253. RenderWorld.sprite_set_depth(self._rw, sprite, depth)
  254. RenderWorld.sprite_set_visible(self._rw, sprite, visible)
  255. self._obb.dirty = true
  256. end
  257. end
  258. function UnitBox:set_camera(projection, fov, near_range, far_range)
  259. local camera = World.camera_instance(self._world, self._unit_id)
  260. if camera then
  261. World.camera_set_projection_type(self._world, camera, projection)
  262. World.camera_set_fov(self._world, camera, fov)
  263. World.camera_set_near_clip_distance(self._world, camera, near_range)
  264. World.camera_set_far_clip_distance(self._world, camera, far_range)
  265. end
  266. end
  267. function UnitBox:send()
  268. Device.console_send { type = "unit_spawned"
  269. , id = self._id
  270. , name = self:prefab()
  271. , position = self:local_position()
  272. , rotation = self:local_rotation()
  273. , scale = self:local_scale()
  274. }
  275. end