TransformControls.js 40 KB

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