PrimitiveOutlineGenerator.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import Check from "../../Core/Check.js";
  2. import defined from "../../Core/defined.js";
  3. import defaultValue from "../../Core/defaultValue.js";
  4. import PixelFormat from "../../Core/PixelFormat.js";
  5. import ContextLimits from "../../Renderer/ContextLimits.js";
  6. import Sampler from "../../Renderer/Sampler.js";
  7. import Texture from "../../Renderer/Texture.js";
  8. import TextureMagnificationFilter from "../../Renderer/TextureMagnificationFilter.js";
  9. import TextureMinificationFilter from "../../Renderer/TextureMinificationFilter.js";
  10. import TextureWrap from "../../Renderer/TextureWrap.js";
  11. // glTF does not allow an index value of 65535 because this is the primitive
  12. // restart value in some APIs.
  13. const MAX_GLTF_UINT16_INDEX = 65534;
  14. const MAX_GLTF_UINT8_INDEX = 255;
  15. /**
  16. * A class to handle the low-level details of processing indices and vertex
  17. * attributes for the CESIUM_primitive_outline extension.
  18. * <p>
  19. * To render outlines, a lookup texture is used 3 times, once per edge of a
  20. * triangle. In order to render correctly, all three vertices must agree on the
  21. * same ordering of the three edges when computing outline (texture)
  22. * coordinates. Sometimes this is not possible, as a vertex shared between
  23. * multiple triangles may become overly constrained. In such cases, vertices are
  24. * copied and indices are updated until valid outline coordinates can be
  25. * defined.
  26. * </p>
  27. *
  28. * @see {@link https://www.researchgate.net/publication/220067637_Fast_and_versatile_texture-based_wireframe_rendering|Fast and versatile texture-based wireframe rendering}
  29. *
  30. * @alias PrimitiveOutlineGenerator
  31. * @constructor
  32. *
  33. * @param {number} options Object with the following properties:
  34. * @param {Uint8Array|Uint16Array|Uint32Array} options.triangleIndices The original triangle indices of the primitive. The constructor takes ownership of this typed array as it will be modified internally. Use the updatedTriangleIndices getter to get the final result.
  35. * @param {number[]} options.outlineIndices The indices of edges in the triangle from the CESIUM_primitive_outline extension
  36. * @param {number} options.originalVertexCount The original number of vertices in the primitive
  37. * @example
  38. * // The constructor will compute the updated indices and generate outline
  39. * // coordinates.
  40. * const outlineGenerator = new PrimitiveOutlineGenerator({
  41. * triangleIndices: primitive.indices.typedArray,
  42. * outlineIndices: outlineIndices,
  43. * originalVertexCount: primitive.attributes[0].count
  44. * });
  45. *
  46. * // Caller must update the indices (the data type may have been upgraded!)
  47. * primitive.indices.typedArray = outlineGenerator.updatedTriangleIndices;
  48. * primitive.indices.indexDatatype =
  49. * IndexDatatype.fromTypedArray(primitive.indices.typedArray);
  50. *
  51. * // Create a new attribute for the generated outline coordinates
  52. * primitive.outlineCoordinates = new ModelComponents.Attribute();
  53. * // ... initialize as a vec3 attribute
  54. * primitive.outlineCoordinates.typedArray =
  55. * outlineGenerator.outlineCoordinates;
  56. *
  57. * // Updating an attribute
  58. * const attribute = primitive.attributes[i];
  59. * attribute.typedArray = outlineGenerator.updateAttribute(
  60. * attribute.typedArray
  61. * );
  62. *
  63. * @private
  64. */
  65. function PrimitiveOutlineGenerator(options) {
  66. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  67. const triangleIndices = options.triangleIndices;
  68. const outlineIndices = options.outlineIndices;
  69. const originalVertexCount = options.originalVertexCount;
  70. //>>includeStart('debug', pragmas.debug);
  71. Check.typeOf.object("options.triangleIndices", triangleIndices);
  72. Check.typeOf.object("options.outlineIndices", outlineIndices);
  73. Check.typeOf.number("options.originalVertexCount", originalVertexCount);
  74. //>>includeEnd('debug');
  75. /**
  76. * The triangle indices. It will be modified in place.
  77. *
  78. * @type {Uint8Array|Uint16Array|Uint32Array}
  79. *
  80. * @private
  81. */
  82. this._triangleIndices = triangleIndices;
  83. /**
  84. * How many vertices were originally in the primitive
  85. *
  86. * @type {number}
  87. *
  88. * @private
  89. */
  90. this._originalVertexCount = originalVertexCount;
  91. /**
  92. * The outline indices represent edges of the primitive's triangle mesh where
  93. * outlines must be drawn. This is stored as a hash set for efficient
  94. * checks of whether an edge is present.
  95. *
  96. * @type {EdgeSet}
  97. *
  98. * @private
  99. */
  100. this._edges = new EdgeSet(outlineIndices, originalVertexCount);
  101. /**
  102. * The typed array that will store the outline texture coordinates
  103. * once computed. This typed array should be turned into a vertex attribute
  104. * when rendering outlines.
  105. *
  106. * @type {Float32Array}
  107. *
  108. * @private
  109. */
  110. this._outlineCoordinatesTypedArray = undefined;
  111. /**
  112. * Array containing the indices of any vertices that must be copied and
  113. * appended to the list.
  114. *
  115. * @type {number[]}
  116. *
  117. * @private
  118. */
  119. this._extraVertices = [];
  120. initialize(this);
  121. }
  122. Object.defineProperties(PrimitiveOutlineGenerator.prototype, {
  123. /**
  124. * The updated triangle indices after generating outlines. The caller is for
  125. * responsible for updating the primitive's indices to use this array.
  126. *
  127. * @memberof PrimitiveOutlineGenerator.prototype
  128. *
  129. * @type {Uint8Array|Uint16Array|Uint32Array}
  130. * @readonly
  131. *
  132. * @private
  133. */
  134. updatedTriangleIndices: {
  135. get: function () {
  136. return this._triangleIndices;
  137. },
  138. },
  139. /**
  140. * The computed outline coordinates. The caller is responsible for
  141. * turning this into a vec3 attribute for rendering.
  142. *
  143. * @memberof PrimitiveOutlineGenerator.prototype
  144. *
  145. * @type {Float32Array}
  146. * @readonly
  147. *
  148. * @private
  149. */
  150. outlineCoordinates: {
  151. get: function () {
  152. return this._outlineCoordinatesTypedArray;
  153. },
  154. },
  155. });
  156. /**
  157. * Initialize the outline generator from the CESIUM_primitive_outline
  158. * extension data. This updates the triangle indices and generates outline
  159. * coordinates, but does not update other attributes (see
  160. * {@link PrimitiveOutlineGenerator#updateAttribute})
  161. *
  162. * @param {PrimitiveOutlineGenerator} outlineGenerator The outline generator
  163. *
  164. * @private
  165. */
  166. function initialize(outlineGenerator) {
  167. // triangle indices may be extended from 16-bits to 32 bits if needed.
  168. let triangleIndices = outlineGenerator._triangleIndices;
  169. const edges = outlineGenerator._edges;
  170. const outlineCoordinates = [];
  171. const extraVertices = outlineGenerator._extraVertices;
  172. const vertexCount = outlineGenerator._originalVertexCount;
  173. // Dictionary of unmatchable vertex index -> copied vertex index. This is
  174. // used so we don't copy the same vertex more than necessary.
  175. const vertexCopies = {};
  176. // For each triangle, adjust vertex data so that the correct edges are outlined.
  177. for (let i = 0; i < triangleIndices.length; i += 3) {
  178. let i0 = triangleIndices[i];
  179. let i1 = triangleIndices[i + 1];
  180. let i2 = triangleIndices[i + 2];
  181. // Check which edges need to be outlined based on the contents of the
  182. // outline indices from the extension.
  183. const all = false; // set this to true to draw a full wireframe.
  184. const hasEdge01 = all || edges.hasEdge(i0, i1);
  185. const hasEdge12 = all || edges.hasEdge(i1, i2);
  186. const hasEdge20 = all || edges.hasEdge(i2, i0);
  187. // Attempt to compute outline coordinates. If no consistent ordering of
  188. // edges can be computed (due to constraints from adjacent faces), the
  189. // first attempt may fail. In such cases, make a copy of a vertex and
  190. // try again. This relaxes the constraints, so the while loop will
  191. // eventually finish.
  192. let unmatchableVertexIndex = matchAndStoreCoordinates(
  193. outlineCoordinates,
  194. i0,
  195. i1,
  196. i2,
  197. hasEdge01,
  198. hasEdge12,
  199. hasEdge20
  200. );
  201. while (defined(unmatchableVertexIndex)) {
  202. // Copy the unmatchable index and try again.
  203. let copy = vertexCopies[unmatchableVertexIndex];
  204. // Only copy if we haven't already
  205. if (!defined(copy)) {
  206. // The new vertex will appear at the end of the vertex list
  207. copy = vertexCount + extraVertices.length;
  208. // Sometimes the copied vertex will in turn be a copy, so search
  209. // for the original one
  210. let original = unmatchableVertexIndex;
  211. while (original >= vertexCount) {
  212. original = extraVertices[original - vertexCount];
  213. }
  214. // Store the original vertex that needs to be copied
  215. extraVertices.push(original);
  216. // mark that we've seen this unmatchable vertex before so we don't
  217. // copy it multiple times.
  218. vertexCopies[unmatchableVertexIndex] = copy;
  219. }
  220. // Corner case: copying a vertex may overflow the range of an
  221. // 8- or 16- bit index buffer, so upgrade to a larger data type.
  222. if (
  223. copy > MAX_GLTF_UINT16_INDEX &&
  224. (triangleIndices instanceof Uint16Array ||
  225. triangleIndices instanceof Uint8Array)
  226. ) {
  227. triangleIndices = new Uint32Array(triangleIndices);
  228. } else if (
  229. copy > MAX_GLTF_UINT8_INDEX &&
  230. triangleIndices instanceof Uint8Array
  231. ) {
  232. triangleIndices = new Uint16Array(triangleIndices);
  233. }
  234. // Update the triangle indices buffer to use the copied vertex instead
  235. // of the original one.
  236. if (unmatchableVertexIndex === i0) {
  237. i0 = copy;
  238. triangleIndices[i] = copy;
  239. } else if (unmatchableVertexIndex === i1) {
  240. i1 = copy;
  241. triangleIndices[i + 1] = copy;
  242. } else {
  243. i2 = copy;
  244. triangleIndices[i + 2] = copy;
  245. }
  246. // Attempt to generate outline coordinates again. This is more likely
  247. // to succeed since the copied vertex has no constraints on which order
  248. // of the 3 edges to use.
  249. unmatchableVertexIndex = matchAndStoreCoordinates(
  250. outlineCoordinates,
  251. i0,
  252. i1,
  253. i2,
  254. hasEdge01,
  255. hasEdge12,
  256. hasEdge20
  257. );
  258. }
  259. }
  260. // Store the triangle indices in case we had to expand to 32-bit indices
  261. outlineGenerator._triangleIndices = triangleIndices;
  262. outlineGenerator._outlineCoordinatesTypedArray = new Float32Array(
  263. outlineCoordinates
  264. );
  265. }
  266. /**
  267. * This function attempts to compute a valid ordering of edges for this triangle
  268. * and if found, computes outline coordinates for the three vertices. If not
  269. * possible, one of the vertices is returned so it can be copied.
  270. *
  271. * @param {number[]} outlineCoordinates An array to store the computed outline coordinates. There are 3 components per vertex. This will be modified in place.
  272. * @param {number} i0 The index of the first vertex of the triangle.
  273. * @param {number} i1 The index of the second vertex of the triangle.
  274. * @param {number} i2 The index of the third vertex of the triangle.
  275. * @param {boolean} hasEdge01 Whether there is an outline edge between vertices 0 and 1 of the triangle
  276. * @param {boolean} hasEdge12 Whether there is an outline edge between vertices 1 and 2 of the triangle
  277. * @param {boolean} hasEdge20 Whether there is an outline edge between vertices 2 and 0 of the triangle
  278. * @returns {number} If it's not possible to compute consistent outline coordinates for this triangle, the index of the most constrained vertex of i0, i1 and i2 is returned. Otherwise, this function returns undefined to indicate a successful match.
  279. *
  280. * @private
  281. */
  282. function matchAndStoreCoordinates(
  283. outlineCoordinates,
  284. i0,
  285. i1,
  286. i2,
  287. hasEdge01,
  288. hasEdge12,
  289. hasEdge20
  290. ) {
  291. const a0 = hasEdge20 ? 1.0 : 0.0;
  292. const b0 = hasEdge01 ? 1.0 : 0.0;
  293. const c0 = 0.0;
  294. const i0Mask = computeOrderMask(outlineCoordinates, i0, a0, b0, c0);
  295. if (i0Mask === 0) {
  296. return i0;
  297. }
  298. const a1 = 0.0;
  299. const b1 = hasEdge01 ? 1.0 : 0.0;
  300. const c1 = hasEdge12 ? 1.0 : 0.0;
  301. const i1Mask = computeOrderMask(outlineCoordinates, i1, a1, b1, c1);
  302. if (i1Mask === 0) {
  303. return i1;
  304. }
  305. const a2 = hasEdge20 ? 1.0 : 0.0;
  306. const b2 = 0.0;
  307. const c2 = hasEdge12 ? 1.0 : 0.0;
  308. const i2Mask = computeOrderMask(outlineCoordinates, i2, a2, b2, c2);
  309. if (i2Mask === 0) {
  310. return i2;
  311. }
  312. const workingOrders = i0Mask & i1Mask & i2Mask;
  313. let a, b, c;
  314. if (workingOrders & (1 << 0)) {
  315. // 0 - abc
  316. a = 0;
  317. b = 1;
  318. c = 2;
  319. } else if (workingOrders & (1 << 1)) {
  320. // 1 - acb
  321. a = 0;
  322. c = 1;
  323. b = 2;
  324. } else if (workingOrders & (1 << 2)) {
  325. // 2 - bac
  326. b = 0;
  327. a = 1;
  328. c = 2;
  329. } else if (workingOrders & (1 << 3)) {
  330. // 3 - bca
  331. b = 0;
  332. c = 1;
  333. a = 2;
  334. } else if (workingOrders & (1 << 4)) {
  335. // 4 - cab
  336. c = 0;
  337. a = 1;
  338. b = 2;
  339. } else if (workingOrders & (1 << 5)) {
  340. // 5 - cba
  341. c = 0;
  342. b = 1;
  343. a = 2;
  344. } else {
  345. // No ordering works. Report the most constrained vertex (i.e. the one with
  346. // fewest valid orderings) as unmatched so we copy that one.
  347. const i0ValidOrderCount = popcount6Bit(i0Mask);
  348. const i1ValidOrderCount = popcount6Bit(i1Mask);
  349. const i2ValidOrderCount = popcount6Bit(i2Mask);
  350. if (
  351. i0ValidOrderCount < i1ValidOrderCount &&
  352. i0ValidOrderCount < i2ValidOrderCount
  353. ) {
  354. return i0;
  355. } else if (i1ValidOrderCount < i2ValidOrderCount) {
  356. return i1;
  357. }
  358. return i2;
  359. }
  360. // We found a valid ordering of the edges, so store the outline coordinates
  361. // for this triangle.
  362. const i0Start = i0 * 3;
  363. outlineCoordinates[i0Start + a] = a0;
  364. outlineCoordinates[i0Start + b] = b0;
  365. outlineCoordinates[i0Start + c] = c0;
  366. const i1Start = i1 * 3;
  367. outlineCoordinates[i1Start + a] = a1;
  368. outlineCoordinates[i1Start + b] = b1;
  369. outlineCoordinates[i1Start + c] = c1;
  370. const i2Start = i2 * 3;
  371. outlineCoordinates[i2Start + a] = a2;
  372. outlineCoordinates[i2Start + b] = b2;
  373. outlineCoordinates[i2Start + c] = c2;
  374. // successful match
  375. return undefined;
  376. }
  377. /**
  378. * Each vertex has three coordinates, a, b, and c.
  379. * a is the coordinate that applies to edge 2-0 for the vertex.
  380. * b is the coordinate that applies to edge 0-1 for the vertex.
  381. * c is the coordinate that applies to edge 1-2 for the vertex.
  382. *
  383. * A single triangle with all edges highlighted:
  384. *
  385. * | a | b | c |
  386. * | 1 | 1 | 0 |
  387. * 0
  388. * / \
  389. * / \
  390. * edge 0-1 / \ edge 2-0
  391. * / \
  392. * / \
  393. * | a | b | c | 1-----------2 | a | b | c |
  394. * | 0 | 1 | 1 | edge 1-2 | 1 | 0 | 1 |
  395. *
  396. * There are 6 possible orderings of coordinates a, b, and c:
  397. * 0 - abc
  398. * 1 - acb
  399. * 2 - bac
  400. * 3 - bca
  401. * 4 - cab
  402. * 5 - cba
  403. *
  404. * All vertices must use the _same ordering_ for the edges to be rendered
  405. * correctly. So we compute a bitmask for each vertex, where the bit at
  406. * each position indicates whether that ordering works (i.e. doesn't
  407. * conflict with already-assigned coordinates) for that vertex.
  408. *
  409. * Then we can find an ordering that works for all three vertices with a
  410. * bitwise AND.
  411. *
  412. * @param {number[]} outlineCoordinates The array of outline coordinates
  413. * @param {number} vertexIndex The index of the vertex to compute the mask for
  414. * @param {number} a The outline coordinate for edge 2-0
  415. * @param {number} b The outline coordinate for edge 0-1
  416. * @param {number} c The outline coordinate for edge 1-2
  417. * @returns {number} A bitmask with 6 bits where a 1 indicates the corresponding ordering is valid.
  418. *
  419. * @private
  420. */
  421. function computeOrderMask(outlineCoordinates, vertexIndex, a, b, c) {
  422. const startIndex = vertexIndex * 3;
  423. const first = outlineCoordinates[startIndex];
  424. const second = outlineCoordinates[startIndex + 1];
  425. const third = outlineCoordinates[startIndex + 2];
  426. // If one coordinate is undefined, they all are since matchAndStoreCoordinates sets
  427. // all 3 components at once. In this case, all orderings are fine.
  428. if (!defined(first)) {
  429. return 0b111111;
  430. }
  431. return (
  432. ((first === a && second === b && third === c) << 0) |
  433. ((first === a && second === c && third === b) << 1) |
  434. ((first === b && second === a && third === c) << 2) |
  435. ((first === b && second === c && third === a) << 3) |
  436. ((first === c && second === a && third === b) << 4) |
  437. ((first === c && second === b && third === a) << 5)
  438. );
  439. }
  440. /**
  441. * Compute the popcount for 6-bit integers (values 0-63). This is the
  442. * number of 1s in the binary representation of the value.
  443. *
  444. * @param {number} value The value to compute the popcount for
  445. * @returns {number} The number of 1s in the binary representation of value
  446. *
  447. * @private
  448. */
  449. function popcount6Bit(value) {
  450. return (
  451. (value & 1) +
  452. ((value >> 1) & 1) +
  453. ((value >> 2) & 1) +
  454. ((value >> 3) & 1) +
  455. ((value >> 4) & 1) +
  456. ((value >> 5) & 1)
  457. );
  458. }
  459. /**
  460. * After computing the outline coordinates, some vertices may need to be
  461. * copied and appended to the end of the list of vertices. This function updates
  462. * a typed array for a single attribute (e.g. POSITION or NORMAL).
  463. *
  464. * @param {Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array} attributeTypedArray The attribute values to update. This function takes ownership of this typed array
  465. * @returns {Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array} A new typed array that contains the existing attribute values, plus any copied values at the end.
  466. *
  467. * @private
  468. */
  469. PrimitiveOutlineGenerator.prototype.updateAttribute = function (
  470. attributeTypedArray
  471. ) {
  472. const extraVertices = this._extraVertices;
  473. const originalLength = attributeTypedArray.length;
  474. // This is a stride in number of typed elements. For example, a VEC3 would
  475. // have a stride of 3 (floats)
  476. const stride = originalLength / this._originalVertexCount;
  477. const extraVerticesLength = extraVertices.length;
  478. // Make a larger typed array of the same type as the input
  479. const ArrayType = attributeTypedArray.constructor;
  480. const result = new ArrayType(
  481. attributeTypedArray.length + extraVerticesLength * stride
  482. );
  483. // Copy original vertices
  484. result.set(attributeTypedArray);
  485. // Copy the vertices added for outlining
  486. for (let i = 0; i < extraVerticesLength; i++) {
  487. const sourceIndex = extraVertices[i] * stride;
  488. const resultIndex = originalLength + i * stride;
  489. for (let j = 0; j < stride; j++) {
  490. result[resultIndex + j] = result[sourceIndex + j];
  491. }
  492. }
  493. return result;
  494. };
  495. /**
  496. * Create a mip-mapped lookup texture for rendering outlines. The texture is
  497. * constant, so it is cached on the context.
  498. *
  499. * @param {Context} context The context to use for creating the texture
  500. * @returns {Texture} The outline lookup texture.
  501. *
  502. * @private
  503. */
  504. PrimitiveOutlineGenerator.createTexture = function (context) {
  505. let cache = context.cache.modelOutliningCache;
  506. if (!defined(cache)) {
  507. cache = context.cache.modelOutliningCache = {};
  508. }
  509. if (defined(cache.outlineTexture)) {
  510. return cache.outlineTexture;
  511. }
  512. const maxSize = Math.min(4096, ContextLimits.maximumTextureSize);
  513. let size = maxSize;
  514. const levelZero = createMipLevel(size);
  515. const mipLevels = [];
  516. while (size > 1) {
  517. size >>= 1;
  518. mipLevels.push(createMipLevel(size));
  519. }
  520. const texture = new Texture({
  521. context: context,
  522. source: {
  523. arrayBufferView: levelZero,
  524. mipLevels: mipLevels,
  525. },
  526. width: maxSize,
  527. height: 1,
  528. pixelFormat: PixelFormat.LUMINANCE,
  529. sampler: new Sampler({
  530. wrapS: TextureWrap.CLAMP_TO_EDGE,
  531. wrapT: TextureWrap.CLAMP_TO_EDGE,
  532. minificationFilter: TextureMinificationFilter.LINEAR_MIPMAP_LINEAR,
  533. magnificationFilter: TextureMagnificationFilter.LINEAR,
  534. }),
  535. });
  536. cache.outlineTexture = texture;
  537. return texture;
  538. };
  539. /**
  540. * Create an outline lookup texture for a single mip level. This is a texture of
  541. * mostly 0 values, except for the last value which is brighter to indicate
  542. * the outline.
  543. *
  544. * @param {number} size The width of the texture for this mip level
  545. * @returns {Uint8Array} A typed array containing the texels of the mip level
  546. *
  547. * @private
  548. */
  549. function createMipLevel(size) {
  550. const texture = new Uint8Array(size);
  551. // This lookup texture creates an outline with width 0.75 px in screen space.
  552. texture[size - 1] = 192;
  553. // As we reach the top of the mip pyramid, a single set pixel becomes a
  554. // significant portion of the texture. This doesn't look great when zoomed
  555. // out, so attenuate the value by 50% at each level.
  556. if (size === 8) {
  557. texture[size - 1] = 96;
  558. } else if (size === 4) {
  559. texture[size - 1] = 48;
  560. } else if (size === 2) {
  561. texture[size - 1] = 24;
  562. } else if (size === 1) {
  563. texture[size - 1] = 12;
  564. }
  565. return texture;
  566. }
  567. /**
  568. * A hash set that provides quick lookups of whether an edge exists between
  569. * two vertices.
  570. *
  571. * @alias EdgeSet
  572. * @constructor
  573. *
  574. * @param {number[]} edgeIndices An array of vertex indices with an even number of elements where each pair of indices defines an edge.
  575. * @param {number} originalVertexCount The original number of vertices. This is used for computing a hash function.
  576. *
  577. * @private
  578. */
  579. function EdgeSet(edgeIndices, originalVertexCount) {
  580. /**
  581. * Original number of vertices in the primitive. This is used for computing
  582. * the hash key
  583. *
  584. * @type {number}
  585. *
  586. * @private
  587. */
  588. this._originalVertexCount = originalVertexCount;
  589. /**
  590. * The internal hash set used to store the edges. Edges are hashed as follows:
  591. * <p>
  592. * smallerVertexIndex * originalVertexCount + biggerVertexIndex
  593. * <p>
  594. * @type {Set}
  595. *
  596. * @private
  597. */
  598. this._edges = new Set();
  599. for (let i = 0; i < edgeIndices.length; i += 2) {
  600. const a = edgeIndices[i];
  601. const b = edgeIndices[i + 1];
  602. const small = Math.min(a, b);
  603. const big = Math.max(a, b);
  604. const hash = small * this._originalVertexCount + big;
  605. this._edges.add(hash);
  606. }
  607. }
  608. /**
  609. * Check if an edge exists in the set. The order of the input vertices does
  610. * not matter.
  611. * @param {number} a The first index
  612. * @param {number} b The second index
  613. * @returns {boolean} true if there is an edge between a and b
  614. *
  615. * @private
  616. */
  617. EdgeSet.prototype.hasEdge = function (a, b) {
  618. const small = Math.min(a, b);
  619. const big = Math.max(a, b);
  620. const hash = small * this._originalVertexCount + big;
  621. return this._edges.has(hash);
  622. };
  623. export default PrimitiveOutlineGenerator;