-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplottedgeometry.js
More file actions
375 lines (338 loc) · 18 KB
/
Copy pathplottedgeometry.js
File metadata and controls
375 lines (338 loc) · 18 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
var PlotGeometryObject =
{
// filled in externally
scene: null,
peaktrianglematerial: null,
peaktriangles: null,
centrelinematerial: null,
centrelinebuffergeometry: null,
centrelines: null,
minyearvalue: 9999.0,
maxyearvalue: 9999.0,
altminF: 0,
altmaxF: 0,
vfac:0.003972,
redalt: 0.894682,
lightvx: 0.2, lightvy: 0.1, lightvz: Math.sqrt(1 - (0.2*0.2 + 0.1*0.1)),
enttrianglematerial: null,
entgeometry: null,
entlabelscard: null,
entriangles: null,
passagetubematerial: null,
passagetubes: null,
// should get rid of this and use entlabelscard.children[i].material instead
textlabelmaterials: [ ], //
MakeLabel: function(card, text, fillstyle, p, visibledistance, scale)
{
var canvas1 = document.createElement('canvas');
canvas1.width = 256; canvas1.height = 32;
var context1 = canvas1.getContext('2d');
context1.font = "28px Helvetica";
context1.fillStyle = fillstyle;
context1.fillText(text, 0, 20);
// this one we can highlight it individually with a selection boolean
var texture1 = new THREE.Texture(canvas1);
texture1.needsUpdate = true;
var textlabelmaterial = new THREE.ShaderMaterial({
uniforms: { pixelsize: {type: 'f', value: 1.2},
aspect: { type: 'f', value: 1.0 },
textureaspect: { type: 'f', value: canvas1.width/canvas1.height },
texture: { value: texture1 },
closedist: { type: 'f', value: 5.0 },
bselindex: { type: 'f', value: 0.0 },
redalt: { type: 'f', value: this.redalt }, // shouldn't be here
vfac: { type: 'f', value: this.vfac }
},
vertexShader: getshader('vertex_shader_textlabel'),
fragmentShader: getshader('fragment_shader_textlabel'),
depthWrite:true, depthTest:true,
side: THREE.DoubleSide
});
this.textlabelmaterials.push(textlabelmaterial);
var textlabelpositionsbuff = new THREE.BufferAttribute(new Float32Array(4*3), 3);
var textlabeluvsbuff = new THREE.BufferAttribute(new Float32Array(4*2), 2);
for (var i = 0; i < 4; i++) {
textlabelpositionsbuff.setXYZ(i, p.x, p.y, p.z);
textlabeluvsbuff.setXY(i, ((i == 0) || (i == 2) ? 0.0 : 1.0), (i <= 1 ? 0.0 : 1.0));
}
var indices = new Uint16Array(6);
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 1; indices[4] = 3; indices[5] = 2;
var buffergeometry = new THREE.BufferGeometry();
buffergeometry.setIndex(new THREE.BufferAttribute(indices, 1));
buffergeometry.addAttribute('position', textlabelpositionsbuff);
buffergeometry.addAttribute('uv', textlabeluvsbuff);
mesh1 = new THREE.Mesh(buffergeometry, textlabelmaterial);
card.add(mesh1);
},
LoadMountains: function(landmarks, svxscaleInv)
{
if (landmarks.length == 0) return;
var peakpositionbuff = new THREE.BufferAttribute(new Float32Array(landmarks.length*9), 3);
var peakcorner = new Float32Array(landmarks.length*3);
var card = new THREE.Object3D();
for (var i = 0; i < landmarks.length; i++) {
var landmark = landmarks[i];
var xl = -landmark[1]*svxscaleInv, yl=landmark[3]*svxscaleInv, zl=landmark[2]*svxscaleInv;
peakpositionbuff.setXYZ(i*3, xl, yl, zl); peakpositionbuff.setXYZ(i*3+1, xl, yl, zl); peakpositionbuff.setXYZ(i*3+2, xl, yl, zl);
peakcorner[i*3] = 0.0; peakcorner[i*3+1] = 1.0; peakcorner[i*3+2] = 2.0;
this.MakeLabel(card, landmarks[i][0], "rgba(255,255,255,0.95)", {x:xl, y:yl, z:zl}, 100000, 10);
}
this.scene.add(card);
var buffergeometry = new THREE.BufferGeometry();
buffergeometry.addAttribute('position', peakpositionbuff);
buffergeometry.addAttribute('pcorner', new THREE.BufferAttribute(peakcorner, 1));
this.peaktrianglematerial = new THREE.ShaderMaterial({
uniforms: { trianglesize: {type: 'f', value: 15.0},
aspect: { type: 'f', value: 1.0 }
},
vertexShader: getshader('vertex_shader_peaktriangle'),
depthWrite:true, depthTest:true, // how is the colour being set to red??
side: THREE.DoubleSide
});
this.peaktriangles = new THREE.Mesh(buffergeometry, this.peaktrianglematerial);
this.scene.add(this.peaktriangles);
},
LoadCentrelines: function(legnodes, legindexes, svxscaleInv)
{
this.centrelinematerial = new THREE.ShaderMaterial({
uniforms: { closedist: { type: 'f', value: 5.0 },
selindexlo: { type: 'f', value: -1.0 },
selindexhi: { type: 'f', value: -1.0 },
redalt: { type: 'f', value: this.redalt },
vfac: { type: 'f', value: this.vfac }
},
vertexShader: getshader('vertex_shader_centreline'),
fragmentShader: getshader('fragment_shader_centreline'),
depthWrite:true, depthTest:true, // not sure these work
linewidth:3
});
var nlegs = legindexes.length/2;
var centrelinepositionsbuff = new THREE.BufferAttribute(new Float32Array(nlegs*2*3), 3);
var cosslope = new Float32Array(nlegs*2);
var legindex = new Float32Array(nlegs*2);
for (var i = 0; i < nlegs; i++) {
var i0 = legindexes[i*2]*3;
var i1 = legindexes[i*2+1]*3;
var x0 = -legnodes[i0]*svxscaleInv, y0=legnodes[i0+2]*svxscaleInv, z0=legnodes[i0+1]*svxscaleInv;
var x1 = -legnodes[i1]*svxscaleInv, y1=legnodes[i1+2]*svxscaleInv, z1=legnodes[i1+1]*svxscaleInv;
centrelinepositionsbuff.setXYZ(i*2, x0, y0, z0);
centrelinepositionsbuff.setXYZ(i*2+1, x1, y1, z1);
var dx = x1 - x0, dy = y1 - y0, dz = z1 - z0;
var lcosslope = Math.cos(Math.atan2(dy, Math.sqrt(dx*dx + dz*dz)));
cosslope[i*2] = lcosslope;
cosslope[i*2+1] = lcosslope;
legindex[i*2] = i;
legindex[i*2+1] = i;
if ((i == 0) || (this.altminF > legnodes[i0+2]))
this.altminF = legnodes[i0+2];
if ((this.altminF > legnodes[i1+2]))
this.altminF = legnodes[i1+2];
if ((i == 0) || (this.altmaxF < legnodes[i1+2]))
this.altmaxF = legnodes[i0+2];
if ((this.altmaxF < legnodes[i1+2]))
this.altmaxF = legnodes[i1+2];
}
console.log("altminmax", this.altminF*svxscaleInv, this.altmaxF*svxscaleInv);
this.vfac = 0.9/((this.altmaxF - this.altminF)*svxscaleInv);
this.redalt = (0.5 - this.altmaxF*svxscaleInv*this.vfac) % 1;
this.centrelinebuffergeometry = new THREE.BufferGeometry();
this.centrelinebuffergeometry.addAttribute('position', centrelinepositionsbuff);
this.centrelinebuffergeometry.addAttribute('cosslope', new THREE.BufferAttribute(cosslope, 1));
this.centrelinebuffergeometry.addAttribute('legindex', new THREE.BufferAttribute(legindex, 1));
this.centrelines = new THREE.LineSegments(this.centrelinebuffergeometry, this.centrelinematerial);
this.scene.add(this.centrelines);
},
// vertex_shader_enttriangle
LoadEntrances: function(legnodes, legentrances, svxscaleInv)
{
var nentrances = legentrances.length/3;
var entpositionbuff = new THREE.BufferAttribute(new Float32Array(nentrances*9), 3);
var entcorner = new Float32Array(nentrances*3);
var entindex = new Float32Array(nentrances*3);
var entvisibledistance = new Float32Array(nentrances*3);
this.entgeometry = new THREE.BufferGeometry();
this.entgeometry.addAttribute('position', entpositionbuff);
this.entgeometry.addAttribute('pcorner', new THREE.BufferAttribute(entcorner, 1));
this.entgeometry.addAttribute('entindex', new THREE.BufferAttribute(entindex, 1));
this.entgeometry.addAttribute('entvisibledistance', new THREE.BufferAttribute(entvisibledistance, 1));
this.enttrianglematerial = new THREE.ShaderMaterial({
uniforms: { trianglesize: {type: 'f', value: 10.0},
aspect: { type: 'f', value: 1.0 },
closedist: { type: 'f', value: 5.0 },
selindexlo: { type: 'f', value: -1.0 },
selindexhi: { type: 'f', value: -1.0 },
redalt: { type: 'f', value: this.redalt },
vfac: { type: 'f', value: this.vfac }
},
vertexShader: getshader('vertex_shader_enttriangle'),
fragmentShader: getshader('fragment_shader_centreline'),
depthWrite:true, depthTest:true,
side: THREE.DoubleSide
});
this.enttriangles = new THREE.Mesh(this.entgeometry, this.enttrianglematerial);
this.scene.add(this.enttriangles);
this.entlabelscard = new THREE.Object3D();
for (var i = 0; i < nentrances; i++) {
var i1 = legentrances[i*3+0]*3; // node index
var x1 = -legnodes[i1]*svxscaleInv, y1=legnodes[i1+2]*svxscaleInv, z1=legnodes[i1+1]*svxscaleInv;
var p = {x:x1, y:y1, z:z1};
entpositionbuff.setXYZ(i*3, p.x, p.y, p.z); entpositionbuff.setXYZ(i*3+1, p.x, p.y, p.z); entpositionbuff.setXYZ(i*3+2, p.x, p.y, p.z);
entcorner[i*3] = 0.0; entcorner[i*3+1] = 1.0; entcorner[i*3+2] = 2.0;
entindex[i*3] = i; entindex[i*3+1] = i; entindex[i*3+2] = i;
var visibledistance = i*2.5;
entvisibledistance[i*3] = visibledistance; entvisibledistance[i*3+1] = visibledistance; entvisibledistance[i*3+2] = visibledistance;
this.MakeLabel(this.entlabelscard, legentrances[i*3+1], "rgba(0,200,200,0.95)", p, visibledistance, 0.5); // rgba(0,200,200,0.95)
}
this.scene.add(this.entlabelscard);
},
LoadPassageTubesP: function(cumupassagexcsseq, xcs, svxscaleInv)
{
var nxcs = cumupassagexcsseq[cumupassagexcsseq.length - 1];
console.assert(xcs.length == nxcs*3*4);
this.passagetubematerial = new THREE.ShaderMaterial({
uniforms: { closedist: { type: 'f', value: 5.0 },
selindexlo: { type: 'f', value: -1.0 },
selindexhi: { type: 'f', value: -1.0 },
redalt: { type: 'f', value: this.redalt },
vfac: { type: 'f', value: this.vfac }
},
vertexShader: getshader('vertex_shader_passage'),
fragmentShader: getshader('fragment_shader_passage'),
wireframe: false,
depthWrite:true, depthTest:true
});
var nnodes = xcs.length/3;
var flatfaceattributes = new Float32Array(nnodes*4);
var ssgvertbuff = new THREE.Float32Attribute(new Float32Array(nnodes*3), 3);
var passageindex = new Float32Array(nnodes);
for (var i = 0; i < nnodes; i++) {
ssgvertbuff.setXYZ(i, -xcs[i*3]*svxscaleInv, xcs[i*3+2]*svxscaleInv, xcs[i*3+1]*svxscaleInv);
passageindex[i] = i/4|0; // int value for index of the xsection
}
var ntris = (4*nxcs - 2*cumupassagexcsseq.length)*2; // 4 quads between pairs of xcs plus endcaps
var indices = new Uint16Array(ntris*3);
var iquad = 0;
var AddQuad = function(q0, q1, q2, q3, iflat) {
indices[iquad*6] = q0;
indices[iquad*6+1] = q2;
indices[iquad*6+2] = q1;
indices[iquad*6+3] = q0;
indices[iquad*6+4] = q3;
indices[iquad*6+5] = q2;
if (iflat != 0) {
var ax = ssgvertbuff.getX(q1) - ssgvertbuff.getX(q0), ay = ssgvertbuff.getY(q1) - ssgvertbuff.getY(q0), az = ssgvertbuff.getZ(q1) - ssgvertbuff.getZ(q0);
var bx = ssgvertbuff.getX(q2) - ssgvertbuff.getX(q0), by = ssgvertbuff.getY(q2) - ssgvertbuff.getY(q0), bz = ssgvertbuff.getZ(q2) - ssgvertbuff.getZ(q0);
var cx = ay*bz - by*az, cy = -ax*bz + bx*az, cz = ax*by - bx*ay;
var cleng = Math.sqrt(cx*cx + cy*cy + cz*cz);
var cdot = Math.min(Math.abs(PlotGeometryObject.lightvx*cx + PlotGeometryObject.lightvy*cz + PlotGeometryObject.lightvz*cy)/cleng, 1.0);
//cdot = Math.random();
var cosfac = (1 - cdot)*0.4;
var flatval = (iflat>0 ? 510+cosfac : -510-cosfac);
var lflatel = (iflat>0?iflat:-iflat)-1;
flatfaceattributes[q0*4+lflatel] = flatval;
flatfaceattributes[q1*4+lflatel] = flatval;
flatfaceattributes[q2*4+lflatel] = flatval;
flatfaceattributes[q3*4+lflatel] = flatval;
}
iquad++;
}
for (var j = 0; j < cumupassagexcsseq.length; j++) {
var k = (j != 0 ? cumupassagexcsseq[j-1] : 0)*4;
var passagexcsseqj = cumupassagexcsseq[j] - (j != 0 ? cumupassagexcsseq[j-1] : 0);
AddQuad(k+0, k+1, k+2, k+3, 2);
for (var l = 0; l < passagexcsseqj-1; l++) {
k += 4;
var l1 = (l%2 + 1)*(Math.floor(l/2)%2 == 0?1:-1);
var l2 = (l%2 + 3)*(Math.floor(l/2)%2 == 0?1:-1);
AddQuad(k+0, k+1, k-4+1, k-4+0, l1);
AddQuad(k+1, k+2, k-4+2, k-4+1, l2);
AddQuad(k+2, k+3, k-4+3, k-4+2, -l1);
AddQuad(k+3, k+0, k-4+0, k-4+3, -l2);
}
AddQuad(k+3, k+2, k+1, k+0, (passagexcsseqj%2 + 2));
k += 4;
}
console.assert(iquad == (4*nxcs - 2*cumupassagexcsseq.length));
var buffergeometry = new THREE.BufferGeometry();
buffergeometry.setIndex(new THREE.BufferAttribute(indices, 1));
buffergeometry.addAttribute('position', ssgvertbuff);
buffergeometry.addAttribute('flat4', new THREE.Float32Attribute(flatfaceattributes, 4)); // goes into vec4
buffergeometry.addAttribute('passageindex', new THREE.Float32Attribute(passageindex, 1));
//this.passagetubematerial = new THREE.MeshBasicMaterial({ color: 0xDD44EE, shading: THREE.FlatShading, depthWrite:true, depthTest:true });
this.passagetubes = new THREE.Mesh(buffergeometry, this.passagetubematerial);
this.scene.add(this.passagetubes);
},
resizeP: function(width, height)
{
var aspect = width / height;
if (this.peaktrianglematerial) {
this.peaktrianglematerial.uniforms.aspect.value = aspect;
this.peaktrianglematerial.uniforms.trianglesize.value = 50/width;
}
for (var i = 0; i < this.textlabelmaterials.length; i++) {
this.textlabelmaterials[i].uniforms.aspect.value = aspect;
this.textlabelmaterials[i].uniforms.pixelsize.value = 60/width;
}
if (this.enttrianglematerial) {
this.enttrianglematerial.uniforms.aspect.value = aspect;
this.enttrianglematerial.uniforms.trianglesize.value = 5/width;
}
},
setredalts: function(redalt, vfac)
{
if (this.passagetubematerial) {
this.passagetubematerial.uniforms.redalt.value = redalt;
this.passagetubematerial.uniforms.vfac.value = vfac;
}
for (var i = 0; i < this.textlabelmaterials.length; i++) {
this.textlabelmaterials[i].uniforms.redalt.value = redalt;
this.textlabelmaterials[i].uniforms.vfac.value = vfac;
}
if (this.centrelinematerial) {
this.centrelinematerial.uniforms.redalt.value = redalt;
this.centrelinematerial.uniforms.vfac.value = vfac;
}
if (this.enttrianglematerial) {
this.enttrianglematerial.uniforms.redalt.value = redalt;
this.enttrianglematerial.uniforms.vfac.value = vfac;
}
},
setclosedistvalueP: function(closedistvalue)
{
if (this.centrelinematerial)
this.centrelinematerial.uniforms.closedist.value = closedistvalue;
if (this.passagetubematerial)
this.passagetubematerial.uniforms.closedist.value = closedistvalue;
for (var i = 0; i < this.textlabelmaterials.length; i++)
this.textlabelmaterials[i].uniforms.closedist.value = closedistvalue;
if (this.enttrianglematerial)
this.enttrianglematerial.uniforms.closedist.value = closedistvalue;
this.Dsetentvisibility(closedistvalue);
},
entproj: new THREE.Vector4(),
Dsetentvisibility: function(closedistvalue) {
for (var i = 0; i < svx3d.nentrances; i++) {
this.entproj.fromArray(PlotGeometryObject.entgeometry.attributes.position.array, i*9);
this.entproj.setW(1.0);
var mvPosition = this.entproj.applyMatrix4(PlotGraphics.camera.modelViewMatrix)
var glPosition = mvPosition.applyMatrix4(PlotGraphics.camera.projectionMatrix);
var fdepth = glPosition.z;
if ((i%50)==0) console.log(glPosition);
// vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
// gl_Position = projectionMatrix * mvPosition;
//if ((i%50)==0) console.log(this.entproj);
this.entlabelscard.children[i].visible = (fdepth < closedistvalue);
}
},
togglelabels: function(event) {
if (this.entlabelscard)
this.entlabelscard.visible = !this.entlabelscard.visible;
PositionObject.footposmesh.visible = !PositionObject.footposmesh.visible;
document.getElementById("labelsonoff").className = (PositionObject.footposmesh.visible ? "" : "selected");
},
togglepassages: function(event) {
this.passagetubes.visible = !this.passagetubes.visible;
document.getElementById("passagesonoff").className = (this.passagetubes.visible ? "" : "selected");
}
};