智慧水务管理系统 - 精河县供水工程综合管理平台

GltfVertexBufferLoader.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. import Check from "../Core/Check.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import Buffer from "../Renderer/Buffer.js";
  6. import BufferUsage from "../Renderer/BufferUsage.js";
  7. import AttributeType from "./AttributeType.js";
  8. import JobType from "./JobType.js";
  9. import ModelComponents from "./ModelComponents.js";
  10. import ResourceLoader from "./ResourceLoader.js";
  11. import ResourceLoaderState from "./ResourceLoaderState.js";
  12. import CesiumMath from "../Core/Math.js";
  13. /**
  14. * Loads a vertex buffer from a glTF buffer view.
  15. * <p>
  16. * Implements the {@link ResourceLoader} interface.
  17. * </p>
  18. *
  19. * @private
  20. */
  21. class GltfVertexBufferLoader extends ResourceLoader {
  22. /**
  23. * @param {object} options Object with the following properties:
  24. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  25. * @param {object} options.gltf The glTF JSON.
  26. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  27. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  28. * @param {number} [options.bufferViewId] The bufferView ID corresponding to the vertex buffer.
  29. * @param {object} [options.primitive] The primitive containing the Draco extension.
  30. * @param {object} [options.draco] The Draco extension object.
  31. * @param {string} [options.attributeSemantic] The attribute semantic, e.g. POSITION or NORMAL.
  32. * @param {number} [options.accessorId] The accessor id.
  33. * @param {string} [options.cacheKey] The cache key of the resource.
  34. * @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  35. * @param {boolean} [options.loadBuffer=false] Load vertex buffer as a GPU vertex buffer.
  36. * @param {boolean} [options.loadTypedArray=false] Load vertex buffer as a typed array.
  37. *
  38. * @exception {DeveloperError} One of options.bufferViewId and options.draco must be defined.
  39. * @exception {DeveloperError} When options.draco is defined options.attributeSemantic must also be defined.
  40. * @exception {DeveloperError} When options.draco is defined options.accessorId must also be defined.
  41. */
  42. constructor(options) {
  43. super();
  44. options = options ?? Frozen.EMPTY_OBJECT;
  45. const resourceCache = options.resourceCache;
  46. const gltf = options.gltf;
  47. const gltfResource = options.gltfResource;
  48. const baseResource = options.baseResource;
  49. const bufferViewId = options.bufferViewId;
  50. const primitive = options.primitive;
  51. const draco = options.draco;
  52. const attributeSemantic = options.attributeSemantic;
  53. const accessorId = options.accessorId;
  54. const cacheKey = options.cacheKey;
  55. const spz = options.spz;
  56. const asynchronous = options.asynchronous ?? true;
  57. const loadBuffer = options.loadBuffer ?? false;
  58. const loadTypedArray = options.loadTypedArray ?? false;
  59. //>>includeStart('debug', pragmas.debug);
  60. Check.typeOf.func("options.resourceCache", resourceCache);
  61. Check.typeOf.object("options.gltf", gltf);
  62. Check.typeOf.object("options.gltfResource", gltfResource);
  63. Check.typeOf.object("options.baseResource", baseResource);
  64. if (!loadBuffer && !loadTypedArray) {
  65. throw new DeveloperError(
  66. "At least one of loadBuffer and loadTypedArray must be true.",
  67. );
  68. }
  69. const hasBufferViewId = defined(bufferViewId);
  70. const hasPrimitive = defined(primitive);
  71. const hasDraco = hasDracoCompression(draco, attributeSemantic);
  72. const hasAttributeSemantic = defined(attributeSemantic);
  73. const hasAccessorId = defined(accessorId);
  74. const hasSpz = defined(spz);
  75. if (hasBufferViewId === (hasDraco !== hasSpz)) {
  76. throw new DeveloperError(
  77. "One of options.bufferViewId, options.draco, or options.spz must be defined.",
  78. );
  79. }
  80. if (hasDraco && !hasAttributeSemantic) {
  81. throw new DeveloperError(
  82. "When options.draco is defined options.attributeSemantic must also be defined.",
  83. );
  84. }
  85. if (hasDraco && !hasAccessorId) {
  86. throw new DeveloperError(
  87. "When options.draco is defined options.accessorId must also be defined.",
  88. );
  89. }
  90. if (hasDraco && !hasPrimitive) {
  91. throw new DeveloperError(
  92. "When options.draco is defined options.primitive must also be defined.",
  93. );
  94. }
  95. if (hasDraco) {
  96. Check.typeOf.object("options.primitive", primitive);
  97. Check.typeOf.object("options.draco", draco);
  98. Check.typeOf.string("options.attributeSemantic", attributeSemantic);
  99. Check.typeOf.number("options.accessorId", accessorId);
  100. }
  101. //>>includeEnd('debug');
  102. this._resourceCache = resourceCache;
  103. this._gltfResource = gltfResource;
  104. this._baseResource = baseResource;
  105. this._gltf = gltf;
  106. this._bufferViewId = bufferViewId;
  107. this._primitive = primitive;
  108. this._draco = draco;
  109. this._spz = spz;
  110. this._attributeSemantic = attributeSemantic;
  111. this._accessorId = accessorId;
  112. this._cacheKey = cacheKey;
  113. this._asynchronous = asynchronous;
  114. this._loadBuffer = loadBuffer;
  115. this._loadTypedArray = loadTypedArray;
  116. this._bufferViewLoader = undefined;
  117. this._dracoLoader = undefined;
  118. this._quantization = undefined;
  119. this._typedArray = undefined;
  120. this._buffer = undefined;
  121. this._state = ResourceLoaderState.UNLOADED;
  122. this._promise = undefined;
  123. }
  124. /**
  125. * The cache key of the resource.
  126. *
  127. *
  128. * @type {string}
  129. * @readonly
  130. * @private
  131. */
  132. get cacheKey() {
  133. return this._cacheKey;
  134. }
  135. /**
  136. * The vertex buffer. This is only defined when <code>loadAsTypedArray</code> is false.
  137. *
  138. *
  139. * @type {Buffer}
  140. * @readonly
  141. * @private
  142. */
  143. get buffer() {
  144. return this._buffer;
  145. }
  146. /**
  147. * The typed array containing vertex buffer data. This is only defined when <code>loadAsTypedArray</code> is true.
  148. *
  149. *
  150. * @type {Uint8Array}
  151. * @readonly
  152. * @private
  153. */
  154. get typedArray() {
  155. return this._typedArray;
  156. }
  157. /**
  158. * Information about the quantized vertex attribute after Draco decode.
  159. *
  160. *
  161. * @type {ModelComponents.Quantization}
  162. * @readonly
  163. * @private
  164. */
  165. get quantization() {
  166. return this._quantization;
  167. }
  168. /**
  169. * Loads the resource.
  170. * @returns {Promise<GltfVertexBufferLoader>} A promise which resolves to the loader when the resource loading is completed.
  171. * @private
  172. */
  173. async load() {
  174. if (defined(this._promise)) {
  175. return this._promise;
  176. }
  177. if (defined(this._spz)) {
  178. this._promise = loadFromSpz(this);
  179. return this._promise;
  180. }
  181. if (hasDracoCompression(this._draco, this._attributeSemantic)) {
  182. this._promise = loadFromDraco(this);
  183. return this._promise;
  184. }
  185. this._promise = loadFromBufferView(this);
  186. return this._promise;
  187. }
  188. /**
  189. * Processes the resource until it becomes ready.
  190. *
  191. * @param {FrameState} frameState The frame state.
  192. * @private
  193. */
  194. process(frameState) {
  195. //>>includeStart('debug', pragmas.debug);
  196. Check.typeOf.object("frameState", frameState);
  197. //>>includeEnd('debug');
  198. if (this._state === ResourceLoaderState.READY) {
  199. return true;
  200. }
  201. if (
  202. this._state !== ResourceLoaderState.LOADED &&
  203. this._state !== ResourceLoaderState.PROCESSING
  204. ) {
  205. return false;
  206. }
  207. if (defined(this._dracoLoader)) {
  208. try {
  209. const ready = this._dracoLoader.process(frameState);
  210. if (!ready) {
  211. return false;
  212. }
  213. } catch (error) {
  214. handleError(this, error);
  215. }
  216. processDraco(this);
  217. }
  218. if (defined(this._spzLoader)) {
  219. try {
  220. const ready = this._spzLoader.process(frameState);
  221. if (!ready) {
  222. return false;
  223. }
  224. } catch (error) {
  225. handleError(this, error);
  226. }
  227. processSpz(this);
  228. }
  229. let buffer;
  230. const typedArray = this._typedArray;
  231. if (this._loadBuffer && this._asynchronous) {
  232. const vertexBufferJob = scratchVertexBufferJob;
  233. vertexBufferJob.set(typedArray, frameState.context);
  234. const jobScheduler = frameState.jobScheduler;
  235. if (!jobScheduler.execute(vertexBufferJob, JobType.BUFFER)) {
  236. // Job scheduler is full. Try again next frame.
  237. return false;
  238. }
  239. buffer = vertexBufferJob.buffer;
  240. } else if (this._loadBuffer) {
  241. buffer = createVertexBuffer(typedArray, frameState.context);
  242. }
  243. // Unload everything except the vertex buffer
  244. this.unload();
  245. this._buffer = buffer;
  246. this._typedArray = this._loadTypedArray ? typedArray : undefined;
  247. this._state = ResourceLoaderState.READY;
  248. this._resourceCache.statistics.addGeometryLoader(this);
  249. return true;
  250. }
  251. /**
  252. * Unloads the resource.
  253. * @private
  254. */
  255. unload() {
  256. if (defined(this._buffer)) {
  257. this._buffer.destroy();
  258. }
  259. const resourceCache = this._resourceCache;
  260. if (
  261. defined(this._bufferViewLoader) &&
  262. !this._bufferViewLoader.isDestroyed()
  263. ) {
  264. resourceCache.unload(this._bufferViewLoader);
  265. }
  266. if (defined(this._dracoLoader)) {
  267. resourceCache.unload(this._dracoLoader);
  268. }
  269. if (defined(this._spzLoader)) {
  270. resourceCache.unload(this._spzLoader);
  271. }
  272. this._bufferViewLoader = undefined;
  273. this._dracoLoader = undefined;
  274. this._spzLoader = undefined;
  275. this._typedArray = undefined;
  276. this._buffer = undefined;
  277. this._gltf = undefined;
  278. this._primitive = undefined;
  279. }
  280. }
  281. function hasDracoCompression(draco, semantic) {
  282. return (
  283. defined(draco) &&
  284. defined(draco.attributes) &&
  285. defined(draco.attributes[semantic])
  286. );
  287. }
  288. function getQuantizationInformation(
  289. dracoQuantization,
  290. componentDatatype,
  291. componentCount,
  292. type,
  293. ) {
  294. const quantizationBits = dracoQuantization.quantizationBits;
  295. const normalizationRange = (1 << quantizationBits) - 1;
  296. const normalizationDivisor = 1.0 / normalizationRange;
  297. const quantization = new ModelComponents.Quantization();
  298. quantization.componentDatatype = componentDatatype;
  299. quantization.octEncoded = dracoQuantization.octEncoded;
  300. quantization.octEncodedZXY = true;
  301. quantization.type = type;
  302. if (quantization.octEncoded) {
  303. quantization.type = AttributeType.VEC2;
  304. quantization.normalizationRange = normalizationRange;
  305. } else {
  306. const MathType = AttributeType.getMathType(type);
  307. if (MathType === Number) {
  308. const dimensions = dracoQuantization.range;
  309. quantization.quantizedVolumeOffset = dracoQuantization.minValues[0];
  310. quantization.quantizedVolumeDimensions = dimensions;
  311. quantization.normalizationRange = normalizationRange;
  312. quantization.quantizedVolumeStepSize = dimensions * normalizationDivisor;
  313. } else {
  314. quantization.quantizedVolumeOffset = MathType.unpack(
  315. dracoQuantization.minValues,
  316. );
  317. quantization.normalizationRange = MathType.unpack(
  318. new Array(componentCount).fill(normalizationRange),
  319. );
  320. const packedDimensions = new Array(componentCount).fill(
  321. dracoQuantization.range,
  322. );
  323. quantization.quantizedVolumeDimensions =
  324. MathType.unpack(packedDimensions);
  325. // Computing the step size
  326. const packedSteps = packedDimensions.map(function (dimension) {
  327. return dimension * normalizationDivisor;
  328. });
  329. quantization.quantizedVolumeStepSize = MathType.unpack(packedSteps);
  330. }
  331. }
  332. return quantization;
  333. }
  334. async function loadFromSpz(vertexBufferLoader) {
  335. vertexBufferLoader._state = ResourceLoaderState.LOADING;
  336. const resourceCache = vertexBufferLoader._resourceCache;
  337. try {
  338. const spzLoader = resourceCache.getSpzLoader({
  339. gltf: vertexBufferLoader._gltf,
  340. primitive: vertexBufferLoader._primitive,
  341. spz: vertexBufferLoader._spz,
  342. gltfResource: vertexBufferLoader._gltfResource,
  343. baseResource: vertexBufferLoader._baseResource,
  344. });
  345. vertexBufferLoader._spzLoader = spzLoader;
  346. await spzLoader.load();
  347. if (vertexBufferLoader.isDestroyed()) {
  348. return;
  349. }
  350. vertexBufferLoader._state = ResourceLoaderState.LOADED;
  351. return vertexBufferLoader;
  352. } catch {
  353. if (vertexBufferLoader.isDestroyed()) {
  354. return;
  355. }
  356. }
  357. }
  358. function getShAttributePrefix(attribute) {
  359. const prefix = attribute.startsWith("KHR_gaussian_splatting:")
  360. ? "KHR_gaussian_splatting:"
  361. : "_";
  362. return `${prefix}SH_DEGREE_`;
  363. }
  364. function extractSHDegreeAndCoef(attribute) {
  365. const prefix = getShAttributePrefix(attribute);
  366. const separator = "_COEF_";
  367. const lStart = prefix.length;
  368. const coefIndex = attribute.indexOf(separator, lStart);
  369. const l = parseInt(attribute.slice(lStart, coefIndex), 10);
  370. const n = parseInt(attribute.slice(coefIndex + separator.length), 10);
  371. return { l, n };
  372. }
  373. function processSpz(vertexBufferLoader) {
  374. vertexBufferLoader._state = ResourceLoaderState.PROCESSING;
  375. const spzLoader = vertexBufferLoader._spzLoader;
  376. const gcloudData = spzLoader.decodedData.gcloud;
  377. if (vertexBufferLoader._attributeSemantic === "POSITION") {
  378. vertexBufferLoader._typedArray = gcloudData.positions;
  379. } else if (
  380. vertexBufferLoader._attributeSemantic === "KHR_gaussian_splatting:SCALE" ||
  381. vertexBufferLoader._attributeSemantic === "_SCALE"
  382. ) {
  383. vertexBufferLoader._typedArray = gcloudData.scales;
  384. } else if (
  385. vertexBufferLoader._attributeSemantic ===
  386. "KHR_gaussian_splatting:ROTATION" ||
  387. vertexBufferLoader._attributeSemantic === "_ROTATION"
  388. ) {
  389. vertexBufferLoader._typedArray = gcloudData.rotations;
  390. } else if (vertexBufferLoader._attributeSemantic === "COLOR_0") {
  391. const colors = gcloudData.colors;
  392. const alphas = gcloudData.alphas;
  393. vertexBufferLoader._typedArray = new Uint8Array((colors.length / 3) * 4);
  394. for (let i = 0; i < colors.length / 3; i++) {
  395. vertexBufferLoader._typedArray[i * 4] = CesiumMath.clamp(
  396. colors[i * 3] * 255.0,
  397. 0.0,
  398. 255.0,
  399. );
  400. vertexBufferLoader._typedArray[i * 4 + 1] = CesiumMath.clamp(
  401. colors[i * 3 + 1] * 255.0,
  402. 0.0,
  403. 255.0,
  404. );
  405. vertexBufferLoader._typedArray[i * 4 + 2] = CesiumMath.clamp(
  406. colors[i * 3 + 2] * 255.0,
  407. 0.0,
  408. 255.0,
  409. );
  410. vertexBufferLoader._typedArray[i * 4 + 3] = CesiumMath.clamp(
  411. alphas[i] * 255.0,
  412. 0.0,
  413. 255.0,
  414. );
  415. }
  416. } else if (vertexBufferLoader._attributeSemantic.includes("SH_DEGREE_")) {
  417. const { l, n } = extractSHDegreeAndCoef(
  418. vertexBufferLoader._attributeSemantic,
  419. );
  420. const sphericalHarmonicDegree = gcloudData.shDegree;
  421. let stride = 0;
  422. const base = [0, 9, 24];
  423. switch (sphericalHarmonicDegree) {
  424. case 1:
  425. stride = 9;
  426. break;
  427. case 2:
  428. stride = 24;
  429. break;
  430. case 3:
  431. stride = 45;
  432. break;
  433. }
  434. const count = gcloudData.numPoints;
  435. const sh = gcloudData.sh;
  436. vertexBufferLoader._typedArray = new Float32Array(count * 3);
  437. for (let i = 0; i < count; i++) {
  438. const idx = i * stride + base[l - 1] + n * 3;
  439. vertexBufferLoader._typedArray[i * 3] = sh[idx];
  440. vertexBufferLoader._typedArray[i * 3 + 1] = sh[idx + 1];
  441. vertexBufferLoader._typedArray[i * 3 + 2] = sh[idx + 2];
  442. }
  443. }
  444. }
  445. async function loadFromDraco(vertexBufferLoader) {
  446. vertexBufferLoader._state = ResourceLoaderState.LOADING;
  447. const resourceCache = vertexBufferLoader._resourceCache;
  448. try {
  449. const dracoLoader = resourceCache.getDracoLoader({
  450. gltf: vertexBufferLoader._gltf,
  451. primitive: vertexBufferLoader._primitive,
  452. draco: vertexBufferLoader._draco,
  453. gltfResource: vertexBufferLoader._gltfResource,
  454. baseResource: vertexBufferLoader._baseResource,
  455. });
  456. vertexBufferLoader._dracoLoader = dracoLoader;
  457. await dracoLoader.load();
  458. if (vertexBufferLoader.isDestroyed()) {
  459. return;
  460. }
  461. // Now wait for process() to run to finish loading
  462. vertexBufferLoader._state = ResourceLoaderState.LOADED;
  463. return vertexBufferLoader;
  464. } catch {
  465. if (vertexBufferLoader.isDestroyed()) {
  466. return;
  467. }
  468. handleError(vertexBufferLoader);
  469. }
  470. }
  471. function processDraco(vertexBufferLoader) {
  472. vertexBufferLoader._state = ResourceLoaderState.PROCESSING;
  473. const dracoLoader = vertexBufferLoader._dracoLoader;
  474. // Get the typed array and quantization information
  475. const decodedVertexAttributes = dracoLoader.decodedData.vertexAttributes;
  476. const attributeSemantic = vertexBufferLoader._attributeSemantic;
  477. const dracoAttribute = decodedVertexAttributes[attributeSemantic];
  478. const accessorId = vertexBufferLoader._accessorId;
  479. const accessor = vertexBufferLoader._gltf.accessors[accessorId];
  480. const type = accessor.type;
  481. const typedArray = dracoAttribute.array;
  482. const dracoQuantization = dracoAttribute.data.quantization;
  483. if (defined(dracoQuantization)) {
  484. vertexBufferLoader._quantization = getQuantizationInformation(
  485. dracoQuantization,
  486. dracoAttribute.data.componentDatatype,
  487. dracoAttribute.data.componentsPerAttribute,
  488. type,
  489. );
  490. }
  491. vertexBufferLoader._typedArray = new Uint8Array(
  492. typedArray.buffer,
  493. typedArray.byteOffset,
  494. typedArray.byteLength,
  495. );
  496. }
  497. async function loadFromBufferView(vertexBufferLoader) {
  498. vertexBufferLoader._state = ResourceLoaderState.LOADING;
  499. const resourceCache = vertexBufferLoader._resourceCache;
  500. try {
  501. const bufferViewLoader = resourceCache.getBufferViewLoader({
  502. gltf: vertexBufferLoader._gltf,
  503. bufferViewId: vertexBufferLoader._bufferViewId,
  504. gltfResource: vertexBufferLoader._gltfResource,
  505. baseResource: vertexBufferLoader._baseResource,
  506. });
  507. vertexBufferLoader._bufferViewLoader = bufferViewLoader;
  508. await bufferViewLoader.load();
  509. if (vertexBufferLoader.isDestroyed()) {
  510. return;
  511. }
  512. vertexBufferLoader._typedArray = bufferViewLoader.typedArray;
  513. vertexBufferLoader._state = ResourceLoaderState.PROCESSING;
  514. return vertexBufferLoader;
  515. } catch (error) {
  516. if (vertexBufferLoader.isDestroyed()) {
  517. return;
  518. }
  519. handleError(vertexBufferLoader, error);
  520. }
  521. }
  522. function handleError(vertexBufferLoader, error) {
  523. vertexBufferLoader.unload();
  524. vertexBufferLoader._state = ResourceLoaderState.FAILED;
  525. const errorMessage = "Failed to load vertex buffer";
  526. throw vertexBufferLoader.getError(errorMessage, error);
  527. }
  528. class CreateVertexBufferJob {
  529. constructor() {
  530. this.typedArray = undefined;
  531. this.context = undefined;
  532. this.buffer = undefined;
  533. }
  534. set(typedArray, context) {
  535. this.typedArray = typedArray;
  536. this.context = context;
  537. }
  538. execute() {
  539. this.buffer = createVertexBuffer(this.typedArray, this.context);
  540. }
  541. }
  542. function createVertexBuffer(typedArray, context) {
  543. const buffer = Buffer.createVertexBuffer({
  544. typedArray: typedArray,
  545. context: context,
  546. usage: BufferUsage.STATIC_DRAW,
  547. });
  548. buffer.vertexArrayDestroyable = false;
  549. return buffer;
  550. }
  551. const scratchVertexBufferJob = new CreateVertexBufferJob();
  552. export default GltfVertexBufferLoader;