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