TransformControls.js 43 KB

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