TransformControls.js 43 KB

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