@pirireis/webglobeplugins 1.1.14 → 1.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pirireis/webglobeplugins",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "main": "index.js",
5
5
  "author": "Toprak Nihat Deniz Ozturk",
6
6
  "license": "MIT",
@@ -4,7 +4,7 @@ import { DemTextureManagerCache, DEM_TEXTURE_BLOCK_STRING } from "../totems/atta
4
4
  import { cartesian3DToGLPosition, mercatorXYToGLPosition, POLE_BY_PI } from "../../util/shaderfunctions/geometrytransformations";
5
5
  import { relativeBBoxPositionRadian } from "../../util/shaderfunctions/geometrytransformations";
6
6
  import { drawArrays } from "../../util/gl-util/draw-options/methods";
7
- import { attributeLoader } from "../../util/gl-util/buffer/attribute-loader";
7
+ import { attributeLoader, resetAttributeDefault, setDefaultAttributeValues } from "../../util/gl-util/buffer/attribute-loader";
8
8
  import { WORLD_RADIUS_3D } from "../../Math/constants";
9
9
  import { UniformBlockManager } from "../../util/gl-util/uniform-block/manager";
10
10
  // const ESCAPE_VALUE = 0.123456;
@@ -15,7 +15,7 @@ const uniformBindingPoints = {
15
15
  selection: 3,
16
16
  };
17
17
  // last value of uint16 is 65535
