unit.lua 10.0 KB

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