Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Picker
  4.  
  5. -- Use this function to perform your initial setup
  6. function setup()
  7. p = Picker()
  8. end
  9.  
  10. -- This function gets called once every frame
  11. function draw()
  12. -- This sets a dark background color
  13. background(p.color or color())
  14.  
  15. -- This sets the line thickness
  16. strokeWidth(5)
  17.  
  18. -- Do your drawing here
  19. translate(WIDTH/2,HEIGHT/2)
  20. p:draw()
  21. end
  22.  
  23. function touched(touch)
  24. -- if touch.state == BEGAN then
  25. local t = vec2(touch.x-WIDTH/2,touch.y-HEIGHT/2)
  26. p:touched(t)
  27. -- end
  28. end
  29.  
  30. --# Picker
  31. Picker = class()
  32.  
  33. function Picker:init(size)
  34. self.colors = {
  35. color(255, 255, 255, 255),
  36. color(255, 0, 0, 255),
  37. color(255, 255, 0, 255),
  38. color(0, 255, 0, 255),
  39. color(0, 255, 255, 255),
  40. color(0, 0, 255, 255),
  41. color(255, 0, 255, 255)
  42. }
  43. self.size = size or 200
  44. self:createCircle()
  45. self:createHexagon()
  46. self:touched(vec2(0,0))
  47. end
  48.  
  49. function Picker:createHexagon()
  50. local vs, cs, cl = {}, {}, self.colors
  51.  
  52. for i=1,6 do
  53. local a = i/6 * math.pi * 2
  54. local b = (i+1)/6 * math.pi * 2
  55. table.insert(vs, vec2(0,0))
  56. table.insert(cs, cl[1])
  57. table.insert(vs, vec2(math.cos(a), math.sin(a)) * self.size)
  58. table.insert(cs, cl[i+1])
  59. table.insert(vs, vec2(math.cos(b), math.sin(b)) * self.size)
  60. table.insert(cs, cl[i+2] or cl[2])
  61. end
  62. self.hexagon = mesh()
  63. self.hexagon.vertices = vs
  64. self.hexagon.colors = cs
  65. self.hexagon.shader = circleShader()
  66. end
  67.  
  68. function Picker:createCircle()
  69. local vs,n,s = {},16,self.size*.15
  70. for i=1,n do
  71. local a = i/n * math.pi * 2
  72. table.insert(vs, vec2(math.cos(a), math.sin(a)) * s)
  73. end
  74. self.circle = mesh()
  75. self.circle.vertices = triangulate(vs)
  76. vs = {}
  77. for i=1,n do
  78. local a = i/n * math.pi * 2
  79. table.insert(vs, vec2(math.cos(a), math.sin(a)) * (s+1))
  80. end
  81. self.bcircle = mesh()
  82. self.bcircle.vertices = triangulate(vs)
  83. self.bcircle:setColors(0,0,0)
  84. end
  85.  
  86. function Picker:draw()
  87. self.hexagon.shader.p = self.pos
  88. self.hexagon.shader.c = self.color
  89. self.hexagon:draw()
  90. if false then
  91. pushMatrix()
  92. translate(self.pos.x, self.pos.y)
  93. self.circle:setColors(self.color)
  94. self.bcircle:draw()
  95. self.circle:draw()
  96. popMatrix()
  97. end
  98. end
  99.  
  100. function Picker:touched(p)
  101. p = vec3(p.x,p.y,0) -- vec3 in mesh
  102. local h = self.hexagon
  103. for i=1,#h.vertices,3 do
  104. local a,b,c = h.vertices[i], h.vertices[i+1], h.vertices[i+2]
  105. local inside, u, v = insideTriangle(a,b,c,p)
  106. if inside then
  107. local d,e,f = h.colors[i]*255, h.colors[i+1]*255, h.colors[i+2]*255
  108. self.color = (1-u-v)*d + u*f + v*e
  109. self.pos = vec2(p.x, p.y)
  110. return
  111. end
  112. end
  113. end
  114.  
  115. function circleShader()
  116. return shader([[
  117. uniform mat4 modelViewProjection;
  118. attribute vec4 position;
  119. varying highp vec2 vTexCoord;
  120. attribute vec4 color;
  121. varying lowp vec4 vColor;
  122.  
  123. void main() {
  124. vTexCoord = position.xy;
  125. vColor = color;
  126. gl_Position = modelViewProjection * position;
  127. }
  128. ]],[[
  129. precision highp float;
  130. varying lowp vec4 vColor;
  131. varying highp vec2 vTexCoord;
  132. uniform vec2 p;
  133. uniform vec4 c;
  134.  
  135. void main() {
  136. lowp vec4 col = vColor;
  137. float l = length(vTexCoord-p.xy);
  138. if(l<20.) {
  139. col = c;
  140. } else if(l<22.) {
  141. col = vec4(0.,0.,0.,1.);
  142. }
  143. gl_FragColor = col;
  144. }
  145. ]])
  146. end
  147.  
  148. --# Utils
  149. function uv(a,b,c,p)
  150. local v0 = c - a
  151. local v1 = b - a
  152. local v2 = p - a
  153. local dot00 = v0:dot(v0)
  154. local dot01 = v0:dot(v1)
  155. local dot02 = v0:dot(v2)
  156. local dot11 = v1:dot(v1)
  157. local dot12 = v1:dot(v2)
  158.  
  159. -- Compute barycentric coordinates
  160. local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
  161. local u = (dot11 * dot02 - dot01 * dot12) * invDenom
  162. local v = (dot00 * dot12 - dot01 * dot02) * invDenom
  163.  
  164. return u,v
  165. end
  166.  
  167. function insideTriangle(a,b,c,p)
  168. local u,v = uv(a,b,c,p)
  169. return (u >= 0) and (v >= 0) and (u + v < 1), u, v
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement