TransformControls.js 44 KB

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