From 336dbfeebfe492fd9cb8d3730c3e8f60a028a13b Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 10:41:09 +0200 Subject: [PATCH 1/9] Renamed global *IdCount variables to lowercase. --- src/core/BufferGeometry.js | 2 +- src/core/DirectGeometry.js | 2 +- src/core/Geometry.js | 4 ++-- src/core/Object3D.js | 4 ++-- src/materials/Material.js | 4 ++-- src/textures/Texture.js | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/BufferGeometry.js b/src/core/BufferGeometry.js index 741780bc062260..9e51eba31c9c92 100644 --- a/src/core/BufferGeometry.js +++ b/src/core/BufferGeometry.js @@ -5,7 +5,7 @@ THREE.BufferGeometry = function () { - Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.geometryIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); diff --git a/src/core/DirectGeometry.js b/src/core/DirectGeometry.js index a50f1a9267bd9a..0b2d54972cb6fe 100644 --- a/src/core/DirectGeometry.js +++ b/src/core/DirectGeometry.js @@ -4,7 +4,7 @@ THREE.DirectGeometry = function () { - Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.geometryIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); diff --git a/src/core/Geometry.js b/src/core/Geometry.js index b36656ab4b1ed2..3c4b7495f85e73 100644 --- a/src/core/Geometry.js +++ b/src/core/Geometry.js @@ -9,7 +9,7 @@ THREE.Geometry = function () { - Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.geometryIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); @@ -1202,4 +1202,4 @@ Object.assign( THREE.Geometry.prototype, THREE.EventDispatcher.prototype, { } ); -THREE.GeometryIdCount = 0; +THREE.geometryIdCount = 0; diff --git a/src/core/Object3D.js b/src/core/Object3D.js index 40fac1246a25f9..091bffc0ba49ad 100644 --- a/src/core/Object3D.js +++ b/src/core/Object3D.js @@ -8,7 +8,7 @@ THREE.Object3D = function () { - Object.defineProperty( this, 'id', { value: THREE.Object3DIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.object3DIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); @@ -715,4 +715,4 @@ Object.assign( THREE.Object3D.prototype, THREE.EventDispatcher.prototype, { } ); -THREE.Object3DIdCount = 0; +THREE.object3DIdCount = 0; diff --git a/src/materials/Material.js b/src/materials/Material.js index 067202c57f916b..549b582e4e3d0f 100644 --- a/src/materials/Material.js +++ b/src/materials/Material.js @@ -5,7 +5,7 @@ THREE.Material = function () { - Object.defineProperty( this, 'id', { value: THREE.MaterialIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.materialIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); @@ -321,4 +321,4 @@ THREE.Material.prototype = { Object.assign( THREE.Material.prototype, THREE.EventDispatcher.prototype ); -THREE.MaterialIdCount = 0; +THREE.materialIdCount = 0; diff --git a/src/textures/Texture.js b/src/textures/Texture.js index 99645043c4abd6..e577c1fe685fcc 100644 --- a/src/textures/Texture.js +++ b/src/textures/Texture.js @@ -6,7 +6,7 @@ THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) { - Object.defineProperty( this, 'id', { value: THREE.TextureIdCount ++ } ); + Object.defineProperty( this, 'id', { value: THREE.texturelIdCount ++ } ); this.uuid = THREE.Math.generateUUID(); @@ -276,4 +276,4 @@ THREE.Texture.prototype = { Object.assign( THREE.Texture.prototype, THREE.EventDispatcher.prototype ); -THREE.TextureIdCount = 0; +THREE.texturelIdCount = 0; From bfbfb62ac2e968d19662e1cee6b3410f869980e6 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 11:17:55 +0200 Subject: [PATCH 2/9] NPM build: Added SPDX compliant license string. - Without it 'npm install' issues a warning message. --- utils/build/package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/build/package.json b/utils/build/package.json index b465eb34e4d2d6..2e1354262129cf 100644 --- a/utils/build/package.json +++ b/utils/build/package.json @@ -17,6 +17,9 @@ "type" : "git", "url" : "git://github.com/mrdoob/three.js.git" }, + + "license": "MIT", + "licenses": [{ "type": "The MIT License", "url": "https://raw.github.com/mrdoob/three.js/master/LICENSE" From 3b00e2b82cc808feb9c3083bc08b8b49b8fb2633 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 11:32:57 +0200 Subject: [PATCH 3/9] Animation: Removed unused local variables. --- src/animation/AnimationAction.js | 4 ---- src/animation/AnimationObjectGroup.js | 1 - 2 files changed, 5 deletions(-) diff --git a/src/animation/AnimationAction.js b/src/animation/AnimationAction.js index 036328c68ed79e..c0c1e5492517cf 100644 --- a/src/animation/AnimationAction.js +++ b/src/animation/AnimationAction.js @@ -119,8 +119,6 @@ THREE.AnimationAction._new.prototype = { isRunning: function() { - var start = this._startTime; - return this.enabled && ! this.paused && this.timeScale !== 0 && this._startTime === null && this._mixer._isActiveAction( this ); @@ -187,8 +185,6 @@ THREE.AnimationAction._new.prototype = { crossFadeFrom: function( fadeOutAction, duration, warp ) { - var mixer = this._mixer; - fadeOutAction.fadeOut( duration ); this.fadeIn( duration ); diff --git a/src/animation/AnimationObjectGroup.js b/src/animation/AnimationObjectGroup.js index 815aee2b4e62c6..6f9df11dcd4d67 100644 --- a/src/animation/AnimationObjectGroup.js +++ b/src/animation/AnimationObjectGroup.js @@ -164,7 +164,6 @@ THREE.AnimationObjectGroup.prototype = { remove: function( var_args ) { var objects = this._objects, - nObjects = objects.length, nCachedObjects = this.nCachedObjects_, indicesByUUID = this._indicesByUUID, bindings = this._bindings, From 0676c43bcc619dc6247509d36ceae660d0b2a706 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 11:38:06 +0200 Subject: [PATCH 4/9] Renderer: Free temporary arrays on 'dispose'. --- src/renderers/WebGLRenderer.js | 1 + src/renderers/webgl/WebGLUniforms.js | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 3a83428950fd97..373300f7a4d8e6 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -527,6 +527,7 @@ THREE.WebGLRenderer = function ( parameters ) { this.dispose = function() { + THREE.WebGLUniforms.purgeCache(); _canvas.removeEventListener( 'webglcontextlost', onContextLost, false ); }; diff --git a/src/renderers/webgl/WebGLUniforms.js b/src/renderers/webgl/WebGLUniforms.js index 0917cb18fe5dac..9d2bf8cddd17ed 100644 --- a/src/renderers/webgl/WebGLUniforms.js +++ b/src/renderers/webgl/WebGLUniforms.js @@ -71,13 +71,6 @@ THREE.WebGLUniforms = ( function() { // scope arrayCacheF32 = [], arrayCacheI32 = [], - uncacheTemporaryArrays = function() { - - arrayCacheF32.length = 0; - arrayCacheI32.length = 0; - - }, - // Flattening for arrays of vectors and matrices flatten = function( array, nBlocks, blockSize ) { @@ -596,6 +589,13 @@ THREE.WebGLUniforms = ( function() { // scope }; + WebGLUniforms.purgeCache = function() { + + arrayCacheF32.length = 0; + arrayCacheI32.length = 0; + + }; + return WebGLUniforms; } )(); From c5bfefbc12793547061802699f49ffcecc9439fb Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 12:13:55 +0200 Subject: [PATCH 5/9] NPM build: Add build.js invocation to package.json. --- utils/build/build.js | 7 +++++++ utils/build/package.json | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/utils/build/build.js b/utils/build/build.js index 9974610636aed7..2dbb033145aaaa 100644 --- a/utils/build/build.js +++ b/utils/build/build.js @@ -4,6 +4,13 @@ var argparse = require( "argparse" ); var uglify = require("uglify-js"); var spawn = require('child_process').spawn; +// This script can be invoked via: +// +// npm run build +// npm run build.min +// +// See 'package.json' for the command lines. + function main() { "use strict"; diff --git a/utils/build/package.json b/utils/build/package.json index 2e1354262129cf..a0c452676921e9 100644 --- a/utils/build/package.json +++ b/utils/build/package.json @@ -12,7 +12,12 @@ "uglify-js": "^2.4.17", "argparse" : "*" }, - + + "scripts": { + "build": "node build.js --include common --include extras --output ../../build/three.js", + "build.min": "node build.js --include common --include extras --minify --output ../../build/three.min.js" + }, + "repository" : { "type" : "git", "url" : "git://github.com/mrdoob/three.js.git" From de6180e4d9633811c823eb5664439110e1a6ae84 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 12:52:52 +0200 Subject: [PATCH 6/9] Added "constifier" build step. - Replaces THREE and WebGL symbolic constants with numerals. - Reduces the build size by (currently) 8KB / 2KB gzipped. - Improves the performance of the renderer's JS code. --- utils/build/build.js | 11 + utils/build/build.py | 40 +- utils/build/constants.json | 430 +++++++++++++++++++ utils/build/constifier/__init__.py | 54 +++ utils/build/constifier/constify.js | 66 +++ utils/build/constifier/constify.js.test.html | 58 +++ utils/build/constifier/dumper.html | 78 ++++ 7 files changed, 730 insertions(+), 7 deletions(-) create mode 100644 utils/build/constants.json create mode 100644 utils/build/constifier/__init__.py create mode 100644 utils/build/constifier/constify.js create mode 100644 utils/build/constifier/constify.js.test.html create mode 100644 utils/build/constifier/dumper.html diff --git a/utils/build/build.js b/utils/build/build.js index 2dbb033145aaaa..e64f30a8841eb4 100644 --- a/utils/build/build.js +++ b/utils/build/build.js @@ -2,6 +2,7 @@ var fs = require("fs"); var path = require("path"); var argparse = require( "argparse" ); var uglify = require("uglify-js"); +var constify = require("./constifier/constify"); var spawn = require('child_process').spawn; // This script can be invoked via: @@ -89,6 +90,16 @@ function main() { var LICENSE = "threejs.org/license"; + // Constant substitution + + var constants = JSON.parse( fs.readFileSync( './constants.json', 'utf8' ) ); + + sources.forEach( function( source ) { + + source.contents = constify( source.contents, constants ); + + } ); + // Parsing var toplevel = null; diff --git a/utils/build/build.py b/utils/build/build.py index 3c680cb85a9861..8bf0c02ac09226 100644 --- a/utils/build/build.py +++ b/utils/build/build.py @@ -14,11 +14,34 @@ import shutil import tempfile +from constifier import constify + from io import open +def readText(path): + + with open(path,'r',encoding='utf-8') as f: + result=f.read() + f.close() + return result + +def writeText(path,contents): + + with open(path,'w',encoding='utf-8') as f: + f.write(contents) + f.close() + +def readJSON(path): + + with open(path,'r',encoding='utf-8') as f: + result=json.load(f) + f.close() + return result + + def main(argv=None): - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser() parser.add_argument('--include', action='append', required=True) parser.add_argument('--externs', action='append', default=['externs/common.js']) parser.add_argument('--amd', action='store_true', default=False) @@ -51,8 +74,9 @@ def main(argv=None): tmp.write(u'( function ( root, factory ) {\n\n\tif ( typeof define === \'function\' && define.amd ) {\n\n\t\tdefine( [ \'exports\' ], factory );\n\n\t} else if ( typeof exports === \'object\' ) {\n\n\t\tfactory( exports );\n\n\t} else {\n\n\t\tfactory( root );\n\n\t}\n\n}( this, function ( exports ) {\n\n') for include in args.include: - with open('includes/' + include + '.json','r', encoding='utf-8') as f: - files = json.load(f) + + files = readJSON('includes/' + include + '.json') + for filename in files: tmp.write(u'// File:' + filename) tmp.write(u'\n\n') @@ -85,9 +109,11 @@ def main(argv=None): else: backup = '' if os.path.exists(output): - with open(output, 'r', encoding='utf-8') as f: backup = f.read() + backup=readText(output) os.remove(output) + writeText( path, constify( readText(path), readJSON('constants.json') ) ) + externs = ' --externs '.join(args.externs) source = ' '.join(sources) cmd = 'java -jar compiler/compiler.jar --warning_level=VERBOSE --jscomp_off=globalThis --jscomp_off=checkTypes --externs %s --language_in=ECMASCRIPT5_STRICT --js %s --js_output_file %s %s' % (externs, path, output, sourcemapargs) @@ -96,11 +122,11 @@ def main(argv=None): # header if os.path.exists(output): - with open(output, 'r', encoding='utf-8') as f: text = f.read() - with open(output, 'w', encoding='utf-8') as f: f.write('// threejs.org/license\n' + text + sourcemapping) + text=readText(output) + writeText(output, '// threejs.org/license\n' + text + sourcemapping) else: + writeText(output, backup) print("Minification with Closure compiler failed. Check your Java runtime version.") - with open(output, 'w', encoding='utf-8') as f: f.write(backup) os.close(fd) os.remove(path) diff --git a/utils/build/constants.json b/utils/build/constants.json new file mode 100644 index 00000000000000..98ac8494826b96 --- /dev/null +++ b/utils/build/constants.json @@ -0,0 +1,430 @@ + [ + { + "contextRegExpString": "^THREE$", + "constants": { + "CullFaceNone": 0, + "CullFaceBack": 1, + "CullFaceFront": 2, + "CullFaceFrontBack": 3, + "FrontFaceDirectionCW": 0, + "FrontFaceDirectionCCW": 1, + "BasicShadowMap": 0, + "PCFShadowMap": 1, + "PCFSoftShadowMap": 2, + "FrontSide": 0, + "BackSide": 1, + "DoubleSide": 2, + "FlatShading": 1, + "SmoothShading": 2, + "NoColors": 0, + "FaceColors": 1, + "VertexColors": 2, + "NoBlending": 0, + "NormalBlending": 1, + "AdditiveBlending": 2, + "SubtractiveBlending": 3, + "MultiplyBlending": 4, + "CustomBlending": 5, + "AddEquation": 100, + "SubtractEquation": 101, + "ReverseSubtractEquation": 102, + "MinEquation": 103, + "MaxEquation": 104, + "ZeroFactor": 200, + "OneFactor": 201, + "SrcColorFactor": 202, + "OneMinusSrcColorFactor": 203, + "SrcAlphaFactor": 204, + "OneMinusSrcAlphaFactor": 205, + "DstAlphaFactor": 206, + "OneMinusDstAlphaFactor": 207, + "DstColorFactor": 208, + "OneMinusDstColorFactor": 209, + "SrcAlphaSaturateFactor": 210, + "NeverDepth": 0, + "AlwaysDepth": 1, + "LessDepth": 2, + "LessEqualDepth": 3, + "EqualDepth": 4, + "GreaterEqualDepth": 5, + "GreaterDepth": 6, + "NotEqualDepth": 7, + "MultiplyOperation": 0, + "MixOperation": 1, + "AddOperation": 2, + "NoToneMapping": 0, + "LinearToneMapping": 1, + "ReinhardToneMapping": 2, + "Uncharted2ToneMapping": 3, + "CineonToneMapping": 4, + "UVMapping": 300, + "CubeReflectionMapping": 301, + "CubeRefractionMapping": 302, + "EquirectangularReflectionMapping": 303, + "EquirectangularRefractionMapping": 304, + "SphericalReflectionMapping": 305, + "CubeUVReflectionMapping": 306, + "CubeUVRefractionMapping": 307, + "RepeatWrapping": 1000, + "ClampToEdgeWrapping": 1001, + "MirroredRepeatWrapping": 1002, + "NearestFilter": 1003, + "NearestMipMapNearestFilter": 1004, + "NearestMipMapLinearFilter": 1005, + "LinearFilter": 1006, + "LinearMipMapNearestFilter": 1007, + "LinearMipMapLinearFilter": 1008, + "UnsignedByteType": 1009, + "ByteType": 1010, + "ShortType": 1011, + "UnsignedShortType": 1012, + "IntType": 1013, + "UnsignedIntType": 1014, + "FloatType": 1015, + "HalfFloatType": 1025, + "UnsignedShort4444Type": 1016, + "UnsignedShort5551Type": 1017, + "UnsignedShort565Type": 1018, + "AlphaFormat": 1019, + "RGBFormat": 1020, + "RGBAFormat": 1021, + "LuminanceFormat": 1022, + "LuminanceAlphaFormat": 1023, + "DepthFormat": 1026, + "RGB_S3TC_DXT1_Format": 2001, + "RGBA_S3TC_DXT1_Format": 2002, + "RGBA_S3TC_DXT3_Format": 2003, + "RGBA_S3TC_DXT5_Format": 2004, + "RGB_PVRTC_4BPPV1_Format": 2100, + "RGB_PVRTC_2BPPV1_Format": 2101, + "RGBA_PVRTC_4BPPV1_Format": 2102, + "RGBA_PVRTC_2BPPV1_Format": 2103, + "RGB_ETC1_Format": 2151, + "LoopOnce": 2200, + "LoopRepeat": 2201, + "LoopPingPong": 2202, + "InterpolateDiscrete": 2300, + "InterpolateLinear": 2301, + "InterpolateSmooth": 2302, + "ZeroCurvatureEnding": 2400, + "ZeroSlopeEnding": 2401, + "WrapAroundEnding": 2402, + "TrianglesDrawMode": 0, + "TriangleStripDrawMode": 1, + "TriangleFanDrawMode": 2, + "LinearEncoding": 3000, + "GammaEncoding": 3007, + "RGBEEncoding": 3002, + "LogLuvEncoding": 3003, + "RGBM7Encoding": 3004, + "RGBM16Encoding": 3005, + "RGBDEncoding": 3006, + "BasicDepthPacking": 3200, + "RGBADepthPacking": 3201, + "LineStrip": 0, + "LinePieces": 1 + } + }, + { + "contextRegExpString": "^_?gl$", + "constants": { + "DEPTH_BUFFER_BIT": 256, + "STENCIL_BUFFER_BIT": 1024, + "COLOR_BUFFER_BIT": 16384, + "POINTS": 0, + "LINES": 1, + "LINE_LOOP": 2, + "LINE_STRIP": 3, + "TRIANGLES": 4, + "TRIANGLE_STRIP": 5, + "TRIANGLE_FAN": 6, + "ZERO": 0, + "ONE": 1, + "SRC_COLOR": 768, + "ONE_MINUS_SRC_COLOR": 769, + "SRC_ALPHA": 770, + "ONE_MINUS_SRC_ALPHA": 771, + "DST_ALPHA": 772, + "ONE_MINUS_DST_ALPHA": 773, + "DST_COLOR": 774, + "ONE_MINUS_DST_COLOR": 775, + "SRC_ALPHA_SATURATE": 776, + "FUNC_ADD": 32774, + "BLEND_EQUATION": 32777, + "BLEND_EQUATION_RGB": 32777, + "BLEND_EQUATION_ALPHA": 34877, + "FUNC_SUBTRACT": 32778, + "FUNC_REVERSE_SUBTRACT": 32779, + "BLEND_DST_RGB": 32968, + "BLEND_SRC_RGB": 32969, + "BLEND_DST_ALPHA": 32970, + "BLEND_SRC_ALPHA": 32971, + "CONSTANT_COLOR": 32769, + "ONE_MINUS_CONSTANT_COLOR": 32770, + "CONSTANT_ALPHA": 32771, + "ONE_MINUS_CONSTANT_ALPHA": 32772, + "BLEND_COLOR": 32773, + "ARRAY_BUFFER": 34962, + "ELEMENT_ARRAY_BUFFER": 34963, + "ARRAY_BUFFER_BINDING": 34964, + "ELEMENT_ARRAY_BUFFER_BINDING": 34965, + "STREAM_DRAW": 35040, + "STATIC_DRAW": 35044, + "DYNAMIC_DRAW": 35048, + "BUFFER_SIZE": 34660, + "BUFFER_USAGE": 34661, + "CURRENT_VERTEX_ATTRIB": 34342, + "FRONT": 1028, + "BACK": 1029, + "FRONT_AND_BACK": 1032, + "CULL_FACE": 2884, + "BLEND": 3042, + "DITHER": 3024, + "STENCIL_TEST": 2960, + "DEPTH_TEST": 2929, + "SCISSOR_TEST": 3089, + "POLYGON_OFFSET_FILL": 32823, + "SAMPLE_ALPHA_TO_COVERAGE": 32926, + "SAMPLE_COVERAGE": 32928, + "NO_ERROR": 0, + "INVALID_ENUM": 1280, + "INVALID_VALUE": 1281, + "INVALID_OPERATION": 1282, + "OUT_OF_MEMORY": 1285, + "CW": 2304, + "CCW": 2305, + "LINE_WIDTH": 2849, + "ALIASED_POINT_SIZE_RANGE": 33901, + "ALIASED_LINE_WIDTH_RANGE": 33902, + "CULL_FACE_MODE": 2885, + "FRONT_FACE": 2886, + "DEPTH_RANGE": 2928, + "DEPTH_WRITEMASK": 2930, + "DEPTH_CLEAR_VALUE": 2931, + "DEPTH_FUNC": 2932, + "STENCIL_CLEAR_VALUE": 2961, + "STENCIL_FUNC": 2962, + "STENCIL_FAIL": 2964, + "STENCIL_PASS_DEPTH_FAIL": 2965, + "STENCIL_PASS_DEPTH_PASS": 2966, + "STENCIL_REF": 2967, + "STENCIL_VALUE_MASK": 2963, + "STENCIL_WRITEMASK": 2968, + "STENCIL_BACK_FUNC": 34816, + "STENCIL_BACK_FAIL": 34817, + "STENCIL_BACK_PASS_DEPTH_FAIL": 34818, + "STENCIL_BACK_PASS_DEPTH_PASS": 34819, + "STENCIL_BACK_REF": 36003, + "STENCIL_BACK_VALUE_MASK": 36004, + "STENCIL_BACK_WRITEMASK": 36005, + "VIEWPORT": 2978, + "SCISSOR_BOX": 3088, + "COLOR_CLEAR_VALUE": 3106, + "COLOR_WRITEMASK": 3107, + "UNPACK_ALIGNMENT": 3317, + "PACK_ALIGNMENT": 3333, + "MAX_TEXTURE_SIZE": 3379, + "MAX_VIEWPORT_DIMS": 3386, + "SUBPIXEL_BITS": 3408, + "RED_BITS": 3410, + "GREEN_BITS": 3411, + "BLUE_BITS": 3412, + "ALPHA_BITS": 3413, + "DEPTH_BITS": 3414, + "STENCIL_BITS": 3415, + "POLYGON_OFFSET_UNITS": 10752, + "POLYGON_OFFSET_FACTOR": 32824, + "TEXTURE_BINDING_2D": 32873, + "SAMPLE_BUFFERS": 32936, + "SAMPLES": 32937, + "SAMPLE_COVERAGE_VALUE": 32938, + "SAMPLE_COVERAGE_INVERT": 32939, + "COMPRESSED_TEXTURE_FORMATS": 34467, + "DONT_CARE": 4352, + "FASTEST": 4353, + "NICEST": 4354, + "GENERATE_MIPMAP_HINT": 33170, + "BYTE": 5120, + "UNSIGNED_BYTE": 5121, + "SHORT": 5122, + "UNSIGNED_SHORT": 5123, + "INT": 5124, + "UNSIGNED_INT": 5125, + "FLOAT": 5126, + "DEPTH_COMPONENT": 6402, + "ALPHA": 6406, + "RGB": 6407, + "RGBA": 6408, + "LUMINANCE": 6409, + "LUMINANCE_ALPHA": 6410, + "UNSIGNED_SHORT_4_4_4_4": 32819, + "UNSIGNED_SHORT_5_5_5_1": 32820, + "UNSIGNED_SHORT_5_6_5": 33635, + "FRAGMENT_SHADER": 35632, + "VERTEX_SHADER": 35633, + "MAX_VERTEX_ATTRIBS": 34921, + "MAX_VERTEX_UNIFORM_VECTORS": 36347, + "MAX_VARYING_VECTORS": 36348, + "MAX_COMBINED_TEXTURE_IMAGE_UNITS": 35661, + "MAX_VERTEX_TEXTURE_IMAGE_UNITS": 35660, + "MAX_TEXTURE_IMAGE_UNITS": 34930, + "MAX_FRAGMENT_UNIFORM_VECTORS": 36349, + "SHADER_TYPE": 35663, + "DELETE_STATUS": 35712, + "LINK_STATUS": 35714, + "VALIDATE_STATUS": 35715, + "ATTACHED_SHADERS": 35717, + "ACTIVE_UNIFORMS": 35718, + "ACTIVE_ATTRIBUTES": 35721, + "SHADING_LANGUAGE_VERSION": 35724, + "CURRENT_PROGRAM": 35725, + "NEVER": 512, + "LESS": 513, + "EQUAL": 514, + "LEQUAL": 515, + "GREATER": 516, + "NOTEQUAL": 517, + "GEQUAL": 518, + "ALWAYS": 519, + "KEEP": 7680, + "REPLACE": 7681, + "INCR": 7682, + "DECR": 7683, + "INVERT": 5386, + "INCR_WRAP": 34055, + "DECR_WRAP": 34056, + "VENDOR": 7936, + "RENDERER": 7937, + "VERSION": 7938, + "NEAREST": 9728, + "LINEAR": 9729, + "NEAREST_MIPMAP_NEAREST": 9984, + "LINEAR_MIPMAP_NEAREST": 9985, + "NEAREST_MIPMAP_LINEAR": 9986, + "LINEAR_MIPMAP_LINEAR": 9987, + "TEXTURE_MAG_FILTER": 10240, + "TEXTURE_MIN_FILTER": 10241, + "TEXTURE_WRAP_S": 10242, + "TEXTURE_WRAP_T": 10243, + "TEXTURE_2D": 3553, + "TEXTURE": 5890, + "TEXTURE_CUBE_MAP": 34067, + "TEXTURE_BINDING_CUBE_MAP": 34068, + "TEXTURE_CUBE_MAP_POSITIVE_X": 34069, + "TEXTURE_CUBE_MAP_NEGATIVE_X": 34070, + "TEXTURE_CUBE_MAP_POSITIVE_Y": 34071, + "TEXTURE_CUBE_MAP_NEGATIVE_Y": 34072, + "TEXTURE_CUBE_MAP_POSITIVE_Z": 34073, + "TEXTURE_CUBE_MAP_NEGATIVE_Z": 34074, + "MAX_CUBE_MAP_TEXTURE_SIZE": 34076, + "TEXTURE0": 33984, + "TEXTURE1": 33985, + "TEXTURE2": 33986, + "TEXTURE3": 33987, + "TEXTURE4": 33988, + "TEXTURE5": 33989, + "TEXTURE6": 33990, + "TEXTURE7": 33991, + "TEXTURE8": 33992, + "TEXTURE9": 33993, + "TEXTURE10": 33994, + "TEXTURE11": 33995, + "TEXTURE12": 33996, + "TEXTURE13": 33997, + "TEXTURE14": 33998, + "TEXTURE15": 33999, + "TEXTURE16": 34000, + "TEXTURE17": 34001, + "TEXTURE18": 34002, + "TEXTURE19": 34003, + "TEXTURE20": 34004, + "TEXTURE21": 34005, + "TEXTURE22": 34006, + "TEXTURE23": 34007, + "TEXTURE24": 34008, + "TEXTURE25": 34009, + "TEXTURE26": 34010, + "TEXTURE27": 34011, + "TEXTURE28": 34012, + "TEXTURE29": 34013, + "TEXTURE30": 34014, + "TEXTURE31": 34015, + "ACTIVE_TEXTURE": 34016, + "REPEAT": 10497, + "CLAMP_TO_EDGE": 33071, + "MIRRORED_REPEAT": 33648, + "FLOAT_VEC2": 35664, + "FLOAT_VEC3": 35665, + "FLOAT_VEC4": 35666, + "INT_VEC2": 35667, + "INT_VEC3": 35668, + "INT_VEC4": 35669, + "BOOL": 35670, + "BOOL_VEC2": 35671, + "BOOL_VEC3": 35672, + "BOOL_VEC4": 35673, + "FLOAT_MAT2": 35674, + "FLOAT_MAT3": 35675, + "FLOAT_MAT4": 35676, + "SAMPLER_2D": 35678, + "SAMPLER_CUBE": 35680, + "VERTEX_ATTRIB_ARRAY_ENABLED": 34338, + "VERTEX_ATTRIB_ARRAY_SIZE": 34339, + "VERTEX_ATTRIB_ARRAY_STRIDE": 34340, + "VERTEX_ATTRIB_ARRAY_TYPE": 34341, + "VERTEX_ATTRIB_ARRAY_NORMALIZED": 34922, + "VERTEX_ATTRIB_ARRAY_POINTER": 34373, + "VERTEX_ATTRIB_ARRAY_BUFFER_BINDING": 34975, + "IMPLEMENTATION_COLOR_READ_TYPE": 35738, + "IMPLEMENTATION_COLOR_READ_FORMAT": 35739, + "COMPILE_STATUS": 35713, + "LOW_FLOAT": 36336, + "MEDIUM_FLOAT": 36337, + "HIGH_FLOAT": 36338, + "LOW_INT": 36339, + "MEDIUM_INT": 36340, + "HIGH_INT": 36341, + "FRAMEBUFFER": 36160, + "RENDERBUFFER": 36161, + "RGBA4": 32854, + "RGB5_A1": 32855, + "RGB565": 36194, + "DEPTH_COMPONENT16": 33189, + "STENCIL_INDEX": 6401, + "STENCIL_INDEX8": 36168, + "DEPTH_STENCIL": 34041, + "RENDERBUFFER_WIDTH": 36162, + "RENDERBUFFER_HEIGHT": 36163, + "RENDERBUFFER_INTERNAL_FORMAT": 36164, + "RENDERBUFFER_RED_SIZE": 36176, + "RENDERBUFFER_GREEN_SIZE": 36177, + "RENDERBUFFER_BLUE_SIZE": 36178, + "RENDERBUFFER_ALPHA_SIZE": 36179, + "RENDERBUFFER_DEPTH_SIZE": 36180, + "RENDERBUFFER_STENCIL_SIZE": 36181, + "FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE": 36048, + "FRAMEBUFFER_ATTACHMENT_OBJECT_NAME": 36049, + "FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL": 36050, + "FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE": 36051, + "COLOR_ATTACHMENT0": 36064, + "DEPTH_ATTACHMENT": 36096, + "STENCIL_ATTACHMENT": 36128, + "DEPTH_STENCIL_ATTACHMENT": 33306, + "NONE": 0, + "FRAMEBUFFER_COMPLETE": 36053, + "FRAMEBUFFER_INCOMPLETE_ATTACHMENT": 36054, + "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT": 36055, + "FRAMEBUFFER_INCOMPLETE_DIMENSIONS": 36057, + "FRAMEBUFFER_UNSUPPORTED": 36061, + "FRAMEBUFFER_BINDING": 36006, + "RENDERBUFFER_BINDING": 36007, + "MAX_RENDERBUFFER_SIZE": 34024, + "INVALID_FRAMEBUFFER_OPERATION": 1286, + "UNPACK_FLIP_Y_WEBGL": 37440, + "UNPACK_PREMULTIPLY_ALPHA_WEBGL": 37441, + "CONTEXT_LOST_WEBGL": 37442, + "UNPACK_COLORSPACE_CONVERSION_WEBGL": 37443, + "BROWSER_DEFAULT_WEBGL": 37444 + } + } +] diff --git a/utils/build/constifier/__init__.py b/utils/build/constifier/__init__.py new file mode 100644 index 00000000000000..e642439a57f5f8 --- /dev/null +++ b/utils/build/constifier/__init__.py @@ -0,0 +1,54 @@ +import re + +def constify( sourceCode, spec, debug = False ): + + for ctxSpec in spec: + if ctxSpec.get('contextRegExp') is not None: + break + + ctxSpec['contextRegExp'] = re.compile( + ctxSpec.get( 'contextRegExpString', None ) ) + + def debugOutput( match ): + + start, end = match.start( 1 ), match.end() + + lineStart = sourceCode.rfind( '\n', 0, start ) + 1 + lineEnd = sourceCode.find( '\n', end ) + if lineEnd == -1: lineEnd = len( sourceCode ) + + line = sourceCode[ lineStart : lineEnd ] + prefix = line[ : start - lineStart ] + + line = line.expandtabs( 4 ) + prefix = prefix.expandtabs( 4 ) + + print line + print ' ' * len( prefix ) + '^' + + def replaceConstant( match ): + before, context, identifier = match.groups() + + for ctxSpec in spec: + + if not ctxSpec['contextRegExp'].match( context ): continue + + value = ctxSpec['constants'].get( identifier ) + + if value is not None: + if debug: debugOutput(match) + return before + unicode( value ) + + break + + return match.group( 0 ) + + return re.sub( + r'(^|\W)(\w+)\s*\.\s*([A-Z]\w+)(?!\s*=[^=])', + # matches . + # where 'Id' must refer to an uppercase identifier. + # The construct must not be followed by '=', but may + # be followed by '=='. + + replaceConstant, sourceCode ) + diff --git a/utils/build/constifier/constify.js b/utils/build/constifier/constify.js new file mode 100644 index 00000000000000..dba71700c2c337 --- /dev/null +++ b/utils/build/constifier/constify.js @@ -0,0 +1,66 @@ +module.exports = function constify( sourceCode, spec, debug ) { + + for ( var i = 0, n = spec.length; i !== n; ++ i ) { + + var ctxSpec = spec[ i ]; + if ( ctxSpec.contextRegExp !== undefined ) break; + + ctxSpec.contextRegExp = new RegExp( ctxSpec.contextRegExpString ); + + } + + return sourceCode.replace( + + /(^|\W)(\w+)\s*\.\s*([A-Z]\w+)(?!\s*=[^=])/g, + // matches . + // where 'Id' must refer to an uppercase identifier. + // The construct must not be followed by '=', but may + // be followed by '=='. + + function( match, before, context, identifier, offset ) { + + for ( var i = 0, n = spec.length; i !== n; ++ i ) { + + var ctxSpec = spec[ i ]; + if ( ! ctxSpec.contextRegExp.test( context ) ) continue; + + var value = ctxSpec.constants[ identifier ]; + + if ( value != undefined ) { + + if ( debug === true ) { + + var start = offset + before.length, + end = offset + match.length, + + lineStart = + sourceCode.lastIndexOf( '\n', start ) + 1, + lineEnd = sourceCode.indexOf( '\n', end ); + + if ( lineEnd === -1 ) lineEnd = sourceCode.length; + + var line = sourceCode.slice( lineStart, lineEnd ), + prefix = line.slice( 0, start - lineStart ); + + line = line.replace( /\t/g, ' ' ); + prefix = prefix.replace( /\t/g, ' ' ); + + console.log( line ); + console.log( ' '.repeat( prefix.length ) + '^\n' ); + + } + + return before + value; + + } + + break; + + } + + return match; // don't replace + + } ); + +}; + diff --git a/utils/build/constifier/constify.js.test.html b/utils/build/constifier/constify.js.test.html new file mode 100644 index 00000000000000..4e9f6d84f1c354 --- /dev/null +++ b/utils/build/constifier/constify.js.test.html @@ -0,0 +1,58 @@ + + + + + constify.js standalone test + + + + + + + + + + + + + + diff --git a/utils/build/constifier/dumper.html b/utils/build/constifier/dumper.html new file mode 100644 index 00000000000000..b06658b6b93966 --- /dev/null +++ b/utils/build/constifier/dumper.html @@ -0,0 +1,78 @@ + + + + + THREE / WebGL constant dump + + + + + + + + + + + + + From 066af7fac0cdeb169337fa42ad738b8fea47cf13 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 20:45:16 +0200 Subject: [PATCH 7/9] Automated extraction of THREE constants. --- .../three.constspec.json | 256 +++++++-------- utils/build/build.js | 28 +- utils/build/build.py | 18 +- utils/build/constifier/constify.js.test.html | 9 +- utils/build/constifier/dumper.html | 78 ----- utils/build/constifier/extractConstants.js | 24 ++ utils/build/constifier/generateThreeSpec.js | 59 ++++ utils/build/constifier/webgl_constants.html | 36 +++ utils/build/constifier/webgl_constants.json | 299 ++++++++++++++++++ 9 files changed, 583 insertions(+), 224 deletions(-) rename utils/build/constants.json => build/three.constspec.json (99%) delete mode 100644 utils/build/constifier/dumper.html create mode 100644 utils/build/constifier/extractConstants.js create mode 100644 utils/build/constifier/generateThreeSpec.js create mode 100644 utils/build/constifier/webgl_constants.html create mode 100644 utils/build/constifier/webgl_constants.json diff --git a/utils/build/constants.json b/build/three.constspec.json similarity index 99% rename from utils/build/constants.json rename to build/three.constspec.json index 98ac8494826b96..54dfaf43b0062e 100644 --- a/utils/build/constants.json +++ b/build/three.constspec.json @@ -1,130 +1,4 @@ - [ - { - "contextRegExpString": "^THREE$", - "constants": { - "CullFaceNone": 0, - "CullFaceBack": 1, - "CullFaceFront": 2, - "CullFaceFrontBack": 3, - "FrontFaceDirectionCW": 0, - "FrontFaceDirectionCCW": 1, - "BasicShadowMap": 0, - "PCFShadowMap": 1, - "PCFSoftShadowMap": 2, - "FrontSide": 0, - "BackSide": 1, - "DoubleSide": 2, - "FlatShading": 1, - "SmoothShading": 2, - "NoColors": 0, - "FaceColors": 1, - "VertexColors": 2, - "NoBlending": 0, - "NormalBlending": 1, - "AdditiveBlending": 2, - "SubtractiveBlending": 3, - "MultiplyBlending": 4, - "CustomBlending": 5, - "AddEquation": 100, - "SubtractEquation": 101, - "ReverseSubtractEquation": 102, - "MinEquation": 103, - "MaxEquation": 104, - "ZeroFactor": 200, - "OneFactor": 201, - "SrcColorFactor": 202, - "OneMinusSrcColorFactor": 203, - "SrcAlphaFactor": 204, - "OneMinusSrcAlphaFactor": 205, - "DstAlphaFactor": 206, - "OneMinusDstAlphaFactor": 207, - "DstColorFactor": 208, - "OneMinusDstColorFactor": 209, - "SrcAlphaSaturateFactor": 210, - "NeverDepth": 0, - "AlwaysDepth": 1, - "LessDepth": 2, - "LessEqualDepth": 3, - "EqualDepth": 4, - "GreaterEqualDepth": 5, - "GreaterDepth": 6, - "NotEqualDepth": 7, - "MultiplyOperation": 0, - "MixOperation": 1, - "AddOperation": 2, - "NoToneMapping": 0, - "LinearToneMapping": 1, - "ReinhardToneMapping": 2, - "Uncharted2ToneMapping": 3, - "CineonToneMapping": 4, - "UVMapping": 300, - "CubeReflectionMapping": 301, - "CubeRefractionMapping": 302, - "EquirectangularReflectionMapping": 303, - "EquirectangularRefractionMapping": 304, - "SphericalReflectionMapping": 305, - "CubeUVReflectionMapping": 306, - "CubeUVRefractionMapping": 307, - "RepeatWrapping": 1000, - "ClampToEdgeWrapping": 1001, - "MirroredRepeatWrapping": 1002, - "NearestFilter": 1003, - "NearestMipMapNearestFilter": 1004, - "NearestMipMapLinearFilter": 1005, - "LinearFilter": 1006, - "LinearMipMapNearestFilter": 1007, - "LinearMipMapLinearFilter": 1008, - "UnsignedByteType": 1009, - "ByteType": 1010, - "ShortType": 1011, - "UnsignedShortType": 1012, - "IntType": 1013, - "UnsignedIntType": 1014, - "FloatType": 1015, - "HalfFloatType": 1025, - "UnsignedShort4444Type": 1016, - "UnsignedShort5551Type": 1017, - "UnsignedShort565Type": 1018, - "AlphaFormat": 1019, - "RGBFormat": 1020, - "RGBAFormat": 1021, - "LuminanceFormat": 1022, - "LuminanceAlphaFormat": 1023, - "DepthFormat": 1026, - "RGB_S3TC_DXT1_Format": 2001, - "RGBA_S3TC_DXT1_Format": 2002, - "RGBA_S3TC_DXT3_Format": 2003, - "RGBA_S3TC_DXT5_Format": 2004, - "RGB_PVRTC_4BPPV1_Format": 2100, - "RGB_PVRTC_2BPPV1_Format": 2101, - "RGBA_PVRTC_4BPPV1_Format": 2102, - "RGBA_PVRTC_2BPPV1_Format": 2103, - "RGB_ETC1_Format": 2151, - "LoopOnce": 2200, - "LoopRepeat": 2201, - "LoopPingPong": 2202, - "InterpolateDiscrete": 2300, - "InterpolateLinear": 2301, - "InterpolateSmooth": 2302, - "ZeroCurvatureEnding": 2400, - "ZeroSlopeEnding": 2401, - "WrapAroundEnding": 2402, - "TrianglesDrawMode": 0, - "TriangleStripDrawMode": 1, - "TriangleFanDrawMode": 2, - "LinearEncoding": 3000, - "GammaEncoding": 3007, - "RGBEEncoding": 3002, - "LogLuvEncoding": 3003, - "RGBM7Encoding": 3004, - "RGBM16Encoding": 3005, - "RGBDEncoding": 3006, - "BasicDepthPacking": 3200, - "RGBADepthPacking": 3201, - "LineStrip": 0, - "LinePieces": 1 - } - }, +[ { "contextRegExpString": "^_?gl$", "constants": { @@ -426,5 +300,131 @@ "UNPACK_COLORSPACE_CONVERSION_WEBGL": 37443, "BROWSER_DEFAULT_WEBGL": 37444 } + }, + { + "contextRegExpString": "^THREE$", + "constants": { + "CullFaceNone": 0, + "CullFaceBack": 1, + "CullFaceFront": 2, + "CullFaceFrontBack": 3, + "FrontFaceDirectionCW": 0, + "FrontFaceDirectionCCW": 1, + "BasicShadowMap": 0, + "PCFShadowMap": 1, + "PCFSoftShadowMap": 2, + "FrontSide": 0, + "BackSide": 1, + "DoubleSide": 2, + "FlatShading": 1, + "SmoothShading": 2, + "NoColors": 0, + "FaceColors": 1, + "VertexColors": 2, + "NoBlending": 0, + "NormalBlending": 1, + "AdditiveBlending": 2, + "SubtractiveBlending": 3, + "MultiplyBlending": 4, + "CustomBlending": 5, + "AddEquation": 100, + "SubtractEquation": 101, + "ReverseSubtractEquation": 102, + "MinEquation": 103, + "MaxEquation": 104, + "ZeroFactor": 200, + "OneFactor": 201, + "SrcColorFactor": 202, + "OneMinusSrcColorFactor": 203, + "SrcAlphaFactor": 204, + "OneMinusSrcAlphaFactor": 205, + "DstAlphaFactor": 206, + "OneMinusDstAlphaFactor": 207, + "DstColorFactor": 208, + "OneMinusDstColorFactor": 209, + "SrcAlphaSaturateFactor": 210, + "NeverDepth": 0, + "AlwaysDepth": 1, + "LessDepth": 2, + "LessEqualDepth": 3, + "EqualDepth": 4, + "GreaterEqualDepth": 5, + "GreaterDepth": 6, + "NotEqualDepth": 7, + "MultiplyOperation": 0, + "MixOperation": 1, + "AddOperation": 2, + "NoToneMapping": 0, + "LinearToneMapping": 1, + "ReinhardToneMapping": 2, + "Uncharted2ToneMapping": 3, + "CineonToneMapping": 4, + "UVMapping": 300, + "CubeReflectionMapping": 301, + "CubeRefractionMapping": 302, + "EquirectangularReflectionMapping": 303, + "EquirectangularRefractionMapping": 304, + "SphericalReflectionMapping": 305, + "CubeUVReflectionMapping": 306, + "CubeUVRefractionMapping": 307, + "RepeatWrapping": 1000, + "ClampToEdgeWrapping": 1001, + "MirroredRepeatWrapping": 1002, + "NearestFilter": 1003, + "NearestMipMapNearestFilter": 1004, + "NearestMipMapLinearFilter": 1005, + "LinearFilter": 1006, + "LinearMipMapNearestFilter": 1007, + "LinearMipMapLinearFilter": 1008, + "UnsignedByteType": 1009, + "ByteType": 1010, + "ShortType": 1011, + "UnsignedShortType": 1012, + "IntType": 1013, + "UnsignedIntType": 1014, + "FloatType": 1015, + "HalfFloatType": 1025, + "UnsignedShort4444Type": 1016, + "UnsignedShort5551Type": 1017, + "UnsignedShort565Type": 1018, + "AlphaFormat": 1019, + "RGBFormat": 1020, + "RGBAFormat": 1021, + "LuminanceFormat": 1022, + "LuminanceAlphaFormat": 1023, + "DepthFormat": 1026, + "RGB_S3TC_DXT1_Format": 2001, + "RGBA_S3TC_DXT1_Format": 2002, + "RGBA_S3TC_DXT3_Format": 2003, + "RGBA_S3TC_DXT5_Format": 2004, + "RGB_PVRTC_4BPPV1_Format": 2100, + "RGB_PVRTC_2BPPV1_Format": 2101, + "RGBA_PVRTC_4BPPV1_Format": 2102, + "RGBA_PVRTC_2BPPV1_Format": 2103, + "RGB_ETC1_Format": 2151, + "LoopOnce": 2200, + "LoopRepeat": 2201, + "LoopPingPong": 2202, + "InterpolateDiscrete": 2300, + "InterpolateLinear": 2301, + "InterpolateSmooth": 2302, + "ZeroCurvatureEnding": 2400, + "ZeroSlopeEnding": 2401, + "WrapAroundEnding": 2402, + "TrianglesDrawMode": 0, + "TriangleStripDrawMode": 1, + "TriangleFanDrawMode": 2, + "LinearEncoding": 3000, + "GammaEncoding": 3007, + "RGBEEncoding": 3002, + "LogLuvEncoding": 3003, + "RGBM7Encoding": 3004, + "RGBM16Encoding": 3005, + "RGBDEncoding": 3006, + "BasicDepthPacking": 3200, + "RGBADepthPacking": 3201, + "LineStrip": 0, + "LinePieces": 1 + } } -] +] \ No newline at end of file diff --git a/utils/build/build.js b/utils/build/build.js index e64f30a8841eb4..014bd7069928fc 100644 --- a/utils/build/build.js +++ b/utils/build/build.js @@ -3,6 +3,7 @@ var path = require("path"); var argparse = require( "argparse" ); var uglify = require("uglify-js"); var constify = require("./constifier/constify"); +var generateSpec = require("./constifier/generateThreeSpec.js"); var spawn = require('child_process').spawn; // This script can be invoked via: @@ -24,12 +25,12 @@ function main() { parser.addArgument( ['--output'], { defaultValue: '../../build/three.js' } ); parser.addArgument( ['--sourcemaps'], { action: 'storeTrue', defaultValue: true } ); - + var args = parser.parseArgs(); - + var output = args.output; console.log(' * Building ' + output); - + var sourcemap = ''; var sourcemapping = ''; @@ -46,16 +47,16 @@ function main() { if ( args.amd ){ buffer.push('function ( root, factory ) {\n\n\tif ( typeof define === \'function\' && define.amd ) {\n\n\t\tdefine( [ \'exports\' ], factory );\n\n\t} else if ( typeof exports === \'object\' ) {\n\n\t\tfactory( exports );\n\n\t} else {\n\n\t\tfactory( root );\n\n\t}\n\n}( this, function ( exports ) {\n\n'); }; - + for ( var i = 0; i < args.include.length; i ++ ){ - + var contents = fs.readFileSync( './includes/' + args.include[i] + '.json', 'utf8' ); var files = JSON.parse( contents ); for ( var j = 0; j < files.length; j ++ ){ var file = '../../' + files[ j ]; - + buffer.push('// File:' + files[ j ]); buffer.push('\n\n'); @@ -75,28 +76,25 @@ function main() { } } - + if ( args.amd ){ buffer.push('exports.THREE = THREE;\n\n} ) );'); }; - - var temp = buffer.join( '' ); - - if ( !args.minify ){ - fs.writeFileSync( output, temp, 'utf8' ); + var temp = buffer.join( '' ); + fs.writeFileSync( output, temp, 'utf8' ); - } else { + if ( args.minify ){ var LICENSE = "threejs.org/license"; // Constant substitution - var constants = JSON.parse( fs.readFileSync( './constants.json', 'utf8' ) ); + var constifierSpec = generateSpec( path.resolve( __dirname, output ) ); sources.forEach( function( source ) { - source.contents = constify( source.contents, constants ); + source.contents = constify( source.contents, constifierSpec ); } ); diff --git a/utils/build/build.py b/utils/build/build.py index 8bf0c02ac09226..df365205e00754 100644 --- a/utils/build/build.py +++ b/utils/build/build.py @@ -112,7 +112,23 @@ def main(argv=None): backup=readText(output) os.remove(output) - writeText( path, constify( readText(path), readJSON('constants.json') ) ) + # Constifier + + #specTempFile = tempfile.NamedTemporaryFile(delete=False) + #specTempFile.close() + + specFile = '../../build/three.constspec.json' # specTempFile.name + + if os.system( 'node -e 0' ) == 0: + os.system( 'node constifier/generateThreeSpec.js ' + path + ' ' + specFile ) + else: + specFile = '../../build/three.constspec.json' + print( "WARNING: Information for constant compression cannot be updated automatically. Please install node.js" ) + + writeText( path, constify( readText(path), readJSON(specFile) ) ) + #os.remove( specTempFile.name ) + + # Closure compiler externs = ' --externs '.join(args.externs) source = ' '.join(sources) diff --git a/utils/build/constifier/constify.js.test.html b/utils/build/constifier/constify.js.test.html index 4e9f6d84f1c354..3c4ac3e632221b 100644 --- a/utils/build/constifier/constify.js.test.html +++ b/utils/build/constifier/constify.js.test.html @@ -15,8 +15,13 @@ function runTest( source, constants ) { + var spec = [ { + contextRegExpString: '^_?gl$', + constants: constants + } ]; + document.getElementById( 'output' ). - innerHTML = constify( source, constants, true ); + innerHTML = constify( source, spec, true ); } @@ -32,7 +37,7 @@ } ); - requestText( '../constants.json', function() { + requestText( './webgl_constants.json', function() { constants = JSON.parse( this.response ); if ( source !== null ) runTest( source, constants ); diff --git a/utils/build/constifier/dumper.html b/utils/build/constifier/dumper.html deleted file mode 100644 index b06658b6b93966..00000000000000 --- a/utils/build/constifier/dumper.html +++ /dev/null @@ -1,78 +0,0 @@ - - - - - THREE / WebGL constant dump - - - - - - - - - - - - - diff --git a/utils/build/constifier/extractConstants.js b/utils/build/constifier/extractConstants.js new file mode 100644 index 00000000000000..f6f158b670db90 --- /dev/null +++ b/utils/build/constifier/extractConstants.js @@ -0,0 +1,24 @@ + +module.exports = function extractConstants( obj ) { + + var result = {}, + + RegExpUpperCaseId = /^[A-Z_]\w+$/; + + for ( var name in obj ) { + + var value = obj[ name ]; + + if ( typeof value === 'number' && + RegExpUpperCaseId.test( name ) ) { + + result[ name ] = value; + + } + + } + + return result; + +}; + diff --git a/utils/build/constifier/generateThreeSpec.js b/utils/build/constifier/generateThreeSpec.js new file mode 100644 index 00000000000000..91b3edd5386da8 --- /dev/null +++ b/utils/build/constifier/generateThreeSpec.js @@ -0,0 +1,59 @@ +// Node.js script to create the 'threeSpec.json' for three.js. +// +// When used as a module defines a function that returns the spec given the +// three.js source file. + +var ConstifierDir = __dirname + '/'; + +var fs = require( 'fs' ), + path = require( 'path' ), + + extractConstants = require( ConstifierDir + 'extractConstants' ); + +function generateThreeSpec( threeJsSourceFile ) { + + var inputDir = path.resolve( + process.cwd(), path.dirname( threeJsSourceFile ) ), + + inputModule = path.basename( threeJsSourceFile, '.js' ); + + var THREE = require( inputDir + '/' + inputModule ); + + return [ + { + contextRegExpString: '^_?gl$', + constants: JSON.parse( + fs.readFileSync( ConstifierDir + 'webgl_constants.json', 'utf-8' ) ) + }, + { + contextRegExpString: '^THREE$', + constants: extractConstants( THREE ) + } + ]; + +} + +if ( require.main === module ) { + + var threeJsSourceFile = process.argv[ 2 ], + outputFile = process.argv[ 3 ]; + + if ( threeJsSourceFile === undefined || outputFile === undefined ) { + + console.error( "Usage:\t%s %s path/to/three.js path/to/output_spec.json", + path.basename( process.argv[ 0 ] ), + path.basename( process.argv[ 1 ] ) ); + + return; + + } + + var spec = generateThreeSpec( threeJsSourceFile ); + + fs.writeFileSync( outputFile, JSON.stringify( spec, null, '\t' ), 'utf-8' ); + +} else { + + module.exports = generateThreeSpec; + +} diff --git a/utils/build/constifier/webgl_constants.html b/utils/build/constifier/webgl_constants.html new file mode 100644 index 00000000000000..107f2d5e86f819 --- /dev/null +++ b/utils/build/constifier/webgl_constants.html @@ -0,0 +1,36 @@ + + + + + WebGL constant dump + + + + + + + + + + + + + + + diff --git a/utils/build/constifier/webgl_constants.json b/utils/build/constifier/webgl_constants.json new file mode 100644 index 00000000000000..7b9b33dbdba9b4 --- /dev/null +++ b/utils/build/constifier/webgl_constants.json @@ -0,0 +1,299 @@ +{ + "DEPTH_BUFFER_BIT": 256, + "STENCIL_BUFFER_BIT": 1024, + "COLOR_BUFFER_BIT": 16384, + "POINTS": 0, + "LINES": 1, + "LINE_LOOP": 2, + "LINE_STRIP": 3, + "TRIANGLES": 4, + "TRIANGLE_STRIP": 5, + "TRIANGLE_FAN": 6, + "ZERO": 0, + "ONE": 1, + "SRC_COLOR": 768, + "ONE_MINUS_SRC_COLOR": 769, + "SRC_ALPHA": 770, + "ONE_MINUS_SRC_ALPHA": 771, + "DST_ALPHA": 772, + "ONE_MINUS_DST_ALPHA": 773, + "DST_COLOR": 774, + "ONE_MINUS_DST_COLOR": 775, + "SRC_ALPHA_SATURATE": 776, + "FUNC_ADD": 32774, + "BLEND_EQUATION": 32777, + "BLEND_EQUATION_RGB": 32777, + "BLEND_EQUATION_ALPHA": 34877, + "FUNC_SUBTRACT": 32778, + "FUNC_REVERSE_SUBTRACT": 32779, + "BLEND_DST_RGB": 32968, + "BLEND_SRC_RGB": 32969, + "BLEND_DST_ALPHA": 32970, + "BLEND_SRC_ALPHA": 32971, + "CONSTANT_COLOR": 32769, + "ONE_MINUS_CONSTANT_COLOR": 32770, + "CONSTANT_ALPHA": 32771, + "ONE_MINUS_CONSTANT_ALPHA": 32772, + "BLEND_COLOR": 32773, + "ARRAY_BUFFER": 34962, + "ELEMENT_ARRAY_BUFFER": 34963, + "ARRAY_BUFFER_BINDING": 34964, + "ELEMENT_ARRAY_BUFFER_BINDING": 34965, + "STREAM_DRAW": 35040, + "STATIC_DRAW": 35044, + "DYNAMIC_DRAW": 35048, + "BUFFER_SIZE": 34660, + "BUFFER_USAGE": 34661, + "CURRENT_VERTEX_ATTRIB": 34342, + "FRONT": 1028, + "BACK": 1029, + "FRONT_AND_BACK": 1032, + "CULL_FACE": 2884, + "BLEND": 3042, + "DITHER": 3024, + "STENCIL_TEST": 2960, + "DEPTH_TEST": 2929, + "SCISSOR_TEST": 3089, + "POLYGON_OFFSET_FILL": 32823, + "SAMPLE_ALPHA_TO_COVERAGE": 32926, + "SAMPLE_COVERAGE": 32928, + "NO_ERROR": 0, + "INVALID_ENUM": 1280, + "INVALID_VALUE": 1281, + "INVALID_OPERATION": 1282, + "OUT_OF_MEMORY": 1285, + "CW": 2304, + "CCW": 2305, + "LINE_WIDTH": 2849, + "ALIASED_POINT_SIZE_RANGE": 33901, + "ALIASED_LINE_WIDTH_RANGE": 33902, + "CULL_FACE_MODE": 2885, + "FRONT_FACE": 2886, + "DEPTH_RANGE": 2928, + "DEPTH_WRITEMASK": 2930, + "DEPTH_CLEAR_VALUE": 2931, + "DEPTH_FUNC": 2932, + "STENCIL_CLEAR_VALUE": 2961, + "STENCIL_FUNC": 2962, + "STENCIL_FAIL": 2964, + "STENCIL_PASS_DEPTH_FAIL": 2965, + "STENCIL_PASS_DEPTH_PASS": 2966, + "STENCIL_REF": 2967, + "STENCIL_VALUE_MASK": 2963, + "STENCIL_WRITEMASK": 2968, + "STENCIL_BACK_FUNC": 34816, + "STENCIL_BACK_FAIL": 34817, + "STENCIL_BACK_PASS_DEPTH_FAIL": 34818, + "STENCIL_BACK_PASS_DEPTH_PASS": 34819, + "STENCIL_BACK_REF": 36003, + "STENCIL_BACK_VALUE_MASK": 36004, + "STENCIL_BACK_WRITEMASK": 36005, + "VIEWPORT": 2978, + "SCISSOR_BOX": 3088, + "COLOR_CLEAR_VALUE": 3106, + "COLOR_WRITEMASK": 3107, + "UNPACK_ALIGNMENT": 3317, + "PACK_ALIGNMENT": 3333, + "MAX_TEXTURE_SIZE": 3379, + "MAX_VIEWPORT_DIMS": 3386, + "SUBPIXEL_BITS": 3408, + "RED_BITS": 3410, + "GREEN_BITS": 3411, + "BLUE_BITS": 3412, + "ALPHA_BITS": 3413, + "DEPTH_BITS": 3414, + "STENCIL_BITS": 3415, + "POLYGON_OFFSET_UNITS": 10752, + "POLYGON_OFFSET_FACTOR": 32824, + "TEXTURE_BINDING_2D": 32873, + "SAMPLE_BUFFERS": 32936, + "SAMPLES": 32937, + "SAMPLE_COVERAGE_VALUE": 32938, + "SAMPLE_COVERAGE_INVERT": 32939, + "COMPRESSED_TEXTURE_FORMATS": 34467, + "DONT_CARE": 4352, + "FASTEST": 4353, + "NICEST": 4354, + "GENERATE_MIPMAP_HINT": 33170, + "BYTE": 5120, + "UNSIGNED_BYTE": 5121, + "SHORT": 5122, + "UNSIGNED_SHORT": 5123, + "INT": 5124, + "UNSIGNED_INT": 5125, + "FLOAT": 5126, + "DEPTH_COMPONENT": 6402, + "ALPHA": 6406, + "RGB": 6407, + "RGBA": 6408, + "LUMINANCE": 6409, + "LUMINANCE_ALPHA": 6410, + "UNSIGNED_SHORT_4_4_4_4": 32819, + "UNSIGNED_SHORT_5_5_5_1": 32820, + "UNSIGNED_SHORT_5_6_5": 33635, + "FRAGMENT_SHADER": 35632, + "VERTEX_SHADER": 35633, + "MAX_VERTEX_ATTRIBS": 34921, + "MAX_VERTEX_UNIFORM_VECTORS": 36347, + "MAX_VARYING_VECTORS": 36348, + "MAX_COMBINED_TEXTURE_IMAGE_UNITS": 35661, + "MAX_VERTEX_TEXTURE_IMAGE_UNITS": 35660, + "MAX_TEXTURE_IMAGE_UNITS": 34930, + "MAX_FRAGMENT_UNIFORM_VECTORS": 36349, + "SHADER_TYPE": 35663, + "DELETE_STATUS": 35712, + "LINK_STATUS": 35714, + "VALIDATE_STATUS": 35715, + "ATTACHED_SHADERS": 35717, + "ACTIVE_UNIFORMS": 35718, + "ACTIVE_ATTRIBUTES": 35721, + "SHADING_LANGUAGE_VERSION": 35724, + "CURRENT_PROGRAM": 35725, + "NEVER": 512, + "LESS": 513, + "EQUAL": 514, + "LEQUAL": 515, + "GREATER": 516, + "NOTEQUAL": 517, + "GEQUAL": 518, + "ALWAYS": 519, + "KEEP": 7680, + "REPLACE": 7681, + "INCR": 7682, + "DECR": 7683, + "INVERT": 5386, + "INCR_WRAP": 34055, + "DECR_WRAP": 34056, + "VENDOR": 7936, + "RENDERER": 7937, + "VERSION": 7938, + "NEAREST": 9728, + "LINEAR": 9729, + "NEAREST_MIPMAP_NEAREST": 9984, + "LINEAR_MIPMAP_NEAREST": 9985, + "NEAREST_MIPMAP_LINEAR": 9986, + "LINEAR_MIPMAP_LINEAR": 9987, + "TEXTURE_MAG_FILTER": 10240, + "TEXTURE_MIN_FILTER": 10241, + "TEXTURE_WRAP_S": 10242, + "TEXTURE_WRAP_T": 10243, + "TEXTURE_2D": 3553, + "TEXTURE": 5890, + "TEXTURE_CUBE_MAP": 34067, + "TEXTURE_BINDING_CUBE_MAP": 34068, + "TEXTURE_CUBE_MAP_POSITIVE_X": 34069, + "TEXTURE_CUBE_MAP_NEGATIVE_X": 34070, + "TEXTURE_CUBE_MAP_POSITIVE_Y": 34071, + "TEXTURE_CUBE_MAP_NEGATIVE_Y": 34072, + "TEXTURE_CUBE_MAP_POSITIVE_Z": 34073, + "TEXTURE_CUBE_MAP_NEGATIVE_Z": 34074, + "MAX_CUBE_MAP_TEXTURE_SIZE": 34076, + "TEXTURE0": 33984, + "TEXTURE1": 33985, + "TEXTURE2": 33986, + "TEXTURE3": 33987, + "TEXTURE4": 33988, + "TEXTURE5": 33989, + "TEXTURE6": 33990, + "TEXTURE7": 33991, + "TEXTURE8": 33992, + "TEXTURE9": 33993, + "TEXTURE10": 33994, + "TEXTURE11": 33995, + "TEXTURE12": 33996, + "TEXTURE13": 33997, + "TEXTURE14": 33998, + "TEXTURE15": 33999, + "TEXTURE16": 34000, + "TEXTURE17": 34001, + "TEXTURE18": 34002, + "TEXTURE19": 34003, + "TEXTURE20": 34004, + "TEXTURE21": 34005, + "TEXTURE22": 34006, + "TEXTURE23": 34007, + "TEXTURE24": 34008, + "TEXTURE25": 34009, + "TEXTURE26": 34010, + "TEXTURE27": 34011, + "TEXTURE28": 34012, + "TEXTURE29": 34013, + "TEXTURE30": 34014, + "TEXTURE31": 34015, + "ACTIVE_TEXTURE": 34016, + "REPEAT": 10497, + "CLAMP_TO_EDGE": 33071, + "MIRRORED_REPEAT": 33648, + "FLOAT_VEC2": 35664, + "FLOAT_VEC3": 35665, + "FLOAT_VEC4": 35666, + "INT_VEC2": 35667, + "INT_VEC3": 35668, + "INT_VEC4": 35669, + "BOOL": 35670, + "BOOL_VEC2": 35671, + "BOOL_VEC3": 35672, + "BOOL_VEC4": 35673, + "FLOAT_MAT2": 35674, + "FLOAT_MAT3": 35675, + "FLOAT_MAT4": 35676, + "SAMPLER_2D": 35678, + "SAMPLER_CUBE": 35680, + "VERTEX_ATTRIB_ARRAY_ENABLED": 34338, + "VERTEX_ATTRIB_ARRAY_SIZE": 34339, + "VERTEX_ATTRIB_ARRAY_STRIDE": 34340, + "VERTEX_ATTRIB_ARRAY_TYPE": 34341, + "VERTEX_ATTRIB_ARRAY_NORMALIZED": 34922, + "VERTEX_ATTRIB_ARRAY_POINTER": 34373, + "VERTEX_ATTRIB_ARRAY_BUFFER_BINDING": 34975, + "IMPLEMENTATION_COLOR_READ_TYPE": 35738, + "IMPLEMENTATION_COLOR_READ_FORMAT": 35739, + "COMPILE_STATUS": 35713, + "LOW_FLOAT": 36336, + "MEDIUM_FLOAT": 36337, + "HIGH_FLOAT": 36338, + "LOW_INT": 36339, + "MEDIUM_INT": 36340, + "HIGH_INT": 36341, + "FRAMEBUFFER": 36160, + "RENDERBUFFER": 36161, + "RGBA4": 32854, + "RGB5_A1": 32855, + "RGB565": 36194, + "DEPTH_COMPONENT16": 33189, + "STENCIL_INDEX": 6401, + "STENCIL_INDEX8": 36168, + "DEPTH_STENCIL": 34041, + "RENDERBUFFER_WIDTH": 36162, + "RENDERBUFFER_HEIGHT": 36163, + "RENDERBUFFER_INTERNAL_FORMAT": 36164, + "RENDERBUFFER_RED_SIZE": 36176, + "RENDERBUFFER_GREEN_SIZE": 36177, + "RENDERBUFFER_BLUE_SIZE": 36178, + "RENDERBUFFER_ALPHA_SIZE": 36179, + "RENDERBUFFER_DEPTH_SIZE": 36180, + "RENDERBUFFER_STENCIL_SIZE": 36181, + "FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE": 36048, + "FRAMEBUFFER_ATTACHMENT_OBJECT_NAME": 36049, + "FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL": 36050, + "FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE": 36051, + "COLOR_ATTACHMENT0": 36064, + "DEPTH_ATTACHMENT": 36096, + "STENCIL_ATTACHMENT": 36128, + "DEPTH_STENCIL_ATTACHMENT": 33306, + "NONE": 0, + "FRAMEBUFFER_COMPLETE": 36053, + "FRAMEBUFFER_INCOMPLETE_ATTACHMENT": 36054, + "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT": 36055, + "FRAMEBUFFER_INCOMPLETE_DIMENSIONS": 36057, + "FRAMEBUFFER_UNSUPPORTED": 36061, + "FRAMEBUFFER_BINDING": 36006, + "RENDERBUFFER_BINDING": 36007, + "MAX_RENDERBUFFER_SIZE": 34024, + "INVALID_FRAMEBUFFER_OPERATION": 1286, + "UNPACK_FLIP_Y_WEBGL": 37440, + "UNPACK_PREMULTIPLY_ALPHA_WEBGL": 37441, + "CONTEXT_LOST_WEBGL": 37442, + "UNPACK_COLORSPACE_CONVERSION_WEBGL": 37443, + "BROWSER_DEFAULT_WEBGL": 37444 +} From e9f738f9f68abdf5c47e8af7b04c287ea66030a2 Mon Sep 17 00:00:00 2001 From: tschw Date: Fri, 6 May 2016 20:46:38 +0200 Subject: [PATCH 8/9] Added 'build.release' to root folder package.json. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 5056f6cf219fee..d16f94f3190b8a 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "scripts": { "build": "cd ./utils/build && node build.js --include common --include extras", + "build.release": "cd ./utils/build && node build.js --include common --include extras --minify --output ../../build/three.min.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { From 6fa4459ac038f99c2b064e7bd4f9a4e9b5b20558 Mon Sep 17 00:00:00 2001 From: tschw Date: Sun, 8 May 2016 05:21:57 +0200 Subject: [PATCH 9/9] Updated three.constspec.json to current build. --- build/three.constspec.json | 1 + 1 file changed, 1 insertion(+) diff --git a/build/three.constspec.json b/build/three.constspec.json index 54dfaf43b0062e..eba0d89948aa7b 100644 --- a/build/three.constspec.json +++ b/build/three.constspec.json @@ -392,6 +392,7 @@ "RGBAFormat": 1021, "LuminanceFormat": 1022, "LuminanceAlphaFormat": 1023, + "RGBEFormat": 1021, "DepthFormat": 1026, "RGB_S3TC_DXT1_Format": 2001, "RGBA_S3TC_DXT1_Format": 2002,