Skip to content

Commit 17bbc95

Browse files
authored
Merge pull request #13313 from CesiumGS/donmccurdy/feat/bufferprimitivecollection-materials
BufferPrimitiveCollection: Material API
2 parents 1238a06 + 5cd8ba3 commit 17bbc95

34 files changed

+913
-236
lines changed

Specs/Data/Cesium3DTiles/Style/style.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"horizontalOrigin" : "0",
2525
"verticalOrigin" : "0",
2626
"labelHorizontalOrigin" : "0",
27-
"labelVerticalOrigin" : "0"
27+
"labelVerticalOrigin" : "0",
28+
"lineWidth" : "10.0"
2829
}

packages/engine/Source/Scene/BufferPoint.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const scratchCartesian = new Cartesian3();
2222
* Represented as one (1) position.
2323
*
2424
* @see BufferPointCollection
25+
* @see BufferPointMaterial
2526
* @see BufferPrimitive
2627
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
2728
* @extends BufferPrimitive

packages/engine/Source/Scene/BufferPointCollection.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import BufferPoint from "./BufferPoint.js";
55
import Cartesian3 from "../Core/Cartesian3.js";
66
import Frozen from "../Core/Frozen.js";
77
import renderPoints from "./renderBufferPointCollection.js";
8+
import BufferPointMaterial from "./BufferPointMaterial.js";
89

9-
/** @import Color from "../Core/Color.js"; */
1010
/** @import Matrix4 from "../Core/Matrix4.js"; */
1111
/** @import FrameState from "./FrameState.js"; */
1212

1313
/**
1414
* @typedef {object} BufferPointOptions
1515
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
1616
* @property {boolean} [show=true]
17-
* @property {Color} [color=Color.WHITE]
17+
* @property {BufferPointMaterial} [material=BufferPointMaterial.DEFAULT_MATERIAL]
1818
* @property {Cartesian3} [position=Cartesian3.ZERO]
1919
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
2020
*/
@@ -30,18 +30,19 @@ import renderPoints from "./renderBufferPointCollection.js";
3030
* const collection = new BufferPointCollection({primitiveCountMax: 1024});
3131
*
3232
* const point = new BufferPoint();
33+
* const material = new BufferPointMaterial({color: Color.WHITE});
3334
*
3435
* // Create a new point, temporarily bound to 'point' local variable.
3536
* collection.add({
3637
* position: new Cartesian3(0.0, 0.0, 0.0),
37-
* color: Color.WHITE,
38+
* material
3839
* }, point);
3940
*
4041
* // Iterate over all points in collection, temporarily binding 'point'
41-
* // local variable to each, and updating point color.
42+
* // local variable to each, and updating point material.
4243
* for (let i = 0; i < collection.primitiveCount; i++) {
4344
* collection.get(i, point);
44-
* point.setColor(Color.RED);
45+
* point.setMaterial(material);
4546
* }
4647
*
4748
* @see BufferPoint
@@ -68,6 +69,10 @@ class BufferPointCollection extends BufferPrimitiveCollection {
6869
return BufferPoint;
6970
}
7071

