TransformControls.js 42 KB

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