TransformControls.js 43 KB

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