diff --git a/CHANGES.md b/CHANGES.md index 104739a56e1a..3d88384c0ab7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ ##### Additions :tada: - Added `Cesium3DTileStyle.fromUrl` for loading a style from a url. [#10348](https://github.com/CesiumGS/cesium/pull/10348) +- Added `IndexDatatype.fromTypedArray`. [#10350](https://github.com/CesiumGS/cesium/pull/10350) ##### Fixes :wrench: diff --git a/Source/Core/ComponentDatatype.js b/Source/Core/ComponentDatatype.js index ebdb5055d523..b7c8d3439fc5 100644 --- a/Source/Core/ComponentDatatype.js +++ b/Source/Core/ComponentDatatype.js @@ -165,6 +165,12 @@ ComponentDatatype.fromTypedArray = function (array) { if (array instanceof Float64Array) { return ComponentDatatype.DOUBLE; } + + //>>includeStart('debug', pragmas.debug); + throw new DeveloperError( + "array must be an Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, or Float64Array." + ); + //>>includeEnd('debug'); }; /** diff --git a/Source/Core/IndexDatatype.js b/Source/Core/IndexDatatype.js index 433eeb7a9ebb..70f4457a8978 100644 --- a/Source/Core/IndexDatatype.js +++ b/Source/Core/IndexDatatype.js @@ -171,4 +171,29 @@ IndexDatatype.createTypedArrayFromArrayBuffer = function ( return new Uint16Array(sourceArray, byteOffset, length); }; + +/** + * Gets the {@link IndexDatatype} for the provided TypedArray instance. + * + * @param {Uint8Array|Uint16Array|Uint32Array} array The typed array. + * @returns {IndexDatatype} The IndexDatatype for the provided array, or undefined if the array is not a Uint8Array, Uint16Array, or Uint32Array. + */ +IndexDatatype.fromTypedArray = function (array) { + if (array instanceof Uint8Array) { + return IndexDatatype.UNSIGNED_BYTE; + } + if (array instanceof Uint16Array) { + return IndexDatatype.UNSIGNED_SHORT; + } + if (array instanceof Uint32Array) { + return IndexDatatype.UNSIGNED_INT; + } + + //>>includeStart('debug', pragmas.debug); + throw new DeveloperError( + "array must be a Uint8Array, Uint16Array, or Uint32Array." + ); + //>>includeEnd('debug'); +}; + export default Object.freeze(IndexDatatype); diff --git a/Source/Scene/PointCloud.js b/Source/Scene/PointCloud.js index 4991c5773a16..a47c5f03f2ab 100644 --- a/Source/Scene/PointCloud.js +++ b/Source/Scene/PointCloud.js @@ -367,7 +367,7 @@ function createResources(pointCloud, frameState) { const positions = parsedContent.positions; const colors = parsedContent.colors; const normals = parsedContent.normals; - let batchIds = parsedContent.batchIds; + const batchIds = parsedContent.batchIds; const styleableProperties = parsedContent.styleableProperties; const hasStyleableProperties = defined(styleableProperties); const isQuantized = pointCloud._isQuantized; @@ -456,7 +456,10 @@ function createResources(pointCloud, frameState) { let batchIdsVertexBuffer; if (hasBatchIds) { - batchIds = prepareVertexAttribute(batchIds, "batchIds"); + batchIds.typedArray = prepareVertexAttribute( + batchIds.typedArray, + "batchIds" + ); batchIdsVertexBuffer = Buffer.createVertexBuffer({ context: context, typedArray: batchIds.typedArray, diff --git a/Specs/Core/ComponentDatatypeSpec.js b/Specs/Core/ComponentDatatypeSpec.js index 4944355c096f..9d956387fe83 100644 --- a/Specs/Core/ComponentDatatypeSpec.js +++ b/Specs/Core/ComponentDatatypeSpec.js @@ -28,6 +28,12 @@ describe("Core/ComponentDatatype", function () { ); }); + it("fromTypedArray throws if array is not a valid typed array", function () { + expect(function () { + ComponentDatatype.fromTypedArray([]); + }).toThrowDeveloperError(); + }); + it("validate works", function () { expect(ComponentDatatype.validate(ComponentDatatype.BYTE)).toBe(true); expect(ComponentDatatype.validate(ComponentDatatype.UNSIGNED_BYTE)).toBe( diff --git a/Specs/Core/IndexDatatypeSpec.js b/Specs/Core/IndexDatatypeSpec.js index edca0eaa32f9..60090429c8f0 100644 --- a/Specs/Core/IndexDatatypeSpec.js +++ b/Specs/Core/IndexDatatypeSpec.js @@ -2,13 +2,13 @@ import { IndexDatatype } from "../../Source/Cesium.js"; import { Math as CesiumMath } from "../../Source/Cesium.js"; describe("Core/IndexDatatype", function () { - it("IndexDatatype.validate validates input", function () { + it("validate validates input", function () { expect(IndexDatatype.validate(IndexDatatype.UNSIGNED_SHORT)).toEqual(true); expect(IndexDatatype.validate("invalid")).toEqual(false); expect(IndexDatatype.validate(undefined)).toEqual(false); }); - it("IndexDatatype.createTypedArray creates array", function () { + it("createTypedArray creates array", function () { expect(IndexDatatype.createTypedArray(3, 3).BYTES_PER_ELEMENT).toEqual( Uint16Array.BYTES_PER_ELEMENT ); @@ -18,13 +18,13 @@ describe("Core/IndexDatatype", function () { ).toEqual(Uint32Array.BYTES_PER_ELEMENT); }); - it("IndexDatatype.createTypedArray throws without numberOfVertices", function () { + it("createTypedArray throws without numberOfVertices", function () { expect(function () { IndexDatatype.createTypedArray(undefined); }).toThrowDeveloperError(); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint16Array", function () { + it("createTypedArrayFromArrayBuffer creates Uint16Array", function () { const sourceArray = new Uint16Array(10); sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer( @@ -40,7 +40,7 @@ describe("Core/IndexDatatype", function () { expect(indexBuffer[0]).toEqual(0); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint16Array with offset", function () { + it("createTypedArrayFromArrayBuffer creates Uint16Array with offset", function () { const sourceArray = new Uint16Array(10); sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer( @@ -56,7 +56,7 @@ describe("Core/IndexDatatype", function () { expect(indexBuffer[0]).toEqual(5); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint32Array", function () { + it("createTypedArrayFromArrayBuffer creates Uint32Array", function () { const sourceArray = new Uint32Array(10); sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer( @@ -72,7 +72,7 @@ describe("Core/IndexDatatype", function () { expect(indexBuffer[0]).toEqual(0); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer creates Uint32Array with offset", function () { + it("createTypedArrayFromArrayBuffer creates Uint32Array with offset", function () { const sourceArray = new Uint32Array(10); sourceArray.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); const indexBuffer = IndexDatatype.createTypedArrayFromArrayBuffer( @@ -88,19 +88,19 @@ describe("Core/IndexDatatype", function () { expect(indexBuffer[0]).toEqual(5); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer throws without numberOfVertices", function () { + it("createTypedArrayFromArrayBuffer throws without numberOfVertices", function () { expect(function () { IndexDatatype.createTypedArrayFromArrayBuffer(undefined); }).toThrowDeveloperError(); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer throws without sourceArray", function () { + it("createTypedArrayFromArrayBuffer throws without sourceArray", function () { expect(function () { IndexDatatype.createTypedArrayFromArrayBuffer(3, undefined); }).toThrowDeveloperError(); }); - it("IndexDatatype.createTypedArrayFromArrayBuffer throws without byteOffset", function () { + it("createTypedArrayFromArrayBuffer throws without byteOffset", function () { const sourceArray = new Uint16Array(5); expect(function () { IndexDatatype.createTypedArrayFromArrayBuffer( @@ -111,7 +111,7 @@ describe("Core/IndexDatatype", function () { }).toThrowDeveloperError(); }); - it("IndexDatatype.getSizeInBytes returns size", function () { + it("getSizeInBytes returns size", function () { expect(IndexDatatype.getSizeInBytes(IndexDatatype.UNSIGNED_BYTE)).toEqual( Uint8Array.BYTES_PER_ELEMENT ); @@ -123,9 +123,31 @@ describe("Core/IndexDatatype", function () { ); }); - it("IndexDatatype.getSizeInBytes throws without indexDatatype", function () { + it("getSizeInBytes throws without indexDatatype", function () { expect(function () { IndexDatatype.getSizeInBytes(undefined); }).toThrowDeveloperError(); }); + + it("fromTypedArray works", function () { + expect(IndexDatatype.fromTypedArray(new Uint8Array())).toBe( + IndexDatatype.UNSIGNED_BYTE + ); + expect(IndexDatatype.fromTypedArray(new Uint16Array())).toBe( + IndexDatatype.UNSIGNED_SHORT + ); + expect(IndexDatatype.fromTypedArray(new Uint32Array())).toBe( + IndexDatatype.UNSIGNED_INT + ); + }); + + it("fromTypedArray throws if array is not a valid typed array", function () { + expect(function () { + IndexDatatype.fromTypedArray(new Int8Array()); + }).toThrowDeveloperError(); + + expect(function () { + IndexDatatype.fromTypedArray([]); + }).toThrowDeveloperError(); + }); });