TransformControls.js 43 KB

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