How to load external shader in javascript?

冷暖自知 提交于 2019-12-24 09:09:50

问题


I want to load the fragment shader from an external file, but it dont works. (after the shader is loaded, the alertbox don't appear.)

var fs = document.createElement('script');
fs.setAttribute("type","x-shader/x-fragment");
fs.setAttribute("src", "shader.fs");

fs.onload = function() {alert('done');}
document.getElementsByTagName("head")[0].appendChild(fs);

回答1:


Storing a shader in a script tag is simply a convention that several WebGL tutorials picked up to give the shader a simple place to "live". It doesn't have to be put in a script tag to work.

XHR is probably the easiest way to pull one down.

var shaderXhr = new XMLHttpRequest();
shaderXhr.open("GET", "shader.fs", true);
shaderXhr.onload = function() {
    yourShaderParsingRoutine(this.responseText);
};
shaderXhr.send(null);


来源:https://stackoverflow.com/questions/8475570/how-to-load-external-shader-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!