OrbitCamera.re.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // so camera.up is the orbit axis
  38. this.quat = new THREE.Quaternion().setFromUnitVectors( this.object3d.up, new THREE.Vector3( 0, 1, 0 ) )
  39. this.quatInverse = this.quat.clone().invert()
  40. this.initialOffset.copy(this.object3d.position).sub(this.target.position)
  41. this.offset.copy(this.initialOffset)
  42. }
  43. update() {
  44. let normalizedScreenPosition = new THREE.Vector2(0,0)
  45. this.normalizeScreenInput(RE.Input.mouse, normalizedScreenPosition)
  46. if(this.enableRotate && RE.Input.mouse.getButtonDown(this.rotateMouseButton)) {
  47. this.rotateStart.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  48. }
  49. if(this.enableRotate && RE.Input.mouse.getButtonPressed(this.rotateMouseButton)) {
  50. this.rotateEnd.set( normalizedScreenPosition.x, normalizedScreenPosition.y )
  51. this.rotateDelta.subVectors( this.rotateEnd, this.rotateStart ).multiplyScalar( this.rotateSpeed )
  52. this.rotateLeft(this.twoPI * this.rotateDelta.x)
  53. this.rotateUp(this.twoPI * -this.rotateDelta.y)
  54. this.rotateStart.copy( this.rotateEnd )
  55. }
  56. if(this.enableDolly && RE.Input.mouse.wheelY > 0) {
  57. this.dollyOut()
  58. }
  59. if(this.enableDolly && RE.Input.mouse.wheelY < 0) {
  60. this.dollyIn()
  61. }
  62. this.updateCamera()
  63. }
  64. rotateLeft(angle) {
  65. this.sphericalDelta.theta -= angle
  66. }
  67. rotateUp(angle) {
  68. this.sphericalDelta.phi -= angle
  69. }
  70. updateCamera() {
  71. // rotate offset to "y-axis-is-up" space
  72. this.offset.applyQuaternion(this.quat)
  73. // angle from z-axis around y-axis
  74. this.spherical.setFromVector3(this.offset)
  75. this.spherical.theta += this.sphericalDelta.theta
  76. this.spherical.phi += this.sphericalDelta.phi
  77. // restrict theta to be between desired limits
  78. let min = this.minAzimuthAngle
  79. let max = this.maxAzimuthAngle
  80. if ( isFinite(min) && isFinite(max) ) {
  81. if (min < -Math.PI) {
  82. min += this.twoPI
  83. } else if (min > Math.PI) {
  84. min -= this.twoPI
  85. }
  86. if (max < - Math.PI) {
  87. max += this.twoPI
  88. } else if (max > Math.PI) {
  89. max -= this.twoPI
  90. }
  91. if (min <= max) {
  92. this.spherical.theta = Math.max(min, Math.min(max, this.spherical.theta))
  93. } else {
  94. this.spherical.theta = (this.spherical.theta > (min + max) / 2 ) ?
  95. Math.max( min, this.spherical.theta ) :
  96. Math.min( max, this.spherical.theta )
  97. }
  98. }
  99. // restrict phi to be between desired limits
  100. this.spherical.phi = Math.max(this.minPolarAngle, Math.min(this.maxPolarAngle, this.spherical.phi))
  101. this.spherical.makeSafe()
  102. this.spherical.radius *= this.scale
  103. // restrict radius to be between desired limits
  104. this.spherical.radius = Math.max(this.minDistance, Math.min(this.maxDistance, this.spherical.radius))
  105. this.offset.setFromSpherical(this.spherical)
  106. // rotate offset back to "camera-up-vector-is-up" space
  107. this.offset.applyQuaternion(this.quatInverse)
  108. this.object3d.position.copy(this.target.position).add(this.offset)
  109. this.object3d.lookAt(this.target.position)
  110. this.sphericalDelta.set(0, 0, 0)
  111. this.scale = 1
  112. // update condition is:
  113. // min(camera displacement, camera rotation in radians)^2 > EPS
  114. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  115. if (this.lastPosition.distanceToSquared(this.object3d.position) > this.EPS ||
  116. 8 * (1 - this.lastQuaternion.dot(this.object3d.quaternion)) > this.EPS) {
  117. this.lastPosition.copy(this.object3d.position)
  118. this.lastQuaternion.copy(this.object3d.quaternion)
  119. }
  120. }
  121. dollyOut() {
  122. this.scale /= Math.pow(0.95, this.dollySpeed)
  123. }
  124. dollyIn() {
  125. this.scale *= Math.pow(0.95, this.dollySpeed)
  126. }
  127. normalizeScreenInput(browserVector, gameVector) {
  128. const bounds = RE.Runtime.rogueDOMContainer.getBoundingClientRect()
  129. gameVector.x = ((browserVector.x - bounds.left) / bounds.width) * 2 - 1
  130. gameVector.y = -((browserVector.y - bounds.top) / bounds.height) * 2 + 1
  131. }
  132. }
  133. RE.registerComponent(OrbitCamera)