GeometryPipeline-d7363877.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './AttributeCompression-f202be44', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './ComponentDatatype-4eeb6d9b', './Transforms-273eeb44', './EncodedCartesian3-491ac596', './GeometryAttribute-9be2d2e5', './IndexDatatype-f228f5fd', './IntersectionTests-ea138127', './Plane-76b84425'], (function (exports, AttributeCompression, Matrix2, RuntimeError, defaultValue, ComponentDatatype, Transforms, EncodedCartesian3, GeometryAttribute, IndexDatatype, IntersectionTests, Plane) { 'use strict';
  26. const scratchCartesian1 = new Matrix2.Cartesian3();
  27. const scratchCartesian2$1 = new Matrix2.Cartesian3();
  28. const scratchCartesian3$1 = new Matrix2.Cartesian3();
  29. /**
  30. * Computes the barycentric coordinates for a point with respect to a triangle.
  31. *
  32. * @function
  33. *
  34. * @param {Cartesian2|Cartesian3} point The point to test.
  35. * @param {Cartesian2|Cartesian3} p0 The first point of the triangle, corresponding to the barycentric x-axis.
  36. * @param {Cartesian2|Cartesian3} p1 The second point of the triangle, corresponding to the barycentric y-axis.
  37. * @param {Cartesian2|Cartesian3} p2 The third point of the triangle, corresponding to the barycentric z-axis.
  38. * @param {Cartesian3} [result] The object onto which to store the result.
  39. * @returns {Cartesian3|undefined} The modified result parameter or a new Cartesian3 instance if one was not provided. If the triangle is degenerate the function will return undefined.
  40. *
  41. * @example
  42. * // Returns Cartesian3.UNIT_X
  43. * const p = new Cesium.Cartesian3(-1.0, 0.0, 0.0);
  44. * const b = Cesium.barycentricCoordinates(p,
  45. * new Cesium.Cartesian3(-1.0, 0.0, 0.0),
  46. * new Cesium.Cartesian3( 1.0, 0.0, 0.0),
  47. * new Cesium.Cartesian3( 0.0, 1.0, 1.0));
  48. */
  49. function barycentricCoordinates(point, p0, p1, p2, result) {
  50. //>>includeStart('debug', pragmas.debug);
  51. RuntimeError.Check.defined("point", point);
  52. RuntimeError.Check.defined("p0", p0);
  53. RuntimeError.Check.defined("p1", p1);
  54. RuntimeError.Check.defined("p2", p2);
  55. //>>includeEnd('debug');
  56. if (!defaultValue.defined(result)) {
  57. result = new Matrix2.Cartesian3();
  58. }
  59. // Implementation based on http://www.blackpawn.com/texts/pointinpoly/default.html.
  60. let v0;
  61. let v1;
  62. let v2;
  63. let dot00;
  64. let dot01;
  65. let dot02;
  66. let dot11;
  67. let dot12;
  68. if (!defaultValue.defined(p0.z)) {
  69. if (Matrix2.Cartesian2.equalsEpsilon(point, p0, ComponentDatatype.CesiumMath.EPSILON14)) {
  70. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_X, result);
  71. }
  72. if (Matrix2.Cartesian2.equalsEpsilon(point, p1, ComponentDatatype.CesiumMath.EPSILON14)) {
  73. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Y, result);
  74. }
  75. if (Matrix2.Cartesian2.equalsEpsilon(point, p2, ComponentDatatype.CesiumMath.EPSILON14)) {
  76. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Z, result);
  77. }
  78. v0 = Matrix2.Cartesian2.subtract(p1, p0, scratchCartesian1);
  79. v1 = Matrix2.Cartesian2.subtract(p2, p0, scratchCartesian2$1);
  80. v2 = Matrix2.Cartesian2.subtract(point, p0, scratchCartesian3$1);
  81. dot00 = Matrix2.Cartesian2.dot(v0, v0);
  82. dot01 = Matrix2.Cartesian2.dot(v0, v1);
  83. dot02 = Matrix2.Cartesian2.dot(v0, v2);
  84. dot11 = Matrix2.Cartesian2.dot(v1, v1);
  85. dot12 = Matrix2.Cartesian2.dot(v1, v2);
  86. } else {
  87. if (Matrix2.Cartesian3.equalsEpsilon(point, p0, ComponentDatatype.CesiumMath.EPSILON14)) {
  88. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_X, result);
  89. }
  90. if (Matrix2.Cartesian3.equalsEpsilon(point, p1, ComponentDatatype.CesiumMath.EPSILON14)) {
  91. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Y, result);
  92. }
  93. if (Matrix2.Cartesian3.equalsEpsilon(point, p2, ComponentDatatype.CesiumMath.EPSILON14)) {
  94. return Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Z, result);
  95. }
  96. v0 = Matrix2.Cartesian3.subtract(p1, p0, scratchCartesian1);
  97. v1 = Matrix2.Cartesian3.subtract(p2, p0, scratchCartesian2$1);
  98. v2 = Matrix2.Cartesian3.subtract(point, p0, scratchCartesian3$1);
  99. dot00 = Matrix2.Cartesian3.dot(v0, v0);
  100. dot01 = Matrix2.Cartesian3.dot(v0, v1);
  101. dot02 = Matrix2.Cartesian3.dot(v0, v2);
  102. dot11 = Matrix2.Cartesian3.dot(v1, v1);
  103. dot12 = Matrix2.Cartesian3.dot(v1, v2);
  104. }
  105. result.y = dot11 * dot02 - dot01 * dot12;
  106. result.z = dot00 * dot12 - dot01 * dot02;
  107. const q = dot00 * dot11 - dot01 * dot01;
  108. // Triangle is degenerate
  109. if (q === 0) {
  110. return undefined;
  111. }
  112. result.y /= q;
  113. result.z /= q;
  114. result.x = 1.0 - result.y - result.z;
  115. return result;
  116. }
  117. /**
  118. * Encapsulates an algorithm to optimize triangles for the post
  119. * vertex-shader cache. This is based on the 2007 SIGGRAPH paper
  120. * 'Fast Triangle Reordering for Vertex Locality and Reduced Overdraw.'
  121. * The runtime is linear but several passes are made.
  122. *
  123. * @namespace Tipsify
  124. *
  125. * @see <a href='http://gfx.cs.princeton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf'>
  126. * Fast Triangle Reordering for Vertex Locality and Reduced Overdraw</a>
  127. * by Sander, Nehab, and Barczak
  128. *
  129. * @private
  130. */
  131. const Tipsify = {};
  132. /**
  133. * Calculates the average cache miss ratio (ACMR) for a given set of indices.
  134. *
  135. * @param {Object} options Object with the following properties:
  136. * @param {Number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
  137. * in the vertex buffer that define the geometry's triangles.
  138. * @param {Number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
  139. * If not supplied, this value will be computed.
  140. * @param {Number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
  141. * @returns {Number} The average cache miss ratio (ACMR).
  142. *
  143. * @exception {DeveloperError} indices length must be a multiple of three.
  144. * @exception {DeveloperError} cacheSize must be greater than two.
  145. *
  146. * @example
  147. * const indices = [0, 1, 2, 3, 4, 5];
  148. * const maxIndex = 5;
  149. * const cacheSize = 3;
  150. * const acmr = Cesium.Tipsify.calculateACMR({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
  151. */
  152. Tipsify.calculateACMR = function (options) {
  153. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  154. const indices = options.indices;
  155. let maximumIndex = options.maximumIndex;
  156. const cacheSize = defaultValue.defaultValue(options.cacheSize, 24);
  157. //>>includeStart('debug', pragmas.debug);
  158. if (!defaultValue.defined(indices)) {
  159. throw new RuntimeError.DeveloperError("indices is required.");
  160. }
  161. //>>includeEnd('debug');
  162. const numIndices = indices.length;
  163. //>>includeStart('debug', pragmas.debug);
  164. if (numIndices < 3 || numIndices % 3 !== 0) {
  165. throw new RuntimeError.DeveloperError("indices length must be a multiple of three.");
  166. }
  167. if (maximumIndex <= 0) {
  168. throw new RuntimeError.DeveloperError("maximumIndex must be greater than zero.");
  169. }
  170. if (cacheSize < 3) {
  171. throw new RuntimeError.DeveloperError("cacheSize must be greater than two.");
  172. }
  173. //>>includeEnd('debug');
  174. // Compute the maximumIndex if not given
  175. if (!defaultValue.defined(maximumIndex)) {
  176. maximumIndex = 0;
  177. let currentIndex = 0;
  178. let intoIndices = indices[currentIndex];
  179. while (currentIndex < numIndices) {
  180. if (intoIndices > maximumIndex) {
  181. maximumIndex = intoIndices;
  182. }
  183. ++currentIndex;
  184. intoIndices = indices[currentIndex];
  185. }
  186. }
  187. // Vertex time stamps
  188. const vertexTimeStamps = [];
  189. for (let i = 0; i < maximumIndex + 1; i++) {
  190. vertexTimeStamps[i] = 0;
  191. }
  192. // Cache processing
  193. let s = cacheSize + 1;
  194. for (let j = 0; j < numIndices; ++j) {
  195. if (s - vertexTimeStamps[indices[j]] > cacheSize) {
  196. vertexTimeStamps[indices[j]] = s;
  197. ++s;
  198. }
  199. }
  200. return (s - cacheSize + 1) / (numIndices / 3);
  201. };
  202. /**
  203. * Optimizes triangles for the post-vertex shader cache.
  204. *
  205. * @param {Object} options Object with the following properties:
  206. * @param {Number[]} options.indices Lists triads of numbers corresponding to the indices of the vertices
  207. * in the vertex buffer that define the geometry's triangles.
  208. * @param {Number} [options.maximumIndex] The maximum value of the elements in <code>args.indices</code>.
  209. * If not supplied, this value will be computed.
  210. * @param {Number} [options.cacheSize=24] The number of vertices that can be stored in the cache at any one time.
  211. * @returns {Number[]} A list of the input indices in an optimized order.
  212. *
  213. * @exception {DeveloperError} indices length must be a multiple of three.
  214. * @exception {DeveloperError} cacheSize must be greater than two.
  215. *
  216. * @example
  217. * const indices = [0, 1, 2, 3, 4, 5];
  218. * const maxIndex = 5;
  219. * const cacheSize = 3;
  220. * const reorderedIndices = Cesium.Tipsify.tipsify({indices : indices, maxIndex : maxIndex, cacheSize : cacheSize});
  221. */
  222. Tipsify.tipsify = function (options) {
  223. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  224. const indices = options.indices;
  225. const maximumIndex = options.maximumIndex;
  226. const cacheSize = defaultValue.defaultValue(options.cacheSize, 24);
  227. let cursor;
  228. function skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne) {
  229. while (deadEnd.length >= 1) {
  230. // while the stack is not empty
  231. const d = deadEnd[deadEnd.length - 1]; // top of the stack
  232. deadEnd.splice(deadEnd.length - 1, 1); // pop the stack
  233. if (vertices[d].numLiveTriangles > 0) {
  234. return d;
  235. }
  236. }
  237. while (cursor < maximumIndexPlusOne) {
  238. if (vertices[cursor].numLiveTriangles > 0) {
  239. ++cursor;
  240. return cursor - 1;
  241. }
  242. ++cursor;
  243. }
  244. return -1;
  245. }
  246. function getNextVertex(
  247. indices,
  248. cacheSize,
  249. oneRing,
  250. vertices,
  251. s,
  252. deadEnd,
  253. maximumIndexPlusOne
  254. ) {
  255. let n = -1;
  256. let p;
  257. let m = -1;
  258. let itOneRing = 0;
  259. while (itOneRing < oneRing.length) {
  260. const index = oneRing[itOneRing];
  261. if (vertices[index].numLiveTriangles) {
  262. p = 0;
  263. if (
  264. s -
  265. vertices[index].timeStamp +
  266. 2 * vertices[index].numLiveTriangles <=
  267. cacheSize
  268. ) {
  269. p = s - vertices[index].timeStamp;
  270. }
  271. if (p > m || m === -1) {
  272. m = p;
  273. n = index;
  274. }
  275. }
  276. ++itOneRing;
  277. }
  278. if (n === -1) {
  279. return skipDeadEnd(vertices, deadEnd, indices, maximumIndexPlusOne);
  280. }
  281. return n;
  282. }
  283. //>>includeStart('debug', pragmas.debug);
  284. if (!defaultValue.defined(indices)) {
  285. throw new RuntimeError.DeveloperError("indices is required.");
  286. }
  287. //>>includeEnd('debug');
  288. const numIndices = indices.length;
  289. //>>includeStart('debug', pragmas.debug);
  290. if (numIndices < 3 || numIndices % 3 !== 0) {
  291. throw new RuntimeError.DeveloperError("indices length must be a multiple of three.");
  292. }
  293. if (maximumIndex <= 0) {
  294. throw new RuntimeError.DeveloperError("maximumIndex must be greater than zero.");
  295. }
  296. if (cacheSize < 3) {
  297. throw new RuntimeError.DeveloperError("cacheSize must be greater than two.");
  298. }
  299. //>>includeEnd('debug');
  300. // Determine maximum index
  301. let maximumIndexPlusOne = 0;
  302. let currentIndex = 0;
  303. let intoIndices = indices[currentIndex];
  304. const endIndex = numIndices;
  305. if (defaultValue.defined(maximumIndex)) {
  306. maximumIndexPlusOne = maximumIndex + 1;
  307. } else {
  308. while (currentIndex < endIndex) {
  309. if (intoIndices > maximumIndexPlusOne) {
  310. maximumIndexPlusOne = intoIndices;
  311. }
  312. ++currentIndex;
  313. intoIndices = indices[currentIndex];
  314. }
  315. if (maximumIndexPlusOne === -1) {
  316. return 0;
  317. }
  318. ++maximumIndexPlusOne;
  319. }
  320. // Vertices
  321. const vertices = [];
  322. let i;
  323. for (i = 0; i < maximumIndexPlusOne; i++) {
  324. vertices[i] = {
  325. numLiveTriangles: 0,
  326. timeStamp: 0,
  327. vertexTriangles: [],
  328. };
  329. }
  330. currentIndex = 0;
  331. let triangle = 0;
  332. while (currentIndex < endIndex) {
  333. vertices[indices[currentIndex]].vertexTriangles.push(triangle);
  334. ++vertices[indices[currentIndex]].numLiveTriangles;
  335. vertices[indices[currentIndex + 1]].vertexTriangles.push(triangle);
  336. ++vertices[indices[currentIndex + 1]].numLiveTriangles;
  337. vertices[indices[currentIndex + 2]].vertexTriangles.push(triangle);
  338. ++vertices[indices[currentIndex + 2]].numLiveTriangles;
  339. ++triangle;
  340. currentIndex += 3;
  341. }
  342. // Starting index
  343. let f = 0;
  344. // Time Stamp
  345. let s = cacheSize + 1;
  346. cursor = 1;
  347. // Process
  348. let oneRing = [];
  349. const deadEnd = []; //Stack
  350. let vertex;
  351. let intoVertices;
  352. let currentOutputIndex = 0;
  353. const outputIndices = [];
  354. const numTriangles = numIndices / 3;
  355. const triangleEmitted = [];
  356. for (i = 0; i < numTriangles; i++) {
  357. triangleEmitted[i] = false;
  358. }
  359. let index;
  360. let limit;
  361. while (f !== -1) {
  362. oneRing = [];
  363. intoVertices = vertices[f];
  364. limit = intoVertices.vertexTriangles.length;
  365. for (let k = 0; k < limit; ++k) {
  366. triangle = intoVertices.vertexTriangles[k];
  367. if (!triangleEmitted[triangle]) {
  368. triangleEmitted[triangle] = true;
  369. currentIndex = triangle + triangle + triangle;
  370. for (let j = 0; j < 3; ++j) {
  371. // Set this index as a possible next index
  372. index = indices[currentIndex];
  373. oneRing.push(index);
  374. deadEnd.push(index);
  375. // Output index
  376. outputIndices[currentOutputIndex] = index;
  377. ++currentOutputIndex;
  378. // Cache processing
  379. vertex = vertices[index];
  380. --vertex.numLiveTriangles;
  381. if (s - vertex.timeStamp > cacheSize) {
  382. vertex.timeStamp = s;
  383. ++s;
  384. }
  385. ++currentIndex;
  386. }
  387. }
  388. }
  389. f = getNextVertex(
  390. indices,
  391. cacheSize,
  392. oneRing,
  393. vertices,
  394. s,
  395. deadEnd,
  396. maximumIndexPlusOne
  397. );
  398. }
  399. return outputIndices;
  400. };
  401. /**
  402. * Content pipeline functions for geometries.
  403. *
  404. * @namespace GeometryPipeline
  405. *
  406. * @see Geometry
  407. */
  408. const GeometryPipeline = {};
  409. function addTriangle(lines, index, i0, i1, i2) {
  410. lines[index++] = i0;
  411. lines[index++] = i1;
  412. lines[index++] = i1;
  413. lines[index++] = i2;
  414. lines[index++] = i2;
  415. lines[index] = i0;
  416. }
  417. function trianglesToLines(triangles) {
  418. const count = triangles.length;
  419. const size = (count / 3) * 6;
  420. const lines = IndexDatatype.IndexDatatype.createTypedArray(count, size);
  421. let index = 0;
  422. for (let i = 0; i < count; i += 3, index += 6) {
  423. addTriangle(lines, index, triangles[i], triangles[i + 1], triangles[i + 2]);
  424. }
  425. return lines;
  426. }
  427. function triangleStripToLines(triangles) {
  428. const count = triangles.length;
  429. if (count >= 3) {
  430. const size = (count - 2) * 6;
  431. const lines = IndexDatatype.IndexDatatype.createTypedArray(count, size);
  432. addTriangle(lines, 0, triangles[0], triangles[1], triangles[2]);
  433. let index = 6;
  434. for (let i = 3; i < count; ++i, index += 6) {
  435. addTriangle(
  436. lines,
  437. index,
  438. triangles[i - 1],
  439. triangles[i],
  440. triangles[i - 2]
  441. );
  442. }
  443. return lines;
  444. }
  445. return new Uint16Array();
  446. }
  447. function triangleFanToLines(triangles) {
  448. if (triangles.length > 0) {
  449. const count = triangles.length - 1;
  450. const size = (count - 1) * 6;
  451. const lines = IndexDatatype.IndexDatatype.createTypedArray(count, size);
  452. const base = triangles[0];
  453. let index = 0;
  454. for (let i = 1; i < count; ++i, index += 6) {
  455. addTriangle(lines, index, base, triangles[i], triangles[i + 1]);
  456. }
  457. return lines;
  458. }
  459. return new Uint16Array();
  460. }
  461. /**
  462. * Converts a geometry's triangle indices to line indices. If the geometry has an <code>indices</code>
  463. * and its <code>primitiveType</code> is <code>TRIANGLES</code>, <code>TRIANGLE_STRIP</code>,
  464. * <code>TRIANGLE_FAN</code>, it is converted to <code>LINES</code>; otherwise, the geometry is not changed.
  465. * <p>
  466. * This is commonly used to create a wireframe geometry for visual debugging.
  467. * </p>
  468. *
  469. * @param {Geometry} geometry The geometry to modify.
  470. * @returns {Geometry} The modified <code>geometry</code> argument, with its triangle indices converted to lines.
  471. *
  472. * @exception {DeveloperError} geometry.primitiveType must be TRIANGLES, TRIANGLE_STRIP, or TRIANGLE_FAN.
  473. *
  474. * @example
  475. * geometry = Cesium.GeometryPipeline.toWireframe(geometry);
  476. */
  477. GeometryPipeline.toWireframe = function (geometry) {
  478. //>>includeStart('debug', pragmas.debug);
  479. if (!defaultValue.defined(geometry)) {
  480. throw new RuntimeError.DeveloperError("geometry is required.");
  481. }
  482. //>>includeEnd('debug');
  483. const indices = geometry.indices;
  484. if (defaultValue.defined(indices)) {
  485. switch (geometry.primitiveType) {
  486. case GeometryAttribute.PrimitiveType.TRIANGLES:
  487. geometry.indices = trianglesToLines(indices);
  488. break;
  489. case GeometryAttribute.PrimitiveType.TRIANGLE_STRIP:
  490. geometry.indices = triangleStripToLines(indices);
  491. break;
  492. case GeometryAttribute.PrimitiveType.TRIANGLE_FAN:
  493. geometry.indices = triangleFanToLines(indices);
  494. break;
  495. //>>includeStart('debug', pragmas.debug);
  496. default:
  497. throw new RuntimeError.DeveloperError(
  498. "geometry.primitiveType must be TRIANGLES, TRIANGLE_STRIP, or TRIANGLE_FAN."
  499. );
  500. //>>includeEnd('debug');
  501. }
  502. geometry.primitiveType = GeometryAttribute.PrimitiveType.LINES;
  503. }
  504. return geometry;
  505. };
  506. /**
  507. * Creates a new {@link Geometry} with <code>LINES</code> representing the provided
  508. * attribute (<code>attributeName</code>) for the provided geometry. This is used to
  509. * visualize vector attributes like normals, tangents, and bitangents.
  510. *
  511. * @param {Geometry} geometry The <code>Geometry</code> instance with the attribute.
  512. * @param {String} [attributeName='normal'] The name of the attribute.
  513. * @param {Number} [length=10000.0] The length of each line segment in meters. This can be negative to point the vector in the opposite direction.
  514. * @returns {Geometry} A new <code>Geometry</code> instance with line segments for the vector.
  515. *
  516. * @exception {DeveloperError} geometry.attributes must have an attribute with the same name as the attributeName parameter.
  517. *
  518. * @example
  519. * const geometry = Cesium.GeometryPipeline.createLineSegmentsForVectors(instance.geometry, 'bitangent', 100000.0);
  520. */
  521. GeometryPipeline.createLineSegmentsForVectors = function (
  522. geometry,
  523. attributeName,
  524. length
  525. ) {
  526. attributeName = defaultValue.defaultValue(attributeName, "normal");
  527. //>>includeStart('debug', pragmas.debug);
  528. if (!defaultValue.defined(geometry)) {
  529. throw new RuntimeError.DeveloperError("geometry is required.");
  530. }
  531. if (!defaultValue.defined(geometry.attributes.position)) {
  532. throw new RuntimeError.DeveloperError("geometry.attributes.position is required.");
  533. }
  534. if (!defaultValue.defined(geometry.attributes[attributeName])) {
  535. throw new RuntimeError.DeveloperError(
  536. `geometry.attributes must have an attribute with the same name as the attributeName parameter, ${attributeName}.`
  537. );
  538. }
  539. //>>includeEnd('debug');
  540. length = defaultValue.defaultValue(length, 10000.0);
  541. const positions = geometry.attributes.position.values;
  542. const vectors = geometry.attributes[attributeName].values;
  543. const positionsLength = positions.length;
  544. const newPositions = new Float64Array(2 * positionsLength);
  545. let j = 0;
  546. for (let i = 0; i < positionsLength; i += 3) {
  547. newPositions[j++] = positions[i];
  548. newPositions[j++] = positions[i + 1];
  549. newPositions[j++] = positions[i + 2];
  550. newPositions[j++] = positions[i] + vectors[i] * length;
  551. newPositions[j++] = positions[i + 1] + vectors[i + 1] * length;
  552. newPositions[j++] = positions[i + 2] + vectors[i + 2] * length;
  553. }
  554. let newBoundingSphere;
  555. const bs = geometry.boundingSphere;
  556. if (defaultValue.defined(bs)) {
  557. newBoundingSphere = new Transforms.BoundingSphere(bs.center, bs.radius + length);
  558. }
  559. return new GeometryAttribute.Geometry({
  560. attributes: {
  561. position: new GeometryAttribute.GeometryAttribute({
  562. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  563. componentsPerAttribute: 3,
  564. values: newPositions,
  565. }),
  566. },
  567. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  568. boundingSphere: newBoundingSphere,
  569. });
  570. };
  571. /**
  572. * Creates an object that maps attribute names to unique locations (indices)
  573. * for matching vertex attributes and shader programs.
  574. *
  575. * @param {Geometry} geometry The geometry, which is not modified, to create the object for.
  576. * @returns {Object} An object with attribute name / index pairs.
  577. *
  578. * @example
  579. * const attributeLocations = Cesium.GeometryPipeline.createAttributeLocations(geometry);
  580. * // Example output
  581. * // {
  582. * // 'position' : 0,
  583. * // 'normal' : 1
  584. * // }
  585. */
  586. GeometryPipeline.createAttributeLocations = function (geometry) {
  587. //>>includeStart('debug', pragmas.debug);
  588. if (!defaultValue.defined(geometry)) {
  589. throw new RuntimeError.DeveloperError("geometry is required.");
  590. }
  591. //>>includeEnd('debug')
  592. // There can be a WebGL performance hit when attribute 0 is disabled, so
  593. // assign attribute locations to well-known attributes.
  594. const semantics = [
  595. "position",
  596. "positionHigh",
  597. "positionLow",
  598. // From VertexFormat.position - after 2D projection and high-precision encoding
  599. "position3DHigh",
  600. "position3DLow",
  601. "position2DHigh",
  602. "position2DLow",
  603. // From Primitive
  604. "pickColor",
  605. // From VertexFormat
  606. "normal",
  607. "st",
  608. "tangent",
  609. "bitangent",
  610. // For shadow volumes
  611. "extrudeDirection",
  612. // From compressing texture coordinates and normals
  613. "compressedAttributes",
  614. ];
  615. const attributes = geometry.attributes;
  616. const indices = {};
  617. let j = 0;
  618. let i;
  619. const len = semantics.length;
  620. // Attribute locations for well-known attributes
  621. for (i = 0; i < len; ++i) {
  622. const semantic = semantics[i];
  623. if (defaultValue.defined(attributes[semantic])) {
  624. indices[semantic] = j++;
  625. }
  626. }
  627. // Locations for custom attributes
  628. for (const name in attributes) {
  629. if (attributes.hasOwnProperty(name) && !defaultValue.defined(indices[name])) {
  630. indices[name] = j++;
  631. }
  632. }
  633. return indices;
  634. };
  635. /**
  636. * Reorders a geometry's attributes and <code>indices</code> to achieve better performance from the GPU's pre-vertex-shader cache.
  637. *
  638. * @param {Geometry} geometry The geometry to modify.
  639. * @returns {Geometry} The modified <code>geometry</code> argument, with its attributes and indices reordered for the GPU's pre-vertex-shader cache.
  640. *
  641. * @exception {DeveloperError} Each attribute array in geometry.attributes must have the same number of attributes.
  642. *
  643. *
  644. * @example
  645. * geometry = Cesium.GeometryPipeline.reorderForPreVertexCache(geometry);
  646. *
  647. * @see GeometryPipeline.reorderForPostVertexCache
  648. */
  649. GeometryPipeline.reorderForPreVertexCache = function (geometry) {
  650. //>>includeStart('debug', pragmas.debug);
  651. if (!defaultValue.defined(geometry)) {
  652. throw new RuntimeError.DeveloperError("geometry is required.");
  653. }
  654. //>>includeEnd('debug');
  655. const numVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  656. const indices = geometry.indices;
  657. if (defaultValue.defined(indices)) {
  658. const indexCrossReferenceOldToNew = new Int32Array(numVertices);
  659. for (let i = 0; i < numVertices; i++) {
  660. indexCrossReferenceOldToNew[i] = -1;
  661. }
  662. // Construct cross reference and reorder indices
  663. const indicesIn = indices;
  664. const numIndices = indicesIn.length;
  665. const indicesOut = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices);
  666. let intoIndicesIn = 0;
  667. let intoIndicesOut = 0;
  668. let nextIndex = 0;
  669. let tempIndex;
  670. while (intoIndicesIn < numIndices) {
  671. tempIndex = indexCrossReferenceOldToNew[indicesIn[intoIndicesIn]];
  672. if (tempIndex !== -1) {
  673. indicesOut[intoIndicesOut] = tempIndex;
  674. } else {
  675. tempIndex = indicesIn[intoIndicesIn];
  676. indexCrossReferenceOldToNew[tempIndex] = nextIndex;
  677. indicesOut[intoIndicesOut] = nextIndex;
  678. ++nextIndex;
  679. }
  680. ++intoIndicesIn;
  681. ++intoIndicesOut;
  682. }
  683. geometry.indices = indicesOut;
  684. // Reorder attributes
  685. const attributes = geometry.attributes;
  686. for (const property in attributes) {
  687. if (
  688. attributes.hasOwnProperty(property) &&
  689. defaultValue.defined(attributes[property]) &&
  690. defaultValue.defined(attributes[property].values)
  691. ) {
  692. const attribute = attributes[property];
  693. const elementsIn = attribute.values;
  694. let intoElementsIn = 0;
  695. const numComponents = attribute.componentsPerAttribute;
  696. const elementsOut = ComponentDatatype.ComponentDatatype.createTypedArray(
  697. attribute.componentDatatype,
  698. nextIndex * numComponents
  699. );
  700. while (intoElementsIn < numVertices) {
  701. const temp = indexCrossReferenceOldToNew[intoElementsIn];
  702. if (temp !== -1) {
  703. for (let j = 0; j < numComponents; j++) {
  704. elementsOut[numComponents * temp + j] =
  705. elementsIn[numComponents * intoElementsIn + j];
  706. }
  707. }
  708. ++intoElementsIn;
  709. }
  710. attribute.values = elementsOut;
  711. }
  712. }
  713. }
  714. return geometry;
  715. };
  716. /**
  717. * Reorders a geometry's <code>indices</code> to achieve better performance from the GPU's
  718. * post vertex-shader cache by using the Tipsify algorithm. If the geometry <code>primitiveType</code>
  719. * is not <code>TRIANGLES</code> or the geometry does not have an <code>indices</code>, this function has no effect.
  720. *
  721. * @param {Geometry} geometry The geometry to modify.
  722. * @param {Number} [cacheCapacity=24] The number of vertices that can be held in the GPU's vertex cache.
  723. * @returns {Geometry} The modified <code>geometry</code> argument, with its indices reordered for the post-vertex-shader cache.
  724. *
  725. * @exception {DeveloperError} cacheCapacity must be greater than two.
  726. *
  727. *
  728. * @example
  729. * geometry = Cesium.GeometryPipeline.reorderForPostVertexCache(geometry);
  730. *
  731. * @see GeometryPipeline.reorderForPreVertexCache
  732. * @see {@link http://gfx.cs.princ0eton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf|Fast Triangle Reordering for Vertex Locality and Reduced Overdraw}
  733. * by Sander, Nehab, and Barczak
  734. */
  735. GeometryPipeline.reorderForPostVertexCache = function (
  736. geometry,
  737. cacheCapacity
  738. ) {
  739. //>>includeStart('debug', pragmas.debug);
  740. if (!defaultValue.defined(geometry)) {
  741. throw new RuntimeError.DeveloperError("geometry is required.");
  742. }
  743. //>>includeEnd('debug');
  744. const indices = geometry.indices;
  745. if (geometry.primitiveType === GeometryAttribute.PrimitiveType.TRIANGLES && defaultValue.defined(indices)) {
  746. const numIndices = indices.length;
  747. let maximumIndex = 0;
  748. for (let j = 0; j < numIndices; j++) {
  749. if (indices[j] > maximumIndex) {
  750. maximumIndex = indices[j];
  751. }
  752. }
  753. geometry.indices = Tipsify.tipsify({
  754. indices: indices,
  755. maximumIndex: maximumIndex,
  756. cacheSize: cacheCapacity,
  757. });
  758. }
  759. return geometry;
  760. };
  761. function copyAttributesDescriptions(attributes) {
  762. const newAttributes = {};
  763. for (const attribute in attributes) {
  764. if (
  765. attributes.hasOwnProperty(attribute) &&
  766. defaultValue.defined(attributes[attribute]) &&
  767. defaultValue.defined(attributes[attribute].values)
  768. ) {
  769. const attr = attributes[attribute];
  770. newAttributes[attribute] = new GeometryAttribute.GeometryAttribute({
  771. componentDatatype: attr.componentDatatype,
  772. componentsPerAttribute: attr.componentsPerAttribute,
  773. normalize: attr.normalize,
  774. values: [],
  775. });
  776. }
  777. }
  778. return newAttributes;
  779. }
  780. function copyVertex(destinationAttributes, sourceAttributes, index) {
  781. for (const attribute in sourceAttributes) {
  782. if (
  783. sourceAttributes.hasOwnProperty(attribute) &&
  784. defaultValue.defined(sourceAttributes[attribute]) &&
  785. defaultValue.defined(sourceAttributes[attribute].values)
  786. ) {
  787. const attr = sourceAttributes[attribute];
  788. for (let k = 0; k < attr.componentsPerAttribute; ++k) {
  789. destinationAttributes[attribute].values.push(
  790. attr.values[index * attr.componentsPerAttribute + k]
  791. );
  792. }
  793. }
  794. }
  795. }
  796. /**
  797. * Splits a geometry into multiple geometries, if necessary, to ensure that indices in the
  798. * <code>indices</code> fit into unsigned shorts. This is used to meet the WebGL requirements
  799. * when unsigned int indices are not supported.
  800. * <p>
  801. * If the geometry does not have any <code>indices</code>, this function has no effect.
  802. * </p>
  803. *
  804. * @param {Geometry} geometry The geometry to be split into multiple geometries.
  805. * @returns {Geometry[]} An array of geometries, each with indices that fit into unsigned shorts.
  806. *
  807. * @exception {DeveloperError} geometry.primitiveType must equal to PrimitiveType.TRIANGLES, PrimitiveType.LINES, or PrimitiveType.POINTS
  808. * @exception {DeveloperError} All geometry attribute lists must have the same number of attributes.
  809. *
  810. * @example
  811. * const geometries = Cesium.GeometryPipeline.fitToUnsignedShortIndices(geometry);
  812. */
  813. GeometryPipeline.fitToUnsignedShortIndices = function (geometry) {
  814. //>>includeStart('debug', pragmas.debug);
  815. if (!defaultValue.defined(geometry)) {
  816. throw new RuntimeError.DeveloperError("geometry is required.");
  817. }
  818. if (
  819. defaultValue.defined(geometry.indices) &&
  820. geometry.primitiveType !== GeometryAttribute.PrimitiveType.TRIANGLES &&
  821. geometry.primitiveType !== GeometryAttribute.PrimitiveType.LINES &&
  822. geometry.primitiveType !== GeometryAttribute.PrimitiveType.POINTS
  823. ) {
  824. throw new RuntimeError.DeveloperError(
  825. "geometry.primitiveType must equal to PrimitiveType.TRIANGLES, PrimitiveType.LINES, or PrimitiveType.POINTS."
  826. );
  827. }
  828. //>>includeEnd('debug');
  829. const geometries = [];
  830. // If there's an index list and more than 64K attributes, it is possible that
  831. // some indices are outside the range of unsigned short [0, 64K - 1]
  832. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  833. if (
  834. defaultValue.defined(geometry.indices) &&
  835. numberOfVertices >= ComponentDatatype.CesiumMath.SIXTY_FOUR_KILOBYTES
  836. ) {
  837. let oldToNewIndex = [];
  838. let newIndices = [];
  839. let currentIndex = 0;
  840. let newAttributes = copyAttributesDescriptions(geometry.attributes);
  841. const originalIndices = geometry.indices;
  842. const numberOfIndices = originalIndices.length;
  843. let indicesPerPrimitive;
  844. if (geometry.primitiveType === GeometryAttribute.PrimitiveType.TRIANGLES) {
  845. indicesPerPrimitive = 3;
  846. } else if (geometry.primitiveType === GeometryAttribute.PrimitiveType.LINES) {
  847. indicesPerPrimitive = 2;
  848. } else if (geometry.primitiveType === GeometryAttribute.PrimitiveType.POINTS) {
  849. indicesPerPrimitive = 1;
  850. }
  851. for (let j = 0; j < numberOfIndices; j += indicesPerPrimitive) {
  852. for (let k = 0; k < indicesPerPrimitive; ++k) {
  853. const x = originalIndices[j + k];
  854. let i = oldToNewIndex[x];
  855. if (!defaultValue.defined(i)) {
  856. i = currentIndex++;
  857. oldToNewIndex[x] = i;
  858. copyVertex(newAttributes, geometry.attributes, x);
  859. }
  860. newIndices.push(i);
  861. }
  862. if (
  863. currentIndex + indicesPerPrimitive >=
  864. ComponentDatatype.CesiumMath.SIXTY_FOUR_KILOBYTES
  865. ) {
  866. geometries.push(
  867. new GeometryAttribute.Geometry({
  868. attributes: newAttributes,
  869. indices: newIndices,
  870. primitiveType: geometry.primitiveType,
  871. boundingSphere: geometry.boundingSphere,
  872. boundingSphereCV: geometry.boundingSphereCV,
  873. })
  874. );
  875. // Reset for next vertex-array
  876. oldToNewIndex = [];
  877. newIndices = [];
  878. currentIndex = 0;
  879. newAttributes = copyAttributesDescriptions(geometry.attributes);
  880. }
  881. }
  882. if (newIndices.length !== 0) {
  883. geometries.push(
  884. new GeometryAttribute.Geometry({
  885. attributes: newAttributes,
  886. indices: newIndices,
  887. primitiveType: geometry.primitiveType,
  888. boundingSphere: geometry.boundingSphere,
  889. boundingSphereCV: geometry.boundingSphereCV,
  890. })
  891. );
  892. }
  893. } else {
  894. // No need to split into multiple geometries
  895. geometries.push(geometry);
  896. }
  897. return geometries;
  898. };
  899. const scratchProjectTo2DCartesian3 = new Matrix2.Cartesian3();
  900. const scratchProjectTo2DCartographic = new Matrix2.Cartographic();
  901. /**
  902. * Projects a geometry's 3D <code>position</code> attribute to 2D, replacing the <code>position</code>
  903. * attribute with separate <code>position3D</code> and <code>position2D</code> attributes.
  904. * <p>
  905. * If the geometry does not have a <code>position</code>, this function has no effect.
  906. * </p>
  907. *
  908. * @param {Geometry} geometry The geometry to modify.
  909. * @param {String} attributeName The name of the attribute.
  910. * @param {String} attributeName3D The name of the attribute in 3D.
  911. * @param {String} attributeName2D The name of the attribute in 2D.
  912. * @param {Object} [projection=new GeographicProjection()] The projection to use.
  913. * @returns {Geometry} The modified <code>geometry</code> argument with <code>position3D</code> and <code>position2D</code> attributes.
  914. *
  915. * @exception {DeveloperError} geometry must have attribute matching the attributeName argument.
  916. * @exception {DeveloperError} The attribute componentDatatype must be ComponentDatatype.DOUBLE.
  917. * @exception {DeveloperError} Could not project a point to 2D.
  918. *
  919. * @example
  920. * geometry = Cesium.GeometryPipeline.projectTo2D(geometry, 'position', 'position3D', 'position2D');
  921. */
  922. GeometryPipeline.projectTo2D = function (
  923. geometry,
  924. attributeName,
  925. attributeName3D,
  926. attributeName2D,
  927. projection
  928. ) {
  929. //>>includeStart('debug', pragmas.debug);
  930. if (!defaultValue.defined(geometry)) {
  931. throw new RuntimeError.DeveloperError("geometry is required.");
  932. }
  933. if (!defaultValue.defined(attributeName)) {
  934. throw new RuntimeError.DeveloperError("attributeName is required.");
  935. }
  936. if (!defaultValue.defined(attributeName3D)) {
  937. throw new RuntimeError.DeveloperError("attributeName3D is required.");
  938. }
  939. if (!defaultValue.defined(attributeName2D)) {
  940. throw new RuntimeError.DeveloperError("attributeName2D is required.");
  941. }
  942. if (!defaultValue.defined(geometry.attributes[attributeName])) {
  943. throw new RuntimeError.DeveloperError(
  944. `geometry must have attribute matching the attributeName argument: ${attributeName}.`
  945. );
  946. }
  947. if (
  948. geometry.attributes[attributeName].componentDatatype !==
  949. ComponentDatatype.ComponentDatatype.DOUBLE
  950. ) {
  951. throw new RuntimeError.DeveloperError(
  952. "The attribute componentDatatype must be ComponentDatatype.DOUBLE."
  953. );
  954. }
  955. //>>includeEnd('debug');
  956. const attribute = geometry.attributes[attributeName];
  957. projection = defaultValue.defined(projection) ? projection : new Transforms.GeographicProjection();
  958. const ellipsoid = projection.ellipsoid;
  959. // Project original values to 2D.
  960. const values3D = attribute.values;
  961. const projectedValues = new Float64Array(values3D.length);
  962. let index = 0;
  963. for (let i = 0; i < values3D.length; i += 3) {
  964. const value = Matrix2.Cartesian3.fromArray(
  965. values3D,
  966. i,
  967. scratchProjectTo2DCartesian3
  968. );
  969. const lonLat = ellipsoid.cartesianToCartographic(
  970. value,
  971. scratchProjectTo2DCartographic
  972. );
  973. //>>includeStart('debug', pragmas.debug);
  974. if (!defaultValue.defined(lonLat)) {
  975. throw new RuntimeError.DeveloperError(
  976. `Could not project point (${value.x}, ${value.y}, ${value.z}) to 2D.`
  977. );
  978. }
  979. //>>includeEnd('debug');
  980. const projectedLonLat = projection.project(
  981. lonLat,
  982. scratchProjectTo2DCartesian3
  983. );
  984. projectedValues[index++] = projectedLonLat.x;
  985. projectedValues[index++] = projectedLonLat.y;
  986. projectedValues[index++] = projectedLonLat.z;
  987. }
  988. // Rename original cartesians to WGS84 cartesians.
  989. geometry.attributes[attributeName3D] = attribute;
  990. // Replace original cartesians with 2D projected cartesians
  991. geometry.attributes[attributeName2D] = new GeometryAttribute.GeometryAttribute({
  992. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  993. componentsPerAttribute: 3,
  994. values: projectedValues,
  995. });
  996. delete geometry.attributes[attributeName];
  997. return geometry;
  998. };
  999. const encodedResult = {
  1000. high: 0.0,
  1001. low: 0.0,
  1002. };
  1003. /**
  1004. * Encodes floating-point geometry attribute values as two separate attributes to improve
  1005. * rendering precision.
  1006. * <p>
  1007. * This is commonly used to create high-precision position vertex attributes.
  1008. * </p>
  1009. *
  1010. * @param {Geometry} geometry The geometry to modify.
  1011. * @param {String} attributeName The name of the attribute.
  1012. * @param {String} attributeHighName The name of the attribute for the encoded high bits.
  1013. * @param {String} attributeLowName The name of the attribute for the encoded low bits.
  1014. * @returns {Geometry} The modified <code>geometry</code> argument, with its encoded attribute.
  1015. *
  1016. * @exception {DeveloperError} geometry must have attribute matching the attributeName argument.
  1017. * @exception {DeveloperError} The attribute componentDatatype must be ComponentDatatype.DOUBLE.
  1018. *
  1019. * @example
  1020. * geometry = Cesium.GeometryPipeline.encodeAttribute(geometry, 'position3D', 'position3DHigh', 'position3DLow');
  1021. */
  1022. GeometryPipeline.encodeAttribute = function (
  1023. geometry,
  1024. attributeName,
  1025. attributeHighName,
  1026. attributeLowName
  1027. ) {
  1028. //>>includeStart('debug', pragmas.debug);
  1029. if (!defaultValue.defined(geometry)) {
  1030. throw new RuntimeError.DeveloperError("geometry is required.");
  1031. }
  1032. if (!defaultValue.defined(attributeName)) {
  1033. throw new RuntimeError.DeveloperError("attributeName is required.");
  1034. }
  1035. if (!defaultValue.defined(attributeHighName)) {
  1036. throw new RuntimeError.DeveloperError("attributeHighName is required.");
  1037. }
  1038. if (!defaultValue.defined(attributeLowName)) {
  1039. throw new RuntimeError.DeveloperError("attributeLowName is required.");
  1040. }
  1041. if (!defaultValue.defined(geometry.attributes[attributeName])) {
  1042. throw new RuntimeError.DeveloperError(
  1043. `geometry must have attribute matching the attributeName argument: ${attributeName}.`
  1044. );
  1045. }
  1046. if (
  1047. geometry.attributes[attributeName].componentDatatype !==
  1048. ComponentDatatype.ComponentDatatype.DOUBLE
  1049. ) {
  1050. throw new RuntimeError.DeveloperError(
  1051. "The attribute componentDatatype must be ComponentDatatype.DOUBLE."
  1052. );
  1053. }
  1054. //>>includeEnd('debug');
  1055. const attribute = geometry.attributes[attributeName];
  1056. const values = attribute.values;
  1057. const length = values.length;
  1058. const highValues = new Float32Array(length);
  1059. const lowValues = new Float32Array(length);
  1060. for (let i = 0; i < length; ++i) {
  1061. EncodedCartesian3.EncodedCartesian3.encode(values[i], encodedResult);
  1062. highValues[i] = encodedResult.high;
  1063. lowValues[i] = encodedResult.low;
  1064. }
  1065. const componentsPerAttribute = attribute.componentsPerAttribute;
  1066. geometry.attributes[attributeHighName] = new GeometryAttribute.GeometryAttribute({
  1067. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1068. componentsPerAttribute: componentsPerAttribute,
  1069. values: highValues,
  1070. });
  1071. geometry.attributes[attributeLowName] = new GeometryAttribute.GeometryAttribute({
  1072. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1073. componentsPerAttribute: componentsPerAttribute,
  1074. values: lowValues,
  1075. });
  1076. delete geometry.attributes[attributeName];
  1077. return geometry;
  1078. };
  1079. let scratchCartesian3 = new Matrix2.Cartesian3();
  1080. function transformPoint(matrix, attribute) {
  1081. if (defaultValue.defined(attribute)) {
  1082. const values = attribute.values;
  1083. const length = values.length;
  1084. for (let i = 0; i < length; i += 3) {
  1085. Matrix2.Cartesian3.unpack(values, i, scratchCartesian3);
  1086. Matrix2.Matrix4.multiplyByPoint(matrix, scratchCartesian3, scratchCartesian3);
  1087. Matrix2.Cartesian3.pack(scratchCartesian3, values, i);
  1088. }
  1089. }
  1090. }
  1091. function transformVector(matrix, attribute) {
  1092. if (defaultValue.defined(attribute)) {
  1093. const values = attribute.values;
  1094. const length = values.length;
  1095. for (let i = 0; i < length; i += 3) {
  1096. Matrix2.Cartesian3.unpack(values, i, scratchCartesian3);
  1097. Matrix2.Matrix3.multiplyByVector(matrix, scratchCartesian3, scratchCartesian3);
  1098. scratchCartesian3 = Matrix2.Cartesian3.normalize(
  1099. scratchCartesian3,
  1100. scratchCartesian3
  1101. );
  1102. Matrix2.Cartesian3.pack(scratchCartesian3, values, i);
  1103. }
  1104. }
  1105. }
  1106. const inverseTranspose = new Matrix2.Matrix4();
  1107. const normalMatrix = new Matrix2.Matrix3();
  1108. /**
  1109. * Transforms a geometry instance to world coordinates. This changes
  1110. * the instance's <code>modelMatrix</code> to {@link Matrix4.IDENTITY} and transforms the
  1111. * following attributes if they are present: <code>position</code>, <code>normal</code>,
  1112. * <code>tangent</code>, and <code>bitangent</code>.
  1113. *
  1114. * @param {GeometryInstance} instance The geometry instance to modify.
  1115. * @returns {GeometryInstance} The modified <code>instance</code> argument, with its attributes transforms to world coordinates.
  1116. *
  1117. * @example
  1118. * Cesium.GeometryPipeline.transformToWorldCoordinates(instance);
  1119. */
  1120. GeometryPipeline.transformToWorldCoordinates = function (instance) {
  1121. //>>includeStart('debug', pragmas.debug);
  1122. if (!defaultValue.defined(instance)) {
  1123. throw new RuntimeError.DeveloperError("instance is required.");
  1124. }
  1125. //>>includeEnd('debug');
  1126. const modelMatrix = instance.modelMatrix;
  1127. if (Matrix2.Matrix4.equals(modelMatrix, Matrix2.Matrix4.IDENTITY)) {
  1128. // Already in world coordinates
  1129. return instance;
  1130. }
  1131. const attributes = instance.geometry.attributes;
  1132. // Transform attributes in known vertex formats
  1133. transformPoint(modelMatrix, attributes.position);
  1134. transformPoint(modelMatrix, attributes.prevPosition);
  1135. transformPoint(modelMatrix, attributes.nextPosition);
  1136. if (
  1137. defaultValue.defined(attributes.normal) ||
  1138. defaultValue.defined(attributes.tangent) ||
  1139. defaultValue.defined(attributes.bitangent)
  1140. ) {
  1141. Matrix2.Matrix4.inverse(modelMatrix, inverseTranspose);
  1142. Matrix2.Matrix4.transpose(inverseTranspose, inverseTranspose);
  1143. Matrix2.Matrix4.getMatrix3(inverseTranspose, normalMatrix);
  1144. transformVector(normalMatrix, attributes.normal);
  1145. transformVector(normalMatrix, attributes.tangent);
  1146. transformVector(normalMatrix, attributes.bitangent);
  1147. }
  1148. const boundingSphere = instance.geometry.boundingSphere;
  1149. if (defaultValue.defined(boundingSphere)) {
  1150. instance.geometry.boundingSphere = Transforms.BoundingSphere.transform(
  1151. boundingSphere,
  1152. modelMatrix,
  1153. boundingSphere
  1154. );
  1155. }
  1156. instance.modelMatrix = Matrix2.Matrix4.clone(Matrix2.Matrix4.IDENTITY);
  1157. return instance;
  1158. };
  1159. function findAttributesInAllGeometries(instances, propertyName) {
  1160. const length = instances.length;
  1161. const attributesInAllGeometries = {};
  1162. const attributes0 = instances[0][propertyName].attributes;
  1163. let name;
  1164. for (name in attributes0) {
  1165. if (
  1166. attributes0.hasOwnProperty(name) &&
  1167. defaultValue.defined(attributes0[name]) &&
  1168. defaultValue.defined(attributes0[name].values)
  1169. ) {
  1170. const attribute = attributes0[name];
  1171. let numberOfComponents = attribute.values.length;
  1172. let inAllGeometries = true;
  1173. // Does this same attribute exist in all geometries?
  1174. for (let i = 1; i < length; ++i) {
  1175. const otherAttribute = instances[i][propertyName].attributes[name];
  1176. if (
  1177. !defaultValue.defined(otherAttribute) ||
  1178. attribute.componentDatatype !== otherAttribute.componentDatatype ||
  1179. attribute.componentsPerAttribute !==
  1180. otherAttribute.componentsPerAttribute ||
  1181. attribute.normalize !== otherAttribute.normalize
  1182. ) {
  1183. inAllGeometries = false;
  1184. break;
  1185. }
  1186. numberOfComponents += otherAttribute.values.length;
  1187. }
  1188. if (inAllGeometries) {
  1189. attributesInAllGeometries[name] = new GeometryAttribute.GeometryAttribute({
  1190. componentDatatype: attribute.componentDatatype,
  1191. componentsPerAttribute: attribute.componentsPerAttribute,
  1192. normalize: attribute.normalize,
  1193. values: ComponentDatatype.ComponentDatatype.createTypedArray(
  1194. attribute.componentDatatype,
  1195. numberOfComponents
  1196. ),
  1197. });
  1198. }
  1199. }
  1200. }
  1201. return attributesInAllGeometries;
  1202. }
  1203. const tempScratch = new Matrix2.Cartesian3();
  1204. function combineGeometries(instances, propertyName) {
  1205. const length = instances.length;
  1206. let name;
  1207. let i;
  1208. let j;
  1209. let k;
  1210. const m = instances[0].modelMatrix;
  1211. const haveIndices = defaultValue.defined(instances[0][propertyName].indices);
  1212. const primitiveType = instances[0][propertyName].primitiveType;
  1213. //>>includeStart('debug', pragmas.debug);
  1214. for (i = 1; i < length; ++i) {
  1215. if (!Matrix2.Matrix4.equals(instances[i].modelMatrix, m)) {
  1216. throw new RuntimeError.DeveloperError("All instances must have the same modelMatrix.");
  1217. }
  1218. if (defaultValue.defined(instances[i][propertyName].indices) !== haveIndices) {
  1219. throw new RuntimeError.DeveloperError(
  1220. "All instance geometries must have an indices or not have one."
  1221. );
  1222. }
  1223. if (instances[i][propertyName].primitiveType !== primitiveType) {
  1224. throw new RuntimeError.DeveloperError(
  1225. "All instance geometries must have the same primitiveType."
  1226. );
  1227. }
  1228. }
  1229. //>>includeEnd('debug');
  1230. // Find subset of attributes in all geometries
  1231. const attributes = findAttributesInAllGeometries(instances, propertyName);
  1232. let values;
  1233. let sourceValues;
  1234. let sourceValuesLength;
  1235. // Combine attributes from each geometry into a single typed array
  1236. for (name in attributes) {
  1237. if (attributes.hasOwnProperty(name)) {
  1238. values = attributes[name].values;
  1239. k = 0;
  1240. for (i = 0; i < length; ++i) {
  1241. sourceValues = instances[i][propertyName].attributes[name].values;
  1242. sourceValuesLength = sourceValues.length;
  1243. for (j = 0; j < sourceValuesLength; ++j) {
  1244. values[k++] = sourceValues[j];
  1245. }
  1246. }
  1247. }
  1248. }
  1249. // Combine index lists
  1250. let indices;
  1251. if (haveIndices) {
  1252. let numberOfIndices = 0;
  1253. for (i = 0; i < length; ++i) {
  1254. numberOfIndices += instances[i][propertyName].indices.length;
  1255. }
  1256. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(
  1257. new GeometryAttribute.Geometry({
  1258. attributes: attributes,
  1259. primitiveType: GeometryAttribute.PrimitiveType.POINTS,
  1260. })
  1261. );
  1262. const destIndices = IndexDatatype.IndexDatatype.createTypedArray(
  1263. numberOfVertices,
  1264. numberOfIndices
  1265. );
  1266. let destOffset = 0;
  1267. let offset = 0;
  1268. for (i = 0; i < length; ++i) {
  1269. const sourceIndices = instances[i][propertyName].indices;
  1270. const sourceIndicesLen = sourceIndices.length;
  1271. for (k = 0; k < sourceIndicesLen; ++k) {
  1272. destIndices[destOffset++] = offset + sourceIndices[k];
  1273. }
  1274. offset += GeometryAttribute.Geometry.computeNumberOfVertices(instances[i][propertyName]);
  1275. }
  1276. indices = destIndices;
  1277. }
  1278. // Create bounding sphere that includes all instances
  1279. let center = new Matrix2.Cartesian3();
  1280. let radius = 0.0;
  1281. let bs;
  1282. for (i = 0; i < length; ++i) {
  1283. bs = instances[i][propertyName].boundingSphere;
  1284. if (!defaultValue.defined(bs)) {
  1285. // If any geometries have an undefined bounding sphere, then so does the combined geometry
  1286. center = undefined;
  1287. break;
  1288. }
  1289. Matrix2.Cartesian3.add(bs.center, center, center);
  1290. }
  1291. if (defaultValue.defined(center)) {
  1292. Matrix2.Cartesian3.divideByScalar(center, length, center);
  1293. for (i = 0; i < length; ++i) {
  1294. bs = instances[i][propertyName].boundingSphere;
  1295. const tempRadius =
  1296. Matrix2.Cartesian3.magnitude(
  1297. Matrix2.Cartesian3.subtract(bs.center, center, tempScratch)
  1298. ) + bs.radius;
  1299. if (tempRadius > radius) {
  1300. radius = tempRadius;
  1301. }
  1302. }
  1303. }
  1304. return new GeometryAttribute.Geometry({
  1305. attributes: attributes,
  1306. indices: indices,
  1307. primitiveType: primitiveType,
  1308. boundingSphere: defaultValue.defined(center)
  1309. ? new Transforms.BoundingSphere(center, radius)
  1310. : undefined,
  1311. });
  1312. }
  1313. /**
  1314. * Combines geometry from several {@link GeometryInstance} objects into one geometry.
  1315. * This concatenates the attributes, concatenates and adjusts the indices, and creates
  1316. * a bounding sphere encompassing all instances.
  1317. * <p>
  1318. * If the instances do not have the same attributes, a subset of attributes common
  1319. * to all instances is used, and the others are ignored.
  1320. * </p>
  1321. * <p>
  1322. * This is used by {@link Primitive} to efficiently render a large amount of static data.
  1323. * </p>
  1324. *
  1325. * @private
  1326. *
  1327. * @param {GeometryInstance[]} [instances] The array of {@link GeometryInstance} objects whose geometry will be combined.
  1328. * @returns {Geometry} A single geometry created from the provided geometry instances.
  1329. *
  1330. * @exception {DeveloperError} All instances must have the same modelMatrix.
  1331. * @exception {DeveloperError} All instance geometries must have an indices or not have one.
  1332. * @exception {DeveloperError} All instance geometries must have the same primitiveType.
  1333. *
  1334. *
  1335. * @example
  1336. * for (let i = 0; i < instances.length; ++i) {
  1337. * Cesium.GeometryPipeline.transformToWorldCoordinates(instances[i]);
  1338. * }
  1339. * const geometries = Cesium.GeometryPipeline.combineInstances(instances);
  1340. *
  1341. * @see GeometryPipeline.transformToWorldCoordinates
  1342. */
  1343. GeometryPipeline.combineInstances = function (instances) {
  1344. //>>includeStart('debug', pragmas.debug);
  1345. if (!defaultValue.defined(instances) || instances.length < 1) {
  1346. throw new RuntimeError.DeveloperError(
  1347. "instances is required and must have length greater than zero."
  1348. );
  1349. }
  1350. //>>includeEnd('debug');
  1351. const instanceGeometry = [];
  1352. const instanceSplitGeometry = [];
  1353. const length = instances.length;
  1354. for (let i = 0; i < length; ++i) {
  1355. const instance = instances[i];
  1356. if (defaultValue.defined(instance.geometry)) {
  1357. instanceGeometry.push(instance);
  1358. } else if (
  1359. defaultValue.defined(instance.westHemisphereGeometry) &&
  1360. defaultValue.defined(instance.eastHemisphereGeometry)
  1361. ) {
  1362. instanceSplitGeometry.push(instance);
  1363. }
  1364. }
  1365. const geometries = [];
  1366. if (instanceGeometry.length > 0) {
  1367. geometries.push(combineGeometries(instanceGeometry, "geometry"));
  1368. }
  1369. if (instanceSplitGeometry.length > 0) {
  1370. geometries.push(
  1371. combineGeometries(instanceSplitGeometry, "westHemisphereGeometry")
  1372. );
  1373. geometries.push(
  1374. combineGeometries(instanceSplitGeometry, "eastHemisphereGeometry")
  1375. );
  1376. }
  1377. return geometries;
  1378. };
  1379. const normal = new Matrix2.Cartesian3();
  1380. const v0 = new Matrix2.Cartesian3();
  1381. const v1 = new Matrix2.Cartesian3();
  1382. const v2 = new Matrix2.Cartesian3();
  1383. /**
  1384. * Computes per-vertex normals for a geometry containing <code>TRIANGLES</code> by averaging the normals of
  1385. * all triangles incident to the vertex. The result is a new <code>normal</code> attribute added to the geometry.
  1386. * This assumes a counter-clockwise winding order.
  1387. *
  1388. * @param {Geometry} geometry The geometry to modify.
  1389. * @returns {Geometry} The modified <code>geometry</code> argument with the computed <code>normal</code> attribute.
  1390. *
  1391. * @exception {DeveloperError} geometry.indices length must be greater than 0 and be a multiple of 3.
  1392. * @exception {DeveloperError} geometry.primitiveType must be {@link PrimitiveType.TRIANGLES}.
  1393. *
  1394. * @example
  1395. * Cesium.GeometryPipeline.computeNormal(geometry);
  1396. */
  1397. GeometryPipeline.computeNormal = function (geometry) {
  1398. //>>includeStart('debug', pragmas.debug);
  1399. if (!defaultValue.defined(geometry)) {
  1400. throw new RuntimeError.DeveloperError("geometry is required.");
  1401. }
  1402. if (
  1403. !defaultValue.defined(geometry.attributes.position) ||
  1404. !defaultValue.defined(geometry.attributes.position.values)
  1405. ) {
  1406. throw new RuntimeError.DeveloperError(
  1407. "geometry.attributes.position.values is required."
  1408. );
  1409. }
  1410. if (!defaultValue.defined(geometry.indices)) {
  1411. throw new RuntimeError.DeveloperError("geometry.indices is required.");
  1412. }
  1413. if (geometry.indices.length < 2 || geometry.indices.length % 3 !== 0) {
  1414. throw new RuntimeError.DeveloperError(
  1415. "geometry.indices length must be greater than 0 and be a multiple of 3."
  1416. );
  1417. }
  1418. if (geometry.primitiveType !== GeometryAttribute.PrimitiveType.TRIANGLES) {
  1419. throw new RuntimeError.DeveloperError(
  1420. "geometry.primitiveType must be PrimitiveType.TRIANGLES."
  1421. );
  1422. }
  1423. //>>includeEnd('debug');
  1424. const indices = geometry.indices;
  1425. const attributes = geometry.attributes;
  1426. const vertices = attributes.position.values;
  1427. const numVertices = attributes.position.values.length / 3;
  1428. const numIndices = indices.length;
  1429. const normalsPerVertex = new Array(numVertices);
  1430. const normalsPerTriangle = new Array(numIndices / 3);
  1431. const normalIndices = new Array(numIndices);
  1432. let i;
  1433. for (i = 0; i < numVertices; i++) {
  1434. normalsPerVertex[i] = {
  1435. indexOffset: 0,
  1436. count: 0,
  1437. currentCount: 0,
  1438. };
  1439. }
  1440. let j = 0;
  1441. for (i = 0; i < numIndices; i += 3) {
  1442. const i0 = indices[i];
  1443. const i1 = indices[i + 1];
  1444. const i2 = indices[i + 2];
  1445. const i03 = i0 * 3;
  1446. const i13 = i1 * 3;
  1447. const i23 = i2 * 3;
  1448. v0.x = vertices[i03];
  1449. v0.y = vertices[i03 + 1];
  1450. v0.z = vertices[i03 + 2];
  1451. v1.x = vertices[i13];
  1452. v1.y = vertices[i13 + 1];
  1453. v1.z = vertices[i13 + 2];
  1454. v2.x = vertices[i23];
  1455. v2.y = vertices[i23 + 1];
  1456. v2.z = vertices[i23 + 2];
  1457. normalsPerVertex[i0].count++;
  1458. normalsPerVertex[i1].count++;
  1459. normalsPerVertex[i2].count++;
  1460. Matrix2.Cartesian3.subtract(v1, v0, v1);
  1461. Matrix2.Cartesian3.subtract(v2, v0, v2);
  1462. normalsPerTriangle[j] = Matrix2.Cartesian3.cross(v1, v2, new Matrix2.Cartesian3());
  1463. j++;
  1464. }
  1465. let indexOffset = 0;
  1466. for (i = 0; i < numVertices; i++) {
  1467. normalsPerVertex[i].indexOffset += indexOffset;
  1468. indexOffset += normalsPerVertex[i].count;
  1469. }
  1470. j = 0;
  1471. let vertexNormalData;
  1472. for (i = 0; i < numIndices; i += 3) {
  1473. vertexNormalData = normalsPerVertex[indices[i]];
  1474. let index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  1475. normalIndices[index] = j;
  1476. vertexNormalData.currentCount++;
  1477. vertexNormalData = normalsPerVertex[indices[i + 1]];
  1478. index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  1479. normalIndices[index] = j;
  1480. vertexNormalData.currentCount++;
  1481. vertexNormalData = normalsPerVertex[indices[i + 2]];
  1482. index = vertexNormalData.indexOffset + vertexNormalData.currentCount;
  1483. normalIndices[index] = j;
  1484. vertexNormalData.currentCount++;
  1485. j++;
  1486. }
  1487. const normalValues = new Float32Array(numVertices * 3);
  1488. for (i = 0; i < numVertices; i++) {
  1489. const i3 = i * 3;
  1490. vertexNormalData = normalsPerVertex[i];
  1491. Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO, normal);
  1492. if (vertexNormalData.count > 0) {
  1493. for (j = 0; j < vertexNormalData.count; j++) {
  1494. Matrix2.Cartesian3.add(
  1495. normal,
  1496. normalsPerTriangle[normalIndices[vertexNormalData.indexOffset + j]],
  1497. normal
  1498. );
  1499. }
  1500. // We can run into an issue where a vertex is used with 2 primitives that have opposite winding order.
  1501. if (
  1502. Matrix2.Cartesian3.equalsEpsilon(Matrix2.Cartesian3.ZERO, normal, ComponentDatatype.CesiumMath.EPSILON10)
  1503. ) {
  1504. Matrix2.Cartesian3.clone(
  1505. normalsPerTriangle[normalIndices[vertexNormalData.indexOffset]],
  1506. normal
  1507. );
  1508. }
  1509. }
  1510. // We end up with a zero vector probably because of a degenerate triangle
  1511. if (
  1512. Matrix2.Cartesian3.equalsEpsilon(Matrix2.Cartesian3.ZERO, normal, ComponentDatatype.CesiumMath.EPSILON10)
  1513. ) {
  1514. // Default to (0,0,1)
  1515. normal.z = 1.0;
  1516. }
  1517. Matrix2.Cartesian3.normalize(normal, normal);
  1518. normalValues[i3] = normal.x;
  1519. normalValues[i3 + 1] = normal.y;
  1520. normalValues[i3 + 2] = normal.z;
  1521. }
  1522. geometry.attributes.normal = new GeometryAttribute.GeometryAttribute({
  1523. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1524. componentsPerAttribute: 3,
  1525. values: normalValues,
  1526. });
  1527. return geometry;
  1528. };
  1529. const normalScratch = new Matrix2.Cartesian3();
  1530. const normalScale = new Matrix2.Cartesian3();
  1531. const tScratch = new Matrix2.Cartesian3();
  1532. /**
  1533. * Computes per-vertex tangents and bitangents for a geometry containing <code>TRIANGLES</code>.
  1534. * The result is new <code>tangent</code> and <code>bitangent</code> attributes added to the geometry.
  1535. * This assumes a counter-clockwise winding order.
  1536. * <p>
  1537. * Based on <a href="http://www.terathon.com/code/tangent.html">Computing Tangent Space Basis Vectors
  1538. * for an Arbitrary Mesh</a> by Eric Lengyel.
  1539. * </p>
  1540. *
  1541. * @param {Geometry} geometry The geometry to modify.
  1542. * @returns {Geometry} The modified <code>geometry</code> argument with the computed <code>tangent</code> and <code>bitangent</code> attributes.
  1543. *
  1544. * @exception {DeveloperError} geometry.indices length must be greater than 0 and be a multiple of 3.
  1545. * @exception {DeveloperError} geometry.primitiveType must be {@link PrimitiveType.TRIANGLES}.
  1546. *
  1547. * @example
  1548. * Cesium.GeometryPipeline.computeTangentAndBiTangent(geometry);
  1549. */
  1550. GeometryPipeline.computeTangentAndBitangent = function (geometry) {
  1551. //>>includeStart('debug', pragmas.debug);
  1552. if (!defaultValue.defined(geometry)) {
  1553. throw new RuntimeError.DeveloperError("geometry is required.");
  1554. }
  1555. //>>includeEnd('debug');
  1556. const attributes = geometry.attributes;
  1557. const indices = geometry.indices;
  1558. //>>includeStart('debug', pragmas.debug);
  1559. if (!defaultValue.defined(attributes.position) || !defaultValue.defined(attributes.position.values)) {
  1560. throw new RuntimeError.DeveloperError(
  1561. "geometry.attributes.position.values is required."
  1562. );
  1563. }
  1564. if (!defaultValue.defined(attributes.normal) || !defaultValue.defined(attributes.normal.values)) {
  1565. throw new RuntimeError.DeveloperError("geometry.attributes.normal.values is required.");
  1566. }
  1567. if (!defaultValue.defined(attributes.st) || !defaultValue.defined(attributes.st.values)) {
  1568. throw new RuntimeError.DeveloperError("geometry.attributes.st.values is required.");
  1569. }
  1570. if (!defaultValue.defined(indices)) {
  1571. throw new RuntimeError.DeveloperError("geometry.indices is required.");
  1572. }
  1573. if (indices.length < 2 || indices.length % 3 !== 0) {
  1574. throw new RuntimeError.DeveloperError(
  1575. "geometry.indices length must be greater than 0 and be a multiple of 3."
  1576. );
  1577. }
  1578. if (geometry.primitiveType !== GeometryAttribute.PrimitiveType.TRIANGLES) {
  1579. throw new RuntimeError.DeveloperError(
  1580. "geometry.primitiveType must be PrimitiveType.TRIANGLES."
  1581. );
  1582. }
  1583. //>>includeEnd('debug');
  1584. const vertices = geometry.attributes.position.values;
  1585. const normals = geometry.attributes.normal.values;
  1586. const st = geometry.attributes.st.values;
  1587. const numVertices = geometry.attributes.position.values.length / 3;
  1588. const numIndices = indices.length;
  1589. const tan1 = new Array(numVertices * 3);
  1590. let i;
  1591. for (i = 0; i < tan1.length; i++) {
  1592. tan1[i] = 0;
  1593. }
  1594. let i03;
  1595. let i13;
  1596. let i23;
  1597. for (i = 0; i < numIndices; i += 3) {
  1598. const i0 = indices[i];
  1599. const i1 = indices[i + 1];
  1600. const i2 = indices[i + 2];
  1601. i03 = i0 * 3;
  1602. i13 = i1 * 3;
  1603. i23 = i2 * 3;
  1604. const i02 = i0 * 2;
  1605. const i12 = i1 * 2;
  1606. const i22 = i2 * 2;
  1607. const ux = vertices[i03];
  1608. const uy = vertices[i03 + 1];
  1609. const uz = vertices[i03 + 2];
  1610. const wx = st[i02];
  1611. const wy = st[i02 + 1];
  1612. const t1 = st[i12 + 1] - wy;
  1613. const t2 = st[i22 + 1] - wy;
  1614. const r = 1.0 / ((st[i12] - wx) * t2 - (st[i22] - wx) * t1);
  1615. const sdirx = (t2 * (vertices[i13] - ux) - t1 * (vertices[i23] - ux)) * r;
  1616. const sdiry =
  1617. (t2 * (vertices[i13 + 1] - uy) - t1 * (vertices[i23 + 1] - uy)) * r;
  1618. const sdirz =
  1619. (t2 * (vertices[i13 + 2] - uz) - t1 * (vertices[i23 + 2] - uz)) * r;
  1620. tan1[i03] += sdirx;
  1621. tan1[i03 + 1] += sdiry;
  1622. tan1[i03 + 2] += sdirz;
  1623. tan1[i13] += sdirx;
  1624. tan1[i13 + 1] += sdiry;
  1625. tan1[i13 + 2] += sdirz;
  1626. tan1[i23] += sdirx;
  1627. tan1[i23 + 1] += sdiry;
  1628. tan1[i23 + 2] += sdirz;
  1629. }
  1630. const tangentValues = new Float32Array(numVertices * 3);
  1631. const bitangentValues = new Float32Array(numVertices * 3);
  1632. for (i = 0; i < numVertices; i++) {
  1633. i03 = i * 3;
  1634. i13 = i03 + 1;
  1635. i23 = i03 + 2;
  1636. const n = Matrix2.Cartesian3.fromArray(normals, i03, normalScratch);
  1637. const t = Matrix2.Cartesian3.fromArray(tan1, i03, tScratch);
  1638. const scalar = Matrix2.Cartesian3.dot(n, t);
  1639. Matrix2.Cartesian3.multiplyByScalar(n, scalar, normalScale);
  1640. Matrix2.Cartesian3.normalize(Matrix2.Cartesian3.subtract(t, normalScale, t), t);
  1641. tangentValues[i03] = t.x;
  1642. tangentValues[i13] = t.y;
  1643. tangentValues[i23] = t.z;
  1644. Matrix2.Cartesian3.normalize(Matrix2.Cartesian3.cross(n, t, t), t);
  1645. bitangentValues[i03] = t.x;
  1646. bitangentValues[i13] = t.y;
  1647. bitangentValues[i23] = t.z;
  1648. }
  1649. geometry.attributes.tangent = new GeometryAttribute.GeometryAttribute({
  1650. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1651. componentsPerAttribute: 3,
  1652. values: tangentValues,
  1653. });
  1654. geometry.attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  1655. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1656. componentsPerAttribute: 3,
  1657. values: bitangentValues,
  1658. });
  1659. return geometry;
  1660. };
  1661. const scratchCartesian2 = new Matrix2.Cartesian2();
  1662. const toEncode1 = new Matrix2.Cartesian3();
  1663. const toEncode2 = new Matrix2.Cartesian3();
  1664. const toEncode3 = new Matrix2.Cartesian3();
  1665. let encodeResult2 = new Matrix2.Cartesian2();
  1666. /**
  1667. * Compresses and packs geometry normal attribute values to save memory.
  1668. *
  1669. * @param {Geometry} geometry The geometry to modify.
  1670. * @returns {Geometry} The modified <code>geometry</code> argument, with its normals compressed and packed.
  1671. *
  1672. * @example
  1673. * geometry = Cesium.GeometryPipeline.compressVertices(geometry);
  1674. */
  1675. GeometryPipeline.compressVertices = function (geometry) {
  1676. //>>includeStart('debug', pragmas.debug);
  1677. if (!defaultValue.defined(geometry)) {
  1678. throw new RuntimeError.DeveloperError("geometry is required.");
  1679. }
  1680. //>>includeEnd('debug');
  1681. const extrudeAttribute = geometry.attributes.extrudeDirection;
  1682. let i;
  1683. let numVertices;
  1684. if (defaultValue.defined(extrudeAttribute)) {
  1685. //only shadow volumes use extrudeDirection, and shadow volumes use vertexFormat: POSITION_ONLY so we don't need to check other attributes
  1686. const extrudeDirections = extrudeAttribute.values;
  1687. numVertices = extrudeDirections.length / 3.0;
  1688. const compressedDirections = new Float32Array(numVertices * 2);
  1689. let i2 = 0;
  1690. for (i = 0; i < numVertices; ++i) {
  1691. Matrix2.Cartesian3.fromArray(extrudeDirections, i * 3.0, toEncode1);
  1692. if (Matrix2.Cartesian3.equals(toEncode1, Matrix2.Cartesian3.ZERO)) {
  1693. i2 += 2;
  1694. continue;
  1695. }
  1696. encodeResult2 = AttributeCompression.AttributeCompression.octEncodeInRange(
  1697. toEncode1,
  1698. 65535,
  1699. encodeResult2
  1700. );
  1701. compressedDirections[i2++] = encodeResult2.x;
  1702. compressedDirections[i2++] = encodeResult2.y;
  1703. }
  1704. geometry.attributes.compressedAttributes = new GeometryAttribute.GeometryAttribute({
  1705. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1706. componentsPerAttribute: 2,
  1707. values: compressedDirections,
  1708. });
  1709. delete geometry.attributes.extrudeDirection;
  1710. return geometry;
  1711. }
  1712. const normalAttribute = geometry.attributes.normal;
  1713. const stAttribute = geometry.attributes.st;
  1714. const hasNormal = defaultValue.defined(normalAttribute);
  1715. const hasSt = defaultValue.defined(stAttribute);
  1716. if (!hasNormal && !hasSt) {
  1717. return geometry;
  1718. }
  1719. const tangentAttribute = geometry.attributes.tangent;
  1720. const bitangentAttribute = geometry.attributes.bitangent;
  1721. const hasTangent = defaultValue.defined(tangentAttribute);
  1722. const hasBitangent = defaultValue.defined(bitangentAttribute);
  1723. let normals;
  1724. let st;
  1725. let tangents;
  1726. let bitangents;
  1727. if (hasNormal) {
  1728. normals = normalAttribute.values;
  1729. }
  1730. if (hasSt) {
  1731. st = stAttribute.values;
  1732. }
  1733. if (hasTangent) {
  1734. tangents = tangentAttribute.values;
  1735. }
  1736. if (hasBitangent) {
  1737. bitangents = bitangentAttribute.values;
  1738. }
  1739. const length = hasNormal ? normals.length : st.length;
  1740. const numComponents = hasNormal ? 3.0 : 2.0;
  1741. numVertices = length / numComponents;
  1742. let compressedLength = numVertices;
  1743. let numCompressedComponents = hasSt && hasNormal ? 2.0 : 1.0;
  1744. numCompressedComponents += hasTangent || hasBitangent ? 1.0 : 0.0;
  1745. compressedLength *= numCompressedComponents;
  1746. const compressedAttributes = new Float32Array(compressedLength);
  1747. let normalIndex = 0;
  1748. for (i = 0; i < numVertices; ++i) {
  1749. if (hasSt) {
  1750. Matrix2.Cartesian2.fromArray(st, i * 2.0, scratchCartesian2);
  1751. compressedAttributes[
  1752. normalIndex++
  1753. ] = AttributeCompression.AttributeCompression.compressTextureCoordinates(scratchCartesian2);
  1754. }
  1755. const index = i * 3.0;
  1756. if (hasNormal && defaultValue.defined(tangents) && defaultValue.defined(bitangents)) {
  1757. Matrix2.Cartesian3.fromArray(normals, index, toEncode1);
  1758. Matrix2.Cartesian3.fromArray(tangents, index, toEncode2);
  1759. Matrix2.Cartesian3.fromArray(bitangents, index, toEncode3);
  1760. AttributeCompression.AttributeCompression.octPack(
  1761. toEncode1,
  1762. toEncode2,
  1763. toEncode3,
  1764. scratchCartesian2
  1765. );
  1766. compressedAttributes[normalIndex++] = scratchCartesian2.x;
  1767. compressedAttributes[normalIndex++] = scratchCartesian2.y;
  1768. } else {
  1769. if (hasNormal) {
  1770. Matrix2.Cartesian3.fromArray(normals, index, toEncode1);
  1771. compressedAttributes[
  1772. normalIndex++
  1773. ] = AttributeCompression.AttributeCompression.octEncodeFloat(toEncode1);
  1774. }
  1775. if (hasTangent) {
  1776. Matrix2.Cartesian3.fromArray(tangents, index, toEncode1);
  1777. compressedAttributes[
  1778. normalIndex++
  1779. ] = AttributeCompression.AttributeCompression.octEncodeFloat(toEncode1);
  1780. }
  1781. if (hasBitangent) {
  1782. Matrix2.Cartesian3.fromArray(bitangents, index, toEncode1);
  1783. compressedAttributes[
  1784. normalIndex++
  1785. ] = AttributeCompression.AttributeCompression.octEncodeFloat(toEncode1);
  1786. }
  1787. }
  1788. }
  1789. geometry.attributes.compressedAttributes = new GeometryAttribute.GeometryAttribute({
  1790. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1791. componentsPerAttribute: numCompressedComponents,
  1792. values: compressedAttributes,
  1793. });
  1794. if (hasNormal) {
  1795. delete geometry.attributes.normal;
  1796. }
  1797. if (hasSt) {
  1798. delete geometry.attributes.st;
  1799. }
  1800. if (hasBitangent) {
  1801. delete geometry.attributes.bitangent;
  1802. }
  1803. if (hasTangent) {
  1804. delete geometry.attributes.tangent;
  1805. }
  1806. return geometry;
  1807. };
  1808. function indexTriangles(geometry) {
  1809. if (defaultValue.defined(geometry.indices)) {
  1810. return geometry;
  1811. }
  1812. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1813. //>>includeStart('debug', pragmas.debug);
  1814. if (numberOfVertices < 3) {
  1815. throw new RuntimeError.DeveloperError("The number of vertices must be at least three.");
  1816. }
  1817. if (numberOfVertices % 3 !== 0) {
  1818. throw new RuntimeError.DeveloperError(
  1819. "The number of vertices must be a multiple of three."
  1820. );
  1821. }
  1822. //>>includeEnd('debug');
  1823. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1824. numberOfVertices,
  1825. numberOfVertices
  1826. );
  1827. for (let i = 0; i < numberOfVertices; ++i) {
  1828. indices[i] = i;
  1829. }
  1830. geometry.indices = indices;
  1831. return geometry;
  1832. }
  1833. function indexTriangleFan(geometry) {
  1834. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1835. //>>includeStart('debug', pragmas.debug);
  1836. if (numberOfVertices < 3) {
  1837. throw new RuntimeError.DeveloperError("The number of vertices must be at least three.");
  1838. }
  1839. //>>includeEnd('debug');
  1840. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1841. numberOfVertices,
  1842. (numberOfVertices - 2) * 3
  1843. );
  1844. indices[0] = 1;
  1845. indices[1] = 0;
  1846. indices[2] = 2;
  1847. let indicesIndex = 3;
  1848. for (let i = 3; i < numberOfVertices; ++i) {
  1849. indices[indicesIndex++] = i - 1;
  1850. indices[indicesIndex++] = 0;
  1851. indices[indicesIndex++] = i;
  1852. }
  1853. geometry.indices = indices;
  1854. geometry.primitiveType = GeometryAttribute.PrimitiveType.TRIANGLES;
  1855. return geometry;
  1856. }
  1857. function indexTriangleStrip(geometry) {
  1858. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1859. //>>includeStart('debug', pragmas.debug);
  1860. if (numberOfVertices < 3) {
  1861. throw new RuntimeError.DeveloperError("The number of vertices must be at least 3.");
  1862. }
  1863. //>>includeEnd('debug');
  1864. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1865. numberOfVertices,
  1866. (numberOfVertices - 2) * 3
  1867. );
  1868. indices[0] = 0;
  1869. indices[1] = 1;
  1870. indices[2] = 2;
  1871. if (numberOfVertices > 3) {
  1872. indices[3] = 0;
  1873. indices[4] = 2;
  1874. indices[5] = 3;
  1875. }
  1876. let indicesIndex = 6;
  1877. for (let i = 3; i < numberOfVertices - 1; i += 2) {
  1878. indices[indicesIndex++] = i;
  1879. indices[indicesIndex++] = i - 1;
  1880. indices[indicesIndex++] = i + 1;
  1881. if (i + 2 < numberOfVertices) {
  1882. indices[indicesIndex++] = i;
  1883. indices[indicesIndex++] = i + 1;
  1884. indices[indicesIndex++] = i + 2;
  1885. }
  1886. }
  1887. geometry.indices = indices;
  1888. geometry.primitiveType = GeometryAttribute.PrimitiveType.TRIANGLES;
  1889. return geometry;
  1890. }
  1891. function indexLines(geometry) {
  1892. if (defaultValue.defined(geometry.indices)) {
  1893. return geometry;
  1894. }
  1895. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1896. //>>includeStart('debug', pragmas.debug);
  1897. if (numberOfVertices < 2) {
  1898. throw new RuntimeError.DeveloperError("The number of vertices must be at least two.");
  1899. }
  1900. if (numberOfVertices % 2 !== 0) {
  1901. throw new RuntimeError.DeveloperError("The number of vertices must be a multiple of 2.");
  1902. }
  1903. //>>includeEnd('debug');
  1904. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1905. numberOfVertices,
  1906. numberOfVertices
  1907. );
  1908. for (let i = 0; i < numberOfVertices; ++i) {
  1909. indices[i] = i;
  1910. }
  1911. geometry.indices = indices;
  1912. return geometry;
  1913. }
  1914. function indexLineStrip(geometry) {
  1915. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1916. //>>includeStart('debug', pragmas.debug);
  1917. if (numberOfVertices < 2) {
  1918. throw new RuntimeError.DeveloperError("The number of vertices must be at least two.");
  1919. }
  1920. //>>includeEnd('debug');
  1921. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1922. numberOfVertices,
  1923. (numberOfVertices - 1) * 2
  1924. );
  1925. indices[0] = 0;
  1926. indices[1] = 1;
  1927. let indicesIndex = 2;
  1928. for (let i = 2; i < numberOfVertices; ++i) {
  1929. indices[indicesIndex++] = i - 1;
  1930. indices[indicesIndex++] = i;
  1931. }
  1932. geometry.indices = indices;
  1933. geometry.primitiveType = GeometryAttribute.PrimitiveType.LINES;
  1934. return geometry;
  1935. }
  1936. function indexLineLoop(geometry) {
  1937. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  1938. //>>includeStart('debug', pragmas.debug);
  1939. if (numberOfVertices < 2) {
  1940. throw new RuntimeError.DeveloperError("The number of vertices must be at least two.");
  1941. }
  1942. //>>includeEnd('debug');
  1943. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  1944. numberOfVertices,
  1945. numberOfVertices * 2
  1946. );
  1947. indices[0] = 0;
  1948. indices[1] = 1;
  1949. let indicesIndex = 2;
  1950. for (let i = 2; i < numberOfVertices; ++i) {
  1951. indices[indicesIndex++] = i - 1;
  1952. indices[indicesIndex++] = i;
  1953. }
  1954. indices[indicesIndex++] = numberOfVertices - 1;
  1955. indices[indicesIndex] = 0;
  1956. geometry.indices = indices;
  1957. geometry.primitiveType = GeometryAttribute.PrimitiveType.LINES;
  1958. return geometry;
  1959. }
  1960. function indexPrimitive(geometry) {
  1961. switch (geometry.primitiveType) {
  1962. case GeometryAttribute.PrimitiveType.TRIANGLE_FAN:
  1963. return indexTriangleFan(geometry);
  1964. case GeometryAttribute.PrimitiveType.TRIANGLE_STRIP:
  1965. return indexTriangleStrip(geometry);
  1966. case GeometryAttribute.PrimitiveType.TRIANGLES:
  1967. return indexTriangles(geometry);
  1968. case GeometryAttribute.PrimitiveType.LINE_STRIP:
  1969. return indexLineStrip(geometry);
  1970. case GeometryAttribute.PrimitiveType.LINE_LOOP:
  1971. return indexLineLoop(geometry);
  1972. case GeometryAttribute.PrimitiveType.LINES:
  1973. return indexLines(geometry);
  1974. }
  1975. return geometry;
  1976. }
  1977. function offsetPointFromXZPlane(p, isBehind) {
  1978. if (Math.abs(p.y) < ComponentDatatype.CesiumMath.EPSILON6) {
  1979. if (isBehind) {
  1980. p.y = -ComponentDatatype.CesiumMath.EPSILON6;
  1981. } else {
  1982. p.y = ComponentDatatype.CesiumMath.EPSILON6;
  1983. }
  1984. }
  1985. }
  1986. function offsetTriangleFromXZPlane(p0, p1, p2) {
  1987. if (p0.y !== 0.0 && p1.y !== 0.0 && p2.y !== 0.0) {
  1988. offsetPointFromXZPlane(p0, p0.y < 0.0);
  1989. offsetPointFromXZPlane(p1, p1.y < 0.0);
  1990. offsetPointFromXZPlane(p2, p2.y < 0.0);
  1991. return;
  1992. }
  1993. const p0y = Math.abs(p0.y);
  1994. const p1y = Math.abs(p1.y);
  1995. const p2y = Math.abs(p2.y);
  1996. let sign;
  1997. if (p0y > p1y) {
  1998. if (p0y > p2y) {
  1999. sign = ComponentDatatype.CesiumMath.sign(p0.y);
  2000. } else {
  2001. sign = ComponentDatatype.CesiumMath.sign(p2.y);
  2002. }
  2003. } else if (p1y > p2y) {
  2004. sign = ComponentDatatype.CesiumMath.sign(p1.y);
  2005. } else {
  2006. sign = ComponentDatatype.CesiumMath.sign(p2.y);
  2007. }
  2008. const isBehind = sign < 0.0;
  2009. offsetPointFromXZPlane(p0, isBehind);
  2010. offsetPointFromXZPlane(p1, isBehind);
  2011. offsetPointFromXZPlane(p2, isBehind);
  2012. }
  2013. const c3 = new Matrix2.Cartesian3();
  2014. function getXZIntersectionOffsetPoints(p, p1, u1, v1) {
  2015. Matrix2.Cartesian3.add(
  2016. p,
  2017. Matrix2.Cartesian3.multiplyByScalar(
  2018. Matrix2.Cartesian3.subtract(p1, p, c3),
  2019. p.y / (p.y - p1.y),
  2020. c3
  2021. ),
  2022. u1
  2023. );
  2024. Matrix2.Cartesian3.clone(u1, v1);
  2025. offsetPointFromXZPlane(u1, true);
  2026. offsetPointFromXZPlane(v1, false);
  2027. }
  2028. const u1 = new Matrix2.Cartesian3();
  2029. const u2 = new Matrix2.Cartesian3();
  2030. const q1 = new Matrix2.Cartesian3();
  2031. const q2 = new Matrix2.Cartesian3();
  2032. const splitTriangleResult = {
  2033. positions: new Array(7),
  2034. indices: new Array(3 * 3),
  2035. };
  2036. function splitTriangle(p0, p1, p2) {
  2037. // In WGS84 coordinates, for a triangle approximately on the
  2038. // ellipsoid to cross the IDL, first it needs to be on the
  2039. // negative side of the plane x = 0.
  2040. if (p0.x >= 0.0 || p1.x >= 0.0 || p2.x >= 0.0) {
  2041. return undefined;
  2042. }
  2043. offsetTriangleFromXZPlane(p0, p1, p2);
  2044. const p0Behind = p0.y < 0.0;
  2045. const p1Behind = p1.y < 0.0;
  2046. const p2Behind = p2.y < 0.0;
  2047. let numBehind = 0;
  2048. numBehind += p0Behind ? 1 : 0;
  2049. numBehind += p1Behind ? 1 : 0;
  2050. numBehind += p2Behind ? 1 : 0;
  2051. const indices = splitTriangleResult.indices;
  2052. if (numBehind === 1) {
  2053. indices[1] = 3;
  2054. indices[2] = 4;
  2055. indices[5] = 6;
  2056. indices[7] = 6;
  2057. indices[8] = 5;
  2058. if (p0Behind) {
  2059. getXZIntersectionOffsetPoints(p0, p1, u1, q1);
  2060. getXZIntersectionOffsetPoints(p0, p2, u2, q2);
  2061. indices[0] = 0;
  2062. indices[3] = 1;
  2063. indices[4] = 2;
  2064. indices[6] = 1;
  2065. } else if (p1Behind) {
  2066. getXZIntersectionOffsetPoints(p1, p2, u1, q1);
  2067. getXZIntersectionOffsetPoints(p1, p0, u2, q2);
  2068. indices[0] = 1;
  2069. indices[3] = 2;
  2070. indices[4] = 0;
  2071. indices[6] = 2;
  2072. } else if (p2Behind) {
  2073. getXZIntersectionOffsetPoints(p2, p0, u1, q1);
  2074. getXZIntersectionOffsetPoints(p2, p1, u2, q2);
  2075. indices[0] = 2;
  2076. indices[3] = 0;
  2077. indices[4] = 1;
  2078. indices[6] = 0;
  2079. }
  2080. } else if (numBehind === 2) {
  2081. indices[2] = 4;
  2082. indices[4] = 4;
  2083. indices[5] = 3;
  2084. indices[7] = 5;
  2085. indices[8] = 6;
  2086. if (!p0Behind) {
  2087. getXZIntersectionOffsetPoints(p0, p1, u1, q1);
  2088. getXZIntersectionOffsetPoints(p0, p2, u2, q2);
  2089. indices[0] = 1;
  2090. indices[1] = 2;
  2091. indices[3] = 1;
  2092. indices[6] = 0;
  2093. } else if (!p1Behind) {
  2094. getXZIntersectionOffsetPoints(p1, p2, u1, q1);
  2095. getXZIntersectionOffsetPoints(p1, p0, u2, q2);
  2096. indices[0] = 2;
  2097. indices[1] = 0;
  2098. indices[3] = 2;
  2099. indices[6] = 1;
  2100. } else if (!p2Behind) {
  2101. getXZIntersectionOffsetPoints(p2, p0, u1, q1);
  2102. getXZIntersectionOffsetPoints(p2, p1, u2, q2);
  2103. indices[0] = 0;
  2104. indices[1] = 1;
  2105. indices[3] = 0;
  2106. indices[6] = 2;
  2107. }
  2108. }
  2109. const positions = splitTriangleResult.positions;
  2110. positions[0] = p0;
  2111. positions[1] = p1;
  2112. positions[2] = p2;
  2113. positions.length = 3;
  2114. if (numBehind === 1 || numBehind === 2) {
  2115. positions[3] = u1;
  2116. positions[4] = u2;
  2117. positions[5] = q1;
  2118. positions[6] = q2;
  2119. positions.length = 7;
  2120. }
  2121. return splitTriangleResult;
  2122. }
  2123. function updateGeometryAfterSplit(geometry, computeBoundingSphere) {
  2124. const attributes = geometry.attributes;
  2125. if (attributes.position.values.length === 0) {
  2126. return undefined;
  2127. }
  2128. for (const property in attributes) {
  2129. if (
  2130. attributes.hasOwnProperty(property) &&
  2131. defaultValue.defined(attributes[property]) &&
  2132. defaultValue.defined(attributes[property].values)
  2133. ) {
  2134. const attribute = attributes[property];
  2135. attribute.values = ComponentDatatype.ComponentDatatype.createTypedArray(
  2136. attribute.componentDatatype,
  2137. attribute.values
  2138. );
  2139. }
  2140. }
  2141. const numberOfVertices = GeometryAttribute.Geometry.computeNumberOfVertices(geometry);
  2142. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(
  2143. numberOfVertices,
  2144. geometry.indices
  2145. );
  2146. if (computeBoundingSphere) {
  2147. geometry.boundingSphere = Transforms.BoundingSphere.fromVertices(
  2148. attributes.position.values
  2149. );
  2150. }
  2151. return geometry;
  2152. }
  2153. function copyGeometryForSplit(geometry) {
  2154. const attributes = geometry.attributes;
  2155. const copiedAttributes = {};
  2156. for (const property in attributes) {
  2157. if (
  2158. attributes.hasOwnProperty(property) &&
  2159. defaultValue.defined(attributes[property]) &&
  2160. defaultValue.defined(attributes[property].values)
  2161. ) {
  2162. const attribute = attributes[property];
  2163. copiedAttributes[property] = new GeometryAttribute.GeometryAttribute({
  2164. componentDatatype: attribute.componentDatatype,
  2165. componentsPerAttribute: attribute.componentsPerAttribute,
  2166. normalize: attribute.normalize,
  2167. values: [],
  2168. });
  2169. }
  2170. }
  2171. return new GeometryAttribute.Geometry({
  2172. attributes: copiedAttributes,
  2173. indices: [],
  2174. primitiveType: geometry.primitiveType,
  2175. });
  2176. }
  2177. function updateInstanceAfterSplit(instance, westGeometry, eastGeometry) {
  2178. const computeBoundingSphere = defaultValue.defined(instance.geometry.boundingSphere);
  2179. westGeometry = updateGeometryAfterSplit(westGeometry, computeBoundingSphere);
  2180. eastGeometry = updateGeometryAfterSplit(eastGeometry, computeBoundingSphere);
  2181. if (defaultValue.defined(eastGeometry) && !defaultValue.defined(westGeometry)) {
  2182. instance.geometry = eastGeometry;
  2183. } else if (!defaultValue.defined(eastGeometry) && defaultValue.defined(westGeometry)) {
  2184. instance.geometry = westGeometry;
  2185. } else {
  2186. instance.westHemisphereGeometry = westGeometry;
  2187. instance.eastHemisphereGeometry = eastGeometry;
  2188. instance.geometry = undefined;
  2189. }
  2190. }
  2191. function generateBarycentricInterpolateFunction(
  2192. CartesianType,
  2193. numberOfComponents
  2194. ) {
  2195. const v0Scratch = new CartesianType();
  2196. const v1Scratch = new CartesianType();
  2197. const v2Scratch = new CartesianType();
  2198. return function (
  2199. i0,
  2200. i1,
  2201. i2,
  2202. coords,
  2203. sourceValues,
  2204. currentValues,
  2205. insertedIndex,
  2206. normalize
  2207. ) {
  2208. const v0 = CartesianType.fromArray(
  2209. sourceValues,
  2210. i0 * numberOfComponents,
  2211. v0Scratch
  2212. );
  2213. const v1 = CartesianType.fromArray(
  2214. sourceValues,
  2215. i1 * numberOfComponents,
  2216. v1Scratch
  2217. );
  2218. const v2 = CartesianType.fromArray(
  2219. sourceValues,
  2220. i2 * numberOfComponents,
  2221. v2Scratch
  2222. );
  2223. CartesianType.multiplyByScalar(v0, coords.x, v0);
  2224. CartesianType.multiplyByScalar(v1, coords.y, v1);
  2225. CartesianType.multiplyByScalar(v2, coords.z, v2);
  2226. const value = CartesianType.add(v0, v1, v0);
  2227. CartesianType.add(value, v2, value);
  2228. if (normalize) {
  2229. CartesianType.normalize(value, value);
  2230. }
  2231. CartesianType.pack(
  2232. value,
  2233. currentValues,
  2234. insertedIndex * numberOfComponents
  2235. );
  2236. };
  2237. }
  2238. const interpolateAndPackCartesian4 = generateBarycentricInterpolateFunction(
  2239. Matrix2.Cartesian4,
  2240. 4
  2241. );
  2242. const interpolateAndPackCartesian3 = generateBarycentricInterpolateFunction(
  2243. Matrix2.Cartesian3,
  2244. 3
  2245. );
  2246. const interpolateAndPackCartesian2 = generateBarycentricInterpolateFunction(
  2247. Matrix2.Cartesian2,
  2248. 2
  2249. );
  2250. const interpolateAndPackBoolean = function (
  2251. i0,
  2252. i1,
  2253. i2,
  2254. coords,
  2255. sourceValues,
  2256. currentValues,
  2257. insertedIndex
  2258. ) {
  2259. const v1 = sourceValues[i0] * coords.x;
  2260. const v2 = sourceValues[i1] * coords.y;
  2261. const v3 = sourceValues[i2] * coords.z;
  2262. currentValues[insertedIndex] = v1 + v2 + v3 > ComponentDatatype.CesiumMath.EPSILON6 ? 1 : 0;
  2263. };
  2264. const p0Scratch = new Matrix2.Cartesian3();
  2265. const p1Scratch = new Matrix2.Cartesian3();
  2266. const p2Scratch = new Matrix2.Cartesian3();
  2267. const barycentricScratch = new Matrix2.Cartesian3();
  2268. function computeTriangleAttributes(
  2269. i0,
  2270. i1,
  2271. i2,
  2272. point,
  2273. positions,
  2274. normals,
  2275. tangents,
  2276. bitangents,
  2277. texCoords,
  2278. extrudeDirections,
  2279. applyOffset,
  2280. currentAttributes,
  2281. customAttributeNames,
  2282. customAttributesLength,
  2283. allAttributes,
  2284. insertedIndex
  2285. ) {
  2286. if (
  2287. !defaultValue.defined(normals) &&
  2288. !defaultValue.defined(tangents) &&
  2289. !defaultValue.defined(bitangents) &&
  2290. !defaultValue.defined(texCoords) &&
  2291. !defaultValue.defined(extrudeDirections) &&
  2292. customAttributesLength === 0
  2293. ) {
  2294. return;
  2295. }
  2296. const p0 = Matrix2.Cartesian3.fromArray(positions, i0 * 3, p0Scratch);
  2297. const p1 = Matrix2.Cartesian3.fromArray(positions, i1 * 3, p1Scratch);
  2298. const p2 = Matrix2.Cartesian3.fromArray(positions, i2 * 3, p2Scratch);
  2299. const coords = barycentricCoordinates(point, p0, p1, p2, barycentricScratch);
  2300. if (!defaultValue.defined(coords)) {
  2301. return;
  2302. }
  2303. if (defaultValue.defined(normals)) {
  2304. interpolateAndPackCartesian3(
  2305. i0,
  2306. i1,
  2307. i2,
  2308. coords,
  2309. normals,
  2310. currentAttributes.normal.values,
  2311. insertedIndex,
  2312. true
  2313. );
  2314. }
  2315. if (defaultValue.defined(extrudeDirections)) {
  2316. const d0 = Matrix2.Cartesian3.fromArray(extrudeDirections, i0 * 3, p0Scratch);
  2317. const d1 = Matrix2.Cartesian3.fromArray(extrudeDirections, i1 * 3, p1Scratch);
  2318. const d2 = Matrix2.Cartesian3.fromArray(extrudeDirections, i2 * 3, p2Scratch);
  2319. Matrix2.Cartesian3.multiplyByScalar(d0, coords.x, d0);
  2320. Matrix2.Cartesian3.multiplyByScalar(d1, coords.y, d1);
  2321. Matrix2.Cartesian3.multiplyByScalar(d2, coords.z, d2);
  2322. let direction;
  2323. if (
  2324. !Matrix2.Cartesian3.equals(d0, Matrix2.Cartesian3.ZERO) ||
  2325. !Matrix2.Cartesian3.equals(d1, Matrix2.Cartesian3.ZERO) ||
  2326. !Matrix2.Cartesian3.equals(d2, Matrix2.Cartesian3.ZERO)
  2327. ) {
  2328. direction = Matrix2.Cartesian3.add(d0, d1, d0);
  2329. Matrix2.Cartesian3.add(direction, d2, direction);
  2330. Matrix2.Cartesian3.normalize(direction, direction);
  2331. } else {
  2332. direction = p0Scratch;
  2333. direction.x = 0;
  2334. direction.y = 0;
  2335. direction.z = 0;
  2336. }
  2337. Matrix2.Cartesian3.pack(
  2338. direction,
  2339. currentAttributes.extrudeDirection.values,
  2340. insertedIndex * 3
  2341. );
  2342. }
  2343. if (defaultValue.defined(applyOffset)) {
  2344. interpolateAndPackBoolean(
  2345. i0,
  2346. i1,
  2347. i2,
  2348. coords,
  2349. applyOffset,
  2350. currentAttributes.applyOffset.values,
  2351. insertedIndex
  2352. );
  2353. }
  2354. if (defaultValue.defined(tangents)) {
  2355. interpolateAndPackCartesian3(
  2356. i0,
  2357. i1,
  2358. i2,
  2359. coords,
  2360. tangents,
  2361. currentAttributes.tangent.values,
  2362. insertedIndex,
  2363. true
  2364. );
  2365. }
  2366. if (defaultValue.defined(bitangents)) {
  2367. interpolateAndPackCartesian3(
  2368. i0,
  2369. i1,
  2370. i2,
  2371. coords,
  2372. bitangents,
  2373. currentAttributes.bitangent.values,
  2374. insertedIndex,
  2375. true
  2376. );
  2377. }
  2378. if (defaultValue.defined(texCoords)) {
  2379. interpolateAndPackCartesian2(
  2380. i0,
  2381. i1,
  2382. i2,
  2383. coords,
  2384. texCoords,
  2385. currentAttributes.st.values,
  2386. insertedIndex
  2387. );
  2388. }
  2389. if (customAttributesLength > 0) {
  2390. for (let i = 0; i < customAttributesLength; i++) {
  2391. const attributeName = customAttributeNames[i];
  2392. genericInterpolate(
  2393. i0,
  2394. i1,
  2395. i2,
  2396. coords,
  2397. insertedIndex,
  2398. allAttributes[attributeName],
  2399. currentAttributes[attributeName]
  2400. );
  2401. }
  2402. }
  2403. }
  2404. function genericInterpolate(
  2405. i0,
  2406. i1,
  2407. i2,
  2408. coords,
  2409. insertedIndex,
  2410. sourceAttribute,
  2411. currentAttribute
  2412. ) {
  2413. const componentsPerAttribute = sourceAttribute.componentsPerAttribute;
  2414. const sourceValues = sourceAttribute.values;
  2415. const currentValues = currentAttribute.values;
  2416. switch (componentsPerAttribute) {
  2417. case 4:
  2418. interpolateAndPackCartesian4(
  2419. i0,
  2420. i1,
  2421. i2,
  2422. coords,
  2423. sourceValues,
  2424. currentValues,
  2425. insertedIndex,
  2426. false
  2427. );
  2428. break;
  2429. case 3:
  2430. interpolateAndPackCartesian3(
  2431. i0,
  2432. i1,
  2433. i2,
  2434. coords,
  2435. sourceValues,
  2436. currentValues,
  2437. insertedIndex,
  2438. false
  2439. );
  2440. break;
  2441. case 2:
  2442. interpolateAndPackCartesian2(
  2443. i0,
  2444. i1,
  2445. i2,
  2446. coords,
  2447. sourceValues,
  2448. currentValues,
  2449. insertedIndex,
  2450. false
  2451. );
  2452. break;
  2453. default:
  2454. currentValues[insertedIndex] =
  2455. sourceValues[i0] * coords.x +
  2456. sourceValues[i1] * coords.y +
  2457. sourceValues[i2] * coords.z;
  2458. }
  2459. }
  2460. function insertSplitPoint(
  2461. currentAttributes,
  2462. currentIndices,
  2463. currentIndexMap,
  2464. indices,
  2465. currentIndex,
  2466. point
  2467. ) {
  2468. const insertIndex = currentAttributes.position.values.length / 3;
  2469. if (currentIndex !== -1) {
  2470. const prevIndex = indices[currentIndex];
  2471. const newIndex = currentIndexMap[prevIndex];
  2472. if (newIndex === -1) {
  2473. currentIndexMap[prevIndex] = insertIndex;
  2474. currentAttributes.position.values.push(point.x, point.y, point.z);
  2475. currentIndices.push(insertIndex);
  2476. return insertIndex;
  2477. }
  2478. currentIndices.push(newIndex);
  2479. return newIndex;
  2480. }
  2481. currentAttributes.position.values.push(point.x, point.y, point.z);
  2482. currentIndices.push(insertIndex);
  2483. return insertIndex;
  2484. }
  2485. const NAMED_ATTRIBUTES = {
  2486. position: true,
  2487. normal: true,
  2488. bitangent: true,
  2489. tangent: true,
  2490. st: true,
  2491. extrudeDirection: true,
  2492. applyOffset: true,
  2493. };
  2494. function splitLongitudeTriangles(instance) {
  2495. const geometry = instance.geometry;
  2496. const attributes = geometry.attributes;
  2497. const positions = attributes.position.values;
  2498. const normals = defaultValue.defined(attributes.normal)
  2499. ? attributes.normal.values
  2500. : undefined;
  2501. const bitangents = defaultValue.defined(attributes.bitangent)
  2502. ? attributes.bitangent.values
  2503. : undefined;
  2504. const tangents = defaultValue.defined(attributes.tangent)
  2505. ? attributes.tangent.values
  2506. : undefined;
  2507. const texCoords = defaultValue.defined(attributes.st) ? attributes.st.values : undefined;
  2508. const extrudeDirections = defaultValue.defined(attributes.extrudeDirection)
  2509. ? attributes.extrudeDirection.values
  2510. : undefined;
  2511. const applyOffset = defaultValue.defined(attributes.applyOffset)
  2512. ? attributes.applyOffset.values
  2513. : undefined;
  2514. const indices = geometry.indices;
  2515. const customAttributeNames = [];
  2516. for (const attributeName in attributes) {
  2517. if (
  2518. attributes.hasOwnProperty(attributeName) &&
  2519. !NAMED_ATTRIBUTES[attributeName] &&
  2520. defaultValue.defined(attributes[attributeName])
  2521. ) {
  2522. customAttributeNames.push(attributeName);
  2523. }
  2524. }
  2525. const customAttributesLength = customAttributeNames.length;
  2526. const eastGeometry = copyGeometryForSplit(geometry);
  2527. const westGeometry = copyGeometryForSplit(geometry);
  2528. let currentAttributes;
  2529. let currentIndices;
  2530. let currentIndexMap;
  2531. let insertedIndex;
  2532. let i;
  2533. const westGeometryIndexMap = [];
  2534. westGeometryIndexMap.length = positions.length / 3;
  2535. const eastGeometryIndexMap = [];
  2536. eastGeometryIndexMap.length = positions.length / 3;
  2537. for (i = 0; i < westGeometryIndexMap.length; ++i) {
  2538. westGeometryIndexMap[i] = -1;
  2539. eastGeometryIndexMap[i] = -1;
  2540. }
  2541. const len = indices.length;
  2542. for (i = 0; i < len; i += 3) {
  2543. const i0 = indices[i];
  2544. const i1 = indices[i + 1];
  2545. const i2 = indices[i + 2];
  2546. let p0 = Matrix2.Cartesian3.fromArray(positions, i0 * 3);
  2547. let p1 = Matrix2.Cartesian3.fromArray(positions, i1 * 3);
  2548. let p2 = Matrix2.Cartesian3.fromArray(positions, i2 * 3);
  2549. const result = splitTriangle(p0, p1, p2);
  2550. if (defaultValue.defined(result) && result.positions.length > 3) {
  2551. const resultPositions = result.positions;
  2552. const resultIndices = result.indices;
  2553. const resultLength = resultIndices.length;
  2554. for (let j = 0; j < resultLength; ++j) {
  2555. const resultIndex = resultIndices[j];
  2556. const point = resultPositions[resultIndex];
  2557. if (point.y < 0.0) {
  2558. currentAttributes = westGeometry.attributes;
  2559. currentIndices = westGeometry.indices;
  2560. currentIndexMap = westGeometryIndexMap;
  2561. } else {
  2562. currentAttributes = eastGeometry.attributes;
  2563. currentIndices = eastGeometry.indices;
  2564. currentIndexMap = eastGeometryIndexMap;
  2565. }
  2566. insertedIndex = insertSplitPoint(
  2567. currentAttributes,
  2568. currentIndices,
  2569. currentIndexMap,
  2570. indices,
  2571. resultIndex < 3 ? i + resultIndex : -1,
  2572. point
  2573. );
  2574. computeTriangleAttributes(
  2575. i0,
  2576. i1,
  2577. i2,
  2578. point,
  2579. positions,
  2580. normals,
  2581. tangents,
  2582. bitangents,
  2583. texCoords,
  2584. extrudeDirections,
  2585. applyOffset,
  2586. currentAttributes,
  2587. customAttributeNames,
  2588. customAttributesLength,
  2589. attributes,
  2590. insertedIndex
  2591. );
  2592. }
  2593. } else {
  2594. if (defaultValue.defined(result)) {
  2595. p0 = result.positions[0];
  2596. p1 = result.positions[1];
  2597. p2 = result.positions[2];
  2598. }
  2599. if (p0.y < 0.0) {
  2600. currentAttributes = westGeometry.attributes;
  2601. currentIndices = westGeometry.indices;
  2602. currentIndexMap = westGeometryIndexMap;
  2603. } else {
  2604. currentAttributes = eastGeometry.attributes;
  2605. currentIndices = eastGeometry.indices;
  2606. currentIndexMap = eastGeometryIndexMap;
  2607. }
  2608. insertedIndex = insertSplitPoint(
  2609. currentAttributes,
  2610. currentIndices,
  2611. currentIndexMap,
  2612. indices,
  2613. i,
  2614. p0
  2615. );
  2616. computeTriangleAttributes(
  2617. i0,
  2618. i1,
  2619. i2,
  2620. p0,
  2621. positions,
  2622. normals,
  2623. tangents,
  2624. bitangents,
  2625. texCoords,
  2626. extrudeDirections,
  2627. applyOffset,
  2628. currentAttributes,
  2629. customAttributeNames,
  2630. customAttributesLength,
  2631. attributes,
  2632. insertedIndex
  2633. );
  2634. insertedIndex = insertSplitPoint(
  2635. currentAttributes,
  2636. currentIndices,
  2637. currentIndexMap,
  2638. indices,
  2639. i + 1,
  2640. p1
  2641. );
  2642. computeTriangleAttributes(
  2643. i0,
  2644. i1,
  2645. i2,
  2646. p1,
  2647. positions,
  2648. normals,
  2649. tangents,
  2650. bitangents,
  2651. texCoords,
  2652. extrudeDirections,
  2653. applyOffset,
  2654. currentAttributes,
  2655. customAttributeNames,
  2656. customAttributesLength,
  2657. attributes,
  2658. insertedIndex
  2659. );
  2660. insertedIndex = insertSplitPoint(
  2661. currentAttributes,
  2662. currentIndices,
  2663. currentIndexMap,
  2664. indices,
  2665. i + 2,
  2666. p2
  2667. );
  2668. computeTriangleAttributes(
  2669. i0,
  2670. i1,
  2671. i2,
  2672. p2,
  2673. positions,
  2674. normals,
  2675. tangents,
  2676. bitangents,
  2677. texCoords,
  2678. extrudeDirections,
  2679. applyOffset,
  2680. currentAttributes,
  2681. customAttributeNames,
  2682. customAttributesLength,
  2683. attributes,
  2684. insertedIndex
  2685. );
  2686. }
  2687. }
  2688. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  2689. }
  2690. const xzPlane = Plane.Plane.fromPointNormal(Matrix2.Cartesian3.ZERO, Matrix2.Cartesian3.UNIT_Y);
  2691. const offsetScratch = new Matrix2.Cartesian3();
  2692. const offsetPointScratch = new Matrix2.Cartesian3();
  2693. function computeLineAttributes(
  2694. i0,
  2695. i1,
  2696. point,
  2697. positions,
  2698. insertIndex,
  2699. currentAttributes,
  2700. applyOffset
  2701. ) {
  2702. if (!defaultValue.defined(applyOffset)) {
  2703. return;
  2704. }
  2705. const p0 = Matrix2.Cartesian3.fromArray(positions, i0 * 3, p0Scratch);
  2706. if (Matrix2.Cartesian3.equalsEpsilon(p0, point, ComponentDatatype.CesiumMath.EPSILON10)) {
  2707. currentAttributes.applyOffset.values[insertIndex] = applyOffset[i0];
  2708. } else {
  2709. currentAttributes.applyOffset.values[insertIndex] = applyOffset[i1];
  2710. }
  2711. }
  2712. function splitLongitudeLines(instance) {
  2713. const geometry = instance.geometry;
  2714. const attributes = geometry.attributes;
  2715. const positions = attributes.position.values;
  2716. const applyOffset = defaultValue.defined(attributes.applyOffset)
  2717. ? attributes.applyOffset.values
  2718. : undefined;
  2719. const indices = geometry.indices;
  2720. const eastGeometry = copyGeometryForSplit(geometry);
  2721. const westGeometry = copyGeometryForSplit(geometry);
  2722. let i;
  2723. const length = indices.length;
  2724. const westGeometryIndexMap = [];
  2725. westGeometryIndexMap.length = positions.length / 3;
  2726. const eastGeometryIndexMap = [];
  2727. eastGeometryIndexMap.length = positions.length / 3;
  2728. for (i = 0; i < westGeometryIndexMap.length; ++i) {
  2729. westGeometryIndexMap[i] = -1;
  2730. eastGeometryIndexMap[i] = -1;
  2731. }
  2732. for (i = 0; i < length; i += 2) {
  2733. const i0 = indices[i];
  2734. const i1 = indices[i + 1];
  2735. const p0 = Matrix2.Cartesian3.fromArray(positions, i0 * 3, p0Scratch);
  2736. const p1 = Matrix2.Cartesian3.fromArray(positions, i1 * 3, p1Scratch);
  2737. let insertIndex;
  2738. if (Math.abs(p0.y) < ComponentDatatype.CesiumMath.EPSILON6) {
  2739. if (p0.y < 0.0) {
  2740. p0.y = -ComponentDatatype.CesiumMath.EPSILON6;
  2741. } else {
  2742. p0.y = ComponentDatatype.CesiumMath.EPSILON6;
  2743. }
  2744. }
  2745. if (Math.abs(p1.y) < ComponentDatatype.CesiumMath.EPSILON6) {
  2746. if (p1.y < 0.0) {
  2747. p1.y = -ComponentDatatype.CesiumMath.EPSILON6;
  2748. } else {
  2749. p1.y = ComponentDatatype.CesiumMath.EPSILON6;
  2750. }
  2751. }
  2752. let p0Attributes = eastGeometry.attributes;
  2753. let p0Indices = eastGeometry.indices;
  2754. let p0IndexMap = eastGeometryIndexMap;
  2755. let p1Attributes = westGeometry.attributes;
  2756. let p1Indices = westGeometry.indices;
  2757. let p1IndexMap = westGeometryIndexMap;
  2758. const intersection = IntersectionTests.IntersectionTests.lineSegmentPlane(
  2759. p0,
  2760. p1,
  2761. xzPlane,
  2762. p2Scratch
  2763. );
  2764. if (defaultValue.defined(intersection)) {
  2765. // move point on the xz-plane slightly away from the plane
  2766. const offset = Matrix2.Cartesian3.multiplyByScalar(
  2767. Matrix2.Cartesian3.UNIT_Y,
  2768. 5.0 * ComponentDatatype.CesiumMath.EPSILON9,
  2769. offsetScratch
  2770. );
  2771. if (p0.y < 0.0) {
  2772. Matrix2.Cartesian3.negate(offset, offset);
  2773. p0Attributes = westGeometry.attributes;
  2774. p0Indices = westGeometry.indices;
  2775. p0IndexMap = westGeometryIndexMap;
  2776. p1Attributes = eastGeometry.attributes;
  2777. p1Indices = eastGeometry.indices;
  2778. p1IndexMap = eastGeometryIndexMap;
  2779. }
  2780. const offsetPoint = Matrix2.Cartesian3.add(
  2781. intersection,
  2782. offset,
  2783. offsetPointScratch
  2784. );
  2785. insertIndex = insertSplitPoint(
  2786. p0Attributes,
  2787. p0Indices,
  2788. p0IndexMap,
  2789. indices,
  2790. i,
  2791. p0
  2792. );
  2793. computeLineAttributes(
  2794. i0,
  2795. i1,
  2796. p0,
  2797. positions,
  2798. insertIndex,
  2799. p0Attributes,
  2800. applyOffset
  2801. );
  2802. insertIndex = insertSplitPoint(
  2803. p0Attributes,
  2804. p0Indices,
  2805. p0IndexMap,
  2806. indices,
  2807. -1,
  2808. offsetPoint
  2809. );
  2810. computeLineAttributes(
  2811. i0,
  2812. i1,
  2813. offsetPoint,
  2814. positions,
  2815. insertIndex,
  2816. p0Attributes,
  2817. applyOffset
  2818. );
  2819. Matrix2.Cartesian3.negate(offset, offset);
  2820. Matrix2.Cartesian3.add(intersection, offset, offsetPoint);
  2821. insertIndex = insertSplitPoint(
  2822. p1Attributes,
  2823. p1Indices,
  2824. p1IndexMap,
  2825. indices,
  2826. -1,
  2827. offsetPoint
  2828. );
  2829. computeLineAttributes(
  2830. i0,
  2831. i1,
  2832. offsetPoint,
  2833. positions,
  2834. insertIndex,
  2835. p1Attributes,
  2836. applyOffset
  2837. );
  2838. insertIndex = insertSplitPoint(
  2839. p1Attributes,
  2840. p1Indices,
  2841. p1IndexMap,
  2842. indices,
  2843. i + 1,
  2844. p1
  2845. );
  2846. computeLineAttributes(
  2847. i0,
  2848. i1,
  2849. p1,
  2850. positions,
  2851. insertIndex,
  2852. p1Attributes,
  2853. applyOffset
  2854. );
  2855. } else {
  2856. let currentAttributes;
  2857. let currentIndices;
  2858. let currentIndexMap;
  2859. if (p0.y < 0.0) {
  2860. currentAttributes = westGeometry.attributes;
  2861. currentIndices = westGeometry.indices;
  2862. currentIndexMap = westGeometryIndexMap;
  2863. } else {
  2864. currentAttributes = eastGeometry.attributes;
  2865. currentIndices = eastGeometry.indices;
  2866. currentIndexMap = eastGeometryIndexMap;
  2867. }
  2868. insertIndex = insertSplitPoint(
  2869. currentAttributes,
  2870. currentIndices,
  2871. currentIndexMap,
  2872. indices,
  2873. i,
  2874. p0
  2875. );
  2876. computeLineAttributes(
  2877. i0,
  2878. i1,
  2879. p0,
  2880. positions,
  2881. insertIndex,
  2882. currentAttributes,
  2883. applyOffset
  2884. );
  2885. insertIndex = insertSplitPoint(
  2886. currentAttributes,
  2887. currentIndices,
  2888. currentIndexMap,
  2889. indices,
  2890. i + 1,
  2891. p1
  2892. );
  2893. computeLineAttributes(
  2894. i0,
  2895. i1,
  2896. p1,
  2897. positions,
  2898. insertIndex,
  2899. currentAttributes,
  2900. applyOffset
  2901. );
  2902. }
  2903. }
  2904. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  2905. }
  2906. const cartesian2Scratch0 = new Matrix2.Cartesian2();
  2907. const cartesian2Scratch1 = new Matrix2.Cartesian2();
  2908. const cartesian3Scratch0 = new Matrix2.Cartesian3();
  2909. const cartesian3Scratch2 = new Matrix2.Cartesian3();
  2910. const cartesian3Scratch3 = new Matrix2.Cartesian3();
  2911. const cartesian3Scratch4 = new Matrix2.Cartesian3();
  2912. const cartesian3Scratch5 = new Matrix2.Cartesian3();
  2913. const cartesian3Scratch6 = new Matrix2.Cartesian3();
  2914. const cartesian4Scratch0 = new Matrix2.Cartesian4();
  2915. function updateAdjacencyAfterSplit(geometry) {
  2916. const attributes = geometry.attributes;
  2917. const positions = attributes.position.values;
  2918. const prevPositions = attributes.prevPosition.values;
  2919. const nextPositions = attributes.nextPosition.values;
  2920. const length = positions.length;
  2921. for (let j = 0; j < length; j += 3) {
  2922. const position = Matrix2.Cartesian3.unpack(positions, j, cartesian3Scratch0);
  2923. if (position.x > 0.0) {
  2924. continue;
  2925. }
  2926. const prevPosition = Matrix2.Cartesian3.unpack(
  2927. prevPositions,
  2928. j,
  2929. cartesian3Scratch2
  2930. );
  2931. if (
  2932. (position.y < 0.0 && prevPosition.y > 0.0) ||
  2933. (position.y > 0.0 && prevPosition.y < 0.0)
  2934. ) {
  2935. if (j - 3 > 0) {
  2936. prevPositions[j] = positions[j - 3];
  2937. prevPositions[j + 1] = positions[j - 2];
  2938. prevPositions[j + 2] = positions[j - 1];
  2939. } else {
  2940. Matrix2.Cartesian3.pack(position, prevPositions, j);
  2941. }
  2942. }
  2943. const nextPosition = Matrix2.Cartesian3.unpack(
  2944. nextPositions,
  2945. j,
  2946. cartesian3Scratch3
  2947. );
  2948. if (
  2949. (position.y < 0.0 && nextPosition.y > 0.0) ||
  2950. (position.y > 0.0 && nextPosition.y < 0.0)
  2951. ) {
  2952. if (j + 3 < length) {
  2953. nextPositions[j] = positions[j + 3];
  2954. nextPositions[j + 1] = positions[j + 4];
  2955. nextPositions[j + 2] = positions[j + 5];
  2956. } else {
  2957. Matrix2.Cartesian3.pack(position, nextPositions, j);
  2958. }
  2959. }
  2960. }
  2961. }
  2962. const offsetScalar = 5.0 * ComponentDatatype.CesiumMath.EPSILON9;
  2963. const coplanarOffset = ComponentDatatype.CesiumMath.EPSILON6;
  2964. function splitLongitudePolyline(instance) {
  2965. const geometry = instance.geometry;
  2966. const attributes = geometry.attributes;
  2967. const positions = attributes.position.values;
  2968. const prevPositions = attributes.prevPosition.values;
  2969. const nextPositions = attributes.nextPosition.values;
  2970. const expandAndWidths = attributes.expandAndWidth.values;
  2971. const texCoords = defaultValue.defined(attributes.st) ? attributes.st.values : undefined;
  2972. const colors = defaultValue.defined(attributes.color)
  2973. ? attributes.color.values
  2974. : undefined;
  2975. const eastGeometry = copyGeometryForSplit(geometry);
  2976. const westGeometry = copyGeometryForSplit(geometry);
  2977. let i;
  2978. let j;
  2979. let index;
  2980. let intersectionFound = false;
  2981. const length = positions.length / 3;
  2982. for (i = 0; i < length; i += 4) {
  2983. const i0 = i;
  2984. const i2 = i + 2;
  2985. const p0 = Matrix2.Cartesian3.fromArray(positions, i0 * 3, cartesian3Scratch0);
  2986. const p2 = Matrix2.Cartesian3.fromArray(positions, i2 * 3, cartesian3Scratch2);
  2987. // Offset points that are close to the 180 longitude and change the previous/next point
  2988. // to be the same offset point so it can be projected to 2D. There is special handling in the
  2989. // shader for when position == prevPosition || position == nextPosition.
  2990. if (Math.abs(p0.y) < coplanarOffset) {
  2991. p0.y = coplanarOffset * (p2.y < 0.0 ? -1.0 : 1.0);
  2992. positions[i * 3 + 1] = p0.y;
  2993. positions[(i + 1) * 3 + 1] = p0.y;
  2994. for (j = i0 * 3; j < i0 * 3 + 4 * 3; j += 3) {
  2995. prevPositions[j] = positions[i * 3];
  2996. prevPositions[j + 1] = positions[i * 3 + 1];
  2997. prevPositions[j + 2] = positions[i * 3 + 2];
  2998. }
  2999. }
  3000. // Do the same but for when the line crosses 180 longitude in the opposite direction.
  3001. if (Math.abs(p2.y) < coplanarOffset) {
  3002. p2.y = coplanarOffset * (p0.y < 0.0 ? -1.0 : 1.0);
  3003. positions[(i + 2) * 3 + 1] = p2.y;
  3004. positions[(i + 3) * 3 + 1] = p2.y;
  3005. for (j = i0 * 3; j < i0 * 3 + 4 * 3; j += 3) {
  3006. nextPositions[j] = positions[(i + 2) * 3];
  3007. nextPositions[j + 1] = positions[(i + 2) * 3 + 1];
  3008. nextPositions[j + 2] = positions[(i + 2) * 3 + 2];
  3009. }
  3010. }
  3011. let p0Attributes = eastGeometry.attributes;
  3012. let p0Indices = eastGeometry.indices;
  3013. let p2Attributes = westGeometry.attributes;
  3014. let p2Indices = westGeometry.indices;
  3015. const intersection = IntersectionTests.IntersectionTests.lineSegmentPlane(
  3016. p0,
  3017. p2,
  3018. xzPlane,
  3019. cartesian3Scratch4
  3020. );
  3021. if (defaultValue.defined(intersection)) {
  3022. intersectionFound = true;
  3023. // move point on the xz-plane slightly away from the plane
  3024. const offset = Matrix2.Cartesian3.multiplyByScalar(
  3025. Matrix2.Cartesian3.UNIT_Y,
  3026. offsetScalar,
  3027. cartesian3Scratch5
  3028. );
  3029. if (p0.y < 0.0) {
  3030. Matrix2.Cartesian3.negate(offset, offset);
  3031. p0Attributes = westGeometry.attributes;
  3032. p0Indices = westGeometry.indices;
  3033. p2Attributes = eastGeometry.attributes;
  3034. p2Indices = eastGeometry.indices;
  3035. }
  3036. const offsetPoint = Matrix2.Cartesian3.add(
  3037. intersection,
  3038. offset,
  3039. cartesian3Scratch6
  3040. );
  3041. p0Attributes.position.values.push(p0.x, p0.y, p0.z, p0.x, p0.y, p0.z);
  3042. p0Attributes.position.values.push(
  3043. offsetPoint.x,
  3044. offsetPoint.y,
  3045. offsetPoint.z
  3046. );
  3047. p0Attributes.position.values.push(
  3048. offsetPoint.x,
  3049. offsetPoint.y,
  3050. offsetPoint.z
  3051. );
  3052. p0Attributes.prevPosition.values.push(
  3053. prevPositions[i0 * 3],
  3054. prevPositions[i0 * 3 + 1],
  3055. prevPositions[i0 * 3 + 2]
  3056. );
  3057. p0Attributes.prevPosition.values.push(
  3058. prevPositions[i0 * 3 + 3],
  3059. prevPositions[i0 * 3 + 4],
  3060. prevPositions[i0 * 3 + 5]
  3061. );
  3062. p0Attributes.prevPosition.values.push(p0.x, p0.y, p0.z, p0.x, p0.y, p0.z);
  3063. p0Attributes.nextPosition.values.push(
  3064. offsetPoint.x,
  3065. offsetPoint.y,
  3066. offsetPoint.z
  3067. );
  3068. p0Attributes.nextPosition.values.push(
  3069. offsetPoint.x,
  3070. offsetPoint.y,
  3071. offsetPoint.z
  3072. );
  3073. p0Attributes.nextPosition.values.push(
  3074. offsetPoint.x,
  3075. offsetPoint.y,
  3076. offsetPoint.z
  3077. );
  3078. p0Attributes.nextPosition.values.push(
  3079. offsetPoint.x,
  3080. offsetPoint.y,
  3081. offsetPoint.z
  3082. );
  3083. Matrix2.Cartesian3.negate(offset, offset);
  3084. Matrix2.Cartesian3.add(intersection, offset, offsetPoint);
  3085. p2Attributes.position.values.push(
  3086. offsetPoint.x,
  3087. offsetPoint.y,
  3088. offsetPoint.z
  3089. );
  3090. p2Attributes.position.values.push(
  3091. offsetPoint.x,
  3092. offsetPoint.y,
  3093. offsetPoint.z
  3094. );
  3095. p2Attributes.position.values.push(p2.x, p2.y, p2.z, p2.x, p2.y, p2.z);
  3096. p2Attributes.prevPosition.values.push(
  3097. offsetPoint.x,
  3098. offsetPoint.y,
  3099. offsetPoint.z
  3100. );
  3101. p2Attributes.prevPosition.values.push(
  3102. offsetPoint.x,
  3103. offsetPoint.y,
  3104. offsetPoint.z
  3105. );
  3106. p2Attributes.prevPosition.values.push(
  3107. offsetPoint.x,
  3108. offsetPoint.y,
  3109. offsetPoint.z
  3110. );
  3111. p2Attributes.prevPosition.values.push(
  3112. offsetPoint.x,
  3113. offsetPoint.y,
  3114. offsetPoint.z
  3115. );
  3116. p2Attributes.nextPosition.values.push(p2.x, p2.y, p2.z, p2.x, p2.y, p2.z);
  3117. p2Attributes.nextPosition.values.push(
  3118. nextPositions[i2 * 3],
  3119. nextPositions[i2 * 3 + 1],
  3120. nextPositions[i2 * 3 + 2]
  3121. );
  3122. p2Attributes.nextPosition.values.push(
  3123. nextPositions[i2 * 3 + 3],
  3124. nextPositions[i2 * 3 + 4],
  3125. nextPositions[i2 * 3 + 5]
  3126. );
  3127. const ew0 = Matrix2.Cartesian2.fromArray(
  3128. expandAndWidths,
  3129. i0 * 2,
  3130. cartesian2Scratch0
  3131. );
  3132. const width = Math.abs(ew0.y);
  3133. p0Attributes.expandAndWidth.values.push(-1, width, 1, width);
  3134. p0Attributes.expandAndWidth.values.push(-1, -width, 1, -width);
  3135. p2Attributes.expandAndWidth.values.push(-1, width, 1, width);
  3136. p2Attributes.expandAndWidth.values.push(-1, -width, 1, -width);
  3137. let t = Matrix2.Cartesian3.magnitudeSquared(
  3138. Matrix2.Cartesian3.subtract(intersection, p0, cartesian3Scratch3)
  3139. );
  3140. t /= Matrix2.Cartesian3.magnitudeSquared(
  3141. Matrix2.Cartesian3.subtract(p2, p0, cartesian3Scratch3)
  3142. );
  3143. if (defaultValue.defined(colors)) {
  3144. const c0 = Matrix2.Cartesian4.fromArray(colors, i0 * 4, cartesian4Scratch0);
  3145. const c2 = Matrix2.Cartesian4.fromArray(colors, i2 * 4, cartesian4Scratch0);
  3146. const r = ComponentDatatype.CesiumMath.lerp(c0.x, c2.x, t);
  3147. const g = ComponentDatatype.CesiumMath.lerp(c0.y, c2.y, t);
  3148. const b = ComponentDatatype.CesiumMath.lerp(c0.z, c2.z, t);
  3149. const a = ComponentDatatype.CesiumMath.lerp(c0.w, c2.w, t);
  3150. for (j = i0 * 4; j < i0 * 4 + 2 * 4; ++j) {
  3151. p0Attributes.color.values.push(colors[j]);
  3152. }
  3153. p0Attributes.color.values.push(r, g, b, a);
  3154. p0Attributes.color.values.push(r, g, b, a);
  3155. p2Attributes.color.values.push(r, g, b, a);
  3156. p2Attributes.color.values.push(r, g, b, a);
  3157. for (j = i2 * 4; j < i2 * 4 + 2 * 4; ++j) {
  3158. p2Attributes.color.values.push(colors[j]);
  3159. }
  3160. }
  3161. if (defaultValue.defined(texCoords)) {
  3162. const s0 = Matrix2.Cartesian2.fromArray(texCoords, i0 * 2, cartesian2Scratch0);
  3163. const s3 = Matrix2.Cartesian2.fromArray(
  3164. texCoords,
  3165. (i + 3) * 2,
  3166. cartesian2Scratch1
  3167. );
  3168. const sx = ComponentDatatype.CesiumMath.lerp(s0.x, s3.x, t);
  3169. for (j = i0 * 2; j < i0 * 2 + 2 * 2; ++j) {
  3170. p0Attributes.st.values.push(texCoords[j]);
  3171. }
  3172. p0Attributes.st.values.push(sx, s0.y);
  3173. p0Attributes.st.values.push(sx, s3.y);
  3174. p2Attributes.st.values.push(sx, s0.y);
  3175. p2Attributes.st.values.push(sx, s3.y);
  3176. for (j = i2 * 2; j < i2 * 2 + 2 * 2; ++j) {
  3177. p2Attributes.st.values.push(texCoords[j]);
  3178. }
  3179. }
  3180. index = p0Attributes.position.values.length / 3 - 4;
  3181. p0Indices.push(index, index + 2, index + 1);
  3182. p0Indices.push(index + 1, index + 2, index + 3);
  3183. index = p2Attributes.position.values.length / 3 - 4;
  3184. p2Indices.push(index, index + 2, index + 1);
  3185. p2Indices.push(index + 1, index + 2, index + 3);
  3186. } else {
  3187. let currentAttributes;
  3188. let currentIndices;
  3189. if (p0.y < 0.0) {
  3190. currentAttributes = westGeometry.attributes;
  3191. currentIndices = westGeometry.indices;
  3192. } else {
  3193. currentAttributes = eastGeometry.attributes;
  3194. currentIndices = eastGeometry.indices;
  3195. }
  3196. currentAttributes.position.values.push(p0.x, p0.y, p0.z);
  3197. currentAttributes.position.values.push(p0.x, p0.y, p0.z);
  3198. currentAttributes.position.values.push(p2.x, p2.y, p2.z);
  3199. currentAttributes.position.values.push(p2.x, p2.y, p2.z);
  3200. for (j = i * 3; j < i * 3 + 4 * 3; ++j) {
  3201. currentAttributes.prevPosition.values.push(prevPositions[j]);
  3202. currentAttributes.nextPosition.values.push(nextPositions[j]);
  3203. }
  3204. for (j = i * 2; j < i * 2 + 4 * 2; ++j) {
  3205. currentAttributes.expandAndWidth.values.push(expandAndWidths[j]);
  3206. if (defaultValue.defined(texCoords)) {
  3207. currentAttributes.st.values.push(texCoords[j]);
  3208. }
  3209. }
  3210. if (defaultValue.defined(colors)) {
  3211. for (j = i * 4; j < i * 4 + 4 * 4; ++j) {
  3212. currentAttributes.color.values.push(colors[j]);
  3213. }
  3214. }
  3215. index = currentAttributes.position.values.length / 3 - 4;
  3216. currentIndices.push(index, index + 2, index + 1);
  3217. currentIndices.push(index + 1, index + 2, index + 3);
  3218. }
  3219. }
  3220. if (intersectionFound) {
  3221. updateAdjacencyAfterSplit(westGeometry);
  3222. updateAdjacencyAfterSplit(eastGeometry);
  3223. }
  3224. updateInstanceAfterSplit(instance, westGeometry, eastGeometry);
  3225. }
  3226. /**
  3227. * Splits the instances's geometry, by introducing new vertices and indices,that
  3228. * intersect the International Date Line and Prime Meridian so that no primitives cross longitude
  3229. * -180/180 degrees. This is not required for 3D drawing, but is required for
  3230. * correcting drawing in 2D and Columbus view.
  3231. *
  3232. * @private
  3233. *
  3234. * @param {GeometryInstance} instance The instance to modify.
  3235. * @returns {GeometryInstance} The modified <code>instance</code> argument, with it's geometry split at the International Date Line.
  3236. *
  3237. * @example
  3238. * instance = Cesium.GeometryPipeline.splitLongitude(instance);
  3239. */
  3240. GeometryPipeline.splitLongitude = function (instance) {
  3241. //>>includeStart('debug', pragmas.debug);
  3242. if (!defaultValue.defined(instance)) {
  3243. throw new RuntimeError.DeveloperError("instance is required.");
  3244. }
  3245. //>>includeEnd('debug');
  3246. const geometry = instance.geometry;
  3247. const boundingSphere = geometry.boundingSphere;
  3248. if (defaultValue.defined(boundingSphere)) {
  3249. const minX = boundingSphere.center.x - boundingSphere.radius;
  3250. if (
  3251. minX > 0 ||
  3252. Transforms.BoundingSphere.intersectPlane(boundingSphere, Plane.Plane.ORIGIN_ZX_PLANE) !==
  3253. Transforms.Intersect.INTERSECTING
  3254. ) {
  3255. return instance;
  3256. }
  3257. }
  3258. if (geometry.geometryType !== GeometryAttribute.GeometryType.NONE) {
  3259. switch (geometry.geometryType) {
  3260. case GeometryAttribute.GeometryType.POLYLINES:
  3261. splitLongitudePolyline(instance);
  3262. break;
  3263. case GeometryAttribute.GeometryType.TRIANGLES:
  3264. splitLongitudeTriangles(instance);
  3265. break;
  3266. case GeometryAttribute.GeometryType.LINES:
  3267. splitLongitudeLines(instance);
  3268. break;
  3269. }
  3270. } else {
  3271. indexPrimitive(geometry);
  3272. if (geometry.primitiveType === GeometryAttribute.PrimitiveType.TRIANGLES) {
  3273. splitLongitudeTriangles(instance);
  3274. } else if (geometry.primitiveType === GeometryAttribute.PrimitiveType.LINES) {
  3275. splitLongitudeLines(instance);
  3276. }
  3277. }
  3278. return instance;
  3279. };
  3280. exports.GeometryPipeline = GeometryPipeline;
  3281. }));
  3282. //# sourceMappingURL=GeometryPipeline-d7363877.js.map