Skip to content

Instantly share code, notes, and snippets.

@indefinit
Created July 22, 2014 02:45
Show Gist options
  • Save indefinit/88dadd770657644c9c82 to your computer and use it in GitHub Desktop.
Save indefinit/88dadd770657644c9c82 to your computer and use it in GitHub Desktop.
Functional shader loader. Requires JQuery
var shadersHolder = { vertex: [], fragment: [] };
/**
* Fancy schmancy functional loading of Shaders
* @param {Array} shaders Object array of shaders
* @return {Jquery.Deferred}
*/
function loadShaders(shaders) {
return $.when.apply($, $.map(shaders, function(shader) {
var def = new $.Deferred();
$.ajax({url:shader.path, dataType: 'text', complete: function(resp){
(shader.type == 'vertex') ? shadersHolder.vertex.push(resp.responseText) : shadersHolder.fragment.push(resp.responseText);
def.resolve();
}});
return def;
})).promise();
}
// First we load our shaders and then init
// the scene
// init defined elsewhere
loadShaders([{path: './shaders/vertex.js', type:'vertex'}, {path: './shaders/frag.js', type:'fragment'}]).then(function(){
init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment