-
Notifications
You must be signed in to change notification settings - Fork 5
/
frac-focus.html
294 lines (243 loc) · 10.1 KB
/
frac-focus.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Google Maps, WebGL, Drilling data</title>
<style>
html, body, #map-div {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="js/CanvasLayer.js"></script>
<script type="text/javascript" src="js/base.js"></script>
<script type="text/javascript" src="js/io.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script>
var map;
var canvasLayer;
var gl;
var pointProgram;
var pointArrayBuffer;
var POINT_COUNT;
var pixelsToWebGLMatrix = new Float32Array(16);
var mapMatrix = new Float32Array(16);
var drillingWells;
var srcData = {"fracfocus": 0, "cmu": 1};
var currentIndex = 0;
function HomeControl(controlDiv, map) {
controlDiv.style.padding = '5px';
var controlUI = document.createElement('select');
controlUI.innerHTML = '<option value="fracfocus">FracFocus</option><option value="cmu">CMU</option>';
controlDiv.appendChild(controlUI);
// Setup the click event listeners: simply set the map to Chicago.
google.maps.event.addDomListener(controlUI, 'change', function() {
currentIndex = srcData[controlUI.value];
drillingWells.update_();
});
}
var DrillingWells = function(urls) {
this.arrayBuffers = [];
var that = this;
for (var i = 0; i < urls.length; i++) {
var completion = function(index) {
return function(arrayBuffer, excpetion) {
if (arrayBuffer) {
var data = new DataView(arrayBuffer);
var rawData = new Float32Array(
data.byteLength / Float32Array.BYTES_PER_ELEMENT);
var len = rawData.length;
for (var i = 0; i < len; i += 2) {
lat = data.getFloat32(i * Float32Array.BYTES_PER_ELEMENT, true);
lon = data.getFloat32((i +1 ) * Float32Array.BYTES_PER_ELEMENT, true);
var pixel = LatLongToPixelXY(lat, lon);
rawData[i] = pixel.x;
rawData[i+1] = pixel.y;
}
that.arrayBuffers[index] = rawData;
}
that.update_();
}
}(i);
tdl.io.loadArrayBuffer(urls[i], completion);
}
}
DrillingWells.prototype.update_ = function() {
if (this.arrayBuffers.length == 2) {
gl.clear(gl.COLOR_BUFFER_BIT);
pointArrayBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointArrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this.arrayBuffers[currentIndex], gl.STATIC_DRAW);
// enable the 'worldCoord' attribute in the shader to receive buffer
var attributeLoc = gl.getAttribLocation(pointProgram, 'worldCoord');
gl.enableVertexAttribArray(attributeLoc);
// tell webgl how buffer is laid out (pairs of x,y coords)
gl.vertexAttribPointer(attributeLoc, 2, gl.FLOAT, false, 0, 0); }
}
function init() {
// initialize the map
var mapOptions = {
zoom: 4,
maxZoom: 25,
minZoom: 4,
center: new google.maps.LatLng(39.3, -95.8),
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [
{
featureType: 'water',
stylers: [{ color: '#c3cfdd'}]
},
{
featureType: 'poi',
stylers: [{visibility: 'off'}]
}
]
};
var mapDiv = document.getElementById('map-div');
map = new google.maps.Map(mapDiv, mapOptions);
var homeControlDiv = document.createElement('div');
var homeControl = new HomeControl(homeControlDiv, map);
homeControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
// initialize the canvasLayer
var canvasLayerOptions = {
map: map,
resizeHandler: resize,
animate: true,
updateHandler: update
};
canvasLayer = new CanvasLayer(canvasLayerOptions);
window.addEventListener('resize', function () { google.maps.event.trigger(map, 'resize') }, false);
// initialize WebGL
gl = canvasLayer.canvas.getContext('experimental-webgl');
gl.enable(gl.BLEND);
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
createShaderProgram();
// tdl.io.loadArrayBuffer('fracfocus_latlon.bin', function() { alert('fracfocus')});
// tdl.io.loadArrayBuffer('wells_latlon.bin', function() { alert('wells')});
drillingWells = new DrillingWells(
["fracfocus_latlon.bin",
"wells_latlon.bin"]);
}
function createShaderProgram() {
// create vertex shader
var vertexSrc = document.getElementById('pointVertexShader').text;
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexSrc);
gl.compileShader(vertexShader);
// create fragment shader
var fragmentSrc = document.getElementById('pointFragmentShader').text;
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentSrc);
gl.compileShader(fragmentShader);
// link shaders to create our program
pointProgram = gl.createProgram();
gl.attachShader(pointProgram, vertexShader);
gl.attachShader(pointProgram, fragmentShader);
gl.linkProgram(pointProgram);
gl.useProgram(pointProgram);
gl.aPointSize = gl.getAttribLocation(pointProgram, "aPointSize");
}
function loadData(arrayBuffer, exception) {
var data = new DataView(arrayBuffer);
POINT_COUNT = (data.byteLength / Float32Array.BYTES_PER_ELEMENT) / 2;
var rawData = new Float32Array(
data.byteLength / Float32Array.BYTES_PER_ELEMENT);
var len = rawData.length;
// Incoming data is raw floating point values
// with little-endian byte ordering.
for (var i = 0; i < len; i += 2) {
lat = data.getFloat32(i * Float32Array.BYTES_PER_ELEMENT, true);
lon = data.getFloat32((i +1 ) * Float32Array.BYTES_PER_ELEMENT, true);
var pixel = LatLongToPixelXY(lat, lon);
rawData[i] = pixel.x;
rawData[i+1] = pixel.y;
}
// create webgl buffer, bind it, and load rawData into it
pointArrayBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointArrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rawData, gl.STATIC_DRAW);
// enable the 'worldCoord' attribute in the shader to receive buffer
var attributeLoc = gl.getAttribLocation(pointProgram, 'worldCoord');
gl.enableVertexAttribArray(attributeLoc);
// tell webgl how buffer is laid out (pairs of x,y coords)
gl.vertexAttribPointer(attributeLoc, 2, gl.FLOAT, false, 0, 0);
}
function resize() {
var width = canvasLayer.canvas.width;
var height = canvasLayer.canvas.height;
gl.viewport(0, 0, width, height);
// matrix which maps pixel coordinates to WebGL coordinates
pixelsToWebGLMatrix.set([2/width, 0, 0, 0, 0, -2/height, 0, 0,
0, 0, 0, 0, -1, 1, 0, 1]);
}
function scaleMatrix(matrix, scaleX, scaleY) {
// scaling x and y, which is just scaling first two columns of matrix
matrix[0] *= scaleX;
matrix[1] *= scaleX;
matrix[2] *= scaleX;
matrix[3] *= scaleX;
matrix[4] *= scaleY;
matrix[5] *= scaleY;
matrix[6] *= scaleY;
matrix[7] *= scaleY;
}
function translateMatrix(matrix, tx, ty) {
// translation is in last column of matrix
matrix[12] += matrix[0]*tx + matrix[4]*ty;
matrix[13] += matrix[1]*tx + matrix[5]*ty;
matrix[14] += matrix[2]*tx + matrix[6]*ty;
matrix[15] += matrix[3]*tx + matrix[7]*ty;
}
function update() {
if (drillingWells.arrayBuffers.length == 2) {
gl.clear(gl.COLOR_BUFFER_BIT);
var pointSize = Math.floor((map.zoom - 4.) / (25. - 4.) * (20. - 1) + 1);
gl.vertexAttrib1f(gl.aPointSize, pointSize*1.0);
var mapProjection = map.getProjection();
// copy pixel->webgl matrix
mapMatrix.set(pixelsToWebGLMatrix);
// Scale to current zoom (worldCoords * 2^zoom)
var scale = Math.pow(2, map.zoom);
scaleMatrix(mapMatrix, scale, scale);
// translate to current view (vector from topLeft to 0,0)
var offset = mapProjection.fromLatLngToPoint(canvasLayer.getTopLeft());
translateMatrix(mapMatrix, -offset.x, -offset.y);
// attach matrix value to 'mapMatrix' uniform in shader
var matrixLoc = gl.getUniformLocation(pointProgram, 'mapMatrix');
gl.uniformMatrix4fv(matrixLoc, false, mapMatrix);
// draw!
gl.drawArrays(gl.POINTS, 0, drillingWells.arrayBuffers[currentIndex].length/2.);
}
}
document.addEventListener('DOMContentLoaded', init, false);
</script>
<script id="pointVertexShader" type="x-shader/x-vertex">
attribute vec4 worldCoord;
attribute float aPointSize;
uniform mat4 mapMatrix;
void main() {
// transform world coordinate by matrix uniform variable
gl_Position = mapMatrix * worldCoord;
// a constant size for points, regardless of zoom level
gl_PointSize = aPointSize;
}
</script>
<script id="pointFragmentShader" type="x-shader/x-fragment">
precision mediump float;
void main() {
float dist = length(gl_PointCoord.xy - vec2(.5, .5));
dist = 1. - (dist * 2.);
dist = max(0., dist);
// float alpha = (dist > .5) ? 0. : 1.;
// set pixels in points to malevolent
gl_FragColor = vec4(.82, .22, .07, 1.) * dist;
}
</script>
</head>
<body>
<div id="map-div"></div>
</body>
</html>