OrbitCamera.re.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. export default class OrbitCamera extends RE.Component {
  4. @RE.Prop("Object3D") target
  5. rotateStart = new THREE.Vector2()
  6. rotateEnd = new THREE.Vector2()
  7. rotateDelta = new THREE.Vector2()
  8. @RE.Prop("Boolean") enableRotate = true
  9. @RE.Prop("Number") rotateSpeed = 1.0
  10. // How far you can orbit horizontally, upper and lower limits.
  11. // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
  12. @RE.Prop("Number") minAzimuthAngle = - Infinity // radians
  13. @RE.Prop("Number") maxAzimuthAngle = Infinity // radians
  14. // How far you can orbit vertically, upper and lower limits.
  15. // Range is 0 to Math.PI radians.
  16. @RE.Prop("Number") minPolarAngle = 0 // radians
  17. @RE.Prop("Number") maxPolarAngle = Math.PI // radians
  18. @RE.Prop("Boolean") enableDolly = true
  19. @RE.Prop("Number") dollySpeed = 1.0
  20. @RE.Prop("Number") minDistance = 1
  21. @RE.Prop("Number") maxDistance = 50
  22. scale = 1
  23. EPS = 0.000001
  24. twoPI = 2 * Math.PI
  25. spherical = new THREE.Spherical()
  26. sphericalDelta = new THREE.Spherical()
  27. @RE.Prop("Vector3") offset = new THREE.Vector3()
  28. @RE.Prop("Number") rotateMouseButton = 2
  29. quat = new THREE.Quaternion().setFromUnitVectors( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 1, 0 ) )
  30. quatInverse = this.quat.clone().invert()
  31. lastPosition = new THREE.Vector3()
  32. lastQuaternion = new THREE.Quaternion()
  33. initialOffset = new THREE.Vector3()
  34. awake() {
  35. }
  36. start() {
  37. RE.Runtime.rogueDOMContainer.addEventListener('contextmenu', event => { event.preventDefault() })
  38. // so camera.up is the orbit axis
  39. this.quat = new THREE.Quaternion().setFromUnitVectors( this.object3d.up, new THREE.Vector3( 0, 1, 0 ) )
  40. this.quatInverse = this.quat.clone().invert()
  41. this.initialOffset.copy(this.object3d.position).sub(this.target.position)
  42. this.offset.copy(this.initialOffset)
  43. }
  44. update() {
  45. let normalizedScreenPosition = new THREE.Vector2(0,0)
  46. this.normalizeScreenInput(RE.Input.mouse, normalizedScreenPosition)
  47. if(this.enableRotate && RE.Input.mouse.getButtonDown(this.rotateMouseButton)) {
  48. this.rotateStart.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  49. }
  50. if(this.enableRotate && RE.Input.mouse.getButtonPressed(this.rotateMouseButton)) {
  51. this.rotateEnd.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  52. this.rotateDelta.subVectors( this.rotateEnd, this.rotateStart ).multiplyScalar( this.rotateSpeed )
  53. this.rotateLeft(this.twoPI * this.rotateDelta.x)
  54. this.rotateUp(this.twoPI * -this.rotateDelta.y)
  55. this.rotateStart.copy( this.rotateEnd )
  56. }
  57. if(this.enableDolly && RE.Input.mouse.wheelY > 0) {
  58. this.dollyOut()
  59. }
  60. if(this.enableDolly && RE.Input.mouse.wheelY < 0) {
  61. this.dollyIn()
  62. }
  63. this.updateCamera()
  64. }
  65. rotateLeft(angle) {
  66. this.sphericalDelta.theta -= angle
  67. }
  68. rotateUp(angle) {
  69. this.sphericalDelta.phi -= angle
  70. }
  71. updateCamera() {
  72. // rotate offset to "y-axis-is-up" space
  73. this.offset.applyQuaternion(this.quat)
  74. // angle from z-axis around y-axis
  75. this.spherical.setFromVector3(this.offset)
  76. this.spherical.theta += this.sphericalDelta.theta
  77. this.spherical.phi += this.sphericalDelta.phi
  78. // restrict theta to be between desired limits
  79. let min = this.minAzimuthAngle
  80. let max = this.maxAzimuthAngle
  81. if ( isFinite(min) && isFinite(max) ) {
  82. if (min < -Math.PI) {
  83. min += this.twoPI
  84. } else if (min > Math.PI) {
  85. min -= this.twoPI
  86. }
  87. if (max < - Math.PI) {
  88. max += this.twoPI
  89. } else if (max > Math.PI) {
  90. max -= this.twoPI
  91. }
  92. if (min <= max) {
  93. this.spherical.theta = Math.max(min, Math.min(max, this.spherical.theta))
  94. } else {
  95. this.spherical.theta = (this.spherical.theta > (min + max) / 2 ) ?
  96. Math.max( min, this.spherical.theta ) :
  97. Math.min( max, this.spherical.theta )
  98. }
  99. }
  100. // restrict phi to be between desired limits
  101. this.spherical.phi = Math.max(this.minPolarAngle, Math.min(this.maxPolarAngle, this.spherical.phi))
  102. this.spherical.makeSafe()
  103. this.spherical.radius *= this.scale
  104. // restrict radius to be between desired limits
  105. this.spherical.radius = Math.max(this.minDistance, Math.min(this.maxDistance, this.spherical.radius))
  106. this.offset.setFromSpherical(this.spherical)
  107. // rotate offset back to "camera-up-vector-is-up" space
  108. this.offset.applyQuaternion(this.quatInverse)
  109. this.object3d.position.copy(this.target.position).add(this.offset)
  110. this.object3d.lookAt(this.target.position)
  111. this.sphericalDelta.set(0, 0, 0)
  112. this.scale = 1
  113. // update condition is:
  114. // min(camera displacement, camera rotation in radians)^2 > EPS
  115. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  116. if (this.lastPosition.distanceToSquared(this.object3d.position) > this.EPS ||
  117. 8 * (1 - this.lastQuaternion.dot(this.object3d.quaternion)) > this.EPS) {
  118. this.lastPosition.copy(this.object3d.position)
  119. this.lastQuaternion.copy(this.object3d.quaternion)
  120. }
  121. }
  122. dollyOut() {
  123. this.scale /= Math.pow(0.95, this.dollySpeed)
  124. }
  125. dollyIn() {
  126. this.scale *= Math.pow(0.95, this.dollySpeed)
  127. }
  128. normalizeScreenInput(browserVector, gameVector) {
  129. const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect()
  130. gameVector.x = ((browserVector.x - bounds.left) / bounds.width) * 2 - 1
  131. gameVector.y = -((browserVector.y - bounds.top) / bounds.height) * 2 + 1
  132. }
  133. }
  134. RE.registerComponent(OrbitCamera)