TransformControls.js 44 KB

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