TransformControls.js 43 KB

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