Skip to content

Commit

Permalink
texture caching (fixes aframevr#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngokevin committed Mar 9, 2016
1 parent 52c1e6f commit 69cf2fa
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 114 deletions.
37 changes: 20 additions & 17 deletions src/shaders/standard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var registerShader = require('../core/shader').registerShader;
var srcLoader = require('../utils/src-loader');
var THREE = require('../lib/three');
var utils = require('../utils/texture');
var utils = require('../utils/');

var CubeLoader = new THREE.CubeTextureLoader();
var texturePromises = {};
Expand Down Expand Up @@ -67,12 +67,12 @@ module.exports.Component = registerShader('standard', {
// Texture added or changed.
this.textureSrc = src;
srcLoader.validateSrc(src,
utils.loadImage.bind(this, material, data),
utils.loadVideo.bind(this, material, data)
utils.texture.loadImage.bind(this, material, data),
utils.texture.loadVideo.bind(this, material, data)
);
} else {
// Texture removed.
utils.updateMaterial(material, null);
utils.texture.updateMaterial(material, null);
}
},

Expand All @@ -97,33 +97,36 @@ module.exports.Component = registerShader('standard', {
var self = this;
var material = this.material;
var envMap = data.envMap;
// Environment cubemaps.

// No envMap defined or already loading.
if (!envMap || this.isLoadingEnvMap) {
material.envMap = null;
material.needsUpdate = true;
return;
}
this.isLoadingEnvMap = true;

// Another material is already loading this texture. Wait on promise.
if (texturePromises[envMap]) {
// Another material is already loading this texture. Wait on promise.
texturePromises[envMap].then(function (cube) {
self.isLoadingEnvMap = false;
material.envMap = cube;
material.needsUpdate = true;
});
} else {
// Material is first to load this texture. Load and resolve texture.
texturePromises[envMap] = new Promise(function (resolve) {
srcLoader.validateCubemapSrc(envMap, function loadEnvMap (urls) {
CubeLoader.load(urls, function (cube) {
// Texture loaded.
self.isLoadingEnvMap = false;
material.envMap = cube;
resolve(cube);
});
return;
}

// Material is first to load this texture. Load and resolve texture.
texturePromises[envMap] = new Promise(function (resolve) {
srcLoader.validateCubemapSrc(envMap, function loadEnvMap (urls) {
CubeLoader.load(urls, function (cube) {
// Texture loaded.
self.isLoadingEnvMap = false;
material.envMap = cube;
resolve(cube);
});
});
}
});
}
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var objectAssign = require('object-assign');

module.exports.coordinates = require('./coordinates');
module.exports.debug = require('./debug');
module.exports.texture = require('./texture');

/**
* Fires a custom DOM event.
Expand Down
Loading

0 comments on commit 69cf2fa

Please sign in to comment.