TransformControls.js 43 KB

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