72+
_getMaterialClass() {
73+
return BufferPointMaterial;
74+
}
75+
7176
/////////////////////////////////////////////////////////////////////////////
7277
// COLLECTION LIFECYCLE
7378

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// @ts-check
2+
3+
import Frozen from "../Core/Frozen.js";
4+
import BufferPrimitiveMaterial from "./BufferPrimitiveMaterial.js";
5+
6+
/** @import Color from "../Core/Color.js"; */
7+
/** @import BufferPoint from "./BufferPoint.js"; */
8+
9+
/**
10+
* @typedef {object} BufferPointMaterialOptions
11+
* @property {Color} [color=Color.WHITE] Color of fill.
12+
* @property {Color} [outlineColor=Color.WHITE] Color of outline.
13+
* @property {number} [outlineWidth=0.0] Width of outline, 0-255px.
14+
* @property {number} [size=1.0] Size of point, 0-255px.
15+
*/
16+
17+
/**
18+
* Material description for a {@link BufferPoint}.
19+
*
20+
* <p>BufferPointMaterial objects are {@link Packable|packable}, stored
21+
* when calling {@link BufferPoint#setMaterial}. Subsequent changes to the
22+
* material will not affect the point until setMaterial() is called again.</p>
23+
*
24+
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
25+
* @extends BufferPrimitiveMaterial
26+
*/
27+
class BufferPointMaterial extends BufferPrimitiveMaterial {
28+
/** @ignore */
29+
static Layout = {
30+
...BufferPrimitiveMaterial.Layout,
31+
SIZE_U8: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH,
32+
__BYTE_LENGTH: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH + 4,
33+
};
34+
35+
/**
36+
* @type {BufferPointMaterial}
37+
* @ignore
38+
*/
39+
static DEFAULT_MATERIAL = Object.freeze(new BufferPointMaterial());
40+
41+
/**
42+
* @param {BufferPointMaterialOptions} [options]
43+
*/
44+
constructor(options = Frozen.EMPTY_OBJECT) {
45+
super(options);
46+
47+
/**
48+
* Size of point, 0-255px.
49+
* @type {number}
50+
*/
51+
this.size = options.size ?? 1;
52+
}
53+
54+
/**
55+
* @override
56+
* @param {BufferPointMaterial} material
57+
* @param {DataView} view
58+
* @param {number} byteOffset
59+
* @override
60+
*/
61+
static pack(material, view, byteOffset) {
62+
super.pack(material, view, byteOffset);
63+
view.setUint8(this.Layout.SIZE_U8 + byteOffset, material.size);
64+
}
65+
66+
/**
67+
* @override
68+
* @param {DataView} view
69+
* @param {number} byteOffset
70+
* @param {BufferPointMaterial} result
71+
* @returns {BufferPointMaterial}
72+
* @override
73+
*/
74+
static unpack(view, byteOffset, result) {
75+
super.unpack(view, byteOffset, result);
76+
result.size = view.getUint8(this.Layout.SIZE_U8 + byteOffset);
77+
return result;
78+
}
79+
80+
/////////////////////////////////////////////////////////////////////////////
81+
// DEBUG
82+
83+
/**
84+
* Returns a JSON-serializable object representing the material. This encoding
85+
* is not memory-efficient, and should generally be used for debugging and
86+
* testing.
87+
*
88+
* @returns {Object} JSON-serializable object.
89+
*/
90+
toJSON() {
91+
return { ...super.toJSON(), size: this.size };
92+
}
93+
}
94+
95+
export default BufferPointMaterial;

packages/engine/Source/Scene/BufferPolygon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const { ERR_CAPACITY, ERR_RESIZE, ERR_OUT_OF_RANGE } =
2929
* triangulation, represented as three vertex indices per triangle.</p>
3030
*
3131
* @see BufferPolygonCollection
32+
* @see BufferPolygonMaterial
3233
* @see BufferPrimitive
3334
* @extends BufferPrimitive
3435
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.

packages/engine/Source/Scene/BufferPolygonCollection.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import Frozen from "../Core/Frozen.js";
77
import assert from "../Core/assert.js";
88
import IndexDatatype from "../Core/IndexDatatype.js";
99
import renderPolygons from "./renderBufferPolygonCollection.js";
10+
import BufferPolygonMaterial from "./BufferPolygonMaterial.js";
1011

1112
/** @import { TypedArray } from "../Core/globalTypes.js"; */
12-
/** @import Color from "../Core/Color.js"; */
1313
/** @import Matrix4 from "../Core/Matrix4.js"; */
1414
/** @import FrameState from "./FrameState.js" */
1515
/** @import ComponentDatatype from "../Core/ComponentDatatype.js"; */
@@ -20,7 +20,7 @@ const { ERR_CAPACITY } = BufferPrimitiveCollection.Error;
2020
* @typedef {object} BufferPolygonOptions
2121
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
2222
* @property {boolean} [show=true]
23-
* @property {Color} [color=Color.WHITE]
23+
* @property {BufferPolygonMaterial} [material=BufferPolygonMaterial.DEFAULT_MATERIAL]
2424
* @property {TypedArray} [positions]
2525
* @property {TypedArray} [holes]
2626
* @property {TypedArray} [triangles]
@@ -47,23 +47,25 @@ const { ERR_CAPACITY } = BufferPrimitiveCollection.Error;
4747
* const polygon = new BufferPolygon();
4848
* const positions = [ ... ];
4949
* const holes = [ ... ];
50+
* const material = new BufferPolygonMaterial({color: Color.WHITE});
5051
*
5152
* // Create a new polygon, temporarily bound to 'polygon' local variable.
5253
* collection.add({
5354
* positions: new Float64Array(positions),
5455
* holes: new Uint32Array(holes),
5556
* triangles: new Uint32Array(earcut(positions, holes, 3)),
56-
* color: Color.WHITE,
57+
* material
5758
* }, polygon);
5859
*
5960
* // Iterate over all polygons in collection, temporarily binding 'polygon'
60-
* // local variable to each, and updating polygon color.
61+
* // local variable to each, and updating polygon material.
6162
* for (let i = 0; i < collection.primitiveCount; i++) {
6263
* collection.get(i, polygon);
63-
* polygon.setColor(Color.RED);
64+
* polygon.setMaterial(material);
6465
* }
6566
*
6667
* @see BufferPolygon
68+
* @see BufferPolygonMaterial
6769
* @see BufferPrimitiveCollection
6870
* @extends BufferPrimitiveCollection<BufferPolygon>
6971
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
@@ -135,6 +137,10 @@ class BufferPolygonCollection extends BufferPrimitiveCollection {
135137
return BufferPolygon;
136138
}
137139

