136 std::ifstream i(p);
if(!i)
return{}; std::string magic;
int version=0;i>>magic>>version;
137 if(magic!=
"NOJOB_PREFAB")
return {};
140 std::string k,name=
"Prefab",mp,a,n,me,r,aop,em;
141 glm::vec3 pos{},rot{},sc{1},ec{};glm::vec4 col{1};
142 float mt=0,ro=.5f,ao=1,ns=1,es=0;
bool hm=
false,useTexture=
false;
144 if(k==
"NAME")i>>std::quoted(name);
145 else if(k==
"TRANSFORM")i>>pos.x>>pos.y>>pos.z>>rot.x>>rot.y>>rot.z>>sc.x>>sc.y>>sc.z;
146 else if(k==
"MESH")i>>std::quoted(mp);
147 else if(k==
"MATERIAL"){i>>col.r>>col.g>>col.b>>col.a>>mt>>ro>>ao>>ns>>ec.r>>ec.g>>ec.b>>es;hm=
true;}
148 else if(k==
"USE_TEXTURE"){
int enabled=0;i>>enabled;useTexture=enabled!=0;}
149 else if(k==
"TEXTURES")i>>std::quoted(a)>>std::quoted(n)>>std::quoted(me)>>std::quoted(r)>>std::quoted(aop)>>std::quoted(em);
154 auto m=std::make_shared<Material>(*base);m->GetColor()=col;m->Metallic()=mt;m->Roughness()=ro;m->AmbientOcclusion()=ao;
155 m->NormalStrength()=ns;m->EmissiveColor()=ec;m->EmissiveStrength()=es;m->UseTexture()=
false;
156 m->SetTexture(LoadTex(a,p));
if(version<3&&!a.empty())useTexture=
true;m->UseTexture()=useTexture&&(m->GetTexture()!=
nullptr);
157 m->SetNormalTexture(LoadTex(n,p));m->SetMetallicTexture(LoadTex(me,p));m->SetRoughnessTexture(LoadTex(r,p));
158 m->SetAOTexture(LoadTex(aop,p));m->SetEmissiveTexture(LoadTex(em,p));e.AddComponent<
MeshRendererComponent>(m);
163 std::unordered_map<int,Entity> made;
Entity root{};
166 if(k!=
"ENTITY"){std::string skip;std::getline(i,skip);
continue;}
167 int id,parent;i>>
id>>parent;std::string name;i>>std::quoted(name);
169 glm::vec3 pos{},rot{},sc{1};std::string meshPath;
size_t matCount=0;
170 std::string animationPath;
int animationClip=0;
float animationSpeed=1.0f;
171 bool animationPlaying=
true,animationLoop=
true;
176 while(i>>k && k!=
"END_ENTITY"){
177 if(k==
"TRANSFORM"){i>>pos.x>>pos.y>>pos.z>>rot.x>>rot.y>>rot.z>>sc.x>>sc.y>>sc.z;}
178 else if(k==
"MESH"){i>>std::quoted(meshPath);}
179 else if(k==
"ANIMATOR"){
180 int playing=1,loop=1;
181 i>>std::quoted(animationPath)>>animationClip>>animationSpeed>>playing>>loop;
182 animationPlaying=playing!=0; animationLoop=loop!=0;
184 else if(k==
"AUDIO_SOURCE"){
190 else if(k==
"AUDIO_LISTENER"){
191 i>>audioListener.
Enabled; hasAudioListener=
true;
193 else if(k==
"PARTICLE_SYSTEM"){
200 hasParticleSystem=
true;
202 else if(k==
"PARTICLE_SYSTEM_V2"){
216 hasParticleSystem=
true;
218 else if(k==
"SCRIPT_V2"){
221 else if(k==
"SCRIPT_FIELD" && hasScript){
222 std::string fieldName;
int type=0; i>>std::quoted(fieldName)>>type;
229 nativeScript.
Fields[fieldName]=value;
231 else if(k==
"MATERIALS"){
232 i>>matCount; std::vector<std::shared_ptr<Material>> mats;
233 for(
size_t m=0;m<matCount;++m){
234 std::string mk;i>>mk;
if(mk==
"MAT_NONE"){mats.push_back({});
continue;}
235 if(mk!=
"MAT")
continue;
236 auto mat=std::make_shared<Material>(*base);
auto& c=mat->GetColor();
auto& ec=mat->EmissiveColor();
237 int use=0,mode=0;i>>c.r>>c.g>>c.b>>c.a>>mat->Metallic()>>mat->Roughness()>>mat->AmbientOcclusion()
238 >>mat->NormalStrength()>>ec.r>>ec.g>>ec.b>>mat->EmissiveStrength()>>use>>mode>>mat->AlphaCutoff();
240 std::string tk,a,n,me,r,ao,em;i>>tk>>std::quoted(a)>>std::quoted(n)>>std::quoted(me)>>std::quoted(r)>>std::quoted(ao)>>std::quoted(em);
241 mat->SetTexture(LoadTex(a,p));mat->UseTexture()=use&&mat->GetTexture();
242 mat->SetNormalTexture(LoadTex(n,p));mat->SetMetallicTexture(LoadTex(me,p));mat->SetRoughnessTexture(LoadTex(r,p));
243 mat->SetAOTexture(LoadTex(ao,p));mat->SetEmissiveTexture(LoadTex(em,p));mats.push_back(mat);
249 if(!meshPath.empty()){
253 if(!animationPath.empty()){
255 const auto resolvedAnimationPath =
258 if(animation&&animation->HasAnimations()){
261 animator.
ClipIndex=std::clamp(animationClip,0,(
int)animator.
Animation->Clips().size()-1);
263 animator.
Speed=animationSpeed;
264 animator.
Playing=animationPlaying;
265 animator.
Loop=animationLoop;
268 }
catch(...){
Log::Warn(
"Prefab animation load failed: " + animationPath);}
274 if(parent>=0&&made.contains(parent))s.
SetParent(e,made[parent],
false);
Texture declarations for the Rendering subsystem.