TransformControls.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  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.traverse( function ( child ) {
  330. if ( child.geometry ) child.geometry.dispose();
  331. if ( child.material ) child.material.dispose();
  332. } );
  333. }
  334. // Set current object
  335. attach( object ) {
  336. this.object = object;
  337. this._root.visible = true;
  338. return this;
  339. }
  340. // Detach from object
  341. detach() {
  342. this.object = undefined;
  343. this.axis = null;
  344. this._root.visible = false;
  345. return this;
  346. }
  347. reset() {
  348. if ( ! this.enabled ) return;
  349. if ( this.dragging ) {
  350. this.object.position.copy( this._positionStart );
  351. this.object.quaternion.copy( this._quaternionStart );
  352. this.object.scale.copy( this._scaleStart );
  353. this.dispatchEvent( _changeEvent );
  354. this.dispatchEvent( _objectChangeEvent );
  355. this.pointStart.copy( this.pointEnd );
  356. }
  357. }
  358. getRaycaster() {
  359. return _raycaster;
  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. }
  384. // mouse / touch event handlers
  385. function getPointer( event ) {
  386. if ( this.domElement.ownerDocument.pointerLockElement ) {
  387. return {
  388. x: 0,
  389. y: 0,
  390. button: event.button
  391. };
  392. } else {
  393. const rect = this.domElement.getBoundingClientRect();
  394. return {
  395. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  396. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  397. button: event.button
  398. };
  399. }
  400. }
  401. function onPointerHover( event ) {
  402. if ( ! this.enabled ) return;
  403. switch ( event.pointerType ) {
  404. case 'mouse':
  405. case 'pen':
  406. this.pointerHover( this._getPointer( event ) );
  407. break;
  408. }
  409. }
  410. function onPointerDown( event ) {
  411. if ( ! this.enabled ) return;
  412. if ( ! document.pointerLockElement ) {
  413. this.domElement.setPointerCapture( event.pointerId );
  414. }
  415. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  416. this.pointerHover( this._getPointer( event ) );
  417. this.pointerDown( this._getPointer( event ) );
  418. }
  419. function onPointerMove( event ) {
  420. if ( ! this.enabled ) return;
  421. this.pointerMove( this._getPointer( event ) );
  422. }
  423. function onPointerUp( event ) {
  424. if ( ! this.enabled ) return;
  425. this.domElement.releasePointerCapture( event.pointerId );
  426. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  427. this.pointerUp( this._getPointer( event ) );
  428. }
  429. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  430. const allIntersections = raycaster.intersectObject( object, true );
  431. for ( let i = 0; i < allIntersections.length; i ++ ) {
  432. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  433. return allIntersections[ i ];
  434. }
  435. }
  436. return false;
  437. }
  438. //
  439. // Reusable utility variables
  440. const _tempEuler = new Euler();
  441. const _alignVector = new Vector3( 0, 1, 0 );
  442. const _zeroVector = new Vector3( 0, 0, 0 );
  443. const _lookAtMatrix = new Matrix4();
  444. const _tempQuaternion2 = new Quaternion();
  445. const _identityQuaternion = new Quaternion();
  446. const _dirVector = new Vector3();
  447. const _tempMatrix = new Matrix4();
  448. const _unitX = new Vector3( 1, 0, 0 );
  449. const _unitY = new Vector3( 0, 1, 0 );
  450. const _unitZ = new Vector3( 0, 0, 1 );
  451. const _v1 = new Vector3();
  452. const _v2 = new Vector3();
  453. const _v3 = new Vector3();
  454. class TransformControlsRoot extends Object3D {
  455. constructor( controls ) {
  456. super();
  457. this.isTransformControlsRoot = true;
  458. this.controls = controls;
  459. this.visible = false;
  460. }
  461. // updateMatrixWorld updates key transformation variables
  462. updateMatrixWorld( force ) {
  463. const controls = this.controls;
  464. if ( controls.object !== undefined ) {
  465. controls.object.updateMatrixWorld();
  466. if ( controls.object.parent === null ) {
  467. console.error( 'TransformControls: The attached 3D object must be a part of the scene graph.' );
  468. } else {
  469. controls.object.parent.matrixWorld.decompose( controls._parentPosition, controls._parentQuaternion, controls._parentScale );
  470. }
  471. controls.object.matrixWorld.decompose( controls.worldPosition, controls.worldQuaternion, controls._worldScale );
  472. controls._parentQuaternionInv.copy( controls._parentQuaternion ).invert();
  473. controls._worldQuaternionInv.copy( controls.worldQuaternion ).invert();
  474. }
  475. controls.camera.updateMatrixWorld();
  476. controls.camera.matrixWorld.decompose( controls.cameraPosition, controls.cameraQuaternion, controls._cameraScale );
  477. if ( controls.camera.isOrthographicCamera ) {
  478. controls.camera.getWorldDirection( controls.eye ).negate();
  479. } else {
  480. controls.eye.copy( controls.cameraPosition ).sub( controls.worldPosition ).normalize();
  481. }
  482. super.updateMatrixWorld( force );
  483. }
  484. }
  485. class TransformControlsGizmo extends Object3D {
  486. constructor() {
  487. super();
  488. this.isTransformControlsGizmo = true;
  489. this.type = 'TransformControlsGizmo';
  490. // shared materials
  491. const gizmoMaterial = new MeshBasicMaterial( {
  492. depthTest: false,
  493. depthWrite: false,
  494. fog: false,
  495. toneMapped: false,
  496. transparent: true
  497. } );
  498. const gizmoLineMaterial = new LineBasicMaterial( {
  499. depthTest: false,
  500. depthWrite: false,
  501. fog: false,
  502. toneMapped: false,
  503. transparent: true
  504. } );
  505. // Make unique material for each axis/color
  506. const matInvisible = gizmoMaterial.clone();
  507. matInvisible.opacity = 0.15;
  508. const matHelper = gizmoLineMaterial.clone();
  509. matHelper.opacity = 0.5;
  510. const matRed = gizmoMaterial.clone();
  511. matRed.color.setHex( 0xff0000 );
  512. const matGreen = gizmoMaterial.clone();
  513. matGreen.color.setHex( 0x00ff00 );
  514. const matBlue = gizmoMaterial.clone();
  515. matBlue.color.setHex( 0x0000ff );
  516. const matRedTransparent = gizmoMaterial.clone();
  517. matRedTransparent.color.setHex( 0xff0000 );
  518. matRedTransparent.opacity = 0.5;
  519. const matGreenTransparent = gizmoMaterial.clone();
  520. matGreenTransparent.color.setHex( 0x00ff00 );
  521. matGreenTransparent.opacity = 0.5;
  522. const matBlueTransparent = gizmoMaterial.clone();
  523. matBlueTransparent.color.setHex( 0x0000ff );
  524. matBlueTransparent.opacity = 0.5;
  525. const matWhiteTransparent = gizmoMaterial.clone();
  526. matWhiteTransparent.opacity = 0.25;
  527. const matYellowTransparent = gizmoMaterial.clone();
  528. matYellowTransparent.color.setHex( 0xffff00 );
  529. matYellowTransparent.opacity = 0.25;
  530. const matYellow = gizmoMaterial.clone();
  531. matYellow.color.setHex( 0xffff00 );
  532. const matGray = gizmoMaterial.clone();
  533. matGray.color.setHex( 0x787878 );
  534. // reusable geometry
  535. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  536. arrowGeometry.translate( 0, 0.05, 0 );
  537. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  538. scaleHandleGeometry.translate( 0, 0.04, 0 );
  539. const lineGeometry = new BufferGeometry();
  540. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  541. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  542. lineGeometry2.translate( 0, 0.25, 0 );
  543. function CircleGeometry( radius, arc ) {
  544. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  545. geometry.rotateY( Math.PI / 2 );
  546. geometry.rotateX( Math.PI / 2 );
  547. return geometry;
  548. }
  549. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  550. function TranslateHelperGeometry() {
  551. const geometry = new BufferGeometry();
  552. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  553. return geometry;
  554. }
  555. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  556. const gizmoTranslate = {
  557. X: [
  558. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  559. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  560. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  561. ],
  562. Y: [
  563. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  564. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  565. [ new Mesh( lineGeometry2, matGreen ) ]
  566. ],
  567. Z: [
  568. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  569. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  570. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  571. ],
  572. XYZ: [
  573. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]
  574. ],
  575. XY: [
  576. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]
  577. ],
  578. YZ: [
  579. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  580. ],
  581. XZ: [
  582. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  583. ]
  584. };
  585. const pickerTranslate = {
  586. X: [
  587. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  588. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  589. ],
  590. Y: [
  591. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  592. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  593. ],
  594. Z: [
  595. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  596. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  597. ],
  598. XYZ: [
  599. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  600. ],
  601. XY: [
  602. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  603. ],
  604. YZ: [
  605. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  606. ],
  607. XZ: [
  608. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  609. ]
  610. };
  611. const helperTranslate = {
  612. START: [
  613. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  614. ],
  615. END: [
  616. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  617. ],
  618. DELTA: [
  619. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  620. ],
  621. X: [
  622. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  623. ],
  624. Y: [
  625. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  626. ],
  627. Z: [
  628. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  629. ]
  630. };
  631. const gizmoRotate = {
  632. XYZE: [
  633. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  634. ],
  635. X: [
  636. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  637. ],
  638. Y: [
  639. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  640. ],
  641. Z: [
  642. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  643. ],
  644. E: [
  645. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  646. ]
  647. };
  648. const helperRotate = {
  649. AXIS: [
  650. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  651. ]
  652. };
  653. const pickerRotate = {
  654. XYZE: [
  655. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  656. ],
  657. X: [
  658. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  659. ],
  660. Y: [
  661. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  662. ],
  663. Z: [
  664. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  665. ],
  666. E: [
  667. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  668. ]
  669. };
  670. const gizmoScale = {
  671. X: [
  672. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  673. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  674. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  675. ],
  676. Y: [
  677. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  678. [ new Mesh( lineGeometry2, matGreen ) ],
  679. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  680. ],
  681. Z: [
  682. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  683. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  684. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  685. ],
  686. XY: [
  687. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  688. ],
  689. YZ: [
  690. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  691. ],
  692. XZ: [
  693. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  694. ],
  695. XYZ: [
  696. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ],
  697. ]
  698. };
  699. const pickerScale = {
  700. X: [
  701. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  702. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  703. ],
  704. Y: [
  705. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  706. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  707. ],
  708. Z: [
  709. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  710. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  711. ],
  712. XY: [
  713. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  714. ],
  715. YZ: [
  716. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  717. ],
  718. XZ: [
  719. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  720. ],
  721. XYZ: [
  722. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  723. ]
  724. };
  725. const helperScale = {
  726. X: [
  727. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  728. ],
  729. Y: [
  730. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  731. ],
  732. Z: [
  733. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  734. ]
  735. };
  736. // Creates an Object3D with gizmos described in custom hierarchy definition.
  737. function setupGizmo( gizmoMap ) {
  738. const gizmo = new Object3D();
  739. for ( const name in gizmoMap ) {
  740. for ( let i = gizmoMap[ name ].length; i --; ) {
  741. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  742. const position = gizmoMap[ name ][ i ][ 1 ];
  743. const rotation = gizmoMap[ name ][ i ][ 2 ];
  744. const scale = gizmoMap[ name ][ i ][ 3 ];
  745. const tag = gizmoMap[ name ][ i ][ 4 ];
  746. // name and tag properties are essential for picking and updating logic.
  747. object.name = name;
  748. object.tag = tag;
  749. if ( position ) {
  750. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  751. }
  752. if ( rotation ) {
  753. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  754. }
  755. if ( scale ) {
  756. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  757. }
  758. object.updateMatrix();
  759. const tempGeometry = object.geometry.clone();
  760. tempGeometry.applyMatrix4( object.matrix );
  761. object.geometry = tempGeometry;
  762. object.renderOrder = Infinity;
  763. object.position.set( 0, 0, 0 );
  764. object.rotation.set( 0, 0, 0 );
  765. object.scale.set( 1, 1, 1 );
  766. gizmo.add( object );
  767. }
  768. }
  769. return gizmo;
  770. }
  771. // Gizmo creation
  772. this.gizmo = {};
  773. this.picker = {};
  774. this.helper = {};
  775. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  776. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  777. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  778. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  779. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  780. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  781. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  782. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  783. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  784. // Pickers should be hidden always
  785. this.picker[ 'translate' ].visible = false;
  786. this.picker[ 'rotate' ].visible = false;
  787. this.picker[ 'scale' ].visible = false;
  788. }
  789. // updateMatrixWorld will update transformations and appearance of individual handles
  790. updateMatrixWorld( force ) {
  791. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  792. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  793. // Show only gizmos for current transform mode
  794. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  795. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  796. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  797. this.helper[ 'translate' ].visible = this.mode === 'translate';
  798. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  799. this.helper[ 'scale' ].visible = this.mode === 'scale';
  800. let handles = [];
  801. handles = handles.concat( this.picker[ this.mode ].children );
  802. handles = handles.concat( this.gizmo[ this.mode ].children );
  803. handles = handles.concat( this.helper[ this.mode ].children );
  804. for ( let i = 0; i < handles.length; i ++ ) {
  805. const handle = handles[ i ];
  806. // hide aligned to camera
  807. handle.visible = true;
  808. handle.rotation.set( 0, 0, 0 );
  809. handle.position.copy( this.worldPosition );
  810. let factor;
  811. if ( this.camera.isOrthographicCamera ) {
  812. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  813. } else {
  814. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  815. }
  816. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  817. // TODO: simplify helpers and consider decoupling from gizmo
  818. if ( handle.tag === 'helper' ) {
  819. handle.visible = false;
  820. if ( handle.name === 'AXIS' ) {
  821. handle.visible = !! this.axis;
  822. if ( this.axis === 'X' ) {
  823. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  824. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  825. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  826. handle.visible = false;
  827. }
  828. }
  829. if ( this.axis === 'Y' ) {
  830. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  831. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  832. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  833. handle.visible = false;
  834. }
  835. }
  836. if ( this.axis === 'Z' ) {
  837. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  838. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  839. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  840. handle.visible = false;
  841. }
  842. }
  843. if ( this.axis === 'XYZE' ) {
  844. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  845. _alignVector.copy( this.rotationAxis );
  846. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  847. handle.quaternion.multiply( _tempQuaternion );
  848. handle.visible = this.dragging;
  849. }
  850. if ( this.axis === 'E' ) {
  851. handle.visible = false;
  852. }
  853. } else if ( handle.name === 'START' ) {
  854. handle.position.copy( this.worldPositionStart );
  855. handle.visible = this.dragging;
  856. } else if ( handle.name === 'END' ) {
  857. handle.position.copy( this.worldPosition );
  858. handle.visible = this.dragging;
  859. } else if ( handle.name === 'DELTA' ) {
  860. handle.position.copy( this.worldPositionStart );
  861. handle.quaternion.copy( this.worldQuaternionStart );
  862. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  863. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  864. handle.scale.copy( _tempVector );
  865. handle.visible = this.dragging;
  866. } else {
  867. handle.quaternion.copy( quaternion );
  868. if ( this.dragging ) {
  869. handle.position.copy( this.worldPositionStart );
  870. } else {
  871. handle.position.copy( this.worldPosition );
  872. }
  873. if ( this.axis ) {
  874. handle.visible = this.axis.search( handle.name ) !== - 1;
  875. }
  876. }
  877. // If updating helper, skip rest of the loop
  878. continue;
  879. }
  880. // Align handles to current local or world rotation
  881. handle.quaternion.copy( quaternion );
  882. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  883. // Hide translate and scale axis facing the camera
  884. const AXIS_HIDE_THRESHOLD = 0.99;
  885. const PLANE_HIDE_THRESHOLD = 0.2;
  886. if ( handle.name === 'X' ) {
  887. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  888. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  889. handle.visible = false;
  890. }
  891. }
  892. if ( handle.name === 'Y' ) {
  893. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  894. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  895. handle.visible = false;
  896. }
  897. }
  898. if ( handle.name === 'Z' ) {
  899. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  900. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  901. handle.visible = false;
  902. }
  903. }
  904. if ( handle.name === 'XY' ) {
  905. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  906. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  907. handle.visible = false;
  908. }
  909. }
  910. if ( handle.name === 'YZ' ) {
  911. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  912. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  913. handle.visible = false;
  914. }
  915. }
  916. if ( handle.name === 'XZ' ) {
  917. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  918. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  919. handle.visible = false;
  920. }
  921. }
  922. } else if ( this.mode === 'rotate' ) {
  923. // Align handles to current local or world rotation
  924. _tempQuaternion2.copy( quaternion );
  925. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  926. if ( handle.name.search( 'E' ) !== - 1 ) {
  927. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  928. }
  929. if ( handle.name === 'X' ) {
  930. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  931. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  932. handle.quaternion.copy( _tempQuaternion );
  933. }
  934. if ( handle.name === 'Y' ) {
  935. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  936. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  937. handle.quaternion.copy( _tempQuaternion );
  938. }
  939. if ( handle.name === 'Z' ) {
  940. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  941. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  942. handle.quaternion.copy( _tempQuaternion );
  943. }
  944. }
  945. // Hide disabled axes
  946. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  947. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  948. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  949. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  950. // highlight selected axis
  951. handle.material._color = handle.material._color || handle.material.color.clone();
  952. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  953. handle.material.color.copy( handle.material._color );
  954. handle.material.opacity = handle.material._opacity;
  955. if ( this.enabled && this.axis ) {
  956. if ( handle.name === this.axis ) {
  957. handle.material.color.setHex( 0xffff00 );
  958. handle.material.opacity = 1.0;
  959. } else if ( this.axis.split( '' ).some( function ( a ) {
  960. return handle.name === a;
  961. } ) ) {
  962. handle.material.color.setHex( 0xffff00 );
  963. handle.material.opacity = 1.0;
  964. }
  965. }
  966. }
  967. super.updateMatrixWorld( force );
  968. }
  969. }
  970. //
  971. class TransformControlsPlane extends Mesh {
  972. constructor() {
  973. super(
  974. new PlaneGeometry( 100000, 100000, 2, 2 ),
  975. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  976. );
  977. this.isTransformControlsPlane = true;
  978. this.type = 'TransformControlsPlane';
  979. }
  980. updateMatrixWorld( force ) {
  981. let space = this.space;
  982. this.position.copy( this.worldPosition );
  983. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  984. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  985. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  986. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  987. // Align the plane for current transform mode, axis and space.
  988. _alignVector.copy( _v2 );
  989. switch ( this.mode ) {
  990. case 'translate':
  991. case 'scale':
  992. switch ( this.axis ) {
  993. case 'X':
  994. _alignVector.copy( this.eye ).cross( _v1 );
  995. _dirVector.copy( _v1 ).cross( _alignVector );
  996. break;
  997. case 'Y':
  998. _alignVector.copy( this.eye ).cross( _v2 );
  999. _dirVector.copy( _v2 ).cross( _alignVector );
  1000. break;
  1001. case 'Z':
  1002. _alignVector.copy( this.eye ).cross( _v3 );
  1003. _dirVector.copy( _v3 ).cross( _alignVector );
  1004. break;
  1005. case 'XY':
  1006. _dirVector.copy( _v3 );
  1007. break;
  1008. case 'YZ':
  1009. _dirVector.copy( _v1 );
  1010. break;
  1011. case 'XZ':
  1012. _alignVector.copy( _v3 );
  1013. _dirVector.copy( _v2 );
  1014. break;
  1015. case 'XYZ':
  1016. case 'E':
  1017. _dirVector.set( 0, 0, 0 );
  1018. break;
  1019. }
  1020. break;
  1021. case 'rotate':
  1022. default:
  1023. // special case for rotate
  1024. _dirVector.set( 0, 0, 0 );
  1025. }
  1026. if ( _dirVector.length() === 0 ) {
  1027. // If in rotate mode, make the plane parallel to camera
  1028. this.quaternion.copy( this.cameraQuaternion );
  1029. } else {
  1030. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  1031. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1032. }
  1033. super.updateMatrixWorld( force );
  1034. }
  1035. }
  1036. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };
粤ICP备19079148号