TransformControls.js 44 KB

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