TransformControls.js 43 KB

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