TransformControls.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. !function(){
  2. var BoxBufferGeometry,
  3. BufferGeometry,
  4. Color,
  5. CylinderBufferGeometry,
  6. DoubleSide,
  7. Euler,
  8. Float32BufferAttribute ,
  9. Line ,
  10. LineBasicMaterial ,
  11. Matrix4 ,
  12. Mesh ,
  13. MeshBasicMaterial ,
  14. Object3D ,
  15. OctahedronBufferGeometry ,
  16. PlaneBufferGeometry ,
  17. Quaternion ,
  18. Raycaster ,
  19. SphereBufferGeometry ,
  20. TorusBufferGeometry ,
  21. Vector2,
  22. Vector3 ;
  23. var TransformControls = function ( camera, domElement, options ) {
  24. if ( domElement === undefined ) {
  25. console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' );
  26. domElement = document;
  27. }
  28. Object3D.call( this );
  29. this.visible = false;
  30. this.domElement = domElement;
  31. var _gizmo = new TransformControlsGizmo(options);
  32. this.add( _gizmo );
  33. var _plane = new TransformControlsPlane(options);
  34. this.add( _plane );
  35. /* this.linesAssistance = new TransformControlsLines(options); //水平 垂直 辅助
  36. this.add( this.linesAssistance ); */
  37. var scope = this;
  38. this.player = options.player;//xzw add
  39. this.options = options//xzw add
  40. // Define properties with getters/setter
  41. // Setting the defined property will automatically trigger change event
  42. // Defined properties are passed down to gizmo and plane
  43. defineProperty( "camera", camera );
  44. defineProperty( "object", undefined );
  45. defineProperty( "enabled", true );
  46. defineProperty( "axis", null );
  47. defineProperty( "mode", "translate" );
  48. defineProperty( "translationSnap", null );
  49. defineProperty( "rotationSnap", null );
  50. defineProperty( "scaleSnap", null );
  51. defineProperty( "space", "world" );
  52. defineProperty( "size", 1 );
  53. defineProperty( "dragging", false );
  54. defineProperty( "showX", true );
  55. defineProperty( "showY", true );
  56. defineProperty( "showZ", true );
  57. var changeEvent = { type: "change" };
  58. var mouseDownEvent = { type: "mouseDown" };
  59. var mouseUpEvent = { type: "mouseUp", mode: scope.mode };
  60. var objectChangeEvent = { type: "objectChange" };
  61. // Reusable utility variables
  62. var ray = new Raycaster();
  63. var _tempVector = new Vector3();
  64. var _tempVector2 = new Vector3();
  65. var _tempQuaternion = new Quaternion();
  66. var _unit = {
  67. X: new Vector3( 1, 0, 0 ),
  68. Y: new Vector3( 0, 1, 0 ),
  69. Z: new Vector3( 0, 0, 1 )
  70. };
  71. var pointStart = new Vector3();
  72. var pointEnd = new Vector3();
  73. var offset = new Vector3();
  74. var rotationAxis = new Vector3();
  75. var startNorm = new Vector3();
  76. var endNorm = new Vector3();
  77. var rotationAngle = 0;
  78. var cameraPosition = new Vector3();
  79. var cameraQuaternion = new Quaternion();
  80. var cameraScale = new Vector3();
  81. var parentPosition = new Vector3();
  82. var parentQuaternion = new Quaternion();
  83. var parentQuaternionInv = new Quaternion();
  84. var parentScale = new Vector3();
  85. var worldPositionStart = new Vector3();
  86. var worldQuaternionStart = new Quaternion();
  87. var worldScaleStart = new Vector3();
  88. var worldPosition = new Vector3();
  89. var worldQuaternion = new Quaternion();
  90. var worldQuaternionInv = new Quaternion();
  91. var worldScale = new Vector3();
  92. var eye = new Vector3();
  93. var positionStart = new Vector3();
  94. var quaternionStart = new Quaternion();
  95. var scaleStart = new Vector3();
  96. // TODO: remove properties unused in plane and gizmo
  97. defineProperty( "worldPosition", worldPosition );
  98. defineProperty( "worldPositionStart", worldPositionStart );
  99. defineProperty( "worldQuaternion", worldQuaternion );
  100. defineProperty( "worldQuaternionStart", worldQuaternionStart );
  101. defineProperty( "cameraPosition", cameraPosition );
  102. defineProperty( "cameraQuaternion", cameraQuaternion );
  103. defineProperty( "pointStart", pointStart );
  104. defineProperty( "pointEnd", pointEnd );
  105. defineProperty( "rotationAxis", rotationAxis );
  106. defineProperty( "rotationAngle", rotationAngle );
  107. defineProperty( "eye", eye );
  108. {
  109. domElement.addEventListener( "mousedown", onPointerDown, false );
  110. domElement.addEventListener( "touchstart", onPointerDown, false );
  111. domElement.addEventListener( "mousemove", onPointerHover, false );
  112. domElement.addEventListener( "touchmove", onPointerHover, false );
  113. domElement.addEventListener( "touchmove", onPointerMove, false );
  114. document.addEventListener( "mouseup", onPointerUp, false );
  115. domElement.addEventListener( "touchend", onPointerUp, false );
  116. domElement.addEventListener( "touchcancel", onPointerUp, false );
  117. domElement.addEventListener( "touchleave", onPointerUp, false );
  118. }
  119. this.dispose = function () {
  120. domElement.removeEventListener( "mousedown", onPointerDown );
  121. domElement.removeEventListener( "touchstart", onPointerDown );
  122. domElement.removeEventListener( "mousemove", onPointerHover );
  123. domElement.removeEventListener( "mousemove", onPointerMove );
  124. domElement.removeEventListener( "touchmove", onPointerHover );
  125. domElement.removeEventListener( "touchmove", onPointerMove );
  126. document.removeEventListener( "mouseup", onPointerUp );
  127. domElement.removeEventListener( "touchend", onPointerUp );
  128. domElement.removeEventListener( "touchcancel", onPointerUp );
  129. domElement.removeEventListener( "touchleave", onPointerUp );
  130. this.traverse( function ( child ) {
  131. if ( child.geometry ) child.geometry.dispose();
  132. if ( child.material ) child.material.dispose();
  133. } );
  134. };
  135. // Set current object
  136. this.attach = function ( object ) {
  137. this.object = object;
  138. this.visible = true;
  139. //Config.keyCon = false;//add
  140. //this.linesAssistance.setVisible(true)
  141. return this;
  142. };
  143. // Detatch from object
  144. this.detach = function () {
  145. this.object = undefined;
  146. this.visible = false;
  147. this.axis = null;
  148. //Config.keyCon = true;//add
  149. //this.linesAssistance.setVisible(false)
  150. return this;
  151. };
  152. // Defined getter, setter and store for a property
  153. function defineProperty( propName, defaultValue ) {
  154. var propValue = defaultValue;
  155. Object.defineProperty( scope, propName, {
  156. get: function () {
  157. return propValue !== undefined ? propValue : defaultValue;
  158. },
  159. set: function ( value ) {
  160. if ( propValue !== value ) {
  161. propValue = value;
  162. _plane[ propName ] = value;
  163. _gizmo[ propName ] = value;
  164. scope.dispatchEvent( { type: propName + "-changed", value: value } );
  165. scope.dispatchEvent( changeEvent );
  166. }
  167. }
  168. } );
  169. scope[ propName ] = defaultValue;
  170. _plane[ propName ] = defaultValue;
  171. _gizmo[ propName ] = defaultValue;
  172. }
  173. // updateMatrixWorld updates key transformation variables
  174. this.updateMatrixWorld = function () {
  175. if ( this.object !== undefined ) {
  176. this.object.updateMatrixWorld();
  177. this.object.parent.matrixWorld.decompose( parentPosition, parentQuaternion, parentScale );
  178. this.object.matrixWorld.decompose( worldPosition, worldQuaternion, worldScale );
  179. parentQuaternionInv.copy( parentQuaternion ).inverse();
  180. worldQuaternionInv.copy( worldQuaternion ).inverse();
  181. }
  182. this.camera.updateMatrixWorld();
  183. this.camera.matrixWorld.decompose( cameraPosition, cameraQuaternion, cameraScale );
  184. eye.copy( cameraPosition ).sub( worldPosition ).normalize();
  185. Object3D.prototype.updateMatrixWorld.call( this );
  186. };
  187. this.pointerHover = function ( pointer ) {
  188. if ( this.object === undefined || this.dragging === true || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  189. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  190. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  191. ray.set(origin, player.getMouseDirection(pointer));
  192. var intersect = ray.intersectObjects( _gizmo.picker[ this.mode ].children, true )[ 0 ] || false;
  193. if ( intersect ) {
  194. this.axis = intersect.object.name;
  195. } else {
  196. this.axis = null;
  197. }
  198. };
  199. this.pointerDown = function ( pointer ) {
  200. if ( this.object === undefined || this.dragging === true || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  201. if ( ( pointer.button === 0 || pointer.button === undefined ) && this.axis !== null ) {
  202. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  203. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  204. ray.set(origin, player.getMouseDirection(pointer));
  205. var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false;
  206. if ( planeIntersect ) {
  207. var space = this.space;
  208. if ( this.mode === 'scale' ) {
  209. space = 'local';
  210. } else if ( this.axis === 'E' || this.axis === 'XYZE' || this.axis === 'XYZ' ) {
  211. space = 'world';
  212. }
  213. if ( space === 'local' && this.mode === 'rotate' ) {
  214. var snap = this.rotationSnap;
  215. if ( this.axis === 'X' && snap ) this.object.rotation.x = Math.round( this.object.rotation.x / snap ) * snap;
  216. if ( this.axis === 'Y' && snap ) this.object.rotation.y = Math.round( this.object.rotation.y / snap ) * snap;
  217. if ( this.axis === 'Z' && snap ) this.object.rotation.z = Math.round( this.object.rotation.z / snap ) * snap;
  218. }
  219. this.object.updateMatrixWorld();
  220. this.object.parent.updateMatrixWorld();
  221. positionStart.copy( this.object.position );
  222. quaternionStart.copy( this.object.quaternion );
  223. scaleStart.copy( this.object.scale );
  224. this.object.matrixWorld.decompose( worldPositionStart, worldQuaternionStart, worldScaleStart );
  225. pointStart.copy( planeIntersect.point ).sub( worldPositionStart );
  226. if(this.player.cameraControls.activeControl){
  227. //this.player.cameraControls.activeControl.locked = true; //add
  228. this.player.cameraControls.activeControl.enabled = false; //add
  229. }
  230. }
  231. this.dragging = true;
  232. mouseDownEvent.mode = this.mode;
  233. this.dispatchEvent( mouseDownEvent );
  234. }
  235. };
  236. this.pointerMove = function ( pointer ) {
  237. var axis = this.axis;
  238. var mode = this.mode;
  239. var object = this.object;
  240. var space = this.space;
  241. if ( mode === 'scale' ) {
  242. space = 'local';
  243. } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
  244. space = 'world';
  245. }
  246. if ( object === undefined || axis === null || this.dragging === false || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  247. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  248. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  249. ray.set(origin, player.getMouseDirection(pointer));
  250. var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false;
  251. if ( planeIntersect === false ) return;
  252. pointEnd.copy( planeIntersect.point ).sub( worldPositionStart );
  253. if ( mode === 'translate' ) {
  254. // Apply translate
  255. offset.copy( pointEnd ).sub( pointStart );
  256. if ( space === 'local' && axis !== 'XYZ' ) {
  257. offset.applyQuaternion( worldQuaternionInv );
  258. }
  259. if ( axis.indexOf( 'X' ) === - 1 ) offset.x = 0;
  260. if ( axis.indexOf( 'Y' ) === - 1 ) offset.y = 0;
  261. if ( axis.indexOf( 'Z' ) === - 1 ) offset.z = 0;
  262. if ( space === 'local' && axis !== 'XYZ' ) {
  263. offset.applyQuaternion( quaternionStart ).divide( parentScale );
  264. } else {
  265. offset.applyQuaternion( parentQuaternionInv ).divide( parentScale );
  266. }
  267. object.position.copy( offset ).add( positionStart );
  268. // Apply translation snap
  269. if ( this.translationSnap ) {
  270. if ( space === 'local' ) {
  271. object.position.applyQuaternion( _tempQuaternion.copy( quaternionStart ).inverse() );
  272. if ( axis.search( 'X' ) !== - 1 ) {
  273. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  274. }
  275. if ( axis.search( 'Y' ) !== - 1 ) {
  276. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  277. }
  278. if ( axis.search( 'Z' ) !== - 1 ) {
  279. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  280. }
  281. object.position.applyQuaternion( quaternionStart );
  282. }
  283. if ( space === 'world' ) {
  284. if ( object.parent ) {
  285. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  286. }
  287. if ( axis.search( 'X' ) !== - 1 ) {
  288. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  289. }
  290. if ( axis.search( 'Y' ) !== - 1 ) {
  291. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  292. }
  293. if ( axis.search( 'Z' ) !== - 1 ) {
  294. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  295. }
  296. if ( object.parent ) {
  297. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  298. }
  299. }
  300. }
  301. //add:
  302. if(editTool.hotpoint.editSpot){
  303. editTool.hotpoint.updateTransform('position');
  304. }
  305. //this.linesAssistance.updateTransform(this.object)
  306. } else if ( mode === 'scale' ) {
  307. if ( axis.search( 'XYZ' ) !== - 1 ) {
  308. var d = pointEnd.length() / pointStart.length();
  309. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  310. if(options.NoScaleZ){//xzw add
  311. _tempVector2.set( d, d, 1 );
  312. }else{
  313. _tempVector2.set( d, d, d );
  314. }
  315. }else if ( axis.search( 'XY' ) !== - 1 ) { //add 等比例for plane
  316. var d = pointEnd.length() / pointStart.length();
  317. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  318. _tempVector2.set( d, d, 1 );
  319. }else {
  320. _tempVector.copy( pointStart );
  321. _tempVector2.copy( pointEnd );
  322. _tempVector.applyQuaternion( worldQuaternionInv );
  323. _tempVector2.applyQuaternion( worldQuaternionInv );
  324. _tempVector2.divide( _tempVector );
  325. if ( axis.search( 'X' ) === - 1 ) {
  326. _tempVector2.x = 1;
  327. }
  328. if ( axis.search( 'Y' ) === - 1 ) {
  329. _tempVector2.y = 1;
  330. }
  331. if ( axis.search( 'Z' ) === - 1 ) {
  332. _tempVector2.z = 1;
  333. }
  334. }
  335. // Apply scale
  336. object.scale.copy( scaleStart ).multiply( _tempVector2 );
  337. if ( this.scaleSnap ) {
  338. if ( axis.search( 'X' ) !== - 1 ) {
  339. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  340. }
  341. if ( axis.search( 'Y' ) !== - 1 ) {
  342. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  343. }
  344. if ( axis.search( 'Z' ) !== - 1 ) {
  345. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  346. }
  347. }
  348. //add:
  349. /* if(EditOverlay.editing){
  350. EditOverlay.updateScale();
  351. } */
  352. if(editTool.hotpoint.editSpot){
  353. editTool.hotpoint.updateTransform('scale');
  354. }
  355. } else if ( mode === 'rotate' ) {
  356. offset.copy( pointEnd ).sub( pointStart );
  357. var ROTATION_SPEED = 2 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  358. if ( axis === 'E' ) {
  359. rotationAxis.copy( eye );
  360. rotationAngle = pointEnd.angleTo( pointStart );
  361. startNorm.copy( pointStart ).normalize();
  362. endNorm.copy( pointEnd ).normalize();
  363. rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 );
  364. } else if ( axis === 'XYZE' ) {
  365. rotationAxis.copy( offset ).cross( eye ).normalize();
  366. rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  367. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  368. rotationAxis.copy( _unit[ axis ] );
  369. _tempVector.copy( _unit[ axis ] );
  370. if ( space === 'local' ) {
  371. _tempVector.applyQuaternion( worldQuaternion );
  372. }
  373. rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
  374. }
  375. // Apply rotation snap
  376. if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap;
  377. this.rotationAngle = rotationAngle;
  378. // Apply rotate
  379. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  380. object.quaternion.copy( quaternionStart );
  381. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize();
  382. } else {
  383. rotationAxis.applyQuaternion( parentQuaternionInv );
  384. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) );
  385. object.quaternion.multiply( quaternionStart ).normalize();
  386. }
  387. //add:
  388. /*
  389. if(EditOverlay.editing){
  390. EditOverlay.updateQua();
  391. } */
  392. if(editTool.hotpoint.editSpot){
  393. editTool.hotpoint.updateTransform('quaternion');
  394. }
  395. }
  396. this.dispatchEvent( changeEvent );
  397. this.dispatchEvent( objectChangeEvent );
  398. };
  399. this.pointerUp = function ( pointer ) {
  400. //if ( pointer.button !== undefined && pointer.button !== 0 ) return;
  401. if ( this.dragging && ( this.axis !== null ) ) {
  402. mouseUpEvent.mode = this.mode;
  403. this.dispatchEvent( mouseUpEvent );
  404. if(this.player.cameraControls.activeControl){
  405. //this.player.cameraControls.activeControl.locked = false; //add
  406. this.player.cameraControls.activeControl.pointerDragOn = false //add
  407. this.player.cameraControls.activeControl.enabled = true
  408. }
  409. }
  410. this.dragging = false;
  411. if ( pointer.button === undefined ) this.axis = null;
  412. /* if(this.player.cameraControls.activeControl){
  413. //this.player.cameraControls.activeControl.locked = false; //add
  414. this.player.cameraControls.activeControl.pointerDragOn = false //add
  415. this.player.cameraControls.activeControl.enabled = true
  416. } */
  417. };
  418. // normalize mouse / touch pointer and remap {x,y} to view space.
  419. function getPointer( event ) {
  420. if(!event){
  421. console.log('hhahhhahah')
  422. return;
  423. }
  424. if ( document.pointerLockElement ) {
  425. return {
  426. x: 0,
  427. y: 0,
  428. button: event.button
  429. };
  430. } else {
  431. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  432. var rect = domElement.getBoundingClientRect();
  433. return {
  434. x: ( pointer.clientX - rect.left ) / rect.width * 2 - 1,
  435. y: - ( pointer.clientY - rect.top ) / rect.height * 2 + 1,
  436. button: event.button
  437. };
  438. }
  439. }
  440. // mouse / touch event handlers
  441. function onPointerHover( event ) {
  442. if ( ! scope.enabled ) return;
  443. //scope.pointerHover( getPointer( event ) );
  444. scope.pointerHover( scope.player.mouse );
  445. }
  446. function onPointerDown( event ) {
  447. if ( ! scope.enabled ) return;
  448. //document.addEventListener( "mousemove", onPointerMove, false );
  449. /* scope.pointerHover( getPointer( event ) );
  450. scope.pointerDown( getPointer( event ) ); */
  451. scope.pointerHover( scope.player.mouse );
  452. scope.pointerDown( scope.player.mouse );
  453. }
  454. this.onPointerDown = onPointerDown;
  455. function onPointerMove( event ) {
  456. if ( ! scope.enabled || !this.dragging) return; //xzw change
  457. //scope.pointerMove( getPointer( event ) );
  458. scope.pointerMove( scope.player.mouse );
  459. }
  460. this.onPointerMove = onPointerMove;
  461. function onPointerUp( event ) {
  462. if ( ! scope.enabled ) return;
  463. //document.removeEventListener( "mousemove", onPointerMove, false );
  464. //scope.pointerUp( getPointer( event ) );
  465. scope.pointerUp( scope.player.mouse );
  466. }
  467. this.onPointerUp = onPointerUp;
  468. // TODO: deprecate
  469. this.getMode = function () {
  470. return scope.mode;
  471. };
  472. this.setMode = function ( mode ) {
  473. scope.mode = mode;
  474. };
  475. this.setTranslationSnap = function ( translationSnap ) {
  476. scope.translationSnap = translationSnap;
  477. };
  478. this.setRotationSnap = function ( rotationSnap ) {
  479. scope.rotationSnap = rotationSnap;
  480. };
  481. this.setScaleSnap = function ( scaleSnap ) {
  482. scope.scaleSnap = scaleSnap;
  483. };
  484. this.setSize = function ( size ) {
  485. scope.size = size;
  486. };
  487. this.setSpace = function ( space ) {
  488. scope.space = space;
  489. };
  490. this.update = function () {
  491. console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' );
  492. };
  493. };
  494. var TransformControlsGizmo = function (options) {
  495. 'use strict';
  496. Object3D.call( this );
  497. this.type = 'TransformControlsGizmo';
  498. // shared materials
  499. var gizmoMaterial = new MeshBasicMaterial( {
  500. depthTest: false,
  501. depthWrite: false,
  502. transparent: true,
  503. side: DoubleSide,
  504. fog: false
  505. } );
  506. var gizmoLineMaterial = new LineBasicMaterial( {
  507. depthTest: false,
  508. depthWrite: false,
  509. transparent: true,
  510. linewidth: 1,
  511. fog: false
  512. } );
  513. // Make unique material for each axis/color
  514. var matInvisible = gizmoMaterial.clone();
  515. matInvisible.opacity = 0.15;
  516. var matHelper = gizmoMaterial.clone();
  517. matHelper.opacity = 0.33;
  518. var matRed = gizmoMaterial.clone();
  519. matRed.color.set( 0xff0000 );
  520. var matGreen = gizmoMaterial.clone();
  521. matGreen.color.set( 0x00ff00 );
  522. var matBlue = gizmoMaterial.clone();
  523. matBlue.color.set( 0x0000ff );
  524. var matWhiteTransparent = gizmoMaterial.clone();
  525. matWhiteTransparent.opacity = 0.35;
  526. matWhiteTransparent.color.set( 0x00d0fd );//xzw add
  527. var matYellowTransparent = matWhiteTransparent.clone();
  528. matYellowTransparent.color.set( 0xffff00 );
  529. var matCyanTransparent = matWhiteTransparent.clone();
  530. matCyanTransparent.color.set( 0x00ffff );
  531. var matMagentaTransparent = matWhiteTransparent.clone();
  532. matMagentaTransparent.color.set( 0xff00ff );
  533. var matYellow = gizmoMaterial.clone();
  534. matYellow.color.set( 0xffff00 );
  535. var matLineRed = gizmoLineMaterial.clone();
  536. matLineRed.color.set( 0xff0000 );
  537. var matLineGreen = gizmoLineMaterial.clone();
  538. matLineGreen.color.set( 0x00ff00 );
  539. var matLineBlue = gizmoLineMaterial.clone();
  540. matLineBlue.color.set( 0x0000ff );
  541. var matLineCyan = gizmoLineMaterial.clone();
  542. matLineCyan.color.set( 0x00ffff );
  543. var matLineMagenta = gizmoLineMaterial.clone();
  544. matLineMagenta.color.set( 0xff00ff );
  545. var matLineYellow = gizmoLineMaterial.clone();
  546. matLineYellow.color.set( 0xffff00 );
  547. var matLineGray = gizmoLineMaterial.clone();
  548. matLineGray.color.set( 0x787878 );
  549. var matLineYellowTransparent = matLineYellow.clone();
  550. matLineYellowTransparent.opacity = 0.25;
  551. // reusable geometry
  552. var arrowGeometry = new CylinderBufferGeometry( 0, 0.05, 0.2, 12, 1, false );
  553. var scaleHandleGeometry = new BoxBufferGeometry( 0.125, 0.125, 0.125 );
  554. var lineGeometry = new BufferGeometry( );
  555. //lineGeometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  556. lineGeometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 0, 0]), 3 ))
  557. var CircleGeometry = function ( radius, arc ) {
  558. var geometry = new BufferGeometry( );
  559. var vertices = [];
  560. for ( var i = 0; i <= 64 * arc; ++ i ) {
  561. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  562. }
  563. //geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  564. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertices), 3 ))
  565. return geometry;
  566. };
  567. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  568. var TranslateHelperGeometry = function () {
  569. var geometry = new BufferGeometry();
  570. //geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  571. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 1, 1 ]), 3 ))
  572. return geometry;
  573. };
  574. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  575. var gizmoTranslate = {
  576. X: [
  577. [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ],
  578. //[ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ],
  579. [ new Line( lineGeometry, matLineRed ) ]
  580. ],
  581. Y: [
  582. [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ],
  583. //[ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ],
  584. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]]
  585. ],
  586. Z: [
  587. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ],
  588. //[ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ],
  589. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]]
  590. ],
  591. /* XYZ: [
  592. [ new Mesh( new OctahedronBufferGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]]
  593. ], */
  594. XY: [
  595. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]],
  596. [ new Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]],
  597. [ new Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  598. ],
  599. YZ: [
  600. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  601. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  602. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  603. ],
  604. XZ: [
  605. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  606. [ new Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]],
  607. [ new Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  608. ]
  609. };
  610. var pickerTranslate = {
  611. X: [
  612. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  613. ],
  614. Y: [
  615. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]]
  616. ],
  617. Z: [
  618. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]]
  619. ],
  620. /* XYZ: [
  621. [ new Mesh( new OctahedronBufferGeometry( 0.2, 0 ), matInvisible ) ]
  622. ], */
  623. XY: [
  624. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]]
  625. ],
  626. YZ: [
  627. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]]
  628. ],
  629. XZ: [
  630. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]]
  631. ]
  632. };
  633. var helperTranslate = {
  634. START: [
  635. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  636. ],
  637. END: [
  638. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  639. ],
  640. DELTA: [
  641. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  642. ],
  643. X: [
  644. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  645. ],
  646. Y: [
  647. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  648. ],
  649. Z: [
  650. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  651. ]
  652. };
  653. var gizmoRotate = {
  654. X: [
  655. [ new Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
  656. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]],
  657. ],
  658. Y: [
  659. [ new Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]],
  660. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]],
  661. ],
  662. Z: [
  663. [ new Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]],
  664. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]],
  665. ],
  666. E: [
  667. [ new Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]],
  668. [ 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 ]],
  669. [ 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 ]],
  670. [ 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 ]],
  671. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]],
  672. ],
  673. XYZE: [
  674. [ new Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]]
  675. ]
  676. };
  677. var helperRotate = {
  678. AXIS: [
  679. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  680. ]
  681. };
  682. var pickerRotate = {
  683. X: [
  684. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  685. ],
  686. Y: [
  687. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  688. ],
  689. Z: [
  690. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  691. ],
  692. E: [
  693. [ new Mesh( new TorusBufferGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]
  694. ],
  695. XYZE: [
  696. [ new Mesh( new SphereBufferGeometry( 0.7, 10, 8 ), matInvisible ) ]
  697. ]
  698. };
  699. var gizmoScale = {
  700. X: [
  701. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  702. [ new Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]]
  703. ],
  704. Y: [
  705. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]],
  706. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]]
  707. ],
  708. Z: [
  709. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]],
  710. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]]
  711. ],
  712. XY: [
  713. [ new Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]],
  714. [ new Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]],
  715. [ new Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  716. ],
  717. /* YZ: [
  718. [ new Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]],
  719. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  720. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  721. ],
  722. XZ: [
  723. [ new Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]],
  724. [ new Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]],
  725. [ new Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  726. ],
  727. XYZX: [
  728. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]],
  729. ],*/
  730. XYZY: [
  731. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]],
  732. ]/* ,
  733. XYZZ: [
  734. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]],
  735. ] */
  736. };
  737. var pickerScale = {
  738. X: [
  739. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  740. ],
  741. Y: [
  742. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]]
  743. ],
  744. Z: [
  745. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]]
  746. ],
  747. XY: [
  748. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]],
  749. ],
  750. /*YZ: [
  751. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]],
  752. ],
  753. XZ: [
  754. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]],
  755. ],
  756. XYZX: [
  757. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]],
  758. ],*/
  759. XYZY: [
  760. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]],
  761. ]/* ,
  762. XYZZ: [
  763. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]],
  764. ] */
  765. };
  766. var helperScale = {
  767. X: [
  768. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  769. ],
  770. Y: [
  771. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  772. ],
  773. Z: [
  774. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  775. ]
  776. };
  777. // Creates an Object3D with gizmos described in custom hierarchy definition.
  778. var setupGizmo = function ( gizmoMap ) {
  779. var gizmo = new Object3D();
  780. for ( var name in gizmoMap ) {
  781. for ( var i = gizmoMap[ name ].length; i --; ) {
  782. var object = gizmoMap[ name ][ i ][ 0 ].clone();
  783. var position = gizmoMap[ name ][ i ][ 1 ];
  784. var rotation = gizmoMap[ name ][ i ][ 2 ];
  785. var scale = gizmoMap[ name ][ i ][ 3 ];
  786. var tag = gizmoMap[ name ][ i ][ 4 ];
  787. // name and tag properties are essential for picking and updating logic.
  788. object.name = name;
  789. object.tag = tag;
  790. if ( position ) {
  791. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  792. }
  793. if ( rotation ) {
  794. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  795. }
  796. if ( scale ) {
  797. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  798. }
  799. object.updateMatrix();
  800. var tempGeometry = object.geometry.clone();
  801. tempGeometry.applyMatrix( object.matrix );
  802. object.geometry = tempGeometry;
  803. object.renderOrder = Infinity;
  804. object.position.set( 0, 0, 0 );
  805. object.rotation.set( 0, 0, 0 );
  806. object.scale.set( 1, 1, 1 );
  807. gizmo.add( object );
  808. }
  809. }
  810. return gizmo;
  811. };
  812. // Reusable utility variables
  813. var tempVector = new Vector3( 0, 0, 0 );
  814. var tempEuler = new Euler();
  815. var alignVector = new Vector3( 0, 1, 0 );
  816. var zeroVector = new Vector3( 0, 0, 0 );
  817. var lookAtMatrix = new Matrix4();
  818. var tempQuaternion = new Quaternion();
  819. var tempQuaternion2 = new Quaternion();
  820. var identityQuaternion = new Quaternion();
  821. var unitX = new Vector3( 1, 0, 0 );
  822. var unitY = new Vector3( 0, 1, 0 );
  823. var unitZ = new Vector3( 0, 0, 1 );
  824. // Gizmo creation
  825. this.gizmo = {};
  826. this.picker = {};
  827. this.helper = {};
  828. this.add( this.gizmo[ "translate" ] = setupGizmo( gizmoTranslate ) );
  829. this.add( this.gizmo[ "rotate" ] = setupGizmo( gizmoRotate ) );
  830. this.add( this.gizmo[ "scale" ] = setupGizmo( gizmoScale ) );
  831. this.add( this.picker[ "translate" ] = setupGizmo( pickerTranslate ) );
  832. this.add( this.picker[ "rotate" ] = setupGizmo( pickerRotate ) );
  833. this.add( this.picker[ "scale" ] = setupGizmo( pickerScale ) );
  834. this.add( this.helper[ "translate" ] = setupGizmo( helperTranslate ) );
  835. this.add( this.helper[ "rotate" ] = setupGizmo( helperRotate ) );
  836. this.add( this.helper[ "scale" ] = setupGizmo( helperScale ) );
  837. // Pickers should be hidden always
  838. this.picker[ "translate" ].visible = false;
  839. this.picker[ "rotate" ].visible = false;
  840. this.picker[ "scale" ].visible = false;
  841. // updateMatrixWorld will update transformations and appearance of individual handles
  842. this.updateMatrixWorld = function () {
  843. var space = this.space;
  844. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  845. var quaternion = space === "local" ? this.worldQuaternion : identityQuaternion;
  846. // Show only gizmos for current transform mode
  847. this.gizmo[ "translate" ].visible = this.mode === "translate";
  848. this.gizmo[ "rotate" ].visible = this.mode === "rotate";
  849. this.gizmo[ "scale" ].visible = this.mode === "scale";
  850. this.helper[ "translate" ].visible = this.mode === "translate";
  851. this.helper[ "rotate" ].visible = this.mode === "rotate";
  852. this.helper[ "scale" ].visible = this.mode === "scale";
  853. var handles = [];
  854. handles = handles.concat( this.picker[ this.mode ].children );
  855. handles = handles.concat( this.gizmo[ this.mode ].children );
  856. handles = handles.concat( this.helper[ this.mode ].children );
  857. for ( var i = 0; i < handles.length; i ++ ) {
  858. var handle = handles[ i ];
  859. // hide aligned to camera
  860. handle.visible = true;
  861. handle.rotation.set( 0, 0, 0 );
  862. handle.position.copy( this.worldPosition );
  863. var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition );
  864. //俯视图的透视和距离无关,因此在两种相机切换切换时大小过渡比较困难
  865. if(player.mode == "transitioning" && (player.modeTran.split('-')[0] == "floorplan" || player.modeTran.split('-')[1] == "floorplan")){
  866. eyeDistance = Math.min(eyeDistance, 6.5)//从一开始很小的距离过渡到俯视图50的高度,会变得很大。
  867. }else if(player.mode == "floorplan"){
  868. var flcamera = player.cameraControls.cameras.floorplan;
  869. eyeDistance = (flcamera.right - flcamera.left ) / flcamera.aspect ;
  870. }
  871. handle.scale.set( 1, 1, 1 ).multiplyScalar( eyeDistance * this.size / 7 );
  872. // TODO: simplify helpers and consider decoupling from gizmo
  873. if ( handle.tag === 'helper' ) {
  874. handle.visible = false;
  875. if ( handle.name === 'AXIS' ) {
  876. handle.position.copy( this.worldPositionStart );
  877. handle.visible = !! this.axis;
  878. if ( this.axis === 'X' ) {
  879. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  880. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  881. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  882. handle.visible = false;
  883. }
  884. }
  885. if ( this.axis === 'Y' ) {
  886. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  887. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  888. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  889. handle.visible = false;
  890. }
  891. }
  892. if ( this.axis === 'Z' ) {
  893. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  894. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  895. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  896. handle.visible = false;
  897. }
  898. }
  899. if ( this.axis === 'XYZE' ) {
  900. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  901. alignVector.copy( this.rotationAxis );
  902. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  903. handle.quaternion.multiply( tempQuaternion );
  904. handle.visible = this.dragging;
  905. }
  906. if ( this.axis === 'E' ) {
  907. handle.visible = false;
  908. }
  909. } else if ( handle.name === 'START' ) {
  910. handle.position.copy( this.worldPositionStart );
  911. handle.visible = this.dragging;
  912. } else if ( handle.name === 'END' ) {
  913. handle.position.copy( this.worldPosition );
  914. handle.visible = this.dragging;
  915. } else if ( handle.name === 'DELTA' ) {
  916. handle.position.copy( this.worldPositionStart );
  917. handle.quaternion.copy( this.worldQuaternionStart );
  918. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  919. tempVector.applyQuaternion( this.worldQuaternionStart.clone().inverse() );
  920. handle.scale.copy( tempVector );
  921. handle.visible = this.dragging;
  922. } else {
  923. handle.quaternion.copy( quaternion );
  924. if ( this.dragging ) {
  925. handle.position.copy( this.worldPositionStart );
  926. } else {
  927. handle.position.copy( this.worldPosition );
  928. }
  929. if ( this.axis ) {
  930. handle.visible = this.axis.search( handle.name ) !== - 1;
  931. }
  932. }
  933. // If updating helper, skip rest of the loop
  934. continue;
  935. }
  936. // Align handles to current local or world rotation
  937. handle.quaternion.copy( quaternion );
  938. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  939. // Hide translate and scale axis facing the camera
  940. var AXIS_HIDE_TRESHOLD = 0.99;
  941. var PLANE_HIDE_TRESHOLD = 0.2;
  942. var AXIS_FLIP_TRESHOLD = 0.0;
  943. if(options.dontHideWhenFaceCamera){//xzw add
  944. //正对镜头时不隐藏箭头
  945. }else{
  946. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  947. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  948. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  949. handle.visible = false;
  950. }
  951. }
  952. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  953. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  954. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  955. handle.visible = false;
  956. }
  957. }
  958. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  959. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  960. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  961. handle.visible = false;
  962. }
  963. }
  964. if ( handle.name === 'XY' ) {
  965. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  966. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  967. handle.visible = false;
  968. }
  969. }
  970. if ( handle.name === 'YZ' ) {
  971. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  972. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  973. handle.visible = false;
  974. }
  975. }
  976. if ( handle.name === 'XZ' ) {
  977. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  978. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  979. handle.visible = false;
  980. }
  981. }
  982. }
  983. // Flip translate and scale axis ocluded behind another axis
  984. //xzw 改 去掉反向箭头
  985. if ( handle.name.search( 'X' ) !== - 1 ) {
  986. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  987. /* if ( handle.tag === 'fwd' ) {
  988. handle.visible = false;
  989. } else {
  990. handle.scale.x *= - 1;
  991. }
  992. } else if ( handle.tag === 'bwd' ) {
  993. handle.visible = false;*/
  994. handle.scale.x *= - 1;
  995. }
  996. }
  997. if ( handle.name.search( 'Y' ) !== - 1 ) {
  998. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  999. /* if ( handle.tag === 'fwd' ) {
  1000. handle.visible = false;
  1001. } else {
  1002. handle.scale.y *= - 1;
  1003. }
  1004. } else if ( handle.tag === 'bwd' ) {
  1005. handle.visible = false;
  1006. */
  1007. handle.scale.y *= - 1;
  1008. }
  1009. }
  1010. if ( handle.name.search( 'Z' ) !== - 1 ) {
  1011. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1012. /* if ( handle.tag === 'fwd' ) {
  1013. handle.visible = false;
  1014. } else {
  1015. handle.scale.z *= - 1;
  1016. }
  1017. } else if ( handle.tag === 'bwd' ) {
  1018. handle.visible = false; */
  1019. handle.scale.z *= - 1;
  1020. }
  1021. }
  1022. } else if ( this.mode === 'rotate' ) {
  1023. // Align handles to current local or world rotation
  1024. tempQuaternion2.copy( quaternion );
  1025. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).inverse() );
  1026. if ( handle.name.search( "E" ) !== - 1 ) {
  1027. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  1028. }
  1029. if ( handle.name === 'X' ) {
  1030. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) );
  1031. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1032. handle.quaternion.copy( tempQuaternion );
  1033. }
  1034. if ( handle.name === 'Y' ) {
  1035. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  1036. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1037. handle.quaternion.copy( tempQuaternion );
  1038. }
  1039. if ( handle.name === 'Z' ) {
  1040. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  1041. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1042. handle.quaternion.copy( tempQuaternion );
  1043. }
  1044. }
  1045. // Hide disabled axes
  1046. handle.visible = handle.visible && ( handle.name.indexOf( "X" ) === - 1 || this.showX );
  1047. handle.visible = handle.visible && ( handle.name.indexOf( "Y" ) === - 1 || this.showY );
  1048. handle.visible = handle.visible && ( handle.name.indexOf( "Z" ) === - 1 || this.showZ );
  1049. handle.visible = handle.visible && ( handle.name.indexOf( "E" ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1050. // highlight selected axis
  1051. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1052. handle.material._color = handle.material._color || handle.material.color.clone();
  1053. handle.material.color.copy( handle.material._color );
  1054. handle.material.opacity = handle.material._opacity;
  1055. if ( ! this.enabled ) {
  1056. handle.material.opacity *= 0.5;
  1057. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1058. } else if ( this.axis ) {
  1059. if ( handle.name === this.axis ) {
  1060. handle.material.opacity = 1.0;
  1061. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1062. } else if ( this.axis.split( '' ).some( function ( a ) {
  1063. return handle.name === a;
  1064. } ) ) {
  1065. handle.material.opacity = 1.0;
  1066. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1067. } else {
  1068. handle.material.opacity *= 0.25;
  1069. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1070. }
  1071. }
  1072. }
  1073. Object3D.prototype.updateMatrixWorld.call( this );
  1074. };
  1075. };
  1076. /* var TransformControlsLines = function (options) {
  1077. Object3D.call( this)
  1078. let label1 = new Label2D({ innerHTML:`<div>与相机水平距离</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1079. let label2 = new Label2D({ innerHTML:`<div>与相机高度差</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1080. let css = {'opacity': 0.7, 'color': '#07fceb', transform:'translate(-50%, -50%)'}
  1081. label1.elem.css(css);
  1082. label2.elem.css(css)
  1083. //for(let i=0;i<2;i++){
  1084. // let line = LineDraw.createLine([new THREE.Vector3, new THREE.Vector3],{color:'#07fceb', opacity:0.7 })
  1085. // this.add(line)
  1086. //}
  1087. this.updateTransform = function(object){
  1088. object = object || this.parent.object;
  1089. let A = player.position.clone(); //当前相机位置
  1090. let B = object.position //物体位置
  1091. let C = B.clone().setY(A.y); //物体在相机的高度的位置
  1092. let D = A.clone().setY(B.y); //相机在物体的高度的位置
  1093. //LineDraw.moveLine(this.children[0], [B, C]) // 垂直线
  1094. //LineDraw.moveLine(this.children[1], [D, B]) // 水平线
  1095. let dis1 = toPrecision(D.distanceTo(B),1)
  1096. let dis2 = C.y - B.y
  1097. label1.elem.text('与相机水平距离: '+dis1+'米')
  1098. label2.elem.text('在相机之'+ (dis2>0?'下' : '上') +' :'+ toPrecision(dis2 , 1) +'米')
  1099. label1.setPos(new THREE.Vector3().addVectors(D,B).multiplyScalar(0.5))
  1100. label2.setPos(new THREE.Vector3().addVectors(B,C).multiplyScalar(0.5))
  1101. }
  1102. this.setVisible = (v,reason)=>{
  1103. label1.setVisible(v, reason || 'unvisi')
  1104. label2.setVisible(v, reason || 'unvisi')
  1105. this.visible = label1.visible
  1106. if(this.visible){
  1107. this.updateTransform()
  1108. }
  1109. }
  1110. this.setVisible(false)
  1111. player.on("mode.changing",(currentMode, mode, pano, duration)=>{
  1112. if(mode != 'panorama'){
  1113. this.setVisible(false, 'isPanorama')
  1114. }else{
  1115. this.setVisible(true, 'isPanorama')
  1116. }
  1117. })
  1118. player.on("flying.started",( )=>{
  1119. this.setVisible(false, 'flying')
  1120. })
  1121. player.on("flying.ended",( )=>{
  1122. this.setVisible(true, 'flying')
  1123. })
  1124. }
  1125. */
  1126. var TransformControlsPlane = function (options) {
  1127. 'use strict';
  1128. Mesh.call( this,
  1129. new PlaneBufferGeometry( 100000, 100000, 2, 2 ),
  1130. new MeshBasicMaterial( { color: "#ff0000", visible: false, wireframe: false, side: DoubleSide, transparent: true, opacity: 0.2 } )
  1131. );
  1132. this.type = 'TransformControlsPlane';
  1133. var unitX = new Vector3( 1, 0, 0 );
  1134. var unitY = new Vector3( 0, 1, 0 );
  1135. var unitZ = new Vector3( 0, 0, 1 );
  1136. var tempVector = new Vector3();
  1137. var dirVector = new Vector3();
  1138. var alignVector = new Vector3();
  1139. var tempMatrix = new Matrix4();
  1140. var identityQuaternion = new Quaternion();
  1141. this.updateMatrixWorld = function () {
  1142. var space = this.space;
  1143. this.position.copy( this.worldPosition );
  1144. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1145. unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1146. unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1147. unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1148. // Align the plane for current transform mode, axis and space.
  1149. alignVector.copy( unitY );
  1150. switch ( this.mode ) {
  1151. case 'translate':
  1152. case 'scale':
  1153. switch ( this.axis ) {
  1154. case 'X':
  1155. alignVector.copy( this.eye ).cross( unitX );
  1156. dirVector.copy( unitX ).cross( alignVector );
  1157. break;
  1158. case 'Y':
  1159. alignVector.copy( this.eye ).cross( unitY );
  1160. dirVector.copy( unitY ).cross( alignVector );
  1161. break;
  1162. case 'Z':
  1163. alignVector.copy( this.eye ).cross( unitZ );
  1164. dirVector.copy( unitZ ).cross( alignVector );
  1165. break;
  1166. case 'XY':
  1167. dirVector.copy( unitZ );
  1168. break;
  1169. case 'YZ':
  1170. dirVector.copy( unitX );
  1171. break;
  1172. case 'XZ':
  1173. alignVector.copy( unitZ );
  1174. dirVector.copy( unitY );
  1175. break;
  1176. case 'XYZ':
  1177. case 'E':
  1178. default: //xzw add for scale xyzz
  1179. dirVector.set( 0, 0, 0 );
  1180. break;
  1181. }
  1182. break;
  1183. case 'rotate':
  1184. default:
  1185. // special case for rotate
  1186. dirVector.set( 0, 0, 0 );
  1187. }
  1188. if ( dirVector.length() === 0 ) {
  1189. // If in rotate mode, make the plane parallel to camera
  1190. this.quaternion.copy( this.cameraQuaternion );
  1191. } else {
  1192. tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector );
  1193. this.quaternion.setFromRotationMatrix( tempMatrix );
  1194. }
  1195. Object3D.prototype.updateMatrixWorld.call( this );
  1196. };
  1197. };
  1198. //export { TransformControls, TransformControlsGizmo, TransformControlsPlane };
  1199. TransformControls.init = function(THREE){
  1200. BoxBufferGeometry = THREE.BoxBufferGeometry,
  1201. BufferGeometry = THREE.BufferGeometry,
  1202. Color = THREE.Color,
  1203. CylinderBufferGeometry = THREE.CylinderBufferGeometry,
  1204. DoubleSide = THREE.DoubleSide,
  1205. Euler = THREE.Euler,
  1206. Float32BufferAttribute = THREE.Float32BufferAttribute,
  1207. Line = THREE.Line,
  1208. LineBasicMaterial = THREE.LineBasicMaterial,
  1209. Matrix4 = THREE.Matrix4,
  1210. Mesh = THREE.Mesh,
  1211. MeshBasicMaterial = THREE.MeshBasicMaterial,
  1212. Object3D = THREE.Object3D,
  1213. //OctahedronBufferGeometry = THREE.OctahedronBufferGeometry,
  1214. PlaneBufferGeometry = THREE.PlaneBufferGeometry,
  1215. Quaternion = THREE.Quaternion,
  1216. Raycaster = THREE.Raycaster,
  1217. SphereBufferGeometry = THREE.SphereBufferGeometry,
  1218. TorusBufferGeometry = THREE.TorusBufferGeometry,
  1219. Vector3 = THREE.Vector3;
  1220. Vector2 = THREE.Vector2;
  1221. TransformControls.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1222. constructor: TransformControls,
  1223. isTransformControls: true
  1224. } );
  1225. TransformControlsGizmo.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1226. constructor: TransformControlsGizmo,
  1227. isTransformControlsGizmo: true
  1228. } );
  1229. TransformControlsPlane.prototype = Object.assign( Object.create( Mesh.prototype ), {
  1230. constructor: TransformControlsPlane,
  1231. isTransformControlsPlane: true
  1232. } );
  1233. /* TransformControlsLines.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1234. constructor: TransformControlsLines,
  1235. isTransformControlsLines: true
  1236. } ); */
  1237. //----------
  1238. function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
  1239. BufferGeometry.call( this );
  1240. this.type = 'PolyhedronBufferGeometry';
  1241. this.parameters = {
  1242. vertices: vertices,
  1243. indices: indices,
  1244. radius: radius,
  1245. detail: detail
  1246. };
  1247. radius = radius || 1;
  1248. detail = detail || 0;
  1249. // default buffer data
  1250. var vertexBuffer = [];
  1251. var uvBuffer = [];
  1252. // the subdivision creates the vertex buffer data
  1253. subdivide( detail );
  1254. // all vertices should lie on a conceptual sphere with a given radius
  1255. appplyRadius( radius );
  1256. // finally, create the uv data
  1257. generateUVs();
  1258. // build non-indexed geometry
  1259. this.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertexBuffer), 3 ))
  1260. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array(vertexBuffer), 3 ) );
  1261. this.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array(uvBuffer), 2 ) );
  1262. if ( detail === 0 ) {
  1263. this.computeVertexNormals(); // flat normals
  1264. } else {
  1265. this.normalizeNormals(); // smooth normals
  1266. }
  1267. // helper functions
  1268. function subdivide( detail ) {
  1269. var a = new Vector3();
  1270. var b = new Vector3();
  1271. var c = new Vector3();
  1272. // iterate over all faces and apply a subdivison with the given detail value
  1273. for ( var i = 0; i < indices.length; i += 3 ) {
  1274. // get the vertices of the face
  1275. getVertexByIndex( indices[ i + 0 ], a );
  1276. getVertexByIndex( indices[ i + 1 ], b );
  1277. getVertexByIndex( indices[ i + 2 ], c );
  1278. // perform subdivision
  1279. subdivideFace( a, b, c, detail );
  1280. }
  1281. }
  1282. function subdivideFace( a, b, c, detail ) {
  1283. var cols = Math.pow( 2, detail );
  1284. // we use this multidimensional array as a data structure for creating the subdivision
  1285. var v = [];
  1286. var i, j;
  1287. // construct all of the vertices for this subdivision
  1288. for ( i = 0; i <= cols; i ++ ) {
  1289. v[ i ] = [];
  1290. var aj = a.clone().lerp( c, i / cols );
  1291. var bj = b.clone().lerp( c, i / cols );
  1292. var rows = cols - i;
  1293. for ( j = 0; j <= rows; j ++ ) {
  1294. if ( j === 0 && i === cols ) {
  1295. v[ i ][ j ] = aj;
  1296. } else {
  1297. v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
  1298. }
  1299. }
  1300. }
  1301. // construct all of the faces
  1302. for ( i = 0; i < cols; i ++ ) {
  1303. for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
  1304. var k = Math.floor( j / 2 );
  1305. if ( j % 2 === 0 ) {
  1306. pushVertex( v[ i ][ k + 1 ] );
  1307. pushVertex( v[ i + 1 ][ k ] );
  1308. pushVertex( v[ i ][ k ] );
  1309. } else {
  1310. pushVertex( v[ i ][ k + 1 ] );
  1311. pushVertex( v[ i + 1 ][ k + 1 ] );
  1312. pushVertex( v[ i + 1 ][ k ] );
  1313. }
  1314. }
  1315. }
  1316. }
  1317. function appplyRadius( radius ) {
  1318. var vertex = new Vector3();
  1319. // iterate over the entire buffer and apply the radius to each vertex
  1320. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1321. vertex.x = vertexBuffer[ i + 0 ];
  1322. vertex.y = vertexBuffer[ i + 1 ];
  1323. vertex.z = vertexBuffer[ i + 2 ];
  1324. vertex.normalize().multiplyScalar( radius );
  1325. vertexBuffer[ i + 0 ] = vertex.x;
  1326. vertexBuffer[ i + 1 ] = vertex.y;
  1327. vertexBuffer[ i + 2 ] = vertex.z;
  1328. }
  1329. }
  1330. function generateUVs() {
  1331. var vertex = new Vector3();
  1332. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1333. vertex.x = vertexBuffer[ i + 0 ];
  1334. vertex.y = vertexBuffer[ i + 1 ];
  1335. vertex.z = vertexBuffer[ i + 2 ];
  1336. var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
  1337. var v = inclination( vertex ) / Math.PI + 0.5;
  1338. uvBuffer.push( u, 1 - v );
  1339. }
  1340. correctUVs();
  1341. correctSeam();
  1342. }
  1343. function correctSeam() {
  1344. // handle case when face straddles the seam, see #3269
  1345. for ( var i = 0; i < uvBuffer.length; i += 6 ) {
  1346. // uv data of a single face
  1347. var x0 = uvBuffer[ i + 0 ];
  1348. var x1 = uvBuffer[ i + 2 ];
  1349. var x2 = uvBuffer[ i + 4 ];
  1350. var max = Math.max( x0, x1, x2 );
  1351. var min = Math.min( x0, x1, x2 );
  1352. // 0.9 is somewhat arbitrary
  1353. if ( max > 0.9 && min < 0.1 ) {
  1354. if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
  1355. if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
  1356. if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
  1357. }
  1358. }
  1359. }
  1360. function pushVertex( vertex ) {
  1361. vertexBuffer.push( vertex.x, vertex.y, vertex.z );
  1362. }
  1363. function getVertexByIndex( index, vertex ) {
  1364. var stride = index * 3;
  1365. vertex.x = vertices[ stride + 0 ];
  1366. vertex.y = vertices[ stride + 1 ];
  1367. vertex.z = vertices[ stride + 2 ];
  1368. }
  1369. function correctUVs() {
  1370. var a = new Vector3();
  1371. var b = new Vector3();
  1372. var c = new Vector3();
  1373. var centroid = new Vector3();
  1374. var uvA = new Vector2();
  1375. var uvB = new Vector2();
  1376. var uvC = new Vector2();
  1377. for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
  1378. a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
  1379. b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
  1380. c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
  1381. uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
  1382. uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
  1383. uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
  1384. centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
  1385. var azi = azimuth( centroid );
  1386. correctUV( uvA, j + 0, a, azi );
  1387. correctUV( uvB, j + 2, b, azi );
  1388. correctUV( uvC, j + 4, c, azi );
  1389. }
  1390. }
  1391. function correctUV( uv, stride, vector, azimuth ) {
  1392. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
  1393. uvBuffer[ stride ] = uv.x - 1;
  1394. }
  1395. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
  1396. uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
  1397. }
  1398. }
  1399. // Angle around the Y axis, counter-clockwise when looking from above.
  1400. function azimuth( vector ) {
  1401. return Math.atan2( vector.z, - vector.x );
  1402. }
  1403. // Angle above the XZ plane.
  1404. function inclination( vector ) {
  1405. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  1406. }
  1407. }
  1408. PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  1409. PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
  1410. OctahedronBufferGeometry = function( radius, detail ) {
  1411. var vertices = [
  1412. 1, 0, 0, - 1, 0, 0, 0, 1, 0,
  1413. 0, - 1, 0, 0, 0, 1, 0, 0, - 1
  1414. ];
  1415. var indices = [
  1416. 0, 2, 4, 0, 4, 3, 0, 3, 5,
  1417. 0, 5, 2, 1, 2, 5, 1, 5, 3,
  1418. 1, 3, 4, 1, 4, 2
  1419. ];
  1420. PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
  1421. this.type = 'OctahedronBufferGeometry';
  1422. this.parameters = {
  1423. radius: radius,
  1424. detail: detail
  1425. };
  1426. }
  1427. OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
  1428. OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
  1429. }
  1430. window.TransformControls = TransformControls;
  1431. }()