-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureManager.js
More file actions
114 lines (99 loc) · 3.32 KB
/
Copy pathTextureManager.js
File metadata and controls
114 lines (99 loc) · 3.32 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
/**
*
*/
"use strict";
function setupTextures(forceReload){
addTextures(forceReload);
}
//var skyWorking = false;
function addTextures(forceReload){
// skyWorking = true;
// console.log("[addTextures]");
// fullZoom
if (fov >=50){
if (fovInRange() && !forceReload){
// skyWorking = false;
return;
}
var sky;
for (var j=0; j<pwgl.selectedSkies.length && j<8;j++){
sky = pwgl.selectedSkies[j];
// console.log(sky);
if (sky.textures.needsRefresh){
sky.textures.images[0] = gl.createTexture();
loadImageForTexture(sky.baseURL+"/Norder3/Allsky.jpg", sky.textures.images[0], j);
sky.textures.needsRefresh = false;
}
}
pwgl.loadedTextures.splice(0, pwgl.loadedTextures.length);
}else if (mouseDown || !fovInRange() || forceReload){
// var toBeLoadedTextures = [];
if (mouseDown){
updateVisiblePixels(false);
setupBuffers();
}
for (var k=0; k<pwgl.selectedSkies.length && k<8;k++){
sky = pwgl.selectedSkies[k];
if (sky.textures.needsRefresh){
for (var d=0;d<sky.textures.images.length;d++){
gl.deleteTexture(sky.textures.images[d]);
}
sky.textures.images.splice(0, sky.textures.images.length);
sky.textures.needsRefresh = false;
}
if (!fovInRange()){
for (var d=0;d<sky.textures.images.length;d++){
gl.deleteTexture(sky.textures.images[d]);
}
sky.textures.images.splice(0, sky.textures.images.length);
sky.textures.cache.splice(0, sky.textures.cache.length);
}
for (var n=0; n<pwgl.pixels.length;n++){
var texCacheIdx = pwgl.pixelsCache.indexOf(pwgl.pixels[n]);
if (texCacheIdx !== -1 ){
// console.log("FROM CACHE");
sky.textures.images[n] = gl.createTexture();
sky.textures.images[n] = sky.textures.cache[texCacheIdx];
}else{
// console.log("NEW TEXTURE");
sky.textures.images[n] = gl.createTexture();
var dirNumber = Math.floor(pwgl.pixels[n] / 10000) * 10000;
loadImageForTexture(sky.baseURL+"/Norder"+norder+"/Dir"+dirNumber+"/Npix"+pwgl.pixels[n]+".jpg", sky.textures.images[n], k);
}
}
sky.textures.cache = sky.textures.images.slice();
}
}
// skyWorking = false;
}
function loadImageForTexture(url, texture, texunit){
var image = new Image();
image.onload = function(){
textureFinishedLoading(image, texture, texunit);
}
image.crossOrigin = "anonymous";
image.src=url;
}
function textureFinishedLoading(image, texture, texunit){
console.log("TEXUNIT "+texunit);
gl.activeTexture(gl.TEXTURE0+texunit);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
if (fov>=50){
// it's not a power of 2. Turn of mips and set wrapping to clamp to edge
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.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}else{
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
}
gl.uniform1i(pwgl.uniformSamplerLoc[texunit], texunit);
if (!gl.isTexture(texture)){
console.log("error in texture");
}
gl.bindTexture(gl.TEXTURE_2D, null);
// skyWorking = false;
}