threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TransformControls.js 51KB

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