Viewport.ViewHelper.js 7.6 KB

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