Viewport.ViewHelper.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. */
  4. import * as THREE from '../../build/three.module.js';
  5. function ViewHelper( editorCamera, container ) {
  6. THREE.Object3D.call( this );
  7. this.animating = false;
  8. var color1 = new THREE.Color( '#ff3653' );
  9. var color2 = new THREE.Color( '#8adb00' );
  10. var color3 = new THREE.Color( '#2c8fff' );
  11. var interactiveObjects = [];
  12. var raycaster = new THREE.Raycaster();
  13. var mouse = new THREE.Vector2();
  14. var dummy = new THREE.Object3D();
  15. var camera = new THREE.OrthographicCamera( - 2, 2, 2, - 2, 0, 4 );
  16. camera.position.set( 0, 0, 2 );
  17. var geometry = new THREE.BoxBufferGeometry( 0.8, 0.05, 0.05 ).translate( 0.4, 0, 0 );
  18. var xAxis = new THREE.Mesh( geometry, getAxisMaterial( color1 ) );
  19. var yAxis = new THREE.Mesh( geometry, getAxisMaterial( color2 ) );
  20. var 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. var posXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1, 'X' ) );
  27. posXAxisHelper.userData.type = 'posX';
  28. var posYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2, 'Y' ) );
  29. posYAxisHelper.userData.type = 'posY';
  30. var posZAxisHelper = new THREE.Sprite( getSpriteMaterial( color3, 'Z' ) );
  31. posZAxisHelper.userData.type = 'posZ';
  32. var negXAxisHelper = new THREE.Sprite( getSpriteMaterial( color1 ) );
  33. negXAxisHelper.userData.type = 'negX';
  34. var negYAxisHelper = new THREE.Sprite( getSpriteMaterial( color2 ) );
  35. negYAxisHelper.userData.type = 'negY';
  36. var 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. var point = new THREE.Vector3();
  60. var dim = 128;
  61. var turnRate = 2 * Math.PI; // turn rate in angles per second
  62. this.render = function ( renderer ) {
  63. this.quaternion.copy( editorCamera.quaternion ).inverse();
  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. var x = container.dom.offsetWidth - dim;
  90. renderer.clearDepth();
  91. renderer.setViewport( x, 0, dim, dim );
  92. renderer.render( this, camera );
  93. };
  94. var targetPosition = new THREE.Vector3();
  95. var targetQuaternion = new THREE.Quaternion();
  96. var q1 = new THREE.Quaternion();
  97. var q2 = new THREE.Quaternion();
  98. var radius = 0;
  99. this.handleClick = function ( event, center ) {
  100. if ( this.animating === true ) return false;
  101. var rect = container.dom.getBoundingClientRect();
  102. var offsetX = rect.left + ( container.dom.offsetWidth - dim );
  103. var offsetY = rect.top + ( container.dom.offsetHeight - dim );
  104. mouse.x = ( ( event.clientX - offsetX ) / ( rect.width - offsetX ) ) * 2 - 1;
  105. mouse.y = - ( ( event.clientY - offsetY ) / ( rect.bottom - offsetY ) ) * 2 + 1;
  106. raycaster.setFromCamera( mouse, camera );
  107. var intersects = raycaster.intersectObjects( interactiveObjects );
  108. if ( intersects.length > 0 ) {
  109. var intersection = intersects[ 0 ];
  110. var object = intersection.object;
  111. prepareAnimationData( object, center );
  112. this.animating = true;
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. };
  118. this.update = function ( delta ) {
  119. var step = delta * turnRate;
  120. // animate position by doing a slerp and then scaling the position on the unit sphere
  121. q1.rotateTowards( q2, step );
  122. editorCamera.position.set( 0, 0, 1 ).applyQuaternion( q1 ).multiplyScalar( radius );
  123. // animate orientation
  124. editorCamera.quaternion.rotateTowards( targetQuaternion, step );
  125. if ( q1.angleTo( q2 ) === 0 ) {
  126. this.animating = false;
  127. }
  128. };
  129. function prepareAnimationData( object, focusPoint ) {
  130. switch ( object.userData.type ) {
  131. case 'posX':
  132. targetPosition.set( 1, 0, 0 );
  133. targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI * 0.5, 0 ) );
  134. break;
  135. case 'posY':
  136. targetPosition.set( 0, 1, 0 );
  137. targetQuaternion.setFromEuler( new THREE.Euler( - Math.PI * 0.5, 0, 0 ) );
  138. break;
  139. case 'posZ':
  140. targetPosition.set( 0, 0, 1 );
  141. targetQuaternion.setFromEuler( new THREE.Euler() );
  142. break;
  143. case 'negX':
  144. targetPosition.set( - 1, 0, 0 );
  145. targetQuaternion.setFromEuler( new THREE.Euler( 0, - Math.PI * 0.5, 0 ) );
  146. break;
  147. case 'negY':
  148. targetPosition.set( 0, - 1, 0 );
  149. targetQuaternion.setFromEuler( new THREE.Euler( Math.PI * 0.5, 0, 0 ) );
  150. break;
  151. case 'negZ':
  152. targetPosition.set( 0, 0, - 1 );
  153. targetQuaternion.setFromEuler( new THREE.Euler( 0, Math.PI, 0 ) );
  154. break;
  155. default:
  156. console.error( 'ViewHelper: Invalid axis.' );
  157. }
  158. //
  159. radius = editorCamera.position.distanceTo( focusPoint );
  160. targetPosition.multiplyScalar( radius );
  161. dummy.position.copy( focusPoint );
  162. dummy.lookAt( editorCamera.position );
  163. q1.copy( dummy.quaternion );
  164. dummy.lookAt( targetPosition );
  165. q2.copy( dummy.quaternion );
  166. }
  167. function getAxisMaterial( color ) {
  168. return new THREE.MeshBasicMaterial( { color: color, toneMapped: false } );
  169. }
  170. function getSpriteMaterial( color, text = null ) {
  171. var canvas = document.createElement( 'canvas' );
  172. canvas.width = 64;
  173. canvas.height = 64;
  174. var context = canvas.getContext( '2d' );
  175. context.beginPath();
  176. context.arc( 32, 32, 16, 0, 2 * Math.PI );
  177. context.closePath();
  178. context.fillStyle = color.getStyle();
  179. context.fill();
  180. if ( text !== null ) {
  181. context.font = '24px Arial';
  182. context.textAlign = 'center';
  183. context.fillStyle = '#000000';
  184. context.fillText( text, 32, 41 );
  185. }
  186. var texture = new THREE.CanvasTexture( canvas );
  187. return new THREE.SpriteMaterial( { map: texture, toneMapped: false } );
  188. }
  189. }
  190. ViewHelper.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  191. constructor: ViewHelper,
  192. isViewHelper: true
  193. } );
  194. export { ViewHelper };