TransformControls.js 42 KB

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