140+
_getMaterialClass() {
141+
return BufferPolygonMaterial;
142+
}
143+
138144
/////////////////////////////////////////////////////////////////////////////
139145
// COLLECTION LIFECYCLE
140146

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// @ts-check
2+
3+
import Frozen from "../Core/Frozen.js";
4+
import BufferPrimitiveMaterial from "./BufferPrimitiveMaterial.js";
5+
6+
/** @import Color from "../Core/Color.js"; */
7+
/** @import BufferPolygon from "./BufferPolygon.js"; */
8+
9+
/**
10+
* @typedef {object} BufferPolygonMaterialOptions
11+
* @property {Color} [color=Color.WHITE] Color of fill.
12+
* @property {Color} [outlineColor=Color.WHITE] Color of outline.
13+
* @property {number} [outlineWidth=0.0] Width of outline, 0-255px.
14+
*/
15+
16+
/**
17+
* Material description for a {@link BufferPolygon}.
18+
*
19+
* <p>BufferPolygonMaterial objects are {@link Packable|packable}, stored
20+
* when calling {@link BufferPolygon#setMaterial}. Subsequent changes to the
21+
* material will not affect the polygon until setMaterial() is called again.</p>
22+
*
23+
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
24+
* @extends BufferPrimitiveMaterial
25+
*/
26+
class BufferPolygonMaterial extends BufferPrimitiveMaterial {
27+
/**
28+
* @type {BufferPolygonMaterial}
29+
* @ignore
30+
*/
31+
static DEFAULT_MATERIAL = Object.freeze(new BufferPolygonMaterial());
32+
33+
/**
34+
* @param {BufferPolygonMaterialOptions} [options]
35+
*/
36+
constructor(options = Frozen.EMPTY_OBJECT) {
37+
super(options);
38+
}
39+
}
40+
41+
export default BufferPolygonMaterial;

packages/engine/Source/Scene/BufferPolyline.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { ERR_RESIZE, ERR_CAPACITY } = BufferPrimitiveCollection.Error;
2121
* Represented as two (2) or more positions.
2222
*
2323
* @see BufferPolylineCollection
24+
* @see BufferPolylineMaterial
2425
* @see BufferPrimitive
2526
* @extends BufferPrimitive
2627
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
@@ -51,17 +52,10 @@ class BufferPolyline extends BufferPrimitive {
5152
POSITION_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 4,
5253

5354
/**
54-
* Width of polyline, 0–255px.
5555
* @type {number}
5656
* @ignore
5757
*/
58-
WIDTH_U8: BufferPrimitive.Layout.__BYTE_LENGTH + 8,
59-
60-
/**
61-
* @type {number}
62-
* @ignore
63-
*/
64-
__BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 12,
58+
__BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 8,
6559
};
6660

6761
/////////////////////////////////////////////////////////////////////////////
@@ -80,7 +74,6 @@ class BufferPolyline extends BufferPrimitive {
8074
static clone(polyline, result) {
8175
super.clone(polyline, result);
8276
result.setPositions(polyline.getPositions());
83-
result.width = polyline.width;
8477
return result;
8578
}
8679

@@ -170,21 +163,6 @@ class BufferPolyline extends BufferPrimitive {
170163
collection._makeDirtyBoundingVolume();
171164
}
172165

173-
/////////////////////////////////////////////////////////////////////////////
174-
// ACCESSORS
175-
176-
/**
177-
* Width of polyline, 0–255px.
178-
* @type {number}
179-
*/
180-
get width() {
181-
return this._getUint8(BufferPolyline.Layout.WIDTH_U8);
182-
}
183-
184-
set width(width) {
185-
this._setUint8(BufferPolyline.Layout.WIDTH_U8, width);
186-
}
187-
188166
/////////////////////////////////////////////////////////////////////////////
189167
// DEBUG
190168

@@ -200,7 +178,6 @@ class BufferPolyline extends BufferPrimitive {
200178
return {
201179
...super.toJSON(),
202180
positions: Array.from(this.getPositions()),
203-
width: this.width,
204181
};
205182
}
206183
}

0 commit comments

Comments
 (0)