TransformControls.js 43 KB

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