18
- export const IndexAttributeEscapeValue = 65535;
18
+ export const IndexAttributeEscapeValue = -1;
19
19
  const styleBlockManager = new UniformBlockManager('Style', [
20
20
  { name: "defaultColor", type: "vec4", value: new Float32Array([0.0, 0.0, 0.0, 0.0]) },
21
21
  { name: "pickedColor", type: "vec4", value: new Float32Array([0, 0, 0, 0]) },
@@ -55,6 +55,8 @@ void main() {
55
55
  float elevation = ${WORLD_RADIUS_3D};
56
56
  float altitude = 0.0;
57
57
 
58
+ // Always define v_index to avoid undefined varyings when picking is disabled.
59
+
58
60
  vec4 decodedColor = vec4(
59
61
  float(a_color.r) / 255.0,
60
62
  float(a_color.g) / 255.0,
@@ -119,10 +121,6 @@ void main() {
119
121
  outIndex = v_index;
120
122
  }
121
123
  `;
122
- function resCalculation(sourceResolution, mergeCount) {
123
- return sourceResolution + (mergeCount - 1) * (sourceResolution - 1);
124
- }
125
- const RESOLUTION = resCalculation(5, 20); // 5 is tile dimension length, 12 is merge count
126
124
  export class TextureDemTriangles {
127
125
  globe;
128
126
  gl;
@@ -208,13 +206,24 @@ export class TextureDemTriangles {
208
206
  this.demTextureManager.bindData(0, uniformBindingPoints.dem);
209
207
  gl.bindVertexArray(vao);
210
208
  ubo.bind();
211
- if (this.globe.api_GetCurrentLODWithDecimal() < 14.5) {
212
- drawOnTopBegin(gl);
213
- }
209
+ // This program has two fragment outputs (location 0: color, location 1: index).
210
+ // When rendering to the default framebuffer (no picking FBO bound), only location 0
211
+ // is valid; explicitly discard location 1 to avoid GL_INVALID_OPERATION.
212
+ // const boundFbo = gl.getParameter(gl.FRAMEBUFFER_BINDING) as WebGLFramebuffer | null;
213
+ // if (boundFbo == null) {
214
+ // gl.drawBuffers([gl.BACK, gl.NONE]);
215
+ // // }
216
+ // if (this.globe.api_GetCurrentLODWithDecimal() < 14.5) {
217
+ // drawOnTopBegin(gl);
218
+ // }
214
219
  drawArrays(gl, drawOptions.drawMode ?? gl.TRIANGLES, drawOptions);
215
- if (this.globe.api_GetCurrentLODWithDecimal() < 14.5) {
216
- drawOnTopEnd(gl);
217
- }
220
+ // if (this.globe.api_GetCurrentLODWithDecimal() < 14.5) {
221
+ // drawOnTopEnd(gl);
222
+ // }
223
+ // if (boundFbo == null) {
224
+ // // Restore the default draw buffer mapping for subsequent draws.
225
+ // gl.drawBuffers([gl.BACK]);
226
+ // }
218
227
  this.demTextureManager.unbindData(0, uniformBindingPoints.dem);
219
228
  gl.bindVertexArray(null);
220
229
  gl.bindTexture(gl.TEXTURE_2D_ARRAY, null);
@@ -222,6 +231,18 @@ export class TextureDemTriangles {
222
231
  ubo.unbind();
223
232
  this.cameraUniformBlock.unbind(uniformBindingPoints.camera);
224
233
  }
234
+ setDefaultDefaultAttributeValues() {
235
+ const gl = this.gl;
236
+ setDefaultAttributeValues(gl, this.locations.attributes.a_index, 1, [IndexAttributeEscapeValue], "uint16");
237
+ setDefaultAttributeValues(gl, this.locations.attributes.a_color, 4, [0, 0, 0, 255], "uint8");
238
+ }
239
+ resetDefaultAttributeValues() {
240
+ const gl = this.gl;
241
+ gl.disableVertexAttribArray(this.locations.attributes.a_index);
242
+ resetAttributeDefault(gl, this.locations.attributes.a_index);
243
+ gl.disableVertexAttribArray(this.locations.attributes.a_color);
244
+ resetAttributeDefault(gl, this.locations.attributes.a_color);
245
+ }
225
246
  _getUniformBlockOffset(name) {
226
247
  // hardcoded offsets based on std140 layout rules
227
248
  const offsets = {
@@ -234,25 +255,3 @@ export class TextureDemTriangles {
234
255
  return result;
235
256
  }
236
257
  }
237
- function drawOnTopBegin(gl) {
238
- gl.disable(gl.DEPTH_TEST);
239
- // gl.enable(gl.POLYGON_OFFSET_FILL);
240
- // // Use an offset to "nudge" the depth value slightly closer or farther.
241
- // // Common values are gl.polygonOffset(1.0, 1.0) or gl.polygonOffset(0.RESOLUTION, 0.RESOLUTION)
242
- // gl.polygonOffset(1.0, 1.0);
243
- // gl.depthRange(0.0, 0.0001); // Use near 0.01% of depth range
244
- // gl.enable(gl.POLYGON_OFFSET_FILL);
245
- // You will need to experiment with these values!
246
- // A good starting point is -1.0, -1.0.
247
- // Negative values "pull" the fragments closer to the camera.
248
- // gl.polygonOffset(-1.0, -1.0);
249
- // gl.enable(gl.POLYGON_OFFSET_FILL);
250
- // gl.polygonOffset(-1.0, -2.0);
251
- }
252
- function drawOnTopEnd(gl) {
253
- gl.enable(gl.DEPTH_TEST);
254
- // gl.disable(gl.POLYGON_OFFSET_FILL);
255
- // gl.disable(gl.POLYGON_OFFSET_FILL);
256
- // gl.depthRange(0.0, 1.0); // Restore full depth range
257
- // gl.disable(gl.POLYGON_OFFSET_FILL);
258
- }
@@ -114,11 +114,15 @@ export class TerrainPolygonSemiPlugin {
114
114
  this._uboHandler.updateSingle("private_isPickedOn", new Float32Array([1.0]));
115
115
  this._uboHandler.updateSingle("pickedColor", new Float32Array(this._options.polygonStyle.pickedColor));
116
116
  }
117
+ else {
118
+ this._uboHandler.updateSingle("private_isPickedOn", new Float32Array([0.0]));
119
+ }
117
120
  if (this._options.drawEdges) {
118
121
  this._uboForRealEdgeArcs = this._program.createUBO();
119
122
  this._uboForRealEdgeArcs.updateSingle("defaultColor", new Float32Array(this._options.edgeStyle.defaultColor));
120
123
  this._uboForRealEdgeArcs.updateSingle("useDefaultColor", new Float32Array([1]));
121
124
  this._uboForRealEdgeArcs.updateSingle("opacity", this._effectiveOpacity("edgeArc"));
125
+ this._uboForRealEdgeArcs.updateSingle("private_isPickedOn", new Float32Array([0.0]));
122
126
  this._drawRealEdgeArcs.elementBuffer = gl.createBuffer();
123
127
  }
124
128
  if (this._options.variativeColorsOn) {
@@ -373,6 +377,7 @@ export class TerrainPolygonSemiPlugin {
373
377
  gl.disable(gl.DEPTH_TEST);
374
378
  gl.enable(gl.BLEND);
375
379
  defaultblendfunction(gl);
380
+ this._program.setDefaultDefaultAttributeValues();
376
381
  // drawPoints
377
382
  if (this._options.showTesselationPoints) {
378
383
  this._program.draw(this._vao, this._drawPointsRangeIndexParams, this._uboHandler);
@@ -397,6 +402,7 @@ export class TerrainPolygonSemiPlugin {
397
402
  this._program.draw(this._vao, this._drawRealEdgeArcs, this._uboForRealEdgeArcs);
398
403
  // gl.disable(gl.POLYGON_OFFSET_FILL);
399
404
  }
405
+ this._program.resetDefaultAttributeValues();
400
406
  gl.enable(gl.DEPTH_TEST);
401
407
  }
402
408
  free() {
@@ -8,10 +8,25 @@ const DATA_TYPE_MAP = {
8
8
  uint32: { getGLType: (gl) => gl.UNSIGNED_INT, useIntegerPointer: true, isSigned: false },
9
9
  };
10
10
  const attributeLoader = (gl, bufferAndReadInfo, index, size, { divisor = null, dataType = null, escapeValues = null, normalized = false, } = {}) => {
11
+ // WebGL returns -1 for inactive attributes (optimized out). Treat as a no-op.
12
+ // This also avoids accidentally mutating global generic attribute state.
13
+ if (index < 0) {
14
+ return;
15
+ }
11
16
  const typeInfo = dataType ? DATA_TYPE_MAP[dataType] : DATA_TYPE_MAP.float;
12
17
  if (bufferAndReadInfo == null || bufferAndReadInfo.buffer == null) {
13
- if (escapeValues)
14
- constantFunction(gl, index, size, escapeValues, typeInfo);
18
+ // Disable array and set constant value
19
+ gl.disableVertexAttribArray(index);
20
+ // If the shader expects an integer attribute (in uint/int/ivec*/uvec*),
21
+ // WebGL requires the constant to be set via vertexAttribI4i/vertexAttribI4ui.
22
+ // Otherwise you'll hit: "Vertex shader input type does not match...".
23
+ if (escapeValues != null) {
24
+ setConstantAttribute(gl, index, size, escapeValues, typeInfo);
25
+ }
26
+ else if (typeInfo.useIntegerPointer) {
27
+ // Default constant for integer attributes when not provided.
28
+ setConstantAttribute(gl, index, size, [0, 0, 0, 0], typeInfo);
29
+ }
15
30
  return;
16
31
  }
17
32
  const { buffer, stride, offset } = bufferAndReadInfo;
@@ -49,16 +64,40 @@ const createBufferAndReadInfo = (buffer, stride = 0, offset = 0) => {
49
64
  return null;
50
65
  return { buffer, stride, offset };
51
66
  };
52
- const constantFunction = (gl, index, size, escapeValues, typeInfo) => {
53
- if (!typeInfo.useIntegerPointer) {
54
- const func = `vertexAttrib${size}f`;
67
+ const setConstantAttribute = (gl, index, size, values, typeInfo) => {
68
+ // Array should already be disabled by caller
69
+ if (size < 1 || size > 4) {
70
+ throw new Error(`Attribute size must be between 1 and 4. Got ${size}.`);
71
+ }
72
+ if (typeInfo.useIntegerPointer) {
73
+ gl.vertexAttrib4f(index, 0, 0, 0, 1);
74
+ // Integer attributes: WebGL2 ONLY supports vertexAttribI4i/I4ui for constants.
75
+ const intValues = values.map((v) => Math.trunc(v));
76
+ while (intValues.length < 4)
77
+ intValues.push(0);
78
+ const func = typeInfo.isSigned ? "vertexAttribI4i" : "vertexAttribI4ui";
55
79
  // @ts-ignore
56
- gl[func](index, ...escapeValues);
80
+ gl[func](index, intValues[0], intValues[1], intValues[2], intValues[3]);
57
81
  return;
58
82
  }
59
- const values = escapeValues.map((v) => Math.trunc(v));
60
- const func = typeInfo.isSigned ? `vertexAttribI${size}i` : `vertexAttribI${size}ui`;
61
- // @ts-ignore
62
- gl[func](index, ...values);
83
+ // Float attributes: use vertexAttrib{1..4}f.
84
+ const v = values.slice(0, 4);
85
+ while (v.length < 4)
86
+ v.push(size === 4 ? 0 : (v.length === 3 ? 1 : 0));
87
+ if (size === 1)
88
+ gl.vertexAttrib1f(index, v[0]);
89
+ else if (size === 2)
90
+ gl.vertexAttrib2f(index, v[0], v[1]);
91
+ else if (size === 3)
92
+ gl.vertexAttrib3f(index, v[0], v[1], v[2]);
93
+ else
94
+ gl.vertexAttrib4f(index, v[0], v[1], v[2], v[3]);
63
95
  };
64
- export { attributeLoader, createBufferAndReadInfo };
96
+ function resetAttributeDefault(gl, index) {
97
+ gl.vertexAttrib4f(index, 0, 0, 0, 1);
98
+ }
99
+ function setDefaultAttributeValues(gl, index, size, escapeValues, typeInfo) {
100
+ // gl.disableVertexAttribArray(index);
101
+ setConstantAttribute(gl, index, size, escapeValues, DATA_TYPE_MAP[typeInfo]);
102
+ }
103
+ export { attributeLoader, createBufferAndReadInfo, resetAttributeDefault, setDefaultAttributeValues };