vertexcolor.script 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function init(self)
  2. msg.post(".", "acquire_input_focus")
  3. local scale = 0.75
  4. local spacingx = 160 * scale + 10
  5. local spacingy = 190 * scale + 10
  6. local startx = 40 + spacingx*0.5
  7. local starty = 40 + spacingy*0.5
  8. local maxy = 3
  9. local maxx = 4
  10. self.urls = {}
  11. -- 1. For all sprites in the example we set a slightly different `mycolor` vertex attribute:
  12. for y = 0, maxy do
  13. for x = 0, maxx do
  14. local p = vmath.vector3(startx + x*spacingx, starty + y*spacingy, 0.5)
  15. local id = factory.create("#factory", p, nil, nil, vmath.vector3(0.8, 0.8, 1))
  16. local url = msg.url(nil, id, "sprite")
  17. table.insert(self.urls, url)
  18. -- set vertex attribute:
  19. go.set(url, "mycolor", vmath.vector4(x/maxx, y/maxy, 0, 1))
  20. end
  21. end
  22. self.updated = false
  23. self.animation_finished = true
  24. end
  25. function update(self, dt)
  26. self.updated = true
  27. end
  28. function on_input(self, action_id, action)
  29. -- 2. On click we animate the `mycolor` vertex attribute of each of the sprites to blue and back.
  30. if action_id == hash("touch") and action.pressed and self.updated and self.animation_finished then
  31. for _, url in ipairs(self.urls) do
  32. self.animation_finished = false
  33. -- animate vertex attribute:
  34. go.animate(url, "mycolor", go.PLAYBACK_ONCE_PINGPONG, vmath.vector4(0, 0, 1, 1), go.EASING_LINEAR, 1, 0, function()
  35. self.animation_finished = true
  36. end)
  37. end
  38. end
  39. end