gatunes

OrbitControls

Jun 15th, 2023 (edited)
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. OrbitControls = {
  2.   dragging = false,
  3.   maxRadius = 100,
  4.   minRadius = 1,
  5.   phi = math.pi * 0.5,
  6.   theta = math.pi,
  7.   radius = 10,
  8.   target = { x = 0, y = 0, z = 0 },
  9.   dragStart = { x = 0, y = 0 },
  10.   orbitDamp = 20,
  11.   targetDamp = 5,
  12. }
  13.  
  14. function OrbitControls:new(o)
  15.   o = o or {}
  16.   setmetatable(o, self)
  17.   self.__index = self
  18.   o.dphi = o.phi
  19.   o.dtheta = o.theta
  20.   o.dradius = o.radius
  21.   o.dtarget = { x = o.target.x, y = o.target.y, z = o.target.z }
  22.   OrbitControls.setView(o)
  23.   return o
  24. end
  25.  
  26. function OrbitControls:update()
  27.   if mouse.primaryDown then
  28.     self.dragStart.x = mouse.x
  29.     self.dragStart.y = mouse.y
  30.   end
  31.   if mouse.primaryUp then
  32.     self.dragging = false
  33.   end
  34.  
  35.   if self.dragging then
  36.     self.phi = math.clamp(self.phi + mouse.dy * resolution.y * 0.003, 0.0001, math.pi - 0.0001)
  37.     self.theta = self.theta - mouse.dx * resolution.x * 0.003
  38.   elseif mouse.primary and (math.abs(self.dragStart.x - mouse.x) * resolution.x > 10 or math.abs(self.dragStart.y - mouse.y) * resolution.y > 10) then
  39.     self.dragging = true
  40.   end
  41.  
  42.   if mouse.wheel ~= 0 then
  43.     self.radius = math.clamp(self.radius * (1 - mouse.wheel * 0.1), self.minRadius, self.maxRadius)
  44.   end
  45.  
  46.   local needsUpdate = false
  47.  
  48.   if (
  49.     math.abs(self.phi - self.dphi) > 0.0001
  50.     or math.abs(self.theta - self.dtheta) > 0.0001
  51.     or math.abs(self.radius - self.dradius) > 0.0001
  52.   ) then
  53.     local damp = 1 - math.exp(-self.orbitDamp * delta)
  54.     self.dphi = math.lerp(self.dphi, self.phi, damp)
  55.     self.dtheta = math.lerp(self.dtheta, self.theta, damp)
  56.     self.dradius = math.lerp(self.dradius, self.radius, damp)
  57.     needsUpdate = true
  58.   end
  59.  
  60.   if (
  61.     math.abs(self.target.x - self.dtarget.x) > 0.0001
  62.     or math.abs(self.target.y - self.dtarget.y) > 0.0001
  63.     or math.abs(self.target.z - self.dtarget.z) > 0.0001
  64.   ) then
  65.     local damp = 1 - math.exp(-self.targetDamp * delta)
  66.     self.dtarget.x = math.lerp(self.dtarget.x, self.target.x, damp)
  67.     self.dtarget.y = math.lerp(self.dtarget.y, self.target.y, damp)
  68.     self.dtarget.z = math.lerp(self.dtarget.z, self.target.z, damp)
  69.     needsUpdate = true
  70.   end
  71.  
  72.   if needsUpdate then
  73.     OrbitControls.setView(self)
  74.   end
  75. end
  76.  
  77. function OrbitControls:setView()
  78.   local x, y, z = math.spherical(self.dphi, self.dtheta, self.dradius)
  79.   camera.setPosition(self.dtarget.x + x, self.dtarget.y + y, self.dtarget.z + z)
  80.   camera.lookAt(self.dtarget.x, self.dtarget.y, self.dtarget.z)
  81. end
  82.  
Advertisement
Add Comment
Please, Sign In to add comment