TransformControls.js 43 KB

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