TransformControls.js 44 KB

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