TransformControls.js 44 KB

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