Jump to content
  • Advertisement
Sign in to follow this  
Yours3!f

load external text file (shader) in javascript (WebGL)

This topic is 2588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

this will be exotic :)
I'm trying to load an external text file from javascript, so that I can load it as a shader later. The file is loaded successfully, however when I try to access its contents I can't.
I checked with firebug if it is loaded properly, and it was. When I try to access its content by using this:

function load_external_script(filename, type)
{
var script = document.createElement('script');
script.type = type;
script.src = filename;
script.setAttribute('id', filename);
head.appendChild(script);
}

function getShader(gl, id)
{
var shaderScript = document.getElementById(id);
if (!shaderScript)
{
return null;
}

var str = shaderScript.text;
...
}

load_external_script('vertex.vs', 'text/plain');
var fragmentShader = getShader(gl, "vertex.vs");


the str = ... line triggers an unexpected EOF error, and I checked: both text and innerHTML properties are empty despite the content is there... any ideas how can I get it?

or is there another way of loading shaders from external source?

I took the basic source from http://learningwebgl.com/blog/?p=28

here's the uploaded file:
link

Best regards,
Yours3!f

Share this post


Link to post
Share on other sites
Advertisement
Hi,

this will be exotic :)
I'm trying to load an external text file from javascript, so that I can load it as a shader later. The file is loaded successfully, however when I try to access its contents I can't.
I checked with firebug if it is loaded properly, and it was. When I try to access its content by using this:

function load_external_script(filename, type)
{
var script = document.createElement('script');
script.type = type;
script.src = filename;
script.setAttribute('id', filename);
head.appendChild(script);
}

function getShader(gl, id)
{
var shaderScript = document.getElementById(id);
if (!shaderScript)
{
return null;
}

var str = shaderScript.text;
...
}

load_external_script('vertex.vs', 'text/plain');
var fragmentShader = getShader(gl, "vertex.vs");


the str = ... line triggers an unexpected EOF error, and I checked: both text and innerHTML properties are empty despite the content is there... any ideas how can I get it?

or is there another way of loading shaders from external source?

I took the basic source from http://learningwebgl.com/blog/?p=28

here's the uploaded file:
link

Best regards,
Yours3!f


Easy: use AJAX! :)

XMLHttpRequest.responseText will give you direct access to the source of a shader. (Alternatively you can use the nicer JQuery or Prototype equivalents).

Problem is you'll want to process the response asynchronously, which may complicate your initialization a little.

Share this post


Link to post
Share on other sites

Easy: use AJAX! :)

XMLHttpRequest.responseText will give you direct access to the source of a shader. (Alternatively you can use the nicer JQuery or Prototype equivalents).

Problem is you'll want to process the response asynchronously, which may complicate your initialization a little.
[/quote]

thanks for the response,

isn't ajax a microsoft technology (meaning proprietary, only works in IE or windows)?

I've tried XMLHttpRequest.responseText but it failed to load it? Might it be because of the asynchronous loading?
umm so should I wait until they're loaded?
something like:

var request = new XMLHttpRequest();
(... sending the request)
var shader_text = "";
while(shader_text == "")
{
shader_text = XMLHttpRequest.reseponseText;
}


What is JQuery and Prototype :D ? (i suppose they're javascript frameworks, right?)
(I've just got into these client side technologies so I'm kind of a noob in this field :) )

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!