TargetedOrbitCamera.re.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. export default class TargetedOrbitCamera extends RE.Component {
  4. @RE.props.object3d() target
  5. @RE.props.checkbox() enableRotate = true
  6. @RE.props.num() rotateSpeed = 1.0
  7. // How far you can orbit horizontally, upper and lower limits.
  8. // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
  9. @RE.props.num() minAzimuthAngle = - 6.28 // 2 PI radians
  10. @RE.props.num() maxAzimuthAngle = 6.28 // -2 PI radians
  11. // How far you can orbit vertically, upper and lower limits.
  12. // Range is 0 to Math.PI radians.
  13. @RE.props.num() minPolarAngle = 0 // radians
  14. @RE.props.num() maxPolarAngle = 1.56 // 0.5 PI radians
  15. @RE.props.checkbox() enableDolly = true
  16. @RE.props.num() dollySpeed = 1.0
  17. @RE.props.num() minDistance = 1
  18. @RE.props.num() maxDistance = 50
  19. @RE.props.vector3() offset = new THREE.Vector3()
  20. @RE.props.num() rotateMouseButton = 2
  21. @RE.props.checkbox() useCameraLock = false
  22. rotateStart = new THREE.Vector2()
  23. rotateEnd = new THREE.Vector2()
  24. rotateDelta = new THREE.Vector2()
  25. scale = 1
  26. EPS = 0.000001
  27. twoPI = 2 * Math.PI
  28. spherical = new THREE.Spherical()
  29. sphericalDelta = new THREE.Spherical()
  30. quat = new THREE.Quaternion().setFromUnitVectors( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 1, 0 ) )
  31. quatInverse = this.quat.clone().invert()
  32. lastPosition = new THREE.Vector3()
  33. lastQuaternion = new THREE.Quaternion()
  34. initialOffset = new THREE.Vector3()
  35. awake() {
  36. if(this.useCameraLock) {
  37. const container = document.getElementById(RE.Runtime.containerId)
  38. // container?.addEventListener("mousedown", this.lock.bind(this), false)
  39. if(container) {
  40. container.onmousedown = this.lock.bind(this)
  41. RE.Runtime.onStop(this.dispose)
  42. }
  43. }
  44. }
  45. start() {
  46. if(!this.target) {
  47. RE.Debug.logError("Orbit controls not set to an Object3D Target.")
  48. return
  49. }
  50. RE.Runtime.rogueDOMContainer.addEventListener('contextmenu', event => { event.preventDefault() })
  51. // so camera.up is the orbit axis
  52. this.quat = new THREE.Quaternion().setFromUnitVectors( this.object3d.up, new THREE.Vector3( 0, 1, 0 ) )
  53. this.quatInverse = this.quat.clone().invert()
  54. this.initialOffset.copy(this.object3d.position).sub(this.target.position)
  55. this.offset.copy(this.initialOffset)
  56. }
  57. update() {
  58. if(!this.target) {
  59. return
  60. }
  61. if(this.useCameraLock && !!document.pointerLockElement) {
  62. if(this.enableRotate && RE.Input.mouse.isMoving) {
  63. let normalizedScreenMovement = new THREE.Vector2(0,0)
  64. this.normalizeScreenMovement({x: RE.Input.mouse.movementX, y: RE.Input.mouse.movementY}, normalizedScreenMovement)
  65. this.rotateLeft(this.twoPI * normalizedScreenMovement.x)
  66. this.rotateUp(this.twoPI * -normalizedScreenMovement.y)
  67. }
  68. } else {
  69. let normalizedScreenPosition = new THREE.Vector2(0,0)
  70. this.normalizeScreenInput(RE.Input.mouse, normalizedScreenPosition)
  71. if(this.enableRotate && RE.Input.mouse.getButtonDown(this.rotateMouseButton)) {
  72. this.rotateStart.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  73. }
  74. if(this.enableRotate && RE.Input.mouse.getButtonPressed(this.rotateMouseButton)) {
  75. this.rotateEnd.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  76. this.rotateDelta.subVectors( this.rotateEnd, this.rotateStart ).multiplyScalar( this.rotateSpeed )
  77. this.rotateLeft(this.twoPI * this.rotateDelta.x)
  78. this.rotateUp(this.twoPI * -this.rotateDelta.y)
  79. this.rotateStart.copy( this.rotateEnd )
  80. }
  81. }
  82. if(this.enableDolly && RE.Input.mouse.wheelY > 0) {
  83. this.dollyOut()
  84. }
  85. if(this.enableDolly && RE.Input.mouse.wheelY < 0) {
  86. this.dollyIn()
  87. }
  88. this.updateCamera()
  89. if(this.useCameraLock && RE.Input.keyboard.getKeyPressed("Escape")) {
  90. this.unlock()
  91. }
  92. }
  93. dispose() {
  94. const container = document.getElementById(RE.Runtime.containerId)
  95. if(container) {
  96. // container?.removeEventListener("mousedown", this.lock)
  97. container.onmousedown = null
  98. }
  99. }
  100. rotateLeft(angle) {
  101. this.sphericalDelta.theta -= angle
  102. }
  103. rotateUp(angle) {
  104. this.sphericalDelta.phi -= angle
  105. }
  106. updateCamera() {
  107. // rotate offset to "y-axis-is-up" space
  108. this.offset.applyQuaternion(this.quat)
  109. // angle from z-axis around y-axis
  110. this.spherical.setFromVector3(this.offset)
  111. this.spherical.theta += this.sphericalDelta.theta
  112. this.spherical.phi += this.sphericalDelta.phi
  113. // restrict theta to be between desired limits
  114. let min = this.minAzimuthAngle
  115. let max = this.maxAzimuthAngle
  116. if ( isFinite(min) && isFinite(max) ) {
  117. if (min < -Math.PI) {
  118. min += this.twoPI
  119. } else if (min > Math.PI) {
  120. min -= this.twoPI
  121. }
  122. if (max < - Math.PI) {
  123. max += this.twoPI
  124. } else if (max > Math.PI) {
  125. max -= this.twoPI
  126. }
  127. if (min <= max) {
  128. this.spherical.theta = Math.max(min, Math.min(max, this.spherical.theta))
  129. } else {
  130. this.spherical.theta = (this.spherical.theta > (min + max) / 2 ) ?
  131. Math.max( min, this.spherical.theta ) :
  132. Math.min( max, this.spherical.theta )
  133. }
  134. }
  135. // restrict phi to be between desired limits
  136. this.spherical.phi = Math.max(this.minPolarAngle, Math.min(this.maxPolarAngle, this.spherical.phi))
  137. this.spherical.makeSafe()
  138. this.spherical.radius *= this.scale
  139. // restrict radius to be between desired limits
  140. this.spherical.radius = Math.max(this.minDistance, Math.min(this.maxDistance, this.spherical.radius))
  141. this.offset.setFromSpherical(this.spherical)
  142. // rotate offset back to "camera-up-vector-is-up" space
  143. this.offset.applyQuaternion(this.quatInverse)
  144. this.object3d.position.copy(this.target.position).add(this.offset)
  145. this.object3d.lookAt(this.target.position)
  146. this.sphericalDelta.set(0, 0, 0)
  147. this.scale = 1
  148. // update condition is:
  149. // min(camera displacement, camera rotation in radians)^2 > EPS
  150. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  151. if (this.lastPosition.distanceToSquared(this.object3d.position) > this.EPS ||
  152. 8 * (1 - this.lastQuaternion.dot(this.object3d.quaternion)) > this.EPS) {
  153. this.lastPosition.copy(this.object3d.position)
  154. this.lastQuaternion.copy(this.object3d.quaternion)
  155. }
  156. }
  157. dollyOut() {
  158. this.scale /= Math.pow(0.95, this.dollySpeed)
  159. }
  160. dollyIn() {
  161. this.scale *= Math.pow(0.95, this.dollySpeed)
  162. }
  163. normalizeScreenInput(browserVector, gameVector) {
  164. const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect()
  165. gameVector.x = ((browserVector.x - bounds.left) / bounds.width) * 2 - 1
  166. gameVector.y = -((browserVector.y - bounds.top) / bounds.height) * 2 + 1
  167. }
  168. normalizeScreenMovement(browserVector, gameVector) {
  169. const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect()
  170. gameVector.x = (browserVector.x / bounds.width)
  171. gameVector.y = -(browserVector.y / bounds.height)
  172. }
  173. lock() {
  174. if(!this.useCameraLock) {
  175. return
  176. }
  177. RE.Input.mouse.lock()
  178. }
  179. unlock() {
  180. if(!this.useCameraLock) {
  181. return
  182. }
  183. RE.Input.mouse.unlock()
  184. }
  185. }
  186. RE.registerComponent(TargetedOrbitCamera)