-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (56 loc) · 1.86 KB
/
index.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
var _ = require("lodash");
var Q = require("q");
function filterUniforms (uniforms) {
return _.omit(uniforms, function (u) { return typeof u !== "string"; });
}
// loadImage : (path) => Promise of Texture or Texture
// loadImage can refuse a path (e.g. for validation) by returning a Failure Promise / or throwing an exception
function UniformsTextureResolver (loadImage) {
this.loadImage = loadImage;
this.lazyLoadingImages = {};
this.lazyLoadedImages = {};
}
UniformsTextureResolver.prototype = {
loadTexture: function (path) {
var self = this;
if (this.lazyLoadingImages[path]) return this.lazyLoadingImages[path];
var maybeImage = Q.fcall(function () {
return self.loadImage(path);
});
this.lazyLoadingImages[path] = maybeImage;
maybeImage.then(function (img) {
self.lazyLoadedImages[path] = img;
}, function (e) {
console.log("Cannot load '"+path+"' :"+e);
self.lazyLoadedImages[path] = null;
}).done();
return maybeImage;
},
resolve: function (uniforms) {
var self = this;
var all = _.clone(uniforms);
var textureUniforms = filterUniforms(all);
return Q.all(_.map(textureUniforms, function (path, key) {
return self.loadTexture(path).then(function (img) {
return [ key, img ];
}, function () {
return [ key, null ];
});
})).then(function (pairsOfTextures) {
return _.extend(all, _.object(pairsOfTextures));
});
},
getTextureOrNull: function (path) {
this.loadTexture(path);
if (path in this.lazyLoadedImages)
return this.lazyLoadedImages[path];
else
return null;
},
resolveSync: function (uniforms) {
var all = _.clone(uniforms);
var textureUniforms = filterUniforms(all);
return _.extend(all, _.mapValues(textureUniforms, this.getTextureOrNull, this));
}
};
module.exports = UniformsTextureResolver;