TransformControls.js 45 KB

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