-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathpixelmapFeature.js
More file actions
180 lines (166 loc) · 5.63 KB
/
pixelmapFeature.js
File metadata and controls
180 lines (166 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var inherit = require('../inherit');
var registerFeature = require('../registry').registerFeature;
var pixelmapFeature = require('../pixelmapFeature');
var lookupTable2D = require('./lookupTable2D');
var util = require('../util');
/**
* Create a new instance of class webgl.pixelmapFeature.
*
* @class
* @alias geo.webgl.pixelmapFeature
* @extends geo.pixelmapFeature
* @param {geo.pixelmapFeature.spec} arg
* @returns {geo.webgl.pixelmapFeature}
*/
var webgl_pixelmapFeature = function (arg) {
'use strict';
if (!(this instanceof webgl_pixelmapFeature)) {
return new webgl_pixelmapFeature(arg);
}
pixelmapFeature.call(this, arg);
var object = require('./object');
object.call(this);
const vgl = require('../vgl');
const fragmentShader = require('./pixelmapFeature.frag');
var m_quadFeature,
m_quadFeatureInit,
s_exit = this._exit,
m_lookupTable,
m_this = this;
/**
* If the specified coordinates are in the rendered quad, use the basis
* information from the quad to determine the pixelmap index value so that it
* can be included in the `found` results.
*
* @param {geo.geoPosition} geo Coordinate.
* @param {string|geo.transform|null} [gcs] Input gcs. `undefined` to use
* the interface gcs, `null` to use the map gcs, or any other transform.
* @returns {geo.feature.searchResult} An object with a list of features and
* feature indices that are located at the specified point.
*/
this.pointSearch = function (geo, gcs) {
if (m_quadFeature && m_this.m_info) {
const result = m_quadFeature.pointSearch(geo, gcs);
return this._pointSearchProcess(result);
}
return {index: [], found: []};
};
/**
* Given the loaded pixelmap image, create a texture for the colors and a
* quad that will use it.
*/
this._computePixelmap = function () {
var data = m_this.data() || [],
colorFunc = m_this.style.get('color');
let indexRange = m_this.indexModified(undefined, 'clear');
let fullUpdate = m_this.dataTime().timestamp() >= m_this.buildTime().timestamp() || indexRange === undefined;
if (!m_lookupTable) {
m_lookupTable = lookupTable2D();
m_lookupTable.setTextureUnit(1);
fullUpdate = true;
}
let clrLen = Math.max(1, data.length);
const maxWidth = m_lookupTable.maxWidth();
if (clrLen > maxWidth && clrLen % maxWidth) {
clrLen += maxWidth - (clrLen % maxWidth);
}
let colors;
if (!fullUpdate) {
colors = m_lookupTable.colorTable();
fullUpdate = colors.length !== clrLen * 4;
indexRange[0] = Math.max(0, indexRange[0]);
indexRange[1] = Math.min(data.length, indexRange[1] + 1);
}
if (fullUpdate) {
colors = new Uint8Array(clrLen * 4);
indexRange = [0, data.length];
}
for (let i = indexRange[0]; i < indexRange[1]; i += 1) {
const d = data[i];
const color = util.convertColor(colorFunc.call(m_this, d, i));
colors[i * 4] = color.r * 255;
colors[i * 4 + 1] = color.g * 255;
colors[i * 4 + 2] = color.b * 255;
colors[i * 4 + 3] = color.a === undefined ? 255 : (color.a * 255);
}
m_this.m_info = {colors: colors};
// check if colors haven't changed
var oldcolors = m_lookupTable.colorTable();
if (oldcolors && oldcolors.length === colors.length) {
let idx = indexRange[0] * 4;
for (; idx < indexRange[1] * 4; idx += 1) {
if (colors[idx] !== oldcolors[idx]) {
break;
}
}
if (idx === indexRange[1] * 4) {
return;
}
}
m_lookupTable.colorTable(colors);
/* If we haven't made a quad feature, make one now */
if (!m_quadFeature) {
m_quadFeature = m_this.layer().createFeature('quad', {
selectionAPI: false,
gcs: m_this.gcs(),
visible: m_this.visible(undefined, true),
nearestPixel: true
});
m_quadFeatureInit = false;
}
if (!m_quadFeatureInit) {
m_this.dependentFeatures([m_quadFeature]);
m_quadFeature.setShader('image_fragment', fragmentShader);
m_quadFeature._hookBuild = (prog) => {
const lutSampler = new vgl.uniform(vgl.GL.INT, 'lutSampler');
lutSampler.set(m_lookupTable.textureUnit());
prog.addUniform(lutSampler);
const lutWidth = new vgl.uniform(vgl.GL.INT, 'lutWidth');
lutWidth.set(m_lookupTable.width);
prog.addUniform(lutWidth);
const lutHeight = new vgl.uniform(vgl.GL.INT, 'lutHeight');
lutHeight.set(m_lookupTable.height);
prog.addUniform(lutHeight);
};
m_quadFeature._hookRenderImageQuads = (renderState, quads) => {
m_lookupTable.bind(renderState, quads);
};
if (m_quadFeatureInit === false) {
m_quadFeature.style({
image: m_this.m_srcImage,
position: m_this.style.get('position')})
.data([{}])
.draw();
}
m_quadFeatureInit = true;
}
};
/**
* Destroy. Deletes the associated quadFeature.
*
* @returns {this}
*/
this._exit = function () {
if (m_quadFeature && m_this.layer()) {
m_this.layer().deleteFeature(m_quadFeature);
m_quadFeature = null;
m_this.dependentFeatures([]);
}
s_exit();
return m_this;
};
if (arg.quadFeature) {
m_quadFeature = arg.quadFeature;
if (m_quadFeature.nearestPixel) {
m_quadFeature.nearestPixel(true);
}
}
this._init(arg);
return this;
};
inherit(webgl_pixelmapFeature, pixelmapFeature);
// Now register it
var capabilities = {};
capabilities[pixelmapFeature.capabilities.lookup] = true;
registerFeature('webgl', 'pixelmap', webgl_pixelmapFeature, capabilities);
module.exports = webgl_pixelmapFeature;