-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibpannellum.js
executable file
·1278 lines (1151 loc) · 50.5 KB
/
libpannellum.js
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libpannellum - A WebGL and CSS 3D transform based Panorama Renderer
* Copyright (c) 2012-2016 Matthew Petroff
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
window.libpannellum = (function(window, document, undefined) {
'use strict';
/**
* Creates a new panorama renderer.
* @constructor
* @param {HTMLElement} container - The container element for the renderer.
*/
function Renderer(container) {
var canvas = document.createElement('canvas');
canvas.style.width = canvas.style.height = '100%';
container.appendChild(canvas);
var program, gl, vs, fs;
var fallbackImgSize;
var world;
var vtmps;
var pose;
var image, imageType, dynamic;
var texCoordBuffer, cubeVertBuf, cubeVertTexCoordBuf, cubeVertIndBuf;
/**
* Initialize renderer.
* @memberof Renderer
* @instance
* @param {Image|Array|Object} image - Input image; format varies based on
* `imageType`. For `equirectangular`, this is an image; for
* `cubemap`, this is an array of images for the cube faces in the
* order [+z, +x, -z, -x, +y, -y]; for `multires`, this is a
* configuration object.
* @param {string} imageType - The type of the image: `equirectangular`,
* `cubemap`, or `multires`.
* @param {boolean} dynamic - Whether or not the image is dynamic (e.g. video).
* @param {number} haov - Initial horizontal angle of view.
* @param {number} vaov - Initial vertical angle of view.
* @param {number} voffset - Initial vertical offset angle.
* @param {function} callback - Load callback function.
* @param {Object} [params] - Other configuration parameters (`horizonPitch`, `horizonRoll`, `backgroundColor`).
*/
this.init = function(_image, _imageType, _dynamic, haov, vaov, voffset, callback, params) {
// Default argument for image type
if (typeof _imageType === undefined)
_imageType = 'equirectangular';
imageType = _imageType;
image = _image;
dynamic = _dynamic;
// Clear old data
if (program) {
if (vs) {
gl.detachShader(program, vs);
gl.deleteShader(vs);
}
if (fs) {
gl.detachShader(program, fs);
gl.deleteShader(fs);
}
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
if (program.texture)
gl.deleteTexture(program.texture);
if (program.nodeCache)
for (var i = 0; i < program.nodeCache.length; i++)
gl.deleteTexture(program.nodeCache[i].texture);
gl.deleteProgram(program);
program = undefined;
}
pose = undefined;
var s;
// This awful browser specific test exists because iOS 8/9 and IE 11
// don't display non-power-of-two cubemap textures but also don't
// throw an error (tested on an iPhone 5c / iOS 8.1.3 / iOS 9.2).
// Therefore, the WebGL context is never created for these browsers for
// NPOT cubemaps, and the CSS 3D transform fallback renderer is used
// instead.
if (!(imageType == 'cubemap' &&
(image[0].width & (image[0].width - 1)) !== 0 &&
(navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad).* os 8_/) ||
navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad).* os 9_/) ||
navigator.userAgent.match(/Trident.*rv[ :]*11\./)))) {
// Enable WebGL on canvas
if (!gl)
gl = canvas.getContext('experimental-webgl', {alpha: false, depth: false});
}
// If there is no WebGL, fall back to CSS 3D transform renderer.
// While browser specific tests are usually frowned upon, the
// fallback viewer only really works with WebKit/Blink and IE 10/11
// (it doesn't work properly in Firefox).
if (!gl && ((imageType == 'multires' && image.hasOwnProperty('fallbackPath')) ||
imageType == 'cubemap') &&
('WebkitAppearance' in document.documentElement.style ||
navigator.userAgent.match(/Trident.*rv[ :]*11\./) ||
navigator.appVersion.indexOf('MSIE 10') !== -1)) {
// Remove old world if it exists
if (world) {
container.removeChild(world);
}
// Initialize renderer
world = document.createElement('div');
world.className = 'pnlm-world';
// Add images
var path;
if (image.basePath) {
path = image.basePath + image.fallbackPath;
} else {
path = image.fallbackPath;
}
var sides = ['f', 'r', 'b', 'l', 'u', 'd'];
var loaded = 0;
var onLoad = function() {
// Draw image on canvas
var faceCanvas = document.createElement('canvas');
faceCanvas.className = 'pnlm-face pnlm-' + sides[this.side] + 'face';
world.appendChild(faceCanvas);
var faceContext = faceCanvas.getContext('2d');
faceCanvas.style.width = this.width + 4 + 'px';
faceCanvas.style.height = this.height + 4 + 'px';
faceCanvas.width = this.width + 4;
faceCanvas.height = this.height + 4;
faceContext.drawImage(this, 2, 2);
var imgData = faceContext.getImageData(0, 0, faceCanvas.width, faceCanvas.height);
var data = imgData.data;
// Duplicate edge pixels
var i;
var j;
for (i = 2; i < faceCanvas.width - 2; i++) {
for (j = 0; j < 4; j++) {
data[(i + faceCanvas.width) * 4 + j] = data[(i + faceCanvas.width * 2) * 4 + j];
data[(i + faceCanvas.width * (faceCanvas.height - 2)) * 4 + j] = data[(i + faceCanvas.width * (faceCanvas.height - 3)) * 4 + j];
}
}
for (i = 2; i < faceCanvas.height - 2; i++) {
for (j = 0; j < 4; j++) {
data[(i * faceCanvas.width + 1) * 4 + j] = data[(i * faceCanvas.width + 2) * 4 + j];
data[((i + 1) * faceCanvas.width - 2) * 4 + j] = data[((i + 1) * faceCanvas.width - 3) * 4 + j];
}
}
for (j = 0; j < 4; j++) {
data[(faceCanvas.width + 1) * 4 + j] = data[(faceCanvas.width * 2 + 2) * 4 + j];
data[(faceCanvas.width * 2 - 2) * 4 + j] = data[(faceCanvas.width * 3 - 3) * 4 + j];
data[(faceCanvas.width * (faceCanvas.height - 2) + 1) * 4 + j] = data[(faceCanvas.width * (faceCanvas.height - 3) + 2) * 4 + j];
data[(faceCanvas.width * (faceCanvas.height - 1) - 2) * 4 + j] = data[(faceCanvas.width * (faceCanvas.height - 2) - 3) * 4 + j];
}
for (i = 1; i < faceCanvas.width - 1; i++) {
for (j = 0; j < 4; j++) {
data[i * 4 + j] = data[(i + faceCanvas.width) * 4 + j];
data[(i + faceCanvas.width * (faceCanvas.height - 1)) * 4 + j] = data[(i + faceCanvas.width * (faceCanvas.height - 2)) * 4 + j];
}
}
for (i = 1; i < faceCanvas.height - 1; i++) {
for (j = 0; j < 4; j++) {
data[(i * faceCanvas.width) * 4 + j] = data[(i * faceCanvas.width + 1) * 4 + j];
data[((i + 1) * faceCanvas.width - 1) * 4 + j] = data[((i + 1) * faceCanvas.width - 2) * 4 + j];
}
}
for (j = 0; j < 4; j++) {
data[j] = data[(faceCanvas.width + 1) * 4 + j];
data[(faceCanvas.width - 1) * 4 + j] = data[(faceCanvas.width * 2 - 2) * 4 + j];
data[(faceCanvas.width * (faceCanvas.height - 1)) * 4 + j] = data[(faceCanvas.width * (faceCanvas.height - 2) + 1) * 4 + j];
data[(faceCanvas.width * faceCanvas.height - 1) * 4 + j] = data[(faceCanvas.width * (faceCanvas.height - 1) - 2) * 4 + j];
}
// Draw image width duplicated edge pixels on canvas
faceContext.putImageData(imgData, 0, 0);
loaded++;
if (loaded == 6) {
fallbackImgSize = this.width;
container.appendChild(world);
callback();
}
};
for (s = 0; s < 6; s++) {
var faceImg = new Image();
faceImg.crossOrigin = 'anonymous';
faceImg.side = s;
faceImg.onload = onLoad;
if (imageType == 'multires') {
faceImg.src = encodeURI(path.replace('%s', sides[s]) + '.' + image.extension);
} else {
faceImg.src = encodeURI(image[s].src);
}
}
return;
} else if (!gl) {
console.log('Error: no WebGL support detected!');
throw {type: 'no webgl'};
}
if (image.basePath) {
image.fullpath = image.basePath + image.path;
} else {
image.fullpath = image.path;
}
image.invTileResolution = 1 / image.tileResolution;
var vertices = createCube();
vtmps = [];
for (s = 0; s < 6; s++) {
vtmps[s] = vertices.slice(s * 12, s * 12 + 12);
vertices = createCube();
}
// Make sure image isn't too big
var width, maxWidth;
if (imageType == 'equirectangular') {
width = Math.max(image.width, image.height);
maxWidth = gl.getParameter(gl.MAX_TEXTURE_SIZE);
if (width > maxWidth) {
console.log('Error: The image is too big; it\'s ' + width + 'px wide, but this device\'s maximum supported width is ' + maxWidth + 'px.');
throw {type: 'webgl size error', width: width, maxWidth: maxWidth};
}
} else if (imageType == 'cubemap') {
width = image[0].width;
maxWidth = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE);
if (width > maxWidth) {
console.log('Error: The cube face image is too big; it\'s ' + width + 'px wide, but this device\'s maximum supported width is ' + maxWidth + 'px.');
throw {type: 'webgl size error', width: width, maxWidth: maxWidth};
}
}
// Store horizon pitch and roll if applicable
if (params !== undefined && (params.horizonPitch !== undefined || params.horizonRoll !== undefined))
pose = [params.horizonPitch == undefined ? 0 : params.horizonPitch,
params.horizonRoll == undefined ? 0 : params.horizonRoll];
// Set 2d texture binding
var glBindType = gl.TEXTURE_2D;
// Create viewport for entire canvas
gl.viewport(0, 0, canvas.width, canvas.height);
// Create vertex shader
vs = gl.createShader(gl.VERTEX_SHADER);
var vertexSrc = v;
if (imageType == 'multires') {
vertexSrc = vMulti;
}
gl.shaderSource(vs, vertexSrc);
gl.compileShader(vs);
// Create fragment shader
fs = gl.createShader(gl.FRAGMENT_SHADER);
var fragmentSrc = fragEquirectangular;
if (imageType == 'cubemap') {
glBindType = gl.TEXTURE_CUBE_MAP;
fragmentSrc = fragCube;
} else if (imageType == 'multires') {
fragmentSrc = fragMulti;
}
gl.shaderSource(fs, fragmentSrc);
gl.compileShader(fs);
// Link WebGL program
program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
// Log errors
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS))
console.log(gl.getShaderInfoLog(vs));
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS))
console.log(gl.getShaderInfoLog(fs));
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
console.log(gl.getProgramInfoLog(program));
// Use WebGL program
gl.useProgram(program);
program.drawInProgress = false;
// Look up texture coordinates location
program.texCoordLocation = gl.getAttribLocation(program, 'a_texCoord');
gl.enableVertexAttribArray(program.texCoordLocation);
if (imageType != 'multires') {
// Provide texture coordinates for rectangle
if (!texCoordBuffer)
texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,1,1,1,1,-1,-1,1,1,-1,-1,-1]), gl.STATIC_DRAW);
gl.vertexAttribPointer(program.texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// Pass aspect ratio
program.aspectRatio = gl.getUniformLocation(program, 'u_aspectRatio');
gl.uniform1f(program.aspectRatio, canvas.width / canvas.height);
// Locate psi, theta, focal length, horizontal extent, vertical extent, and vertical offset
program.psi = gl.getUniformLocation(program, 'u_psi');
program.theta = gl.getUniformLocation(program, 'u_theta');
program.f = gl.getUniformLocation(program, 'u_f');
program.h = gl.getUniformLocation(program, 'u_h');
program.v = gl.getUniformLocation(program, 'u_v');
program.vo = gl.getUniformLocation(program, 'u_vo');
program.rot = gl.getUniformLocation(program, 'u_rot');
// Pass horizontal extent, vertical extent, and vertical offset
gl.uniform1f(program.h, haov / (Math.PI * 2.0));
gl.uniform1f(program.v, vaov / Math.PI);
gl.uniform1f(program.vo, voffset / Math.PI * 2);
// Set background color
if (imageType == 'equirectangular') {
program.backgroundColor = gl.getUniformLocation(program, 'u_backgroundColor');
var color = params.backgroundColor ? params.backgroundColor : [0, 0, 0];
gl.uniform4fv(program.backgroundColor, color.concat([1]));
}
// Create texture
program.texture = gl.createTexture();
gl.bindTexture(glBindType, program.texture);
// Upload images to texture depending on type
if (imageType == 'cubemap') {
// Load all six sides of the cube map
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[1]);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[3]);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[4]);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[5]);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[0]);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[2]);
} else {
// Upload image to the texture
gl.texImage2D(glBindType, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
}
// Set parameters for rendering any size
gl.texParameteri(glBindType, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(glBindType, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(glBindType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(glBindType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
} else {
// Look up vertex coordinates location
program.vertPosLocation = gl.getAttribLocation(program, 'a_vertCoord');
gl.enableVertexAttribArray(program.vertPosLocation);
// Create buffers
if (!cubeVertBuf)
cubeVertBuf = gl.createBuffer();
if (!cubeVertTexCoordBuf)
cubeVertTexCoordBuf = gl.createBuffer();
if (!cubeVertIndBuf)
cubeVertIndBuf = gl.createBuffer();
// Bind texture coordinate buffer and pass coordinates to WebGL
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertTexCoordBuf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0,0,1,0,1,1,0,1]), gl.STATIC_DRAW);
// Bind square index buffer and pass indicies to WebGL
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertIndBuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0,1,2,0,2,3]), gl.STATIC_DRAW);
// Find uniforms
program.perspUniform = gl.getUniformLocation(program, 'u_perspMatrix');
program.cubeUniform = gl.getUniformLocation(program, 'u_cubeMatrix');
//program.colorUniform = gl.getUniformLocation(program, 'u_color');
program.level = -1;
program.currentNodes = [];
program.nodeCache = [];
program.nodeCacheTimestamp = 0;
}
// Check if there was an error
if (gl.getError() !== 0) {
console.log('Error: Something went wrong with WebGL!');
throw {type: 'webgl error'};
}
callback();
};
/**
* Destroy renderer.
* @memberof Renderer
* @instance
*/
this.destroy = function() {
if (container !== undefined) {
if (canvas !== undefined) {
container.removeChild(canvas);
}
if (world !== undefined) {
container.removeChild(world);
}
}
if (gl) {
// The spec says this is only supposed to simulate losing the WebGL
// context, but in practice it tends to actually free the memory.
var extension = gl.getExtension('WEBGL_lose_context');
if (extension)
extension.loseContext();
}
};
/**
* Resize renderer (call after resizing container).
* @memberof Renderer
* @instance
*/
this.resize = function() {
var pixelRatio = window.devicePixelRatio || 1;
canvas.width = canvas.clientWidth * pixelRatio;
canvas.height = canvas.clientHeight * pixelRatio;
if (gl) {
gl.viewport(0, 0, canvas.width, canvas.height);
if (imageType != 'multires') {
gl.uniform1f(program.aspectRatio, canvas.width / canvas.height);
}
}
};
// Initialize canvas size
this.resize();
/**
* Render new view of panorama.
* @memberof Renderer
* @instance
* @param {number} pitch - Pitch to render at (in radians).
* @param {number} yaw - Yaw to render at (in radians).
* @param {number} hfov - Horizontal field of view to render with (in radians).
* @param {Object} [params] - Extra configuration parameters.
* @param {number} [params.roll] - Camera roll (in radians).
* @param {boolean} [params.returnImage] - Return rendered image?
*/
this.render = function(pitch, yaw, hfov, params) {
var focal, i, s, roll = 0;
if (params === undefined)
params = {};
if (params.roll)
roll = params.roll;
// Apply pitch and roll transformation if applicable
if (pose !== undefined) {
var horizonPitch = pose[0],
horizonRoll = pose[1];
// Calculate new pitch and yaw
var orig_pitch = pitch,
orig_yaw = yaw,
x = Math.cos(horizonRoll) * Math.sin(pitch) * Math.sin(horizonPitch) +
Math.cos(pitch) * (Math.cos(horizonPitch) * Math.cos(yaw) +
Math.sin(horizonRoll) * Math.sin(horizonPitch) * Math.sin(yaw)),
y = -Math.sin(pitch) * Math.sin(horizonRoll) +
Math.cos(pitch) * Math.cos(horizonRoll) * Math.sin(yaw),
z = Math.cos(horizonRoll) * Math.cos(horizonPitch) * Math.sin(pitch) +
Math.cos(pitch) * (-Math.cos(yaw) * Math.sin(horizonPitch) +
Math.cos(horizonPitch) * Math.sin(horizonRoll) * Math.sin(yaw));
pitch = Math.asin(Math.max(Math.min(z, 1), -1));
yaw = Math.atan2(y, x);
// Calculate roll
var v = [Math.cos(orig_pitch) * (Math.sin(horizonRoll) * Math.sin(horizonPitch) * Math.cos(orig_yaw) -
Math.cos(horizonPitch) * Math.sin(orig_yaw)),
Math.cos(orig_pitch) * Math.cos(horizonRoll) * Math.cos(orig_yaw),
Math.cos(orig_pitch) * (Math.cos(horizonPitch) * Math.sin(horizonRoll) * Math.cos(orig_yaw) +
Math.sin(orig_yaw) * Math.sin(horizonPitch))],
w = [-Math.cos(pitch) * Math.sin(yaw), Math.cos(pitch) * Math.cos(yaw)];
var roll_adj = Math.acos(Math.max(Math.min((v[0]*w[0] + v[1]*w[1]) /
(Math.sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]) *
Math.sqrt(w[0]*w[0]+w[1]*w[1])), 1), -1));
if (v[2] < 0)
roll_adj = 2 * Math.PI - roll_adj;
roll += roll_adj;
}
// If no WebGL
if (!gl && (imageType == 'multires' || imageType == 'cubemap')) {
// Determine face transforms
s = fallbackImgSize / 2;
var transforms = {
f: 'translate3d(-' + (s + 2) + 'px, -' + (s + 2) + 'px, -' + s + 'px)',
b: 'translate3d(' + (s + 2) + 'px, -' + (s + 2) + 'px, ' + s + 'px) rotateX(180deg) rotateZ(180deg)',
u: 'translate3d(-' + (s + 2) + 'px, -' + s + 'px, ' + (s + 2) + 'px) rotateX(270deg)',
d: 'translate3d(-' + (s + 2) + 'px, ' + s + 'px, -' + (s + 2) + 'px) rotateX(90deg)',
l: 'translate3d(-' + s + 'px, -' + (s + 2) + 'px, ' + (s + 2) + 'px) rotateX(180deg) rotateY(90deg) rotateZ(180deg)',
r: 'translate3d(' + s + 'px, -' + (s + 2) + 'px, -' + (s + 2) + 'px) rotateY(270deg)'
};
focal = 1 / Math.tan(hfov / 2);
var zoom = focal * canvas.width / (window.devicePixelRatio || 1) / 2 + 'px';
var transform = 'perspective(' + zoom + ') translateZ(' + zoom + ') rotateX(' + pitch + 'rad) rotateY(' + yaw + 'rad) ';
// Apply face transforms
var faces = Object.keys(transforms);
for (i = 0; i < 6; i++) {
var face = world.querySelector('.pnlm-' + faces[i] + 'face').style;
face.webkitTransform = transform + transforms[faces[i]];
face.transform = transform + transforms[faces[i]];
}
return;
}
if (imageType != 'multires') {
// Calculate focal length from vertical field of view
var vfov = 2 * Math.atan(Math.tan(hfov * 0.5) / (canvas.width / canvas.height));
focal = 1 / Math.tan(vfov * 0.5);
// Pass psi, theta, roll, and focal length
gl.uniform1f(program.psi, yaw);
gl.uniform1f(program.theta, pitch);
gl.uniform1f(program.rot, roll);
gl.uniform1f(program.f, focal);
if (dynamic === true) {
// Update texture if dynamic
if (imageType == 'equirectangular') {
gl.bindTexture(gl.TEXTURE_2D, program.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
}
}
// Draw using current buffer
gl.drawArrays(gl.TRIANGLES, 0, 6);
} else {
// Create perspective matrix
var perspMatrix = makePersp(hfov, canvas.width / canvas.height, 0.1, 100.0);
// Find correct zoom level
checkZoom(hfov);
// Create rotation matrix
var matrix = identityMatrix3();
matrix = rotateMatrix(matrix, -roll, 'z');
matrix = rotateMatrix(matrix, -pitch, 'x');
matrix = rotateMatrix(matrix, yaw, 'y');
matrix = makeMatrix4(matrix);
// Set matrix uniforms
gl.uniformMatrix4fv(program.perspUniform, false, new Float32Array(transposeMatrix4(perspMatrix)));
gl.uniformMatrix4fv(program.cubeUniform, false, new Float32Array(transposeMatrix4(matrix)));
// Find current nodes
var rotPersp = rotatePersp(perspMatrix, matrix);
program.nodeCache.sort(multiresNodeSort);
if (program.nodeCache.length > 200 &&
program.nodeCache.length > program.currentNodes.length + 50) {
// Remove older nodes from cache
var removed = program.nodeCache.splice(200, program.nodeCache.length - 200);
for (var i = 0; i < removed.length; i++) {
// Explicitly delete textures
gl.deleteTexture(removed[i].texture);
}
}
program.currentNodes = [];
var sides = ['f', 'b', 'u', 'd', 'l', 'r'];
for (s = 0; s < 6; s++) {
var ntmp = new MultiresNode(vtmps[s], sides[s], 1, 0, 0, image.fullpath);
testMultiresNode(rotPersp, ntmp, pitch, yaw, hfov);
}
program.currentNodes.sort(multiresNodeRenderSort);
// Only process one tile per frame to improve responsiveness
for (i = 0; i < program.currentNodes.length; i++) {
if (!program.currentNodes[i].texture) {
setTimeout(processNextTile(program.currentNodes[i]), 0);
break;
}
}
// Draw tiles
multiresDraw();
}
if (params.returnImage !== undefined) {
return canvas.toDataURL('image/png');
}
};
/**
* Check if images are loading.
* @memberof Renderer
* @instance
* @returns {boolean} Whether or not images are loading.
*/
this.isLoading = function() {
if (gl && imageType == 'multires') {
for ( var i = 0; i < program.currentNodes.length; i++ ) {
if (!program.currentNodes[i].textureLoaded) {
return true;
}
}
}
return false;
};
/**
* Retrieve renderer's canvas.
* @memberof Renderer
* @instance
* @returns {HTMLElement} Renderer's canvas.
*/
this.getCanvas = function() {
return canvas;
};
/**
* Sorting method for multires nodes.
* @private
* @param {MultiresNode} a - First node.
* @param {MultiresNode} b - Second node.
* @returns {number} Base tiles first, then higher timestamp first.
*/
function multiresNodeSort(a, b) {
// Base tiles are always first
if (a.level == 1 && b.level != 1) {
return -1;
}
if (b. level == 1 && a.level != 1) {
return 1;
}
// Higher timestamp first
return b.timestamp - a.timestamp;
}
/**
* Sorting method for multires node rendering.
* @private
* @param {MultiresNode} a - First node.
* @param {MultiresNode} b - Second node.
* @returns {number} Lower zoom levels first, then closest to center first.
*/
function multiresNodeRenderSort(a, b) {
// Lower zoom levels first
if (a.level != b.level) {
return a.level - b.level;
}
// Lower distance from center first
return a.diff - b.diff;
}
/**
* Draws multires nodes.
* @private
*/
function multiresDraw() {
if (!program.drawInProgress) {
program.drawInProgress = true;
for ( var i = 0; i < program.currentNodes.length; i++ ) {
if (program.currentNodes[i].textureLoaded) {
//var color = program.currentNodes[i].color;
//gl.uniform4f(program.colorUniform, color[0], color[1], color[2], 1.0);
// Bind vertex buffer and pass vertices to WebGL
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertBuf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(program.currentNodes[i].vertices), gl.STATIC_DRAW);
gl.vertexAttribPointer(program.vertPosLocation, 3, gl.FLOAT, false, 0, 0);
// Prep for texture
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertTexCoordBuf);
gl.vertexAttribPointer(program.texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// Bind texture and draw tile
gl.bindTexture(gl.TEXTURE_2D, program.currentNodes[i].texture); // Bind program.currentNodes[i].texture to TEXTURE0
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
}
program.drawInProgress = false;
}
}
/**
* Creates new multires node.
* @constructor
* @private
* @param {number[]} vertices - Node's verticies.
* @param {string} side - Node's cube face.
* @param {number} level - Node's zoom level.
* @param {number} x - Node's x position.
* @param {number} y - Node's y position.
* @param {string} path - Node's path.
*/
function MultiresNode(vertices, side, level, x, y, path) {
this.vertices = vertices;
this.side = side;
this.level = level;
this.x = x;
this.y = y;
this.path = path.replace('%s',side).replace('%l',level).replace('%x',x).replace('%y',y);
}
/**
* Test if multires node is visible. If it is, add it to current nodes,
* load its texture, and load appropriate child nodes.
* @private
* @param {number[]} rotPersp - Rotated perspective matrix.
* @param {MultiresNode} node - Multires node to check.
* @param {number} pitch - Pitch to check at.
* @param {number} yaw - Yaw to check at.
* @param {number} hfov - Horizontal field of view to check at.
*/
function testMultiresNode(rotPersp, node, pitch, yaw, hfov) {
if (checkSquareInView(rotPersp, node.vertices)) {
// Calculate central angle between center of view and center of tile
var v = node.vertices;
var x = v[0] + v[3] + v[6] + v[ 9];
var y = v[1] + v[4] + v[7] + v[10];
var z = v[2] + v[5] + v[8] + v[11];
var r = Math.sqrt(x*x + y*y + z*z);
var theta = Math.asin(z / r);
var phi = Math.atan2(y, x);
var ydiff = phi - yaw;
ydiff += (ydiff > Math.PI) ? -2 * Math.PI : (ydiff < -Math.PI) ? 2 * Math.PI : 0;
ydiff = Math.abs(ydiff);
node.diff = Math.acos(Math.sin(pitch) * Math.sin(theta) + Math.cos(pitch) * Math.cos(theta) * Math.cos(ydiff));
// Add node to current nodes and load texture if needed
var inCurrent = false;
for (var k = 0; k < program.nodeCache.length; k++) {
if (program.nodeCache[k].path == node.path) {
inCurrent = true;
program.nodeCache[k].timestamp = program.nodeCacheTimestamp++;
program.nodeCache[k].diff = node.diff;
program.currentNodes.push(program.nodeCache[k]);
break;
}
}
if (!inCurrent) {
//node.color = [Math.random(), Math.random(), Math.random()];
node.timestamp = program.nodeCacheTimestamp++;
program.currentNodes.push(node);
program.nodeCache.push(node);
}
// TODO: Test error
// Create child nodes
if (node.level < program.level) {
var cubeSize = image.cubeResolution * Math.pow(2, node.level - image.maxLevel);
var numTiles = Math.ceil(cubeSize * image.invTileResolution) - 1;
var doubleTileSize = cubeSize % image.tileResolution * 2;
var lastTileSize = (cubeSize * 2) % image.tileResolution;
if (lastTileSize === 0) {
lastTileSize = image.tileResolution;
}
if (doubleTileSize === 0) {
doubleTileSize = image.tileResolution * 2;
}
var f = 0.5;
if (node.x == numTiles || node.y == numTiles) {
f = 1.0 - image.tileResolution / (image.tileResolution + lastTileSize);
}
var i = 1.0 - f;
var children = [];
var vtmp, ntmp;
var f1 = f, f2 = f, f3 = f, i1 = i, i2 = i, i3 = i;
// Handle non-symmetric tiles
if (lastTileSize < image.tileResolution) {
if (node.x == numTiles && node.y != numTiles) {
f2 = 0.5;
i2 = 0.5;
if (node.side == 'd' || node.side == 'u') {
f3 = 0.5;
i3 = 0.5;
}
} else if (node.x != numTiles && node.y == numTiles) {
f1 = 0.5;
i1 = 0.5;
if (node.side == 'l' || node.side == 'r') {
f3 = 0.5;
i3 = 0.5;
}
}
}
// Handle small tiles that have fewer than four children
if (doubleTileSize <= image.tileResolution) {
if (node.x == numTiles) {
f1 = 0;
i1 = 1;
if (node.side == 'l' || node.side == 'r') {
f3 = 0;
i3 = 1;
}
}
if (node.y == numTiles) {
f2 = 0;
i2 = 1;
if (node.side == 'd' || node.side == 'u') {
f3 = 0;
i3 = 1;
}
}
}
vtmp = [ v[0], v[1], v[2],
v[0]*f1+v[3]*i1, v[1]*f+v[4]*i, v[2]*f3+v[5]*i3,
v[0]*f1+v[6]*i1, v[1]*f2+v[7]*i2, v[2]*f3+v[8]*i3,
v[0]*f+v[9]*i, v[1]*f2+v[10]*i2, v[2]*f3+v[11]*i3
];
ntmp = new MultiresNode(vtmp, node.side, node.level + 1, node.x*2, node.y*2, image.fullpath);
children.push(ntmp);
if (!(node.x == numTiles && doubleTileSize <= image.tileResolution)) {
vtmp = [v[0]*f1+v[3]*i1, v[1]*f+v[4]*i, v[2]*f3+v[5]*i3,
v[3], v[4], v[5],
v[3]*f+v[6]*i, v[4]*f2+v[7]*i2, v[5]*f3+v[8]*i3,
v[0]*f1+v[6]*i1, v[1]*f2+v[7]*i2, v[2]*f3+v[8]*i3
];
ntmp = new MultiresNode(vtmp, node.side, node.level + 1, node.x*2+1, node.y*2, image.fullpath);
children.push(ntmp);
}
if (!(node.x == numTiles && doubleTileSize <= image.tileResolution) &&
!(node.y == numTiles && doubleTileSize <= image.tileResolution)) {
vtmp = [v[0]*f1+v[6]*i1, v[1]*f2+v[7]*i2, v[2]*f3+v[8]*i3,
v[3]*f+v[6]*i, v[4]*f2+v[7]*i2, v[5]*f3+v[8]*i3,
v[6], v[7], v[8],
v[9]*f1+v[6]*i1, v[10]*f+v[7]*i, v[11]*f3+v[8]*i3
];
ntmp = new MultiresNode(vtmp, node.side, node.level + 1, node.x*2+1, node.y*2+1, image.fullpath);
children.push(ntmp);
}
if (!(node.y == numTiles && doubleTileSize <= image.tileResolution)) {
vtmp = [ v[0]*f+v[9]*i, v[1]*f2+v[10]*i2, v[2]*f3+v[11]*i3,
v[0]*f1+v[6]*i1, v[1]*f2+v[7]*i2, v[2]*f3+v[8]*i3,
v[9]*f1+v[6]*i1, v[10]*f+v[7]*i, v[11]*f3+v[8]*i3,
v[9], v[10], v[11]
];
ntmp = new MultiresNode(vtmp, node.side, node.level + 1, node.x*2, node.y*2+1, image.fullpath);
children.push(ntmp);
}
for (var j = 0; j < children.length; j++) {
testMultiresNode(rotPersp, children[j], pitch, yaw, hfov);
}
}
}
}
/**
* Creates cube vertex array.
* @private
* @returns {number[]} Cube vertex array.
*/
function createCube() {
return [-1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, // Front face
1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, // Back face
-1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, // Up face
-1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, // Down face
-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, // Left face
1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1 // Right face
];
}
/**
* Creates 3x3 identity matrix.
* @private
* @returns {number[]} Identity matrix.
*/
function identityMatrix3() {
return [
1, 0, 0,
0, 1, 0,
0, 0, 1
];
}
/**
* Rotates a 3x3 matrix.
* @private
* @param {number[]} m - Matrix to rotate.
* @param {number[]} angle - Angle to rotate by in radians.
* @param {string} axis - Axis to rotate about (`x`, `y`, or `z`).
* @returns {number[]} Rotated matrix.
*/
function rotateMatrix(m, angle, axis) {
var s = Math.sin(angle);
var c = Math.cos(angle);
if (axis == 'x') {
return [
m[0], c*m[1] + s*m[2], c*m[2] - s*m[1],
m[3], c*m[4] + s*m[5], c*m[5] - s*m[4],
m[6], c*m[7] + s*m[8], c*m[8] - s*m[7]
];
}
if (axis == 'y') {
return [
c*m[0] - s*m[2], m[1], c*m[2] + s*m[0],
c*m[3] - s*m[5], m[4], c*m[5] + s*m[3],
c*m[6] - s*m[8], m[7], c*m[8] + s*m[6]
];
}
if (axis == 'z') {
return [
c*m[0] + s*m[1], c*m[1] - s*m[0], m[2],
c*m[3] + s*m[4], c*m[4] - s*m[3], m[5],
c*m[6] + s*m[7], c*m[7] - s*m[6], m[8]
];
}
}
/**
* Turns a 3x3 matrix into a 4x4 matrix.
* @private
* @param {number[]} m - Input matrix.
* @returns {number[]} Expanded matrix.
*/
function makeMatrix4(m) {
return [
m[0], m[1], m[2], 0,
m[3], m[4], m[5], 0,
m[6], m[7], m[8], 0,
0, 0, 0, 1
];
}
/**
* Transposes a 4x4 matrix.
* @private
* @param {number[]} m - Input matrix.
* @returns {number[]} Transposed matrix.
*/
function transposeMatrix4(m) {
return [
m[ 0], m[ 4], m[ 8], m[12],
m[ 1], m[ 5], m[ 9], m[13],
m[ 2], m[ 6], m[10], m[14],
m[ 3], m[ 7], m[11], m[15]
];
}
/**
* Creates a perspective matrix.
* @private
* @param {number} hfov - Desired horizontal field of view.
* @param {number} aspect - Desired aspect ratio.
* @param {number} znear - Near distance.
* @param {number} zfar - Far distance.
* @returns {number[]} Generated perspective matrix.
*/
function makePersp(hfov, aspect, znear, zfar) {
var fovy = 2 * Math.atan(Math.tan(hfov/2) * canvas.height / canvas.width);
var f = 1 / Math.tan(fovy/2);
return [
f/aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, (zfar+znear)/(znear-zfar), (2*zfar*znear)/(znear-zfar),
0, 0, -1, 0
];
}
/**
* Processes a loaded texture image into a WebGL texture.
* @private
* @param {Image} img - Input image.
* @param {WebGLTexture} tex - Texture to bind image to.
*/
function processLoadedTexture(img, tex) {
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
}
// Based on http://blog.tojicode.com/2012/03/javascript-memory-optimization-and.html
var loadTexture = (function() {
var cacheTop = 4; // Maximum number of concurrents loads
var textureImageCache = {};
var pendingTextureRequests = [];
function TextureImageLoader() {
var self = this;
this.texture = this.callback = null;
this.image = new Image();
this.image.crossOrigin = 'anonymous';