Advertisement
9551

Untitled

Aug 26th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. -- AR tools API
  2.  
  3. --Draws a line from x1,y1 to x2,y2 with given color, using name of wrapped AR controller
  4. local function drawLineAR(arController,x1,y1,x2,y2,color)
  5. --color = 0x000000 --Overriding color to black. Comment out to use custom colors
  6. --arController.addLine({x1,y1},{x2,y2},color,0.5)
  7. end
  8.  
  9. --Basically a line function with cutoffs for anything < 0.
  10. local function drawHLine(arController,x1,x2,y1,color)
  11. local width = 0.5
  12. local border = width*2
  13. if y1 > border and x1 > border then
  14. arController.horizontalLine(x1, x2, y1, color)
  15. end
  16. end
  17.  
  18. --Draws an object with an indexList which points to a list x/y points, using name of wrapped AR controller
  19. local function drawWireObj(arController,iL,vL,cL) --vL == vertice list. cL == color list
  20. local vList = {}
  21. for i = 1, #iL, 3 do --Draws a whole triangle per loop
  22. if vL.cullFlags[i] >= 0 then --if not marked for culling then render
  23. local i1,i2 = i+1, i+2
  24. local col = cL[(i+2)/3]
  25. drawLineAR(arController, vL[ iL[i] ], vL[ iL[i] +1 ], vL[ iL[i1] ], vL[ iL[i1]+1 ], col) --vert 1 to 2
  26. drawLineAR(arController, vL[ iL[i1] ], vL[ iL[i1]+1 ], vL[ iL[i2] ], vL[ iL[i2]+1 ], col) --vert 2 to 3
  27. drawLineAR(arController, vL[ iL[i2] ], vL[ iL[i2]+1 ], vL[ iL[i] ], vL[ iL[i] +1 ], col) --vert 3 to 1
  28. end
  29. end
  30. end
  31.  
  32. --Both draw flat top and bottom draw an entire line at a time instead of each pixel individually b/c performance. Will add individual pixel support later.
  33. local function drawFlatTopTriangle( arController,vec1,vec2,vec3,color )
  34. --Calculate slopes in screen space
  35. --Run over rise so we don't get infinite slopes
  36. local m1 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  37. local m2 = (vec3.x - vec2.x) / (vec3.y - vec2.y)
  38.  
  39. --Calculate start and end scanlines
  40. local yStart = math.ceil(vec1.y - 0.5)
  41. local yEnd = math.ceil(vec3.y - 0.5)-1 --the scanline AFTER the last line drawn
  42.  
  43. for y = yStart, yEnd do
  44. --calculate start and end x's
  45. --Add 0.5 because we're calculating based on pixel centers
  46. local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  47. local px2 = m2 * (y + 0.5 - vec2.y) + vec2.x
  48.  
  49. --calculate start and end pixelels
  50. local xStart = math.ceil(px1 - 0.5)
  51. local xEnd = math.ceil(px2 - 0.5) --the pixel after the last pixel drawn
  52.  
  53. for x = xStart, xEnd do
  54. --drawHLine( arController,xStart,xEnd,y,color )
  55. arController.addDot({x,y},color,1)
  56. end
  57. end
  58. end
  59.  
  60. local function drawFlatBottomTriangle( arController,vec1,vec2,vec3,color )
  61. --Calculate slopes in screen space
  62. local m1 = (vec2.x - vec1.x) / (vec2.y - vec1.y)
  63. local m2 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  64.  
  65. --Calculate start and end scanlines
  66. local yStart = math.ceil(vec1.y-0.5)
  67. local yEnd = math.ceil(vec3.y-0.5)-1 --the scanline AFTER the last line drawn, which is why we need to subtract 1 otherwise we get visual bugs
  68.  
  69. for y = yStart, yEnd do
  70.  
  71. --calculate start and end points (x coords)
  72. --Add 0.5 because we're calculating based on pixel centers
  73. local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  74. local px2 = m2 * (y + 0.5 - vec1.y) + vec1.x
  75.  
  76. --calculate start and end pixelsh
  77. local xStart = math.ceil(px1 - 0.5)
  78. local xEnd = math.ceil(px2 - 0.5)
  79.  
  80. for x = xStart, xEnd do
  81. --drawHLine( arController,xStart,xEnd,y,color )
  82. arController.addDot({x,y},color,1)
  83. end
  84. end
  85. end
  86.  
  87. --Draws a solid triangle from 3 vectors
  88. local function drawSolidTriangle( arController,vec1,vec2,vec3,color )
  89.  
  90. --using pointers so we can swap (for sorting purposes) probably don't need this b/c lua? idk
  91. local pv1 = vec1
  92. local pv2 = vec2
  93. local pv3 = vec3
  94.  
  95. --Sort vertices by y
  96. if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  97. if pv3.y < pv2.y then pv2,pv3 = pv3,pv2 end
  98. if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  99.  
  100. if pv1.y == pv2.y then --Natural flat top
  101. --Sort top vertice by x
  102. if pv2.x < pv1.x then pv1,pv2 = pv2,pv1 end
  103. drawFlatTopTriangle(arController,pv1,pv2,pv3,color )
  104.  
  105. elseif pv2.y == pv3.y then --Natural flat bottom
  106. --Sort bottom vertice by x
  107. if pv3.x < pv2.x then pv3,pv2 = pv2,pv3 end
  108. drawFlatBottomTriangle(arController,pv1,pv2,pv3,color )
  109.  
  110. else --General triangle
  111. local alphaSplit = (pv2.y-pv1.y)/(pv3.y-pv1.y)
  112. local vi ={
  113. x = pv1.x + ((pv3.x - pv1.x) * alphaSplit),
  114. y = pv1.y + ((pv3.y - pv1.y) * alphaSplit), }
  115. if pv2.x < vi.x then --Major right
  116. drawFlatBottomTriangle(arController,pv1,pv2,vi,color)
  117. drawFlatTopTriangle(arController,pv2,vi,pv3,color)
  118. else --Major left
  119. drawFlatBottomTriangle(arController,pv1,vi,pv2,color)
  120. drawFlatTopTriangle(arController,vi,pv2,pv3,color)
  121. end
  122. end
  123. end
  124.  
  125. --Draw an entire object one triangle at a time
  126. --Converts points from the 1D table to vectors b/c I'm lazy
  127. local function drawSolidObj( arController,iL,vL,cL ) --iL == index List. vL == vertex list. cL == color list
  128. --OPTIMIZATION here?. Probably should convert to 1D at least
  129. for i = 1, #iL, 3 do --Try to draw a triangle
  130. if vL.cullFlags[i] >= 0 then --If true then render, else cull that polygon
  131. local i1,i2 = i+1, i+2
  132. local vec1 = { x= vL[ iL[i] ], y= vL[ iL[i]+1 ], z= vL[ iL[i]+2 ] } --Target vertex1's x/y and z
  133. local vec2 = { x= vL[ iL[i1] ], y= vL[ iL[i1]+1 ], z= vL[ iL[i1]+2 ] } --Target vertex2's x/y and z
  134. local vec3 = { x= vL[ iL[i2] ], y= vL[ iL[i2]+1 ], z= vL[ iL[i2]+2 ] } --Target vertex3's x/y and z
  135. --drawSolidTriangle(arController,vec1,vec2,vec3, cL[(i2)/3])
  136. local tr = arController.addTriangle(vec1, vec2, vec3)
  137. tr.setAlpha(255)
  138. tr.setColor(cL[(i2)/3][1],cL[(i2)/3][2],cL[(i2)/3][3])
  139. end
  140. end
  141. end
  142.  
  143. --Expose functions
  144. return
  145. {
  146. drawWireObj = drawWireObj,
  147. drawSolidObj = drawSolidObj
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement