Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function parseOBJ(objFile)
- -- Parses an OBJ File into a data array.
- local obj = {
- Verts = {};
- Norms = {};
- Texs = {};
- Faces = {};
- }
- local currentMtl = "";
- for line in objFile:gmatch("[^\r\n]+") do
- if #line > 0 then
- local info = {}
- local tag = ""
- local process = ""
- for char in line:gmatch(".") do
- if char == " " then
- if tag == "" then
- tag = process
- else
- table.insert(info,tonumber(process) or process)
- end
- process = ""
- else
- process = process .. char
- end
- end
- if tag == "g" then
- currentMtl = info[1]
- elseif tag == "v" then
- local Vert = {
- Mtl = currentMtl;
- Coords = Vector3.new(unpack(info));
- };
- table.insert(obj.Verts,Vert)
- elseif tag == "vn" then
- local Norm = Vector3.new(unpack(info));
- table.insert(obj.Norms,Norm)
- elseif tag == "vt" then
- local texCoord = Vector2.new(unpack(info))
- table.insert(obj.Texs,texCoord)
- elseif tag == "f" then
- local triangle = {}
- local v,t,n
- for _,pair in pairs(info) do
- local index = {}
- if type(pair) == "number" then
- v = pair
- elseif string.find(pair,"//") then
- v,n = string.match(pair,"(%S+)//(%S+)")
- else
- v,t,n = string.match(pair,"(%S+)/(%S+)/(%S+)")
- if not v or not t or not n then
- v,t = string.match(pair,"(%S+)/(%S+)")
- end
- end
- end
- triangle.Vertex = tonumber(v)
- triangle.Texture = tonumber(t)
- triangle.Normal = tonumber(n)
- table.insert(obj.Faces,triangle)
- end
- end
- end
- return obj
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement