TransformControls.js 43 KB

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