TransformControls.js 43 KB

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