TransformControls.js 43 KB

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