Advertisement
advancedev

Untitled

Feb 7th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. --[[
  2.  
  3. .obj to roblox V1.2.2 by magnalite for PUBLIC release
  4. credits to stravant for triangle making function
  5.  
  6. Options
  7.  
  8.  
  9. loadspeed = How fast you want the mesh to load (Higher the greater but more laggy)
  10. scale = How big you want the mesh to be
  11. wrongAngleFixer = Should only be true if your model is at an odd angle
  12. loadColor = If you want the mesh to have colors or not
  13. offset = translate the model by this Vector
  14.  
  15. --]]
  16.  
  17.  
  18.  
  19. loadspeed = 100
  20. scale = 10
  21. wrongAngleFixer = false
  22. loadColors = true
  23. offset = Vector3.new()
  24.  
  25.  
  26. shouldrenderVertexes = false
  27.  
  28. shouldrenderWireFrame = false
  29.  
  30. shouldrenderFaces = true
  31.  
  32.  
  33.  
  34. --[[
  35. Changelog-
  36. V 1.2.2
  37.  
  38. --Improved accuracy
  39. --Stravant's triangle code
  40.  
  41. V 1.2.1
  42.  
  43. --fixed bug reported by SlingshotJunkie
  44.  
  45. V 1.2.0
  46. --Oysi's triangle code
  47. --Minor code cleanup
  48. --PUBLIC RELEASE
  49.  
  50. V 1.1.0
  51. --Added support for more obj file structures
  52.  
  53. V 1.0.0
  54. --Added support for colors (.mtl parsing)
  55.  
  56.  
  57.  
  58.  
  59. ]]--
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. -------------------------------------------------------------------------------------------------------------------
  72. print("\n")
  73. _G.Mesh = nil
  74. _G.Materials = nil
  75.  
  76. print("Waiting for Mesh and Materials Data\n")
  77. repeat wait() until _G.Mesh and _G.Materials
  78.  
  79. objFile = _G.Mesh
  80. mtlFile = _G.Materials
  81.  
  82. PolyDraw = {}
  83.  
  84. function parseMaterials()
  85. materials = {}
  86. filePos = 0
  87. local endfile = false
  88. while endfile == false do
  89. endFile = true
  90. materialPos = string.find(mtlFile, "newmtl" ,filePos)
  91.  
  92. if materialPos then
  93. materialNameStartPos = string.find(mtlFile, "%s", materialPos+1)
  94. materialNameEndPos = string.find(mtlFile, "%s", materialNameStartPos+1)
  95. materialName = string.sub(mtlFile,materialNameStartPos+1,materialNameEndPos-1)
  96.  
  97. colorPos = string.find(mtlFile, "Kd", materialNameEndPos)
  98. colorStartPos = string.find(mtlFile, "%s", colorPos)
  99. colorEndPos = string.find(mtlFile, "Ks", colorStartPos)
  100. color = string.sub(mtlFile, colorStartPos+1,colorEndPos-2)
  101.  
  102. color1EndPos = string.find(color, " ")
  103. color1 = string.sub(color,0,color1EndPos-1)
  104.  
  105. color2EndPos = string.find(color, " ", color1EndPos+1)
  106. color2 = string.sub(color,color1EndPos+1,color2EndPos-1)
  107.  
  108. color3 = string.sub(color,color2EndPos+1)
  109.  
  110. endColor = Color3.new(tonumber(color1),tonumber(color2),tonumber(color3))
  111.  
  112. materials[materialName] = endColor
  113.  
  114. filePos = colorEndPos
  115.  
  116. else
  117. endFile = true
  118. break
  119. end
  120.  
  121. end
  122. end
  123.  
  124.  
  125.  
  126.  
  127. function parseVertexes()
  128. vertexes = {}
  129. filePos = 0
  130. local endfile = false
  131. while not endfile do
  132. vertexPos = string.find(objFile, "v ",filePos)
  133.  
  134. if vertexPos then
  135. startPos = string.find(objFile, "%S", vertexPos+1)
  136. firstCordPos = string.find(objFile, " ", startPos)
  137. firstCord = string.sub(objFile,vertexPos+1,firstCordPos-1)
  138.  
  139. secondCordPos = string.find(objFile, " ", firstCordPos+1)
  140. secondCord = string.sub(objFile,firstCordPos+1,secondCordPos-1)
  141.  
  142. thirdCordPos = string.find(objFile, "%s", secondCordPos+1)
  143. thirdCord = string.sub(objFile,secondCordPos+1,thirdCordPos-1)
  144.  
  145. filePos = thirdCordPos
  146.  
  147. if wrongAngleFixer then
  148. vertex = Vector3.new(tonumber(firstCord)*scale,tonumber(thirdCord)*scale,tonumber(secondCord)*scale) + offset * scale
  149. else
  150. vertex = Vector3.new(tonumber(firstCord)*scale,tonumber(secondCord)*scale,tonumber(thirdCord)*scale) + offset * scale
  151. end
  152.  
  153. table.insert(vertexes,vertex)
  154.  
  155. else
  156. print("Number of vertices : " .. #vertexes)
  157. endfile = true
  158. end
  159. end
  160. end
  161.  
  162. function parseFaces()
  163. faces = {}
  164. filePos = 0
  165. local endfile = false
  166. while not endfile do
  167. faceStringPos = string.find(objFile, "f ",filePos)
  168. if loadColors then
  169.  
  170. useMaterial = string.find(objFile, "usemtl",filePos)
  171.  
  172. if useMaterial and useMaterial < faceStringPos then
  173.  
  174. materialNameStartPos = string.find(objFile, "%s", useMaterial)
  175. materialNameEndPos = string.find(objFile, "%s", materialNameStartPos+1)
  176. materialName = string.sub(objFile,materialNameStartPos+1,materialNameEndPos-1)
  177.  
  178. useColor = materials[materialName]
  179.  
  180. end
  181. end
  182.  
  183. if faceStringPos then
  184.  
  185. face2startPos = string.find(objFile," ",faceStringPos+2)
  186. face3startPos = string.find(objFile," ",face2startPos+1)
  187.  
  188. facePos1String = objFile:match("%d+",faceStringPos)
  189. facePos2String = objFile:match("%d+",face2startPos)
  190. facePos3String = objFile:match("%d+",face3startPos)
  191.  
  192. face1Pos = vertexes[tonumber(facePos1String)]
  193. face2Pos = vertexes[tonumber(facePos2String)]
  194. face3Pos = vertexes[tonumber(facePos3String)]
  195.  
  196. table.insert(faces,{face1Pos,face2Pos,face3Pos,useColor})
  197.  
  198. filePos = face3startPos+1
  199. else
  200. print("Number of faces : " .. #faces)
  201. endfile = true
  202. end
  203. end
  204. end
  205.  
  206.  
  207. function renderVertexes()
  208. speedLoad = 0
  209. for key, value in pairs(vertexes) do
  210. if speedLoad > loadspeed then
  211. wait()
  212. speedLoad = 0
  213. end
  214. speedLoad = speedLoad + 1
  215. vertexPart = Instance.new("Part",Workspace)
  216. vertexPart.Size = Vector3.new(1,1,1)
  217. vertexPart.CFrame = CFrame.new(value.X,value.Y,value.Z)
  218. vertexPart.Name = key
  219. vertexPart.Anchored = true
  220. vertexPart.BrickColor = BrickColor.new("Really red")
  221. end
  222. end
  223.  
  224. function renderVertexesOfFace(face)
  225. for key, value in pairs(face) do
  226. vertexPart = Instance.new("Part",Workspace)
  227. vertexPart.Size = Vector3.new(1,1,1)
  228. vertexPart.CFrame = CFrame.new(value.X,value.Y,value.Z)
  229. vertexPart.Anchored = true
  230. vertexPart.BrickColor = BrickColor.new("Really red")
  231. end
  232. end
  233.  
  234. function renderFace(face)
  235. facesRendered = facesRendered + 1
  236. PolyDraw.Tri.new(face[1],face[2],face[3],face[4])
  237. end
  238.  
  239. function createFaceInfoHint()
  240. coroutine.wrap(function()
  241. local hint = Instance.new("Hint", Workspace)
  242. local startTime = tick()
  243. while wait() do
  244. hint.Text = "Faces rendered : "
  245. .. facesRendered
  246. .. " Percent done : "
  247. .. math.floor((facesRendered/#faces)*10000)/100
  248. .. "%"
  249. .. " Approx time left : "
  250. .. math.floor((((tick() - startTime) / facesRendered) * (#faces-facesRendered))*10)/10
  251. .. " seconds"
  252. end
  253. end)()
  254. end
  255.  
  256.  
  257. function renderFaces()
  258. speedLoad = 0
  259. facesRendered = 0
  260. createFaceInfoHint()
  261. for key,value in pairs(faces) do
  262. if speedLoad > loadspeed then
  263. wait()
  264. speedLoad = 0
  265. end
  266. speedLoad = speedLoad + 1
  267. renderFace(value)
  268. end
  269. print("Done rendering!")
  270. end
  271.  
  272. function renderWireFrame()
  273. speedLoad = 0
  274. for key,value in pairs(faces) do
  275. if speedLoad > loadspeed then
  276. wait()
  277. speedLoad = 0
  278. end
  279. speedLoad = speedLoad + 1
  280. renderWiresForFace(value)
  281. end
  282. end
  283.  
  284. wire = Instance.new("Part")
  285. wire.Anchored = true
  286. wire.formFactor = Enum.FormFactor.Custom
  287. wire.BrickColor = BrickColor.new("New Yeller")
  288.  
  289. function renderWiresForFace(face)
  290. renderWire(face[1],face[2])
  291. renderWire(face[2],face[3])
  292. renderWire(face[3],face[1])
  293. end
  294.  
  295. function renderWire(pos1,pos2)
  296. local distance = (pos1 - pos2).magnitude
  297. local wire = wire:Clone()
  298.  
  299. wire.Parent = Workspace
  300. wire.Size = Vector3.new(0.2,0.2,distance)
  301. wire.CFrame = CFrame.new( pos1, pos2) * CFrame.new(0, 0, -distance/2)
  302. end
  303.  
  304. mesh = Instance.new("Model", Workspace)
  305. mesh.Name = "Mesh"
  306.  
  307. PolyDraw.BrickColor = BrickColor.new(21)
  308. PolyDraw.CanCollide = false
  309.  
  310. local function setupPart(part)
  311. part.Anchored = true
  312. part.FormFactor = 'Custom'
  313. part.CanCollide = PolyDraw.CanCollide
  314. part.BrickColor = PolyDraw.BrickColor
  315. part.TopSurface = 'Smooth'
  316. part.BottomSurface = 'Smooth'
  317.  
  318. local mesh = Instance.new("SpecialMesh", part)
  319. mesh.MeshType = "Wedge"
  320. mesh.Scale = Vector3.new(0,1,1)
  321. end
  322.  
  323. local Point = {}
  324. PolyDraw.Point = Point
  325.  
  326. function Point.new(parent, at)
  327. local this = {}
  328. local mPart = Instance.new('Part')
  329. setupPart(mPart)
  330. mPart.Size = Vector3.new(1, 1, 1)
  331. mPart.Parent = parent
  332. function this:Set(at)
  333. mPart.CFrame = CFrame.new(at)
  334. end
  335. function this:Destroy()
  336. mPart:Destroy()
  337. end
  338. function this:SetProperty(prop, value)
  339. mPart[prop] = value
  340. end
  341. this:Set(at)
  342. return this
  343. end
  344.  
  345. local Edge = {}
  346. PolyDraw.Edge = Edge
  347.  
  348. function Edge.new(parent, a, b)
  349. local this = {}
  350. local mPart = Instance.new('Part')
  351. setupPart(mPart)
  352. mPart.Parent = parent
  353. function this:Set(a, b)
  354. local sep = (a-b).magnitude
  355. mPart.Size = Vector3.new(0.5, 0.5, sep)
  356. mPart.CFrame = CFrame.new(a, b) * CFrame.new(0, 0, -0.5*sep)
  357. end
  358. function this:Destroy()
  359. mPart:Destroy()
  360. end
  361. function this:SetProperty(prop, value)
  362. mPart[prop] = value
  363. end
  364. this:Set(a, b)
  365. return this
  366. end
  367.  
  368. Tri = {}
  369. PolyDraw.Tri = Tri
  370.  
  371. local function CFrameFromTopBack(at, top, back)
  372. local right = top:Cross(back)
  373. return CFrame.new(at.x, at.y, at.z,
  374. right.x, top.x, back.x,
  375. right.y, top.y, back.y,
  376. right.z, top.z, back.z)
  377. end
  378.  
  379. function Tri.new(a, b, c, color)
  380. local this = {}
  381. local mPart1 = Instance.new('WedgePart')
  382. setupPart(mPart1)
  383. local mPart2 = Instance.new('WedgePart')
  384. setupPart(mPart2)
  385. function this:Set(a, b, c)
  386. --[[ edg1
  387. A ------|------>B --.
  388. '\ | / \
  389. \part1|part2/ |
  390. \ cut / / Direction edges point in:
  391. edg3 \ / edg2 / (clockwise)
  392. \ / |/
  393. \<- / `
  394. \ /
  395. C
  396. --]]
  397. local ab, bc, ca = b-a, c-b, a-c
  398. local abm, bcm, cam = ab.magnitude, bc.magnitude, ca.magnitude
  399. local edg1 = math.abs(0.5 + ca:Dot(ab)/(abm*abm))
  400. local edg2 = math.abs(0.5 + ab:Dot(bc)/(bcm*bcm))
  401. local edg3 = math.abs(0.5 + bc:Dot(ca)/(cam*cam))
  402. -- Idea: Find the edge onto which the vertex opposite that
  403. -- edge has the projection closest to 1/2 of the way along that
  404. -- edge. That is the edge thatwe want to split on in order to
  405. -- avoid ending up with small "sliver" triangles with one very
  406. -- small dimension relative to the other one.
  407. if edg1 < edg2 then
  408. if edg1 < edg3 then
  409. -- min is edg1: less than both
  410. -- nothing to change
  411. else
  412. -- min is edg3: edg3 < edg1 < edg2
  413. -- "rotate" verts twice counterclockwise
  414. a, b, c = c, a, b
  415. ab, bc, ca = ca, ab, bc
  416. abm = cam
  417. end
  418. else
  419. if edg2 < edg3 then
  420. -- min is edg2: less than both
  421. -- "rotate" verts once counterclockwise
  422. a, b, c = b, c, a
  423. ab, bc, ca = bc, ca, ab
  424. abm = bcm
  425. else
  426. -- min is edg3: edg3 < edg2 < edg1
  427. -- "rotate" verts twice counterclockwise
  428. a, b, c = c, a, b
  429. ab, bc, ca = ca, ab, bc
  430. abm = cam
  431. end
  432. end
  433.  
  434. --calculate lengths
  435. local len1 = -ca:Dot(ab)/abm
  436. local len2 = abm - len1
  437. local width = (ca + ab.unit*len1).magnitude
  438.  
  439. --calculate "base" CFrame to pasition parts by
  440. local maincf = CFrameFromTopBack(a, ab:Cross(bc).unit, -ab.unit)
  441.  
  442. --make parts
  443. mPart1.Parent = mesh
  444. mPart1.Size = Vector3.new(0.2, 0.2, 0.2)
  445. mPart1.Mesh.Scale = Vector3.new(0.00001, width, len1)/0.2
  446. mPart1.CFrame = maincf*CFrame.Angles(math.pi,0,math.pi/2)*CFrame.new(0,width/2,len1/2)
  447. mPart1.Color = color or Color3.new(0, 0, 0.7)
  448. mPart2.Parent = mesh
  449. mPart2.Size = Vector3.new(0.2, 0.2, 0.2)
  450. mPart2.Mesh.Scale = Vector3.new(0.00001, width, len2)/0.2
  451. mPart2.CFrame = maincf*CFrame.Angles(math.pi,math.pi,-math.pi/2)*CFrame.new(0,width/2,-len1 - len2/2)
  452. mPart2.Color = color or Color3.new(0, 0, 0.7)
  453. end
  454. function this:SetProperty(prop, value)
  455. mPart1[prop] = value
  456. mPart2[prop] = value
  457. end
  458. this:Set(a, b, c)
  459. function this:Destroy()
  460. mPart1:Destroy()
  461. mPart2:Destroy()
  462. end
  463. return this
  464. end
  465.  
  466. -------------------------------------------------------------------------------------------------------------------------------------
  467.  
  468. if loadColors then
  469. parseMaterials()
  470. end
  471. parseVertexes()
  472. parseFaces()
  473.  
  474. if shouldrenderVertexes then
  475. renderVertexes()
  476. end
  477. if shouldrenderWireFrame then
  478. renderWireFrame()
  479. end
  480. if shouldrenderFaces then
  481. renderFaces()
  482. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement