My custom OBJ Loader module script!

Well, I past the last 4-6 months on creating some script that allow you to load any OBJ model into the game. I made it for my plugin, but thought: “Why not share the source code here?” and now I am sharing it with you guys! You can get it from Github or requiring it using the Roblox Library, choose what you want. Anyways, it just contains one function (for you to use, the rest are private functions) that’s called loadObj. It takes 2 parameters, the first is the data, wich is only the OBJ file content (when you open an obj file, you see the content. That’s what I mean). Then there’s the settings, wich I will improve in the future. For now (at the time this topic was written) it’s only a dictionary that contains 1 single key, the scale, wich is default to 1 if you don’t specify the second parameter (you don’t have to specify it anyways). It looks like this:

{ 
    scale = 1
}

, where scale is used to make your model bigger or smaller while loading (scale 1 is normal, 0.5 would be 0.5% more smaller and 1.5 would be 0.5%).

Now for the data, to avoid to have an over 500 line of only pure strings in your main code, you should create a module script, place the obj file data there and when you need it simply require it.
So, I would do something like this:

--Module Script containing your obj data
return 
 [[ Add here your obj file content
]]

-- some other Script
local Loader = path.to.my.obl_loader
local Data = path.to.the.modulescript.of.above
local OBJ_Model = Loader.loadobj(Data)

Now, if you want to know what functions it have: I pre-made some functions to show how you should use them, but normally you can add your functions to make them work in the way that you want (warning, OOP skills or atleast know what the keyword “self” mean is required to add custom functions)!

I made for you 4 functions:

FillFaces
HideFaces
ShowVertices
HideVertices

you can find them in the private “CreateModel” function.

And here I am only showcasing some example of what you can achieve with this (please, never and never and don’t ever try to give the client access to this. Malicious players could profit of this and load inappropiate stuff into your game, so always keep this on the server side):

The code used in the video:

local Importer = require(TheLoaderPath.write.your.path.here)

local Model = Importer.loadObj(TheOBJdata.write.your.data.here)

local isOn:boolean = true

local StorageModel:Model = Instance.new("Model") do
    StorageModel.Parent = workspace
    StorageModel.Name = "TEST_NAME"
end

Model:FillFaces(StorageModel)

while true do

	wait(5)

	isOn = not isOn

	if isOn then
		Model:FillFaces(StorageModel)
	else
	Model:HideFaces()
	end
end

Finally, enjoy it!

Credits:

License

11 Likes

What’s the purpose of this loader? It doesn’t actually load an obj mesh it just replicates it’s triangles with parts (due to roblox’s limitations, I know), so instead of storing vertices, texture coordinates, indices and normals we store all of the part’s properties for each face. In case anyone makes the argument that it’s faster to load this way than loading the roblox asset then no it’s not

And speaking of texture coordinates and normals those aren’t even possible to have this way

3 Likes

It can be used to dynamic load meshes into the game. As everyone know, Roblox has a loader, but you only can load meshes from studio and not in game. With this, you can update the vertices to e.g. create some better ice spikes spells (or many other things, it‘s just one example) by animating it (changing the vectors position, I didn‘t included it there because the code isn‘t readable at all but I can include it if someone wants) or as I said, load any Mesh that you want in game and not in studio, so that you can change it anytime that you want.

Roblox limitations… But if there‘s a way to correctly import one obj model, I would try it, 100% (If Roblox would give us more permissions…)!

It‘s the only way that I found. As I said, I am working on a plugin (it‘s very buggy, I can‘t solve all bugs at once, so I rolled it out and I am starting to fix bugs one by one.

I think it‘s impossible that it can be faster than Roblox Wavefront (another word for OBJ) importer, because developers are writting in Lua and Roblox‘s importer is written in C++, but I can try making it faster (it‘s currently slow because of the wait() that I added, if you are importing too large meshes then the Loader give an error, because you know what happen, it‘s the same like you would run an

while true do
    print("What am I doing here?")
end

without adding a wait(). But removing it and importing smaller Wavefront models is really much faster, you should try this out if you want. This is another thing that I want to improve).

You can read them, but you can‘t do anything with them (if you add something in these lines for the texture coordinates and normals)in Roblox (again, thanks for the limitations), so this is why I don‘t even get them, this would just take more time for nothing (if you say me that I can do something with them, I would start by doing it, believe me. Did I miss something?)

I think that this is some nice alternative until one day (2050 lol), Roblox will finally decide to give their developpers more permissions like in Unity or UnrealEngine4 (yes, yes I know that Roblox is cross-platform compatible and Unity and UE4 aren‘t, and that this is the cause of the limitations, but still…)

2 Likes