-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Three.js source does not work as ES6 module #14446
Comments
I already tested this approach and it works.
This should work but having shader chunks in both glsl and js form might confuse some developers.
Problem with this one is that glsl template literals don't have syntax highlighting by default so developers would need plugins for their code editors. |
Also, the glsl rollup plugin does some minor string clean up (removes comments). |
@mrdoob yeah I'm not sure why it didn't remove comments in my PR but it appears there were other issues. It appeared that most people don't like partial builds in src/ directory. That leaves solution 3. and 4. only candidates for now. Also, keep in mind that template literals can in fact strip out comments (they are actually functions). Moreover, comment stripping can still be done during build process. |
To be clear, this was not so much a suggestion as an explanation of the current situation. I don't currently have a strong opinion as to what the best approach is here. |
I just pushed solution no.3 (template literals) to shaderchunk-template branch to illustrate what I meant. I don't expect this solution to be accepted. This works as ES6 modules and does not require build step whatsoever but has several issues:
|
@arodic Did you take look at three-full ? All shaders are put in the same folder and are es6 modules. In that way you should be able to import them in the way you want. Waiting that #9562 be close. |
@arodic @mrdoob Just created a straightforward PR that could help with syntax highlighting of glsl templates literals: #15473 (more infos in PR) // Advantage here is that there would be no need for a glsl tag function, just a comment
+ var encodings_fragment = glsl`
- var encodings_fragment = /* glsl */`
gl_FragColor = linearToOutputTexel( gl_FragColor );
`;
export {encodings_fragment}; |
Description of the problem
Three.js source cannot be imported as ES6 module. Developers should be able to use Three.js in pure ES6 module form.
For example, the following should work:
Instead, we get errors like this one:
This happens because glsl shader chunks are not real modules and need to be processed during build step by rollup glsl plugin.
Fixing this would improve development experience and make it possible to use threejs without build step or even without any npm dependencies at all.
Motivation
ES6 modules are a great improvement to JavaScript. Together with HTTP/2 network protocol which eliminates multiple HTTP request overhead they enable seamless development experience without continuous build process. Building, bundling and minification should most definitely still be part of the deployment strategy, but for development process, it is not necessary. Here is an interesting article about ES6 modules by contentful.
This issue is about enabling threejs to work without any build process and without
npm install
during development.Suggested solutions
Rollup ShadersChunkSrc.js into ShaderChunk.js separately before building the library. This solution was attempted in PR ShaderChunk to ES6 Module #14447 but it did not reach consensus. Main issue was that this approach results with a partial "build" inside src/ directory.Convert each individual glsl shader chunk into ES6 module (automatically). This approach would suffer from the same issue as no.1 but some prefer it better.
Rewrite and maintain shader chunks as ES6 modules using template literals. For example:
Three.js version
Browser
OS
The text was updated successfully, but these errors were encountered: