TransformControls.js 44 KB

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