ViewHelper.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import * as THREE from 'three';
  2. const vpTemp = new THREE.Vector4();
  3. class ViewHelper extends THREE.Object3D {
  4. constructor( editorCamera, dom ) {
  5. super();
  6. this.animating = false;
  7. this.controls = null;
  8. const color1 = new THREE.Color( '#ff3653' );
  9. const color2 = new THREE.Color( '#8adb00' );
  10. const color3 = new THREE.Color( '#2c8fff' );
  11. const interactiveObjects = [];
  12. const raycaster = new THREE.Raycaster();
  13. const mouse = new THREE.Vector2();
  14. const dummy = new THREE.Object3D();
  15. const camera = new THREE.OrthographicCamera( - 2, 2, 2, - 2, 0, 4 );
  16. camera.position.set( 0, 0, 2 );
  17. const geometry = new THREE.BoxGeometry( 0.8, 0.05, 0.05 ).translate( 0.4, 0, 0 );
  18. const xAxis = new THREE.Mesh( geometry, getAxisMaterial( color1 ) );
  19. const yAxis = new THREE.Mesh( geometry, getAxisMaterial( color2 ) );
  20. const zAxis = new THREE.Mesh( geometry, getAxisMaterial( color3 ) );
  21. yAxis.rotation.z = Math.PI / 2;
  22. zAxis.rotation.y = - Math.PI / 2;
  23. this.add( xAxis );
  24. this.add( zAxis );
  25. this.add( yAxis );
  26. const posXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1, 'X' ) );
  27. posXAxisHelper.userData.type = 'posX';
  28. const posYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2, 'Y' ) );
  29. posYAxisHelper.userData.type = 'posY';
  30. const posZAxisHelper = new THREE.Sprite( getSpriteMaterial( color3, 'Z' ) );
  31. posZAxisHelper.userData.type = 'posZ';
  32. const negXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1 ) );
  33. negXAxisHelper.userData.type = 'negX';
  34. const negYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2 ) );
  35. negYAxisHelper.userData.type = 'negY';
  36. const negZAxisHelper = new THREE.Sprite( getSpriteMaterial( color3 ) );
  37. negZAxisHelper.userData.type = 'negZ';
  38. posXAxisHelper.position.x = 1;
  39. posYAxisHelper.position.y = 1;
  40. posZAxisHelper.position.z = 1;
  41. negXAxisHelper.position.x = - 1;
  42. negXAxisHelper.scale.setScalar( 0.8 );
  43. negYAxisHelper.position.y = - 1;
  44. negYAxisHelper.scale.setScalar( 0.8 );
  45. negZAxisHelper.position.z = - 1;
  46. negZAxisHelper.scale.setScalar( 0.8 );
  47. this.add( posXAxisHelper );
  48. this.add( posYAxisHelper );
  49. this.add( posZAxisHelper );
  50. this.add( negXAxisHelper );
  51. this.add( negYAxisHelper );
  52. this.add( negZAxisHelper );
  53. interactiveObjects.push( posXAxisHelper );
  54. interactiveObjects.push( posYAxisHelper );
  55. interactiveObjects.push( posZAxisHelper );
  56. interactiveObjects.push( negXAxisHelper );
  57. interactiveObjects.push( negYAxisHelper );
  58. interactiveObjects.push( negZAxisHelper );
  59. const point = new THREE.Vector3();
  60. const dim = 128;
  61. const turnRate = 2 * Math.PI; // turn rate in angles per second
  62. this.render = function ( renderer ) {
  63. this.quaternion.copy( editorCamera.quaternion ).invert();
  64. this.updateMatrixWorld();
  65. point.set( 0, 0, 1 );
  66. point.applyQuaternion( editorCamera.quaternion );
  67. if ( point.x >= 0 ) {
  68. posXAxisHelper.material.opacity = 1;
  69. negXAxisHelper.material.opacity = 0.5;
  70. } else {
  71. posXAxisHelper.material.opacity = 0.5;
  72. negXAxisHelper.material.opacity = 1;
  73. }
  74. if ( point.y >= 0 ) {
  75. posYAxisHelper.material.opacity = 1;
  76. negYAxisHelper.material.opacity = 0.5;
  77. } else {
  78. posYAxisHelper.material.opacity = 0.5;
  79. negYAxisHelper.material.opacity = 1;
  80. }
  81. if ( point.z >= 0 ) {
  82. posZAxisHelper.material.opacity = 1;
  83. negZAxisHelper.material.opacity = 0.5;
  84. } else {
  85. posZAxisHelper.material.opacity = 0.5;
  86. negZAxisHelper.material.opacity = 1;
  87. }
  88. //
  89. const x = dom.offsetWidth - dim;
  90. renderer.clearDepth();
  91. renderer.getViewport( vpTemp );
  92. renderer.setViewport( x, 0, dim, dim );
  93. renderer.render( this, camera );
  94. renderer.setViewport( vpTemp.x, vpTemp.y, vpTemp.z, vpTemp.w );
  95. };
  96. const targetPosition = new THREE.Vector3();
  97. const targetQuaternion = new THREE.Quaternion();
  98. const q1 = new THREE.Quaternion();
  99. const q2 = new THREE.Quaternion();
  100. let radius = 0;
  101. this.handleClick = function ( event ) {
  102. if ( this.animating === true ) return false;
  103. const rect = dom.getBoundingClientRect();
  104. const offsetX = rect.left + ( dom.offsetWidth - dim );
  105. const offsetY = rect.top + ( dom.offsetHeight - dim );
  106. mouse.x = ( ( event.clientX - offsetX ) / ( rect.width - offsetX ) ) * 2 - 1;
  107. mouse.y = - ( ( event.clientY - offsetY ) / ( rect.bottom - offsetY ) ) * 2 + 1;
  108. raycaster.setFromCamera( mouse, camera );
  109. const intersects = raycaster.intersectObjects( interactiveObjects );
  110. if ( intersects.length > 0 ) {
  111. const intersection = intersects[ 0 ];
  112. const object = intersection.object;
  113. prepareAnimationData( object, this.controls.center );
  114. this.animating = true;
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. };
  120. this.update = function ( delta ) {
  121. const step = delta * turnRate;
  122. const focusPoint = this.controls.center;
  123. // animate position by doing a slerp and then scaling the position on the unit sphere
  124. q1.rotateTowards( q2, step );
  125. editorCamera.position.set( 0, 0, 1 ).applyQuaternion( q1 ).multiplyScalar( radius ).add( focusPoint );
  126. // animate orientation
  127. editorCamera.quaternion.rotateTowards( targetQuaternion, step );
  128. if ( q1.angleTo( q2 ) === 0 ) {
  129. this.animating = false;
  130. }
  131. };
  132. function prepareAnimationData( object, focusPoint ) {
  133. switch ( object.userData.type ) {
  134. case 'posX':
  135. targetPosition.set( 1, 0, 0 );
  136. targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI * 0.5, 0 ) );
  137. break;
  138. case 'posY':
  139. targetPosition.set( 0, 1, 0 );
  140. targetQuaternion.setFromEuler( new THREE.Euler( - Math.PI * 0.5, 0, 0 ) );
  141. break;
  142. case 'posZ':
  143. targetPosition.set( 0, 0, 1 );
  144. targetQuaternion.setFromEuler( new THREE.Euler() );
  145. break;
  146. case 'negX':
  147. targetPosition.set( - 1, 0, 0 );
  148. targetQuaternion.setFromEuler( new THREE.Euler( 0, - Math.PI * 0.5, 0 ) );
  149. break;
  150. case 'negY':
  151. targetPosition.set( 0, - 1, 0 );
  152. targetQuaternion.setFromEuler( new THREE.Euler( Math.PI * 0.5, 0, 0 ) );
  153. break;
  154. case 'negZ':
  155. targetPosition.set( 0, 0, - 1 );
  156. targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI, 0 ) );
  157. break;
  158. default:
  159. console.error( 'ViewHelper: Invalid axis.' );
  160. }
  161. //
  162. radius = editorCamera.position.distanceTo( focusPoint );
  163. targetPosition.multiplyScalar( radius ).add( focusPoint );
  164. dummy.position.copy( focusPoint );
  165. dummy.lookAt( editorCamera.position );
  166. q1.copy( dummy.quaternion );
  167. dummy.lookAt( targetPosition );
  168. q2.copy( dummy.quaternion );
  169. }
  170. function getAxisMaterial( color ) {
  171. return new THREE.MeshBasicMaterial( { color: color, toneMapped: false } );
  172. }
  173. function getSpriteMaterial( color, text = null ) {
  174. const canvas = document.createElement( 'canvas' );
  175. canvas.width = 64;
  176. canvas.height = 64;
  177. const context = canvas.getContext( '2d' );
  178. context.beginPath();
  179. context.arc( 32, 32, 16, 0, 2 * Math.PI );
  180. context.closePath();
  181. context.fillStyle = color.getStyle();
  182. context.fill();
  183. if ( text !== null ) {
  184. context.font = '24px Arial';
  185. context.textAlign = 'center';
  186. context.fillStyle = '#000000';
  187. context.fillText( text, 32, 41 );
  188. }
  189. const texture = new THREE.CanvasTexture( canvas );
  190. return new THREE.SpriteMaterial( { map: texture, toneMapped: false } );
  191. }
  192. }
  193. }
  194. ViewHelper.prototype.isViewHelper = true;
  195. export { ViewHelper };