TransformControls.js 41 KB

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