Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/engine/Source/Scene/BufferPointCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import BufferPoint from "./BufferPoint.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Frozen from "../Core/Frozen.js";
import renderPoints from "./renderBufferPointCollection.js";
import BufferPointMaterial from "./BufferPointMaterial.js";

/** @import Color from "../Core/Color.js"; */
/** @import Matrix4 from "../Core/Matrix4.js"; */
/** @import FrameState from "./FrameState.js"; */

/**
* @typedef {object} BufferPointOptions
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
* @property {boolean} [show=true]
* @property {Color} [color=Color.WHITE]
* @property {BufferPointMaterial} [material=BufferPointMaterial.DEFAULT_MATERIAL]
* @property {Cartesian3} [position=Cartesian3.ZERO]
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
*/
Expand Down Expand Up @@ -68,6 +68,10 @@ class BufferPointCollection extends BufferPrimitiveCollection {
return BufferPoint;
}

_getMaterialClass() {
return BufferPointMaterial;
}

/////////////////////////////////////////////////////////////////////////////
// COLLECTION LIFECYCLE

Expand Down
95 changes: 95 additions & 0 deletions packages/engine/Source/Scene/BufferPointMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// @ts-check

import Frozen from "../Core/Frozen.js";
import BufferPrimitiveMaterial from "./BufferPrimitiveMaterial.js";

/** @import Color from "../Core/Color.js"; */
/** @import BufferPoint from "./BufferPoint.js"; */

/**
* @typedef {object} BufferPointMaterialOptions
* @property {Color} [color=Color.WHITE] Color of fill.
* @property {Color} [outlineColor=Color.WHITE] Color of outline.
* @property {number} [outlineWidth=0.0] Width of outline, 0-255px.
* @property {number} [pixelSize=1.0] Size of point, 0-255px.
*/

/**
* Material description for a {@link BufferPoint}.
*
* <p>BufferPointMaterial objects are {@link Packable|packable}, stored
* when calling {@link BufferPoint#setMaterial}. Subsequent changes to the
* material will not affect the point until setMaterial() is called again.</p>
*
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
* @extends BufferPrimitiveMaterial
*/
class BufferPointMaterial extends BufferPrimitiveMaterial {
/** @ignore */
static Layout = {
...BufferPrimitiveMaterial.Layout,
PIXEL_SIZE_U8: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH,
__BYTE_LENGTH: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH + 4,
};

/**
* @type {BufferPointMaterial}
* @ignore
*/
static DEFAULT_MATERIAL = Object.freeze(new BufferPointMaterial());

/**
* @param {BufferPointMaterialOptions} [options]
*/
constructor(options = Frozen.EMPTY_OBJECT) {
super(options);

/**
* Size of point, 0-255px.
* @type {number}
*/
this.pixelSize = options.pixelSize ?? 1;
}

/**
* @override
* @param {BufferPointMaterial} material
* @param {DataView} view
* @param {number} byteOffset
* @override
*/
static pack(material, view, byteOffset) {
super.pack(material, view, byteOffset);
view.setUint8(this.Layout.PIXEL_SIZE_U8 + byteOffset, material.pixelSize);
}

/**
* @override
* @param {DataView} view
* @param {number} byteOffset
* @param {BufferPointMaterial} result
* @returns {BufferPointMaterial}
* @override
*/
static unpack(view, byteOffset, result) {
super.unpack(view, byteOffset, result);
result.pixelSize = view.getUint8(this.Layout.PIXEL_SIZE_U8 + byteOffset);
return result;
}

/////////////////////////////////////////////////////////////////////////////
// DEBUG

/**
* Returns a JSON-serializable object representing the material. This encoding
* is not memory-efficient, and should generally be used for debugging and
* testing.
*
* @returns {Object} JSON-serializable object.
*/
toJSON() {
return { ...super.toJSON(), pixelSize: this.pixelSize };
}
}

export default BufferPointMaterial;
8 changes: 6 additions & 2 deletions packages/engine/Source/Scene/BufferPolygonCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import Frozen from "../Core/Frozen.js";
import assert from "../Core/assert.js";
import IndexDatatype from "../Core/IndexDatatype.js";
import renderPolygons from "./renderBufferPolygonCollection.js";
import BufferPolygonMaterial from "./BufferPolygonMaterial.js";

