Skip to content

Commit 48874f5

Browse files
committed
tidy up polygon attribute management
1 parent e6b9ce3 commit 48874f5

File tree

2 files changed

+78
-70
lines changed

2 files changed

+78
-70
lines changed

packages/engine/Source/Scene/renderBufferPolygonCollection.js

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import EncodedCartesian3 from "../Core/EncodedCartesian3.js";
2121
import AttributeCompression from "../Core/AttributeCompression.js";
2222
import IndexDatatype from "../Core/IndexDatatype.js";
2323

24-
/** @import {TypedArray, TypedArrayConstructor} from "../Core/globalTypes.js"; */
24+
/** @import {TypedArray} from "../Core/globalTypes.js"; */
2525
/** @import FrameState from "./FrameState.js"; */
2626
/** @import BufferPolygonCollection from "./BufferPolygonCollection.js"; */
2727

2828
/**
29-
* @typedef {'positionHighAndShow' | 'positionLowAndColor'} BufferPolygonAttribute
29+
* @typedef {'positionHigh' | 'positionLow' | 'showAndColor'} BufferPolygonAttribute
3030
* @ignore
3131
*/
3232

@@ -35,8 +35,9 @@ import IndexDatatype from "../Core/IndexDatatype.js";
3535
* @ignore
3636
*/
3737
const BufferPolygonAttributeLocations = {
38-
positionHighAndShow: 0,
39-
positionLowAndColor: 1,
38+
positionHigh: 0,
39+
positionLow: 1,
40+
showAndColor: 2,
4041
};
4142

