TransformControls.js 43 KB

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