TransformControls.js 39 KB

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