Skip to content

Commit b82c47d

Browse files
tsherifXiaoji Chen
authored andcommitted
Use shader attributes for solid polygon layer (visgl#2703)
#### Background Use `shaderAttributes` to replace SolidPolygonLayer attribute hijacking in `updateAttributes`. #### Change List - Use new `shaderAttributes` instead of creating new attributes in `updateAttributes`. The `updateAttributes` override is is still necessary to switch to instance drawing for the sides. - Update vertex shader module for SolidPolygonLayer to inject source code instead of branching. Was necessary to prevent the topModel from trying to access attributes it doesn't use.
1 parent ccd741c commit b82c47d

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

modules/layers/src/solid-polygon-layer/solid-polygon-layer-vertex.glsl.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ attribute vec2 vertexPositions;
2525
attribute float vertexValid;
2626
attribute vec3 positions;
2727
attribute vec2 positions64xyLow;
28+
29+
#ifdef IS_SIDE_VERTEX
2830
attribute vec3 nextPositions;
2931
attribute vec2 nextPositions64xyLow;
32+
#endif
33+
3034
attribute float elevations;
3135
attribute vec4 fillColors;
3236
attribute vec4 lineColors;
3337
attribute vec3 pickingColors;
3438
35-
uniform bool isSideVertex;
3639
uniform bool extruded;
3740
uniform bool isWireframe;
3841
uniform float elevationScale;
@@ -47,30 +50,31 @@ void main(void) {
4750
vec3 normal;
4851
vec4 colors = isWireframe ? lineColors : fillColors;
4952
50-
if (isSideVertex) {
51-
pos = mix(positions, nextPositions, vertexPositions.x);
52-
pos64xyLow = mix(positions64xyLow, nextPositions64xyLow, vertexPositions.x);
53-
isValid = vertexValid;
54-
} else {
55-
pos = positions;
56-
pos64xyLow = positions64xyLow;
57-
isValid = 1.0;
58-
}
53+
#ifdef IS_SIDE_VERTEX
54+
pos = mix(positions, nextPositions, vertexPositions.x);
55+
pos64xyLow = mix(positions64xyLow, nextPositions64xyLow, vertexPositions.x);
56+
isValid = vertexValid;
57+
#else
58+
pos = positions;
59+
pos64xyLow = positions64xyLow;
60+
isValid = 1.0;
61+
#endif
62+
5963
if (extruded) {
6064
pos.z += elevations * vertexPositions.y;
6165
}
6266
pos.z *= elevationScale;
6367
6468
vec4 position_worldspace;
6569
gl_Position = project_position_to_clipspace(pos, pos64xyLow, vec3(0.), position_worldspace);
66-
70+
6771
if (extruded) {
68-
if (isSideVertex) {
69-
normal = vec3(positions.y - nextPositions.y, nextPositions.x - positions.x, 0.0);
70-
normal = project_normal(normal);
71-
} else {
72-
normal = vec3(0.0, 0.0, 1.0);
73-
}
72+
#ifdef IS_SIDE_VERTEX
73+
normal = vec3(positions.y - nextPositions.y, nextPositions.x - positions.x, 0.0);
74+
normal = project_normal(normal);
75+
#else
76+
normal = vec3(0.0, 0.0, 1.0);
77+
#endif
7478
7579
vec3 lightColor = lighting_getLightColor(colors.rgb, project_uCameraPosition, position_worldspace.xyz, normal);
7680
vColor = vec4(lightColor, colors.a * opacity) / 255.0;

modules/layers/src/solid-polygon-layer/solid-polygon-layer.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,16 @@ const ATTRIBUTE_TRANSITION = {
6060
};
6161

6262
export default class SolidPolygonLayer extends Layer {
63-
getShaders() {
63+
getShaders(isSide) {
6464
const projectModule = this.use64bitProjection() ? 'project64' : 'project32';
65-
return {vs, fs, modules: [projectModule, 'lighting', 'picking']};
65+
return {
66+
vs,
67+
fs,
68+
modules: [projectModule, 'lighting', 'picking'],
69+
defines: {
70+
IS_SIDE_VERTEX: isSide
71+
}
72+
};
6673
}
6774

6875
initializeState() {
@@ -87,9 +94,29 @@ export default class SolidPolygonLayer extends Layer {
8794
transition: ATTRIBUTE_TRANSITION,
8895
accessor: 'getPolygon',
8996
update: this.calculatePositions,
90-
noAlloc
97+
shaderAttributes: {
98+
positions: {
99+
size: 3
100+
},
101+
nextPositions: {
102+
size: 3,
103+
offset: 12
104+
}
105+
}
106+
},
107+
positions64xyLow: {
108+
size: 2,
109+
update: this.calculatePositionsLow,
110+
shaderAttributes: {
111+
positions64xyLow: {
112+
size: 2
113+
},
114+
nextPositions64xyLow: {
115+
size: 2,
116+
offset: 8
117+
}
118+
}
91119
},
92-
positions64xyLow: {size: 2, update: this.calculatePositionsLow, noAlloc},
93120
vertexValid: {
94121
size: 1,
95122
type: GL.UNSIGNED_BYTE,
@@ -228,20 +255,6 @@ export default class SolidPolygonLayer extends Layer {
228255
});
229256
}
230257
}
231-
if (newAttributes.positions) {
232-
newAttributes.nextPositions = Object.assign(
233-
{},
234-
newAttributes.positions,
235-
{id: 'nextPositions', offset: 12} // 1 vertex * 3 floats * 4 bits
236-
);
237-
}
238-
if (newAttributes.positions64xyLow) {
239-
newAttributes.nextPositions64xyLow = Object.assign(
240-
{},
241-
newAttributes.positions64xyLow,
242-
{id: 'nextPositions64xyLow', offset: 8} // 1 vertex * 2 floats * 4 bits
243-
);
244-
}
245258
sideModel.setAttributes(newAttributes);
246259
}
247260
}
@@ -255,14 +268,12 @@ export default class SolidPolygonLayer extends Layer {
255268
if (filled) {
256269
topModel = new Model(
257270
gl,
258-
Object.assign({}, this.getShaders(), {
271+
Object.assign({}, this.getShaders(false), {
259272
id: `${id}-top`,
260273
geometry: new Geometry({
261274
drawMode: GL.TRIANGLES,
262275
attributes: {
263-
vertexPositions: {size: 2, constant: true, value: new Float32Array([0, 1])},
264-
nextPositions: {size: 3, constant: true, value: new Float32Array(3)},
265-
nextPositions64xyLow: {size: 2, constant: true, value: new Float32Array(2)}
276+
vertexPositions: {size: 2, constant: true, value: new Float32Array([0, 1])}
266277
}
267278
}),
268279
uniforms: {
@@ -278,7 +289,7 @@ export default class SolidPolygonLayer extends Layer {
278289
if (extruded) {
279290
sideModel = new Model(
280291
gl,
281-
Object.assign({}, this.getShaders(), {
292+
Object.assign({}, this.getShaders(true), {
282293
id: `${id}-side`,
283294
geometry: new Geometry({
284295
drawMode: GL.LINES,

0 commit comments

Comments
 (0)