repeating_background.script 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- Size of a single tile in pixels
  2. go.property("tile_size", 128)
  3. -- Scroll speed vector (x, y, z) in pixels per second
  4. go.property("scroll_speed", vmath.vector3(50, 0, 0))
  5. -- Applies layout based on current window size
  6. -- Scales the game object to fill the entire window and calculates UV scale
  7. local function apply_layout(self)
  8. local width, height = window.get_size()
  9. -- Scale the game object to match window dimensions
  10. go.set(".", "scale", vmath.vector3(width, height, 1))
  11. -- Calculate how many tiles fit in the window (for UV tiling)
  12. self.uv_scale = vmath.vector3(width / self.tile_size, height / self.tile_size, 0)
  13. -- Send UV parameters to the shader: scale (x, y) and offset (z, w)
  14. local uv_params = vmath.vector4(self.uv_scale.x, self.uv_scale.y, self.offset.x, self.offset.y)
  15. go.set("#model", "uv_params", uv_params)
  16. end
  17. -- Updates UV offset for scrolling animation
  18. -- Moves the texture offset based on scroll speed and wraps it using modulo
  19. local function update_uv_params(self, dt)
  20. -- Calculate offset delta in tile units (0-1 range)
  21. local delta = self.scroll_speed * dt / self.tile_size
  22. -- Update offset (subtract because we want to scroll in the direction of scroll_speed)
  23. self.offset = self.offset - delta
  24. -- Wrap offset to 0-1 range to create seamless repeating
  25. self.offset.x = self.offset.x % 1
  26. self.offset.y = self.offset.y % 1
  27. -- Send updated UV parameters to the shader
  28. local uv_params = vmath.vector4(self.uv_scale.x, self.uv_scale.y, self.offset.x, self.offset.y)
  29. go.set("#model", "uv_params", uv_params)
  30. end
  31. -- Initialize the script
  32. -- Sets up the initial UV offset to zero
  33. function init(self)
  34. self.offset = vmath.vector3(0)
  35. end
  36. function final(self)
  37. end
  38. -- Update function called every frame
  39. -- Applies layout and updates UV parameters for scrolling
  40. function update(self, dt)
  41. apply_layout(self)
  42. update_uv_params(self, dt)
  43. end