TransformControls.js 44 KB

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