Loading external shader file for Webgl
See original GitHub issueHi,
A part of my app use Webgl (with shaders).
I would like to load my shader files in my bundle (instead of write shaders as string inside the JS files).
So I wrote an a file named shader.vert
which contains
attribute vec4 a_position;
attribute vec2 a_texcoord;
uniform mat4 u_matrix;
varying vec2 v_texcoord;
void main() {
gl_Position = u_matrix * a_position;
v_texcoord = a_texcoord;
}
Then I try to load it in an other file
import vert from './shader.vert';
console.log(vert);
but I get this output instead of the original content of my file:
data:application/octet-stream;base64,YXR0cmlidXRlIHZlYzQgYV9wb3NpdGlvbjsKYX…A9IHVfbWF0cml4ICogYV9wb3NpdGlvbjsKICB2X3RleGNvb3JkID0gYV90ZXhjb29yZDsKfQo=
I’m not a very confortable with Webpack (and that’s why I love create-react-app). I mean I know that webpack use many “loaders” etc but I don’t really know how they work.
Should I eject and add custom loaders ? (all that I DONT want to do)
Or is there a way to import file as a simple string ?
With .vert
, .frag
, .glsl
or other extension ?
node@6.2.2
npm@3.9.5
react-scripts@0.8.5
Thanks
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
WebGL - is there an alternative to embedding shaders in HTML?
Now the javascript file loading code works locally since http: is used, rather than file:. Just thought I'd put this up here in...
Read more >Getting Your Shaders Into Your WebGL Code
The “right” answer is to put each shader into a separate file, and then load these files from JavaScript. This isn't hard to...
Read more >load external text file (shader) in javascript (WebGL)
I'm trying to load an external text file from javascript, so that I can load it as a shader later. The file is...
Read more >Using textures in WebGL - Web APIs | MDN
To load the texture from the image file, it then creates an Image object and assigns the src to the URL for our...
Read more >WebGL Shaders and GLSL
As mentioned in how it works WebGL requires 2 shaders every time you draw something. A vertex shader and a fragment shader. Each...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://github.com/facebookincubator/create-react-app/issues/1944 this should wor disable the eslint ` /* eslint import/no-webpack-loader-syntax: off */ import * as fragmentShader from ‘!raw-loader!glslify-loader!../shaders/fragment.glsl’;
/* eslint import/no-webpack-loader-syntax: off */ import * as vertexShader from ‘!raw-loader!glslify-loader!../shaders/vertex.glsl’; `
If you need it as a string, could you just write a JS file exporting a string?
In CRA’s setup, when you import an unrecognized file, you’ll get its URL (which may be a data URL when it’s small enough and inlined). This is useful for media files, but not useful for shaders.
Alternatively, you can
npm run eject
and then use any custom Webpack loaders (I thinkraw-loader
might work for your use case).I hope this helps!