将obj文件转换为lua中的自定义格式

时间:2018-12-10 23:55:47

标签: lua

此代码几乎可以将obj文件转换为我的自定义格式,但是当您放置具有超过9张面的形状时,它会产生所有摆动,并给我错误的面顶点编号。格式应该是{vertex1,vertex2,vertex3,rvalue,gvalue,bvalue},所以说我有一张使用顶点1,2和4的脸,那么我将得到{1,2,4,255,255,0},。但是如果我的顶点为1,2和10,那么我得到{1,2,101,255,255,0},而我不知道为什么

-- see if the file exists
function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

-- get all lines from a file, returns an empty 
-- list/table if the file does not exist
function lines_from(file)
 if not file_exists(file) then return {} end
 lines = {}
 for line in io.lines(file) do 
   lines[#lines + 1] = line
 end
 return lines
end

-- tests the functions above
local file = 'cube.txt'
local lines = lines_from(file)
-- print all line numbers and their contents

for k,v in pairs(lines) do
 str = tostring(v)
 if 9 <= k then
   str = str .. ","
 end
 print("before space : "..str)
 str = str:gsub("v ", "")
 str = string.gsub(str, "v", "")
 str = str:gsub("f ", "")
 str = string.gsub(str, "f", "")
 str = str:gsub("%s+", ",")
 str = string.gsub(str, "%s+", "")
 print("before // : "..str)
 str = str:gsub("(//).(,)", ",")
 str = string.gsub(str, "//", "")
 if k >= 9 then
   print("  { " .. str .. "255,255,0 },")
 else
   print("  { " .. str .. " },")
 end
end

0 个答案:

没有答案