TransformControls.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. }
  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. }
  369. TransformControls.prototype.isTransformControls = true; // mouse / touch event handlers
  370. function getPointer( event ) {
  371. if ( this.domElement.ownerDocument.pointerLockElement ) {
  372. return {
  373. x: 0,
  374. y: 0,
  375. button: event.button
  376. };
  377. } else {
  378. const rect = this.domElement.getBoundingClientRect();
  379. return {
  380. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  381. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  382. button: event.button
  383. };
  384. }
  385. }
  386. function onPointerHover( event ) {
  387. if ( ! this.enabled ) return;
  388. switch ( event.pointerType ) {
  389. case 'mouse':
  390. case 'pen':
  391. this.pointerHover( this._getPointer( event ) );
  392. break;
  393. }
  394. }
  395. function onPointerDown( event ) {
  396. if ( ! this.enabled ) return;
  397. this.domElement.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
  398. this.pointerHover( this._getPointer( event ) );
  399. this.pointerDown( this._getPointer( event ) );
  400. }
  401. function onPointerMove( event ) {
  402. if ( ! this.enabled ) return;
  403. this.pointerMove( this._getPointer( event ) );
  404. }
  405. function onPointerUp( event ) {
  406. if ( ! this.enabled ) return;
  407. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  408. this.pointerUp( this._getPointer( event ) );
  409. }
  410. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  411. const allIntersections = raycaster.intersectObject( object, true );
  412. for ( let i = 0; i < allIntersections.length; i ++ ) {
  413. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  414. return allIntersections[ i ];
  415. }
  416. }
  417. return false;
  418. } //
  419. // Reusable utility variables
  420. const _tempEuler = new THREE.Euler();
  421. const _alignVector = new THREE.Vector3( 0, 1, 0 );
  422. const _zeroVector = new THREE.Vector3( 0, 0, 0 );
  423. const _lookAtMatrix = new THREE.Matrix4();
  424. const _tempQuaternion2 = new THREE.Quaternion();
  425. const _identityQuaternion = new THREE.Quaternion();
  426. const _dirVector = new THREE.Vector3();
  427. const _tempMatrix = new THREE.Matrix4();
  428. const _unitX = new THREE.Vector3( 1, 0, 0 );
  429. const _unitY = new THREE.Vector3( 0, 1, 0 );
  430. const _unitZ = new THREE.Vector3( 0, 0, 1 );
  431. const _v1 = new THREE.Vector3();
  432. const _v2 = new THREE.Vector3();
  433. const _v3 = new THREE.Vector3();
  434. class TransformControlsGizmo extends THREE.Object3D {
  435. constructor() {
  436. super();
  437. this.type = 'TransformControlsGizmo'; // shared materials
  438. const gizmoMaterial = new THREE.MeshBasicMaterial( {
  439. depthTest: false,
  440. depthWrite: false,
  441. fog: false,
  442. toneMapped: false,
  443. transparent: true
  444. } );
  445. const gizmoLineMaterial = new THREE.LineBasicMaterial( {
  446. depthTest: false,
  447. depthWrite: false,
  448. fog: false,
  449. toneMapped: false,
  450. transparent: true
  451. } ); // Make unique material for each axis/color
  452. const matInvisible = gizmoMaterial.clone();
  453. matInvisible.opacity = 0.15;
  454. const matHelper = gizmoLineMaterial.clone();
  455. matHelper.opacity = 0.5;
  456. const matRed = gizmoMaterial.clone();
  457. matRed.color.setHex( 0xff0000 );
  458. const matGreen = gizmoMaterial.clone();
  459. matGreen.color.setHex( 0x00ff00 );
  460. const matBlue = gizmoMaterial.clone();
  461. matBlue.color.setHex( 0x0000ff );
  462. const matRedTransparent = gizmoMaterial.clone();
  463. matRedTransparent.color.setHex( 0xff0000 );
  464. matRedTransparent.opacity = 0.5;
  465. const matGreenTransparent = gizmoMaterial.clone();
  466. matGreenTransparent.color.setHex( 0x00ff00 );
  467. matGreenTransparent.opacity = 0.5;
  468. const matBlueTransparent = gizmoMaterial.clone();
  469. matBlueTransparent.color.setHex( 0x0000ff );
  470. matBlueTransparent.opacity = 0.5;
  471. const matWhiteTransparent = gizmoMaterial.clone();
  472. matWhiteTransparent.opacity = 0.25;
  473. const matYellowTransparent = gizmoMaterial.clone();
  474. matYellowTransparent.color.setHex( 0xffff00 );
  475. matYellowTransparent.opacity = 0.25;
  476. const matYellow = gizmoMaterial.clone();
  477. matYellow.color.setHex( 0xffff00 );
  478. const matGray = gizmoMaterial.clone();
  479. matGray.color.setHex( 0x787878 ); // reusable geometry
  480. const arrowGeometry = new THREE.CylinderGeometry( 0, 0.04, 0.1, 12 );
  481. arrowGeometry.translate( 0, 0.05, 0 );
  482. const scaleHandleGeometry = new THREE.BoxGeometry( 0.08, 0.08, 0.08 );
  483. scaleHandleGeometry.translate( 0, 0.04, 0 );
  484. const lineGeometry = new THREE.BufferGeometry();
  485. lineGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  486. const lineGeometry2 = new THREE.CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  487. lineGeometry2.translate( 0, 0.25, 0 );
  488. function CircleGeometry( radius, arc ) {
  489. const geometry = new THREE.TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  490. geometry.rotateY( Math.PI / 2 );
  491. geometry.rotateX( Math.PI / 2 );
  492. return geometry;
  493. } // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  494. function TranslateHelperGeometry() {
  495. const geometry = new THREE.BufferGeometry();
  496. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  497. return geometry;
  498. } // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  499. const gizmoTranslate = {
  500. 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 ]]],
  501. 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 ) ]],
  502. 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 ]]],
  503. XYZ: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]],
  504. XY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]],
  505. 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 ]]],
  506. 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 ]]]
  507. };
  508. const pickerTranslate = {
  509. 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 ]]],
  510. 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 ]]],
  511. 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 ]]],
  512. XYZ: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), matInvisible ) ]],
  513. XY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]],
  514. YZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]],
  515. XZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]]
  516. };
  517. const helperTranslate = {
  518. START: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]],
  519. END: [[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]],
  520. DELTA: [[ new THREE.Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]],
  521. X: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]],
  522. Y: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]],
  523. Z: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]]
  524. };
  525. const gizmoRotate = {
  526. XYZE: [[ new THREE.Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]],
  527. X: [[ new THREE.Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]],
  528. Y: [[ new THREE.Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]],
  529. Z: [[ new THREE.Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]],
  530. E: [[ new THREE.Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]]
  531. };
  532. const helperRotate = {
  533. AXIS: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]]
  534. };
  535. const pickerRotate = {
  536. XYZE: [[ new THREE.Mesh( new THREE.SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]],
  537. X: [[ new THREE.Mesh( new THREE.TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]]],
  538. Y: [[ new THREE.Mesh( new THREE.TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]]],
  539. Z: [[ new THREE.Mesh( new THREE.TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]],
  540. E: [[ new THREE.Mesh( new THREE.TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]]
  541. };
  542. const gizmoScale = {
  543. 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 ]]],
  544. 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 ]]],
  545. 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 ]]],
  546. XY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]],
  547. YZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]],
  548. XZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]],
  549. XYZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ]]
  550. };
  551. const pickerScale = {
  552. 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 ]]],
  553. 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 ]]],
  554. 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 ]]],
  555. XY: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]],
  556. YZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]],
  557. XZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]],
  558. XYZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]]]
  559. };
  560. const helperScale = {
  561. X: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]],
  562. Y: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]],
  563. Z: [[ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]]
  564. }; // Creates an THREE.Object3D with gizmos described in custom hierarchy definition.
  565. function setupGizmo( gizmoMap ) {
  566. const gizmo = new THREE.Object3D();
  567. for ( const name in gizmoMap ) {
  568. for ( let i = gizmoMap[ name ].length; i --; ) {
  569. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  570. const position = gizmoMap[ name ][ i ][ 1 ];
  571. const rotation = gizmoMap[ name ][ i ][ 2 ];
  572. const scale = gizmoMap[ name ][ i ][ 3 ];
  573. const tag = gizmoMap[ name ][ i ][ 4 ]; // name and tag properties are essential for picking and updating logic.
  574. object.name = name;
  575. object.tag = tag;
  576. if ( position ) {
  577. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  578. }
  579. if ( rotation ) {
  580. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  581. }
  582. if ( scale ) {
  583. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  584. }
  585. object.updateMatrix();
  586. const tempGeometry = object.geometry.clone();
  587. tempGeometry.applyMatrix4( object.matrix );
  588. object.geometry = tempGeometry;
  589. object.renderOrder = Infinity;
  590. object.position.set( 0, 0, 0 );
  591. object.rotation.set( 0, 0, 0 );
  592. object.scale.set( 1, 1, 1 );
  593. gizmo.add( object );
  594. }
  595. }
  596. return gizmo;
  597. } // Gizmo creation
  598. this.gizmo = {};
  599. this.picker = {};
  600. this.helper = {};
  601. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  602. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  603. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  604. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  605. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  606. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  607. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  608. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  609. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) ); // Pickers should be hidden always
  610. this.picker[ 'translate' ].visible = false;
  611. this.picker[ 'rotate' ].visible = false;
  612. this.picker[ 'scale' ].visible = false;
  613. } // updateMatrixWorld will update transformations and appearance of individual handles
  614. updateMatrixWorld( force ) {
  615. const space = this.mode === 'scale' ? 'local' : this.space; // scale always oriented to local rotation
  616. const quaternion = space === 'local' ? this.worldQuaternion : _identityQuaternion; // Show only gizmos for current transform mode
  617. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  618. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  619. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  620. this.helper[ 'translate' ].visible = this.mode === 'translate';
  621. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  622. this.helper[ 'scale' ].visible = this.mode === 'scale';
  623. let handles = [];
  624. handles = handles.concat( this.picker[ this.mode ].children );
  625. handles = handles.concat( this.gizmo[ this.mode ].children );
  626. handles = handles.concat( this.helper[ this.mode ].children );
  627. for ( let i = 0; i < handles.length; i ++ ) {
  628. const handle = handles[ i ]; // hide aligned to camera
  629. handle.visible = true;
  630. handle.rotation.set( 0, 0, 0 );
  631. handle.position.copy( this.worldPosition );
  632. let factor;
  633. if ( this.camera.isOrthographicCamera ) {
  634. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  635. } else {
  636. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  637. }
  638. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 ); // TODO: simplify helpers and consider decoupling from gizmo
  639. if ( handle.tag === 'helper' ) {
  640. handle.visible = false;
  641. if ( handle.name === 'AXIS' ) {
  642. handle.position.copy( this.worldPositionStart );
  643. handle.visible = !! this.axis;
  644. if ( this.axis === 'X' ) {
  645. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  646. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  647. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  648. handle.visible = false;
  649. }
  650. }
  651. if ( this.axis === 'Y' ) {
  652. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  653. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  654. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  655. handle.visible = false;
  656. }
  657. }
  658. if ( this.axis === 'Z' ) {
  659. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  660. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  661. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  662. handle.visible = false;
  663. }
  664. }
  665. if ( this.axis === 'XYZE' ) {
  666. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  667. _alignVector.copy( this.rotationAxis );
  668. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  669. handle.quaternion.multiply( _tempQuaternion );
  670. handle.visible = this.dragging;
  671. }
  672. if ( this.axis === 'E' ) {
  673. handle.visible = false;
  674. }
  675. } else if ( handle.name === 'START' ) {
  676. handle.position.copy( this.worldPositionStart );
  677. handle.visible = this.dragging;
  678. } else if ( handle.name === 'END' ) {
  679. handle.position.copy( this.worldPosition );
  680. handle.visible = this.dragging;
  681. } else if ( handle.name === 'DELTA' ) {
  682. handle.position.copy( this.worldPositionStart );
  683. handle.quaternion.copy( this.worldQuaternionStart );
  684. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  685. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  686. handle.scale.copy( _tempVector );
  687. handle.visible = this.dragging;
  688. } else {
  689. handle.quaternion.copy( quaternion );
  690. if ( this.dragging ) {
  691. handle.position.copy( this.worldPositionStart );
  692. } else {
  693. handle.position.copy( this.worldPosition );
  694. }
  695. if ( this.axis ) {
  696. handle.visible = this.axis.search( handle.name ) !== - 1;
  697. }
  698. } // If updating helper, skip rest of the loop
  699. continue;
  700. } // Align handles to current local or world rotation
  701. handle.quaternion.copy( quaternion );
  702. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  703. // Hide translate and scale axis facing the camera
  704. const AXIS_HIDE_TRESHOLD = 0.99;
  705. const PLANE_HIDE_TRESHOLD = 0.2;
  706. if ( handle.name === 'X' ) {
  707. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  708. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  709. handle.visible = false;
  710. }
  711. }
  712. if ( handle.name === 'Y' ) {
  713. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  714. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  715. handle.visible = false;
  716. }
  717. }
  718. if ( handle.name === 'Z' ) {
  719. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  720. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  721. handle.visible = false;
  722. }
  723. }
  724. if ( handle.name === 'XY' ) {
  725. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  726. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  727. handle.visible = false;
  728. }
  729. }
  730. if ( handle.name === 'YZ' ) {
  731. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  732. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  733. handle.visible = false;
  734. }
  735. }
  736. if ( handle.name === 'XZ' ) {
  737. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  738. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  739. handle.visible = false;
  740. }
  741. }
  742. } else if ( this.mode === 'rotate' ) {
  743. // Align handles to current local or world rotation
  744. _tempQuaternion2.copy( quaternion );
  745. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  746. if ( handle.name.search( 'E' ) !== - 1 ) {
  747. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  748. }
  749. if ( handle.name === 'X' ) {
  750. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  751. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  752. handle.quaternion.copy( _tempQuaternion );
  753. }
  754. if ( handle.name === 'Y' ) {
  755. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  756. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  757. handle.quaternion.copy( _tempQuaternion );
  758. }
  759. if ( handle.name === 'Z' ) {
  760. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  761. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  762. handle.quaternion.copy( _tempQuaternion );
  763. }
  764. } // Hide disabled axes
  765. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  766. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  767. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  768. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || this.showX && this.showY && this.showZ ); // highlight selected axis
  769. handle.material._color = handle.material._color || handle.material.color.clone();
  770. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  771. handle.material.color.copy( handle.material._color );
  772. handle.material.opacity = handle.material._opacity;
  773. if ( this.enabled && this.axis ) {
  774. if ( handle.name === this.axis ) {
  775. handle.material.color.setHex( 0xffff00 );
  776. handle.material.opacity = 1.0;
  777. } else if ( this.axis.split( '' ).some( function ( a ) {
  778. return handle.name === a;
  779. } ) ) {
  780. handle.material.color.setHex( 0xffff00 );
  781. handle.material.opacity = 1.0;
  782. }
  783. }
  784. }
  785. super.updateMatrixWorld( force );
  786. }
  787. }
  788. TransformControlsGizmo.prototype.isTransformControlsGizmo = true; //
  789. class TransformControlsPlane extends THREE.Mesh {
  790. constructor() {
  791. super( new THREE.PlaneGeometry( 100000, 100000, 2, 2 ), new THREE.MeshBasicMaterial( {
  792. visible: false,
  793. wireframe: true,
  794. side: THREE.DoubleSide,
  795. transparent: true,
  796. opacity: 0.1,
  797. toneMapped: false
  798. } ) );
  799. this.type = 'TransformControlsPlane';
  800. }
  801. updateMatrixWorld( force ) {
  802. let space = this.space;
  803. this.position.copy( this.worldPosition );
  804. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  805. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  806. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  807. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion ); // Align the plane for current transform mode, axis and space.
  808. _alignVector.copy( _v2 );
  809. switch ( this.mode ) {
  810. case 'translate':
  811. case 'scale':
  812. switch ( this.axis ) {
  813. case 'X':
  814. _alignVector.copy( this.eye ).cross( _v1 );
  815. _dirVector.copy( _v1 ).cross( _alignVector );
  816. break;
  817. case 'Y':
  818. _alignVector.copy( this.eye ).cross( _v2 );
  819. _dirVector.copy( _v2 ).cross( _alignVector );
  820. break;
  821. case 'Z':
  822. _alignVector.copy( this.eye ).cross( _v3 );
  823. _dirVector.copy( _v3 ).cross( _alignVector );
  824. break;
  825. case 'XY':
  826. _dirVector.copy( _v3 );
  827. break;
  828. case 'YZ':
  829. _dirVector.copy( _v1 );
  830. break;
  831. case 'XZ':
  832. _alignVector.copy( _v3 );
  833. _dirVector.copy( _v2 );
  834. break;
  835. case 'XYZ':
  836. case 'E':
  837. _dirVector.set( 0, 0, 0 );
  838. break;
  839. }
  840. break;
  841. case 'rotate':
  842. default:
  843. // special case for rotate
  844. _dirVector.set( 0, 0, 0 );
  845. }
  846. if ( _dirVector.length() === 0 ) {
  847. // If in rotate mode, make the plane parallel to camera
  848. this.quaternion.copy( this.cameraQuaternion );
  849. } else {
  850. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  851. this.quaternion.setFromRotationMatrix( _tempMatrix );
  852. }
  853. super.updateMatrixWorld( force );
  854. }
  855. }
  856. TransformControlsPlane.prototype.isTransformControlsPlane = true;
  857. THREE.TransformControls = TransformControls;
  858. THREE.TransformControlsGizmo = TransformControlsGizmo;
  859. THREE.TransformControlsPlane = TransformControlsPlane;
  860. } )();