TransformControls.js 43 KB

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