TransformControls.js 45 KB

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