Batched3DModel3DTileContent.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Color from "../Core/Color.js";
  3. import ComponentDatatype from "../Core/ComponentDatatype.js";
  4. import defaultValue from "../Core/defaultValue.js";
  5. import defined from "../Core/defined.js";
  6. import deprecationWarning from "../Core/deprecationWarning.js";
  7. import destroyObject from "../Core/destroyObject.js";
  8. import DeveloperError from "../Core/DeveloperError.js";
  9. import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
  10. import Matrix4 from "../Core/Matrix4.js";
  11. import RequestType from "../Core/RequestType.js";
  12. import RuntimeError from "../Core/RuntimeError.js";
  13. import Pass from "../Renderer/Pass.js";
  14. import Axis from "./Axis.js";
  15. import Cesium3DTileBatchTable from "./Cesium3DTileBatchTable.js";
  16. import Cesium3DTileFeature from "./Cesium3DTileFeature.js";
  17. import Cesium3DTileFeatureTable from "./Cesium3DTileFeatureTable.js";
  18. import ClassificationModel from "./ClassificationModel.js";
  19. import Model from "./Model.js";
  20. import ModelUtility from "./ModelUtility.js";
  21. import ModelAnimationLoop from "./ModelAnimationLoop.js";
  22. /**
  23. * Represents the contents of a
  24. * {@link https://github.com/CesiumGS/3d-tiles/tree/master/specification/TileFormats/Batched3DModel|Batched 3D Model}
  25. * tile in a {@link https://github.com/CesiumGS/3d-tiles/tree/master/specification|3D Tiles} tileset.
  26. * <p>
  27. * Implements the {@link Cesium3DTileContent} interface.
  28. * </p>
  29. *
  30. * @alias Batched3DModel3DTileContent
  31. * @constructor
  32. *
  33. * @private
  34. */
  35. function Batched3DModel3DTileContent(
  36. tileset,
  37. tile,
  38. resource,
  39. arrayBuffer,
  40. byteOffset
  41. ) {
  42. this._tileset = tileset;
  43. this._tile = tile;
  44. this._resource = resource;
  45. this._model = undefined;
  46. this._batchTable = undefined;
  47. this._features = undefined;
  48. // Populate from gltf when available
  49. this._batchIdAttributeName = undefined;
  50. this._diffuseAttributeOrUniformName = {};
  51. this._rtcCenterTransform = undefined;
  52. this._contentModelMatrix = undefined;
  53. this.featurePropertiesDirty = false;
  54. initialize(this, arrayBuffer, byteOffset);
  55. }
  56. // This can be overridden for testing purposes
  57. Batched3DModel3DTileContent._deprecationWarning = deprecationWarning;
  58. Object.defineProperties(Batched3DModel3DTileContent.prototype, {
  59. featuresLength: {
  60. get: function () {
  61. return this._batchTable.featuresLength;
  62. },
  63. },
  64. pointsLength: {
  65. get: function () {
  66. return 0;
  67. },
  68. },
  69. trianglesLength: {
  70. get: function () {
  71. return this._model.trianglesLength;
  72. },
  73. },
  74. geometryByteLength: {
  75. get: function () {
  76. return this._model.geometryByteLength;
  77. },
  78. },
  79. texturesByteLength: {
  80. get: function () {
  81. return this._model.texturesByteLength;
  82. },
  83. },
  84. batchTableByteLength: {
  85. get: function () {
  86. return this._batchTable.memorySizeInBytes;
  87. },
  88. },
  89. innerContents: {
  90. get: function () {
  91. return undefined;
  92. },
  93. },
  94. readyPromise: {
  95. get: function () {
  96. return this._model.readyPromise;
  97. },
  98. },
  99. tileset: {
  100. get: function () {
  101. return this._tileset;
  102. },
  103. },
  104. tile: {
  105. get: function () {
  106. return this._tile;
  107. },
  108. },
  109. url: {
  110. get: function () {
  111. return this._resource.getUrlComponent(true);
  112. },
  113. },
  114. batchTable: {
  115. get: function () {
  116. return this._batchTable;
  117. },
  118. },
  119. });
  120. var sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  121. function getBatchIdAttributeName(gltf) {
  122. var batchIdAttributeName = ModelUtility.getAttributeOrUniformBySemantic(
  123. gltf,
  124. "_BATCHID"
  125. );
  126. if (!defined(batchIdAttributeName)) {
  127. batchIdAttributeName = ModelUtility.getAttributeOrUniformBySemantic(
  128. gltf,
  129. "BATCHID"
  130. );
  131. if (defined(batchIdAttributeName)) {
  132. Batched3DModel3DTileContent._deprecationWarning(
  133. "b3dm-legacy-batchid",
  134. "The glTF in this b3dm uses the semantic `BATCHID`. Application-specific semantics should be prefixed with an underscore: `_BATCHID`."
  135. );
  136. }
  137. }
  138. return batchIdAttributeName;
  139. }
  140. function getVertexShaderCallback(content) {
  141. return function (vs, programId) {
  142. var batchTable = content._batchTable;
  143. var handleTranslucent = !defined(content._tileset.classificationType);
  144. var gltf = content._model.gltf;
  145. if (defined(gltf)) {
  146. content._batchIdAttributeName = getBatchIdAttributeName(gltf);
  147. content._diffuseAttributeOrUniformName[
  148. programId
  149. ] = ModelUtility.getDiffuseAttributeOrUniform(gltf, programId);
  150. }
  151. var callback = batchTable.getVertexShaderCallback(
  152. handleTranslucent,
  153. content._batchIdAttributeName,
  154. content._diffuseAttributeOrUniformName[programId]
  155. );
  156. return defined(callback) ? callback(vs) : vs;
  157. };
  158. }
  159. function getFragmentShaderCallback(content) {
  160. return function (fs, programId) {
  161. var batchTable = content._batchTable;
  162. var handleTranslucent = !defined(content._tileset.classificationType);
  163. var gltf = content._model.gltf;
  164. if (defined(gltf)) {
  165. content._diffuseAttributeOrUniformName[
  166. programId
  167. ] = ModelUtility.getDiffuseAttributeOrUniform(gltf, programId);
  168. }
  169. var callback = batchTable.getFragmentShaderCallback(
  170. handleTranslucent,
  171. content._diffuseAttributeOrUniformName[programId]
  172. );
  173. return defined(callback) ? callback(fs) : fs;
  174. };
  175. }
  176. function getPickIdCallback(content) {
  177. return function () {
  178. return content._batchTable.getPickId();
  179. };
  180. }
  181. function getClassificationFragmentShaderCallback(content) {
  182. return function (fs) {
  183. var batchTable = content._batchTable;
  184. var callback = batchTable.getClassificationFragmentShaderCallback();
  185. return defined(callback) ? callback(fs) : fs;
  186. };
  187. }
  188. function createColorChangedCallback(content) {
  189. return function (batchId, color) {
  190. content._model.updateCommands(batchId, color);
  191. };
  192. }
  193. function initialize(content, arrayBuffer, byteOffset) {
  194. var tileset = content._tileset;
  195. var tile = content._tile;
  196. var resource = content._resource;
  197. var byteStart = defaultValue(byteOffset, 0);
  198. byteOffset = byteStart;
  199. var uint8Array = new Uint8Array(arrayBuffer);
  200. var view = new DataView(arrayBuffer);
  201. byteOffset += sizeOfUint32; // Skip magic
  202. var version = view.getUint32(byteOffset, true);
  203. if (version !== 1) {
  204. throw new RuntimeError(
  205. "Only Batched 3D Model version 1 is supported. Version " +
  206. version +
  207. " is not."
  208. );
  209. }
  210. byteOffset += sizeOfUint32;
  211. var byteLength = view.getUint32(byteOffset, true);
  212. byteOffset += sizeOfUint32;
  213. var featureTableJsonByteLength = view.getUint32(byteOffset, true);
  214. byteOffset += sizeOfUint32;
  215. var featureTableBinaryByteLength = view.getUint32(byteOffset, true);
  216. byteOffset += sizeOfUint32;
  217. var batchTableJsonByteLength = view.getUint32(byteOffset, true);
  218. byteOffset += sizeOfUint32;
  219. var batchTableBinaryByteLength = view.getUint32(byteOffset, true);
  220. byteOffset += sizeOfUint32;
  221. var batchLength;
  222. // Legacy header #1: [batchLength] [batchTableByteLength]
  223. // Legacy header #2: [batchTableJsonByteLength] [batchTableBinaryByteLength] [batchLength]
  224. // Current header: [featureTableJsonByteLength] [featureTableBinaryByteLength] [batchTableJsonByteLength] [batchTableBinaryByteLength]
  225. // If the header is in the first legacy format 'batchTableJsonByteLength' will be the start of the JSON string (a quotation mark) or the glTF magic.
  226. // Accordingly its first byte will be either 0x22 or 0x67, and so the minimum uint32 expected is 0x22000000 = 570425344 = 570MB. It is unlikely that the feature table JSON will exceed this length.
  227. // The check for the second legacy format is similar, except it checks 'batchTableBinaryByteLength' instead
  228. if (batchTableJsonByteLength >= 570425344) {
  229. // First legacy check
  230. byteOffset -= sizeOfUint32 * 2;
  231. batchLength = featureTableJsonByteLength;
  232. batchTableJsonByteLength = featureTableBinaryByteLength;
  233. batchTableBinaryByteLength = 0;
  234. featureTableJsonByteLength = 0;
  235. featureTableBinaryByteLength = 0;
  236. Batched3DModel3DTileContent._deprecationWarning(
  237. "b3dm-legacy-header",
  238. "This b3dm header is using the legacy format [batchLength] [batchTableByteLength]. The new format is [featureTableJsonByteLength] [featureTableBinaryByteLength] [batchTableJsonByteLength] [batchTableBinaryByteLength] from https://github.com/CesiumGS/3d-tiles/tree/master/specification/TileFormats/Batched3DModel."
  239. );
  240. } else if (batchTableBinaryByteLength >= 570425344) {
  241. // Second legacy check
  242. byteOffset -= sizeOfUint32;
  243. batchLength = batchTableJsonByteLength;
  244. batchTableJsonByteLength = featureTableJsonByteLength;
  245. batchTableBinaryByteLength = featureTableBinaryByteLength;
  246. featureTableJsonByteLength = 0;
  247. featureTableBinaryByteLength = 0;
  248. Batched3DModel3DTileContent._deprecationWarning(
  249. "b3dm-legacy-header",
  250. "This b3dm header is using the legacy format [batchTableJsonByteLength] [batchTableBinaryByteLength] [batchLength]. The new format is [featureTableJsonByteLength] [featureTableBinaryByteLength] [batchTableJsonByteLength] [batchTableBinaryByteLength] from https://github.com/CesiumGS/3d-tiles/tree/master/specification/TileFormats/Batched3DModel."
  251. );
  252. }
  253. var featureTableJson;
  254. if (featureTableJsonByteLength === 0) {
  255. featureTableJson = {
  256. BATCH_LENGTH: defaultValue(batchLength, 0),
  257. };
  258. } else {
  259. var featureTableString = getStringFromTypedArray(
  260. uint8Array,
  261. byteOffset,
  262. featureTableJsonByteLength
  263. );
  264. featureTableJson = JSON.parse(featureTableString);
  265. byteOffset += featureTableJsonByteLength;
  266. }
  267. var featureTableBinary = new Uint8Array(
  268. arrayBuffer,
  269. byteOffset,
  270. featureTableBinaryByteLength
  271. );
  272. byteOffset += featureTableBinaryByteLength;
  273. var featureTable = new Cesium3DTileFeatureTable(
  274. featureTableJson,
  275. featureTableBinary
  276. );
  277. batchLength = featureTable.getGlobalProperty("BATCH_LENGTH");
  278. featureTable.featuresLength = batchLength;
  279. var batchTableJson;
  280. var batchTableBinary;
  281. if (batchTableJsonByteLength > 0) {
  282. // PERFORMANCE_IDEA: is it possible to allocate this on-demand? Perhaps keep the
  283. // arraybuffer/string compressed in memory and then decompress it when it is first accessed.
  284. //
  285. // We could also make another request for it, but that would make the property set/get
  286. // API async, and would double the number of numbers in some cases.
  287. var batchTableString = getStringFromTypedArray(
  288. uint8Array,
  289. byteOffset,
  290. batchTableJsonByteLength
  291. );
  292. batchTableJson = JSON.parse(batchTableString);
  293. byteOffset += batchTableJsonByteLength;
  294. if (batchTableBinaryByteLength > 0) {
  295. // Has a batch table binary
  296. batchTableBinary = new Uint8Array(
  297. arrayBuffer,
  298. byteOffset,
  299. batchTableBinaryByteLength
  300. );
  301. // Copy the batchTableBinary section and let the underlying ArrayBuffer be freed
  302. batchTableBinary = new Uint8Array(batchTableBinary);
  303. byteOffset += batchTableBinaryByteLength;
  304. }
  305. }
  306. var colorChangedCallback;
  307. if (defined(tileset.classificationType)) {
  308. colorChangedCallback = createColorChangedCallback(content);
  309. }
  310. var batchTable = new Cesium3DTileBatchTable(
  311. content,
  312. batchLength,
  313. batchTableJson,
  314. batchTableBinary,
  315. colorChangedCallback
  316. );
  317. content._batchTable = batchTable;
  318. var gltfByteLength = byteStart + byteLength - byteOffset;
  319. if (gltfByteLength === 0) {
  320. throw new RuntimeError("glTF byte length must be greater than 0.");
  321. }
  322. var gltfView;
  323. if (byteOffset % 4 === 0) {
  324. gltfView = new Uint8Array(arrayBuffer, byteOffset, gltfByteLength);
  325. } else {
  326. // Create a copy of the glb so that it is 4-byte aligned
  327. Batched3DModel3DTileContent._deprecationWarning(
  328. "b3dm-glb-unaligned",
  329. "The embedded glb is not aligned to a 4-byte boundary."
  330. );
  331. gltfView = new Uint8Array(
  332. uint8Array.subarray(byteOffset, byteOffset + gltfByteLength)
  333. );
  334. }
  335. var pickObject = {
  336. content: content,
  337. primitive: tileset,
  338. };
  339. content._rtcCenterTransform = Matrix4.IDENTITY;
  340. var rtcCenter = featureTable.getGlobalProperty(
  341. "RTC_CENTER",
  342. ComponentDatatype.FLOAT,
  343. 3
  344. );
  345. if (defined(rtcCenter)) {
  346. content._rtcCenterTransform = Matrix4.fromTranslation(
  347. Cartesian3.fromArray(rtcCenter)
  348. );
  349. }
  350. content._contentModelMatrix = Matrix4.multiply(
  351. tile.computedTransform,
  352. content._rtcCenterTransform,
  353. new Matrix4()
  354. );
  355. if (!defined(tileset.classificationType)) {
  356. // PERFORMANCE_IDEA: patch the shader on demand, e.g., the first time show/color changes.
  357. // The pick shader still needs to be patched.
  358. content._model = new Model({
  359. gltf: gltfView,
  360. cull: false, // The model is already culled by 3D Tiles
  361. releaseGltfJson: true, // Models are unique and will not benefit from caching so save memory
  362. opaquePass: Pass.CESIUM_3D_TILE, // Draw opaque portions of the model during the 3D Tiles pass
  363. basePath: resource,
  364. requestType: RequestType.TILES3D,
  365. modelMatrix: content._contentModelMatrix,
  366. upAxis: tileset._gltfUpAxis,
  367. forwardAxis: Axis.X,
  368. shadows: tileset.shadows,
  369. debugWireframe: tileset.debugWireframe,
  370. incrementallyLoadTextures: false,
  371. vertexShaderLoaded: getVertexShaderCallback(content),
  372. fragmentShaderLoaded: getFragmentShaderCallback(content),
  373. uniformMapLoaded: batchTable.getUniformMapCallback(),
  374. pickIdLoaded: getPickIdCallback(content),
  375. addBatchIdToGeneratedShaders: batchLength > 0, // If the batch table has values in it, generated shaders will need a batchId attribute
  376. pickObject: pickObject,
  377. imageBasedLightingFactor: tileset.imageBasedLightingFactor,
  378. lightColor: tileset.lightColor,
  379. luminanceAtZenith: tileset.luminanceAtZenith,
  380. sphericalHarmonicCoefficients: tileset.sphericalHarmonicCoefficients,
  381. specularEnvironmentMaps: tileset.specularEnvironmentMaps,
  382. backFaceCulling: tileset.backFaceCulling,
  383. });
  384. content._model.readyPromise.then(function (model) {
  385. model.activeAnimations.addAll({
  386. loop: ModelAnimationLoop.REPEAT,
  387. });
  388. });
  389. } else {
  390. // This transcodes glTF to an internal representation for geometry so we can take advantage of the re-batching of vector data.
  391. // For a list of limitations on the input glTF, see the documentation for classificationType of Cesium3DTileset.
  392. content._model = new ClassificationModel({
  393. gltf: gltfView,
  394. cull: false, // The model is already culled by 3D Tiles
  395. basePath: resource,
  396. requestType: RequestType.TILES3D,
  397. modelMatrix: content._contentModelMatrix,
  398. upAxis: tileset._gltfUpAxis,
  399. forwardAxis: Axis.X,
  400. debugWireframe: tileset.debugWireframe,
  401. vertexShaderLoaded: getVertexShaderCallback(content),
  402. classificationShaderLoaded: getClassificationFragmentShaderCallback(
  403. content
  404. ),
  405. uniformMapLoaded: batchTable.getUniformMapCallback(),
  406. pickIdLoaded: getPickIdCallback(content),
  407. classificationType: tileset._classificationType,
  408. batchTable: batchTable,
  409. });
  410. }
  411. }
  412. function createFeatures(content) {
  413. var featuresLength = content.featuresLength;
  414. if (!defined(content._features) && featuresLength > 0) {
  415. var features = new Array(featuresLength);
  416. for (var i = 0; i < featuresLength; ++i) {
  417. features[i] = new Cesium3DTileFeature(content, i);
  418. }
  419. content._features = features;
  420. }
  421. }
  422. Batched3DModel3DTileContent.prototype.hasProperty = function (batchId, name) {
  423. return this._batchTable.hasProperty(batchId, name);
  424. };
  425. Batched3DModel3DTileContent.prototype.getFeature = function (batchId) {
  426. //>>includeStart('debug', pragmas.debug);
  427. var featuresLength = this.featuresLength;
  428. if (!defined(batchId) || batchId < 0 || batchId >= featuresLength) {
  429. throw new DeveloperError(
  430. "batchId is required and between zero and featuresLength - 1 (" +
  431. (featuresLength - 1) +
  432. ")."
  433. );
  434. }
  435. //>>includeEnd('debug');
  436. createFeatures(this);
  437. return this._features[batchId];
  438. };
  439. Batched3DModel3DTileContent.prototype.applyDebugSettings = function (
  440. enabled,
  441. color
  442. ) {
  443. color = enabled ? color : Color.WHITE;
  444. if (this.featuresLength === 0) {
  445. this._model.color = color;
  446. } else {
  447. this._batchTable.setAllColor(color);
  448. }
  449. };
  450. Batched3DModel3DTileContent.prototype.applyStyle = function (style) {
  451. if (this.featuresLength === 0) {
  452. var hasColorStyle = defined(style) && defined(style.color);
  453. var hasShowStyle = defined(style) && defined(style.show);
  454. this._model.color = hasColorStyle
  455. ? style.color.evaluateColor(undefined, this._model.color)
  456. : Color.clone(Color.WHITE, this._model.color);
  457. this._model.show = hasShowStyle ? style.show.evaluate(undefined) : true;
  458. } else {
  459. this._batchTable.applyStyle(style);
  460. }
  461. };
  462. Batched3DModel3DTileContent.prototype.update = function (tileset, frameState) {
  463. var commandStart = frameState.commandList.length;
  464. // In the PROCESSING state we may be calling update() to move forward
  465. // the content's resource loading. In the READY state, it will
  466. // actually generate commands.
  467. this._batchTable.update(tileset, frameState);
  468. this._contentModelMatrix = Matrix4.multiply(
  469. this._tile.computedTransform,
  470. this._rtcCenterTransform,
  471. this._contentModelMatrix
  472. );
  473. this._model.modelMatrix = this._contentModelMatrix;
  474. this._model.shadows = this._tileset.shadows;
  475. this._model.imageBasedLightingFactor = this._tileset.imageBasedLightingFactor;
  476. this._model.lightColor = this._tileset.lightColor;
  477. this._model.luminanceAtZenith = this._tileset.luminanceAtZenith;
  478. this._model.sphericalHarmonicCoefficients = this._tileset.sphericalHarmonicCoefficients;
  479. this._model.specularEnvironmentMaps = this._tileset.specularEnvironmentMaps;
  480. this._model.backFaceCulling = this._tileset.backFaceCulling;
  481. this._model.debugWireframe = this._tileset.debugWireframe;
  482. // Update clipping planes
  483. var tilesetClippingPlanes = this._tileset.clippingPlanes;
  484. this._model.clippingPlanesOriginMatrix = this._tileset.clippingPlanesOriginMatrix;
  485. if (defined(tilesetClippingPlanes) && this._tile.clippingPlanesDirty) {
  486. // Dereference the clipping planes from the model if they are irrelevant.
  487. // Link/Dereference directly to avoid ownership checks.
  488. // This will also trigger synchronous shader regeneration to remove or add the clipping plane and color blending code.
  489. this._model._clippingPlanes =
  490. tilesetClippingPlanes.enabled && this._tile._isClipped
  491. ? tilesetClippingPlanes
  492. : undefined;
  493. }
  494. // If the model references a different ClippingPlaneCollection due to the tileset's collection being replaced with a
  495. // ClippingPlaneCollection that gives this tile the same clipping status, update the model to use the new ClippingPlaneCollection.
  496. if (
  497. defined(tilesetClippingPlanes) &&
  498. defined(this._model._clippingPlanes) &&
  499. this._model._clippingPlanes !== tilesetClippingPlanes
  500. ) {
  501. this._model._clippingPlanes = tilesetClippingPlanes;
  502. }
  503. this._model.update(frameState);
  504. // If any commands were pushed, add derived commands
  505. var commandEnd = frameState.commandList.length;
  506. if (
  507. commandStart < commandEnd &&
  508. (frameState.passes.render || frameState.passes.pick) &&
  509. !defined(tileset.classificationType)
  510. ) {
  511. this._batchTable.addDerivedCommands(frameState, commandStart);
  512. }
  513. };
  514. Batched3DModel3DTileContent.prototype.isDestroyed = function () {
  515. return false;
  516. };
  517. Batched3DModel3DTileContent.prototype.destroy = function () {
  518. this._model = this._model && this._model.destroy();
  519. this._batchTable = this._batchTable && this._batchTable.destroy();
  520. return destroyObject(this);
  521. };
  522. export default Batched3DModel3DTileContent;