GeometryPipeline.js 92 KB

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