-
-
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
ShaderChunk to ES6 Module #14447
ShaderChunk to ES6 Module #14447
Conversation
Very nice! |
|
||
var alphatest_fragment = "#ifdef ALPHATEST\r\n\r\n\tif ( diffuseColor.a < ALPHATEST ) discard;\r\n\r\n#endif\r\n"; | ||
|
||
var aomap_fragment = "#ifdef USE_AOMAP\r\n\r\n\t// reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture\r\n\tfloat ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;\r\n\r\n\treflectedLight.indirectDiffuse *= ambientOcclusion;\r\n\r\n\t#if defined( USE_ENVMAP ) && defined( PHYSICAL )\r\n\r\n\t\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\r\n\r\n\t\treflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );\r\n\r\n\t#endif\r\n\r\n#endif\r\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the glsl() plugin is not cleaning up the code comments anymore?
output: [ | ||
{ | ||
format: 'es', | ||
file: 'src/renderers/shaders/ShaderChunk.js', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So a build-result is now part of the src
directory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah i'm confused, this file gets built, and then is used for another build? How would you describe ShaderChunk.js
now?
With this change we will have files in the src/ directory that are out of sync in the dev branch. |
Sort of... It's a compiled ES6 version of glsl files which otherwise cannot be used as modules. Alternatively, the file can sit outside src/ however, modules that need it should have import paths pointing to built version, not .glsl. I'm not sure I like |
@looeee I'm not sure what you mean by that? As far as I can tell, making any changes to glsl files requires a build step. This PR only splits the build in two steps to unblock ShaderChunk from breaking otherwise working modular architecture. As long as you continue building after changing glsl (same as before) nothing should be out of sync. However @Mugen87 brings up a valid concern, should modularized (built) version of ShaderChunk sit in src/ or build/ ? Also, I'm thinking there should be a separate npm task for watching and building glsl files alone. Are there any other suggestions to fix the issue? |
Yes. As I mentioned above, the comments in the glsl code are no loger being removed when converting to js string. Any ideas? |
This is what I mean - if I submit a PR to update or add a glsl file, once that PR is accepted the src/ files will be out of sync in the dev branch, unless I also run build and include the build files in the PR. Since build currently builds all the files, we would need at least a separate npm task for this. Conceptually, it's a little odd to have files from a build process in src/, although an argument can be made as to why this should be an exception, since the user will be using the built files in a secondary build process. But then again, it's not hard for the user to include the rollup glsl() plugin. Unless there are other build systems for which this is an issue, perhaps we could just document how to do that instead and avoid adding this entirely? |
My hope is to make threejs work without any build process whatsoever since modules and http2 have landed in most browsers. Of course - bundling, minifying, tree shaking etc. will be necessary for most projects. But increasing number of projects could benefit from using threejs in its source form. So, glsl files converted to js are still javascript source files and belong to src/ from javascript perspective. Unfortunately, since there is a "build" step to convert glsl to js, ShaderChunk.js in not a true source file. Perhaps it would be less confusing if glsl files are converted to js individually (not bundled)? Ideally, I'd like to have shader files consumable without any "build" process. As I already mentioned, there are alternative solutions such as to write shaders source in template literals (which are quite powerful and can remove comments btw). Another option is to use XHR with http2-push but I believe it requires a custom server... Anyway... I am happy with this as a temporary solution for my project. I can amend this PR if there is consensus on how to resolve the brought up issues. Otherwise, I'll be closing this PR. |
Hmm, that is a good point. On the other hand, we may possibly never reach the time where importing thousands of small files is as performant as importing ten or so larger ones, since you will still want to do minification and compression. Compression will not work as well on small files as it does on large ones, while minification will at least still require a build step of some kind. There is also an overhead associated with importing ES6 modules. If you are importing ten or twenty modules, you can probably ignore this. But if you are importing 1000 critical modules, it's going to add some significant time to your page load.
This would be less confusing, but for the reasons above is unlikely to ever be useful.
Could you give an example of such a project? Since browsers being able to import modules is very new and I'm not fully familiar with it yet, perhaps I'm just not seeing the potential applications. |
I've started exploring how to remove the
I'm under the impression, that for my needs, this introduces a lot of flexibility, and i can nuke a lot of code from Given that this is the most recent I don't think i'll ever end up in a situation where some
Should this tree shake all the other non
I think it would be very useful with some build system though. |
@pailhead this PR is about importing GLSL files as ES6 modules in the browser (or in module bundlers). No matter what system we use for materials - the current system, your system, node materials etc. we will still have GLSL code that needs to be consumed by tools that are designed for JavaScript modules - this means that discussion of alternate approaches to materials, while interesting, is not relevant here. To summarise:
Possible solutions:
|
I don't understand this, could you please elaborate? If Both your a) and b) mean this: (?)
Which makes a whole lot of sense to me.
I think you misread something.
These two comments, from yourself and @arodic seem to tackle the material systems or at least the intricacies of building them (along with the rest of three). Not just how browsers import some js files in 2018. Feel free to clarify what you want discussed in this thread, but i'm curious about the same thing you are. |
Sure, I should have said "bundlers" rather than build tools. Rollup.js is an example - you cannot pass a
Yes. |
Is a rollup plugin analogous to a webpack loader? I still don't understand the final scenario. Say we have some chunk
What are the potential uses for it? Someone who uses I couldn't find if the es6 syntax is any different but if it's the same as before:
What does this achieve, since, it's highly likely that this page will call Sounds like this is not even a consideration for this library (?):
With this pr though, it seems like something like this would be possible:
But i'm not sure if it is actually, nor if it is - how does it map to what the browser does. Either way, each one of these snippets results in a string, no more no less. All of this seems very relevant to how the materials are being used? |
Here is where my motivation for this PR comes from... It's mostly about development process, not deployment. I currently find myself in a situation where I experiment with UI framework designed around three.js while simultaneously I thinker with three.js source and examples/ From my process perspective, these components share the same source tree. This enables me to quickly explore ideas that require changes in three.js source, examples, and UI framework without any constraints or limitations. At the same time, In my development environment, I have no issues loading thousands of files via module imports in a split second. Full threejs src reload with multiple example reload is so fast it adds pretty much zero overhead to my process thus I have no need for any build tools whatsoever #nonpminstall Anyway, Another thing to consider (future tech) is HTML modules. It should give us an option to include HTML files from js modules. If glsl code lives inside HTML modules with Thanks @looeee for the summary. Should I close this PR @mrdoob? |
I think loading the shaders async could be an interesting approach. It would enalbe you to use the The Ember.js team are investigating something interesting for the templates (HTML files) in their framework. They split the HTML "code" from the JS code and serve the HTML "code" as binary data. This makes parsing very fast and helped them improve startup time a lot. More info here, and a very interesting video here I think something similar could be possible with glsl files as well. But if you load glsl files async this makes everything which relies on these files async as well and this can get complicated very quickly. I'm not sure if this is worth the whole effort but definitly an interesting idea. |
I think every single user involved in this conversation has a different use case. @arodic doesn’t seem to use npm at all. What you’re describing is totally possible, right here right now. You just have to write your own work around and three.js will still load the glsl through the built chunks thus doing it twice. Removing the chunks from the core may be the common denominator that all of us people, and all of the proposals share. SO sees the async loading and CORS questions daily. Imagine if |
I stumbled across this issue after going through the following issue: #6241
I don't think we should bloat three.js only because some hobby developers have problem setting up a server correctly. We could still provide It's off topic but I think if we do not focus on file size, we will lose ground against upcoming and other engines. In my opinion it's good to focus on ease of use but there should also be an option for professional users. And it makes sense to bake this into the framework and not let every user implement her or his own workaround. |
Ok, since there is no clear path forward for this PR, I'll close it and update the issue with relevant information from this discussion. |
Added another step to rollup.config.js to make ShaderChunk.js compatible with ES6 modules.
Fixes #14446