@pirireis/webglobeplugins 0.1.9 → 0.1.10

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": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "main": "index.js",
5
5
  "author": "Toprak Nihat Deniz Ozturk",
6
6
  "license": "MIT"
@@ -117,7 +117,7 @@ class BufferManager extends BufferOffsetManager {
117
117
  */
118
118
  insertRings(rings) {
119
119
  if (rings.length === 0) return;
120
- this._autoExtendBuffer(rings.length);
120
+ this.autoExtendBuffer(rings.length);
121
121
  const { gl, buffer } = this;
122
122
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
123
123
  for (let { key, center, startAngle, tailAngle, color, radius, colorMode } of rings) {
@@ -156,12 +156,7 @@ class BufferManager extends BufferOffsetManager {
156
156
  this.globe.DrawRender();
157
157
  }
158
158
 
159
- _autoExtendBuffer(payloadSize) {
160
- const { gl, buffer, bufferType, extendRatio } = this;
161
- if (payloadSize <= this.spaceLeft) return;
162
- const newCapacity = Math.ceil((payloadSize + this.length) * extendRatio);
163
- this.extendBuffer(gl, buffer, bufferType, newCapacity);
164
- }
159
+
165
160
 
166
161
 
167
162
  free() {
@@ -241,5 +241,5 @@ export class Logic {
241
241
 
242
242
  export const programCache = Object.freeze({
243
243
  get: (globe) => noRegisterGlobeProgramCache.getProgram(globe, Logic),
244
- free: (globe) => noRegisterGlobeProgramCache.releaseProgram(globe, Logic)
244
+ release: (globe) => noRegisterGlobeProgramCache.releaseProgram(globe, Logic)
245
245
  });
@@ -0,0 +1 @@
1
+ import { LineOnGlobeCache } from './program.js';
@@ -1,9 +1,9 @@
1
1
  import { CameraUniformBlockString, CameraUniformBlockTotem } from "../totems";
2
- import { longLatRadToMercator, longLatRadToCartesian3D, cartesian3DToGLPosition, mercatorXYTo2DPoint } from "../shaderfunctions/geometrytransformations";
3
- import { createProgram } from "../webgl/program";
2
+ import { longLatRadToMercator, longLatRadToCartesian3D, cartesian3DToGLPosition, mercatorXYTo2DPoint } from "../../util/shaderfunctions/geometrytransformations";
3
+ import { createProgram } from "../../util";
4
4
  import { noRegisterGlobeProgramCache, globeProgramCache } from "../programcache";
5
- import { BufferOffsetManger } from "../../util/account";
6
-
5
+ import BufferOffsetManger from "../../util/account/bufferoffsetmanager";
6
+ const Radian = Math.PI / 180.0;
7
7
  const GLOBE_MIDPOINT_COUNT = 100;
8
8
  const ITEM_SIZE = 8;
9
9
 
@@ -25,8 +25,8 @@ void main() {
25
25
  vec2 longLat;
26
26
  if (is3D) {
27
27
  float interpolation = float(gl_VertexID) / ${GLOBE_MIDPOINT_COUNT}.0;
28
- longLat = mix(start_position, end_position, interpolation)
29
- vec3 cartesian = longLatRadToMercator(longLat);
28
+ longLat = mix(start_position, end_position, interpolation);
29
+ vec3 cartesian = longLatRadToCartesian3D(longLat);
30
30
  gl_Position = cartesian3DToGLPosition(cartesian);
31
31
  } else {
32
32
  if ( gl_VertexID == 0 ) {
@@ -55,61 +55,196 @@ class Logic {
55
55
 
56
56
  constructor(globe) {
57
57
  this.globe = globe;
58
- this.gl = gl;
59
- this.program = createProgram(gl, vertexShader, fragmentShader);
58
+ this.gl = globe.gl;
59
+ this.program = createProgram(this.gl, vertexShader, fragmentShader);
60
60
  const { gl, program } = this;
61
61
  {
62
62
  // assign attribute locations
63
63
  gl.bindAttribLocation(program, 0, "start_position");
64
64
  gl.bindAttribLocation(program, 1, "end_position");
65
65
  gl.bindAttribLocation(program, 2, "color");
66
-
67
66
  }
68
67
 
69
68
 
70
69
  this.cameraBlockBindingPoint = 0;
71
70
  const cameraBlockIndex = this.gl.getUniformBlockIndex(this.program, "CameraUniformBlock");
72
71
  this.cameraBlockTotem = globeProgramCache.getProgram(globe, CameraUniformBlockTotem);
72
+ gl.uniformBlockBinding(this.program, cameraBlockIndex, this.cameraBlockBindingPoint);
73
+
73
74
 
74
75
  }
75
76
 
76
77
 
77
78
  draw(bufferManager) {
78
-
79
+ const { gl, program, globe } = this;
80
+ gl.useProgram(program);
81
+ const { cameraBlockTotem, cameraBlockBindingPoint } = this;
82
+ cameraBlockTotem.bind(cameraBlockBindingPoint);
83
+ gl.bindVertexArray(bufferManager.vao);
84
+ const drawCount = globe.api_GetCurrentGeometry() === 0 ? GLOBE_MIDPOINT_COUNT : 2;
85
+ gl.disable(gl.DEPTH_TEST);
86
+ gl.drawArraysInstanced(gl.LINE_STRIP, 0, drawCount, bufferManager.length);
87
+ gl.enable(gl.DEPTH_TEST);
88
+ gl.bindVertexArray(null);
89
+ cameraBlockTotem.unbind(cameraBlockBindingPoint);
79
90
  }
80
91
 
81
92
 
82
-
93
+ createBufferManager({ capacity = 10, bufferType = "DYNAMIC_DRAW" } = {}) {
94
+ return new BufferManager(this.globe, { capacity, bufferType });
95
+ }
83
96
  }
84
97
 
85
98
 
86
99
  class BufferManager extends BufferOffsetManger {
87
100
 
88
- constructor(gl, { capacity = 10, bufferType = "DYNAMIC_DRAW" } = {}) {
101
+ constructor(globe, { capacity = 10, bufferType = "DYNAMIC_DRAW" } = {}) {
89
102
  super(ITEM_SIZE, { capacity, bufferType });
90
- this.gl = gl;
91
- this.buffer = gl.createBuffer();
103
+ this.globe = globe
104
+ this.gl = globe.gl;
105
+ this.buffer = this.gl.createBuffer();
92
106
 
93
107
  const { gl, buffer } = this;
94
108
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
95
- gl.bufferData(gl.ARRAY_BUFFER, _capacity * ITEM_SIZE * 4, gl[bufferType]);
109
+ gl.bufferData(gl.ARRAY_BUFFER, capacity * ITEM_SIZE * 4, gl[bufferType]);
96
110
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
97
111
 
98
112
  this.vao = gl.createVertexArray();
99
113
  {
100
-
114
+ gl.bindVertexArray(this.vao);
115
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
116
+ gl.enableVertexAttribArray(0);
117
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, ITEM_SIZE * 4, 0);
118
+ gl.enableVertexAttribArray(1);
119
+ gl.vertexAttribPointer(1, 2, gl.FLOAT, false, ITEM_SIZE * 4, 8);
120
+ gl.enableVertexAttribArray(2);
121
+ gl.vertexAttribPointer(2, 4, gl.FLOAT, false, ITEM_SIZE * 4, 16);
122
+ gl.vertexAttribDivisor(0, 1);
123
+ gl.vertexAttribDivisor(1, 1);
124
+ gl.vertexAttribDivisor(2, 1);
125
+ gl.bindVertexArray(null);
101
126
  }
102
127
  }
103
128
 
129
+
130
+ /**
131
+ *
132
+ * @param {Array<{key:string, startLong:number, startLat:number, endLong:number, endLat:number, rgba:[4numbers] }} items
133
+ */
104
134
  insertBulk(items) {
135
+ this.autoExtendBuffer(items.length)
136
+ const { gl, buffer, globe } = this;
137
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
138
+ for (let { key, startLong, startLat, endLong, endLat, rgba } of items) {
139
+ const payload = new Float32Array([
140
+ Radian * startLong,
141
+ Radian * startLat,
142
+ Radian * endLong,
143
+ Radian * endLat,
144
+ ...rgba]);
145
+ const offset = this.getOffset(key) | this.nextOffset();
146
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset, payload);
147
+ this.setOffset(key, offset);
148
+ }
149
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
150
+ globe.DrawRender();
151
+ }
105
152
 
153
+ /**
154
+ *
155
+ * @param {Array<{key:string, startLong:number, startLat:number, endLong:number, endLat:number, rgba:[4numbers] }} items
156
+ */
157
+ updateBulk(items) {
158
+ const { gl, buffer, globe } = this;
159
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
160
+ for (let { key, startLong, startLat, endLong, endLat, rgba } of items) {
161
+ const payload = new Float32Array([
162
+ Radian * startLong,
163
+ Radian * startLat,
164
+ Radian * endLong,
165
+ Radian * endLat,
166
+ ...rgba]);
167
+ const offset = this.getOffset(key);
168
+ if (offset !== undefined) {
169
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset, payload);
170
+ } else {
171
+ console.warn("key not found", key);
172
+ }
173
+ }
174
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
175
+ globe.DrawRender();
106
176
  }
107
- }
108
177
 
178
+ /**
179
+ *
180
+ * @param {Array<{key:string, startLong:number, startLat:number, endLong:number, endLat:number }} items
181
+ */
182
+ updateCoordinatesBulk(items) {
183
+ const { gl, buffer, globe } = this;
184
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
185
+ for (let { key, startLong, startLat, endLong, endLat } of items) {
186
+ const payload = new Float32Array([
187
+ Radian * startLong,
188
+ Radian * startLat,
189
+ Radian * endLong,
190
+ Radian * endLat]);
191
+ const offset = this.getOffset(key);
192
+ if (offset !== undefined) {
193
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset, payload);
194
+ } else {
195
+ console.warn("key not found", key);
196
+ }
197
+ }
198
+ globe.DrawRender();
199
+ }
200
+
201
+
202
+ /**
203
+ *
204
+ * @param {Array<{key:string, rgba:[4numbers] }} items
205
+ */
206
+ updateColorBulk(items) {
207
+ const { gl, buffer, globe } = this;
208
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
209
+ for (let { key, rgba } of items) {
210
+ const payload = new Float32Array([...rgba]);
211
+ const offset = this.getOffset(key);
212
+ if (offset !== undefined) {
213
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset + 16, payload);
214
+ } else {
215
+ console.warn("key not found", key);
216
+ }
217
+ }
218
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
219
+ globe.DrawRender();
220
+ }
221
+
222
+
223
+ /**
224
+ *
225
+ * @param {Array<string>} keys
226
+ */
227
+ deleteBulk(keys) {
228
+ const { gl, buffer, globe } = this;
229
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
230
+ const emptyBlock = new Float32Array(ITEM_SIZE).fill(0);
231
+ for (let key of keys) {
232
+ const offset = this.getOffset(key);
233
+ if (offset !== undefined) {
234
+ this.deleteFromAccount(key);
235
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset, emptyBlock);
236
+ } else {
237
+ console.warn("key not found", key);
238
+ }
239
+ }
240
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
241
+ globe.DrawRender();
242
+ }
243
+ }
109
244
 
110
245
 
111
- export const programCache = Object.freeze({
246
+ export const LineOnGlobeCache = Object.freeze({
112
247
  get: (globe) => { return noRegisterGlobeProgramCache.getProgram(globe, Logic) },
113
- release: (globe) => { noRegisterGlobeProgramCache.releaseProgram(globe, Logic) }
248
+ free: (globe) => { return noRegisterGlobeProgramCache.releaseProgram(globe, Logic) }
114
249
  });
115
250
 
@@ -31,6 +31,9 @@ export default class {
31
31
  this.circleEdgeCount = 360;
32
32
  this._onedegreepaddingOn = oneDegreePadding;
33
33
  this.bufferManager = null;
34
+
35
+ this._deleteCounter = 0;
36
+ this._deleteThreshold = 10;
34
37
  }
35
38
 
36
39
 
@@ -157,8 +160,14 @@ export default class {
157
160
  this.bufferManager.removeCenters(centerIds);
158
161
  this.paddingBufferManager.removeCenters(centerIds);
159
162
  this.textPlugin?.removeCenters(centerIds);
160
- // this.bufferManager.defrag();
161
- // this.paddingBufferManager.defrag();
163
+ this._deleteCounter += centerIds.length;
164
+ if (this._deleteCounter > this._deleteThreshold) {
165
+ // this is naive since we are not checking the actual deletion
166
+ // but it works
167
+ this._deleteCounter = 0;
168
+ this.bufferManager.defrag();
169
+ this.paddingBufferManager.defrag();
170
+ }
162
171
  this.globe.DrawRender();
163
172
 
164
173
  }
@@ -90,7 +90,7 @@ export default class {
90
90
  const itemSize = this.itemSize;
91
91
  const itemCount = this.itemCount;
92
92
  const capacity = (newCapacity && newCapacity > itemCount) ? newCapacity : itemCount;
93
-
93
+ console.log("defrag", itemCount, capacity);
94
94
 
95
95
  const newArray = new Float32Array(itemCount * itemSize);
96
96
 
@@ -98,6 +98,7 @@ export default class {
98
98
  let newOffSet = 0;
99
99
  const newOffSetMap = new Map();
100
100
  for (const [key, offSet] of this.offSetMap) {
101
+ console.log(key, offSet);
101
102
  const itemOffSet = offSet / 4;
102
103
  newArray.set(bufferData.slice(itemOffSet, itemOffSet + itemSize), newOffSet);
103
104
  // this.offSetMap.set(key, newOffSet * 4);
@@ -112,9 +113,16 @@ export default class {
112
113
 
113
114
  this._capacity = capacity;
114
115
  this.tombstoneOffSet = [];
116
+ this._length = itemCount;
115
117
  this.offSetMap = newOffSetMap;
116
118
  }
117
119
 
120
+ autoExtendBuffer(payloadSize) {
121
+ if (payloadSize <= this.spaceLeft) return;
122
+ const newCapacity = Math.ceil((payloadSize + this.length));
123
+ this.extendBuffer(newCapacity);
124
+ }
125
+
118
126
 
119
127
  extendBuffer(newCapacity) {
120
128
  const { gl, buffer, bufferType } = this;
@@ -161,7 +169,6 @@ export default class {
161
169
  }
162
170
 
163
171
 
164
-
165
172
  _removeFromBuffer(keys) {
166
173
  const { gl, buffer } = this;
167
174
  const emptyBlock = new Float32Array(this.itemSize).fill(0)
@@ -175,6 +182,14 @@ export default class {
175
182
  }
176
183
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
177
184
  }
185
+
186
+ free() {
187
+ this.gl.deleteBuffer(this.buffer);
188
+ this.gl.deleteVertexArray(this.vao);
189
+ this.buffer = null;
190
+ this.vao = null;
191
+ this.gl = null;
192
+ }
178
193
  }
179
194
 
180
195