u3d-community / U3D

Open-source, cross-platform 2D and 3D game engine built in C++
https://u3d.io
MIT License
187 stars 30 forks source link

Can not import 3d model with multiple materials #41

Closed alexefu73 closed 1 year ago

alexefu73 commented 1 year ago

Hi gurus, I started to learn programming games with the great framework U3D (Urho3D), and in progress of learning the engine I faced some problems. And searched the whole web for the solution, and no luck.

I exported 3D scene made in Blender to .obj, then imported to the U3D Editor.sh. Every thing looks good but the shadows not exactly what I expect to look, but that's not the serious problem I faced. Then saved the file.

The big and serious problem is when imported it to U3D it's look like couldn't find other than one of the material files or the textures exported with it.

Here is the scene I made in Blender: Scene in Blender

And here is when imported to the U3D Editor.sh, every thing in place, but the shadows not settled as it in blender: Scene in Urho3D Editor sh

And eventually here is what the scene look when imported the my app, there is just one material with it's texture image for all the objects in the scene: Scene after Urho3D

And here is the code I used to create the scene and imported materials: //////////////////////////////////////////////////////////////////////////////// // (4-1) Create sci-fi scene static model { scifi = scene_->CreateChild("scifiPlanet"); //scifi->SetPosition(Vector3(-1, 1.6, -3)); scifi->SetPosition(Vector3::ZERO); scifi->Scale(1);

    StaticModel* object = scifi->CreateComponent<StaticModel>();
    object->SetModel(cache->GetResource<Model>("Models/Sci-fi.mdl"));
    object->SetMaterial(cache->GetResource<Material>("Materials/Corridor_ways_mat.xml"));
    object->SetCastShadows(true);

    RigidBody* scifiBody = scifi->CreateComponent<RigidBody>();
    scifiBody->SetCollisionLayer(2);

    CollisionShape* shape = scifi->CreateComponent<CollisionShape>();
    shape->SetBox(Vector3::ONE);
}

////////////////////////////////////////////////////////////////////////////////

So, where is the wrong here. What mistake I did in the code please?

alexefu73 commented 1 year ago

Sorry I forget to brought the a list of the material files and textures.. But here it's:

Material files: /Data/Materials/Corridor_ways_mat.xml /Data/Materials/None.xml /Data/Materials/Standard(Clone).xml

Texture files: /Date/Textures/Corridor_ways_tex.png /Date/Textures/scifi_map.png

SirNate0 commented 1 year ago
    object->SetMaterial(cache->GetResource<Material>("Materials/Corridor_ways_mat.xml"));

You are only loading one material, so you only see one material. Add the material index before the material in SetMaterial to add more than one.

Exporter-wise, there is also a Blender->Urho3D exporter plugin, which can also create an XML file that you load as a node with all the materials and such setup properly for you (though you will have to use an older version of Blender, <2.8, or use the newer versions but use special Materials that the exporter recognizes to map to Urho's materials).

SirNate0 commented 1 year ago

As to the shadows, Blender will do a better job of shadows (especially with the Rendered view), as it's meant for rendering videos and such ahead-of-time, and not necessarily real-time. Urho's shadows typically have hard edges, though there was some work to get soft shadows working. Possibly others have more info on that.

alexefu73 commented 1 year ago
    object->SetMaterial(cache->GetResource<Material>("Materials/Corridor_ways_mat.xml"));

You are only loading one material, so you only see one material. Add the material index before the material in SetMaterial to add more than one.

Exporter-wise, there is also a Blender->Urho3D exporter plugin, which can also create an XML file that you load as a node with all the materials and such setup properly for you (though you will have to use an older version of Blender, <2.8, or use the newer versions but use special Materials that the exporter recognizes to map to Urho's materials).

Thank you so much. This is really the reason to not show the other materials, but could you add this information to the wiki, because it's not intuitive to do this trick.

Secondly, I'm now doing that by 'try, check and correct' way for each model to set its material, but because the scene contain much models it's hard to keep going using this way, so is there a way to know the index of each model and its material.

alexefu73 commented 1 year ago

Ok. It's done now. I follow the instructions in https://discourse-urho3d.github.io/t/how-would-i-use-blender-to-load-a-model-with-more-than-one-material/6824/, and made the following modifies to the code as follows:

////////////////////////////////////////////////////////////////

StaticModel* object = scifi->CreateComponent(); object->SetModel(cache->GetResource("Models/Sci-fi.mdl")); object->ApplyMaterialList("Models/Sci-fi.txt");

////////////////////////////////////////////////////////////////////////////

The materials is in it their's places correctly.

Screenshot_20230311_111224