Viewport.ViewHelper.js 7.8 KB

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