2
0

TransformControls.js 43 KB

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