GeometryPipeline-5a61c463.js 117 KB

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