Moving to a modular architecture
Browserify Moving to this architecture has advantages and disadvantages. Please add your thoughts.
Note: this does not require three.js consumers to use browserify.
One advantage is that this would enforce a modular architecture for the ongoing development of three.js.
The common style in node/browserify has each file declare its dependencies at the top, and considers global variables an anti-pattern.
Here is an example snippet:
// src/geometry/BoxGeometry.js
var Geometry = require('./Geometry.js');
var Vector3 = require('../core/Vector3.js');
module.exports = BoxGeometry;
function BoxGeometry() {
// ...
}
BoxGeometry.prototype = Geometry.prototype;
Another advantage is that consumers of three.js using browserify would be able to pick and choose the parts they want. They could just import Scene, BoxGeometry, PerspectiveCamera, and WebGLRenderer, get the dependencies for all those automatically ( Object3D etc ), and have a small bundle of javascript that supports just the feature set they want.
This could be done in a way that imposes no breaking changes. At the top level, we would export all classes we consider to be part of the standard package
// src/three.js
var THREE = { rev: 101 }
module.exports = THREE
THREE.Geometry = require('./geometry/Geometry.js')
THREE.BoxGeometry = require('./geometry/BoxGeometry.js')
// ...
note: I'm not exactly requiring the dependencies at the top in this example, because this file would almost exclusively be require statements.
Finally we would wrap that in a Universal Module Definition that detects if a module system (node/browserify, AMD) is in use, and if so exports it, or otherwise appends it to the global object ( window ).
Lets review:
- enforces good modular style
- allows
three.jsconsumers using browserify to pick and choose functionality - no breaking changes
This would require replacing the build system, but the new one would be pretty straight forward.
Some other advantages:
- You can structure your code
- You can create / reuse modules without polluting the global namespace
- You can build for production
- You can debug more easily since every module has it's own file you don't need to search for the corresponding module in a large three.js file
@shi-314 I guess I'm a little confused, I feel like You can structure your code and You can build for production are things that you can do without the architectural shift? Are you talking about three.js source or things built using three.js?
One practice that three.js uses that makes it tricky to use in commonjs environments is the use of instanceof: https://github.com/mrdoob/three.js/blob/master/src/core/Geometry.js#L82
This is because in an application you often end up with different versions of the same library in your source tree, so checking instanceof doesn't work between different versions of the same library. It would be good in preparation for a move to a commonjs module system to replace those instanceof checks with feature-checking behind a Geometry.isGeometry(geom) style interface.
@kumavis I am talking about things built in three.js. Let's say you want to create your own material with your shaders etc. At the moment you need to extend the global THREE object to stay consistent with the rest of the three.js code:
THREE.MeshMyCoolMaterial = function (...) { ... }
But if we had Browserify than you could do:
var MeshLambertMaterial = require('./../MeshLambertMaterial');
var MeshMyCoolMaterial = function (...) {...}
So your namespace stays consistent and you don't need to use THREE.MeshLambertMaterial and MeshMyCoolMaterial in your code.
And with You can build for production I basicly meant the same thing you mentioned: allows three.js consumers using browserify to pick and choose functionality.
@shi-314 thank you, that is more clear. That does impact my proposed general solution to deserialization consumer-defined classes:
// given that `data` is a hash of a serialized object
var ObjectClass = THREE[ data.type ]
new ObjectClass.fromJSON( data )
This is from my proposed serialization / deserialization refactor https://github.com/mrdoob/three.js/pull/4621
Performance shouldn't be affected by a change like this.
This is a pretty huge change but I'm also in favour of it.
Some other major advantages:
- You can use browserify's
standaloneoption to generate a UMD build for you. No need to manually tinker with UMD wrappers. - The package can easily be consumed by users of browserify/NPM
- Pulling in dependencies for threejs (like poly2tri, color-string, etc) becomes much easier
- Modules that "don't really belong" in a rendering library (like vector/math libraries) can be pulled out as separate NPM modules and re-used for many other types of projects. One major benefit of this is that the individual modules have their own repository for bugs/issues, PRs, etc (cleaning up ThreeJS issues).
- NPM will handle semantic versioning for us. e.g. we can push a breaking change in the
threejs-vecmathwithout worrying about everyone's code breaking. And on the flip side, if we make a patch or minor release in a particular module, people consuming those modules will be able to get the changes automatically. - It makes "extras" like EffectComposer and various shaders easy to package and consume (imagine
npm install threejs-shader-bloom) - As modules are pulled out, the final distribution size will start to get smaller and more application-specific. There will eventually be no need for different "builds types" since we will just
require()the modules that our app is actually using.
To @mrdoob and the other authors; if you don't have much experience with NPM/Browserify I would suggest making a couple little projects with it and getting a feel for its "philosophy." It's very different from ThreeJS architecture; rather than big frameworks it encourages lots of small things.
Another advantage of this approach is that there can be an ecosystem of open source, third party Three.JS modules, especially shaders, geometries, model loaders etc. Published through NPM or Github/Component which people can then easily just reference and use. At the moment stuff is shared by hosting a demo which people then 'view source' on. Three.JS deserves better!
I think one of the problems I have with Three.JS is how quickly code becomes incompatible with the current version of Three.JS. Another advantage of switching to something like this is being able to specify specific versions of bits of Three.JS would be very powerful and handy.
+1
+1 for a CommonJS/browserify architecture, it would make the core more lightweight and extensions would fit even if they come from third-parties
Fragmenting three.js into little modules has a lot of costs as well. The current system allows pretty simple third party addons (witness eg. jetienne's THREEx modules). There's a lot to be said about the simplicity of the current setup, as long as the JS module systems are just wrappers around build systems.
Another way of minimizing build size is what ClojureScript does. They follow some conventions to allow Google's Closure compiler to do whole-program analysis and dead code elimination.
+1 for the unappreciated, and often overlooked, elegance of simplicity
+1
Fragmenting three.js into little modules has a lot of costs as well. The current system allows pretty simple third party addons (witness eg. jetienne's THREEx modules).
The idea here is that a UMD build would still be provided for non-Node environments. Plugins like THREEx would work the same way for those depending on ThreeJS with simple <script> tags.
The tricky thing will be: how do we require() a particular plugin if we are in a CommonJS environment? Maybe browserify-shim could help.
There's a lot to be said about the simplicity of the current setup, as long as the JS module systems are just wrappers around build systems.
ThreeJS's current plugin/extension system is pretty awful to work with, and far from "simple" or easy. Most ThreeJS projects tend to use some form of plugin or extension, like EffectComposer, or FirstPersonControls, or a model loader, or one of the other many JS files floating around in the examples folder. Right now the only way to depend on these plugins:
- Download the current build of ThreeJS
- Copy-paste the necessary files into your
vendorfolder - Wire up gulp/grunt tasks to concat and minify all the plugins you need; making sure to concat them in the correct order otherwise things will break. Manually maintain this list as you add more plugins.
- Repeat steps 1 and 2 every time ThreeJS is updated; and then pull your hair out when you realize the new code is not backward-compatible
Now, imagine, with browserify you could do something like this:
var FirstPersonControls = require('threejs-controls').FirstPersonControls;
//more granular, only requiring necessary files
var FirstPersonControls = require('threejs-controls/lib/FirstPersonControls');
Those plugins will require('threejs') and anything else that they may need (like GLSL snippets or text triangulation). The dependency/version management is all hidden to the user, and there is no need for manually maintained grunt/gulp concat tasks.
The tricky thing will be: how do we require() a particular plugin if we are in a CommonJS environment?
I've been using CommonJS for THREE.js projects for a bit now. It's a bit of a manual process, converting chunks of other people's code into modules and I don't think there'll be an easy way to avoid that for legacy code that isn't converted by the authors or contributors.
The important bit is that there's a module exporting the entire 'standard' THREE object, which can then be required by anything that wishes to extend it.
var THREE = require('three');
THREE.EffectComposer = // ... etc, remembering to include copyright notices :)
This has worked pretty well for me, especially as the project grows and I start adding my own shaders and geometries into their own modules etc.
As long as there's a 'threejs-full' or 'threejs-classic' npm package then this becomes a pretty viable way of working with old Three.js stuff in a CommonJS environment but I suspect this is pretty niche!
+1 I believe once fragmented threejs modules are available in npm, plugin developers will love to migrate to CommonJS env. On Jun 5, 2014 9:19 PM, "Charlotte Gore" [email protected] wrote:
The tricky thing will be: how do we require() a particular plugin if we are in a CommonJS environment?
I've been using CommonJS for THREE.js projects for a bit now. It's a bit of a manual process, converting chunks of other people's code into modules and I don't think there'll be an easy way to avoid that for legacy code that isn't converted by the authors or contributors.
The important bit is that there's a module exporting the entire 'standard' THREE object, which can then be required by anything that wishes to extend it.
var THREE = require('three'); THREE.EffectComposer = // ... etc, remembering to include copyright notices :)
This has worked pretty well for me, especially as the project grows and I start adding my own shaders and geometries into their own modules etc.
As long as there's a 'threejs-full' or 'threejs-classic' npm package then this becomes a pretty viable way of working with old Three.js stuff in a CommonJS environment but I suspect this is pretty niche!
— Reply to this email directly or view it on GitHub https://github.com/mrdoob/three.js/issues/4776#issuecomment-45236911.
It could also make the shaders made modular as well, e.g. using glslify. Even things like making an Express middleware that generates shaders on demand becomes easier then.
Some months ago I moved frame.js to require.js and I finally understood how this AMD stuff works.
I still need to learn, however, how to "compile" this. What's the tool/workflow for generating a three.min.js out of a list of modules?
I prefer gulp.js as a build system with the gulp-browserify plugin. It's really easy to understand and the code looks cleaner than grunt in my opinion. Check this out: http://travismaynard.com/writing/no-need-to-grunt-take-a-gulp-of-fresh-air :wink: