TransformControls.js 43 KB

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