raylib: [rmodels] OBJ models with multiple materials/meshes not loading correctly
- I tested it on latest raylib version from master branch
- I checked there is no similar issue already reported
- I checked the documentation on the wiki
- My code has no errors or misuse of raylib
Issue description
I have a model made in blender with multiple meshes and materials that load correctly in raylib 4.5, but after updating to 5.0 everything is in one material. I have provided a minimal example with a simple 2 mesh/material model.
Environment
Tried on multiple machines and operating systems
Issue Screenshot
4.5
master/5.0
Code Example
#include "src/raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "model loading");
Camera camera = { 0 };
camera.position = (Vector3){ 2.0f, 2.0f, 2.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
Model model = LoadModel("tomato.obj");
Vector3 position = {0};
DisableCursor();
SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(model, (Vector3) {0}, 1.0f, WHITE);
DrawGrid(20, 10.0f);
EndMode3D();
EndDrawing();
}
UnloadModel(model);
CloseWindow();
return 0;
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 17 (9 by maintainers)
I have written a new LoadOBJ in this branch. It till needs massive testing and Debugging. https://github.com/JeffM2501/raylib/tree/LoadObjNew
I am working on new code for raylib to read the data from TinyOBJ. The loader has the data needed, it just needs to be converted properly into multiple meshes. The old code made too many assumptions and took some shortcuts that can be made.
Splitting the mesh by material is required for OBJ to work simply due to how the OBJ format is defined. To OBJ the entire file is one mesh with N groups, each group can have N material assignments. To represent this as a raylib mesh, each group must be split into sub-meshes when the material changes, since a raylib mesh can only have one material. It is insufficient to combine sub meshes with the same material into one, since the group split must also be maintained. A user may need to manipulate individual groups. To do this the mesh must be scanned multiple times to get the actual number of meshes and number of triangles per mesh.
As a side note, we may want to see about adding an array of mesh names to the model structure so that people can know what groups each mesh in the model came from, but that is not required for this fix.
It’s tedious code but very possible. I will continue to work on it and make a PR when I have something. It will involve an entirely new LoadOBJ function.
If it helps anyone else, for OBJ model loaded I ended up using this single file library to load OBJ models into raylib: https://github.com/Not-Nik/rlobj
Not sure the author would agree to swapping it out, but for anything more complicated than a single object it’s required
I had a similar issue, and the above code by BobSleigh38 didn’t work for me with the materials not being set properly to the meshes. What I did was just export in Blender as a .glb file instead of an .obj file (so this isn’t really a fix to this issue, rather a workaround).
When I initially did this however, all my meshes were centered at (0,0,0) and with normalized scales, so my fix was just to select all the objects in Blender (A), and apply the transforms by pressing Ctrl+A and selecting All Transforms like so:
Here’s my export settings in Blender…
Hello I also have issues loading a model with 3 materials defined by an obj+mtl files. Only the first material appearing in mtl file is used for the entire model. BUT ! If I add this
model.meshMaterial[0]=0; model.meshMaterial[1]=1; model.meshMaterial[2]=2; Then my model displays completely fine, with the right 3 materials …
I’m closing this issue, feel free to send a PR when ready for further discussion there.
Yes use glb when possible, obj is an old and fragile format.
I have the same issue. My OBJ models are exported from Blender triangulated - the else statement here is the issue - the tri count is actually an offset when there are two or more models in the same file:
https://github.com/raysan5/raylib/blob/dc7f81a7b0cbc666812c870841313e0c15a87a0c/src/rmodels.c#L4070-L4076
Here’s an example - it crashes the demo here too - https://www.raylib.com/examples/models/loader.html?name=models_loading
obj_multiple_models.zip
My fix is to use the length (despite the comment) warning about quads