TransformControls.js 44 KB

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