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:
- Shoutout to @Ewanophobia (his today name or his old name, atleast the one that I know, Cinema_Sin, for providing me some usefull links that helped me with creating my own Loader):
Read OBJ Files, but in Roblox studio? - #13 by Ewanophobia - This github repositories, that Ewanophobia linked me, that really helped me creating my own loader:
- karai17/lua-obj: Load a Wavefront Object file into Lua (github.com)
- kenetec/OBJ-Loader: A general purpose, bare bones .obj loader. (github.com)
License
- GNU AGPLv3, you really should check out this website, GNU Affero General Public License v3.0 | Choose a License to know what it does without having to read the whole License.