TransformControls.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 matLineYellow = gizmoLineMaterial.clone();
  439. matLineYellow.color.set( 0xffff00 );
  440. var matLineGray = gizmoLineMaterial.clone();
  441. matLineGray.color.set( 0x787878);
  442. var matLineYellowTransparent = matLineYellow.clone();
  443. matLineYellowTransparent.opacity = 0.25;
  444. // reusable geometry
  445. var arrowGeometry = new THREE.CylinderBufferGeometry( 0, 0.05, 0.2, 12, 1, false);
  446. var scaleHandleGeometry = new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125);
  447. var lineGeometry = new THREE.BufferGeometry( );
  448. lineGeometry.addAttribute('position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  449. var CircleGeometry = function( radius, arc ) {
  450. var geometry = new THREE.BufferGeometry( );
  451. var vertices = [];
  452. for ( var i = 0; i <= 64 * arc; ++i ) {
  453. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  454. }
  455. geometry.addAttribute('position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  456. return geometry;
  457. };
  458. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  459. var TranslateHelperGeometry = function( radius, arc ) {
  460. var geometry = new THREE.BufferGeometry()
  461. geometry.addAttribute('position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  462. return geometry;
  463. };
  464. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  465. var gizmoTranslate = {
  466. X: [
  467. [ new THREE.Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, -Math.PI / 2 ], null, 'fwd' ],
  468. [ new THREE.Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ],
  469. [ new THREE.Line( lineGeometry, matLineRed ) ]
  470. ],
  471. Y: [
  472. [ new THREE.Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ],
  473. [ new THREE.Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ],
  474. [ new THREE.Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ] ]
  475. ],
  476. Z: [
  477. [ new THREE.Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ],
  478. [ new THREE.Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ -Math.PI / 2, 0, 0 ], null, 'bwd' ],
  479. [ new THREE.Line( lineGeometry, matLineBlue ), null, [ 0, -Math.PI / 2, 0 ] ]
  480. ],
  481. XYZ: [
  482. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.1, 0 ), matWhiteTransperent ), [ 0, 0, 0 ], [ 0, 0, 0 ] ]
  483. ],
  484. XY: [
  485. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.295, 0.295 ), matYellowTransparent ), [ 0.15, 0.15, 0 ] ],
  486. [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ] ],
  487. [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ] ]
  488. ],
  489. YZ: [
  490. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.295, 0.295 ), matCyanTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ] ],
  491. [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ] ],
  492. [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, -Math.PI / 2, 0 ], [ 0.125, 1, 1 ] ]
  493. ],
  494. XZ: [
  495. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.295, 0.295 ), matMagentaTransparent ), [ 0.15, 0, 0.15 ], [ -Math.PI / 2, 0, 0 ] ],
  496. [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ] ],
  497. [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, -Math.PI / 2, 0 ], [ 0.125, 1, 1 ] ]
  498. ]
  499. };
  500. var pickerTranslate = {
  501. X: [
  502. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
  503. ],
  504. Y: [
  505. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ] ]
  506. ],
  507. Z: [
  508. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  509. ],
  510. XYZ: [
  511. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.2, 0 ), matInvisible ) ]
  512. ],
  513. XY: [
  514. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ] ]
  515. ],
  516. YZ: [
  517. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ] ]
  518. ],
  519. XZ: [
  520. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ -Math.PI / 2, 0, 0 ] ]
  521. ]
  522. };
  523. var helperTranslate = {
  524. START: [
  525. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  526. ],
  527. END: [
  528. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  529. ],
  530. DELTA: [
  531. [ new THREE.Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  532. ],
  533. X: [
  534. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ -1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  535. ],
  536. Y: [
  537. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, -1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  538. ],
  539. Z: [
  540. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, -1e3 ], [ 0, -Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  541. ]
  542. };
  543. var gizmoRotate = {
  544. X: [
  545. [ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
  546. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ] ],
  547. ],
  548. Y: [
  549. [ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, -Math.PI / 2 ] ],
  550. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ] ],
  551. ],
  552. Z: [
  553. [ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ] ],
  554. [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ] ],
  555. ],
  556. E: [
  557. [ new THREE.Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ] ],
  558. [ 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 ]],
  559. [ 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 ]],
  560. [ 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 ]],
  561. [ 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 ]],
  562. ],
  563. XYZE: [
  564. [ new THREE.Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ] ]
  565. ]
  566. };
  567. var helperRotate = {
  568. AXIS: [
  569. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ -1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  570. ]
  571. };
  572. var pickerRotate = {
  573. X: [
  574. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, -Math.PI / 2, -Math.PI / 2 ] ],
  575. ],
  576. Y: [
  577. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ] ],
  578. ],
  579. Z: [
  580. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
  581. ],
  582. E: [
  583. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]
  584. ],
  585. XYZE: [
  586. [ new THREE.Mesh( new THREE.SphereBufferGeometry( 0.7, 10, 8 ), matInvisible ) ]
  587. ]
  588. };
  589. var gizmoScale = {
  590. X: [
  591. [ new THREE.Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
  592. [ new THREE.Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ] ]
  593. ],
  594. Y: [
  595. [ new THREE.Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ] ],
  596. [ new THREE.Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ] ]
  597. ],
  598. Z: [
  599. [ new THREE.Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ] ],
  600. [ new THREE.Line( lineGeometry, matLineBlue ), null, [ 0, -Math.PI / 2, 0 ], [ 0.8, 1, 1 ] ]
  601. ],
  602. XY: [
  603. [ new THREE.Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ] ],
  604. [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ] ],
  605. [ new THREE.Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ] ]
  606. ],
  607. YZ: [
  608. [ new THREE.Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ] ],
  609. [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ] ],
  610. [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, -Math.PI / 2, 0 ], [ 0.125, 1, 1 ] ]
  611. ],
  612. XZ: [
  613. [ new THREE.Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ] ],
  614. [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ] ],
  615. [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, -Math.PI / 2, 0 ], [ 0.125, 1, 1 ] ]
  616. ],
  617. XYZX: [
  618. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransperent ), [ 1.1, 0, 0 ] ],
  619. ],
  620. XYZY: [
  621. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransperent ), [ 0, 1.1, 0 ] ],
  622. ],
  623. XYZZ: [
  624. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransperent ), [ 0, 0, 1.1 ] ],
  625. ]
  626. };
  627. var pickerScale = {
  628. X: [
  629. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
  630. ],
  631. Y: [
  632. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ] ]
  633. ],
  634. Z: [
  635. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ]
  636. ],
  637. XY: [
  638. [ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ] ],
  639. ],
  640. YZ: [
  641. [ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ] ],
  642. ],
  643. XZ: [
  644. [ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ] ],
  645. ],
  646. XYZX: [
  647. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ] ],
  648. ],
  649. XYZY: [
  650. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ] ],
  651. ],
  652. XYZZ: [
  653. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ] ],
  654. ]
  655. };
  656. var helperScale = {
  657. X: [
  658. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ -1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  659. ],
  660. Y: [
  661. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, -1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  662. ],
  663. Z: [
  664. [ new THREE.Line( lineGeometry, matHelper.clone() ), [ 0, 0, -1e3 ], [ 0, -Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  665. ]
  666. };
  667. // Creates an Object3D with gizmos described in custom hierarchy definition.
  668. var setupGizmo = function( gizmoMap ) {
  669. var gizmo = new THREE.Object3D();
  670. for ( var name in gizmoMap ) {
  671. for ( var i = gizmoMap[ name ].length; i --; ) {
  672. var object = gizmoMap[ name ][ i ][ 0 ].clone();
  673. var position = gizmoMap[ name ][ i ][ 1 ];
  674. var rotation = gizmoMap[ name ][ i ][ 2 ];
  675. var scale = gizmoMap[ name ][ i ][ 3 ];
  676. var tag = gizmoMap[ name ][ i ][ 4 ];
  677. // name and tag properties are essential for picking and updating logic.
  678. object.name = name;
  679. object.tag = tag;
  680. if (position) {
  681. object.position.set(position[ 0 ], position[ 1 ], position[ 2 ]);
  682. }
  683. if (rotation) {
  684. object.rotation.set(rotation[ 0 ], rotation[ 1 ], rotation[ 2 ]);
  685. }
  686. if (scale) {
  687. object.scale.set(scale[ 0 ], scale[ 1 ], scale[ 2 ]);
  688. }
  689. object.updateMatrix();
  690. var tempGeometry = object.geometry.clone();
  691. tempGeometry.applyMatrix(object.matrix);
  692. object.geometry = tempGeometry;
  693. object.position.set( 0, 0, 0 );
  694. object.rotation.set( 0, 0, 0 );
  695. object.scale.set(1, 1, 1);
  696. gizmo.add(object);
  697. }
  698. }
  699. return gizmo;
  700. };
  701. // Reusable utility variables
  702. var tempVector = new THREE.Vector3( 0, 0, 0 );
  703. var tempEuler = new THREE.Euler();
  704. var alignVector = new THREE.Vector3( 0, 1, 0 );
  705. var zeroVector = new THREE.Vector3( 0, 0, 0 );
  706. var lookAtMatrix = new THREE.Matrix4();
  707. var tempQuaternion = new THREE.Quaternion();
  708. var tempQuaternion2 = new THREE.Quaternion();
  709. var identityQuaternion = new THREE.Quaternion();
  710. var unitX = new THREE.Vector3( 1, 0, 0 );
  711. var unitY = new THREE.Vector3( 0, 1, 0 );
  712. var unitZ = new THREE.Vector3( 0, 0, 1 );
  713. // Gizmo creation
  714. this.gizmo = {};
  715. this.picker = {};
  716. this.helper = {};
  717. this.add( this.gizmo[ "translate" ] = setupGizmo( gizmoTranslate ) );
  718. this.add( this.gizmo[ "rotate" ] = setupGizmo( gizmoRotate ) );
  719. this.add( this.gizmo[ "scale" ] = setupGizmo( gizmoScale ) );
  720. this.add( this.picker[ "translate" ] = setupGizmo( pickerTranslate ) );
  721. this.add( this.picker[ "rotate" ] = setupGizmo( pickerRotate ) );
  722. this.add( this.picker[ "scale" ] = setupGizmo( pickerScale ) );
  723. this.add( this.helper[ "translate" ] = setupGizmo( helperTranslate ) );
  724. this.add( this.helper[ "rotate" ] = setupGizmo( helperRotate ) );
  725. this.add( this.helper[ "scale" ] = setupGizmo( helperScale ) );
  726. // Pickers should be hidden always
  727. this.picker[ "translate" ].visible = false;
  728. this.picker[ "rotate" ].visible = false;
  729. this.picker[ "scale" ].visible = false;
  730. // updateMatrixWorld will update transformations and appearance of individual handles
  731. this.updateMatrixWorld = function () {
  732. var space = this.space;
  733. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  734. var quaternion = space === "local" ? this.worldQuaternion : identityQuaternion;
  735. // Show only gizmos for current transform mode
  736. this.gizmo[ "translate" ].visible = this.mode === "translate";
  737. this.gizmo[ "rotate" ].visible = this.mode === "rotate";
  738. this.gizmo[ "scale" ].visible = this.mode === "scale";
  739. this.helper[ "translate" ].visible = this.mode === "translate";
  740. this.helper[ "rotate" ].visible = this.mode === "rotate";
  741. this.helper[ "scale" ].visible = this.mode === "scale";
  742. var handles = [];
  743. handles = handles.concat( this.picker[ this.mode ].children );
  744. handles = handles.concat( this.gizmo[ this.mode ].children );
  745. handles = handles.concat( this.helper[ this.mode ].children );
  746. for ( var i = 0; i < handles.length; i++ ) {
  747. var handle = handles[i];
  748. // hide aligned to camera
  749. handle.visible = true;
  750. handle.rotation.set( 0, 0, 0 );
  751. handle.position.copy( this.worldPosition );
  752. var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition);
  753. handle.scale.set( 1, 1, 1 ).multiplyScalar( eyeDistance * this.size / 7 );
  754. // TODO: simplify helpers and consider decoupling from gizmo
  755. if ( handle.tag === 'helper' ) {
  756. handle.visible = false;
  757. if ( handle.name === 'AXIS' ) {
  758. handle.position.copy( this.worldPositionStart );
  759. handle.visible = !!this.axis;
  760. if ( this.axis === 'X' ) {
  761. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  762. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  763. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  764. handle.visible = false;
  765. }
  766. }
  767. if ( this.axis === 'Y' ) {
  768. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  769. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  770. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  771. handle.visible = false;
  772. }
  773. }
  774. if ( this.axis === 'Z' ) {
  775. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  776. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  777. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  778. handle.visible = false;
  779. }
  780. }
  781. if ( this.axis === 'XYZE' ) {
  782. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  783. alignVector.copy( this.rotationAxis );
  784. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  785. handle.quaternion.multiply( tempQuaternion );
  786. handle.visible = this.dragging;
  787. }
  788. if ( this.axis === 'E' ) {
  789. handle.visible = false;
  790. }
  791. } else if ( handle.name === 'START' ) {
  792. handle.position.copy( this.worldPositionStart );
  793. handle.visible = this.dragging;
  794. } else if ( handle.name === 'END' ) {
  795. handle.position.copy( this.worldPosition );
  796. handle.visible = this.dragging;
  797. } else if ( handle.name === 'DELTA' ) {
  798. handle.position.copy( this.worldPositionStart );
  799. handle.quaternion.copy( this.worldQuaternionStart );
  800. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( -1 );
  801. tempVector.applyQuaternion( this.worldQuaternionStart.clone().inverse() );
  802. handle.scale.copy( tempVector );
  803. handle.visible = this.dragging;
  804. } else {
  805. handle.quaternion.copy( quaternion );
  806. if ( this.dragging ) {
  807. handle.position.copy( this.worldPositionStart );
  808. } else {
  809. handle.position.copy( this.worldPosition );
  810. }
  811. if ( this.axis ) {
  812. handle.visible = this.axis.search( handle.name ) !== -1;
  813. }
  814. }
  815. // If updating helper, skip rest of the loop
  816. continue;
  817. }
  818. // Align handles to current local or world rotation
  819. handle.quaternion.copy( quaternion );
  820. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  821. // Hide translate and scale axis facing the camera
  822. var AXIS_HIDE_TRESHOLD = 0.99;
  823. var PLANE_HIDE_TRESHOLD = 0.2;
  824. var AXIS_FLIP_TRESHOLD = -0.4;
  825. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  826. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  827. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  828. handle.visible = false;
  829. }
  830. }
  831. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  832. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  833. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  834. handle.visible = false;
  835. }
  836. }
  837. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  838. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  839. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  840. handle.visible = false;
  841. }
  842. }
  843. if ( handle.name === 'XY' ) {
  844. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  845. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  846. handle.visible = false;
  847. }
  848. }
  849. if ( handle.name === 'YZ' ) {
  850. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  851. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  852. handle.visible = false;
  853. }
  854. }
  855. if ( handle.name === 'XZ' ) {
  856. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  857. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  858. handle.visible = false;
  859. }
  860. }
  861. // Flip translate and scale axis ocluded behind another axis
  862. if ( handle.name.search( 'X' ) !== -1 ) {
  863. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  864. if ( handle.tag === 'fwd' ) {
  865. handle.visible = false;
  866. } else {
  867. handle.scale.x *= -1;
  868. }
  869. } else if ( handle.tag === 'bwd' ) {
  870. handle.visible = false;
  871. }
  872. }
  873. if ( handle.name.search( 'Y' ) !== -1 ) {
  874. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  875. if ( handle.tag === 'fwd' ) {
  876. handle.visible = false;
  877. } else {
  878. handle.scale.y *= -1;
  879. }
  880. } else if ( handle.tag === 'bwd' ) {
  881. handle.visible = false;
  882. }
  883. }
  884. if ( handle.name.search( 'Z' ) !== -1 ) {
  885. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  886. if ( handle.tag === 'fwd' ) {
  887. handle.visible = false;
  888. } else {
  889. handle.scale.z *= -1;
  890. }
  891. } else if ( handle.tag === 'bwd' ) {
  892. handle.visible = false;
  893. }
  894. }
  895. } else if ( this.mode === 'rotate' ) {
  896. // Align handles to current local or world rotation
  897. tempQuaternion2.copy( quaternion );
  898. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).inverse() );
  899. if ( handle.name.search( "E" ) !== - 1 ) {
  900. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  901. }
  902. if ( handle.name === 'X' ) {
  903. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( -alignVector.y, alignVector.z ) );
  904. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  905. handle.quaternion.copy( tempQuaternion );
  906. }
  907. if ( handle.name === 'Y' ) {
  908. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  909. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  910. handle.quaternion.copy( tempQuaternion );
  911. }
  912. if ( handle.name === 'Z' ) {
  913. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  914. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  915. handle.quaternion.copy( tempQuaternion );
  916. }
  917. }
  918. // Hide disabled axes
  919. for ( var a in handle.name ) {
  920. handle.visible = handle.visible && ( handle.name.indexOf( "X" ) === -1 || this.showX );
  921. handle.visible = handle.visible && ( handle.name.indexOf( "Y" ) === -1 || this.showY );
  922. handle.visible = handle.visible && ( handle.name.indexOf( "Z" ) === -1 || this.showZ );
  923. handle.visible = handle.visible && ( handle.name.indexOf( "E" ) === -1 || ( this.showX && this.showY && this.showZ ) );
  924. }
  925. // highlight selected axis
  926. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  927. handle.material._color = handle.material._color || handle.material.color.clone();
  928. handle.material.color.copy( handle.material._color );
  929. handle.material.opacity = handle.material._opacity;
  930. if ( this.axis ) {
  931. if ( handle.name === this.axis ) {
  932. handle.material.opacity = 1.0;
  933. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  934. } else if ( this.axis.split('').some( function( a ) { return handle.name === a; } ) ) {
  935. handle.material.opacity = 1.0;
  936. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  937. } else {
  938. handle.material.opacity *= 0.05;
  939. }
  940. }
  941. }
  942. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  943. };
  944. };
  945. THREE.TransformControlsGizmo.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  946. constructor: THREE.TransformControlsGizmo,
  947. isTransformControlsGizmo: true
  948. } );
  949. THREE.TransformControlsPlane = function () {
  950. 'use strict';
  951. THREE.Mesh.call( this,
  952. new THREE.PlaneBufferGeometry( 100000, 100000, 2, 2 ),
  953. new THREE.MeshBasicMaterial( { visible: false, wireframe: true, side: THREE.DoubleSide, transparent: true, opacity: 0.1 } )
  954. );
  955. this.type = 'TransformControlsPlane';
  956. var unitX = new THREE.Vector3( 1, 0, 0 );
  957. var unitY = new THREE.Vector3( 0, 1, 0 );
  958. var unitZ = new THREE.Vector3( 0, 0, 1 );
  959. var tempVector = new THREE.Vector3();
  960. var dirVector = new THREE.Vector3();
  961. var alignVector = new THREE.Vector3();
  962. var tempMatrix = new THREE.Matrix4();
  963. var identityQuaternion = new THREE.Quaternion();
  964. this.updateMatrixWorld = function() {
  965. var space = this.space;
  966. this.position.copy( this.worldPosition );
  967. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  968. unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  969. unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  970. unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  971. // Align the plane for current transform mode, axis and space.
  972. alignVector.copy( unitY );
  973. switch ( this.mode ) {
  974. case 'translate':
  975. case 'scale':
  976. switch ( this.axis ) {
  977. case 'X':
  978. alignVector.copy( this.eye ).cross( unitX );
  979. dirVector.copy( unitX ).cross( alignVector );
  980. break;
  981. case 'Y':
  982. alignVector.copy( this.eye ).cross( unitY );
  983. dirVector.copy( unitY ).cross( alignVector );
  984. break;
  985. case 'Z':
  986. alignVector.copy( this.eye ).cross( unitZ );
  987. dirVector.copy( unitZ ).cross( alignVector );
  988. break;
  989. case 'XY':
  990. dirVector.copy( unitZ );
  991. break;
  992. case 'YZ':
  993. dirVector.copy( unitX );
  994. break;
  995. case 'XZ':
  996. alignVector.copy( unitZ );
  997. dirVector.copy( unitY );
  998. break;
  999. case 'XYZ':
  1000. case 'E':
  1001. dirVector.set( 0, 0, 0 );
  1002. break;
  1003. }
  1004. break;
  1005. case 'rotate':
  1006. default:
  1007. // special case for rotate
  1008. dirVector.set( 0, 0, 0 );
  1009. }
  1010. if ( dirVector.length() === 0 ) {
  1011. // If in rotate mode, make the plane parallel to camera
  1012. this.quaternion.copy( this.cameraQuaternion );
  1013. } else {
  1014. tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector );
  1015. this.quaternion.setFromRotationMatrix( tempMatrix );
  1016. }
  1017. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  1018. };
  1019. };
  1020. THREE.TransformControlsPlane.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  1021. constructor: THREE.TransformControlsPlane,
  1022. isTransformControlsPlane: true
  1023. } );