TransformControls.js 43 KB

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