-
-
Save humbletim/7069a894697370d02c00 to your computer and use it in GitHub Desktop.
THREE.js r85 cndjs hifi loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dynamically load and patch three.js... | |
// -humbletim | |
// load three.js source code from cdnjs.com | |
var src = ( | |
function(xhr){ | |
xhr.open("GET", 'https://cdn.rawgit.com/mrdoob/three.js/edc89403b47dd976590ada1d0656a97c46ba77f3/build/three.js', false); | |
xhr.send(); | |
return xhr.responseText; | |
} | |
)(new XMLHttpRequest); | |
// monkey-patch it | |
[ | |
[ /var THREE =/, "THREE ="], // export a global for Script.include | |
[ /[.]delete\b/g, "['delete']"], // delete is a reserved keyword | |
[ /\b(boolean|char)\b/g, ' $$$1 '], // boolean/char are reserved words | |
// this changes [sg]et x() {... -> [sg]et$x: function() { ... | |
[ /^(\s*[sg]et)\s+(\w+)\s*\(/mg, '$1$$$2: function(' ] | |
].forEach( | |
function(rep) { | |
//print('monkey-patch:', rep); | |
src = src.replace(rep[0],rep[1]); | |
}); | |
self=this; // note: THREE references 'self' somewhere | |
Object.freeze = function(ob) { return ob; }; | |
Script.evaluate(src, '#patched-three.js'); | |
THREE = Script.THREE; | |
THREE.REVISION += ' [monkey-patched]'; | |
// rework P.get$x as function() { ... } | |
// into Object.defineProperty(P,'x',{ get: function() {... }}) | |
Object.keys(THREE) | |
.forEach( | |
function(k) { | |
var v = THREE[k]; | |
var proto = v&&v.prototype; | |
if (proto) { | |
Object.keys(proto) | |
.filter(function(gs){return /^[sg]et[$]/.test(gs)}) | |
.forEach( | |
function(gs) { | |
var tmp = gs.split('$'); | |
//print("patching", k, tmp) | |
var cfg = { configurable: true }; | |
cfg[tmp[0]] = proto[gs]; | |
Object.defineProperty(proto, tmp[1], cfg); | |
}); | |
} | |
} | |
); | |
print("THREE.REVISION == " + THREE.REVISION); | |
// sanity check for getters/setters | |
THREE.$$test = function test() { | |
print("new THREE.Quaternion(1,2,3,4).w == ", new THREE.Quaternion(1,2,3,4).w); | |
var q = new THREE.Quaternion(1,2,3,4); | |
q.w = Math.PI; | |
print("new THREE.Quaternion(1,2,3,4).w = PI; toArray == ", q.toArray()); | |
}; | |
//THREE.$$test(); | |
try { module.exports = THREE; } catch(e) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment