world.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright (c) 2012-2022 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/containers/hash_map.inl"
  6. #include "core/error/error.h"
  7. #include "core/list.inl"
  8. #include "core/math/matrix4x4.inl"
  9. #include "core/math/vector3.inl"
  10. #include "core/math/vector4.inl"
  11. #include "core/memory/temp_allocator.inl"
  12. #include "core/strings/string_id.inl"
  13. #include "lua/lua_environment.h"
  14. #include "resource/resource_manager.h"
  15. #include "resource/unit_resource.h"
  16. #include "world/animation_state_machine.h"
  17. #include "world/debug_line.h"
  18. #include "world/event_stream.inl"
  19. #include "world/gui.h"
  20. #include "world/level.h"
  21. #include "world/physics_world.h"
  22. #include "world/render_world.h"
  23. #include "world/scene_graph.h"
  24. #include "world/script_world.h"
  25. #include "world/sound_world.h"
  26. #include "world/unit_manager.h"
  27. #include "world/world.h"
  28. #include <bgfx/bgfx.h>
  29. #include <bx/math.h>
  30. namespace crown
  31. {
  32. World::World(Allocator& a, ResourceManager& rm, ShaderManager& sm, MaterialManager& mm, UnitManager& um, LuaEnvironment& env)
  33. : _marker(WORLD_MARKER)
  34. , _allocator(&a)
  35. , _resource_manager(&rm)
  36. , _shader_manager(&sm)
  37. , _material_manager(&mm)
  38. , _lua_environment(&env)
  39. , _unit_manager(&um)
  40. , _lines(NULL)
  41. , _scene_graph(NULL)
  42. , _render_world(NULL)
  43. , _physics_world(NULL)
  44. , _sound_world(NULL)
  45. , _animation_state_machine(NULL)
  46. , _units(a)
  47. , _camera(a)
  48. , _camera_map(a)
  49. , _events(a)
  50. , _gui_buffer(sm)
  51. {
  52. _lines = create_debug_line(true);
  53. _scene_graph = CE_NEW(*_allocator, SceneGraph)(*_allocator, um);
  54. _render_world = CE_NEW(*_allocator, RenderWorld)(*_allocator, rm, sm, mm, um);
  55. _physics_world = CE_NEW(*_allocator, PhysicsWorld)(*_allocator, rm, um, *_lines);
  56. _sound_world = CE_NEW(*_allocator, SoundWorld)(*_allocator);
  57. _script_world = CE_NEW(*_allocator, ScriptWorld)(*_allocator, um, rm, env, *this);
  58. _animation_state_machine = CE_NEW(*_allocator, AnimationStateMachine)(*_allocator, rm, um);
  59. _gui_buffer.create();
  60. list::init_head(_guis);
  61. list::init_head(_levels);
  62. _node.next = NULL;
  63. _node.prev = NULL;
  64. }
  65. World::~World()
  66. {
  67. // Destroy loaded levels
  68. ListNode* cur;
  69. ListNode* tmp;
  70. list_for_each_safe(cur, tmp, &_levels)
  71. {
  72. Level* level = (Level*)container_of(cur, Level, _node);
  73. CE_DELETE(*_allocator, level);
  74. }
  75. // Destroy units
  76. for (u32 i = 0; i < array::size(_units); ++i)
  77. _unit_manager->destroy(_units[i]);
  78. // Destroy subsystems
  79. CE_DELETE(*_allocator, _animation_state_machine);
  80. CE_DELETE(*_allocator, _script_world);
  81. CE_DELETE(*_allocator, _sound_world);
  82. CE_DELETE(*_allocator, _physics_world);
  83. CE_DELETE(*_allocator, _render_world);
  84. CE_DELETE(*_allocator, _scene_graph);
  85. destroy_debug_line(*_lines);
  86. _marker = 0;
  87. }
  88. UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot, const Vector3& scl)
  89. {
  90. const UnitResource* ur = (const UnitResource*)_resource_manager->get(RESOURCE_TYPE_UNIT, name);
  91. UnitId* unit_lookup = (UnitId*)default_scratch_allocator().allocate(sizeof(*unit_lookup) * ur->num_units);
  92. for (u32 i = 0; i < ur->num_units; ++i)
  93. unit_lookup[i] = _unit_manager->create();
  94. spawn_units(*this, ur, pos, rot, scl, unit_lookup);
  95. UnitId root_unit = unit_lookup[0];
  96. default_scratch_allocator().deallocate(unit_lookup);
  97. return root_unit;
  98. }
  99. UnitId World::spawn_empty_unit()
  100. {
  101. UnitId unit = _unit_manager->create();
  102. array::push_back(_units, unit);
  103. post_unit_spawned_event(unit);
  104. return unit;
  105. }
  106. void World::destroy_unit(UnitId unit)
  107. {
  108. _unit_manager->destroy(unit);
  109. for (u32 i = 0, n = array::size(_units); i < n; ++i)
  110. {
  111. if (_units[i] == unit)
  112. {
  113. _units[i] = _units[n - 1];
  114. array::pop_back(_units);
  115. break;
  116. }
  117. }
  118. post_unit_destroyed_event(unit);
  119. }
  120. u32 World::num_units() const
  121. {
  122. return array::size(_units);
  123. }
  124. void World::units(Array<UnitId>& units) const
  125. {
  126. array::reserve(units, array::size(_units));
  127. array::push(units, array::begin(_units), array::size(_units));
  128. }
  129. UnitId World::unit_by_name(StringId32 name)
  130. {
  131. ListNode* cur;
  132. list_for_each(cur, &_levels)
  133. {
  134. Level* level = (Level*)container_of(cur, Level, _node);
  135. UnitId unit = level->unit_by_name(name);
  136. if (unit != UNIT_INVALID)
  137. return unit;
  138. }
  139. return UNIT_INVALID;
  140. }
  141. void World::update_animations(f32 dt)
  142. {
  143. _animation_state_machine->update(dt);
  144. }
  145. void World::update_scene(f32 dt)
  146. {
  147. // Process animation events
  148. {
  149. EventStream& events = _animation_state_machine->_events;
  150. const u32 size = array::size(events);
  151. u32 read = 0;
  152. while (read < size)
  153. {
  154. const EventHeader* eh = (EventHeader*)&events[read];
  155. const char* data = (char*)&eh[1];
  156. read += sizeof(*eh) + eh->size;
  157. switch (eh->type)
  158. {
  159. case 0:
  160. {
  161. const SpriteFrameChangeEvent& ptev = *(SpriteFrameChangeEvent*)data;
  162. const SpriteInstance si = _render_world->sprite_instance(ptev.unit);
  163. _render_world->sprite_set_frame(si, ptev.frame_num);
  164. break;
  165. }
  166. default:
  167. CE_FATAL("Unknown event type");
  168. break;
  169. }
  170. }
  171. array::clear(events);
  172. }
  173. TempAllocator4096 ta;
  174. Array<UnitId> changed_units(ta);
  175. Array<Matrix4x4> changed_world(ta);
  176. _scene_graph->get_changed(changed_units, changed_world);
  177. _physics_world->update_actor_world_poses(array::begin(changed_units)
  178. , array::end(changed_units)
  179. , array::begin(changed_world)
  180. );
  181. _physics_world->update(dt);
  182. // Process physics events
  183. {
  184. EventStream& events = _physics_world->events();
  185. const u32 size = array::size(events);
  186. u32 read = 0;
  187. while (read < size)
  188. {
  189. const EventHeader* eh = (EventHeader*)&events[read];
  190. const char* data = (char*)&eh[1];
  191. read += sizeof(*eh) + eh->size;
  192. switch (eh->type)
  193. {
  194. case EventType::PHYSICS_TRANSFORM:
  195. {
  196. const PhysicsTransformEvent& ptev = *(PhysicsTransformEvent*)data;
  197. const TransformInstance ti = _scene_graph->instance(ptev.unit_id);
  198. if (is_valid(ti)) // User code may have destroyed the actor
  199. _scene_graph->set_world_pose_and_rescale(ti, ptev.world);
  200. break;
  201. }
  202. case EventType::PHYSICS_COLLISION:
  203. {
  204. const PhysicsCollisionEvent& pcev = *(PhysicsCollisionEvent*)data;
  205. script_world::collision(*_script_world, pcev);
  206. break;
  207. }
  208. case EventType::PHYSICS_TRIGGER:
  209. break;
  210. default:
  211. CE_FATAL("Unknown event type");
  212. break;
  213. }
  214. }
  215. array::clear(events);
  216. }
  217. array::clear(changed_units);
  218. array::clear(changed_world);
  219. _scene_graph->get_changed(changed_units, changed_world);
  220. _scene_graph->clear_changed();
  221. _render_world->update_transforms(array::begin(changed_units)
  222. , array::end(changed_units)
  223. , array::begin(changed_world)
  224. );
  225. _sound_world->update();
  226. _gui_buffer.reset();
  227. array::clear(_events);
  228. script_world::update(*_script_world, dt);
  229. }
  230. void World::update(f32 dt)
  231. {
  232. update_animations(dt);
  233. update_scene(dt);
  234. }
  235. void World::render(const Matrix4x4& view)
  236. {
  237. _render_world->render(view);
  238. _physics_world->debug_draw();
  239. _render_world->debug_draw(*_lines);
  240. _lines->submit();
  241. _lines->reset();
  242. }
  243. CameraInstance World::camera_create(UnitId unit, const CameraDesc& cd, const Matrix4x4& /*tr*/)
  244. {
  245. CE_ASSERT(!hash_map::has(_camera_map, unit), "Unit already has a camera component");
  246. Camera camera;
  247. camera.unit = unit;
  248. camera.projection_type = (ProjectionType::Enum)cd.type;
  249. camera.fov = cd.fov;
  250. camera.near_range = cd.near_range;
  251. camera.far_range = cd.far_range;
  252. const u32 last = array::size(_camera);
  253. array::push_back(_camera, camera);
  254. hash_map::set(_camera_map, unit, last);
  255. return camera_make_instance(last);
  256. }
  257. void World::camera_destroy(CameraInstance camera)
  258. {
  259. const u32 last = array::size(_camera) - 1;
  260. const UnitId u = _camera[camera.i].unit;
  261. const UnitId last_u = _camera[last].unit;
  262. _camera[camera.i] = _camera[last];
  263. hash_map::set(_camera_map, last_u, camera.i);
  264. hash_map::remove(_camera_map, u);
  265. }
  266. CameraInstance World::camera_instance(UnitId unit)
  267. {
  268. return camera_make_instance(hash_map::get(_camera_map, unit, UINT32_MAX));
  269. }
  270. void World::camera_set_projection_type(CameraInstance camera, ProjectionType::Enum type)
  271. {
  272. _camera[camera.i].projection_type = type;
  273. }
  274. ProjectionType::Enum World::camera_projection_type(CameraInstance camera)
  275. {
  276. return _camera[camera.i].projection_type;
  277. }
  278. Matrix4x4 World::camera_projection_matrix(CameraInstance camera)
  279. {
  280. Camera& cam = _camera[camera.i];
  281. const bgfx::Caps* caps = bgfx::getCaps();
  282. f32 bx_proj[16];
  283. switch (cam.projection_type)
  284. {
  285. case ProjectionType::ORTHOGRAPHIC:
  286. bx::mtxOrtho(bx_proj
  287. , -cam.half_size * cam.aspect
  288. , cam.half_size * cam.aspect
  289. , -cam.half_size
  290. , cam.half_size
  291. , cam.near_range
  292. , cam.far_range
  293. , 0.0f
  294. , caps->homogeneousDepth
  295. );
  296. break;
  297. case ProjectionType::PERSPECTIVE:
  298. bx::mtxProj(bx_proj
  299. , fdeg(cam.fov)
  300. , cam.aspect
  301. , cam.near_range
  302. , cam.far_range
  303. , caps->homogeneousDepth
  304. );
  305. break;
  306. default:
  307. CE_FATAL("Unknown projection type");
  308. break;
  309. }
  310. return from_array(bx_proj);
  311. }
  312. Matrix4x4 World::camera_view_matrix(CameraInstance camera)
  313. {
  314. TransformInstance ti = _scene_graph->instance(_camera[camera.i].unit);
  315. Matrix4x4 view = _scene_graph->world_pose(ti);
  316. invert(view);
  317. return view;
  318. }
  319. f32 World::camera_fov(CameraInstance camera)
  320. {
  321. return _camera[camera.i].fov;
  322. }
  323. void World::camera_set_fov(CameraInstance camera, f32 fov)
  324. {
  325. _camera[camera.i].fov = fov;
  326. }
  327. void World::camera_set_aspect(CameraInstance camera, f32 aspect)
  328. {
  329. _camera[camera.i].aspect = aspect;
  330. }
  331. f32 World::camera_near_clip_distance(CameraInstance camera)
  332. {
  333. return _camera[camera.i].near_range;
  334. }
  335. void World::camera_set_near_clip_distance(CameraInstance camera, f32 near)
  336. {
  337. _camera[camera.i].near_range = near;
  338. }
  339. f32 World::camera_far_clip_distance(CameraInstance camera)
  340. {
  341. return _camera[camera.i].far_range;
  342. }
  343. void World::camera_set_far_clip_distance(CameraInstance camera, f32 far)
  344. {
  345. _camera[camera.i].far_range = far;
  346. }
  347. void World::camera_set_orthographic_size(CameraInstance camera, f32 half_size)
  348. {
  349. _camera[camera.i].half_size = half_size;
  350. }
  351. void World::camera_set_viewport_metrics(CameraInstance camera, u16 x, u16 y, u16 width, u16 height)
  352. {
  353. _camera[camera.i].view_x = x;
  354. _camera[camera.i].view_y = y;
  355. _camera[camera.i].view_width = width;
  356. _camera[camera.i].view_height = height;
  357. }
  358. Vector3 World::camera_screen_to_world(CameraInstance camera, const Vector3& pos)
  359. {
  360. TransformInstance ti = _scene_graph->instance(_camera[camera.i].unit);
  361. Matrix4x4 projection = camera_projection_matrix(camera);
  362. Matrix4x4 world_inv = _scene_graph->world_pose(ti);
  363. invert(world_inv);
  364. Matrix4x4 mvp = world_inv * projection;
  365. invert(mvp);
  366. const bgfx::Caps* caps = bgfx::getCaps();
  367. Vector4 ndc;
  368. ndc.x = (2.0f * (pos.x - 0.0f)) / _camera[camera.i].view_width - 1.0f;
  369. ndc.y = (2.0f * (_camera[camera.i].view_height - pos.y)) / _camera[camera.i].view_height - 1.0f;
  370. ndc.z = caps->homogeneousDepth
  371. ? (2.0f * pos.z) - 1.0f
  372. : pos.z
  373. ;
  374. ndc.w = 1.0f;
  375. Vector4 tmp = ndc * mvp;
  376. tmp *= 1.0f / tmp.w;
  377. return vector3(tmp.x, tmp.y, tmp.z);
  378. }
  379. Vector3 World::camera_world_to_screen(CameraInstance camera, const Vector3& pos)
  380. {
  381. TransformInstance ti = _scene_graph->instance(_camera[camera.i].unit);
  382. Matrix4x4 projection = camera_projection_matrix(camera);
  383. Matrix4x4 world_inv = _scene_graph->world_pose(ti);
  384. invert(world_inv);
  385. Vector4 xyzw;
  386. xyzw.x = pos.x;
  387. xyzw.y = pos.y;
  388. xyzw.z = pos.z;
  389. xyzw.w = 1.0f;
  390. Vector4 clip = xyzw * (world_inv * projection);
  391. Vector4 ndc;
  392. ndc.x = clip.x / clip.w;
  393. ndc.y = clip.y / clip.w;
  394. Vector3 screen;
  395. screen.x = (_camera[camera.i].view_x + _camera[camera.i].view_width * (ndc.x + 1.0f)) / 2.0f;
  396. screen.y = (_camera[camera.i].view_y + _camera[camera.i].view_height * (1.0f - ndc.y)) / 2.0f;
  397. screen.z = 0.0f;
  398. return screen;
  399. }
  400. SoundInstanceId World::play_sound(const SoundResource& sr, const bool loop, const f32 volume, const Vector3& pos, const f32 range)
  401. {
  402. return _sound_world->play(sr, loop, volume, range, pos);
  403. }
  404. SoundInstanceId World::play_sound(StringId64 name, const bool loop, const f32 volume, const Vector3& pos, const f32 range)
  405. {
  406. const SoundResource* sr = (const SoundResource*)_resource_manager->get(RESOURCE_TYPE_SOUND, name);
  407. return play_sound(*sr, loop, volume, pos, range);
  408. }
  409. void World::stop_sound(SoundInstanceId id)
  410. {
  411. _sound_world->stop(id);
  412. }
  413. void World::link_sound(SoundInstanceId /*id*/, UnitId /*unit*/, s32 /*node*/)
  414. {
  415. CE_FATAL("Not implemented yet");
  416. }
  417. void World::set_listener_pose(const Matrix4x4& pose)
  418. {
  419. _sound_world->set_listener_pose(pose);
  420. }
  421. void World::set_sound_position(SoundInstanceId id, const Vector3& pos)
  422. {
  423. _sound_world->set_sound_positions(1, &id, &pos);
  424. }
  425. void World::set_sound_range(SoundInstanceId id, f32 range)
  426. {
  427. _sound_world->set_sound_ranges(1, &id, &range);
  428. }
  429. void World::set_sound_volume(SoundInstanceId id, f32 vol)
  430. {
  431. _sound_world->set_sound_volumes(1, &id, &vol);
  432. }
  433. DebugLine* World::create_debug_line(bool depth_test)
  434. {
  435. return CE_NEW(*_allocator, DebugLine)(*_shader_manager, depth_test);
  436. }
  437. void World::destroy_debug_line(DebugLine& line)
  438. {
  439. CE_DELETE(*_allocator, &line);
  440. }
  441. Gui* World::create_screen_gui()
  442. {
  443. Gui* gui = CE_NEW(*_allocator, Gui)(_gui_buffer, *_resource_manager
  444. , *_shader_manager
  445. , *_material_manager
  446. );
  447. list::add(gui->_node, _guis);
  448. return gui;
  449. }
  450. void World::destroy_gui(Gui& gui)
  451. {
  452. list::remove(gui._node);
  453. CE_DELETE(*_allocator, &gui);
  454. }
  455. Level* World::load_level(StringId64 name, const Vector3& pos, const Quaternion& rot)
  456. {
  457. const LevelResource* lr = (const LevelResource*)_resource_manager->get(RESOURCE_TYPE_LEVEL, name);
  458. Level* level = CE_NEW(*_allocator, Level)(*_allocator, *_unit_manager, *this, *lr);
  459. level->load(pos, rot);
  460. list::add(level->_node, _levels);
  461. post_level_loaded_event();
  462. return level;
  463. }
  464. void World::post_unit_spawned_event(UnitId unit)
  465. {
  466. UnitSpawnedEvent ev;
  467. ev.unit = unit;
  468. event_stream::write(_events, EventType::UNIT_SPAWNED, ev);
  469. }
  470. void World::post_unit_destroyed_event(UnitId unit)
  471. {
  472. UnitDestroyedEvent ev;
  473. ev.unit = unit;
  474. event_stream::write(_events, EventType::UNIT_DESTROYED, ev);
  475. }
  476. void World::post_level_loaded_event()
  477. {
  478. LevelLoadedEvent ev;
  479. event_stream::write(_events, EventType::LEVEL_LOADED, ev);
  480. }
  481. #if CROWN_DEBUG
  482. void World::disable_unit_callbacks()
  483. {
  484. _script_world->_disable_callbacks = true;
  485. }
  486. #else
  487. void World::disable_unit_callbacks()
  488. {
  489. }
  490. #endif
  491. void spawn_units(World& w, const UnitResource* ur, const Vector3& pos, const Quaternion& rot, const Vector3& scl, const UnitId* unit_lookup)
  492. {
  493. SceneGraph* scene_graph = w._scene_graph;
  494. RenderWorld* render_world = w._render_world;
  495. PhysicsWorld* physics_world = w._physics_world;
  496. ScriptWorld* script_world = w._script_world;
  497. AnimationStateMachine* animation_state_machine = w._animation_state_machine;
  498. const u32* unit_parents = unit_resource::parents(ur);
  499. // Create components
  500. const ComponentData* component = unit_resource::component_type_data(ur, NULL);
  501. for (u32 cc = 0; cc < ur->num_component_types; ++cc)
  502. {
  503. const u32* unit_index = unit_resource::component_unit_index(component);
  504. const char* data = unit_resource::component_payload(component);
  505. if (component->type == COMPONENT_TYPE_TRANSFORM)
  506. {
  507. const TransformDesc* td = (const TransformDesc*)data;
  508. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++td)
  509. {
  510. // FIXME: add SceneGraph::allocate() to reserve an instance
  511. // without initializing it.
  512. const TransformInstance ti = scene_graph->create(unit_lookup[unit_index[i]]
  513. , td->position
  514. , td->rotation
  515. , td->scale
  516. );
  517. if (unit_parents[unit_index[i]] != UINT32_MAX)
  518. {
  519. TransformInstance parent_ti = scene_graph->instance(unit_lookup[unit_parents[unit_index[i]]]);
  520. scene_graph->link(parent_ti, ti, td->position, td->rotation, td->scale);
  521. }
  522. else
  523. {
  524. const Vector3 scale = vector3(td->scale.x * scl.x
  525. , td->scale.y * scl.y
  526. , td->scale.z * scl.z
  527. );
  528. Matrix4x4 tr = from_quaternion_translation(rot, pos);
  529. scene_graph->set_local_pose(ti, scene_graph->local_pose(ti) * tr);
  530. scene_graph->set_local_scale(ti, scale);
  531. }
  532. }
  533. }
  534. else if (component->type == COMPONENT_TYPE_CAMERA)
  535. {
  536. const CameraDesc* cd = (const CameraDesc*)data;
  537. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++cd)
  538. {
  539. w.camera_create(unit_lookup[unit_index[i]], *cd, MATRIX4X4_IDENTITY);
  540. }
  541. }
  542. else if (component->type == COMPONENT_TYPE_COLLIDER)
  543. {
  544. const ColliderDesc* cd = (const ColliderDesc*)data;
  545. for (u32 i = 0, n = component->num_instances; i < n; ++i)
  546. {
  547. TransformInstance ti = scene_graph->instance(unit_lookup[unit_index[i]]);
  548. Matrix4x4 tm = scene_graph->world_pose(ti);
  549. physics_world->collider_create(unit_lookup[unit_index[i]], cd, scale(tm));
  550. cd = (ColliderDesc*)((char*)(cd + 1) + cd->size);
  551. }
  552. }
  553. else if (component->type == COMPONENT_TYPE_ACTOR)
  554. {
  555. const ActorResource* ar = (const ActorResource*)data;
  556. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++ar)
  557. {
  558. TransformInstance ti = scene_graph->instance(unit_lookup[unit_index[i]]);
  559. Matrix4x4 tm = scene_graph->world_pose(ti);
  560. physics_world->actor_create(unit_lookup[unit_index[i]], ar, from_quaternion_translation(rotation(tm), translation(tm)));
  561. }
  562. }
  563. else if (component->type == COMPONENT_TYPE_MESH_RENDERER)
  564. {
  565. const MeshRendererDesc* mrd = (const MeshRendererDesc*)data;
  566. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++mrd)
  567. {
  568. TransformInstance ti = scene_graph->instance(unit_lookup[unit_index[i]]);
  569. Matrix4x4 tm = scene_graph->world_pose(ti);
  570. render_world->mesh_create(unit_lookup[unit_index[i]], *mrd, tm);
  571. }
  572. }
  573. else if (component->type == COMPONENT_TYPE_SPRITE_RENDERER)
  574. {
  575. const SpriteRendererDesc* srd = (const SpriteRendererDesc*)data;
  576. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++srd)
  577. {
  578. TransformInstance ti = scene_graph->instance(unit_lookup[unit_index[i]]);
  579. Matrix4x4 tm = scene_graph->world_pose(ti);
  580. render_world->sprite_create(unit_lookup[unit_index[i]], *srd, tm);
  581. }
  582. }
  583. else if (component->type == COMPONENT_TYPE_LIGHT)
  584. {
  585. const LightDesc* ld = (const LightDesc*)data;
  586. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++ld)
  587. {
  588. TransformInstance ti = scene_graph->instance(unit_lookup[unit_index[i]]);
  589. Matrix4x4 tm = scene_graph->world_pose(ti);
  590. render_world->light_create(unit_lookup[unit_index[i]], *ld, tm);
  591. }
  592. }
  593. else if (component->type == COMPONENT_TYPE_SCRIPT)
  594. {
  595. const ScriptDesc* sd = (const ScriptDesc*)data;
  596. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++sd)
  597. {
  598. script_world::create(*script_world, unit_lookup[unit_index[i]], *sd);
  599. }
  600. }
  601. else if (component->type == COMPONENT_TYPE_ANIMATION_STATE_MACHINE)
  602. {
  603. const AnimationStateMachineDesc* asmd = (const AnimationStateMachineDesc*)data;
  604. for (u32 i = 0, n = component->num_instances; i < n; ++i, ++asmd)
  605. {
  606. animation_state_machine->create(unit_lookup[unit_index[i]], *asmd);
  607. }
  608. }
  609. else
  610. {
  611. CE_FATAL("Unknown component type");
  612. }
  613. component = unit_resource::component_type_data(ur, component);
  614. }
  615. for (u32 i = 0; i < ur->num_units; ++i)
  616. array::push_back(w._units, unit_lookup[i]);
  617. // Post events
  618. for (u32 i = 0; i < ur->num_units; ++i)
  619. w.post_unit_spawned_event(unit_lookup[i]);
  620. }
  621. } // namespace crown