2
0

TransformControls.js 45 KB

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