After taking a read of the code, it seems to only open and read files which could be possibly replicated with the use of require
and ModuleScripts.
Simply return your OBJ data from the ModuleScript:
return "<data>";
Then, you’ll need to update the reading segments to use require
& split the data by “\n” (new line) for a table to iterate through.
Here’s an example of the updated loadObj:
local function loadObj(filePath, settings)
settings = settings or {
scale = 1;
};
local active_material = "";
local active_object, active_group = MinimalObjModel(), nil;
---------------------------------------------------------------------------
local obj_Operations = {
--[[
# is a comment, skip these
--]]
['#'] = function(model, token) end,
--[[
mtllib denotes a required mtl file
--]]
['mtllib'] = function(model, token)
model.material_file_name = token[1];
end,
--[[
usemtl tells what material to use for the faces
--]]
['usemtl'] = function(model, token)
active_material = token[1];
end,
--[[
v denotes vertex
--]]
['v'] = function(model, token, settings)
local v = Vertex(token[1] * settings.scale, token[2] * settings.scale, token[3] * settings.scale, token[4]);
model.vertices[#model.vertices+1] = v
active_object.vertices[#model.vertices] = true;
end,
--[[
vt denotes texture coordinates(UV)
--]]
['vt'] = function(model, token, settings)
local uv = UV(token[1] * settings.scale, token[2] * settings.scale, token[3]);
model.uvs[#model.uvs+1] = uv;
active_object.uvs[#model.uvs] = true;
end,
--[[
vn denotes normal
--]]
['vn'] = function(model, token, settings)
local normal = Normal(token[1] * settings.scale, token[2] * settings.scale, token[3] * settings.scale);
model.normals[#model.normals+1] = normal;
active_object.normals[#model.normals] = true;
end,
--[[
vp denotes free form geometry
--]]
['vp'] = function(model, token, settings)
local vp = free_form(token[1], token[2], token[3]);
model.space_vertices[#model.space_vertices+1] = vp;
active_object.space_vertices[#model.space_vertices] = true;
end,
--[[
f denotes faces
--]]
['f'] = function(model, token, settings)
local face = _readFace(model, token, active_material);
model.faces[#model.faces+1] = face;
active_object.faces[#model.faces] = true;
model.poly_count = model.poly_count+1;
end,
--[[
o denotes object
--]]
['o'] = function(model, token, settings)
local object = MeshObjectModel(token[1], active_group);
model.objects[#model.objects+1] = object;
active_object = object;
end,
--[[
g denotes group
--]]
['g'] = function(model, token)
local group = GroupModel(token[1]);
model.groups[#model.groups+1] = group;
active_group = group;
end,
};
---------------------------------------------------------------------------
local data = require(ModuleScript) --// UPDATED
local model = ObjModel();
for _, line in ipairs(string.split(data, " ")) do --// UPDATED
if (line ~= "") then
local token = {};
for tk in line:gmatch("%S+") do
token[#token+1] = tk;
end
local identifier = token[1];
if (obj_Operations[identifier]) then
table.remove(token, 1);
obj_Operations[identifier](model, token, settings);
end
end
end
return model;
end
Of course, you’ll need to remove the assertion at the top the Script:
assert(io, "Requires io library to function!");