TransformControls.js 44 KB

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