TransformControls.js 44 KB

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