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

GltfSpzLoader.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import Check from "../Core/Check.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import RuntimeError from "../Core/RuntimeError.js";
  5. import ResourceLoader from "./ResourceLoader.js";
  6. import ResourceLoaderState from "./ResourceLoaderState.js";
  7. import { loadSpz } from "@spz-loader/core";
  8. // Cumulative number of SH coefficient floats per splat per channel for each
  9. // degree. Degree 0 has no extra SH data (base color is stored separately in
  10. // the "colors" attribute). Degrees 1-3 follow the standard SH basis count:
  11. // l=1 adds 3 bands × 3 channels = 9; l=2 adds 5 × 3 = 15 (total 24);
  12. // l=3 adds 7 × 3 = 21 (total 45).
  13. const SH_FLOATS_PER_SPLAT_BY_DEGREE = [0, 9, 24, 45];
  14. // Non-SH attribute floats per splat: position(3) + scale(3) + rotation(4)
  15. // + opacity(1) + color(3) = 14.
  16. const BASE_FLOATS_PER_SPLAT = 14;
  17. // The spz-loader WASM module is compiled with a signed 32-bit address space,
  18. // giving a hard ceiling of 2 GB. An additional factor of ~2× is required
  19. // because spz-loader copies every decoded C++ vector into a JavaScript
  20. // TypedArray. 1.6 GB is used as a conservative pre-flight threshold.
  21. const WASM_MEMORY_LIMIT_BYTES = 1.6 * 1024 * 1024 * 1024;
  22. /**
  23. * Derives the point count and maximum spherical harmonics degree for an SPZ
  24. * primitive from the glTF JSON, without touching the compressed binary data.
  25. * <p>
  26. * The SPZ payload is gzip-compressed and therefore cannot be inspected
  27. * directly. Instead, <code>numPoints</code> is read from the POSITION
  28. * accessor's <code>count</code> field and <code>shDegree</code> is inferred
  29. * from the highest-numbered <code>SH_DEGREE_n</code> attribute present in
  30. * the primitive. Returns <code>undefined</code> if the required information
  31. * is unavailable.
  32. * </p>
  33. * @param {object} gltf The glTF JSON object.
  34. * @param {object} primitive The glTF primitive object.
  35. * @returns {{ numPoints: number, shDegree: number }|undefined}
  36. * @private
  37. */
  38. function getSpzInfoFromGltf(gltf, primitive) {
  39. const attributes = primitive?.attributes;
  40. if (!defined(attributes)) {
  41. return undefined;
  42. }
  43. const positionAccessorId = attributes["POSITION"];
  44. if (!defined(positionAccessorId)) {
  45. return undefined;
  46. }
  47. const accessor = gltf?.accessors?.[positionAccessorId];
  48. if (!defined(accessor) || accessor.count <= 0) {
  49. return undefined;
  50. }
  51. let shDegree = 0;
  52. for (const semantic in attributes) {
  53. if (Object.prototype.hasOwnProperty.call(attributes, semantic)) {
  54. const match = /SH_DEGREE_(\d+)_COEF_/.exec(semantic);
  55. if (match) {
  56. shDegree = Math.max(shDegree, parseInt(match[1], 10));
  57. }
  58. }
  59. }
  60. return { numPoints: accessor.count, shDegree };
  61. }
  62. /**
  63. * Estimates the peak memory consumption (in bytes) of decoding an SPZ file
  64. * with the given parameters. The estimate accounts for both the WASM heap
  65. * allocations and the JavaScript TypedArray copies produced by spz-loader.
  66. * @param {number} numPoints Number of Gaussian splats.
  67. * @param {number} shDegree Spherical harmonics degree (0–3).
  68. * @returns {number} Estimated byte count.
  69. * @private
  70. */
  71. function estimateSpzMemoryBytes(numPoints, shDegree) {
  72. const floatsPerPoint =
  73. BASE_FLOATS_PER_SPLAT + (SH_FLOATS_PER_SPLAT_BY_DEGREE[shDegree] ?? 0);
  74. // ×2 accounts for WASM heap + JS TypedArray mirror.
  75. return numPoints * floatsPerPoint * Float32Array.BYTES_PER_ELEMENT * 2;
  76. }
  77. /**
  78. * Load a SPZ buffer from a glTF.
  79. * <p>
  80. * Implements the {@link ResourceLoader} interface.
  81. * </p>
  82. *
  83. * @private
  84. */
  85. class GltfSpzLoader extends ResourceLoader {
  86. /**
  87. * @param {object} options Object with the following properties:
  88. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  89. * @param {object} options.gltf The glTF JSON.
  90. * @param {object} options.primitive The primitive containing the SPZ extension.
  91. * @param {object} options.spz The SPZ extension object.
  92. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  93. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  94. * @param {string} [options.cacheKey] The cache key of the resource.
  95. */
  96. constructor(options) {
  97. super();
  98. options = options ?? Frozen.EMPTY_OBJECT;
  99. const resourceCache = options.resourceCache;
  100. const gltf = options.gltf;
  101. const primitive = options.primitive;
  102. const spz = options.spz;
  103. const gltfResource = options.gltfResource;
  104. const baseResource = options.baseResource;
  105. const cacheKey = options.cacheKey;
  106. //>>includeStart('debug', pragmas.debug);
  107. Check.typeOf.func("options.resourceCache", resourceCache);
  108. Check.typeOf.object("options.gltf", gltf);
  109. Check.typeOf.object("options.primitive", primitive);
  110. Check.typeOf.object("options.spz", spz);
  111. Check.typeOf.object("options.gltfResource", gltfResource);
  112. Check.typeOf.object("options.baseResource", baseResource);
  113. //>>includeEnd('debug');
  114. this._resourceCache = resourceCache;
  115. this._gltfResource = gltfResource;
  116. this._baseResource = baseResource;
  117. this._gltf = gltf;
  118. this._primitive = primitive;
  119. this._spz = spz;
  120. this._cacheKey = cacheKey;
  121. this._bufferViewLoader = undefined;
  122. this._bufferViewTypedArray = undefined;
  123. this._decodePromise = undefined;
  124. this._decodedData = undefined;
  125. this._state = ResourceLoaderState.UNLOADED;
  126. this._promise = undefined;
  127. this._spzError = undefined;
  128. }
  129. /**
  130. * The cache key of the resource.
  131. * @type {string}
  132. * @readonly
  133. * @private
  134. */
  135. get cacheKey() {
  136. return this._cacheKey;
  137. }
  138. /**
  139. * The decoded SPZ data.
  140. * @type {object}
  141. * @readonly
  142. * @private
  143. */
  144. get decodedData() {
  145. return this._decodedData;
  146. }
  147. /**
  148. * Loads the SPZ resource.
  149. * @returns {Promise<Resource>} A promise that resolves to the resource when the SPZ is loaded.
  150. * @private
  151. */
  152. async load() {
  153. if (defined(this._promise)) {
  154. return this._promise;
  155. }
  156. this._state = ResourceLoaderState.LOADING;
  157. this._promise = loadResources(this);
  158. return this._promise;
  159. }
  160. /**
  161. * Processes the SPZ resource.
  162. * @param {FrameState} frameState The frame state.
  163. * @private
  164. */
  165. process(frameState) {
  166. //>>includeStart('debug', pragmas.debug);
  167. Check.typeOf.object("frameState", frameState);
  168. //>>includeEnd('debug');
  169. if (this._state === ResourceLoaderState.READY) {
  170. return true;
  171. }
  172. if (this._state !== ResourceLoaderState.PROCESSING) {
  173. return false;
  174. }
  175. if (defined(this._spzError)) {
  176. handleError(this, this._spzError);
  177. }
  178. if (!defined(this._bufferViewTypedArray)) {
  179. return false;
  180. }
  181. if (defined(this._decodePromise)) {
  182. return false;
  183. }
  184. // Reject oversized SPZ payloads before invoking the WASM decoder.
  185. // The spz-loader WASM module has a hard 2 GB memory ceiling; exceeding
  186. // it causes an unrecoverable Aborted() call with no useful diagnostic.
  187. // See: https://github.com/CesiumGS/cesium/issues/13283
  188. //
  189. // The SPZ binary is gzip-compressed, so its header cannot be read
  190. // directly. Point count and SH degree are therefore derived from the
  191. // glTF JSON, which is available at this stage.
  192. const spzInfo = getSpzInfoFromGltf(this._gltf, this._primitive);
  193. if (defined(spzInfo)) {
  194. const estimatedBytes = estimateSpzMemoryBytes(
  195. spzInfo.numPoints,
  196. spzInfo.shDegree,
  197. );
  198. if (estimatedBytes > WASM_MEMORY_LIMIT_BYTES) {
  199. const estimatedMB = Math.round(estimatedBytes / (1024 * 1024));
  200. handleError(
  201. this,
  202. new RuntimeError(
  203. `SPZ data too large to decode: ${spzInfo.numPoints.toLocaleString()} splats ` +
  204. `with spherical harmonics degree ${spzInfo.shDegree} would require ` +
  205. `approximately ${estimatedMB} MB, which exceeds the WASM memory limit. ` +
  206. `Consider using a lower spherical harmonics degree or splitting the ` +
  207. `dataset into smaller tiles.`,
  208. ),
  209. );
  210. return false;
  211. }
  212. }
  213. const decodePromise = loadSpz(this._bufferViewTypedArray, {
  214. unpackOptions: { coordinateSystem: "UNSPECIFIED" },
  215. });
  216. if (!defined(decodePromise)) {
  217. return false;
  218. }
  219. this._decodePromise = processDecode(this, decodePromise);
  220. }
  221. /**
  222. * Unloads the SPZ resource and frees associated resources.
  223. * @private
  224. */
  225. unload() {
  226. if (defined(this._bufferViewLoader)) {
  227. this._resourceCache.unload(this._bufferViewLoader);
  228. }
  229. this._bufferViewLoader = undefined;
  230. this._bufferViewTypedArray = undefined;
  231. this._decodedData = undefined;
  232. this._gltf = undefined;
  233. this._primitive = undefined;
  234. }
  235. }
  236. async function loadResources(loader) {
  237. const resourceCache = loader._resourceCache;
  238. try {
  239. const bufferViewLoader = resourceCache.getBufferViewLoader({
  240. gltf: loader._gltf,
  241. bufferViewId: 0,
  242. gltfResource: loader._gltfResource,
  243. baseResource: loader._baseResource,
  244. });
  245. loader._bufferViewLoader = bufferViewLoader;
  246. await bufferViewLoader.load();
  247. if (loader.isDestroyed()) {
  248. return;
  249. }
  250. loader._bufferViewTypedArray = bufferViewLoader.typedArray;
  251. loader._state = ResourceLoaderState.PROCESSING;
  252. return loader;
  253. } catch (error) {
  254. if (loader.isDestroyed()) {
  255. return;
  256. }
  257. handleError(loader, error);
  258. }
  259. }
  260. function handleError(spzLoader, error) {
  261. spzLoader.unload();
  262. spzLoader._state = ResourceLoaderState.FAILED;
  263. const errorMessage = "Failed to load SPZ";
  264. throw spzLoader.getError(errorMessage, error);
  265. }
  266. async function processDecode(loader, decodePromise) {
  267. try {
  268. const gcloud = await decodePromise;
  269. if (loader.isDestroyed()) {
  270. return;
  271. }
  272. loader.unload();
  273. loader._decodedData = {
  274. gcloud: gcloud,
  275. };
  276. loader._state = ResourceLoaderState.READY;
  277. return loader._baseResource;
  278. } catch (error) {
  279. if (loader.isDestroyed()) {
  280. return;
  281. }
  282. loader._spzError = error;
  283. }
  284. }
  285. export { estimateSpzMemoryBytes, getSpzInfoFromGltf };
  286. export default GltfSpzLoader;