TransformControls.js 42 KB

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