/** @import { TypedArray } from "../Core/globalTypes.js"; */
/** @import Color from "../Core/Color.js"; */
/** @import Matrix4 from "../Core/Matrix4.js"; */
/** @import FrameState from "./FrameState.js" */
/** @import ComponentDatatype from "../Core/ComponentDatatype.js"; */
Expand All @@ -20,7 +20,7 @@ const { ERR_CAPACITY } = BufferPrimitiveCollection.Error;
* @typedef {object} BufferPolygonOptions
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
* @property {boolean} [show=true]
* @property {Color} [color=Color.WHITE]
* @property {BufferPolygonMaterial} [material=BufferPolygonMaterial.DEFAULT_MATERIAL]
* @property {TypedArray} [positions]
* @property {TypedArray} [holes]
* @property {TypedArray} [triangles]
Expand Down Expand Up @@ -134,6 +134,10 @@ class BufferPolygonCollection extends BufferPrimitiveCollection {
return BufferPolygon;
}

_getMaterialClass() {
return BufferPolygonMaterial;
}

/////////////////////////////////////////////////////////////////////////////
// COLLECTION LIFECYCLE

Expand Down
41 changes: 41 additions & 0 deletions packages/engine/Source/Scene/BufferPolygonMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check

import Frozen from "../Core/Frozen.js";
import BufferPrimitiveMaterial from "./BufferPrimitiveMaterial.js";

/** @import Color from "../Core/Color.js"; */
/** @import BufferPolygon from "./BufferPolygon.js"; */

/**
* @typedef {object} BufferPolygonMaterialOptions
* @property {Color} [color=Color.WHITE] Color of fill.
* @property {Color} [outlineColor=Color.WHITE] Color of outline.
* @property {number} [outlineWidth=0.0] Width of outline, 0-255px.
*/

/**
* Material description for a {@link BufferPolygon}.
*
* <p>BufferPolygonMaterial objects are {@link Packable|packable}, stored
* when calling {@link BufferPolygon#setMaterial}. Subsequent changes to the
* material will not affect the polygon until setMaterial() is called again.</p>
*
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
* @extends BufferPrimitiveMaterial
*/
class BufferPolygonMaterial extends BufferPrimitiveMaterial {
/**
* @type {BufferPolygonMaterial}
* @ignore
*/
static DEFAULT_MATERIAL = Object.freeze(new BufferPolygonMaterial());

/**
* @param {BufferPolygonMaterialOptions} [options]
*/
constructor(options = Frozen.EMPTY_OBJECT) {
super(options);
}
}

