TransformControls.js 45 KB

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