TransformControls.js 43 KB

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