export default BufferPolygonMaterial;
26 changes: 1 addition & 25 deletions packages/engine/Source/Scene/BufferPolyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,10 @@ class BufferPolyline extends BufferPrimitive {
POSITION_COUNT_U32: BufferPrimitive.Layout.__BYTE_LENGTH + 4,

/**
* Width of polyline, 0–255px.
* @type {number}
* @ignore
*/
WIDTH_U8: BufferPrimitive.Layout.__BYTE_LENGTH + 8,

/**
* @type {number}
* @ignore
*/
__BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 12,
__BYTE_LENGTH: BufferPrimitive.Layout.__BYTE_LENGTH + 8,
};

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -80,7 +73,6 @@ class BufferPolyline extends BufferPrimitive {
static clone(polyline, result) {
super.clone(polyline, result);
result.setPositions(polyline.getPositions());
result.width = polyline.width;
return result;
}

Expand Down Expand Up @@ -170,21 +162,6 @@ class BufferPolyline extends BufferPrimitive {
collection._makeDirtyBoundingVolume();
}

/////////////////////////////////////////////////////////////////////////////
// ACCESSORS

/**
* Width of polyline, 0–255px.
* @type {number}
*/
get width() {
return this._getUint8(BufferPolyline.Layout.WIDTH_U8);
}

set width(width) {
this._setUint8(BufferPolyline.Layout.WIDTH_U8, width);
}

/////////////////////////////////////////////////////////////////////////////
// DEBUG

Expand All @@ -200,7 +177,6 @@ class BufferPolyline extends BufferPrimitive {
return {
...super.toJSON(),
positions: Array.from(this.getPositions()),
width: this.width,
};
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/engine/Source/Scene/BufferPolylineCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import defined from "../Core/defined.js";
import BufferPrimitiveCollection from "./BufferPrimitiveCollection.js";
import BufferPolyline from "./BufferPolyline.js";
import renderPolylines from "./renderBufferPolylineCollection.js";
import BufferPolylineMaterial from "./BufferPolylineMaterial.js";

/** @import { TypedArray } from "../Core/globalTypes.js"; */
/** @import Color from "../Core/Color.js"; */
/** @import Matrix4 from "../Core/Matrix4.js"; */
/** @import FrameState from "./FrameState.js" */

/**
* @typedef {object} BufferPolylineOptions
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
* @property {boolean} [show=true]
* @property {Color} [color=Color.WHITE]
* @property {BufferPolylineMaterial} [material=BufferPolylineMaterial.DEFAULT_MATERIAL]
* @property {TypedArray} [positions]
* @property {number} [width=1]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result.width = options.width ?? 1; has already been removed, please remember update the docs accordingly.

Also, the JSDoc/examples for BufferPointCollection, BufferPolylineCollection, and BufferPolygonCollection still reference the old color initialization and setColor() workflow. Since this PR moves styling to material classes, those examples should we update to use material / setMaterial() instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch – I think I've updated all the documentation now.

I'd like for the type checker to either catch errors like the unused width, or to extend the BufferPrimitiveMaterialOptions so we don't have to duplicate so much here, but I think that's currently blocked by #10455.

* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
Expand Down Expand Up @@ -62,6 +62,10 @@ class BufferPolylineCollection extends BufferPrimitiveCollection {
return BufferPolyline;
}

_getMaterialClass() {
return BufferPolylineMaterial;
}

/////////////////////////////////////////////////////////////////////////////
// COLLECTION LIFECYCLE

Expand Down Expand Up @@ -100,8 +104,6 @@ class BufferPolylineCollection extends BufferPrimitiveCollection {
result._setUint32(BufferPolyline.Layout.POSITION_OFFSET_U32, vertexOffset);
result._setUint32(BufferPolyline.Layout.POSITION_COUNT_U32, 0);

result.width = options.width ?? 1;

if (defined(options.positions)) {
result.setPositions(options.positions);
}
Expand Down
93 changes: 93 additions & 0 deletions packages/engine/Source/Scene/BufferPolylineMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// @ts-check

import Frozen from "../Core/Frozen.js";
import BufferPrimitiveMaterial from "./BufferPrimitiveMaterial.js";

/** @import Color from "../Core/Color.js"; */
/** @import BufferPolyline from "./BufferPolyline.js"; */

/**
* @typedef {object} BufferPolylineMaterialOptions
* @property {Color} [color=Color.WHITE] Color of fill.
* @property {Color} [outlineColor=Color.WHITE] Color of outline.
* @property {number} [outlineWidth=0.0] Width of outline, 0-255px.
* @property {number} [width=1.0] Width of line, 0-255px.
*/

/**
* Material description for a {@link BufferPolyline}.
*
* <p>BufferPolylineMaterial objects are {@link Packable|packable}, stored
* when calling {@link BufferPolyline#setMaterial}. Subsequent changes to the
* material will not affect the polyline until setMaterial() is called again.</p>
*
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
* @extends BufferPrimitiveMaterial
*/
class BufferPolylineMaterial extends BufferPrimitiveMaterial {
/** @ignore */
static Layout = {
...BufferPrimitiveMaterial.Layout,
WIDTH_U8: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH,
__BYTE_LENGTH: BufferPrimitiveMaterial.Layout.__BYTE_LENGTH + 4,
};

/**
* @type {BufferPolylineMaterial}
* @ignore
*/
static DEFAULT_MATERIAL = Object.freeze(new BufferPolylineMaterial());

/**
* @param {BufferPolylineMaterialOptions} [options]
*/
constructor(options = Frozen.EMPTY_OBJECT) {
super(options);

/**
* Width of polyline, 0–255px.
* @type {number}
*/
this.width = options.width ?? 1;
}

/**
* @param {BufferPolylineMaterial} material
* @param {DataView} view
* @param {number} byteOffset
* @override
*/
static pack(material, view, byteOffset) {
super.pack(material, view, byteOffset);
view.setUint8(this.Layout.WIDTH_U8 + byteOffset, material.width);
}

/**
* @param {DataView} view
* @param {number} byteOffset
* @param {BufferPolylineMaterial} result
* @returns {BufferPolylineMaterial}
* @override
*/
static unpack(view, byteOffset, result) {
super.unpack(view, byteOffset, result);
result.width = view.getUint8(this.Layout.WIDTH_U8 + byteOffset);
return result;
}

/////////////////////////////////////////////////////////////////////////////
// DEBUG

/**
* Returns a JSON-serializable object representing the material. This encoding
* is not memory-efficient, and should generally be used for debugging and
* testing.
*
* @returns {Object} JSON-serializable object.
*/
toJSON() {
return { ...super.toJSON(), width: this.width };
}
}

export default BufferPolylineMaterial;
Loading