TransformControls.js 40 KB

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