GeometryPipeline-b4816e69.js 116 KB

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