TransformControls.js 42 KB

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