question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading external shader file for Webgl

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
arpucommented, Dec 8, 2017

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’; `

2reactions
gaearoncommented, Feb 10, 2017

If you need it as a string, could you just write a JS file exporting a string?

export default `
attribute vec4 a_position;
attribute vec2 a_texcoord;
// ...
`;

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 think raw-loader might work for your use case).

I hope this helps!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found