[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] WebGLSL Media Type Proposal



On Mon, May 21, 2012 at 5:36 PM, Jeff Gilbert <jgilbert@mozilla.com> wrote:
 However, for my simple ping-pong framebuffer-texture implementation of Conway's Life, if I had a file for each shader, it would need an extra four files, each of which contain only a handful of lines.
There's a couple solutions to that too:

The first one is a typical loader:
var loadFiles = function(files, ondone){
  var loaded = 0;
  var count = 0;
  var result = {};

  var doLoad = function(key, path){
    var request = new XMLHttpRequest();
    request.open('GET', file, true);
    request.>
      result[key] = request.response;
      loaded += 1;
      if(count == loaded) ondone(result);
    }
    request.send();
  }

  for(var key in files){
    var path = files[key];
    count += 1;
    doLoad(key, path);
  }
}

And you can use it like:
loadFiles({
  foo: 'foo.shader',
  bar: 'bar.shader',
  test: 'test.shader',
  another: 'whatever.shader'
}, function(files){
  ctx.shaderSource(shader, files.foo);
});

The other is a bit cleaner in my opinion, which is:
- Some script to pack all your shaders in a JSON, for instance in python
import os, json
result = {}
for path, dirs, names in os.walk('.'):
  for name in names:
    result[os.path.join(path, name)] = open(os.path.join(path, name)).read()
open('shaders.json', 'w').write(json.encode(result))

And then in JS do:
var request = new XMLHttpRequest();
  request.open('GET', 'sometext.txt', true);
  request.>
    var shaders = JSON.parse(request.response);
    gl.shaderSource(shader, shaders['whatever.shader']);
  }
  request.send();

 
(Not to mention the requirement of a local http server, which most definitely negatively impacts the pick-up-and-hack capabilities of WebGL)
Afaik, XHRs work locally either if you toggle them on (in chrome) or if you request only from below your html files path (firefox).