TransformControls.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. import {
  2. BoxGeometry,
  3. BufferGeometry,
  4. CylinderGeometry,
  5. DoubleSide,
  6. Euler,
  7. Float32BufferAttribute,
  8. Line,
  9. LineBasicMaterial,
  10. Matrix4,
  11. Mesh,
  12. MeshBasicMaterial,
  13. Object3D,
  14. OctahedronGeometry,
  15. PlaneGeometry,
  16. Quaternion,
  17. Raycaster,
  18. SphereGeometry,
  19. TorusGeometry,
  20. Vector3
  21. } from '../../../build/three.module.js';
  22. const _raycaster = new Raycaster();
  23. const _tempVector = new Vector3();
  24. const _tempVector2 = new Vector3();
  25. const _tempQuaternion = new Quaternion();
  26. const _unit = {
  27. X: new Vector3( 1, 0, 0 ),
  28. Y: new Vector3( 0, 1, 0 ),
  29. Z: new Vector3( 0, 0, 1 )
  30. };
  31. const _changeEvent = { type: 'change' };
  32. const _mouseDownEvent = { type: 'mouseDown' };
  33. const _mouseUpEvent = { type: 'mouseUp', mode: null };
  34. const _objectChangeEvent = { type: 'objectChange' };
  35. class TransformControls extends Object3D {
  36. constructor( camera, domElement ) {
  37. super();
  38. if ( domElement === undefined ) {
  39. console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' );
  40. domElement = document;
  41. }
  42. this.visible = false;
  43. this.domElement = domElement;
  44. this.domElement.style.touchAction = 'none'; // disable touch scroll
  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.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
  416. this.pointerHover( this._getPointer( event ) );
  417. this.pointerDown( this._getPointer( event ) );
  418. }
  419. function onPointerMove( event ) {
  420. if ( ! this.enabled ) return;
  421. this.pointerMove( this._getPointer( event ) );
  422. }
  423. function onPointerUp( event ) {
  424. if ( ! this.enabled ) return;
  425. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  426. this.pointerUp( this._getPointer( event ) );
  427. }
  428. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  429. const allIntersections = raycaster.intersectObject( object, true );
  430. for ( let i = 0; i < allIntersections.length; i ++ ) {
  431. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  432. return allIntersections[ i ];
  433. }
  434. }
  435. return false;
  436. }
  437. //
  438. // Reusable utility variables
  439. const _tempEuler = new Euler();
  440. const _alignVector = new Vector3( 0, 1, 0 );
  441. const _zeroVector = new Vector3( 0, 0, 0 );
  442. const _lookAtMatrix = new Matrix4();
  443. const _tempQuaternion2 = new Quaternion();
  444. const _identityQuaternion = new Quaternion();
  445. const _dirVector = new Vector3();
  446. const _tempMatrix = new Matrix4();
  447. const _unitX = new Vector3( 1, 0, 0 );
  448. const _unitY = new Vector3( 0, 1, 0 );
  449. const _unitZ = new Vector3( 0, 0, 1 );
  450. const _v1 = new Vector3();
  451. const _v2 = new Vector3();
  452. const _v3 = new Vector3();
  453. class TransformControlsGizmo extends Object3D {
  454. constructor() {
  455. super();
  456. this.type = 'TransformControlsGizmo';
  457. // shared materials
  458. const gizmoMaterial = new MeshBasicMaterial( {
  459. depthTest: false,
  460. depthWrite: false,
  461. transparent: true,
  462. side: DoubleSide,
  463. fog: false,
  464. toneMapped: false
  465. } );
  466. const gizmoLineMaterial = new LineBasicMaterial( {
  467. depthTest: false,
  468. depthWrite: false,
  469. transparent: true,
  470. linewidth: 1,
  471. fog: false,
  472. toneMapped: false
  473. } );
  474. // Make unique material for each axis/color
  475. const matInvisible = gizmoMaterial.clone();
  476. matInvisible.opacity = 0.15;
  477. const matHelper = gizmoMaterial.clone();
  478. matHelper.opacity = 0.33;
  479. const matRed = gizmoMaterial.clone();
  480. matRed.color.setHex( 0xff0000 );
  481. const matGreen = gizmoMaterial.clone();
  482. matGreen.color.setHex( 0x00ff00 );
  483. const matBlue = gizmoMaterial.clone();
  484. matBlue.color.setHex( 0x0000ff );
  485. const matWhiteTransparent = gizmoMaterial.clone();
  486. matWhiteTransparent.opacity = 0.25;
  487. const matYellowTransparent = matWhiteTransparent.clone();
  488. matYellowTransparent.color.setHex( 0xffff00 );
  489. const matCyanTransparent = matWhiteTransparent.clone();
  490. matCyanTransparent.color.setHex( 0x00ffff );
  491. const matMagentaTransparent = matWhiteTransparent.clone();
  492. matMagentaTransparent.color.setHex( 0xff00ff );
  493. const matYellow = gizmoMaterial.clone();
  494. matYellow.color.setHex( 0xffff00 );
  495. const matLineRed = gizmoLineMaterial.clone();
  496. matLineRed.color.setHex( 0xff0000 );
  497. const matLineGreen = gizmoLineMaterial.clone();
  498. matLineGreen.color.setHex( 0x00ff00 );
  499. const matLineBlue = gizmoLineMaterial.clone();
  500. matLineBlue.color.setHex( 0x0000ff );
  501. const matLineCyan = gizmoLineMaterial.clone();
  502. matLineCyan.color.setHex( 0x00ffff );
  503. const matLineMagenta = gizmoLineMaterial.clone();
  504. matLineMagenta.color.setHex( 0xff00ff );
  505. const matLineYellow = gizmoLineMaterial.clone();
  506. matLineYellow.color.setHex( 0xffff00 );
  507. const matLineGray = gizmoLineMaterial.clone();
  508. matLineGray.color.setHex( 0x787878 );
  509. const matLineYellowTransparent = matLineYellow.clone();
  510. matLineYellowTransparent.opacity = 0.25;
  511. // reusable geometry
  512. const arrowGeometry = new CylinderGeometry( 0, 0.05, 0.2, 12, 1, false );
  513. const scaleHandleGeometry = new BoxGeometry( 0.125, 0.125, 0.125 );
  514. const lineGeometry = new BufferGeometry();
  515. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  516. function CircleGeometry( radius, arc ) {
  517. const geometry = new BufferGeometry( );
  518. const vertices = [];
  519. for ( let i = 0; i <= 64 * arc; ++ i ) {
  520. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  521. }
  522. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  523. return geometry;
  524. }
  525. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  526. function TranslateHelperGeometry() {
  527. const geometry = new BufferGeometry();
  528. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  529. return geometry;
  530. }
  531. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  532. const gizmoTranslate = {
  533. X: [
  534. [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ],
  535. [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ],
  536. [ new Line( lineGeometry, matLineRed ) ]
  537. ],
  538. Y: [
  539. [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ],
  540. [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ],
  541. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]]
  542. ],
  543. Z: [
  544. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ],
  545. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ],
  546. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]]
  547. ],
  548. XYZ: [
  549. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]]
  550. ],
  551. XY: [
  552. [ new Mesh( new PlaneGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]],
  553. [ new Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]],
  554. [ new Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  555. ],
  556. YZ: [
  557. [ new Mesh( new PlaneGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  558. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  559. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  560. ],
  561. XZ: [
  562. [ new Mesh( new PlaneGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  563. [ new Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]],
  564. [ new Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  565. ]
  566. };
  567. const pickerTranslate = {
  568. X: [
  569. [ new Mesh( new CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  570. ],
  571. Y: [
  572. [ new Mesh( new CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]]
  573. ],
  574. Z: [
  575. [ new Mesh( new CylinderGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]]
  576. ],
  577. XYZ: [
  578. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  579. ],
  580. XY: [
  581. [ new Mesh( new PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]]
  582. ],
  583. YZ: [
  584. [ new Mesh( new PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]]
  585. ],
  586. XZ: [
  587. [ new Mesh( new PlaneGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]]
  588. ]
  589. };
  590. const helperTranslate = {
  591. START: [
  592. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  593. ],
  594. END: [
  595. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  596. ],
  597. DELTA: [
  598. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  599. ],
  600. X: [
  601. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  602. ],
  603. Y: [
  604. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  605. ],
  606. Z: [
  607. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  608. ]
  609. };
  610. const gizmoRotate = {
  611. X: [
  612. [ new Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
  613. [ new Mesh( new OctahedronGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]],
  614. ],
  615. Y: [
  616. [ new Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]],
  617. [ new Mesh( new OctahedronGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]],
  618. ],
  619. Z: [
  620. [ new Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]],
  621. [ new Mesh( new OctahedronGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]],
  622. ],
  623. E: [
  624. [ new Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]],
  625. [ 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 ]],
  626. [ 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 ]],
  627. [ 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 ]],
  628. [ new Mesh( new CylinderGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]],
  629. ],
  630. XYZE: [
  631. [ new Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]]
  632. ]
  633. };
  634. const helperRotate = {
  635. AXIS: [
  636. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  637. ]
  638. };
  639. const pickerRotate = {
  640. X: [
  641. [ new Mesh( new TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  642. ],
  643. Y: [
  644. [ new Mesh( new TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  645. ],
  646. Z: [
  647. [ new Mesh( new TorusGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  648. ],
  649. E: [
  650. [ new Mesh( new TorusGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]
  651. ],
  652. XYZE: [
  653. [ new Mesh( new SphereGeometry( 0.7, 10, 8 ), matInvisible ) ]
  654. ]
  655. };
  656. const gizmoScale = {
  657. X: [
  658. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  659. [ new Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]]
  660. ],
  661. Y: [
  662. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]],
  663. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]]
  664. ],
  665. Z: [
  666. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]],
  667. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]]
  668. ],
  669. XY: [
  670. [ new Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]],
  671. [ new Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]],
  672. [ new Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  673. ],
  674. YZ: [
  675. [ new Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]],
  676. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  677. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  678. ],
  679. XZ: [
  680. [ new Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]],
  681. [ new Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]],
  682. [ new Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  683. ],
  684. XYZX: [
  685. [ new Mesh( new BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]],
  686. ],
  687. XYZY: [
  688. [ new Mesh( new BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]],
  689. ],
  690. XYZZ: [
  691. [ new Mesh( new BoxGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]],
  692. ]
  693. };
  694. const pickerScale = {
  695. X: [
  696. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  697. ],
  698. Y: [
  699. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]]
  700. ],
  701. Z: [
  702. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]]
  703. ],
  704. XY: [
  705. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]],
  706. ],
  707. YZ: [
  708. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]],
  709. ],
  710. XZ: [
  711. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]],
  712. ],
  713. XYZX: [
  714. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]],
  715. ],
  716. XYZY: [
  717. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]],
  718. ],
  719. XYZZ: [
  720. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]],
  721. ]
  722. };
  723. const helperScale = {
  724. X: [
  725. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  726. ],
  727. Y: [
  728. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  729. ],
  730. Z: [
  731. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  732. ]
  733. };
  734. // Creates an Object3D with gizmos described in custom hierarchy definition.
  735. function setupGizmo( gizmoMap ) {
  736. const gizmo = new Object3D();
  737. for ( const name in gizmoMap ) {
  738. for ( let i = gizmoMap[ name ].length; i --; ) {
  739. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  740. const position = gizmoMap[ name ][ i ][ 1 ];
  741. const rotation = gizmoMap[ name ][ i ][ 2 ];
  742. const scale = gizmoMap[ name ][ i ][ 3 ];
  743. const tag = gizmoMap[ name ][ i ][ 4 ];
  744. // name and tag properties are essential for picking and updating logic.
  745. object.name = name;
  746. object.tag = tag;
  747. if ( position ) {
  748. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  749. }
  750. if ( rotation ) {
  751. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  752. }
  753. if ( scale ) {
  754. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  755. }
  756. object.updateMatrix();
  757. const tempGeometry = object.geometry.clone();
  758. tempGeometry.applyMatrix4( object.matrix );
  759. object.geometry = tempGeometry;
  760. object.renderOrder = Infinity;
  761. object.position.set( 0, 0, 0 );
  762. object.rotation.set( 0, 0, 0 );
  763. object.scale.set( 1, 1, 1 );
  764. gizmo.add( object );
  765. }
  766. }
  767. return gizmo;
  768. }
  769. // Gizmo creation
  770. this.gizmo = {};
  771. this.picker = {};
  772. this.helper = {};
  773. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  774. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  775. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  776. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  777. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  778. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  779. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  780. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  781. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  782. // Pickers should be hidden always
  783. this.picker[ 'translate' ].visible = false;
  784. this.picker[ 'rotate' ].visible = false;
  785. this.picker[ 'scale' ].visible = false;
  786. }
  787. // updateMatrixWorld will update transformations and appearance of individual handles
  788. updateMatrixWorld( force ) {
  789. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  790. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  791. // Show only gizmos for current transform mode
  792. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  793. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  794. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  795. this.helper[ 'translate' ].visible = this.mode === 'translate';
  796. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  797. this.helper[ 'scale' ].visible = this.mode === 'scale';
  798. let handles = [];
  799. handles = handles.concat( this.picker[ this.mode ].children );
  800. handles = handles.concat( this.gizmo[ this.mode ].children );
  801. handles = handles.concat( this.helper[ this.mode ].children );
  802. for ( let i = 0; i < handles.length; i ++ ) {
  803. const handle = handles[ i ];
  804. // hide aligned to camera
  805. handle.visible = true;
  806. handle.rotation.set( 0, 0, 0 );
  807. handle.position.copy( this.worldPosition );
  808. let factor;
  809. if ( this.camera.isOrthographicCamera ) {
  810. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  811. } else {
  812. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  813. }
  814. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 7 );
  815. // TODO: simplify helpers and consider decoupling from gizmo
  816. if ( handle.tag === 'helper' ) {
  817. handle.visible = false;
  818. if ( handle.name === 'AXIS' ) {
  819. handle.position.copy( this.worldPositionStart );
  820. handle.visible = !! this.axis;
  821. if ( this.axis === 'X' ) {
  822. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  823. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  824. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  825. handle.visible = false;
  826. }
  827. }
  828. if ( this.axis === 'Y' ) {
  829. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  830. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  831. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  832. handle.visible = false;
  833. }
  834. }
  835. if ( this.axis === 'Z' ) {
  836. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  837. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  838. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  839. handle.visible = false;
  840. }
  841. }
  842. if ( this.axis === 'XYZE' ) {
  843. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  844. _alignVector.copy( this.rotationAxis );
  845. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  846. handle.quaternion.multiply( _tempQuaternion );
  847. handle.visible = this.dragging;
  848. }
  849. if ( this.axis === 'E' ) {
  850. handle.visible = false;
  851. }
  852. } else if ( handle.name === 'START' ) {
  853. handle.position.copy( this.worldPositionStart );
  854. handle.visible = this.dragging;
  855. } else if ( handle.name === 'END' ) {
  856. handle.position.copy( this.worldPosition );
  857. handle.visible = this.dragging;
  858. } else if ( handle.name === 'DELTA' ) {
  859. handle.position.copy( this.worldPositionStart );
  860. handle.quaternion.copy( this.worldQuaternionStart );
  861. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  862. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  863. handle.scale.copy( _tempVector );
  864. handle.visible = this.dragging;
  865. } else {
  866. handle.quaternion.copy( quaternion );
  867. if ( this.dragging ) {
  868. handle.position.copy( this.worldPositionStart );
  869. } else {
  870. handle.position.copy( this.worldPosition );
  871. }
  872. if ( this.axis ) {
  873. handle.visible = this.axis.search( handle.name ) !== - 1;
  874. }
  875. }
  876. // If updating helper, skip rest of the loop
  877. continue;
  878. }
  879. // Align handles to current local or world rotation
  880. handle.quaternion.copy( quaternion );
  881. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  882. // Hide translate and scale axis facing the camera
  883. const AXIS_HIDE_TRESHOLD = 0.99;
  884. const PLANE_HIDE_TRESHOLD = 0.2;
  885. const AXIS_FLIP_TRESHOLD = 0.0;
  886. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  887. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  888. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  889. handle.visible = false;
  890. }
  891. }
  892. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  893. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  894. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  895. handle.visible = false;
  896. }
  897. }
  898. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  899. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  900. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  901. handle.visible = false;
  902. }
  903. }
  904. if ( handle.name === 'XY' ) {
  905. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  906. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  907. handle.visible = false;
  908. }
  909. }
  910. if ( handle.name === 'YZ' ) {
  911. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  912. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  913. handle.visible = false;
  914. }
  915. }
  916. if ( handle.name === 'XZ' ) {
  917. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  918. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  919. handle.visible = false;
  920. }
  921. }
  922. // Flip translate and scale axis ocluded behind another axis
  923. if ( handle.name.search( 'X' ) !== - 1 ) {
  924. if ( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  925. if ( handle.tag === 'fwd' ) {
  926. handle.visible = false;
  927. } else {
  928. handle.scale.x *= - 1;
  929. }
  930. } else if ( handle.tag === 'bwd' ) {
  931. handle.visible = false;
  932. }
  933. }
  934. if ( handle.name.search( 'Y' ) !== - 1 ) {
  935. if ( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  936. if ( handle.tag === 'fwd' ) {
  937. handle.visible = false;
  938. } else {
  939. handle.scale.y *= - 1;
  940. }
  941. } else if ( handle.tag === 'bwd' ) {
  942. handle.visible = false;
  943. }
  944. }
  945. if ( handle.name.search( 'Z' ) !== - 1 ) {
  946. if ( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  947. if ( handle.tag === 'fwd' ) {
  948. handle.visible = false;
  949. } else {
  950. handle.scale.z *= - 1;
  951. }
  952. } else if ( handle.tag === 'bwd' ) {
  953. handle.visible = false;
  954. }
  955. }
  956. } else if ( this.mode === 'rotate' ) {
  957. // Align handles to current local or world rotation
  958. _tempQuaternion2.copy( quaternion );
  959. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  960. if ( handle.name.search( 'E' ) !== - 1 ) {
  961. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  962. }
  963. if ( handle.name === 'X' ) {
  964. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  965. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  966. handle.quaternion.copy( _tempQuaternion );
  967. }
  968. if ( handle.name === 'Y' ) {
  969. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  970. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  971. handle.quaternion.copy( _tempQuaternion );
  972. }
  973. if ( handle.name === 'Z' ) {
  974. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  975. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  976. handle.quaternion.copy( _tempQuaternion );
  977. }
  978. }
  979. // Hide disabled axes
  980. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  981. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  982. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  983. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  984. // highlight selected axis
  985. handle.material._color = handle.material._color || handle.material.color.clone();
  986. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  987. handle.material.color.copy( handle.material._color );
  988. handle.material.opacity = handle.material._opacity;
  989. if ( this.enabled && this.axis ) {
  990. if ( handle.name === this.axis ) {
  991. handle.material.color.setHex( 0xffff00 );
  992. handle.material.opacity = 1.0;
  993. } else if ( this.axis.split( '' ).some( function ( a ) {
  994. return handle.name === a;
  995. } ) ) {
  996. handle.material.color.setHex( 0xffff00 );
  997. handle.material.opacity = 1.0;
  998. }
  999. }
  1000. }
  1001. super.updateMatrixWorld( force );
  1002. }
  1003. }
  1004. TransformControlsGizmo.prototype.isTransformControlsGizmo = true;
  1005. //
  1006. class TransformControlsPlane extends Mesh {
  1007. constructor() {
  1008. super(
  1009. new PlaneGeometry( 100000, 100000, 2, 2 ),
  1010. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  1011. );
  1012. this.type = 'TransformControlsPlane';
  1013. }
  1014. updateMatrixWorld( force ) {
  1015. let space = this.space;
  1016. this.position.copy( this.worldPosition );
  1017. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1018. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1019. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1020. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1021. // Align the plane for current transform mode, axis and space.
  1022. _alignVector.copy( _v2 );
  1023. switch ( this.mode ) {
  1024. case 'translate':
  1025. case 'scale':
  1026. switch ( this.axis ) {
  1027. case 'X':
  1028. _alignVector.copy( this.eye ).cross( _v1 );
  1029. _dirVector.copy( _v1 ).cross( _alignVector );
  1030. break;
  1031. case 'Y':
  1032. _alignVector.copy( this.eye ).cross( _v2 );
  1033. _dirVector.copy( _v2 ).cross( _alignVector );
  1034. break;
  1035. case 'Z':
  1036. _alignVector.copy( this.eye ).cross( _v3 );
  1037. _dirVector.copy( _v3 ).cross( _alignVector );
  1038. break;
  1039. case 'XY':
  1040. _dirVector.copy( _v3 );
  1041. break;
  1042. case 'YZ':
  1043. _dirVector.copy( _v1 );
  1044. break;
  1045. case 'XZ':
  1046. _alignVector.copy( _v3 );
  1047. _dirVector.copy( _v2 );
  1048. break;
  1049. case 'XYZ':
  1050. case 'E':
  1051. _dirVector.set( 0, 0, 0 );
  1052. break;
  1053. }
  1054. break;
  1055. case 'rotate':
  1056. default:
  1057. // special case for rotate
  1058. _dirVector.set( 0, 0, 0 );
  1059. }
  1060. if ( _dirVector.length() === 0 ) {
  1061. // If in rotate mode, make the plane parallel to camera
  1062. this.quaternion.copy( this.cameraQuaternion );
  1063. } else {
  1064. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  1065. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1066. }
  1067. super.updateMatrixWorld( force );
  1068. }
  1069. }
  1070. TransformControlsPlane.prototype.isTransformControlsPlane = true;
  1071. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };