RotationController.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * RotationController.js
  3. *
  4. * @author realor
  5. */
  6. import { AnimationController } from './AnimationController.js'
  7. import { Controller } from './Controller.js'
  8. class RotationController extends AnimationController {
  9. constructor(object, name) {
  10. super(object, name)
  11. let minAngle = (object.rotation.z * 180) / Math.PI
  12. let maxAngle = minAngle + 90
  13. this.axis = 'z'
  14. this.minAngle = minAngle // degrees
  15. this.maxAngle = maxAngle // degrees
  16. this.minValue = 0
  17. this.maxValue = 1
  18. this.maxSpeed = 90 // degrees / second
  19. }
  20. animate(event) {
  21. let value = this.input
  22. if (value === null) return
  23. value = parseFloat(value)
  24. if (typeof value !== 'number') return
  25. let axis = this.axis || 'z'
  26. let minValue = parseFloat(this.minValue)
  27. let maxValue = parseFloat(this.maxValue)
  28. let minAngle = (parseFloat(this.minAngle) * Math.PI) / 180
  29. let maxAngle = (parseFloat(this.maxAngle) * Math.PI) / 180
  30. let maxSpeed = (parseFloat(this.maxSpeed) * Math.PI) / 180
  31. let factor = (value - minValue) / (maxValue - minValue) // [0..1]
  32. let targetAngle = minAngle + factor * (maxAngle - minAngle)
  33. let angle = this.object.rotation[axis]
  34. let delta = targetAngle - angle
  35. if (Math.abs(delta) < 0.001) {
  36. this.stopAnimation()
  37. } else {
  38. let speed = delta / 0.2
  39. if (speed > maxSpeed) speed = maxSpeed
  40. else if (speed < -maxSpeed) speed = -maxSpeed
  41. this.object.rotation[axis] += speed * event.delta
  42. this.object.updateMatrix()
  43. this.application.notifyObjectsChanged(this.object, this)
  44. }
  45. }
  46. }
  47. Controller.addClass(RotationController)
  48. export { RotationController }