TransformControls.js 43 KB

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