SceneLoader.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. #include <utility>
  2. #include <vector>
  3. #include "ComponentConstructorInfo.h"
  4. #include "PropertyLoader.h"
  5. #include "SceneLoader.h"
  6. SceneLoader::SceneLoader()
  7. {
  8. m_changeController = nullptr;
  9. m_loadInBackground = false;
  10. for(int i = 0; i < Systems::NumberOfSystems; i++)
  11. m_systemScenes[i] = g_nullSystemBase.createScene(this, EngineStateType::EngineStateType_Default);
  12. }
  13. SceneLoader::~SceneLoader()
  14. {
  15. }
  16. ErrorCode SceneLoader::loadFromProperties(const PropertySet &p_sceneProperties)
  17. {
  18. ErrorCode returnError = ErrorCode::Success;
  19. EntitiesConstructionInfo constructionInfo;
  20. // Get systems property set
  21. auto &systemProperties = p_sceneProperties.getPropertySetByID(Properties::Systems);
  22. // Iterate over all systems scenes
  23. for(int sysIndex = 0; sysIndex < Systems::NumberOfSystems; sysIndex++)
  24. {
  25. // Create an empty property set, in case there is none in the loaded file, because a system scene setup must be called either way
  26. PropertySet scenePropertySet;
  27. PropertySet systemropertySet;
  28. // Iterate over each system property set
  29. for(decltype(systemProperties.getNumPropertySets()) propIndex = 0, propSize = systemProperties.getNumPropertySets(); propIndex < propSize; propIndex++)
  30. {
  31. // If the system scene property matches in the loaded file, retrieve it so it can be passed to the corresponding scene
  32. if(Systems::SystemNames[m_systemScenes[sysIndex]->getSystemType()] == GetString(systemProperties.getPropertySetUnsafe(propIndex).getPropertyID()))
  33. {
  34. scenePropertySet = systemProperties.getPropertySetUnsafe(propIndex).getPropertySetByID(Properties::Scene);
  35. systemropertySet = systemProperties.getPropertySetUnsafe(propIndex).getPropertySetByID(Properties::System);
  36. }
  37. }
  38. // Pass the scene and system propertySet parameters
  39. m_systemScenes[sysIndex]->getSystem()->setup(systemropertySet);
  40. m_systemScenes[sysIndex]->setup(scenePropertySet);
  41. }
  42. // Get Game Objects
  43. auto &gameObjects = p_sceneProperties.getPropertySetByID(Properties::GameObject);
  44. if(gameObjects)
  45. {
  46. // Reserve enough room for all the game objects
  47. constructionInfo.resize(gameObjects.getNumPropertySets());
  48. // Iterate over all game objects
  49. for(decltype(gameObjects.getNumPropertySets()) objIndex = 0, objSize = gameObjects.getNumPropertySets(); objIndex < objSize; objIndex++)
  50. {
  51. // Import the game object data from PropertySets to EntitiesConstructionInfo
  52. importFromProperties(constructionInfo[objIndex], gameObjects.getPropertySetUnsafe(objIndex));
  53. }
  54. // Get the world scene required for creating entities
  55. WorldScene *worldScene = static_cast<WorldScene *>(m_systemScenes[Systems::World]);
  56. // Go over each entity and create it
  57. for(decltype(constructionInfo.size()) i = 0, size = constructionInfo.size(); i < size; i++)
  58. {
  59. worldScene->createEntity(constructionInfo[i], false);
  60. }
  61. }
  62. else
  63. {
  64. // GameObject property set is missing
  65. returnError = ErrorCode::GameObjects_missing;
  66. ErrHandlerLoc().get().log(ErrorCode::GameObjects_missing, ErrorSource::Source_SceneLoader);
  67. }
  68. // Check if the scene should be loaded in background
  69. if(p_sceneProperties.getPropertyByID(Properties::LoadInBackground).getBool())
  70. {
  71. m_loadInBackground = true;
  72. // Start loading in background threads in all scenes
  73. for(int i = 0; i < Systems::NumberOfSystems; i++)
  74. m_systemScenes[i]->loadInBackground();
  75. }
  76. else
  77. {
  78. m_loadInBackground = false;
  79. // Preload all scenes sequentially
  80. for(int i = 0; i < Systems::NumberOfSystems; i++)
  81. m_systemScenes[i]->preload();
  82. }
  83. // Make sure to clear the memory of contructionInfo
  84. for(decltype(constructionInfo.size()) i = 0, size = constructionInfo.size(); i < size; i++)
  85. constructionInfo[i].deleteConstructionInfo();
  86. return returnError;
  87. }
  88. ErrorCode SceneLoader::loadFromFile(const std::string &p_filename)
  89. {
  90. ErrorCode returnError = ErrorCode::Success;
  91. m_filename = p_filename;
  92. EntitiesConstructionInfo constructionInfo;
  93. // Load properties from file
  94. PropertyLoader loadedProperties(Config::filepathVar().map_path + p_filename);
  95. returnError = loadedProperties.loadFromFile();
  96. // Check if loading was successful, return an error, if not
  97. if(returnError != ErrorCode::Success)
  98. {
  99. ErrHandlerLoc().get().log(returnError, ErrorSource::Source_SceneLoader);
  100. }
  101. else
  102. {
  103. returnError = loadFromProperties(loadedProperties.getPropertySet());
  104. }
  105. return returnError;
  106. }
  107. ErrorCode SceneLoader::saveToFile(const std::string p_filename)
  108. {
  109. std::string filename;
  110. if(!p_filename.empty())
  111. filename = Config::filepathVar().map_path + p_filename;
  112. else
  113. filename = m_filename;
  114. if(!filename.empty())
  115. {
  116. // Get the world scene required for getting the entity registry
  117. WorldScene *worldScene = static_cast<WorldScene *>(m_systemScenes[Systems::World]);
  118. // Get the entity registry
  119. auto &entityRegistry = worldScene->getEntityRegistry();
  120. // Add root property set for the whole file
  121. PropertySet rootPropertySet(Properties::Default);
  122. // Add root property set game objects
  123. auto &gameObjects = rootPropertySet.addPropertySet(Properties::GameObject);
  124. // An array holding all of the entities
  125. std::vector<EntityID> allEntities;
  126. // Add all entities to the array
  127. for(auto entity : entityRegistry.view<EntityID>())
  128. allEntities.push_back(entity);
  129. // Sort the entities so they are written to file in order
  130. std::sort(allEntities.begin(), allEntities.end());
  131. // Iterate every entity
  132. for(auto &entity : allEntities)
  133. {
  134. // Create an array entry for the entity
  135. auto &gameObjectEntry = gameObjects.addPropertySet(Properties::ArrayEntry);
  136. // Export the entity to the Construction Info
  137. ComponentsConstructionInfo constructionInfo;
  138. worldScene->exportEntity(entity, constructionInfo);
  139. // Export the Construction Info to the Property Set
  140. exportToProperties(constructionInfo, gameObjectEntry);
  141. }
  142. // Add scene loaded properties
  143. rootPropertySet.addProperty(Properties::LoadInBackground, m_loadInBackground);
  144. // Add root property set for systems
  145. auto &rootSystemsPropertySet = rootPropertySet.addPropertySet(Properties::Systems);
  146. // Add each system's properties
  147. for(size_t systemType = 0; systemType < Systems::NumberOfSystems; systemType++)
  148. {
  149. // Convert TypeID to PropertyID
  150. Properties::PropertyID systemTypeProperty = Properties::Null;
  151. switch(systemType)
  152. {
  153. case Systems::TypeID::Audio:
  154. systemTypeProperty = Properties::Audio;
  155. break;
  156. case Systems::TypeID::Graphics:
  157. systemTypeProperty = Properties::Graphics;
  158. break;
  159. case Systems::TypeID::GUI:
  160. systemTypeProperty = Properties::GUI;
  161. break;
  162. case Systems::TypeID::Physics:
  163. systemTypeProperty = Properties::Physics;
  164. break;
  165. case Systems::TypeID::Script:
  166. systemTypeProperty = Properties::Script;
  167. break;
  168. case Systems::TypeID::World:
  169. systemTypeProperty = Properties::World;
  170. break;
  171. }
  172. // Add system type entry
  173. auto &systemPropertyIDentry = rootSystemsPropertySet.addPropertySet(systemTypeProperty);
  174. // Add scene and system settings
  175. m_systemScenes[systemType]->exportSetup(systemPropertyIDentry.addPropertySet(Properties::Scene));
  176. m_systemScenes[systemType]->getSystem()->exportSetup(systemPropertyIDentry.addPropertySet(Properties::System));
  177. }
  178. // Save properties to a file
  179. PropertyLoader savedProperties(filename);
  180. ErrorCode loaderError = savedProperties.saveToFile(rootPropertySet);
  181. // Check if loading was successful, return an error, if not
  182. if(loaderError != ErrorCode::Success)
  183. return loaderError;
  184. ErrHandlerLoc().get().log(ErrorType::Info, ErrorSource::Source_PropertyLoader, "File has been exported: " + filename);
  185. }
  186. // TODO ERROR empty filename
  187. return ErrorCode::Failure;
  188. }
  189. ErrorCode SceneLoader::importPrefab(ComponentsConstructionInfo &p_constructionInfo, const std::string &p_filename, const bool p_forceReload)
  190. {
  191. ErrorCode returnError = ErrorCode::Success;
  192. // Check if the given filename isn't empty
  193. if(!p_filename.empty())
  194. {
  195. // Search for the given prefab (it might have been loaded before, already)
  196. auto prefabIterator = m_prefabs.find(p_filename);
  197. // If the prefab was already imported and exists in the map, use it
  198. if(prefabIterator != m_prefabs.end())
  199. {
  200. if(p_forceReload)
  201. returnError = importFromFile(p_constructionInfo, Config::filepathVar().prefab_path + p_filename);
  202. else
  203. p_constructionInfo.completeCopy(prefabIterator->second);
  204. }
  205. else // If the prefab doesn't exist in the map, import it
  206. {
  207. // Make sure calls from other threads are locked, while current call is in progress
  208. // This is needed as the prefab that is being requested might be currently being imported
  209. // Mutex prevents duplicate prefabs being loaded, and same data being changed.
  210. SpinWait::Lock lock(m_mutex);
  211. // Search for the prefab again, as it might have been imported from another thread call before mutex lock was released
  212. auto prefabIteratorNew = m_prefabs.find(p_filename);
  213. if(prefabIteratorNew != m_prefabs.end())
  214. {
  215. if(p_forceReload)
  216. returnError = importFromFile(p_constructionInfo, Config::filepathVar().prefab_path + p_filename);
  217. else
  218. p_constructionInfo.completeCopy(prefabIterator->second);
  219. }
  220. else
  221. {
  222. // Load properties from file
  223. PropertyLoader loadedProperties(Config::filepathVar().prefab_path + p_filename);
  224. returnError = loadedProperties.loadFromFile();
  225. if(returnError == ErrorCode::Success)
  226. {
  227. // Insert a new prefab into the map
  228. ComponentsConstructionInfo &constructionInfo = m_prefabs.try_emplace(p_filename).first->second;
  229. // Populate the newly imported prefab
  230. importFromProperties(constructionInfo, loadedProperties.getPropertySet());
  231. p_constructionInfo.completeCopy(constructionInfo);
  232. }
  233. }
  234. }
  235. }
  236. else
  237. returnError = ErrorCode::Filename_empty;
  238. return returnError;
  239. }
  240. ErrorCode SceneLoader::importFromFile(ComponentsConstructionInfo &p_constructionInfo, const std::string &p_filename)
  241. {
  242. ErrorCode returnError = ErrorCode::Success;
  243. // Load properties from file
  244. PropertyLoader loadedProperties(p_filename);
  245. returnError = loadedProperties.loadFromFile();
  246. if(returnError == ErrorCode::Success)
  247. importFromProperties(p_constructionInfo, loadedProperties.getPropertySet());
  248. return returnError;
  249. }
  250. void SceneLoader::importFromProperties(ComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties)
  251. {
  252. // Load Prefab first
  253. auto &prefabProperty = p_properties.getPropertyByID(Properties::Prefab);
  254. if(prefabProperty)
  255. {
  256. std::string prefabName = prefabProperty.getString();
  257. importPrefab(p_constructionInfo, prefabName);
  258. p_constructionInfo.m_prefab = prefabName;
  259. }
  260. // Variable for the object name
  261. std::string name;
  262. // Load property data
  263. for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
  264. {
  265. switch(p_properties[i].getPropertyID())
  266. {
  267. case Properties::ID:
  268. {
  269. // Get the desired ID of the entity
  270. p_constructionInfo.m_id = (EntityID)p_properties[i].getInt();
  271. }
  272. break;
  273. case Properties::Name:
  274. {
  275. // Get the entity name
  276. name = p_properties[i].getString();
  277. }
  278. break;
  279. case Properties::Parent:
  280. {
  281. // Get the entity ID if the parent object
  282. p_constructionInfo.m_parent = (EntityID)p_properties[i].getInt();
  283. }
  284. break;
  285. }
  286. }
  287. // If the name property is missing, generate a unique name based on the entity ID
  288. if(name.empty())
  289. if(p_constructionInfo.m_name.empty())
  290. name = GetString(Properties::GameObject) + Utilities::toString(p_constructionInfo.m_id);
  291. else
  292. name = p_constructionInfo.m_name + Utilities::toString(p_constructionInfo.m_id);
  293. // Make sure to assign values after the Prefab has been imported (if there was one)
  294. p_constructionInfo.m_name = name;
  295. // Load audio components
  296. {
  297. auto &sceneProperty = p_properties.getPropertySetByID(Properties::Audio);
  298. if(sceneProperty)
  299. {
  300. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  301. {
  302. importFromProperties(p_constructionInfo.m_audioComponents, sceneProperty.getPropertySet(i), name);
  303. }
  304. }
  305. }
  306. // Load graphics components
  307. {
  308. auto &sceneProperty = p_properties.getPropertySetByID(Properties::Graphics);
  309. if(sceneProperty)
  310. {
  311. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  312. {
  313. importFromProperties(p_constructionInfo.m_graphicsComponents, sceneProperty.getPropertySet(i), name);
  314. }
  315. }
  316. }
  317. // Load GUI components
  318. {
  319. auto &sceneProperty = p_properties.getPropertySetByID(Properties::GUI);
  320. if(sceneProperty)
  321. {
  322. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  323. {
  324. importFromProperties(p_constructionInfo.m_guiComponents, sceneProperty.getPropertySet(i), name);
  325. }
  326. }
  327. }
  328. // Load physics components
  329. {
  330. auto &sceneProperty = p_properties.getPropertySetByID(Properties::Physics);
  331. if(sceneProperty)
  332. {
  333. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  334. {
  335. importFromProperties(p_constructionInfo.m_physicsComponents, sceneProperty.getPropertySet(i), name);
  336. }
  337. }
  338. }
  339. // Load script components
  340. {
  341. auto &sceneProperty = p_properties.getPropertySetByID(Properties::Script);
  342. if(sceneProperty)
  343. {
  344. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  345. {
  346. importFromProperties(p_constructionInfo.m_scriptComponents, sceneProperty.getPropertySet(i), name);
  347. }
  348. }
  349. }
  350. // Load world components
  351. {
  352. auto &sceneProperty = p_properties.getPropertySetByID(Properties::World);
  353. if(sceneProperty)
  354. {
  355. for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
  356. {
  357. importFromProperties(p_constructionInfo.m_worldComponents, sceneProperty.getPropertySet(i), name);
  358. }
  359. }
  360. }
  361. }
  362. void SceneLoader::importFromProperties(AudioComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  363. {
  364. // Check if property set node is present
  365. if(p_properties)
  366. {
  367. switch(p_properties.getPropertyID())
  368. {
  369. case Properties::PropertyID::SoundComponent:
  370. {
  371. if(p_constructionInfo.m_soundConstructionInfo == nullptr)
  372. p_constructionInfo.m_soundConstructionInfo = new SoundComponent::SoundComponentConstructionInfo();
  373. p_constructionInfo.m_soundConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SoundComponent);
  374. // Get the sound filename
  375. auto const &filename = p_properties.getPropertyByID(Properties::Filename).getString();
  376. if(!filename.empty())
  377. {
  378. // Get the sound type
  379. auto const &type = p_properties.getPropertyByID(Properties::Type).getID();
  380. // Load values based on the type of sound
  381. switch(type)
  382. {
  383. case Properties::Ambient:
  384. p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_Ambient;
  385. p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
  386. p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
  387. p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
  388. p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
  389. p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
  390. break;
  391. case Properties::Music:
  392. p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_Music;
  393. p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
  394. p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
  395. p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
  396. p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
  397. p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
  398. break;
  399. case Properties::SoundEffect:
  400. p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_SoundEffect;
  401. p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
  402. p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
  403. p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
  404. p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
  405. p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
  406. break;
  407. default:
  408. ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_SoundComponent);
  409. delete p_constructionInfo.m_soundConstructionInfo;
  410. p_constructionInfo.m_soundConstructionInfo = nullptr;
  411. break;
  412. }
  413. }
  414. else
  415. {
  416. ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SoundComponent);
  417. delete p_constructionInfo.m_soundConstructionInfo;
  418. p_constructionInfo.m_soundConstructionInfo = nullptr;
  419. }
  420. }
  421. break;
  422. case Properties::PropertyID::SoundListenerComponent:
  423. {
  424. if(p_constructionInfo.m_soundListenerConstructionInfo == nullptr)
  425. p_constructionInfo.m_soundListenerConstructionInfo = new SoundListenerComponent::SoundListenerComponentConstructionInfo();
  426. p_constructionInfo.m_soundListenerConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SoundListenerComponent);
  427. p_properties.getValueByID(Properties::PropertyID::ID, p_constructionInfo.m_soundListenerConstructionInfo->m_listenerID);
  428. p_properties.getValueByID(Properties::PropertyID::Active, p_constructionInfo.m_soundListenerConstructionInfo->m_active);
  429. }
  430. break;
  431. }
  432. }
  433. }
  434. void SceneLoader::importFromProperties(GraphicsComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  435. {
  436. // Check if property set node is present
  437. if(p_properties)
  438. {
  439. switch(p_properties.getPropertyID())
  440. {
  441. case Properties::PropertyID::CameraComponent:
  442. {
  443. if(p_constructionInfo.m_cameraConstructionInfo == nullptr)
  444. p_constructionInfo.m_cameraConstructionInfo = new CameraComponent::CameraComponentConstructionInfo();
  445. p_constructionInfo.m_cameraConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::CameraComponent);
  446. }
  447. break;
  448. case Properties::PropertyID::LightComponent:
  449. {
  450. if(p_constructionInfo.m_lightConstructionInfo == nullptr)
  451. p_constructionInfo.m_lightConstructionInfo = new LightComponent::LightComponentConstructionInfo();
  452. p_constructionInfo.m_lightConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::LightComponent);
  453. // Get the light type
  454. auto const &type = p_properties.getPropertyByID(Properties::Type).getID();
  455. // Load values based on the type of light
  456. switch(type)
  457. {
  458. case Properties::DirectionalLight:
  459. p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_directional;
  460. p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  461. p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  462. break;
  463. case Properties::PointLight:
  464. p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_point;
  465. p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  466. p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  467. break;
  468. case Properties::SpotLight:
  469. p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_spot;
  470. p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  471. p_properties.getValueByID(Properties::CutoffAngle, p_constructionInfo.m_lightConstructionInfo->m_cutoffAngle);
  472. p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  473. break;
  474. default:
  475. ErrHandlerLoc().get().log(ErrorType::Warning, ErrorSource::Source_LightComponent, p_name + " - missing \'Type\' identifier");
  476. delete p_constructionInfo.m_lightConstructionInfo;
  477. p_constructionInfo.m_lightConstructionInfo = nullptr;
  478. break;
  479. }
  480. }
  481. break;
  482. case Properties::PropertyID::ModelComponent:
  483. {
  484. if(p_constructionInfo.m_modelConstructionInfo == nullptr)
  485. p_constructionInfo.m_modelConstructionInfo = new ModelComponent::ModelComponentConstructionInfo();
  486. p_constructionInfo.m_modelConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ModelComponent);
  487. bool modelDataPresent = false;
  488. auto &modelsProperty = p_properties.getPropertySetByID(Properties::Models);
  489. if(modelsProperty)
  490. {
  491. if(modelsProperty.getNumPropertySets() > 0)
  492. p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.clear();
  493. // Loop over each model entry in the node
  494. for(decltype(modelsProperty.getNumPropertySets()) iModel = 0, numModels = modelsProperty.getNumPropertySets(); iModel < numModels; iModel++)
  495. {
  496. // Get model filename
  497. auto modelName = modelsProperty.getPropertySet(iModel).getPropertyByID(Properties::Filename).getString();
  498. // Continue only of the model filename is not empty
  499. if(!modelName.empty())
  500. {
  501. modelDataPresent = true;
  502. // Add a new model data entry, and get a reference to it
  503. p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.push_back(ModelComponent::MeshProperties());
  504. auto &newModelEntry = p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.back();
  505. // Assign the model filename
  506. newModelEntry.m_modelName = modelName;
  507. // Get meshes property
  508. auto &meshesProperty = modelsProperty.getPropertySet(iModel).getPropertySetByID(Properties::Meshes);
  509. // Check if the meshes array node is present;
  510. // If it is present, only add the meshes included in the meshes node
  511. // If it is not present, add all the meshes included in the model
  512. if(meshesProperty)
  513. {
  514. if(meshesProperty.getNumPropertySets() > 0)
  515. {
  516. // Loop over each mesh entry in the model node
  517. for(decltype(meshesProperty.getNumPropertySets()) iMesh = 0, numMeshes = meshesProperty.getNumPropertySets(); iMesh < numMeshes; iMesh++)
  518. {
  519. // Try to get the mesh index property node and check if it is present
  520. auto &meshIndexProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::Index);
  521. if(meshIndexProperty)
  522. {
  523. // Get the mesh index, check if it is valid and within the range of mesh array that was loaded from the model
  524. const int meshDataIndex = meshIndexProperty.getInt();
  525. // Make sure the meshMaterials vector can fit the given mesh index
  526. if(meshDataIndex >= newModelEntry.m_meshMaterials.size())
  527. {
  528. newModelEntry.resize(meshDataIndex + 1);
  529. newModelEntry.m_present[meshDataIndex] = true;
  530. }
  531. // Get the active flag, if it is present
  532. if(auto activeProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::Active); activeProperty)
  533. newModelEntry.m_active[meshDataIndex] = activeProperty.getBool();
  534. // Get material alpha threshold value, if it is present
  535. if(auto alphaThresholdProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::AlphaThreshold); alphaThresholdProperty)
  536. newModelEntry.m_alphaThreshold[meshDataIndex] = alphaThresholdProperty.getFloat();
  537. // Get emissive intensity, if it is present
  538. if(auto emissiveIntensityProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::EmissiveIntensity); emissiveIntensityProperty)
  539. newModelEntry.m_emissiveIntensity[meshDataIndex] = emissiveIntensityProperty.getFloat();
  540. // Get material height scale value, if it is present
  541. if(auto heightScaleProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::HeightScale); heightScaleProperty)
  542. newModelEntry.m_heightScale[meshDataIndex] = heightScaleProperty.getFloat();
  543. // Get material wrap mode
  544. if(auto wrapModeProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::WrapMode); wrapModeProperty)
  545. {
  546. switch(wrapModeProperty.getID())
  547. {
  548. case Properties::ClampToBorder:
  549. newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_ClampToBorder;
  550. break;
  551. case Properties::ClampToEdge:
  552. newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_ClampToEdge;
  553. break;
  554. case Properties::MirroredClampToEdge:
  555. newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_MirroredClampToEdge;
  556. break;
  557. case Properties::MirroredRepeat:
  558. newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_MirroredRepeat;
  559. break;
  560. case Properties::Repeat:
  561. default:
  562. newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_Repeat;
  563. break;
  564. }
  565. }
  566. // Get material properties
  567. auto materialsProperty = meshesProperty.getPropertySet(iMesh).getPropertySetByID(Properties::Materials);
  568. // Define material data and material properties
  569. MaterialData materials[MaterialType::MaterialType_NumOfTypes];
  570. PropertySet materialProperties[MaterialType::MaterialType_NumOfTypes] =
  571. {
  572. materialsProperty.getPropertySetByID(Properties::Diffuse),
  573. materialsProperty.getPropertySetByID(Properties::Normal),
  574. materialsProperty.getPropertySetByID(Properties::Emissive),
  575. materialsProperty.getPropertySetByID(Properties::RMHAO)
  576. };
  577. // Go over each material type
  578. for(unsigned int iMatType = 0; iMatType < MaterialType::MaterialType_NumOfTypes; iMatType++)
  579. {
  580. // Check if an entry for the current material type was present within the properties
  581. if(materialProperties[iMatType])
  582. {
  583. // Get texture filename property, check if it is valid
  584. auto filenameProperty = materialProperties[iMatType].getPropertyByID(Properties::Filename);
  585. if(filenameProperty.isVariableTypeString())
  586. {
  587. // Get texture filename string, check if it is valid
  588. newModelEntry.m_meshMaterials[meshDataIndex][iMatType] = filenameProperty.getString();
  589. }
  590. // Get texture scale property, check if it is valid
  591. auto scaleProperty = materialProperties[iMatType].getPropertyByID(Properties::TextureScale);
  592. if(scaleProperty)
  593. newModelEntry.m_meshMaterialsScale[meshDataIndex][iMatType] = scaleProperty.getVec2f();
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. }
  603. if(p_properties.getNumPropertySets() == 0 || !modelDataPresent)
  604. {
  605. ErrHandlerLoc().get().log(ErrorType::Info, ErrorSource::Source_ModelComponent, p_name + " - missing model data");
  606. delete p_constructionInfo.m_modelConstructionInfo;
  607. p_constructionInfo.m_modelConstructionInfo = nullptr;
  608. }
  609. }
  610. break;
  611. case Properties::PropertyID::ShaderComponent:
  612. {
  613. if(p_constructionInfo.m_shaderConstructionInfo == nullptr)
  614. p_constructionInfo.m_shaderConstructionInfo = new ShaderComponent::ShaderComponentConstructionInfo();
  615. p_constructionInfo.m_shaderConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ShaderComponent);
  616. // Get nodes for different shader types
  617. auto geometryShaderNode = p_properties.getPropertyByID(Properties::GeometryShader);
  618. auto vertexShaderNode = p_properties.getPropertyByID(Properties::VertexShader);
  619. auto fragmentShaderNode = p_properties.getPropertyByID(Properties::FragmentShader);
  620. if(geometryShaderNode)
  621. p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename = geometryShaderNode.getString();
  622. if(vertexShaderNode)
  623. p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename = vertexShaderNode.getString();
  624. if(fragmentShaderNode)
  625. p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename = fragmentShaderNode.getString();
  626. }
  627. break;
  628. }
  629. }
  630. }
  631. void SceneLoader::importFromProperties(GUIComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  632. {
  633. // Check if property set node is present
  634. if(p_properties)
  635. {
  636. switch(p_properties.getPropertyID())
  637. {
  638. case Properties::PropertyID::GUISequenceComponent:
  639. {
  640. if(p_constructionInfo.m_guiSequenceConstructionInfo == nullptr)
  641. p_constructionInfo.m_guiSequenceConstructionInfo = new GUISequenceComponent::GUISequenceComponentConstructionInfo();
  642. p_constructionInfo.m_guiSequenceConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::GUISequenceComponent);
  643. }
  644. break;
  645. }
  646. }
  647. }
  648. void SceneLoader::importFromProperties(PhysicsComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  649. {
  650. // Check if property set node is present
  651. if(p_properties)
  652. {
  653. switch(p_properties.getPropertyID())
  654. {
  655. case Properties::PropertyID::RigidBodyComponent:
  656. {
  657. if(p_constructionInfo.m_rigidBodyConstructionInfo == nullptr)
  658. p_constructionInfo.m_rigidBodyConstructionInfo = new RigidBodyComponent::RigidBodyComponentConstructionInfo();
  659. p_constructionInfo.m_rigidBodyConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::RigidBodyComponent);
  660. // --------------------
  661. // Load collision shape
  662. // --------------------
  663. auto const &collisionShapeProperty = p_properties.getPropertySetByID(Properties::CollisionShape);
  664. if(collisionShapeProperty)
  665. {
  666. // Get the type of the collision shape and load the data based on it
  667. auto const &typeProperty = collisionShapeProperty.getPropertyByID(Properties::Type);
  668. if(typeProperty)
  669. {
  670. switch(typeProperty.getID())
  671. {
  672. case Properties::Box:
  673. {
  674. // Get the size property
  675. auto const &sizeProperty = collisionShapeProperty.getPropertyByID(Properties::Size);
  676. // If the size was not given, leave it to a default 0.5f all around (so it gets to the final 1.0/1.0/1.0 dimension)
  677. if(sizeProperty)
  678. p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize = sizeProperty.getVec3f();
  679. else
  680. ErrHandlerLoc().get().log(ErrorCode::Property_missing_size, p_name, ErrorSource::Source_RigidBodyComponent);
  681. p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType = RigidBodyComponent::CollisionShapeType::CollisionShapeType_Box;
  682. }
  683. break;
  684. case Properties::Sphere:
  685. {
  686. // Get the size property
  687. auto const &radiusProperty = collisionShapeProperty.getPropertyByID(Properties::Radius);
  688. // If the size was not given, leave it to a default radius of 0.5f (which makes the sphere diameter equal to 1.0)
  689. if(radiusProperty)
  690. p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize.x = radiusProperty.getFloat();
  691. else
  692. ErrHandlerLoc().get().log(ErrorCode::Property_missing_radius, p_name, ErrorSource::Source_RigidBodyComponent);
  693. p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType = RigidBodyComponent::CollisionShapeType::CollisionShapeType_Sphere;
  694. }
  695. break;
  696. default:
  697. // If this is reached, the collision shape type was not valid
  698. ErrHandlerLoc().get().log(ErrorCode::Collision_invalid, p_name, ErrorSource::Source_RigidBodyComponent);
  699. break;
  700. }
  701. }
  702. else
  703. {
  704. // Missing the Type property entirely
  705. ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_RigidBodyComponent);
  706. }
  707. }
  708. // -----------------------------
  709. // Load individual property data
  710. // -----------------------------
  711. for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
  712. {
  713. switch(p_properties[i].getPropertyID())
  714. {
  715. case Properties::Friction:
  716. p_constructionInfo.m_rigidBodyConstructionInfo->m_friction = p_properties[i].getFloat();
  717. break;
  718. case Properties::Kinematic:
  719. p_constructionInfo.m_rigidBodyConstructionInfo->m_kinematic = p_properties[i].getBool();
  720. break;
  721. case Properties::Mass:
  722. p_constructionInfo.m_rigidBodyConstructionInfo->m_mass = p_properties[i].getFloat();
  723. break;
  724. case Properties::Restitution:
  725. p_constructionInfo.m_rigidBodyConstructionInfo->m_restitution = p_properties[i].getFloat();
  726. break;
  727. case Properties::Velocity:
  728. p_constructionInfo.m_rigidBodyConstructionInfo->m_linearVelocity = p_properties[i].getVec3f();
  729. break;
  730. }
  731. }
  732. }
  733. break;
  734. }
  735. }
  736. }
  737. void SceneLoader::importFromProperties(ScriptComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  738. {
  739. // Check if property set node is present
  740. if(p_properties)
  741. {
  742. switch(p_properties.getPropertyID())
  743. {
  744. case Properties::PropertyID::LuaComponent:
  745. {
  746. if(p_constructionInfo.m_luaConstructionInfo == nullptr)
  747. p_constructionInfo.m_luaConstructionInfo = new LuaComponent::LuaComponentConstructionInfo();
  748. p_constructionInfo.m_luaConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::LuaComponent);
  749. auto const &luaFilenameProperty = p_properties.getPropertyByID(Properties::Filename);
  750. auto const &luaVariablesProperty = p_properties.getPropertySetByID(Properties::Variables);
  751. if(luaFilenameProperty)
  752. {
  753. std::string luaFilename = luaFilenameProperty.getString();
  754. if(!luaFilename.empty())
  755. {
  756. p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename = luaFilename;
  757. if(luaVariablesProperty)
  758. {
  759. // Loop over each variable entry in the node
  760. for(decltype(luaVariablesProperty.getNumPropertySets()) iVariable = 0, numVariables = luaVariablesProperty.getNumPropertySets(); iVariable < numVariables; iVariable++)
  761. {
  762. // Add the variable
  763. p_constructionInfo.m_luaConstructionInfo->m_variables.emplace_back(
  764. luaVariablesProperty.getPropertySet(iVariable).getPropertyByID(Properties::Name).getString(),
  765. luaVariablesProperty.getPropertySet(iVariable).getPropertyByID(Properties::Value));
  766. }
  767. }
  768. }
  769. else
  770. {
  771. ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SceneLoader);
  772. }
  773. }
  774. else
  775. {
  776. ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SceneLoader);
  777. }
  778. }
  779. break;
  780. }
  781. }
  782. }
  783. void SceneLoader::importFromProperties(WorldComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
  784. {
  785. // Check if property set node is present
  786. if(p_properties)
  787. {
  788. switch(p_properties.getPropertyID())
  789. {
  790. case Properties::PropertyID::ObjectMaterialComponent:
  791. {
  792. if(p_constructionInfo.m_objectMaterialConstructionInfo == nullptr)
  793. p_constructionInfo.m_objectMaterialConstructionInfo = new ObjectMaterialComponent::ObjectMaterialComponentConstructionInfo();
  794. p_constructionInfo.m_objectMaterialConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ObjectMaterialComponent);
  795. // Get the type property representing the object material type
  796. auto const &typeProperty = p_properties.getPropertyByID(Properties::Type);
  797. // Check if the property exists
  798. if(typeProperty)
  799. {
  800. // Define the type based on the imported property
  801. switch(typeProperty.getID())
  802. {
  803. case Properties::Concrete:
  804. default:
  805. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Concrete;
  806. break;
  807. case Properties::Glass:
  808. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Glass;
  809. break;
  810. case Properties::Metal:
  811. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Metal;
  812. break;
  813. case Properties::Plastic:
  814. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Plastic;
  815. break;
  816. case Properties::Rock:
  817. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Rock;
  818. break;
  819. case Properties::Rubber:
  820. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Rubber;
  821. break;
  822. case Properties::Wood:
  823. p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Wood;
  824. break;
  825. // If this is reached, the object material type was not valid
  826. ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_ObjectMaterialComponent);
  827. break;
  828. }
  829. }
  830. else
  831. {
  832. // Missing the Type property entirely
  833. ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_ObjectMaterialComponent);
  834. }
  835. }
  836. break;
  837. case Properties::PropertyID::SpatialComponent:
  838. {
  839. if(p_constructionInfo.m_spatialConstructionInfo == nullptr)
  840. p_constructionInfo.m_spatialConstructionInfo = new SpatialComponent::SpatialComponentConstructionInfo();
  841. p_constructionInfo.m_spatialConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SpatialComponent);
  842. // Load property data
  843. for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
  844. {
  845. switch(p_properties[i].getPropertyID())
  846. {
  847. case Properties::LocalPosition:
  848. p_constructionInfo.m_spatialConstructionInfo->m_localPosition = p_properties[i].getVec3f();
  849. break;
  850. case Properties::LocalRotation:
  851. p_constructionInfo.m_spatialConstructionInfo->m_localRotationEuler = p_properties[i].getVec3f();
  852. break;
  853. case Properties::LocalRotationQuaternion:
  854. p_constructionInfo.m_spatialConstructionInfo->m_localRotationQuaternion = Math::toGlmQuat(p_properties[i].getVec4f());
  855. break;
  856. case Properties::LocalScale:
  857. p_constructionInfo.m_spatialConstructionInfo->m_localScale = p_properties[i].getVec3f();
  858. break;
  859. }
  860. }
  861. }
  862. break;
  863. }
  864. }
  865. }
  866. void SceneLoader::exportToProperties(const ComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  867. {
  868. p_properties.addProperty(Properties::PropertyID::Name, p_constructionInfo.m_name);
  869. p_properties.addProperty(Properties::PropertyID::ID, (int)p_constructionInfo.m_id);
  870. p_properties.addProperty(Properties::PropertyID::Parent, (int)p_constructionInfo.m_parent);
  871. if(!p_constructionInfo.m_prefab.empty())
  872. p_properties.addProperty(Properties::PropertyID::Prefab, p_constructionInfo.m_prefab);
  873. // Export audio components
  874. exportToProperties(p_constructionInfo.m_audioComponents, p_properties);
  875. // Export graphics components
  876. exportToProperties(p_constructionInfo.m_graphicsComponents, p_properties);
  877. // Export GUI components
  878. exportToProperties(p_constructionInfo.m_guiComponents, p_properties);
  879. // Export physics components
  880. exportToProperties(p_constructionInfo.m_physicsComponents, p_properties);
  881. // Export script components
  882. exportToProperties(p_constructionInfo.m_scriptComponents, p_properties);
  883. // Export world components
  884. exportToProperties(p_constructionInfo.m_worldComponents, p_properties);
  885. }
  886. void SceneLoader::exportToProperties(const AudioComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  887. {
  888. if( p_constructionInfo.m_soundConstructionInfo != nullptr ||
  889. p_constructionInfo.m_soundListenerConstructionInfo != nullptr)
  890. {
  891. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Audio);
  892. // Export SoundComponent
  893. if(p_constructionInfo.m_soundConstructionInfo != nullptr)
  894. {
  895. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SoundComponent);
  896. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_soundConstructionInfo->m_active);
  897. componentPropertySet.addProperty(Properties::PropertyID::Filename, p_constructionInfo.m_soundConstructionInfo->m_soundFilename);
  898. switch(p_constructionInfo.m_soundConstructionInfo->m_soundType)
  899. {
  900. case SoundComponent::SoundType::SoundType_Ambient:
  901. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::Ambient);
  902. break;
  903. case SoundComponent::SoundType::SoundType_Music:
  904. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::Music);
  905. break;
  906. case SoundComponent::SoundType::SoundType_SoundEffect:
  907. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::SoundEffect);
  908. break;
  909. }
  910. componentPropertySet.addProperty(Properties::PropertyID::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
  911. componentPropertySet.addProperty(Properties::PropertyID::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
  912. componentPropertySet.addProperty(Properties::PropertyID::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
  913. componentPropertySet.addProperty(Properties::PropertyID::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
  914. }
  915. // Export SoundListenerComponent
  916. if(p_constructionInfo.m_soundListenerConstructionInfo != nullptr)
  917. {
  918. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SoundListenerComponent);
  919. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_soundListenerConstructionInfo->m_active);
  920. //componentPropertySet.addProperty(Properties::PropertyID::ListenerID, p_constructionInfo.m_soundListenerConstructionInfo->m_listenerID);
  921. }
  922. }
  923. }
  924. void SceneLoader::exportToProperties(const GraphicsComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  925. {
  926. if(p_constructionInfo.m_cameraConstructionInfo != nullptr ||
  927. p_constructionInfo.m_lightConstructionInfo != nullptr ||
  928. p_constructionInfo.m_modelConstructionInfo != nullptr ||
  929. p_constructionInfo.m_shaderConstructionInfo != nullptr)
  930. {
  931. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Graphics);
  932. // Export CameraComponent
  933. if(p_constructionInfo.m_cameraConstructionInfo != nullptr)
  934. {
  935. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::CameraComponent);
  936. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_cameraConstructionInfo->m_active);
  937. //componentPropertySet.addProperty(Properties::PropertyID::FOV, p_constructionInfo.m_cameraConstructionInfo->m_fov);
  938. }
  939. // Export LightComponent
  940. if(p_constructionInfo.m_lightConstructionInfo != nullptr)
  941. {
  942. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::LightComponent);
  943. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_lightConstructionInfo->m_active);
  944. switch(p_constructionInfo.m_lightConstructionInfo->m_lightComponentType)
  945. {
  946. case LightComponent::LightComponentType::LightComponentType_directional:
  947. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::DirectionalLight);
  948. componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  949. componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  950. break;
  951. case LightComponent::LightComponentType::LightComponentType_point:
  952. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::PointLight);
  953. componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  954. componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  955. break;
  956. case LightComponent::LightComponentType::LightComponentType_spot:
  957. componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::SpotLight);
  958. componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
  959. componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
  960. componentPropertySet.addProperty(Properties::PropertyID::CutoffAngle, p_constructionInfo.m_lightConstructionInfo->m_cutoffAngle);
  961. break;
  962. }
  963. }
  964. // Export ModelComponent
  965. if(p_constructionInfo.m_modelConstructionInfo != nullptr)
  966. {
  967. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ModelComponent);
  968. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_modelConstructionInfo->m_active);
  969. auto &modelsPropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::Models);
  970. // Go over each model
  971. for(auto &model : p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models)
  972. {
  973. // Make sure the number of meshes is not bigger than any of the property arrays
  974. if( model.m_numOfMeshes <= model.m_meshMaterials.size() &&
  975. model.m_numOfMeshes <= model.m_meshMaterialsScale.size() &&
  976. model.m_numOfMeshes <= model.m_alphaThreshold.size() &&
  977. model.m_numOfMeshes <= model.m_heightScale.size() &&
  978. model.m_numOfMeshes <= model.m_active.size() &&
  979. model.m_numOfMeshes <= model.m_present.size())
  980. {
  981. auto &modelPropertyArrayEntry = modelsPropertySet.addPropertySet(Properties::ArrayEntry);
  982. // Add model data
  983. modelPropertyArrayEntry.addProperty(Properties::PropertyID::Filename, model.m_modelName);
  984. auto &meshesPropertySet = modelPropertyArrayEntry.addPropertySet(Properties::PropertyID::Meshes);
  985. // Go over each mesh
  986. for(decltype(model.m_numOfMeshes) i = 0; i < model.m_numOfMeshes; i++)
  987. {
  988. // Make sure the mesh data is present
  989. if(model.m_present[i])
  990. {
  991. auto &meshPropertyArrayEntry = meshesPropertySet.addPropertySet(Properties::ArrayEntry);
  992. // Add mesh data
  993. meshPropertyArrayEntry.addProperty(Properties::PropertyID::Index, (int)i);
  994. meshPropertyArrayEntry.addProperty(Properties::PropertyID::Active, model.m_active[i]);
  995. meshPropertyArrayEntry.addProperty(Properties::PropertyID::AlphaThreshold, model.m_alphaThreshold[i]);
  996. meshPropertyArrayEntry.addProperty(Properties::PropertyID::EmissiveIntensity, model.m_emissiveIntensity[i]);
  997. meshPropertyArrayEntry.addProperty(Properties::PropertyID::HeightScale, model.m_heightScale[i]);
  998. switch(model.m_textureWrapMode[i])
  999. {
  1000. case TextureWrapType::TextureWrapType_ClampToBorder:
  1001. meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::ClampToBorder);
  1002. break;
  1003. case TextureWrapType::TextureWrapType_ClampToEdge:
  1004. meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::ClampToEdge);
  1005. break;
  1006. case TextureWrapType::TextureWrapType_MirroredClampToEdge:
  1007. meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::MirroredClampToEdge);
  1008. break;
  1009. case TextureWrapType::TextureWrapType_MirroredRepeat:
  1010. meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::MirroredRepeat);
  1011. break;
  1012. case TextureWrapType::TextureWrapType_Repeat:
  1013. meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::Repeat);
  1014. break;
  1015. }
  1016. auto &materialsPropertySet = meshPropertyArrayEntry.addPropertySet(Properties::Materials);
  1017. // Go over each material
  1018. for(unsigned int materialType = 0; materialType < MaterialType::MaterialType_NumOfTypes; materialType++)
  1019. {
  1020. // Make sure the material filename is not empty
  1021. if(!model.m_meshMaterials[i][materialType].empty())
  1022. {
  1023. Properties::PropertyID materialPropertyID = Properties::Null;
  1024. // Convert MaterialType to PropertyID
  1025. switch(materialType)
  1026. {
  1027. case MaterialType_Diffuse:
  1028. materialPropertyID = Properties::Diffuse;
  1029. break;
  1030. case MaterialType_Normal:
  1031. materialPropertyID = Properties::Normal;
  1032. break;
  1033. case MaterialType_Emissive:
  1034. materialPropertyID = Properties::Emissive;
  1035. break;
  1036. case MaterialType_Combined:
  1037. materialPropertyID = Properties::RMHAO;
  1038. break;
  1039. }
  1040. auto &materialPropertySet = materialsPropertySet.addPropertySet(materialPropertyID);
  1041. // Add material data
  1042. materialPropertySet.addProperty(Properties::PropertyID::Filename, model.m_meshMaterials[i][materialType]);
  1043. materialPropertySet.addProperty(Properties::PropertyID::TextureScale, model.m_meshMaterialsScale[i][materialType]);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. else
  1050. {
  1051. ErrHandlerLoc::get().log(ErrorCode::Number_of_meshes_missmatch, ErrorSource::Source_SceneLoader, model.m_modelName);
  1052. }
  1053. }
  1054. }
  1055. // Export ShaderComponent
  1056. if(p_constructionInfo.m_shaderConstructionInfo != nullptr)
  1057. {
  1058. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ShaderComponent);
  1059. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_shaderConstructionInfo->m_active);
  1060. // Add shader data, making sure isn't not empty before adding it
  1061. if(!p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename.empty())
  1062. componentPropertySet.addProperty(Properties::PropertyID::FragmentShader, p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename);
  1063. if(!p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename.empty())
  1064. componentPropertySet.addProperty(Properties::PropertyID::VertexShader, p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename);
  1065. if(!p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename.empty())
  1066. componentPropertySet.addProperty(Properties::PropertyID::GeometryShader, p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename);
  1067. }
  1068. }
  1069. }
  1070. void SceneLoader::exportToProperties(const GUIComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  1071. {
  1072. if(p_constructionInfo.m_guiSequenceConstructionInfo != nullptr)
  1073. {
  1074. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::GUI);
  1075. // Export GUISequenceComponent
  1076. if(p_constructionInfo.m_guiSequenceConstructionInfo != nullptr)
  1077. {
  1078. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::GUISequenceComponent);
  1079. // Add GUI sequence data
  1080. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_guiSequenceConstructionInfo->m_active);
  1081. componentPropertySet.addProperty(Properties::PropertyID::Static, p_constructionInfo.m_guiSequenceConstructionInfo->m_staticSequence);
  1082. }
  1083. }
  1084. }
  1085. void SceneLoader::exportToProperties(const PhysicsComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  1086. {
  1087. if(p_constructionInfo.m_rigidBodyConstructionInfo != nullptr)
  1088. {
  1089. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Physics);
  1090. // Export RigidBodyComponent
  1091. if(p_constructionInfo.m_rigidBodyConstructionInfo != nullptr)
  1092. {
  1093. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::RigidBodyComponent);
  1094. // Add rigid body data
  1095. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_rigidBodyConstructionInfo->m_active);
  1096. componentPropertySet.addProperty(Properties::PropertyID::Friction, p_constructionInfo.m_rigidBodyConstructionInfo->m_friction);
  1097. componentPropertySet.addProperty(Properties::PropertyID::Mass, p_constructionInfo.m_rigidBodyConstructionInfo->m_mass);
  1098. componentPropertySet.addProperty(Properties::PropertyID::Restitution, p_constructionInfo.m_rigidBodyConstructionInfo->m_restitution);
  1099. componentPropertySet.addProperty(Properties::PropertyID::Kinematic, p_constructionInfo.m_rigidBodyConstructionInfo->m_kinematic);
  1100. // Convert CollisionShapeType to PropertyID
  1101. Properties::PropertyID collisionShapeType = Properties::Null;
  1102. switch(p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType)
  1103. {
  1104. case RigidBodyComponent::CollisionShapeType_Box:
  1105. collisionShapeType = Properties::Box;
  1106. break;
  1107. case RigidBodyComponent::CollisionShapeType_Capsule:
  1108. collisionShapeType = Properties::Capsule;
  1109. break;
  1110. case RigidBodyComponent::CollisionShapeType_Cone:
  1111. collisionShapeType = Properties::Cone;
  1112. break;
  1113. case RigidBodyComponent::CollisionShapeType_ConvexHull:
  1114. collisionShapeType = Properties::ConvexHull;
  1115. break;
  1116. case RigidBodyComponent::CollisionShapeType_Cylinder:
  1117. collisionShapeType = Properties::Cylinder;
  1118. break;
  1119. case RigidBodyComponent::CollisionShapeType_Sphere:
  1120. collisionShapeType = Properties::Sphere;
  1121. break;
  1122. }
  1123. // Add collision shape data, if it's valid
  1124. if(collisionShapeType != Properties::Null)
  1125. {
  1126. auto &collisionShapePropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::CollisionShape);
  1127. collisionShapePropertySet.addProperty(Properties::PropertyID::Type, collisionShapeType);
  1128. collisionShapePropertySet.addProperty(Properties::PropertyID::Size, p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. void SceneLoader::exportToProperties(const ScriptComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  1134. {
  1135. if(p_constructionInfo.m_luaConstructionInfo != nullptr)
  1136. {
  1137. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Script);
  1138. // Export LuaComponent
  1139. if(p_constructionInfo.m_luaConstructionInfo != nullptr)
  1140. {
  1141. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::LuaComponent);
  1142. // Add LUA component data
  1143. if(!p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename.empty())
  1144. {
  1145. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_luaConstructionInfo->m_active);
  1146. componentPropertySet.addProperty(Properties::PropertyID::Filename, p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename);
  1147. if(!p_constructionInfo.m_luaConstructionInfo->m_variables.empty())
  1148. {
  1149. auto &variablesPropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::Variables);
  1150. // Go over each variable
  1151. for(auto &variable : p_constructionInfo.m_luaConstructionInfo->m_variables)
  1152. {
  1153. // Make sure variable type is valid
  1154. if(variable.second.getVariableType() != Property::PropertyVariableType::Type_null)
  1155. {
  1156. auto &variableArrayEntry = variablesPropertySet.addPropertySet(Properties::PropertyID::ArrayEntry);
  1157. // Add variable data
  1158. variableArrayEntry.addProperty(Properties::PropertyID::Name, variable.first);
  1159. // Add the appropriate data based on the variable's data type
  1160. switch(variable.second.getVariableType())
  1161. {
  1162. case Property::PropertyVariableType::Type_bool:
  1163. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getBool());
  1164. break;
  1165. case Property::PropertyVariableType::Type_int:
  1166. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getInt());
  1167. break;
  1168. case Property::PropertyVariableType::Type_float:
  1169. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getFloat());
  1170. break;
  1171. case Property::PropertyVariableType::Type_double:
  1172. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getDouble());
  1173. break;
  1174. case Property::PropertyVariableType::Type_vec2i:
  1175. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec2i());
  1176. break;
  1177. case Property::PropertyVariableType::Type_vec2f:
  1178. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec2f());
  1179. break;
  1180. case Property::PropertyVariableType::Type_vec3f:
  1181. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec3f());
  1182. break;
  1183. case Property::PropertyVariableType::Type_vec4f:
  1184. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec4f());
  1185. break;
  1186. case Property::PropertyVariableType::Type_string:
  1187. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getString());
  1188. break;
  1189. case Property::PropertyVariableType::Type_propertyID:
  1190. variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getID());
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. std::string m_luaScriptFilename;
  1200. std::vector<std::pair<std::string, Property>> m_variables;
  1201. }
  1202. void SceneLoader::exportToProperties(const WorldComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
  1203. {
  1204. if( p_constructionInfo.m_objectMaterialConstructionInfo != nullptr ||
  1205. p_constructionInfo.m_spatialConstructionInfo != nullptr)
  1206. {
  1207. auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::World);
  1208. // Export ObjectMaterialComponent
  1209. if(p_constructionInfo.m_objectMaterialConstructionInfo != nullptr)
  1210. {
  1211. // Convert ObjectMaterialType to PropertyID
  1212. Properties::PropertyID objectMaterialType = Properties::Null;
  1213. switch(p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType)
  1214. {
  1215. case Concrete:
  1216. objectMaterialType = Properties::Concrete;
  1217. break;
  1218. case Glass:
  1219. objectMaterialType = Properties::Glass;
  1220. break;
  1221. case Metal:
  1222. objectMaterialType = Properties::Metal;
  1223. break;
  1224. case Plastic:
  1225. objectMaterialType = Properties::Plastic;
  1226. break;
  1227. case Rock:
  1228. objectMaterialType = Properties::Rock;
  1229. break;
  1230. case Rubber:
  1231. objectMaterialType = Properties::Rubber;
  1232. break;
  1233. case Wood:
  1234. objectMaterialType = Properties::Wood;
  1235. break;
  1236. }
  1237. // Add object material data, if it's valid
  1238. if(objectMaterialType != Properties::Null)
  1239. {
  1240. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ObjectMaterialComponent);
  1241. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_objectMaterialConstructionInfo->m_active);
  1242. componentPropertySet.addProperty(Properties::PropertyID::Type, objectMaterialType);
  1243. }
  1244. }
  1245. // Export SpatialComponent
  1246. if(p_constructionInfo.m_spatialConstructionInfo != nullptr)
  1247. {
  1248. auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SpatialComponent);
  1249. // Add spatial data
  1250. componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_spatialConstructionInfo->m_active);
  1251. componentPropertySet.addProperty(Properties::PropertyID::LocalPosition, p_constructionInfo.m_spatialConstructionInfo->m_localPosition);
  1252. componentPropertySet.addProperty(Properties::PropertyID::LocalRotation, p_constructionInfo.m_spatialConstructionInfo->m_localRotationEuler);
  1253. componentPropertySet.addProperty(Properties::PropertyID::LocalRotationQuaternion, Math::toGlmVec4(p_constructionInfo.m_spatialConstructionInfo->m_localRotationQuaternion));
  1254. componentPropertySet.addProperty(Properties::PropertyID::LocalScale, p_constructionInfo.m_spatialConstructionInfo->m_localScale);
  1255. }
  1256. }
  1257. }