| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490 |
- #include <utility>
- #include <vector>
- #include "ComponentConstructorInfo.h"
- #include "PropertyLoader.h"
- #include "SceneLoader.h"
- SceneLoader::SceneLoader()
- {
- m_changeController = nullptr;
- m_loadInBackground = false;
- for(int i = 0; i < Systems::NumberOfSystems; i++)
- m_systemScenes[i] = g_nullSystemBase.createScene(this, EngineStateType::EngineStateType_Default);
- }
- SceneLoader::~SceneLoader()
- {
- }
- ErrorCode SceneLoader::loadFromProperties(const PropertySet &p_sceneProperties)
- {
- ErrorCode returnError = ErrorCode::Success;
- EntitiesConstructionInfo constructionInfo;
- // Get systems property set
- auto &systemProperties = p_sceneProperties.getPropertySetByID(Properties::Systems);
- // Iterate over all systems scenes
- for(int sysIndex = 0; sysIndex < Systems::NumberOfSystems; sysIndex++)
- {
- // Create an empty property set, in case there is none in the loaded file, because a system scene setup must be called either way
- PropertySet scenePropertySet;
- PropertySet systemropertySet;
- // Iterate over each system property set
- for(decltype(systemProperties.getNumPropertySets()) propIndex = 0, propSize = systemProperties.getNumPropertySets(); propIndex < propSize; propIndex++)
- {
- // If the system scene property matches in the loaded file, retrieve it so it can be passed to the corresponding scene
- if(Systems::SystemNames[m_systemScenes[sysIndex]->getSystemType()] == GetString(systemProperties.getPropertySetUnsafe(propIndex).getPropertyID()))
- {
- scenePropertySet = systemProperties.getPropertySetUnsafe(propIndex).getPropertySetByID(Properties::Scene);
- systemropertySet = systemProperties.getPropertySetUnsafe(propIndex).getPropertySetByID(Properties::System);
- }
- }
- // Pass the scene and system propertySet parameters
- m_systemScenes[sysIndex]->getSystem()->setup(systemropertySet);
- m_systemScenes[sysIndex]->setup(scenePropertySet);
- }
- // Get Game Objects
- auto &gameObjects = p_sceneProperties.getPropertySetByID(Properties::GameObject);
- if(gameObjects)
- {
- // Reserve enough room for all the game objects
- constructionInfo.resize(gameObjects.getNumPropertySets());
- // Iterate over all game objects
- for(decltype(gameObjects.getNumPropertySets()) objIndex = 0, objSize = gameObjects.getNumPropertySets(); objIndex < objSize; objIndex++)
- {
- // Import the game object data from PropertySets to EntitiesConstructionInfo
- importFromProperties(constructionInfo[objIndex], gameObjects.getPropertySetUnsafe(objIndex));
- }
- // Get the world scene required for creating entities
- WorldScene *worldScene = static_cast<WorldScene *>(m_systemScenes[Systems::World]);
- // Go over each entity and create it
- for(decltype(constructionInfo.size()) i = 0, size = constructionInfo.size(); i < size; i++)
- {
- worldScene->createEntity(constructionInfo[i], false);
- }
- }
- else
- {
- // GameObject property set is missing
- returnError = ErrorCode::GameObjects_missing;
- ErrHandlerLoc().get().log(ErrorCode::GameObjects_missing, ErrorSource::Source_SceneLoader);
- }
- // Check if the scene should be loaded in background
- if(p_sceneProperties.getPropertyByID(Properties::LoadInBackground).getBool())
- {
- m_loadInBackground = true;
- // Start loading in background threads in all scenes
- for(int i = 0; i < Systems::NumberOfSystems; i++)
- m_systemScenes[i]->loadInBackground();
- }
- else
- {
- m_loadInBackground = false;
- // Preload all scenes sequentially
- for(int i = 0; i < Systems::NumberOfSystems; i++)
- m_systemScenes[i]->preload();
- }
- // Make sure to clear the memory of contructionInfo
- for(decltype(constructionInfo.size()) i = 0, size = constructionInfo.size(); i < size; i++)
- constructionInfo[i].deleteConstructionInfo();
- return returnError;
- }
- ErrorCode SceneLoader::loadFromFile(const std::string &p_filename)
- {
- ErrorCode returnError = ErrorCode::Success;
- m_filename = p_filename;
- EntitiesConstructionInfo constructionInfo;
- // Load properties from file
- PropertyLoader loadedProperties(Config::filepathVar().map_path + p_filename);
- returnError = loadedProperties.loadFromFile();
- // Check if loading was successful, return an error, if not
- if(returnError != ErrorCode::Success)
- {
- ErrHandlerLoc().get().log(returnError, ErrorSource::Source_SceneLoader);
- }
- else
- {
- returnError = loadFromProperties(loadedProperties.getPropertySet());
- }
- return returnError;
- }
- ErrorCode SceneLoader::saveToFile(const std::string p_filename)
- {
- std::string filename;
- if(!p_filename.empty())
- filename = Config::filepathVar().map_path + p_filename;
- else
- filename = m_filename;
- if(!filename.empty())
- {
- // Get the world scene required for getting the entity registry
- WorldScene *worldScene = static_cast<WorldScene *>(m_systemScenes[Systems::World]);
- // Get the entity registry
- auto &entityRegistry = worldScene->getEntityRegistry();
- // Add root property set for the whole file
- PropertySet rootPropertySet(Properties::Default);
- // Add root property set game objects
- auto &gameObjects = rootPropertySet.addPropertySet(Properties::GameObject);
- // An array holding all of the entities
- std::vector<EntityID> allEntities;
- // Add all entities to the array
- for(auto entity : entityRegistry.view<EntityID>())
- allEntities.push_back(entity);
- // Sort the entities so they are written to file in order
- std::sort(allEntities.begin(), allEntities.end());
- // Iterate every entity
- for(auto &entity : allEntities)
- {
- // Create an array entry for the entity
- auto &gameObjectEntry = gameObjects.addPropertySet(Properties::ArrayEntry);
- // Export the entity to the Construction Info
- ComponentsConstructionInfo constructionInfo;
- worldScene->exportEntity(entity, constructionInfo);
- // Export the Construction Info to the Property Set
- exportToProperties(constructionInfo, gameObjectEntry);
- }
- // Add scene loaded properties
- rootPropertySet.addProperty(Properties::LoadInBackground, m_loadInBackground);
- // Add root property set for systems
- auto &rootSystemsPropertySet = rootPropertySet.addPropertySet(Properties::Systems);
- // Add each system's properties
- for(size_t systemType = 0; systemType < Systems::NumberOfSystems; systemType++)
- {
- // Convert TypeID to PropertyID
- Properties::PropertyID systemTypeProperty = Properties::Null;
- switch(systemType)
- {
- case Systems::TypeID::Audio:
- systemTypeProperty = Properties::Audio;
- break;
- case Systems::TypeID::Graphics:
- systemTypeProperty = Properties::Graphics;
- break;
- case Systems::TypeID::GUI:
- systemTypeProperty = Properties::GUI;
- break;
- case Systems::TypeID::Physics:
- systemTypeProperty = Properties::Physics;
- break;
- case Systems::TypeID::Script:
- systemTypeProperty = Properties::Script;
- break;
- case Systems::TypeID::World:
- systemTypeProperty = Properties::World;
- break;
- }
- // Add system type entry
- auto &systemPropertyIDentry = rootSystemsPropertySet.addPropertySet(systemTypeProperty);
- // Add scene and system settings
- m_systemScenes[systemType]->exportSetup(systemPropertyIDentry.addPropertySet(Properties::Scene));
- m_systemScenes[systemType]->getSystem()->exportSetup(systemPropertyIDentry.addPropertySet(Properties::System));
- }
- // Save properties to a file
- PropertyLoader savedProperties(filename);
- ErrorCode loaderError = savedProperties.saveToFile(rootPropertySet);
- // Check if loading was successful, return an error, if not
- if(loaderError != ErrorCode::Success)
- return loaderError;
- ErrHandlerLoc().get().log(ErrorType::Info, ErrorSource::Source_PropertyLoader, "File has been exported: " + filename);
- }
- // TODO ERROR empty filename
- return ErrorCode::Failure;
- }
- ErrorCode SceneLoader::importPrefab(ComponentsConstructionInfo &p_constructionInfo, const std::string &p_filename, const bool p_forceReload)
- {
- ErrorCode returnError = ErrorCode::Success;
- // Check if the given filename isn't empty
- if(!p_filename.empty())
- {
- // Search for the given prefab (it might have been loaded before, already)
- auto prefabIterator = m_prefabs.find(p_filename);
- // If the prefab was already imported and exists in the map, use it
- if(prefabIterator != m_prefabs.end())
- {
- if(p_forceReload)
- returnError = importFromFile(p_constructionInfo, Config::filepathVar().prefab_path + p_filename);
- else
- p_constructionInfo.completeCopy(prefabIterator->second);
- }
- else // If the prefab doesn't exist in the map, import it
- {
- // Make sure calls from other threads are locked, while current call is in progress
- // This is needed as the prefab that is being requested might be currently being imported
- // Mutex prevents duplicate prefabs being loaded, and same data being changed.
- SpinWait::Lock lock(m_mutex);
- // Search for the prefab again, as it might have been imported from another thread call before mutex lock was released
- auto prefabIteratorNew = m_prefabs.find(p_filename);
- if(prefabIteratorNew != m_prefabs.end())
- {
- if(p_forceReload)
- returnError = importFromFile(p_constructionInfo, Config::filepathVar().prefab_path + p_filename);
- else
- p_constructionInfo.completeCopy(prefabIterator->second);
- }
- else
- {
- // Load properties from file
- PropertyLoader loadedProperties(Config::filepathVar().prefab_path + p_filename);
- returnError = loadedProperties.loadFromFile();
- if(returnError == ErrorCode::Success)
- {
- // Insert a new prefab into the map
- ComponentsConstructionInfo &constructionInfo = m_prefabs.try_emplace(p_filename).first->second;
- // Populate the newly imported prefab
- importFromProperties(constructionInfo, loadedProperties.getPropertySet());
- p_constructionInfo.completeCopy(constructionInfo);
- }
- }
- }
- }
- else
- returnError = ErrorCode::Filename_empty;
- return returnError;
- }
- ErrorCode SceneLoader::importFromFile(ComponentsConstructionInfo &p_constructionInfo, const std::string &p_filename)
- {
- ErrorCode returnError = ErrorCode::Success;
- // Load properties from file
- PropertyLoader loadedProperties(p_filename);
- returnError = loadedProperties.loadFromFile();
- if(returnError == ErrorCode::Success)
- importFromProperties(p_constructionInfo, loadedProperties.getPropertySet());
- return returnError;
- }
- void SceneLoader::importFromProperties(ComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties)
- {
- // Load Prefab first
- auto &prefabProperty = p_properties.getPropertyByID(Properties::Prefab);
- if(prefabProperty)
- {
- std::string prefabName = prefabProperty.getString();
- importPrefab(p_constructionInfo, prefabName);
- p_constructionInfo.m_prefab = prefabName;
- }
- // Variable for the object name
- std::string name;
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::ID:
- {
- // Get the desired ID of the entity
- p_constructionInfo.m_id = (EntityID)p_properties[i].getInt();
- }
- break;
- case Properties::Name:
- {
- // Get the entity name
- name = p_properties[i].getString();
- }
- break;
- case Properties::Parent:
- {
- // Get the entity ID if the parent object
- p_constructionInfo.m_parent = (EntityID)p_properties[i].getInt();
- }
- break;
- }
- }
- // If the name property is missing, generate a unique name based on the entity ID
- if(name.empty())
- if(p_constructionInfo.m_name.empty())
- name = GetString(Properties::GameObject) + Utilities::toString(p_constructionInfo.m_id);
- else
- name = p_constructionInfo.m_name + Utilities::toString(p_constructionInfo.m_id);
- // Make sure to assign values after the Prefab has been imported (if there was one)
- p_constructionInfo.m_name = name;
- // Load audio components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::Audio);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_audioComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- // Load graphics components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::Graphics);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_graphicsComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- // Load GUI components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::GUI);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_guiComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- // Load physics components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::Physics);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_physicsComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- // Load script components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::Script);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_scriptComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- // Load world components
- {
- auto &sceneProperty = p_properties.getPropertySetByID(Properties::World);
- if(sceneProperty)
- {
- for(decltype(sceneProperty.getNumPropertySets()) i = 0, size = sceneProperty.getNumPropertySets(); i < size; i++)
- {
- importFromProperties(p_constructionInfo.m_worldComponents, sceneProperty.getPropertySet(i), name);
- }
- }
- }
- }
- void SceneLoader::importFromProperties(AudioComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::SoundComponent:
- {
- if(p_constructionInfo.m_soundConstructionInfo == nullptr)
- p_constructionInfo.m_soundConstructionInfo = new SoundComponent::SoundComponentConstructionInfo();
- p_constructionInfo.m_soundConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SoundComponent);
- // Get the sound filename
- auto const &filename = p_properties.getPropertyByID(Properties::Filename).getString();
- if(!filename.empty())
- {
- // Get the sound type
- auto const &type = p_properties.getPropertyByID(Properties::Type).getID();
- // Load values based on the type of sound
- switch(type)
- {
- case Properties::Ambient:
- p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_Ambient;
- p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
- p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
- p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
- p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
- p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
- break;
- case Properties::Music:
- p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_Music;
- p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
- p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
- p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
- p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
- p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
- break;
- case Properties::SoundEffect:
- p_constructionInfo.m_soundConstructionInfo->m_soundType = SoundComponent::SoundType::SoundType_SoundEffect;
- p_properties.getValueByID(Properties::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
- p_properties.getValueByID(Properties::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
- p_properties.getValueByID(Properties::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
- p_properties.getValueByID(Properties::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
- p_constructionInfo.m_soundConstructionInfo->m_soundFilename = filename;
- break;
- default:
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_SoundComponent);
- delete p_constructionInfo.m_soundConstructionInfo;
- p_constructionInfo.m_soundConstructionInfo = nullptr;
- break;
- }
- }
- else
- {
- ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SoundComponent);
- delete p_constructionInfo.m_soundConstructionInfo;
- p_constructionInfo.m_soundConstructionInfo = nullptr;
- }
- }
- break;
- case Properties::PropertyID::SoundListenerComponent:
- {
- if(p_constructionInfo.m_soundListenerConstructionInfo == nullptr)
- p_constructionInfo.m_soundListenerConstructionInfo = new SoundListenerComponent::SoundListenerComponentConstructionInfo();
- p_constructionInfo.m_soundListenerConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SoundListenerComponent);
- p_properties.getValueByID(Properties::PropertyID::ID, p_constructionInfo.m_soundListenerConstructionInfo->m_listenerID);
- p_properties.getValueByID(Properties::PropertyID::Active, p_constructionInfo.m_soundListenerConstructionInfo->m_active);
- }
- break;
- }
- }
- }
- void SceneLoader::importFromProperties(GraphicsComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::CameraComponent:
- {
- if(p_constructionInfo.m_cameraConstructionInfo == nullptr)
- p_constructionInfo.m_cameraConstructionInfo = new CameraComponent::CameraComponentConstructionInfo();
- p_constructionInfo.m_cameraConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::CameraComponent);
- }
- break;
- case Properties::PropertyID::LightComponent:
- {
- if(p_constructionInfo.m_lightConstructionInfo == nullptr)
- p_constructionInfo.m_lightConstructionInfo = new LightComponent::LightComponentConstructionInfo();
- p_constructionInfo.m_lightConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::LightComponent);
- // Get the light type
- auto const &type = p_properties.getPropertyByID(Properties::Type).getID();
- // Load values based on the type of light
- switch(type)
- {
- case Properties::DirectionalLight:
- p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_directional;
- p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- break;
- case Properties::PointLight:
- p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_point;
- p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- break;
- case Properties::SpotLight:
- p_constructionInfo.m_lightConstructionInfo->m_lightComponentType = LightComponent::LightComponentType::LightComponentType_spot;
- p_properties.getValueByID(Properties::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- p_properties.getValueByID(Properties::CutoffAngle, p_constructionInfo.m_lightConstructionInfo->m_cutoffAngle);
- p_properties.getValueByID(Properties::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- break;
- default:
- ErrHandlerLoc().get().log(ErrorType::Warning, ErrorSource::Source_LightComponent, p_name + " - missing \'Type\' identifier");
- delete p_constructionInfo.m_lightConstructionInfo;
- p_constructionInfo.m_lightConstructionInfo = nullptr;
- break;
- }
- }
- break;
- case Properties::PropertyID::ModelComponent:
- {
- if(p_constructionInfo.m_modelConstructionInfo == nullptr)
- p_constructionInfo.m_modelConstructionInfo = new ModelComponent::ModelComponentConstructionInfo();
- p_constructionInfo.m_modelConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ModelComponent);
- bool modelDataPresent = false;
- auto &modelsProperty = p_properties.getPropertySetByID(Properties::Models);
- if(modelsProperty)
- {
- if(modelsProperty.getNumPropertySets() > 0)
- p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.clear();
- // Loop over each model entry in the node
- for(decltype(modelsProperty.getNumPropertySets()) iModel = 0, numModels = modelsProperty.getNumPropertySets(); iModel < numModels; iModel++)
- {
- // Get model filename
- auto modelName = modelsProperty.getPropertySet(iModel).getPropertyByID(Properties::Filename).getString();
- // Continue only of the model filename is not empty
- if(!modelName.empty())
- {
- modelDataPresent = true;
- // Add a new model data entry, and get a reference to it
- p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.push_back(ModelComponent::MeshProperties());
- auto &newModelEntry = p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models.back();
- // Assign the model filename
- newModelEntry.m_modelName = modelName;
- // Get meshes property
- auto &meshesProperty = modelsProperty.getPropertySet(iModel).getPropertySetByID(Properties::Meshes);
- // Check if the meshes array node is present;
- // If it is present, only add the meshes included in the meshes node
- // If it is not present, add all the meshes included in the model
- if(meshesProperty)
- {
- if(meshesProperty.getNumPropertySets() > 0)
- {
- // Loop over each mesh entry in the model node
- for(decltype(meshesProperty.getNumPropertySets()) iMesh = 0, numMeshes = meshesProperty.getNumPropertySets(); iMesh < numMeshes; iMesh++)
- {
- // Try to get the mesh index property node and check if it is present
- auto &meshIndexProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::Index);
- if(meshIndexProperty)
- {
- // Get the mesh index, check if it is valid and within the range of mesh array that was loaded from the model
- const int meshDataIndex = meshIndexProperty.getInt();
- // Make sure the meshMaterials vector can fit the given mesh index
- if(meshDataIndex >= newModelEntry.m_meshMaterials.size())
- {
- newModelEntry.resize(meshDataIndex + 1);
- newModelEntry.m_present[meshDataIndex] = true;
- }
- // Get the active flag, if it is present
- if(auto activeProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::Active); activeProperty)
- newModelEntry.m_active[meshDataIndex] = activeProperty.getBool();
- // Get material alpha threshold value, if it is present
- if(auto alphaThresholdProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::AlphaThreshold); alphaThresholdProperty)
- newModelEntry.m_alphaThreshold[meshDataIndex] = alphaThresholdProperty.getFloat();
- // Get emissive intensity, if it is present
- if(auto emissiveIntensityProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::EmissiveIntensity); emissiveIntensityProperty)
- newModelEntry.m_emissiveIntensity[meshDataIndex] = emissiveIntensityProperty.getFloat();
- // Get material height scale value, if it is present
- if(auto heightScaleProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::HeightScale); heightScaleProperty)
- newModelEntry.m_heightScale[meshDataIndex] = heightScaleProperty.getFloat();
- // Get material wrap mode
- if(auto wrapModeProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::WrapMode); wrapModeProperty)
- {
- switch(wrapModeProperty.getID())
- {
- case Properties::ClampToBorder:
- newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_ClampToBorder;
- break;
- case Properties::ClampToEdge:
- newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_ClampToEdge;
- break;
- case Properties::MirroredClampToEdge:
- newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_MirroredClampToEdge;
- break;
- case Properties::MirroredRepeat:
- newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_MirroredRepeat;
- break;
- case Properties::Repeat:
- default:
- newModelEntry.m_textureWrapMode[meshDataIndex] = TextureWrapType::TextureWrapType_Repeat;
- break;
- }
- }
- // Get material properties
- auto materialsProperty = meshesProperty.getPropertySet(iMesh).getPropertySetByID(Properties::Materials);
- // Define material data and material properties
- MaterialData materials[MaterialType::MaterialType_NumOfTypes];
- PropertySet materialProperties[MaterialType::MaterialType_NumOfTypes] =
- {
- materialsProperty.getPropertySetByID(Properties::Diffuse),
- materialsProperty.getPropertySetByID(Properties::Normal),
- materialsProperty.getPropertySetByID(Properties::Emissive),
- materialsProperty.getPropertySetByID(Properties::RMHAO)
- };
- // Go over each material type
- for(unsigned int iMatType = 0; iMatType < MaterialType::MaterialType_NumOfTypes; iMatType++)
- {
- // Check if an entry for the current material type was present within the properties
- if(materialProperties[iMatType])
- {
- // Get texture filename property, check if it is valid
- auto filenameProperty = materialProperties[iMatType].getPropertyByID(Properties::Filename);
- if(filenameProperty.isVariableTypeString())
- {
- // Get texture filename string, check if it is valid
- newModelEntry.m_meshMaterials[meshDataIndex][iMatType] = filenameProperty.getString();
- }
- // Get texture scale property, check if it is valid
- auto scaleProperty = materialProperties[iMatType].getPropertyByID(Properties::TextureScale);
- if(scaleProperty)
- newModelEntry.m_meshMaterialsScale[meshDataIndex][iMatType] = scaleProperty.getVec2f();
- }
- }
- }
- }
- }
- }
- }
- }
- }
- if(p_properties.getNumPropertySets() == 0 || !modelDataPresent)
- {
- ErrHandlerLoc().get().log(ErrorType::Info, ErrorSource::Source_ModelComponent, p_name + " - missing model data");
- delete p_constructionInfo.m_modelConstructionInfo;
- p_constructionInfo.m_modelConstructionInfo = nullptr;
- }
- }
- break;
- case Properties::PropertyID::ShaderComponent:
- {
- if(p_constructionInfo.m_shaderConstructionInfo == nullptr)
- p_constructionInfo.m_shaderConstructionInfo = new ShaderComponent::ShaderComponentConstructionInfo();
- p_constructionInfo.m_shaderConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ShaderComponent);
- // Get nodes for different shader types
- auto geometryShaderNode = p_properties.getPropertyByID(Properties::GeometryShader);
- auto vertexShaderNode = p_properties.getPropertyByID(Properties::VertexShader);
- auto fragmentShaderNode = p_properties.getPropertyByID(Properties::FragmentShader);
- if(geometryShaderNode)
- p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename = geometryShaderNode.getString();
- if(vertexShaderNode)
- p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename = vertexShaderNode.getString();
- if(fragmentShaderNode)
- p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename = fragmentShaderNode.getString();
- }
- break;
- }
- }
- }
- void SceneLoader::importFromProperties(GUIComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::GUISequenceComponent:
- {
- if(p_constructionInfo.m_guiSequenceConstructionInfo == nullptr)
- p_constructionInfo.m_guiSequenceConstructionInfo = new GUISequenceComponent::GUISequenceComponentConstructionInfo();
- p_constructionInfo.m_guiSequenceConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::GUISequenceComponent);
- }
- break;
- }
- }
- }
- void SceneLoader::importFromProperties(PhysicsComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::RigidBodyComponent:
- {
- if(p_constructionInfo.m_rigidBodyConstructionInfo == nullptr)
- p_constructionInfo.m_rigidBodyConstructionInfo = new RigidBodyComponent::RigidBodyComponentConstructionInfo();
- p_constructionInfo.m_rigidBodyConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::RigidBodyComponent);
- // --------------------
- // Load collision shape
- // --------------------
- auto const &collisionShapeProperty = p_properties.getPropertySetByID(Properties::CollisionShape);
- if(collisionShapeProperty)
- {
- // Get the type of the collision shape and load the data based on it
- auto const &typeProperty = collisionShapeProperty.getPropertyByID(Properties::Type);
- if(typeProperty)
- {
- switch(typeProperty.getID())
- {
- case Properties::Box:
- {
- // Get the size property
- auto const &sizeProperty = collisionShapeProperty.getPropertyByID(Properties::Size);
- // 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)
- if(sizeProperty)
- p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize = sizeProperty.getVec3f();
- else
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_size, p_name, ErrorSource::Source_RigidBodyComponent);
- p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType = RigidBodyComponent::CollisionShapeType::CollisionShapeType_Box;
- }
- break;
- case Properties::Sphere:
- {
- // Get the size property
- auto const &radiusProperty = collisionShapeProperty.getPropertyByID(Properties::Radius);
- // If the size was not given, leave it to a default radius of 0.5f (which makes the sphere diameter equal to 1.0)
- if(radiusProperty)
- p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize.x = radiusProperty.getFloat();
- else
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_radius, p_name, ErrorSource::Source_RigidBodyComponent);
- p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType = RigidBodyComponent::CollisionShapeType::CollisionShapeType_Sphere;
- }
- break;
- default:
- // If this is reached, the collision shape type was not valid
- ErrHandlerLoc().get().log(ErrorCode::Collision_invalid, p_name, ErrorSource::Source_RigidBodyComponent);
- break;
- }
- }
- else
- {
- // Missing the Type property entirely
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_RigidBodyComponent);
- }
- }
- // -----------------------------
- // Load individual property data
- // -----------------------------
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::Friction:
- p_constructionInfo.m_rigidBodyConstructionInfo->m_friction = p_properties[i].getFloat();
- break;
- case Properties::Kinematic:
- p_constructionInfo.m_rigidBodyConstructionInfo->m_kinematic = p_properties[i].getBool();
- break;
- case Properties::Mass:
- p_constructionInfo.m_rigidBodyConstructionInfo->m_mass = p_properties[i].getFloat();
- break;
- case Properties::Restitution:
- p_constructionInfo.m_rigidBodyConstructionInfo->m_restitution = p_properties[i].getFloat();
- break;
- case Properties::Velocity:
- p_constructionInfo.m_rigidBodyConstructionInfo->m_linearVelocity = p_properties[i].getVec3f();
- break;
- }
- }
- }
- break;
- }
- }
- }
- void SceneLoader::importFromProperties(ScriptComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::LuaComponent:
- {
- if(p_constructionInfo.m_luaConstructionInfo == nullptr)
- p_constructionInfo.m_luaConstructionInfo = new LuaComponent::LuaComponentConstructionInfo();
- p_constructionInfo.m_luaConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::LuaComponent);
- auto const &luaFilenameProperty = p_properties.getPropertyByID(Properties::Filename);
- auto const &luaVariablesProperty = p_properties.getPropertySetByID(Properties::Variables);
- if(luaFilenameProperty)
- {
- std::string luaFilename = luaFilenameProperty.getString();
- if(!luaFilename.empty())
- {
- p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename = luaFilename;
- if(luaVariablesProperty)
- {
- // Loop over each variable entry in the node
- for(decltype(luaVariablesProperty.getNumPropertySets()) iVariable = 0, numVariables = luaVariablesProperty.getNumPropertySets(); iVariable < numVariables; iVariable++)
- {
- // Add the variable
- p_constructionInfo.m_luaConstructionInfo->m_variables.emplace_back(
- luaVariablesProperty.getPropertySet(iVariable).getPropertyByID(Properties::Name).getString(),
- luaVariablesProperty.getPropertySet(iVariable).getPropertyByID(Properties::Value));
- }
- }
- }
- else
- {
- ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SceneLoader);
- }
- }
- else
- {
- ErrHandlerLoc().get().log(ErrorCode::Property_no_filename, p_name, ErrorSource::Source_SceneLoader);
- }
- }
- break;
- }
- }
- }
- void SceneLoader::importFromProperties(WorldComponentsConstructionInfo &p_constructionInfo, const PropertySet &p_properties, const std::string &p_name)
- {
- // Check if property set node is present
- if(p_properties)
- {
- switch(p_properties.getPropertyID())
- {
- case Properties::PropertyID::ObjectMaterialComponent:
- {
- if(p_constructionInfo.m_objectMaterialConstructionInfo == nullptr)
- p_constructionInfo.m_objectMaterialConstructionInfo = new ObjectMaterialComponent::ObjectMaterialComponentConstructionInfo();
- p_constructionInfo.m_objectMaterialConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::ObjectMaterialComponent);
- // Get the type property representing the object material type
- auto const &typeProperty = p_properties.getPropertyByID(Properties::Type);
- // Check if the property exists
- if(typeProperty)
- {
- // Define the type based on the imported property
- switch(typeProperty.getID())
- {
- case Properties::Concrete:
- default:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Concrete;
- break;
- case Properties::Glass:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Glass;
- break;
- case Properties::Metal:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Metal;
- break;
- case Properties::Plastic:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Plastic;
- break;
- case Properties::Rock:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Rock;
- break;
- case Properties::Rubber:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Rubber;
- break;
- case Properties::Wood:
- p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType = ObjectMaterialType::Wood;
- break;
- // If this is reached, the object material type was not valid
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_ObjectMaterialComponent);
- break;
- }
- }
- else
- {
- // Missing the Type property entirely
- ErrHandlerLoc().get().log(ErrorCode::Property_missing_type, p_name, ErrorSource::Source_ObjectMaterialComponent);
- }
- }
- break;
- case Properties::PropertyID::SpatialComponent:
- {
- if(p_constructionInfo.m_spatialConstructionInfo == nullptr)
- p_constructionInfo.m_spatialConstructionInfo = new SpatialComponent::SpatialComponentConstructionInfo();
- p_constructionInfo.m_spatialConstructionInfo->m_name = p_name + Config::componentVar().component_name_separator + GetString(Properties::PropertyID::SpatialComponent);
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::LocalPosition:
- p_constructionInfo.m_spatialConstructionInfo->m_localPosition = p_properties[i].getVec3f();
- break;
- case Properties::LocalRotation:
- p_constructionInfo.m_spatialConstructionInfo->m_localRotationEuler = p_properties[i].getVec3f();
- break;
- case Properties::LocalRotationQuaternion:
- p_constructionInfo.m_spatialConstructionInfo->m_localRotationQuaternion = Math::toGlmQuat(p_properties[i].getVec4f());
- break;
- case Properties::LocalScale:
- p_constructionInfo.m_spatialConstructionInfo->m_localScale = p_properties[i].getVec3f();
- break;
- }
- }
- }
- break;
- }
- }
- }
- void SceneLoader::exportToProperties(const ComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- p_properties.addProperty(Properties::PropertyID::Name, p_constructionInfo.m_name);
- p_properties.addProperty(Properties::PropertyID::ID, (int)p_constructionInfo.m_id);
- p_properties.addProperty(Properties::PropertyID::Parent, (int)p_constructionInfo.m_parent);
- if(!p_constructionInfo.m_prefab.empty())
- p_properties.addProperty(Properties::PropertyID::Prefab, p_constructionInfo.m_prefab);
- // Export audio components
- exportToProperties(p_constructionInfo.m_audioComponents, p_properties);
- // Export graphics components
- exportToProperties(p_constructionInfo.m_graphicsComponents, p_properties);
- // Export GUI components
- exportToProperties(p_constructionInfo.m_guiComponents, p_properties);
- // Export physics components
- exportToProperties(p_constructionInfo.m_physicsComponents, p_properties);
- // Export script components
- exportToProperties(p_constructionInfo.m_scriptComponents, p_properties);
- // Export world components
- exportToProperties(p_constructionInfo.m_worldComponents, p_properties);
- }
- void SceneLoader::exportToProperties(const AudioComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if( p_constructionInfo.m_soundConstructionInfo != nullptr ||
- p_constructionInfo.m_soundListenerConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Audio);
- // Export SoundComponent
- if(p_constructionInfo.m_soundConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SoundComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_soundConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::Filename, p_constructionInfo.m_soundConstructionInfo->m_soundFilename);
- switch(p_constructionInfo.m_soundConstructionInfo->m_soundType)
- {
- case SoundComponent::SoundType::SoundType_Ambient:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::Ambient);
- break;
- case SoundComponent::SoundType::SoundType_Music:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::Music);
- break;
- case SoundComponent::SoundType::SoundType_SoundEffect:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::SoundEffect);
- break;
- }
- componentPropertySet.addProperty(Properties::PropertyID::Loop, p_constructionInfo.m_soundConstructionInfo->m_loop);
- componentPropertySet.addProperty(Properties::PropertyID::Spatialized, p_constructionInfo.m_soundConstructionInfo->m_spatialized);
- componentPropertySet.addProperty(Properties::PropertyID::StartPlaying, p_constructionInfo.m_soundConstructionInfo->m_startPlaying);
- componentPropertySet.addProperty(Properties::PropertyID::Volume, p_constructionInfo.m_soundConstructionInfo->m_volume);
- }
- // Export SoundListenerComponent
- if(p_constructionInfo.m_soundListenerConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SoundListenerComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_soundListenerConstructionInfo->m_active);
- //componentPropertySet.addProperty(Properties::PropertyID::ListenerID, p_constructionInfo.m_soundListenerConstructionInfo->m_listenerID);
- }
- }
- }
- void SceneLoader::exportToProperties(const GraphicsComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if(p_constructionInfo.m_cameraConstructionInfo != nullptr ||
- p_constructionInfo.m_lightConstructionInfo != nullptr ||
- p_constructionInfo.m_modelConstructionInfo != nullptr ||
- p_constructionInfo.m_shaderConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Graphics);
- // Export CameraComponent
- if(p_constructionInfo.m_cameraConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::CameraComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_cameraConstructionInfo->m_active);
- //componentPropertySet.addProperty(Properties::PropertyID::FOV, p_constructionInfo.m_cameraConstructionInfo->m_fov);
- }
- // Export LightComponent
- if(p_constructionInfo.m_lightConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::LightComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_lightConstructionInfo->m_active);
- switch(p_constructionInfo.m_lightConstructionInfo->m_lightComponentType)
- {
- case LightComponent::LightComponentType::LightComponentType_directional:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::DirectionalLight);
- componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- break;
- case LightComponent::LightComponentType::LightComponentType_point:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::PointLight);
- componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- break;
- case LightComponent::LightComponentType::LightComponentType_spot:
- componentPropertySet.addProperty(Properties::PropertyID::Type, Properties::PropertyID::SpotLight);
- componentPropertySet.addProperty(Properties::PropertyID::Color, p_constructionInfo.m_lightConstructionInfo->m_color);
- componentPropertySet.addProperty(Properties::PropertyID::Intensity, p_constructionInfo.m_lightConstructionInfo->m_intensity);
- componentPropertySet.addProperty(Properties::PropertyID::CutoffAngle, p_constructionInfo.m_lightConstructionInfo->m_cutoffAngle);
- break;
- }
- }
- // Export ModelComponent
- if(p_constructionInfo.m_modelConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ModelComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_modelConstructionInfo->m_active);
- auto &modelsPropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::Models);
- // Go over each model
- for(auto &model : p_constructionInfo.m_modelConstructionInfo->m_modelsProperties.m_models)
- {
- // Make sure the number of meshes is not bigger than any of the property arrays
- if( model.m_numOfMeshes <= model.m_meshMaterials.size() &&
- model.m_numOfMeshes <= model.m_meshMaterialsScale.size() &&
- model.m_numOfMeshes <= model.m_alphaThreshold.size() &&
- model.m_numOfMeshes <= model.m_heightScale.size() &&
- model.m_numOfMeshes <= model.m_active.size() &&
- model.m_numOfMeshes <= model.m_present.size())
- {
- auto &modelPropertyArrayEntry = modelsPropertySet.addPropertySet(Properties::ArrayEntry);
- // Add model data
- modelPropertyArrayEntry.addProperty(Properties::PropertyID::Filename, model.m_modelName);
- auto &meshesPropertySet = modelPropertyArrayEntry.addPropertySet(Properties::PropertyID::Meshes);
- // Go over each mesh
- for(decltype(model.m_numOfMeshes) i = 0; i < model.m_numOfMeshes; i++)
- {
- // Make sure the mesh data is present
- if(model.m_present[i])
- {
- auto &meshPropertyArrayEntry = meshesPropertySet.addPropertySet(Properties::ArrayEntry);
- // Add mesh data
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::Index, (int)i);
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::Active, model.m_active[i]);
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::AlphaThreshold, model.m_alphaThreshold[i]);
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::EmissiveIntensity, model.m_emissiveIntensity[i]);
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::HeightScale, model.m_heightScale[i]);
- switch(model.m_textureWrapMode[i])
- {
- case TextureWrapType::TextureWrapType_ClampToBorder:
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::ClampToBorder);
- break;
- case TextureWrapType::TextureWrapType_ClampToEdge:
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::ClampToEdge);
- break;
- case TextureWrapType::TextureWrapType_MirroredClampToEdge:
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::MirroredClampToEdge);
- break;
- case TextureWrapType::TextureWrapType_MirroredRepeat:
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::MirroredRepeat);
- break;
- case TextureWrapType::TextureWrapType_Repeat:
- meshPropertyArrayEntry.addProperty(Properties::PropertyID::WrapMode, Properties::PropertyID::Repeat);
- break;
- }
- auto &materialsPropertySet = meshPropertyArrayEntry.addPropertySet(Properties::Materials);
- // Go over each material
- for(unsigned int materialType = 0; materialType < MaterialType::MaterialType_NumOfTypes; materialType++)
- {
- // Make sure the material filename is not empty
- if(!model.m_meshMaterials[i][materialType].empty())
- {
- Properties::PropertyID materialPropertyID = Properties::Null;
- // Convert MaterialType to PropertyID
- switch(materialType)
- {
- case MaterialType_Diffuse:
- materialPropertyID = Properties::Diffuse;
- break;
- case MaterialType_Normal:
- materialPropertyID = Properties::Normal;
- break;
- case MaterialType_Emissive:
- materialPropertyID = Properties::Emissive;
- break;
- case MaterialType_Combined:
- materialPropertyID = Properties::RMHAO;
- break;
- }
- auto &materialPropertySet = materialsPropertySet.addPropertySet(materialPropertyID);
- // Add material data
- materialPropertySet.addProperty(Properties::PropertyID::Filename, model.m_meshMaterials[i][materialType]);
- materialPropertySet.addProperty(Properties::PropertyID::TextureScale, model.m_meshMaterialsScale[i][materialType]);
- }
- }
- }
- }
- }
- else
- {
- ErrHandlerLoc::get().log(ErrorCode::Number_of_meshes_missmatch, ErrorSource::Source_SceneLoader, model.m_modelName);
- }
- }
- }
- // Export ShaderComponent
- if(p_constructionInfo.m_shaderConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ShaderComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_shaderConstructionInfo->m_active);
- // Add shader data, making sure isn't not empty before adding it
- if(!p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename.empty())
- componentPropertySet.addProperty(Properties::PropertyID::FragmentShader, p_constructionInfo.m_shaderConstructionInfo->m_fragmentShaderFilename);
- if(!p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename.empty())
- componentPropertySet.addProperty(Properties::PropertyID::VertexShader, p_constructionInfo.m_shaderConstructionInfo->m_vetexShaderFilename);
- if(!p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename.empty())
- componentPropertySet.addProperty(Properties::PropertyID::GeometryShader, p_constructionInfo.m_shaderConstructionInfo->m_geometryShaderFilename);
- }
- }
- }
- void SceneLoader::exportToProperties(const GUIComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if(p_constructionInfo.m_guiSequenceConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::GUI);
- // Export GUISequenceComponent
- if(p_constructionInfo.m_guiSequenceConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::GUISequenceComponent);
- // Add GUI sequence data
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_guiSequenceConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::Static, p_constructionInfo.m_guiSequenceConstructionInfo->m_staticSequence);
- }
- }
- }
- void SceneLoader::exportToProperties(const PhysicsComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if(p_constructionInfo.m_rigidBodyConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Physics);
- // Export RigidBodyComponent
- if(p_constructionInfo.m_rigidBodyConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::RigidBodyComponent);
- // Add rigid body data
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_rigidBodyConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::Friction, p_constructionInfo.m_rigidBodyConstructionInfo->m_friction);
- componentPropertySet.addProperty(Properties::PropertyID::Mass, p_constructionInfo.m_rigidBodyConstructionInfo->m_mass);
- componentPropertySet.addProperty(Properties::PropertyID::Restitution, p_constructionInfo.m_rigidBodyConstructionInfo->m_restitution);
- componentPropertySet.addProperty(Properties::PropertyID::Kinematic, p_constructionInfo.m_rigidBodyConstructionInfo->m_kinematic);
- // Convert CollisionShapeType to PropertyID
- Properties::PropertyID collisionShapeType = Properties::Null;
- switch(p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeType)
- {
- case RigidBodyComponent::CollisionShapeType_Box:
- collisionShapeType = Properties::Box;
- break;
- case RigidBodyComponent::CollisionShapeType_Capsule:
- collisionShapeType = Properties::Capsule;
- break;
- case RigidBodyComponent::CollisionShapeType_Cone:
- collisionShapeType = Properties::Cone;
- break;
- case RigidBodyComponent::CollisionShapeType_ConvexHull:
- collisionShapeType = Properties::ConvexHull;
- break;
- case RigidBodyComponent::CollisionShapeType_Cylinder:
- collisionShapeType = Properties::Cylinder;
- break;
- case RigidBodyComponent::CollisionShapeType_Sphere:
- collisionShapeType = Properties::Sphere;
- break;
- }
- // Add collision shape data, if it's valid
- if(collisionShapeType != Properties::Null)
- {
- auto &collisionShapePropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::CollisionShape);
- collisionShapePropertySet.addProperty(Properties::PropertyID::Type, collisionShapeType);
- collisionShapePropertySet.addProperty(Properties::PropertyID::Size, p_constructionInfo.m_rigidBodyConstructionInfo->m_collisionShapeSize);
- }
- }
- }
- }
- void SceneLoader::exportToProperties(const ScriptComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if(p_constructionInfo.m_luaConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::Script);
- // Export LuaComponent
- if(p_constructionInfo.m_luaConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::LuaComponent);
- // Add LUA component data
- if(!p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename.empty())
- {
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_luaConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::Filename, p_constructionInfo.m_luaConstructionInfo->m_luaScriptFilename);
- if(!p_constructionInfo.m_luaConstructionInfo->m_variables.empty())
- {
- auto &variablesPropertySet = componentPropertySet.addPropertySet(Properties::PropertyID::Variables);
- // Go over each variable
- for(auto &variable : p_constructionInfo.m_luaConstructionInfo->m_variables)
- {
- // Make sure variable type is valid
- if(variable.second.getVariableType() != Property::PropertyVariableType::Type_null)
- {
- auto &variableArrayEntry = variablesPropertySet.addPropertySet(Properties::PropertyID::ArrayEntry);
- // Add variable data
- variableArrayEntry.addProperty(Properties::PropertyID::Name, variable.first);
- // Add the appropriate data based on the variable's data type
- switch(variable.second.getVariableType())
- {
- case Property::PropertyVariableType::Type_bool:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getBool());
- break;
- case Property::PropertyVariableType::Type_int:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getInt());
- break;
- case Property::PropertyVariableType::Type_float:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getFloat());
- break;
- case Property::PropertyVariableType::Type_double:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getDouble());
- break;
- case Property::PropertyVariableType::Type_vec2i:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec2i());
- break;
- case Property::PropertyVariableType::Type_vec2f:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec2f());
- break;
- case Property::PropertyVariableType::Type_vec3f:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec3f());
- break;
- case Property::PropertyVariableType::Type_vec4f:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getVec4f());
- break;
- case Property::PropertyVariableType::Type_string:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getString());
- break;
- case Property::PropertyVariableType::Type_propertyID:
- variableArrayEntry.addProperty(Properties::PropertyID::Value, variable.second.getID());
- break;
- }
- }
- }
- }
- }
- }
- }
- std::string m_luaScriptFilename;
- std::vector<std::pair<std::string, Property>> m_variables;
- }
- void SceneLoader::exportToProperties(const WorldComponentsConstructionInfo &p_constructionInfo, PropertySet &p_properties)
- {
- if( p_constructionInfo.m_objectMaterialConstructionInfo != nullptr ||
- p_constructionInfo.m_spatialConstructionInfo != nullptr)
- {
- auto &propertySet = p_properties.addPropertySet(Properties::PropertyID::World);
- // Export ObjectMaterialComponent
- if(p_constructionInfo.m_objectMaterialConstructionInfo != nullptr)
- {
- // Convert ObjectMaterialType to PropertyID
- Properties::PropertyID objectMaterialType = Properties::Null;
- switch(p_constructionInfo.m_objectMaterialConstructionInfo->m_materialType)
- {
- case Concrete:
- objectMaterialType = Properties::Concrete;
- break;
- case Glass:
- objectMaterialType = Properties::Glass;
- break;
- case Metal:
- objectMaterialType = Properties::Metal;
- break;
- case Plastic:
- objectMaterialType = Properties::Plastic;
- break;
- case Rock:
- objectMaterialType = Properties::Rock;
- break;
- case Rubber:
- objectMaterialType = Properties::Rubber;
- break;
- case Wood:
- objectMaterialType = Properties::Wood;
- break;
- }
- // Add object material data, if it's valid
- if(objectMaterialType != Properties::Null)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::ObjectMaterialComponent);
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_objectMaterialConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::Type, objectMaterialType);
- }
- }
- // Export SpatialComponent
- if(p_constructionInfo.m_spatialConstructionInfo != nullptr)
- {
- auto &componentPropertySet = propertySet.addPropertySet(Properties::PropertyID::SpatialComponent);
- // Add spatial data
- componentPropertySet.addProperty(Properties::PropertyID::Active, p_constructionInfo.m_spatialConstructionInfo->m_active);
- componentPropertySet.addProperty(Properties::PropertyID::LocalPosition, p_constructionInfo.m_spatialConstructionInfo->m_localPosition);
- componentPropertySet.addProperty(Properties::PropertyID::LocalRotation, p_constructionInfo.m_spatialConstructionInfo->m_localRotationEuler);
- componentPropertySet.addProperty(Properties::PropertyID::LocalRotationQuaternion, Math::toGlmVec4(p_constructionInfo.m_spatialConstructionInfo->m_localRotationQuaternion));
- componentPropertySet.addProperty(Properties::PropertyID::LocalScale, p_constructionInfo.m_spatialConstructionInfo->m_localScale);
- }
- }
- }
|