4243
/**
@@ -75,8 +76,9 @@ function renderBufferPolygonCollection(collection, frameState, renderContext) {
7576

7677
renderContext.indexArray = new Uint32Array(triangleCountMax * 3);
7778
renderContext.attributeArrays = {
78-
positionHighAndShow: new Float32Array(vertexCountMax * 4),
79-
positionLowAndColor: new Float32Array(vertexCountMax * 4),
79+
positionHigh: new Float32Array(vertexCountMax * 3),
80+
positionLow: new Float32Array(vertexCountMax * 3),
81+
showAndColor: new Float32Array(vertexCountMax * 2),
8082
};
8183
}
8284

@@ -85,8 +87,9 @@ function renderBufferPolygonCollection(collection, frameState, renderContext) {
8587
const { _dirtyOffset, _dirtyCount } = collection;
8688

8789
const indexArray = renderContext.indexArray;
88-
const positionHighAndShowArray = attributeArrays.positionHighAndShow;
89-
const positionLowAndColorArray = attributeArrays.positionLowAndColor;
90+
const positionHighArray = attributeArrays.positionHigh;
91+
const positionLowArray = attributeArrays.positionLow;
92+
const showAndColorArray = attributeArrays.showAndColor;
9093

9194
for (let i = _dirtyOffset, il = _dirtyOffset + _dirtyCount; i < il; i++) {
9295
collection.get(i, polygon);
@@ -95,9 +98,12 @@ function renderBufferPolygonCollection(collection, frameState, renderContext) {
9598
continue;
9699
}
97100

101+
const show = polygon.show;
98102
const polygonIndexArray = polygon.getTriangles();
99103
const cartesianArray = polygon.getPositions();
100-
polygon.getColor(color);
104+
const encodedColor = AttributeCompression.encodeRGB8(
105+
polygon.getColor(color),
106+
);
101107

102108
let vOffset = polygon._getUint32(
103109
BufferPolygon.Layout.POSITION_OFFSET_U32,
@@ -118,16 +124,16 @@ function renderBufferPolygonCollection(collection, frameState, renderContext) {
118124
Cartesian3.fromArray(cartesianArray, j * 3, cartesian);
119125
EncodedCartesian3.fromCartesian(cartesian, encodedCartesian);
120126

121-
positionHighAndShowArray[vOffset * 4] = encodedCartesian.high.x;
122-
positionHighAndShowArray[vOffset * 4 + 1] = encodedCartesian.high.y;
123-
positionHighAndShowArray[vOffset * 4 + 2] = encodedCartesian.high.z;
124-
positionHighAndShowArray[vOffset * 4 + 3] = polygon.show ? 1 : 0;
127+
positionHighArray[vOffset * 3] = encodedCartesian.high.x;
128+
positionHighArray[vOffset * 3 + 1] = encodedCartesian.high.y;
129+
positionHighArray[vOffset * 3 + 2] = encodedCartesian.high.z;
130+
131+
positionLowArray[vOffset * 3] = encodedCartesian.low.x;
132+
positionLowArray[vOffset * 3 + 1] = encodedCartesian.low.y;
133+
positionLowArray[vOffset * 3 + 2] = encodedCartesian.low.z;
125134

126-
positionLowAndColorArray[vOffset * 4] = encodedCartesian.low.x;
127-
positionLowAndColorArray[vOffset * 4 + 1] = encodedCartesian.low.y;
128-
positionLowAndColorArray[vOffset * 4 + 2] = encodedCartesian.low.z;
129-
positionLowAndColorArray[vOffset * 4 + 3] =
130-
AttributeCompression.encodeRGB8(color);
135+
showAndColorArray[vOffset * 2] = show ? 1 : 0;
136+
showAndColorArray[vOffset * 2 + 1] = encodedColor;
131137

132138
vOffset++;
133139
}
@@ -139,71 +145,75 @@ function renderBufferPolygonCollection(collection, frameState, renderContext) {
139145
if (!defined(renderContext.vertexArray)) {
140146
const { attributeArrays } = renderContext;
141147

142-
const indexArray = renderContext.indexArray;
143-
const positionHighAndShowArray = attributeArrays.positionHighAndShow;
144-
const positionLowAndColorArray = attributeArrays.positionLowAndColor;
145-
146-
const positionHighBuffer = Buffer.createVertexBuffer({
147-
typedArray: positionHighAndShowArray,
148-
context,
149-
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
150-
usage: BufferUsage.STATIC_DRAW,
151-
});
152-
153-
const positionLowBuffer = Buffer.createVertexBuffer({
154-
typedArray: positionLowAndColorArray,
148+
renderContext.vertexArray = new VertexArray({
155149
context,
156-
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
157-
usage: BufferUsage.STATIC_DRAW,
158-
});
159150

160-
const indexBuffer = Buffer.createIndexBuffer({
161-
context,
162-
typedArray: indexArray,
163-
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
164-
usage: BufferUsage.STATIC_DRAW,
165-
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
166-
indexDatatype: IndexDatatype.UNSIGNED_INT,
167-
});
151+
indexBuffer: Buffer.createIndexBuffer({
152+
context,
153+
typedArray: renderContext.indexArray,
154+
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
155+
usage: BufferUsage.STATIC_DRAW,
156+
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
157+
indexDatatype: IndexDatatype.UNSIGNED_INT,
158+
}),
168159

169-
renderContext.vertexArray = new VertexArray({
170-
context,
171-
indexBuffer,
172160
attributes: [
173161
{
174-
index: BufferPolygonAttributeLocations.positionHighAndShow,
175-
vertexBuffer: positionHighBuffer,
162+
index: BufferPolygonAttributeLocations.positionHigh,
163+
componentDatatype: ComponentDatatype.FLOAT,
164+
componentsPerAttribute: 3,
165+
vertexBuffer: Buffer.createVertexBuffer({
166+
typedArray: attributeArrays.positionHigh,
167+
context,
168+
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
169+
usage: BufferUsage.STATIC_DRAW,
170+
}),
171+
},
172+
{
173+
index: BufferPolygonAttributeLocations.positionLow,
176174
componentDatatype: ComponentDatatype.FLOAT,
177-
componentsPerAttribute: 4,
175+
componentsPerAttribute: 3,
176+
vertexBuffer: Buffer.createVertexBuffer({
177+
typedArray: attributeArrays.positionLow,
178+
context,
179+
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
180+
usage: BufferUsage.STATIC_DRAW,
181+
}),
178182
},
179183
{
180-
index: BufferPolygonAttributeLocations.positionLowAndColor,
181-
vertexBuffer: positionLowBuffer,
184+
index: BufferPolygonAttributeLocations.showAndColor,
182185
componentDatatype: ComponentDatatype.FLOAT,
183-
componentsPerAttribute: 4,
186+
componentsPerAttribute: 2,
187+
vertexBuffer: Buffer.createVertexBuffer({
188+
typedArray: attributeArrays.showAndColor,
189+
context,
190+
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
191+
usage: BufferUsage.STATIC_DRAW,
192+
}),
184193
},
185194
],
186195
});
187196
} else if (collection._dirtyCount > 0) {
188197
const { indexOffset, indexCount, vertexOffset, vertexCount } =
189198
getPolygonDirtyRanges(collection);
190-
renderContext.vertexArray.copyAttributeFromRange(
191-
BufferPolygonAttributeLocations.positionHighAndShow,
192-
renderContext.attributeArrays.positionHighAndShow,
193-
vertexOffset,
194-
vertexCount,
195-
);
196-
renderContext.vertexArray.copyAttributeFromRange(
197-
BufferPolygonAttributeLocations.positionLowAndColor,
198-
renderContext.attributeArrays.positionLowAndColor,
199-
vertexOffset,
200-
vertexCount,
201-
);
199+
202200
renderContext.vertexArray.copyIndexFromRange(
203201
renderContext.indexArray,
204202
indexOffset,
205203
indexCount,
206204
);
205+
206+
for (const key in BufferPolygonAttributeLocations) {
207+
if (Object.hasOwn(BufferPolygonAttributeLocations, key)) {
208+
const attribute = /** @type {BufferPolygonAttribute} */ (key);
209+
renderContext.vertexArray.copyAttributeFromRange(
210+
BufferPolygonAttributeLocations[attribute],
211+
renderContext.attributeArrays[attribute],
212+
vertexOffset,
213+
vertexCount,
214+
);
215+
}
216+
}
207217
}
208218

209219
if (!defined(renderContext.renderState)) {

packages/engine/Source/Shaders/BufferPolygonCollectionVS.glsl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
in vec4 positionHighAndShow;
2-
in vec4 positionLowAndColor;
1+
in vec3 positionHigh;
2+
in vec3 positionLow;
3+
in vec2 showAndColor;
34

45
out vec4 v_color;
56

67
void main()
78
{
8-
// Unpack attributes.
9-
vec3 positionHigh = positionHighAndShow.xyz;
10-
vec3 positionLow = positionLowAndColor.xyz;
11-
float show = positionHighAndShow.w;
12-
vec4 color = czm_decodeRGB8(positionLowAndColor.w);
9+
float show = showAndColor.x;
10+
vec4 color = czm_decodeRGB8(showAndColor.y);
1311

1412
///////////////////////////////////////////////////////////////////////////
1513

0 commit comments

Comments
 (0)