TransformControls.js 44 KB

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