TransformControls.js 44 KB

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