Skip to content
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

-- size, ++ speed #8824

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
431 changes: 431 additions & 0 deletions build/three.constspec.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 0 additions & 4 deletions src/animation/AnimationAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -187,8 +185,6 @@ THREE.AnimationAction._new.prototype = {

crossFadeFrom: function( fadeOutAction, duration, warp ) {

var mixer = this._mixer;

fadeOutAction.fadeOut( duration );
this.fadeIn( duration );

Expand Down
1 change: 0 additions & 1 deletion src/animation/AnimationObjectGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/BufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/core/DirectGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/core/Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -1202,4 +1202,4 @@ Object.assign( THREE.Geometry.prototype, THREE.EventDispatcher.prototype, {

} );

THREE.GeometryIdCount = 0;
THREE.geometryIdCount = 0;
4 changes: 2 additions & 2 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -715,4 +715,4 @@ Object.assign( THREE.Object3D.prototype, THREE.EventDispatcher.prototype, {

} );

THREE.Object3DIdCount = 0;
THREE.object3DIdCount = 0;
4 changes: 2 additions & 2 deletions src/materials/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -321,4 +321,4 @@ THREE.Material.prototype = {

Object.assign( THREE.Material.prototype, THREE.EventDispatcher.prototype );

THREE.MaterialIdCount = 0;
THREE.materialIdCount = 0;
1 change: 1 addition & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ THREE.WebGLRenderer = function ( parameters ) {

this.dispose = function() {

THREE.WebGLUniforms.purgeCache();
_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );

};
Expand Down
14 changes: 7 additions & 7 deletions src/renderers/webgl/WebGLUniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -596,6 +589,13 @@ THREE.WebGLUniforms = ( function() { // scope

};

WebGLUniforms.purgeCache = function() {

arrayCacheF32.length = 0;
arrayCacheI32.length = 0;

};

return WebGLUniforms;

} )();
Expand Down
4 changes: 2 additions & 2 deletions src/textures/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -276,4 +276,4 @@ THREE.Texture.prototype = {

Object.assign( THREE.Texture.prototype, THREE.EventDispatcher.prototype );

THREE.TextureIdCount = 0;
THREE.texturelIdCount = 0;
42 changes: 29 additions & 13 deletions utils/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ var fs = require("fs");
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:
//
// npm run build
// npm run build.min
//
// See 'package.json' for the command lines.

function main() {

"use strict";
Expand All @@ -16,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 = '';

Expand All @@ -38,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');

Expand All @@ -67,21 +76,28 @@ 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 constifierSpec = generateSpec( path.resolve( __dirname, output ) );

sources.forEach( function( source ) {

source.contents = constify( source.contents, constifierSpec );

} );

// Parsing

var toplevel = null;
Expand Down
56 changes: 49 additions & 7 deletions utils/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -85,9 +109,27 @@ 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)

# 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)
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)
Expand All @@ -96,11 +138,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)
Expand Down
54 changes: 54 additions & 0 deletions utils/build/constifier/__init__.py
Original file line number Diff line number Diff line change
@@ -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 <context> . <Id>
# where 'Id' must refer to an uppercase identifier.
# The construct must not be followed by '=', but may
# be followed by '=='.

replaceConstant, sourceCode )

Loading