TransformControls.js 43 KB

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