[FREE] Runtime OBJ Loader

Features
-Ability to load OBJ file from stream
-mtl file loading, and texture loading (dds, crn, jpg, jpeg, png, tga, bmp)
-Triangle, Quad, and Ngon import
-Fast loading times
-Works in both the editor, and runtime*
-Usable in the editor

*Runtime Usage Requirements
In order for this to work in runtime, you must go to the “Graphics” tab in your project, and add “Standard (Specular Setup)” to the always included shaders. This will make your next build take a while.

If you don’t do this, the required shader / shader variants won’t exist.

How Do I Use It?

Editor
Select the GameObject menu (up top)
Then select “Import from OBJ”

Runtime

using Dummiesman;
using System.IO;
using UnityEngine;

public class ObjFromFile : MonoBehaviour
{
    void Start()
    {
        //file path
        string filePath = @"I:\random\cylinder.obj";
        if (!File.Exists(filePath))
        {
            Debug.LogError("Please set FilePath in ObjFromFile.cs to a valid path.");
            return;
        }

        //load
        var loadedObj = new OBJLoader().Load(filePath);
    }
}

Example Imports


Downloads

2368527–353893–OBJImportV2-Preview.zip (32.8 KB)

12 Likes

I submitted this to the asset store on the day I submitted the last update here on the forum (nov 13th). Hopefully you can find it soon :slight_smile: (Pending Approval atm)

1 Like

Thank you, Aaro4130. :sunglasses:

1 Like

Package has just been approved on the asset store. :smile:

2 Likes

Here is version 1.1 early (since it’ll take ~3 weeks at the current Asset Store approval rate)

Changes
-Now uses Standard shader
-Added transparency support
-Added normal map support
-Added support for reading partially malformed or improperly created OBJ files

2451991–168397–uploadtool_OBJImport.unitypackage (5.42 KB)

2 Likes

This fixed my index out of bounds issue, my models can now successfully import. Thanks for the quick response and fix.

Here is version 1.11 early

Changes
-Added ‘searchPaths’ variable
-Added ‘splitByMaterial’ variable

Use splitByMaterial if meshes have too many vertices in them.

2452981–168505–uploadtool_OBJImport.unitypackage (5.56 KB)

One last update

Version 1.12
Changes
-Material names, mesh names, textures, etc now support spaces.
-Script optimizations
-Fixed an issue where search paths in MTL files weren’t working correctly

2453527–168758–uploadtool_OBJImport.unitypackage (5.54 KB)

First off, thank you for making this!
Now, I’m having problem making this work. I called OBJLoader.LoadOBJFile(“C:/Users/gamer/Documents/image.obj”);
and when i run on unity editor on windows, unity freezes. I’m not even sure if my file path name is in the correct format. Please help.

Thank you again.

Unity just unfroze and I get the errors:
Mesh.vertices is too large.
Mesh.normals is out of bounds
Mesh.uv is out of bounds
Failed setting triangles

I am trying to load a medical image exported as obj and it is quite large: 20MB. Is it too large?

Thank you.

I had the same issue before some optimization. Try setting the spitbymaterial to true

Tried it, got same errors.

Could you screenshot the error log for me?

Here it is

Ah, you’ll need to divide the mesh from where it’s being exported from. If that’s not possible, i can see if I can work something out for you.

Wow 800k vertices. can unity even handle meshes that large?

Yes. If you import it in the editor, unit splits it up into different meshes. But I don’t know how to do it myself. It’s an obj file from a catscan so its pretty big.

I could work something out for you :slight_smile:

That would be beautiful. I’m still clueless on how to solve this problem.

I suggest you could make vertices to be queued and rendered as 1 mesh with 65000 vertices. Then dequeue the remaining :slight_smile: to finish