TransformControls.js 40 KB

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