@pirireis/webglobeplugins 0.17.0 → 0.17.1

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.
@@ -2,6 +2,7 @@
2
2
  * Author: Toprak Nihat Deniz Ozturk
3
3
  */
4
4
  import { createImageFromBase64, createProgramWrapper, createTexture, createBuffer, bindAttribute, bindFramebuffer, bindTexture, getColorRamp } from "../../util/webglobjectbuilders";
5
+ import { defaultblendfunction } from "../../util/webglobe/gldefaultstates";
5
6
  import imageToMagnitude, { imageToRadianAngle } from "./imagetovectorfieldandmagnitude";
6
7
  import { TexturePointSampler } from "../../heatwave/texture-point-sampler";
7
8
  /**
@@ -111,7 +112,6 @@ void main() {
111
112
 
112
113
  // take EPSG:4236 distortion into account for calculating where the particle moved
113
114
  // float distortion = 1.0; //cos(radians(pos.y * 180.0 - 90.0));
114
- // vec2 offset = vec2(velocity.x / distortion, -velocity.y) * 0.0001 * u_speed_factor;
115
115
  vec2 offset = vec2(velocity.x, velocity.y) * 0.0001 * u_speed_factor;
116
116
 
117
117
  // update particle position, wrapping around the date line
@@ -638,12 +638,12 @@ export default class WindPlugin {
638
638
  // globe calls `draw3D` method on each frame
639
639
  draw3D(projMatrix, modelviewMatrix, transPos) {
640
640
  const gl = this.gl;
641
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
642
- this.transPos.set([transPos.x, transPos.y, transPos.z], 0);
643
- this.projMatrix = projMatrix;
644
- this.modelviewMatrix = modelviewMatrix;
645
- gl.activeTexture(gl.TEXTURE0);
646
641
  if (this._doDraw()) {
642
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
643
+ this.transPos.set([transPos.x, transPos.y, transPos.z], 0);
644
+ this.projMatrix = projMatrix;
645
+ this.modelviewMatrix = modelviewMatrix;
646
+ gl.activeTexture(gl.TEXTURE0);
647
647
  const depthTest = gl.isEnabled(gl.DEPTH_TEST);
648
648
  if (depthTest)
649
649
  gl.disable(gl.DEPTH_TEST);
@@ -651,17 +651,16 @@ export default class WindPlugin {
651
651
  this._draw();
652
652
  if (depthTest)
653
653
  gl.enable(gl.DEPTH_TEST);
654
+ // test visuals
655
+ // gl.viewport(0, 0, 100, 100);
656
+ // this._drawTexture(this.windTexture, 1.0);
657
+ // gl.viewport(0, 100, 100, 100);
658
+ // this._drawTexture(this.particleStateTexture0, 1.0);
659
+ // gl.viewport(0, 200, 100, 100);
660
+ // this._drawTexture(this.colorRampTexture, 1.0);
661
+ this._resetMachineStates();
662
+ this.globe.DrawRender();
654
663
  }
655
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
656
- // test visuals
657
- // gl.viewport(0, 0, 100, 100);
658
- // this._drawTexture(this.windTexture, 1.0);
659
- // gl.viewport(0, 100, 100, 100);
660
- // this._drawTexture(this.particleStateTexture0, 1.0);
661
- // gl.viewport(0, 200, 100, 100);
662
- // this._drawTexture(this.colorRampTexture, 1.0);
663
- this._resetMachineStates();
664
- this.globe.DrawRender();
665
664
  }
666
665
  _drawScreen() {
667
666
  const { gl, globe } = this;
@@ -676,7 +675,7 @@ export default class WindPlugin {
676
675
  this._drawParticles();
677
676
  bindFramebuffer(gl, null);
678
677
  this._drawTexture(this.screenTexture, 1.0);
679
- gl.disable(gl.BLEND);
678
+ // gl.disable(gl.BLEND);
680
679
  // save the current screen as the background for the next frame
681
680
  const temp = this.backgroundTexture;
682
681
  this.backgroundTexture = this.screenTexture;
@@ -691,6 +690,8 @@ export default class WindPlugin {
691
690
  gl.uniform1i(program.u_screen, 2);
692
691
  gl.uniform1f(program.u_opacity, opacity);
693
692
  gl.drawArrays(gl.TRIANGLES, 0, 6);
693
+ gl.bindTexture(gl.TEXTURE_2D, null);
694
+ gl.activeTexture(gl.TEXTURE0);
694
695
  }
695
696
  _loadRampMax() {
696
697
  const { gl } = this;
@@ -761,6 +762,7 @@ export default class WindPlugin {
761
762
  _updateParticles() {
762
763
  const gl = this.gl;
763
764
  const program = this.updateProgram;
765
+ gl.disable(gl.BLEND);
764
766
  gl.useProgram(program.program);
765
767
  gl.viewport(0, 0, this.particleStateResolution, this.particleStateResolution);
766
768
  bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
@@ -774,12 +776,24 @@ export default class WindPlugin {
774
776
  const temp = this.particleStateTexture0;
775
777
  this.particleStateTexture0 = this.particleStateTexture1;
776
778
  this.particleStateTexture1 = temp;
779
+ // Add this line to restore the viewport
780
+ gl.viewport(0, 0, this.globe.api_ScrW(), this.globe.api_ScrH());
777
781
  }
778
782
  _resetMachineStates() {
779
783
  const { gl, globe } = this;
780
- // gl.disable(gl.DEPTH_TEST);
781
- // gl.disable(gl.STENCIL_TEST);
784
+ // Unbind all textures
785
+ for (let i = 0; i < 3; i++) {
786
+ gl.activeTexture(gl.TEXTURE0 + i);
787
+ gl.bindTexture(gl.TEXTURE_2D, null);
788
+ }
782
789
  gl.activeTexture(gl.TEXTURE0);
790
+ // Unbind buffers
791
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
792
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
793
+ gl.bindVertexArray(null); // Add this line
794
+ gl.enable(gl.BLEND);
795
+ defaultblendfunction(gl);
796
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
783
797
  gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
784
798
  }
785
799
  _loadHeight() {
@@ -103,8 +103,9 @@ export class ContextTextWriter4 {
103
103
  }, style.zMode === CSZMode.Z_MSL);
104
104
  textFont.size = sizeMultiplier * textSize;
105
105
  textFont.position = position;
106
- if (x !== null && y !== null)
106
+ if (x !== null && y !== null) {
107
107
  globe.api_DrawContextTextMultiLine(text, textFont, o, { x: x + xOffset, y: y - yOffset }, angleIsOn, angle);
108
+ }
108
109
  }
109
110
  textFont.size = textSize;
110
111
  }
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,118 +0,0 @@
1
- import { TextureDemTriangles } from "./texture-dem-triangles";
2
- /**
3
- * Loads a plugin with test data for TextureDemTriangles
4
- */
5
- const TEST_ITEM_COUNT = 1000000;
6
- function createTestLongLatArray() {
7
- const arr = new Float32Array(TEST_ITEM_COUNT * 2);
8
- for (let i = 0; i < TEST_ITEM_COUNT; i++) {
9
- arr[i * 2 + 0] = (Math.random() - 0.5) * Math.PI * 2; // longitude
10
- arr[i * 2 + 1] = (Math.random() - 0.5) * Math.PI; // latitude
11
- }
12
- return arr;
13
- }
14
- function createTestPos3dArray(longLatArray) {
15
- const arr = new Float32Array(TEST_ITEM_COUNT * 3);
16
- for (let i = 0; i < TEST_ITEM_COUNT; i++) {
17
- const long = longLatArray[i * 2 + 0];
18
- const lat = longLatArray[i * 2 + 1];
19
- // Convert long/lat to 3D position (simplified)
20
- arr[i * 3 + 0] = Math.cos(lat) * Math.cos(long);
21
- arr[i * 3 + 1] = Math.cos(lat) * Math.sin(long);
22
- arr[i * 3 + 2] = Math.sin(lat);
23
- }
24
- return arr;
25
- }
26
- const createTestBBOXES = () => {
27
- const bboxes = [];
28
- for (let i = 0; i < 6; i++) {
29
- const north = Math.random(); // random latitude
30
- const west = Math.random() * -1; // random longitude
31
- const south = north - Math.PI / 6; // random latitude
32
- const east = west + Math.PI / 6; // random longitude
33
- const nw = [west, north]; // random northWest
34
- const se = [east, south]; // random southEast
35
- bboxes.push({ northWest: nw, southEast: se });
36
- }
37
- return bboxes;
38
- };
39
- const createTestDemTextures = () => {
40
- const textures = [];
41
- for (let i = 0; i < 6; i++) {
42
- const texture = [];
43
- for (let j = 0; j < 5 * 5; j++) {
44
- const value = 1000 * Math.random(); // random elevation value
45
- texture.push(value);
46
- }
47
- textures.push(texture);
48
- }
49
- return textures;
50
- };
51
- export function createBuffersAndFill(gl, vec3s, longLats, indices) {
52
- const positionBuffer = gl.createBuffer();
53
- const longLatBuffer = gl.createBuffer();
54
- const indexBuffer = gl.createBuffer();
55
- gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
56
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vec3s), gl.STATIC_DRAW);
57
- gl.bindBuffer(gl.ARRAY_BUFFER, longLatBuffer);
58
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(longLats), gl.STATIC_DRAW);
59
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
60
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
61
- return {
62
- buffers: {
63
- positionBuffer,
64
- longLatBuffer,
65
- indexBuffer
66
- }
67
- };
68
- }
69
- export class TextureDemTrianglesTestPlugin {
70
- globe = null;
71
- textureDemTriangles = null;
72
- vao = null;
73
- drawOptions = null;
74
- bufferInfo = null;
75
- ubo = null;
76
- id;
77
- constructor(id) {
78
- this.id = id;
79
- }
80
- init(globe) {
81
- this.globe = globe;
82
- this.textureDemTriangles = new TextureDemTriangles(globe);
83
- // set bboxes and dem textures
84
- const demTextures = createTestDemTextures();
85
- const demTextureBBOX = createTestBBOXES();
86
- this.textureDemTriangles.setDemTextures(demTextures, demTextureBBOX);
87
- //
88
- this.ubo = this.textureDemTriangles.createUBO();
89
- this.ubo.updateSingle("u_color", new Float32Array([1.0, 0.0, 0.0, 0.0]));
90
- const bufferInfo = {
91
- pos3dBufferInfo: {
92
- buffer: globe.gl.createBuffer(),
93
- stride: 0,
94
- offset: 0,
95
- },
96
- longLatBufferInfo: {
97
- buffer: globe.gl.createBuffer(),
98
- stride: 0,
99
- offset: 0,
100
- },
101
- drawOptions: { drawRange: { count: TEST_ITEM_COUNT, first: 0 } }
102
- };
103
- this.vao = this.textureDemTriangles.createVAO(bufferInfo.pos3dBufferInfo, bufferInfo.longLatBufferInfo);
104
- const gl = globe.gl;
105
- const longLatArray = createTestLongLatArray();
106
- const pos3dArray = createTestPos3dArray(longLatArray);
107
- gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.pos3dBufferInfo.buffer);
108
- gl.bufferData(gl.ARRAY_BUFFER, pos3dArray, gl.STATIC_DRAW);
109
- gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.longLatBufferInfo.buffer);
110
- gl.bufferData(gl.ARRAY_BUFFER, longLatArray, gl.STATIC_DRAW);
111
- this.bufferInfo = bufferInfo;
112
- }
113
- draw3D() {
114
- if (!this.globe || !this.textureDemTriangles || !this.vao || !this.bufferInfo || !this.ubo)
115
- return;
116
- this.textureDemTriangles.draw(this.vao, this.bufferInfo.drawOptions, this.ubo);
117
- }
118
- }
@@ -1,76 +0,0 @@
1
- /**
2
- * Iceride bir canvas uretir ve degisiklikler bu kanvasta yapilir
3
- */
4
- // this class loads texture to raster overlay
5
- // TODO: IS DEADCODE?
6
- class RasterOverlayLoader {
7
- constructor(id, globe, { opacity = 1.0, minLon = -180, minLat = -90, maxLon = 180, maxLat = 90, beforeObject = null, canvas = null, callBack = null } = {}) {
8
- this.canvas = canvas || document.createElement('canvas');
9
- this.gl = this.canvas.getContext('webgl2');
10
- this.globe = globe;
11
- this.id = id;
12
- this._opacity = opacity;
13
- this.bbox = { ll: { x: minLon, y: minLat }, ur: { x: maxLon, y: maxLat } };
14
- this._beforeObject = beforeObject;
15
- this._isProcessing = false;
16
- this._callBack = callBack;
17
- }
18
- getGL() {
19
- return this.gl;
20
- }
21
- getCanvasURL() {
22
- return this.canvas.toDataURL();
23
- }
24
- setBBox(minLon, minLat, maxLon, maxLat) {
25
- this.bbox = { ll: { x: minLon, y: minLat }, ur: { x: maxLon, y: maxLat } };
26
- this.draw();
27
- }
28
- setCanvasWidthHeight(width, height) {
29
- this.canvas.width = width;
30
- this.canvas.height = height;
31
- this.gl.viewport(0, 0, width, height);
32
- }
33
- draw() {
34
- if (!this._isProcessing) {
35
- requestAnimationFrame(this._draw.bind(this));
36
- this._isProcessing = true;
37
- }
38
- }
39
- _draw() {
40
- this.globe.api_DeleteImageOverlay(this.id);
41
- const id = this.id;
42
- const imgUrl = this.getCanvasURL();
43
- const bbox = this.bbox;
44
- const opacity = this._opacity;
45
- const rotDeg = 0;
46
- const color = null;
47
- const beforeObject = this._beforeObject;
48
- const callback = this._callBack || function () { };
49
- this.globe.api_AddImageOverlay(id, imgUrl, bbox, color, opacity, rotDeg, beforeObject, callback);
50
- this._isProcessing = false;
51
- }
52
- setBeforeObject(beforeObject) {
53
- this._beforeObject = beforeObject;
54
- this.draw();
55
- }
56
- setCallback(callback) {
57
- this._callBack = callback;
58
- }
59
- setOpacity(opacity) {
60
- if (opacity < 0 || opacity > 1) {
61
- return;
62
- }
63
- this._opacity = opacity;
64
- this.globe.api_SetImageOverlayColor(this.id, null, opacity);
65
- }
66
- free() {
67
- this.globe.api_DeleteImageOverlay(this.id);
68
- }
69
- on() {
70
- this.draw();
71
- }
72
- off() {
73
- this.globe.api_DeleteImageOverlay(this.id);
74
- }
75
- }
76
- export { RasterOverlayLoader };
@@ -1,95 +0,0 @@
1
- import { CSZMode } from "@pirireis/webglobe";
2
- import { isTextFont, opacityCheck } from "../util/check/typecheck";
3
- /**
4
- * Light wight version of context text writer.
5
- * The parameters by text are calculated on user side and inserted as items. (ContextTextWriter3 constructs text data from items)
6
- *
7
- * Good side of ContextTextWriters pattern is that there is only one way to create text.
8
- * The glue is inside the plugin.
9
- * This makes the code more readable and maintainable.
10
- *
11
- * With AttachedTextWriter the glue is created by user.
12
- * I observed that the users generally uses a wrapper around the plugin logic anyway. This wrapper can be used as the glue for plugin and attached writer.
13
- */
14
- export class AttachedTextWriter {
15
- constructor(globe, { style = {
16
- textFont: {
17
- name: 'Arial',
18
- textColor: '#FFFFFF', // beyaz
19
- hollowColor: '#000000', // siyah
20
- size: 12, // piksel
21
- hollow: true,
22
- bold: true,
23
- italic: false,
24
- },
25
- opacity: 1.0,
26
- zMode: CSZMode.Z_GROUND_PERVERTEX,
27
- }, doDraw = true, angleOnSphere = false, zoomLevelAdaptor = (zoomLevel) => (item) => {
28
- return {
29
- opacityMultiplier: 1,
30
- sizeMultiplier: 1
31
- };
32
- }, } = {}) {
33
- this.globe = globe;
34
- this._itemMap = new Map();
35
- this.doDraw = doDraw;
36
- this.angleOnSphere = angleOnSphere;
37
- this.zoomLevelAdaptor = zoomLevelAdaptor;
38
- this.setStyle(style);
39
- }
40
- setOpacity(opacity) {
41
- opacityCheck(opacity);
42
- this._style.opacity = opacity;
43
- this.globe.DrawRender();
44
- }
45
- setStyle(style) {
46
- isTextFont(style.textFont);
47
- this._style = style;
48
- this.globe.DrawRender();
49
- }
50
- /**
51
- *
52
- * @param {{text, long, lat, z = 0, xOffset = 0, yOffset = 0, isAngleOn = false, Angle = 0 }} item
53
- * @returns
54
- */
55
- insertItem(item) {
56
- const key = this.keyAdaptor ? this.keyAdaptor(item) : item;
57
- this._itemMap.set(key, item);
58
- this.globe.DrawRender();
59
- }
60
- removeItem(item) {
61
- const key = this.keyAdaptor ? this.keyAdaptor(item) : item;
62
- if (this._itemMap.delete(key)) {
63
- this.globe.DrawRender();
64
- return true;
65
- }
66
- return false;
67
- }
68
- // TODO: use item style
69
- draw() {
70
- if (!this.doDraw) {
71
- return;
72
- }
73
- const { globe, _itemMap, _style } = this;
74
- const { zMode, textFont, opacity } = _style;
75
- const is3D = globe.api_GetCurrentGeometry() === 0;
76
- const angleIsOn = is3D ? true : this.angleOnSphere;
77
- const zoomLevel = globe.api_GetCurrentLODWithDecimal();
78
- const zoomAdaptor = this.zoomLevelAdaptor(zoomLevel);
79
- const theSize = textFont.size;
80
- _itemMap.forEach((item, key) => {
81
- const { text, long, lat, z = 0, xOffset = 0, yOffset = 0, isAngleOn = false, angle = 0 } = item;
82
- const { x, y } = globe.api_GetScreenPointFromGeo({
83
- long,
84
- lat,
85
- z
86
- }, zMode);
87
- const { opacityMultiplier, sizeMultiplier } = zoomAdaptor(item);
88
- textFont.size = sizeMultiplier * theSize;
89
- if (x !== null && y !== null) {
90
- globe.api_DrawContextText(text, textFont, opacityMultiplier * opacity, { x: x + xOffset, y: y - yOffset }, (isAngleOn && angleIsOn), angle);
91
- }
92
- });
93
- textFont.size = theSize;
94
- }
95
- }