TransformControls.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. ( function () {
  2. var TransformControls = function ( camera, domElement ) {
  3. if ( domElement === undefined ) {
  4. console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' );
  5. domElement = document;
  6. }
  7. THREE.Object3D.call( this );
  8. this.visible = false;
  9. this.domElement = domElement;
  10. var _gizmo = new TransformControlsGizmo();
  11. this.add( _gizmo );
  12. var _plane = new TransformControlsPlane();
  13. this.add( _plane );
  14. var scope = this; // Define properties with getters/setter
  15. // Setting the defined property will automatically trigger change event
  16. // Defined properties are passed down to gizmo and plane
  17. defineProperty( 'camera', camera );
  18. defineProperty( 'object', undefined );
  19. defineProperty( 'enabled', true );
  20. defineProperty( 'axis', null );
  21. defineProperty( 'mode', 'translate' );
  22. defineProperty( 'translationSnap', null );
  23. defineProperty( 'rotationSnap', null );
  24. defineProperty( 'scaleSnap', null );
  25. defineProperty( 'space', 'world' );
  26. defineProperty( 'size', 1 );
  27. defineProperty( 'dragging', false );
  28. defineProperty( 'showX', true );
  29. defineProperty( 'showY', true );
  30. defineProperty( 'showZ', true );
  31. var changeEvent = {
  32. type: 'change'
  33. };
  34. var mouseDownEvent = {
  35. type: 'mouseDown'
  36. };
  37. var mouseUpEvent = {
  38. type: 'mouseUp',
  39. mode: scope.mode
  40. };
  41. var objectChangeEvent = {
  42. type: 'objectChange'
  43. }; // Reusable utility variables
  44. var raycaster = new THREE.Raycaster();
  45. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  46. var allIntersections = raycaster.intersectObject( object, true );
  47. for ( var i = 0; i < allIntersections.length; i ++ ) {
  48. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  49. return allIntersections[ i ];
  50. }
  51. }
  52. return false;
  53. }
  54. var _tempVector = new THREE.Vector3();
  55. var _tempVector2 = new THREE.Vector3();
  56. var _tempQuaternion = new THREE.Quaternion();
  57. var _unit = {
  58. X: new THREE.Vector3( 1, 0, 0 ),
  59. Y: new THREE.Vector3( 0, 1, 0 ),
  60. Z: new THREE.Vector3( 0, 0, 1 )
  61. };
  62. var pointStart = new THREE.Vector3();
  63. var pointEnd = new THREE.Vector3();
  64. var offset = new THREE.Vector3();
  65. var rotationAxis = new THREE.Vector3();
  66. var startNorm = new THREE.Vector3();
  67. var endNorm = new THREE.Vector3();
  68. var rotationAngle = 0;
  69. var cameraPosition = new THREE.Vector3();
  70. var cameraQuaternion = new THREE.Quaternion();
  71. var cameraScale = new THREE.Vector3();
  72. var parentPosition = new THREE.Vector3();
  73. var parentQuaternion = new THREE.Quaternion();
  74. var parentQuaternionInv = new THREE.Quaternion();
  75. var parentScale = new THREE.Vector3();
  76. var worldPositionStart = new THREE.Vector3();
  77. var worldQuaternionStart = new THREE.Quaternion();
  78. var worldScaleStart = new THREE.Vector3();
  79. var worldPosition = new THREE.Vector3();
  80. var worldQuaternion = new THREE.Quaternion();
  81. var worldQuaternionInv = new THREE.Quaternion();
  82. var worldScale = new THREE.Vector3();
  83. var eye = new THREE.Vector3();
  84. var positionStart = new THREE.Vector3();
  85. var quaternionStart = new THREE.Quaternion();
  86. var scaleStart = new THREE.Vector3(); // TODO: remove properties unused in plane and gizmo
  87. defineProperty( 'worldPosition', worldPosition );
  88. defineProperty( 'worldPositionStart', worldPositionStart );
  89. defineProperty( 'worldQuaternion', worldQuaternion );
  90. defineProperty( 'worldQuaternionStart', worldQuaternionStart );
  91. defineProperty( 'cameraPosition', cameraPosition );
  92. defineProperty( 'cameraQuaternion', cameraQuaternion );
  93. defineProperty( 'pointStart', pointStart );
  94. defineProperty( 'pointEnd', pointEnd );
  95. defineProperty( 'rotationAxis', rotationAxis );
  96. defineProperty( 'rotationAngle', rotationAngle );
  97. defineProperty( 'eye', eye );
  98. {
  99. domElement.addEventListener( 'pointerdown', onPointerDown );
  100. domElement.addEventListener( 'pointermove', onPointerHover );
  101. scope.domElement.ownerDocument.addEventListener( 'pointerup', onPointerUp );
  102. }
  103. this.dispose = function () {
  104. domElement.removeEventListener( 'pointerdown', onPointerDown );
  105. domElement.removeEventListener( 'pointermove', onPointerHover );
  106. scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  107. scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
  108. this.traverse( function ( child ) {
  109. if ( child.geometry ) child.geometry.dispose();
  110. if ( child.material ) child.material.dispose();
  111. } );
  112. }; // Set current object
  113. this.attach = function ( object ) {
  114. this.object = object;
  115. this.visible = true;
  116. return this;
  117. }; // Detatch from object
  118. this.detach = function () {
  119. this.object = undefined;
  120. this.visible = false;
  121. this.axis = null;
  122. return this;
  123. }; // Defined getter, setter and store for a property
  124. function defineProperty( propName, defaultValue ) {
  125. var propValue = defaultValue;
  126. Object.defineProperty( scope, propName, {
  127. get: function () {
  128. return propValue !== undefined ? propValue : defaultValue;
  129. },
  130. set: function ( value ) {
  131. if ( propValue !== value ) {
  132. propValue = value;
  133. _plane[ propName ] = value;
  134. _gizmo[ propName ] = value;
  135. scope.dispatchEvent( {
  136. type: propName + '-changed',
  137. value: value
  138. } );
  139. scope.dispatchEvent( changeEvent );
  140. }
  141. }
  142. } );
  143. scope[ propName ] = defaultValue;
  144. _plane[ propName ] = defaultValue;
  145. _gizmo[ propName ] = defaultValue;
  146. } // updateMatrixWorld updates key transformation variables
  147. this.updateMatrixWorld = function () {
  148. if ( this.object !== undefined ) {
  149. this.object.updateMatrixWorld();
  150. if ( this.object.parent === null ) {
  151. console.error( 'TransformControls: The attached 3D object must be a part of the scene graph.' );
  152. } else {
  153. this.object.parent.matrixWorld.decompose( parentPosition, parentQuaternion, parentScale );
  154. }
  155. this.object.matrixWorld.decompose( worldPosition, worldQuaternion, worldScale );
  156. parentQuaternionInv.copy( parentQuaternion ).invert();
  157. worldQuaternionInv.copy( worldQuaternion ).invert();
  158. }
  159. this.camera.updateMatrixWorld();
  160. this.camera.matrixWorld.decompose( cameraPosition, cameraQuaternion, cameraScale );
  161. eye.copy( cameraPosition ).sub( worldPosition ).normalize();
  162. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  163. };
  164. this.pointerHover = function ( pointer ) {
  165. if ( this.object === undefined || this.dragging === true ) return;
  166. raycaster.setFromCamera( pointer, this.camera );
  167. var intersect = intersectObjectWithRay( _gizmo.picker[ this.mode ], raycaster );
  168. if ( intersect ) {
  169. this.axis = intersect.object.name;
  170. } else {
  171. this.axis = null;
  172. }
  173. };
  174. this.pointerDown = function ( pointer ) {
  175. if ( this.object === undefined || this.dragging === true || pointer.button !== 0 ) return;
  176. if ( this.axis !== null ) {
  177. raycaster.setFromCamera( pointer, this.camera );
  178. var planeIntersect = intersectObjectWithRay( _plane, raycaster, true );
  179. if ( planeIntersect ) {
  180. var space = this.space;
  181. if ( this.mode === 'scale' ) {
  182. space = 'local';
  183. } else if ( this.axis === 'E' || this.axis === 'XYZE' || this.axis === 'XYZ' ) {
  184. space = 'world';
  185. }
  186. if ( space === 'local' && this.mode === 'rotate' ) {
  187. var snap = this.rotationSnap;
  188. if ( this.axis === 'X' && snap ) this.object.rotation.x = Math.round( this.object.rotation.x / snap ) * snap;
  189. if ( this.axis === 'Y' && snap ) this.object.rotation.y = Math.round( this.object.rotation.y / snap ) * snap;
  190. if ( this.axis === 'Z' && snap ) this.object.rotation.z = Math.round( this.object.rotation.z / snap ) * snap;
  191. }
  192. this.object.updateMatrixWorld();
  193. this.object.parent.updateMatrixWorld();
  194. positionStart.copy( this.object.position );
  195. quaternionStart.copy( this.object.quaternion );
  196. scaleStart.copy( this.object.scale );
  197. this.object.matrixWorld.decompose( worldPositionStart, worldQuaternionStart, worldScaleStart );
  198. pointStart.copy( planeIntersect.point ).sub( worldPositionStart );
  199. }
  200. this.dragging = true;
  201. mouseDownEvent.mode = this.mode;
  202. this.dispatchEvent( mouseDownEvent );
  203. }
  204. };
  205. this.pointerMove = function ( pointer ) {
  206. var axis = this.axis;
  207. var mode = this.mode;
  208. var object = this.object;
  209. var space = this.space;
  210. if ( mode === 'scale' ) {
  211. space = 'local';
  212. } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
  213. space = 'world';
  214. }
  215. if ( object === undefined || axis === null || this.dragging === false || pointer.button !== - 1 ) return;
  216. raycaster.setFromCamera( pointer, this.camera );
  217. var planeIntersect = intersectObjectWithRay( _plane, raycaster, true );
  218. if ( ! planeIntersect ) return;
  219. pointEnd.copy( planeIntersect.point ).sub( worldPositionStart );
  220. if ( mode === 'translate' ) {
  221. // Apply translate
  222. offset.copy( pointEnd ).sub( pointStart );
  223. if ( space === 'local' && axis !== 'XYZ' ) {
  224. offset.applyQuaternion( worldQuaternionInv );
  225. }
  226. if ( axis.indexOf( 'X' ) === - 1 ) offset.x = 0;
  227. if ( axis.indexOf( 'Y' ) === - 1 ) offset.y = 0;
  228. if ( axis.indexOf( 'Z' ) === - 1 ) offset.z = 0;
  229. if ( space === 'local' && axis !== 'XYZ' ) {
  230. offset.applyQuaternion( quaternionStart ).divide( parentScale );
  231. } else {
  232. offset.applyQuaternion( parentQuaternionInv ).divide( parentScale );
  233. }
  234. object.position.copy( offset ).add( positionStart ); // Apply translation snap
  235. if ( this.translationSnap ) {
  236. if ( space === 'local' ) {
  237. object.position.applyQuaternion( _tempQuaternion.copy( quaternionStart ).invert() );
  238. if ( axis.search( 'X' ) !== - 1 ) {
  239. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  240. }
  241. if ( axis.search( 'Y' ) !== - 1 ) {
  242. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  243. }
  244. if ( axis.search( 'Z' ) !== - 1 ) {
  245. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  246. }
  247. object.position.applyQuaternion( quaternionStart );
  248. }
  249. if ( space === 'world' ) {
  250. if ( object.parent ) {
  251. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  252. }
  253. if ( axis.search( 'X' ) !== - 1 ) {
  254. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  255. }
  256. if ( axis.search( 'Y' ) !== - 1 ) {
  257. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  258. }
  259. if ( axis.search( 'Z' ) !== - 1 ) {
  260. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  261. }
  262. if ( object.parent ) {
  263. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  264. }
  265. }
  266. }
  267. } else if ( mode === 'scale' ) {
  268. if ( axis.search( 'XYZ' ) !== - 1 ) {
  269. var d = pointEnd.length() / pointStart.length();
  270. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  271. _tempVector2.set( d, d, d );
  272. } else {
  273. _tempVector.copy( pointStart );
  274. _tempVector2.copy( pointEnd );
  275. _tempVector.applyQuaternion( worldQuaternionInv );
  276. _tempVector2.applyQuaternion( worldQuaternionInv );
  277. _tempVector2.divide( _tempVector );
  278. if ( axis.search( 'X' ) === - 1 ) {
  279. _tempVector2.x = 1;
  280. }
  281. if ( axis.search( 'Y' ) === - 1 ) {
  282. _tempVector2.y = 1;
  283. }
  284. if ( axis.search( 'Z' ) === - 1 ) {
  285. _tempVector2.z = 1;
  286. }
  287. } // Apply scale
  288. object.scale.copy( scaleStart ).multiply( _tempVector2 );
  289. if ( this.scaleSnap ) {
  290. if ( axis.search( 'X' ) !== - 1 ) {
  291. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  292. }
  293. if ( axis.search( 'Y' ) !== - 1 ) {
  294. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  295. }
  296. if ( axis.search( 'Z' ) !== - 1 ) {
  297. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  298. }
  299. }
  300. } else if ( mode === 'rotate' ) {
  301. offset.copy( pointEnd ).sub( pointStart );
  302. var ROTATION_SPEED = 20 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  303. if ( axis === 'E' ) {
  304. rotationAxis.copy( eye );
  305. rotationAngle = pointEnd.angleTo( pointStart );
  306. startNorm.copy( pointStart ).normalize();
  307. endNorm.copy( pointEnd ).normalize();
  308. rotationAngle *= endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1;
  309. } else if ( axis === 'XYZE' ) {
  310. rotationAxis.copy( offset ).cross( eye ).normalize();
  311. rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  312. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  313. rotationAxis.copy( _unit[ axis ] );
  314. _tempVector.copy( _unit[ axis ] );
  315. if ( space === 'local' ) {
  316. _tempVector.applyQuaternion( worldQuaternion );
  317. }
  318. rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
  319. } // Apply rotation snap
  320. if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap;
  321. this.rotationAngle = rotationAngle; // Apply rotate
  322. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  323. object.quaternion.copy( quaternionStart );
  324. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize();
  325. } else {
  326. rotationAxis.applyQuaternion( parentQuaternionInv );
  327. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) );
  328. object.quaternion.multiply( quaternionStart ).normalize();
  329. }
  330. }
  331. this.dispatchEvent( changeEvent );
  332. this.dispatchEvent( objectChangeEvent );
  333. };
  334. this.pointerUp = function ( pointer ) {
  335. if ( pointer.button !== 0 ) return;
  336. if ( this.dragging && this.axis !== null ) {
  337. mouseUpEvent.mode = this.mode;
  338. this.dispatchEvent( mouseUpEvent );
  339. }
  340. this.dragging = false;
  341. this.axis = null;
  342. }; // normalize mouse / touch pointer and remap {x,y} to view space.
  343. function getPointer( event ) {
  344. if ( scope.domElement.ownerDocument.pointerLockElement ) {
  345. return {
  346. x: 0,
  347. y: 0,
  348. button: event.button
  349. };
  350. } else {
  351. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  352. var rect = domElement.getBoundingClientRect();
  353. return {
  354. x: ( pointer.clientX - rect.left ) / rect.width * 2 - 1,
  355. y: - ( pointer.clientY - rect.top ) / rect.height * 2 + 1,
  356. button: event.button
  357. };
  358. }
  359. } // mouse / touch event handlers
  360. function onPointerHover( event ) {
  361. if ( ! scope.enabled ) return;
  362. switch ( event.pointerType ) {
  363. case 'mouse':
  364. case 'pen':
  365. scope.pointerHover( getPointer( event ) );
  366. break;
  367. }
  368. }
  369. function onPointerDown( event ) {
  370. if ( ! scope.enabled ) return;
  371. scope.domElement.style.touchAction = 'none'; // disable touch scroll
  372. scope.domElement.ownerDocument.addEventListener( 'pointermove', onPointerMove );
  373. scope.pointerHover( getPointer( event ) );
  374. scope.pointerDown( getPointer( event ) );
  375. }
  376. function onPointerMove( event ) {
  377. if ( ! scope.enabled ) return;
  378. scope.pointerMove( getPointer( event ) );
  379. }
  380. function onPointerUp( event ) {
  381. if ( ! scope.enabled ) return;
  382. scope.domElement.style.touchAction = '';
  383. scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  384. scope.pointerUp( getPointer( event ) );
  385. } // TODO: deprecate
  386. this.getMode = function () {
  387. return scope.mode;
  388. };
  389. this.setMode = function ( mode ) {
  390. scope.mode = mode;
  391. };
  392. this.setTranslationSnap = function ( translationSnap ) {
  393. scope.translationSnap = translationSnap;
  394. };
  395. this.setRotationSnap = function ( rotationSnap ) {
  396. scope.rotationSnap = rotationSnap;
  397. };
  398. this.setScaleSnap = function ( scaleSnap ) {
  399. scope.scaleSnap = scaleSnap;
  400. };
  401. this.setSize = function ( size ) {
  402. scope.size = size;
  403. };
  404. this.setSpace = function ( space ) {
  405. scope.space = space;
  406. };
  407. this.update = function () {
  408. console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' );
  409. };
  410. };
  411. TransformControls.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  412. constructor: TransformControls,
  413. isTransformControls: true
  414. } );
  415. var TransformControlsGizmo = function () {
  416. 'use strict';
  417. THREE.Object3D.call( this );
  418. this.type = 'TransformControlsGizmo'; // shared materials
  419. var gizmoMaterial = new THREE.MeshBasicMaterial( {
  420. depthTest: false,
  421. depthWrite: false,
  422. transparent: true,
  423. side: THREE.DoubleSide,
  424. fog: false,
  425. toneMapped: false
  426. } );
  427. var gizmoLineMaterial = new THREE.LineBasicMaterial( {
  428. depthTest: false,
  429. depthWrite: false,
  430. transparent: true,
  431. linewidth: 1,
  432. fog: false,
  433. toneMapped: false
  434. } ); // Make unique material for each axis/color
  435. var matInvisible = gizmoMaterial.clone();
  436. matInvisible.opacity = 0.15;
  437. var matHelper = gizmoMaterial.clone();
  438. matHelper.opacity = 0.33;
  439. var matRed = gizmoMaterial.clone();
  440. matRed.color.set( 0xff0000 );
  441. var matGreen = gizmoMaterial.clone();
  442. matGreen.color.set( 0x00ff00 );
  443. var matBlue = gizmoMaterial.clone();
  444. matBlue.color.set( 0x0000ff );
  445. var matWhiteTransparent = gizmoMaterial.clone();
  446. matWhiteTransparent.opacity = 0.25;
  447. var matYellowTransparent = matWhiteTransparent.clone();
  448. matYellowTransparent.color.set( 0xffff00 );
  449. var matCyanTransparent = matWhiteTransparent.clone();
  450. matCyanTransparent.color.set( 0x00ffff );
  451. var matMagentaTransparent = matWhiteTransparent.clone();
  452. matMagentaTransparent.color.set( 0xff00ff );
  453. var matYellow = gizmoMaterial.clone();
  454. matYellow.color.set( 0xffff00 );
  455. var matLineRed = gizmoLineMaterial.clone();
  456. matLineRed.color.set( 0xff0000 );
  457. var matLineGreen = gizmoLineMaterial.clone();
  458. matLineGreen.color.set( 0x00ff00 );
  459. var matLineBlue = gizmoLineMaterial.clone();
  460. matLineBlue.color.set( 0x0000ff );
  461. var matLineCyan = gizmoLineMaterial.clone();
  462. matLineCyan.color.set( 0x00ffff );
  463. var matLineMagenta = gizmoLineMaterial.clone();
  464. matLineMagenta.color.set( 0xff00ff );
  465. var matLineYellow = gizmoLineMaterial.clone();
  466. matLineYellow.color.set( 0xffff00 );
  467. var matLineGray = gizmoLineMaterial.clone();
  468. matLineGray.color.set( 0x787878 );
  469. var matLineYellowTransparent = matLineYellow.clone();
  470. matLineYellowTransparent.opacity = 0.25; // reusable geometry
  471. var arrowGeometry = new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false );
  472. var scaleHandleGeometry = new THREE.BoxGeometry( 0.125, 0.125, 0.125 );
  473. var lineGeometry = new THREE.BufferGeometry();
  474. lineGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  475. var CircleGeometry = function ( radius, arc ) {
  476. var geometry = new THREE.BufferGeometry();
  477. var vertices = [];
  478. for ( var i = 0; i <= 64 * arc; ++ i ) {
  479. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  480. }
  481. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  482. return geometry;
  483. }; // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  484. var TranslateHelperGeometry = function () {
  485. var geometry = new THREE.BufferGeometry();
  486. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  487. return geometry;
  488. }; // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  489. var gizmoTranslate = {
  490. X: [[ new THREE.Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ], [ new THREE.Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ], [ new THREE.Line( lineGeometry, matLineRed ) ]],
  491. Y: [[ new THREE.Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ], [ new THREE.Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ], [ new THREE.Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]]],
  492. Z: [[ new THREE.Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ], [ new THREE.Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ], [ new THREE.Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]]],
  493. XYZ: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]]],
  494. XY: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]], [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]],
  495. YZ: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]],
  496. XZ: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]]
  497. };
  498. var pickerTranslate = {
  499. X: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]],
  500. Y: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]]],
  501. Z: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]]],
  502. XYZ: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), matInvisible ) ]],
  503. XY: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]]],
  504. YZ: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]]],
  505. XZ: [[ new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]]]
  506. };
  507. var helperTranslate = {
  508. START: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]],
  509. END: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]],
  510. DELTA: [[ new THREE.Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]],
  511. X: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]],
  512. Y: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]],
  513. Z: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]]
  514. };
  515. var gizmoRotate = {
  516. X: [[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineRed ) ], [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]]],
  517. Y: [[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]], [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]]],
  518. Z: [[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]], [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]]],
  519. E: [[ new THREE.Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]], [ new THREE.Mesh( new THREE.CylinderGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 1.17, 0, 0 ], [ 0, 0, - Math.PI / 2 ], [ 1, 1, 0.001 ]], [ new THREE.Mesh( new THREE.CylinderGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ - 1.17, 0, 0 ], [ 0, 0, Math.PI / 2 ], [ 1, 1, 0.001 ]], [ new THREE.Mesh( new THREE.CylinderGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, - 1.17, 0 ], [ Math.PI, 0, 0 ], [ 1, 1, 0.001 ]], [ new THREE.Mesh( new THREE.CylinderGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]]],
  520. XYZE: [[ new THREE.Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]]]
  521. };
  522. var helperRotate = {
  523. AXIS: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]]
  524. };
  525. var pickerRotate = {
  526. X: [[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]]],
  527. Y: [[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]]],
  528. Z: [[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]],
  529. E: [[ new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]],
  530. XYZE: [[ new THREE.Mesh( new THREE.SphereGeometry( 0.7, 10, 8 ), matInvisible ) ]]
  531. };
  532. var gizmoScale = {
  533. X: [[ new THREE.Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]], [ new THREE.Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]]],
  534. Y: [[ new THREE.Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]], [ new THREE.Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]]],
  535. Z: [[ new THREE.Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]], [ new THREE.Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]]],
  536. XY: [[ new THREE.Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]], [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]],
  537. YZ: [[ new THREE.Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]],
  538. XZ: [[ new THREE.Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]],
  539. XYZX: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]]],
  540. XYZY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]]],
  541. XYZZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]]]
  542. };
  543. var pickerScale = {
  544. X: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]],
  545. Y: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]]],
  546. Z: [[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]]],
  547. XY: [[ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]]],
  548. YZ: [[ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]]],
  549. XZ: [[ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]]],
  550. XYZX: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]]],
  551. XYZY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]]],
  552. XYZZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]]]
  553. };
  554. var helperScale = {
  555. X: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]],
  556. Y: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]],
  557. Z: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]]
  558. }; // Creates an THREE.Object3D with gizmos described in custom hierarchy definition.
  559. var setupGizmo = function ( gizmoMap ) {
  560. var gizmo = new THREE.Object3D();
  561. for ( var name in gizmoMap ) {
  562. for ( var i = gizmoMap[ name ].length; i --; ) {
  563. var object = gizmoMap[ name ][ i ][ 0 ].clone();
  564. var position = gizmoMap[ name ][ i ][ 1 ];
  565. var rotation = gizmoMap[ name ][ i ][ 2 ];
  566. var scale = gizmoMap[ name ][ i ][ 3 ];
  567. var tag = gizmoMap[ name ][ i ][ 4 ]; // name and tag properties are essential for picking and updating logic.
  568. object.name = name;
  569. object.tag = tag;
  570. if ( position ) {
  571. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  572. }
  573. if ( rotation ) {
  574. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  575. }
  576. if ( scale ) {
  577. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  578. }
  579. object.updateMatrix();
  580. var tempGeometry = object.geometry.clone();
  581. tempGeometry.applyMatrix4( object.matrix );
  582. object.geometry = tempGeometry;
  583. object.renderOrder = Infinity;
  584. object.position.set( 0, 0, 0 );
  585. object.rotation.set( 0, 0, 0 );
  586. object.scale.set( 1, 1, 1 );
  587. gizmo.add( object );
  588. }
  589. }
  590. return gizmo;
  591. }; // Reusable utility variables
  592. var tempVector = new THREE.Vector3( 0, 0, 0 );
  593. var tempEuler = new THREE.Euler();
  594. var alignVector = new THREE.Vector3( 0, 1, 0 );
  595. var zeroVector = new THREE.Vector3( 0, 0, 0 );
  596. var lookAtMatrix = new THREE.Matrix4();
  597. var tempQuaternion = new THREE.Quaternion();
  598. var tempQuaternion2 = new THREE.Quaternion();
  599. var identityQuaternion = new THREE.Quaternion();
  600. var unitX = new THREE.Vector3( 1, 0, 0 );
  601. var unitY = new THREE.Vector3( 0, 1, 0 );
  602. var unitZ = new THREE.Vector3( 0, 0, 1 ); // Gizmo creation
  603. this.gizmo = {};
  604. this.picker = {};
  605. this.helper = {};
  606. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  607. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  608. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  609. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  610. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  611. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  612. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  613. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  614. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) ); // Pickers should be hidden always
  615. this.picker[ 'translate' ].visible = false;
  616. this.picker[ 'rotate' ].visible = false;
  617. this.picker[ 'scale' ].visible = false; // updateMatrixWorld will update transformations and appearance of individual handles
  618. this.updateMatrixWorld = function () {
  619. var space = this.space;
  620. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  621. var quaternion = space === 'local' ? this.worldQuaternion : identityQuaternion; // Show only gizmos for current transform mode
  622. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  623. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  624. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  625. this.helper[ 'translate' ].visible = this.mode === 'translate';
  626. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  627. this.helper[ 'scale' ].visible = this.mode === 'scale';
  628. var handles = [];
  629. handles = handles.concat( this.picker[ this.mode ].children );
  630. handles = handles.concat( this.gizmo[ this.mode ].children );
  631. handles = handles.concat( this.helper[ this.mode ].children );
  632. for ( var i = 0; i < handles.length; i ++ ) {
  633. var handle = handles[ i ]; // hide aligned to camera
  634. handle.visible = true;
  635. handle.rotation.set( 0, 0, 0 );
  636. handle.position.copy( this.worldPosition );
  637. var factor;
  638. if ( this.camera.isOrthographicCamera ) {
  639. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  640. } else {
  641. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  642. }
  643. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 7 ); // TODO: simplify helpers and consider decoupling from gizmo
  644. if ( handle.tag === 'helper' ) {
  645. handle.visible = false;
  646. if ( handle.name === 'AXIS' ) {
  647. handle.position.copy( this.worldPositionStart );
  648. handle.visible = !! this.axis;
  649. if ( this.axis === 'X' ) {
  650. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  651. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  652. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  653. handle.visible = false;
  654. }
  655. }
  656. if ( this.axis === 'Y' ) {
  657. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  658. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  659. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  660. handle.visible = false;
  661. }
  662. }
  663. if ( this.axis === 'Z' ) {
  664. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  665. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  666. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  667. handle.visible = false;
  668. }
  669. }
  670. if ( this.axis === 'XYZE' ) {
  671. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  672. alignVector.copy( this.rotationAxis );
  673. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  674. handle.quaternion.multiply( tempQuaternion );
  675. handle.visible = this.dragging;
  676. }
  677. if ( this.axis === 'E' ) {
  678. handle.visible = false;
  679. }
  680. } else if ( handle.name === 'START' ) {
  681. handle.position.copy( this.worldPositionStart );
  682. handle.visible = this.dragging;
  683. } else if ( handle.name === 'END' ) {
  684. handle.position.copy( this.worldPosition );
  685. handle.visible = this.dragging;
  686. } else if ( handle.name === 'DELTA' ) {
  687. handle.position.copy( this.worldPositionStart );
  688. handle.quaternion.copy( this.worldQuaternionStart );
  689. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  690. tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  691. handle.scale.copy( tempVector );
  692. handle.visible = this.dragging;
  693. } else {
  694. handle.quaternion.copy( quaternion );
  695. if ( this.dragging ) {
  696. handle.position.copy( this.worldPositionStart );
  697. } else {
  698. handle.position.copy( this.worldPosition );
  699. }
  700. if ( this.axis ) {
  701. handle.visible = this.axis.search( handle.name ) !== - 1;
  702. }
  703. } // If updating helper, skip rest of the loop
  704. continue;
  705. } // Align handles to current local or world rotation
  706. handle.quaternion.copy( quaternion );
  707. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  708. // Hide translate and scale axis facing the camera
  709. var AXIS_HIDE_TRESHOLD = 0.99;
  710. var PLANE_HIDE_TRESHOLD = 0.2;
  711. var AXIS_FLIP_TRESHOLD = 0.0;
  712. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  713. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  714. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  715. handle.visible = false;
  716. }
  717. }
  718. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  719. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  720. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  721. handle.visible = false;
  722. }
  723. }
  724. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  725. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  726. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  727. handle.visible = false;
  728. }
  729. }
  730. if ( handle.name === 'XY' ) {
  731. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  732. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  733. handle.visible = false;
  734. }
  735. }
  736. if ( handle.name === 'YZ' ) {
  737. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  738. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  739. handle.visible = false;
  740. }
  741. }
  742. if ( handle.name === 'XZ' ) {
  743. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  744. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  745. handle.visible = false;
  746. }
  747. } // Flip translate and scale axis ocluded behind another axis
  748. if ( handle.name.search( 'X' ) !== - 1 ) {
  749. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  750. if ( handle.tag === 'fwd' ) {
  751. handle.visible = false;
  752. } else {
  753. handle.scale.x *= - 1;
  754. }
  755. } else if ( handle.tag === 'bwd' ) {
  756. handle.visible = false;
  757. }
  758. }
  759. if ( handle.name.search( 'Y' ) !== - 1 ) {
  760. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  761. if ( handle.tag === 'fwd' ) {
  762. handle.visible = false;
  763. } else {
  764. handle.scale.y *= - 1;
  765. }
  766. } else if ( handle.tag === 'bwd' ) {
  767. handle.visible = false;
  768. }
  769. }
  770. if ( handle.name.search( 'Z' ) !== - 1 ) {
  771. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  772. if ( handle.tag === 'fwd' ) {
  773. handle.visible = false;
  774. } else {
  775. handle.scale.z *= - 1;
  776. }
  777. } else if ( handle.tag === 'bwd' ) {
  778. handle.visible = false;
  779. }
  780. }
  781. } else if ( this.mode === 'rotate' ) {
  782. // Align handles to current local or world rotation
  783. tempQuaternion2.copy( quaternion );
  784. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).invert() );
  785. if ( handle.name.search( 'E' ) !== - 1 ) {
  786. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  787. }
  788. if ( handle.name === 'X' ) {
  789. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) );
  790. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  791. handle.quaternion.copy( tempQuaternion );
  792. }
  793. if ( handle.name === 'Y' ) {
  794. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  795. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  796. handle.quaternion.copy( tempQuaternion );
  797. }
  798. if ( handle.name === 'Z' ) {
  799. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  800. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  801. handle.quaternion.copy( tempQuaternion );
  802. }
  803. } // Hide disabled axes
  804. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  805. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  806. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  807. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || this.showX && this.showY && this.showZ ); // highlight selected axis
  808. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  809. handle.material._color = handle.material._color || handle.material.color.clone();
  810. handle.material.color.copy( handle.material._color );
  811. handle.material.opacity = handle.material._opacity;
  812. if ( ! this.enabled ) {
  813. handle.material.opacity *= 0.5;
  814. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  815. } else if ( this.axis ) {
  816. if ( handle.name === this.axis ) {
  817. handle.material.opacity = 1.0;
  818. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  819. } else if ( this.axis.split( '' ).some( function ( a ) {
  820. return handle.name === a;
  821. } ) ) {
  822. handle.material.opacity = 1.0;
  823. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  824. } else {
  825. handle.material.opacity *= 0.25;
  826. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  827. }
  828. }
  829. }
  830. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  831. };
  832. };
  833. TransformControlsGizmo.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  834. constructor: TransformControlsGizmo,
  835. isTransformControlsGizmo: true
  836. } );
  837. const _unitX = new THREE.Vector3( 1, 0, 0 );
  838. const _unitY = new THREE.Vector3( 0, 1, 0 );
  839. const _unitZ = new THREE.Vector3( 0, 0, 1 );
  840. const _tempVector = new THREE.Vector3();
  841. const _dirVector = new THREE.Vector3();
  842. const _alignVector = new THREE.Vector3();
  843. const _tempMatrix = new THREE.Matrix4();
  844. const _identityQuaternion = new THREE.Quaternion();
  845. class TransformControlsPlane extends THREE.Mesh {
  846. constructor() {
  847. super( new THREE.PlaneGeometry( 100000, 100000, 2, 2 ), new THREE.MeshBasicMaterial( {
  848. visible: false,
  849. wireframe: true,
  850. side: THREE.DoubleSide,
  851. transparent: true,
  852. opacity: 0.1,
  853. toneMapped: false
  854. } ) );
  855. this.type = 'TransformControlsPlane';
  856. }
  857. updateMatrixWorld( force ) {
  858. var space = this.space;
  859. this.position.copy( this.worldPosition );
  860. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  861. _unitX.set( 1, 0, 0 ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  862. _unitY.set( 0, 1, 0 ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  863. _unitZ.set( 0, 0, 1 ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion ); // Align the plane for current transform mode, axis and space.
  864. _alignVector.copy( _unitY );
  865. switch ( this.mode ) {
  866. case 'translate':
  867. case 'scale':
  868. switch ( this.axis ) {
  869. case 'X':
  870. _alignVector.copy( this.eye ).cross( _unitX );
  871. _dirVector.copy( _unitX ).cross( _alignVector );
  872. break;
  873. case 'Y':
  874. _alignVector.copy( this.eye ).cross( _unitY );
  875. _dirVector.copy( _unitY ).cross( _alignVector );
  876. break;
  877. case 'Z':
  878. _alignVector.copy( this.eye ).cross( _unitZ );
  879. _dirVector.copy( _unitZ ).cross( _alignVector );
  880. break;
  881. case 'XY':
  882. _dirVector.copy( _unitZ );
  883. break;
  884. case 'YZ':
  885. _dirVector.copy( _unitX );
  886. break;
  887. case 'XZ':
  888. _alignVector.copy( _unitZ );
  889. _dirVector.copy( _unitY );
  890. break;
  891. case 'XYZ':
  892. case 'E':
  893. _dirVector.set( 0, 0, 0 );
  894. break;
  895. }
  896. break;
  897. case 'rotate':
  898. default:
  899. // special case for rotate
  900. _dirVector.set( 0, 0, 0 );
  901. }
  902. if ( _dirVector.length() === 0 ) {
  903. // If in rotate mode, make the plane parallel to camera
  904. this.quaternion.copy( this.cameraQuaternion );
  905. } else {
  906. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  907. this.quaternion.setFromRotationMatrix( _tempMatrix );
  908. }
  909. super.updateMatrixWorld( force );
  910. }
  911. }
  912. TransformControlsPlane.prototype.isTransformControlsPlane = true;
  913. THREE.TransformControls = TransformControls;
  914. THREE.TransformControlsGizmo = TransformControlsGizmo;
  915. THREE.TransformControlsPlane = TransformControlsPlane;
  916. } )();