From f225e245f09df7e1a38b6ce4760e5d664d60b167 Mon Sep 17 00:00:00 2001 From: Andrew Ray Date: Wed, 16 Mar 2016 11:10:49 -0700 Subject: [PATCH 1/3] Removing use of global grunt and adding npm build script This is a minor modernization of the project. You shouldn't require users to have global versions of packages installed. Using npm scripts means the grunt command will be available when the npm script is run. Note the indentation of package.json has changed to two spaces. This is because of the use of the automatic command `npm install --save-dev grunt`. Note any automatic npm command will change the spacing to 2 spaces, so I think it's best to keep it this way. Additionally this adds missing grunt plugins to the packages, which presumably you had installed along with your global grunt. --- README.md | 10 ++----- package.json | 75 ++++++++++++++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index e14fcd1..ecbde05 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,9 @@ A massive improvement over the previous versions of this library is _actual docu Building -------- -This project uses [Grunt](http://gruntjs.com/) to create the distributions, one dev build (not minimized) and one production build (minimized). If you make changes and want to build it, follow these steps: +This project uses [Grunt](http://gruntjs.com/) to create the distributions, one dev build (not minimized) and one production build (minimized). If you make changes and want to build it, run: -If you don't have grunt installed, first make sure you've got [NodeJS](http://nodejs.org/) and NPM installed, then install Grunt CLI. You might have to do this as root: - -```npm install -g grunt-cli``` - -Now you can install the local grunt package: - -```cd [projectFolder] && npm install && grunt``` +```npm run build``` The output of grunt will sit in the `build` folder. diff --git a/package.json b/package.json index 81b569e..ea4f8d0 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,40 @@ { - "name": "shader-particle-engine", - "description": "A GLSL-focused particle engine for THREE.js.", - "version": "1.0.3", - "author": "Luke Moody (https://github.com/squarefeet/)", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/squarefeet/ShaderParticleEngine.git" - }, - "bugs": { - "url": "https://github.com/squarefeet/ShaderParticleEngine/issues" - }, - "homepage": "https://github.com/squarefeet/ShaderParticleEngine/", - "keywords": [ - "three.js", - "webgl", - "particle", - "particles", - "glsl", - "shader", - "spe" - ], - "devDependencies": { - "grunt": "^0.4.5", - "grunt-contrib-concat": "^0.5.1", - "grunt-contrib-uglify": "^0.9.2", - "grunt-docco2": "^0.2.1", - "grunt-jsdoc": "^1.0.0" - }, - "scripts": { - "test": "grunt" - }, - "main": "build/SPE.min.js", - "directories": { - "doc": "docs", - "example": "examples" - } + "name": "shader-particle-engine", + "description": "A GLSL-focused particle engine for THREE.js.", + "version": "1.0.3", + "author": "Luke Moody (https://github.com/squarefeet/)", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/squarefeet/ShaderParticleEngine.git" + }, + "bugs": { + "url": "https://github.com/squarefeet/ShaderParticleEngine/issues" + }, + "homepage": "https://github.com/squarefeet/ShaderParticleEngine/", + "keywords": [ + "three.js", + "webgl", + "particle", + "particles", + "glsl", + "shader", + "spe" + ], + "devDependencies": { + "grunt": "^0.4.5", + "grunt-cli": "^0.1.13", + "grunt-contrib-concat": "^0.5.1", + "grunt-contrib-uglify": "^0.9.2", + "grunt-docco2": "^0.2.1", + "grunt-jsdoc": "^1.1.0" + }, + "scripts": { + "build": "grunt" + }, + "main": "build/SPE.min.js", + "directories": { + "doc": "docs", + "example": "examples" + } } From aecfccb94566075ccbc5ed8255d847024c7e8676 Mon Sep 17 00:00:00 2001 From: Andrew Ray Date: Wed, 16 Mar 2016 12:10:58 -0700 Subject: [PATCH 2/3] Replacing jshint with eslint This is a first step to modernizing the codebase and to make it work with npm. Ticket #95 currently tracks the npm build process. --- .eslintrc | 14 ++++++++++++ .jshintrc | 64 ---------------------------------------------------- package.json | 1 + 3 files changed, 15 insertions(+), 64 deletions(-) create mode 100644 .eslintrc delete mode 100644 .jshintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..f37e41f --- /dev/null +++ b/.eslintrc @@ -0,0 +1,14 @@ +{ + "env": { + "browser": true, + "node": true + }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, + "rules": { + "semi": 2, + "no-undef": 1 + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 50940a0..0000000 --- a/.jshintrc +++ /dev/null @@ -1,64 +0,0 @@ -{ - // Settings - "passfail" : true, // Stop on first error. - "maxerr" : 100, // Maximum errors before stopping. - - // Predefined globals whom JSHint will ignore. - "browser" : true, // Standard browser globals e.g. `window`, `document`. - - "node" : false, - "rhino" : false, - "couch" : false, - - "jquery" : false, - "prototypejs" : false, - "mootools" : false, - "dojo" : false, - - "predef" : [ // Extra globals. - "SPE", - "THREE", - "define", - "module" - ], - - // Development. - "debug" : false, // Allow debugger statements e.g. browser breakpoints. - "devel" : true, // Allow development statements e.g. `console.log();`. - - // EcmaScript 5. - "strict" : true, // Require `use strict` pragma in every file. - "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). - - // The Good Parts. - "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). - "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. - "bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.). - "boss" : true, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. - "curly" : true, // Require {} for every new block or scope. - "eqeqeq" : false, // Require triple equals i.e. `===`. - "eqnull" : false, // Tolerate use of `== null`. - "evil" : false, // Tolerate use of `eval`. - "expr" : true, // Tolerate `ExpressionStatement` as Programs. - "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`. - "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` - "latedef" : true, // Prohibit variable use before definition. - "loopfunc" : true, // Allow functions to be defined within loops. - "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. - "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions. - "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. - "scripturl" : true, // Tolerate script-targeted URLs. - "shadow" : true, // Allows re-define variables later in code e.g. `var x=1; x=2;`. - "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. - "undef" : true, // Require all non-global variables be declared before they are used. - - // Persone styling prefrences. - "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. - "noempty" : true, // Prohibit use of empty blocks. - "nonew" : true, // Prohibit use of constructors for side-effects. - "nomen" : false, // Prohibit use of initial or trailing underbars in names. - "onevar" : false, // Allow only one `var` statement per function. - "plusplus" : false, // Prohibit use of `++` & `--`. - "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. - "trailing" : true // Prohibit trailing whitespaces. -} diff --git a/package.json b/package.json index ea4f8d0..9b064d4 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "spe" ], "devDependencies": { + "eslint": "^2.4.0", "grunt": "^0.4.5", "grunt-cli": "^0.1.13", "grunt-contrib-concat": "^0.5.1", From 0cd6780ae03d6d524a325622a4549b3bbc560e0b Mon Sep 17 00:00:00 2001 From: Andrew Ray Date: Wed, 16 Mar 2016 12:12:34 -0700 Subject: [PATCH 3/3] Removing implicit use of THREE and SPE in source Related to issue #95, the source code of this project relies on the use of the global SPE variable and the global THREE variable. This makes it hard to track what files use what dependencies. Plus the javascript community has moved to commonjs or es6 module syntax for good reason. It makes it easier to work on projects. Relying on the order of a file included in an array of strings is not ideal for dependency management. This also removes the SPE namespace from files which felt redundant, and also mirrors the elimination of the SPE namespace attachments used inside those files. This namespaces all of the objects in a central place src/index.js and exports that as main from package.json --- package.json | 8 +- src/core/{SPE.Emitter.js => Emitter.js} | 103 ++++++++-------- src/core/{SPE.Group.js => Group.js} | 114 ++++++++++-------- src/core/{SPE.utils.js => utils.js} | 8 +- src/{SPE.js => core/variables.js} | 18 +-- ....ShaderAttribute.js => ShaderAttribute.js} | 39 +++--- ...ypedArrayHelper.js => TypedArrayHelper.js} | 70 +++++------ src/index.js | 24 ++++ .../{SPE.shaderChunks.js => shaderChunks.js} | 6 +- src/shaders/{SPE.shaders.js => shaders.js} | 41 ++++--- 10 files changed, 236 insertions(+), 195 deletions(-) rename src/core/{SPE.Emitter.js => Emitter.js} (93%) rename src/core/{SPE.Group.js => Group.js} (87%) rename src/core/{SPE.utils.js => utils.js} (99%) rename src/{SPE.js => core/variables.js} (83%) rename src/helpers/{SPE.ShaderAttribute.js => ShaderAttribute.js} (83%) rename src/helpers/{SPE.TypedArrayHelper.js => TypedArrayHelper.js} (79%) create mode 100644 src/index.js rename src/shaders/{SPE.shaderChunks.js => shaderChunks.js} (99%) rename src/shaders/{SPE.shaders.js => shaders.js} (87%) diff --git a/package.json b/package.json index 9b064d4..67f6120 100644 --- a/package.json +++ b/package.json @@ -28,12 +28,16 @@ "grunt-contrib-concat": "^0.5.1", "grunt-contrib-uglify": "^0.9.2", "grunt-docco2": "^0.2.1", - "grunt-jsdoc": "^1.1.0" + "grunt-jsdoc": "^1.1.0", + "three": "^0.74.0" + }, + "peerDependencies": { + "three": ">=0.72.0" }, "scripts": { "build": "grunt" }, - "main": "build/SPE.min.js", + "main": "src/index.js", "directories": { "doc": "docs", "example": "examples" diff --git a/src/core/SPE.Emitter.js b/src/core/Emitter.js similarity index 93% rename from src/core/SPE.Emitter.js rename to src/core/Emitter.js index 4f72621..96a5c81 100644 --- a/src/core/SPE.Emitter.js +++ b/src/core/Emitter.js @@ -1,11 +1,15 @@ +var THREE = require('three'); +var utils = require('./utils.js'); +var variables = require('./variables.js'); + /** - * An SPE.Emitter instance. + * An Emitter instance. * @typedef {Object} Emitter - * @see SPE.Emitter + * @see Emitter */ /** - * A map of options to configure an SPE.Emitter instance. + * A map of options to configure an Emitter instance. * * @typedef {Object} EmitterOptions * @@ -22,7 +26,7 @@ * will emit particles indefinitely. * NOTE: When an emitter is older than a specified duration, the emitter is NOT removed from * it's group, but rather is just marked as dead, allowing it to be reanimated at a later time - * using `SPE.Emitter.prototype.enable()`. + * using `Emitter.prototype.enable()`. * * @property {Boolean} [isStatic=false] Whether this emitter should be not be simulated (true). * @property {Boolean} [activeMultiplier=1] A value between 0 and 1 describing what percentage of this emitter's particlesPerSecond should be @@ -141,18 +145,17 @@ */ /** - * The SPE.Emitter class. + * The Emitter class. * * @constructor * * @param {EmitterOptions} options A map of options to configure the emitter. */ -SPE.Emitter = function( options ) { +var Emitter = function( options ) { 'use strict'; - var utils = SPE.utils, - types = utils.types, - lifetimeLength = SPE.valueOverLifetimeLength; + var types = utils.types, + lifetimeLength = variables.valueOverLifetimeLength; // Ensure we have a map of options to play with, // and that each option is in the correct format. @@ -176,7 +179,7 @@ SPE.Emitter = function( options ) { this.uuid = THREE.Math.generateUUID(); - this.type = utils.ensureTypedArg( options.type, types.NUMBER, SPE.distributions.BOX ); + this.type = utils.ensureTypedArg( options.type, types.NUMBER, variables.distributions.BOX ); // Start assigning properties...kicking it off with props that DON'T support values over // lifetimes. @@ -379,9 +382,9 @@ SPE.Emitter = function( options ) { utils.ensureValueOverLifetimeCompliance( this.angle, lifetimeLength, lifetimeLength ); }; -SPE.Emitter.constructor = SPE.Emitter; +Emitter.constructor = Emitter; -SPE.Emitter.prototype._createGetterSetters = function( propObj, propName ) { +Emitter.prototype._createGetterSetters = function( propObj, propName ) { 'use strict'; var self = this; @@ -402,7 +405,7 @@ SPE.Emitter.prototype._createGetterSetters = function( propObj, propName ) { return function( value ) { var mapName = self.updateMap[ propName ], prevValue = this[ prop ], - length = SPE.valueOverLifetimeLength; + length = variables.valueOverLifetimeLength; if ( prop === '_rotationCenter' ) { self.updateFlags.rotationCenter = true; @@ -423,7 +426,7 @@ SPE.Emitter.prototype._createGetterSetters = function( propObj, propName ) { // If the previous value was an array, then make // sure the provided value is interpolated correctly. if ( Array.isArray( prevValue ) ) { - SPE.utils.ensureValueOverLifetimeCompliance( self[ propName ], length, length ); + utils.ensureValueOverLifetimeCompliance( self[ propName ], length, length ); } }; }( i ) ) @@ -432,7 +435,7 @@ SPE.Emitter.prototype._createGetterSetters = function( propObj, propName ) { } }; -SPE.Emitter.prototype._setBufferUpdateRanges = function( keys ) { +Emitter.prototype._setBufferUpdateRanges = function( keys ) { 'use strict'; this.attributeKeys = keys; @@ -446,7 +449,7 @@ SPE.Emitter.prototype._setBufferUpdateRanges = function( keys ) { } }; -SPE.Emitter.prototype._calculatePPSValue = function( groupMaxAge ) { +Emitter.prototype._calculatePPSValue = function( groupMaxAge ) { 'use strict'; var particleCount = this.particleCount; @@ -463,14 +466,14 @@ SPE.Emitter.prototype._calculatePPSValue = function( groupMaxAge ) { } }; -SPE.Emitter.prototype._setAttributeOffset = function( startIndex ) { +Emitter.prototype._setAttributeOffset = function( startIndex ) { this.attributeOffset = startIndex; this.activationIndex = startIndex; this.activationEnd = startIndex + this.particleCount; }; -SPE.Emitter.prototype._assignValue = function( prop, index ) { +Emitter.prototype._assignValue = function( prop, index ) { 'use strict'; switch ( prop ) { @@ -506,11 +509,10 @@ SPE.Emitter.prototype._assignValue = function( prop, index ) { } }; -SPE.Emitter.prototype._assignPositionValue = function( index ) { +Emitter.prototype._assignPositionValue = function( index ) { 'use strict'; - var distributions = SPE.distributions, - utils = SPE.utils, + var distributions = variables.distributions, prop = this.position, attr = this.attributes.position, value = prop._value, @@ -532,11 +534,10 @@ SPE.Emitter.prototype._assignPositionValue = function( index ) { } }; -SPE.Emitter.prototype._assignForceValue = function( index, attrName ) { +Emitter.prototype._assignForceValue = function( index, attrName ) { 'use strict'; - var distributions = SPE.distributions, - utils = SPE.utils, + var distributions = variables.distributions, prop = this[ attrName ], value = prop._value, spread = prop._spread, @@ -603,12 +604,11 @@ SPE.Emitter.prototype._assignForceValue = function( index, attrName ) { } }; -SPE.Emitter.prototype._assignAbsLifetimeValue = function( index, propName ) { +Emitter.prototype._assignAbsLifetimeValue = function( index, propName ) { 'use strict'; var array = this.attributes[ propName ].typedArray, prop = this[ propName ], - utils = SPE.utils, value; if ( utils.arrayValuesAreEqual( prop._value ) && utils.arrayValuesAreEqual( prop._spread ) ) { @@ -625,12 +625,11 @@ SPE.Emitter.prototype._assignAbsLifetimeValue = function( index, propName ) { } }; -SPE.Emitter.prototype._assignAngleValue = function( index ) { +Emitter.prototype._assignAngleValue = function( index ) { 'use strict'; var array = this.attributes.angle.typedArray, prop = this.angle, - utils = SPE.utils, value; if ( utils.arrayValuesAreEqual( prop._value ) && utils.arrayValuesAreEqual( prop._spread ) ) { @@ -647,35 +646,35 @@ SPE.Emitter.prototype._assignAngleValue = function( index ) { } }; -SPE.Emitter.prototype._assignParamsValue = function( index ) { +Emitter.prototype._assignParamsValue = function( index ) { 'use strict'; this.attributes.params.typedArray.setVec4Components( index, this.isStatic ? 1 : 0, 0.0, - Math.abs( SPE.utils.randomFloat( this.maxAge._value, this.maxAge._spread ) ), - SPE.utils.randomFloat( this.wiggle._value, this.wiggle._spread ) + Math.abs( utils.randomFloat( this.maxAge._value, this.maxAge._spread ) ), + utils.randomFloat( this.wiggle._value, this.wiggle._spread ) ); }; -SPE.Emitter.prototype._assignRotationValue = function( index ) { +Emitter.prototype._assignRotationValue = function( index ) { 'use strict'; this.attributes.rotation.typedArray.setVec3Components( index, - SPE.utils.getPackedRotationAxis( this.rotation._axis, this.rotation._axisSpread ), - SPE.utils.randomFloat( this.rotation._angle, this.rotation._angleSpread ), + utils.getPackedRotationAxis( this.rotation._axis, this.rotation._axisSpread ), + utils.randomFloat( this.rotation._angle, this.rotation._angleSpread ), this.rotation._static ? 0 : 1 ); this.attributes.rotationCenter.typedArray.setVec3( index, this.rotation._center ); }; -SPE.Emitter.prototype._assignColorValue = function( index ) { +Emitter.prototype._assignColorValue = function( index ) { 'use strict'; - SPE.utils.randomColorAsHex( this.attributes.color, index, this.color._value, this.color._spread ); + utils.randomColorAsHex( this.attributes.color, index, this.color._value, this.color._spread ); }; -SPE.Emitter.prototype._resetParticle = function( index ) { +Emitter.prototype._resetParticle = function( index ) { 'use strict'; var resetFlags = this.resetFlags, @@ -704,7 +703,7 @@ SPE.Emitter.prototype._resetParticle = function( index ) { } }; -SPE.Emitter.prototype._updateAttributeUpdateRange = function( attr, i ) { +Emitter.prototype._updateAttributeUpdateRange = function( attr, i ) { 'use strict'; var ranges = this.bufferUpdateRanges[ attr ]; @@ -713,7 +712,7 @@ SPE.Emitter.prototype._updateAttributeUpdateRange = function( attr, i ) { ranges.max = Math.max( i, ranges.max ); }; -SPE.Emitter.prototype._resetBufferRanges = function() { +Emitter.prototype._resetBufferRanges = function() { 'use strict'; var ranges = this.bufferUpdateRanges, @@ -728,7 +727,7 @@ SPE.Emitter.prototype._resetBufferRanges = function() { } }; -SPE.Emitter.prototype._onRemove = function() { +Emitter.prototype._onRemove = function() { 'use strict'; // Reset any properties of the emitter that were set by // a group when it was added. @@ -742,7 +741,7 @@ SPE.Emitter.prototype._onRemove = function() { this.age = 0.0; }; -SPE.Emitter.prototype._decrementParticleCount = function() { +Emitter.prototype._decrementParticleCount = function() { 'use strict'; --this.activeParticleCount; @@ -750,7 +749,7 @@ SPE.Emitter.prototype._decrementParticleCount = function() { // - Trigger event if count === 0. }; -SPE.Emitter.prototype._incrementParticleCount = function() { +Emitter.prototype._incrementParticleCount = function() { 'use strict'; ++this.activeParticleCount; @@ -758,7 +757,7 @@ SPE.Emitter.prototype._incrementParticleCount = function() { // - Trigger event if count === this.particleCount. }; -SPE.Emitter.prototype._checkParticleAges = function( start, end, params, dt ) { +Emitter.prototype._checkParticleAges = function( start, end, params, dt ) { 'use strict'; for ( var i = end - 1, index, maxAge, age, alive; i >= start; --i ) { index = i * 4; @@ -799,7 +798,7 @@ SPE.Emitter.prototype._checkParticleAges = function( start, end, params, dt ) { } }; -SPE.Emitter.prototype._activateParticles = function( activationStart, activationEnd, params, dtPerParticle ) { +Emitter.prototype._activateParticles = function( activationStart, activationEnd, params, dtPerParticle ) { 'use strict'; var direction = this.direction; @@ -830,7 +829,7 @@ SPE.Emitter.prototype._activateParticles = function( activationStart, activation // This stops particles being 'clumped' together // when frame rates are on the lower side of 60fps // or not constant (a very real possibility!) - dtValue = dtPerParticle * ( i - activationStart ) + dtValue = dtPerParticle * ( i - activationStart ); params[ index + 1 ] = direction === -1 ? params[ index + 2 ] - dtValue : dtValue; this._updateAttributeUpdateRange( 'params', i ); @@ -846,7 +845,7 @@ SPE.Emitter.prototype._activateParticles = function( activationStart, activation * * @param {Number} dt The number of seconds to simulate (deltaTime) */ -SPE.Emitter.prototype.tick = function( dt ) { +Emitter.prototype.tick = function( dt ) { 'use strict'; if ( this.isStatic ) { @@ -913,7 +912,7 @@ SPE.Emitter.prototype.tick = function( dt ) { * @param {Boolean} [force=undefined] If true, all particles will be marked as dead instantly. * @return {Emitter} This emitter instance. */ -SPE.Emitter.prototype.reset = function( force ) { +Emitter.prototype.reset = function( force ) { 'use strict'; this.age = 0.0; @@ -946,7 +945,7 @@ SPE.Emitter.prototype.reset = function( force ) { * * @return {Emitter} This emitter instance. */ -SPE.Emitter.prototype.enable = function() { +Emitter.prototype.enable = function() { 'use strict'; this.alive = true; return this; @@ -960,7 +959,7 @@ SPE.Emitter.prototype.enable = function() { * * @return {Emitter} This emitter instance. */ -SPE.Emitter.prototype.disable = function() { +Emitter.prototype.disable = function() { 'use strict'; this.alive = false; @@ -978,7 +977,7 @@ SPE.Emitter.prototype.disable = function() { * * @see SPE.Group.prototype.removeEmitter */ -SPE.Emitter.prototype.remove = function() { +Emitter.prototype.remove = function() { 'use strict'; if ( this.group !== null ) { this.group.removeEmitter( this ); @@ -988,4 +987,6 @@ SPE.Emitter.prototype.remove = function() { } return this; -}; \ No newline at end of file +}; + +module.exports = Emitter; diff --git a/src/core/SPE.Group.js b/src/core/Group.js similarity index 87% rename from src/core/SPE.Group.js rename to src/core/Group.js index 86d3406..df20e46 100644 --- a/src/core/SPE.Group.js +++ b/src/core/Group.js @@ -1,11 +1,18 @@ +var THREE = require('three'); +var Emitter = require('./Emitter.js'); +var ShaderAttribute = require('../helpers/ShaderAttribute.js'); +var variables = require('./variables.js'); +var shaders = require('../shaders/shaders.js'); +var utils = require('./utils'); + /** - * An SPE.Group instance. + * An Group instance. * @typedef {Object} Group - * @see SPE.Group + * @see Group */ /** - * A map of options to configure an SPE.Group instance. + * A map of options to configure an Group instance. * @typedef {Object} GroupOptions * * @property {Object} texture An object describing the texture used by the group. @@ -52,16 +59,15 @@ /** - * The SPE.Group class. Creates a new group, containing a material, geometry, and mesh. + * The Group class. Creates a new group, containing a material, geometry, and mesh. * * @constructor * @param {GroupOptions} options A map of options to configure the group instance. */ -SPE.Group = function( options ) { +function Group( options ) { 'use strict'; - var utils = SPE.utils, - types = utils.types; + var types = utils.types; // Ensure we have a map of options to play with options = utils.ensureTypedArg( options, types.OBJECT, {} ); @@ -70,7 +76,7 @@ SPE.Group = function( options ) { // Assign a UUID to this instance this.uuid = THREE.Math.generateUUID(); - // If no `deltaTime` value is passed to the `SPE.Group.tick` function, + // If no `deltaTime` value is passed to the `Group.tick` function, // the value of this property will be used to advance the simulation. this.fixedTimeStep = utils.ensureTypedArg( options.fixedTimeStep, types.NUMBER, 0.016 ); @@ -166,7 +172,7 @@ SPE.Group = function( options ) { this.defines = { HAS_PERSPECTIVE: this.hasPerspective, COLORIZE: this.colorize, - VALUE_OVER_LIFETIME_LENGTH: SPE.valueOverLifetimeLength, + VALUE_OVER_LIFETIME_LENGTH: variables.valueOverLifetimeLength, SHOULD_ROTATE_TEXTURE: false, SHOULD_ROTATE_PARTICLES: false, @@ -177,18 +183,18 @@ SPE.Group = function( options ) { // Map of all attributes to be applied to the particles. // - // See SPE.ShaderAttribute for a bit more info on this bit. + // See ShaderAttribute for a bit more info on this bit. this.attributes = { - position: new SPE.ShaderAttribute( 'v3', true ), - acceleration: new SPE.ShaderAttribute( 'v4', true ), // w component is drag - velocity: new SPE.ShaderAttribute( 'v3', true ), - rotation: new SPE.ShaderAttribute( 'v4', true ), - rotationCenter: new SPE.ShaderAttribute( 'v3', true ), - params: new SPE.ShaderAttribute( 'v4', true ), // Holds (alive, age, delay, wiggle) - size: new SPE.ShaderAttribute( 'v4', true ), - angle: new SPE.ShaderAttribute( 'v4', true ), - color: new SPE.ShaderAttribute( 'v4', true ), - opacity: new SPE.ShaderAttribute( 'v4', true ) + position: new ShaderAttribute( 'v3', true ), + acceleration: new ShaderAttribute( 'v4', true ), // w component is drag + velocity: new ShaderAttribute( 'v3', true ), + rotation: new ShaderAttribute( 'v4', true ), + rotationCenter: new ShaderAttribute( 'v3', true ), + params: new ShaderAttribute( 'v4', true ), // Holds (alive, age, delay, wiggle) + size: new ShaderAttribute( 'v4', true ), + angle: new ShaderAttribute( 'v4', true ), + color: new ShaderAttribute( 'v4', true ), + opacity: new ShaderAttribute( 'v4', true ) }; this.attributeKeys = Object.keys( this.attributes ); @@ -198,8 +204,8 @@ SPE.Group = function( options ) { // particles. this.material = new THREE.ShaderMaterial( { uniforms: this.uniforms, - vertexShader: SPE.shaders.vertex, - fragmentShader: SPE.shaders.fragment, + vertexShader: shaders.vertex, + fragmentShader: shaders.fragment, blending: this.blending, transparent: this.transparent, alphaTest: this.alphaTest, @@ -215,14 +221,14 @@ SPE.Group = function( options ) { this.mesh = new THREE.Points( this.geometry, this.material ); if ( this.maxParticleCount === null ) { - console.warn( 'SPE.Group: No maxParticleCount specified. Adding emitters after rendering will probably cause errors.' ); + console.warn( 'Group: No maxParticleCount specified. Adding emitters after rendering will probably cause errors.' ); } }; -SPE.Group.constructor = SPE.Group; +Group.constructor = Group; -SPE.Group.prototype._updateDefines = function() { +Group.prototype._updateDefines = function() { 'use strict'; var emitters = this.emitters, @@ -257,7 +263,7 @@ SPE.Group.prototype._updateDefines = function() { this.material.needsUpdate = true; }; -SPE.Group.prototype._applyAttributesToGeometry = function() { +Group.prototype._applyAttributesToGeometry = function() { 'use strict'; var attributes = this.attributes, @@ -300,12 +306,12 @@ SPE.Group.prototype._applyAttributesToGeometry = function() { }; /** - * Adds an SPE.Emitter instance to this group, creating particle values and + * Adds an Emitter instance to this group, creating particle values and * assigning them to this group's shader attributes. * * @param {Emitter} emitter The emitter to add to this group. */ -SPE.Group.prototype.addEmitter = function( emitter ) { +Group.prototype.addEmitter = function( emitter ) { 'use strict'; // Ensure an actual emitter instance is passed here. @@ -313,8 +319,8 @@ SPE.Group.prototype.addEmitter = function( emitter ) { // Decided not to throw here, just in case a scene's // rendering would be paused. Logging an error instead // of stopping execution if exceptions aren't caught. - if ( emitter instanceof SPE.Emitter === false ) { - console.error( '`emitter` argument must be instance of SPE.Emitter. Was provided with:', emitter ); + if ( emitter instanceof Emitter === false ) { + console.error( '`emitter` argument must be instance of Emitter. Was provided with:', emitter ); return; } @@ -341,7 +347,7 @@ SPE.Group.prototype.addEmitter = function( emitter ) { // Emit a warning if the emitter being added will exceed the buffer sizes specified. if ( this.maxParticleCount !== null && this.particleCount > this.maxParticleCount ) { - console.warn( 'SPE.Group: maxParticleCount exceeded. Requesting', this.particleCount, 'particles, can support only', this.maxParticleCount ); + console.warn( 'Group: maxParticleCount exceeded. Requesting', this.particleCount, 'particles, can support only', this.maxParticleCount ); } @@ -413,13 +419,13 @@ SPE.Group.prototype.addEmitter = function( emitter ) { }; /** - * Removes an SPE.Emitter instance from this group. When called, + * Removes an Emitter instance from this group. When called, * all particle's belonging to the given emitter will be instantly * removed from the scene. * * @param {Emitter} emitter The emitter to add to this group. */ -SPE.Group.prototype.removeEmitter = function( emitter ) { +Group.prototype.removeEmitter = function( emitter ) { 'use strict'; var emitterIndex = this.emitterIDs.indexOf( emitter.uuid ); @@ -429,8 +435,8 @@ SPE.Group.prototype.removeEmitter = function( emitter ) { // Decided not to throw here, just in case a scene's // rendering would be paused. Logging an error instead // of stopping execution if exceptions aren't caught. - if ( emitter instanceof SPE.Emitter === false ) { - console.error( '`emitter` argument must be instance of SPE.Emitter. Was provided with:', emitter ); + if ( emitter instanceof Emitter === false ) { + console.error( '`emitter` argument must be instance of Emitter. Was provided with:', emitter ); return; } @@ -484,7 +490,7 @@ SPE.Group.prototype.removeEmitter = function( emitter ) { * * @return {Emitter|null} */ -SPE.Group.prototype.getFromPool = function() { +Group.prototype.getFromPool = function() { 'use strict'; var pool = this._pool, @@ -494,7 +500,7 @@ SPE.Group.prototype.getFromPool = function() { return pool.pop(); } else if ( createNew ) { - return new SPE.Emitter( this._poolCreationSettings ); + return new Emitter( this._poolCreationSettings ); } return null; @@ -507,11 +513,11 @@ SPE.Group.prototype.getFromPool = function() { * @param {ShaderParticleEmitter} emitter * @return {Group} This group instance. */ -SPE.Group.prototype.releaseIntoPool = function( emitter ) { +Group.prototype.releaseIntoPool = function( emitter ) { 'use strict'; - if ( emitter instanceof SPE.Emitter === false ) { - console.error( 'Argument is not instanceof SPE.Emitter:', emitter ); + if ( emitter instanceof Emitter === false ) { + console.error( 'Argument is not instanceof Emitter:', emitter ); return; } @@ -527,7 +533,7 @@ SPE.Group.prototype.releaseIntoPool = function( emitter ) { * * @return {Array} */ -SPE.Group.prototype.getPool = function() { +Group.prototype.getPool = function() { 'use strict'; return this._pool; }; @@ -541,7 +547,7 @@ SPE.Group.prototype.getPool = function() { * @param {Boolean} createNew Should a new emitter be created if the pool runs out? * @return {Group} This group instance. */ -SPE.Group.prototype.addPool = function( numEmitters, emitterOptions, createNew ) { +Group.prototype.addPool = function( numEmitters, emitterOptions, createNew ) { 'use strict'; var emitter; @@ -553,10 +559,10 @@ SPE.Group.prototype.addPool = function( numEmitters, emitterOptions, createNew ) // Create the emitters, add them to this group and the pool. for ( var i = 0; i < numEmitters; ++i ) { if ( Array.isArray( emitterOptions ) ) { - emitter = new SPE.Emitter( emitterOptions[ i ] ); + emitter = new Emitter( emitterOptions[ i ] ); } else { - emitter = new SPE.Emitter( emitterOptions ); + emitter = new Emitter( emitterOptions ); } this.addEmitter( emitter ); this.releaseIntoPool( emitter ); @@ -567,14 +573,14 @@ SPE.Group.prototype.addPool = function( numEmitters, emitterOptions, createNew ) -SPE.Group.prototype._triggerSingleEmitter = function( pos ) { +Group.prototype._triggerSingleEmitter = function( pos ) { 'use strict'; var emitter = this.getFromPool(), self = this; if ( emitter === null ) { - console.log( 'SPE.Group pool ran out.' ); + console.log( 'Group pool ran out.' ); return; } @@ -607,7 +613,7 @@ SPE.Group.prototype._triggerSingleEmitter = function( pos ) { * @param {Object} [position=undefined] A THREE.Vector3 instance describing the position to activate the emitter(s) at. * @return {Group} This group instance. */ -SPE.Group.prototype.triggerPoolEmitter = function( numEmitters, position ) { +Group.prototype.triggerPoolEmitter = function( numEmitters, position ) { 'use strict'; if ( typeof numEmitters === 'number' && numEmitters > 1 ) { @@ -624,14 +630,14 @@ SPE.Group.prototype.triggerPoolEmitter = function( numEmitters, position ) { -SPE.Group.prototype._updateUniforms = function( dt ) { +Group.prototype._updateUniforms = function( dt ) { 'use strict'; this.uniforms.runTime.value += dt; this.uniforms.deltaTime.value = dt; }; -SPE.Group.prototype._resetBufferRanges = function() { +Group.prototype._resetBufferRanges = function() { 'use strict'; var keys = this.attributeKeys, @@ -644,7 +650,7 @@ SPE.Group.prototype._resetBufferRanges = function() { }; -SPE.Group.prototype._updateBuffers = function( emitter ) { +Group.prototype._updateBuffers = function( emitter ) { 'use strict'; var keys = this.attributeKeys, @@ -670,7 +676,7 @@ SPE.Group.prototype._updateBuffers = function( emitter ) { * attribute values along the way. * @param {Number} [dt=Group's `fixedTimeStep` value] The number of seconds to simulate the group's emitters for (deltaTime) */ -SPE.Group.prototype.tick = function( dt ) { +Group.prototype.tick = function( dt ) { 'use strict'; var emitters = this.emitters, @@ -740,9 +746,11 @@ SPE.Group.prototype.tick = function( dt ) { * * @return {Group} Group instance. */ -SPE.Group.prototype.dispose = function() { +Group.prototype.dispose = function() { 'use strict'; this.geometry.dispose(); this.material.dispose(); return this; -}; \ No newline at end of file +}; + +module.exports = Group; diff --git a/src/core/SPE.utils.js b/src/core/utils.js similarity index 99% rename from src/core/SPE.utils.js rename to src/core/utils.js index f5df4a8..fce65b8 100644 --- a/src/core/SPE.utils.js +++ b/src/core/utils.js @@ -1,9 +1,11 @@ +var THREE = require('three'); + /** * A bunch of utility functions used throughout the library. * @namespace * @type {Object} */ -SPE.utils = { +var utils = { /** * A map of types used by `SPE.utils.ensureTypedArg` and * `SPE.utils.ensureArrayTypedArg` to compare types against. @@ -702,4 +704,6 @@ SPE.utils = { return c.getHex(); }; }() ) -}; \ No newline at end of file +}; + +module.exports = utils; diff --git a/src/SPE.js b/src/core/variables.js similarity index 83% rename from src/SPE.js rename to src/core/variables.js index e58ef70..55fa054 100644 --- a/src/SPE.js +++ b/src/core/variables.js @@ -5,15 +5,7 @@ * @property {Number} SPE.distributions.DISC Values will be distributed within a 2D disc. */ -/** - * Namespace for Shader Particle Engine. - * - * All SPE-related code sits under this namespace. - * - * @type {Object} - * @namespace - */ -var SPE = { +var variables = { /** * A map of supported distribution types used @@ -73,10 +65,4 @@ var SPE = { valueOverLifetimeLength: 4 }; -// Module loader support: -if ( typeof define === 'function' && define.amd ) { - define( 'spe', SPE ); -} -else if ( typeof exports !== 'undefined' && typeof module !== 'undefined' ) { - module.exports = SPE; -} \ No newline at end of file +module.exports = variables; diff --git a/src/helpers/SPE.ShaderAttribute.js b/src/helpers/ShaderAttribute.js similarity index 83% rename from src/helpers/SPE.ShaderAttribute.js rename to src/helpers/ShaderAttribute.js index 5f7d8b3..341de73 100644 --- a/src/helpers/SPE.ShaderAttribute.js +++ b/src/helpers/ShaderAttribute.js @@ -1,16 +1,19 @@ +var TypedArrayHelper = require('./TypedArrayHelper.js'); +var THREE = require('three'); + /** * A helper to handle creating and updating a THREE.BufferAttribute instance. * * @author Luke Moody * @constructor - * @param {String} type The buffer attribute type. See SPE.ShaderAttribute.typeSizeMap for valid values. + * @param {String} type The buffer attribute type. See ShaderAttribute.typeSizeMap for valid values. * @param {Boolean=} dynamicBuffer Whether this buffer attribute should be marked as dynamic or not. * @param {Function=} arrayType A reference to a TypedArray constructor. Defaults to Float32Array if none provided. */ -SPE.ShaderAttribute = function( type, dynamicBuffer, arrayType ) { +function ShaderAttribute( type, dynamicBuffer, arrayType ) { 'use strict'; - var typeMap = SPE.ShaderAttribute.typeSizeMap; + var typeMap = ShaderAttribute.typeSizeMap; this.type = typeof type === 'string' && typeMap.hasOwnProperty( type ) ? type : 'f'; this.componentSize = typeMap[ this.type ]; @@ -23,13 +26,13 @@ SPE.ShaderAttribute = function( type, dynamicBuffer, arrayType ) { this.updateMax = 0; }; -SPE.ShaderAttribute.constructor = SPE.ShaderAttribute; +ShaderAttribute.constructor = ShaderAttribute; /** * A map of uniform types to their component size. * @enum {Number} */ -SPE.ShaderAttribute.typeSizeMap = { +ShaderAttribute.typeSizeMap = { /** * Float * @type {Number} @@ -80,7 +83,7 @@ SPE.ShaderAttribute.typeSizeMap = { * @param {Number} min The start of the range to mark as needing an update. * @param {Number} max The end of the range to mark as needing an update. */ -SPE.ShaderAttribute.prototype.setUpdateRange = function( min, max ) { +ShaderAttribute.prototype.setUpdateRange = function( min, max ) { 'use strict'; this.updateMin = Math.min( min * this.componentSize, this.updateMin * this.componentSize ); @@ -91,7 +94,7 @@ SPE.ShaderAttribute.prototype.setUpdateRange = function( min, max ) { * Calculate the number of indices that this attribute should mark as needing * updating. Also marks the attribute as needing an update. */ -SPE.ShaderAttribute.prototype.flagUpdate = function() { +ShaderAttribute.prototype.flagUpdate = function() { 'use strict'; var attr = this.bufferAttribute, @@ -109,14 +112,14 @@ SPE.ShaderAttribute.prototype.flagUpdate = function() { /** * Reset the index update counts for this attribute */ -SPE.ShaderAttribute.prototype.resetUpdateRange = function() { +ShaderAttribute.prototype.resetUpdateRange = function() { 'use strict'; this.updateMin = 0; this.updateMax = 0; }; -SPE.ShaderAttribute.prototype.resetDynamic = function() { +ShaderAttribute.prototype.resetDynamic = function() { 'use strict'; this.bufferAttribute.dynamic = this.dynamicBuffer; }; @@ -126,7 +129,7 @@ SPE.ShaderAttribute.prototype.resetDynamic = function() { * @param {Number} start The start index of the splice. Will be multiplied by the number of components for this attribute. * @param {Number} end The end index of the splice. Will be multiplied by the number of components for this attribute. */ -SPE.ShaderAttribute.prototype.splice = function( start, end ) { +ShaderAttribute.prototype.splice = function( start, end ) { 'use strict'; this.typedArray.splice( start, end ); @@ -136,7 +139,7 @@ SPE.ShaderAttribute.prototype.splice = function( start, end ) { this.forceUpdateAll(); }; -SPE.ShaderAttribute.prototype.forceUpdateAll = function() { +ShaderAttribute.prototype.forceUpdateAll = function() { 'use strict'; this.bufferAttribute.array = this.typedArray.array; @@ -151,11 +154,11 @@ SPE.ShaderAttribute.prototype.forceUpdateAll = function() { * * If it does, then it will ensure the typed array is of the correct size. * - * If not, a new SPE.TypedArrayHelper instance will be created. + * If not, a new TypedArrayHelper instance will be created. * * @param {Number} size The size of the typed array to create or update to. */ -SPE.ShaderAttribute.prototype._ensureTypedArray = function( size ) { +ShaderAttribute.prototype._ensureTypedArray = function( size ) { 'use strict'; // Condition that's most likely to be true at the top: no change. @@ -171,7 +174,7 @@ SPE.ShaderAttribute.prototype._ensureTypedArray = function( size ) { // This condition should only occur once in an attribute's lifecycle. else if ( this.typedArray === null ) { - this.typedArray = new SPE.TypedArrayHelper( this.arrayType, size, this.componentSize ); + this.typedArray = new TypedArrayHelper( this.arrayType, size, this.componentSize ); } }; @@ -185,7 +188,7 @@ SPE.ShaderAttribute.prototype._ensureTypedArray = function( size ) { * * @param {Number} size The size of the typed array to create if one doesn't exist, or resize existing array to. */ -SPE.ShaderAttribute.prototype._createBufferAttribute = function( size ) { +ShaderAttribute.prototype._createBufferAttribute = function( size ) { 'use strict'; // Make sure the typedArray is present and correct. @@ -208,7 +211,7 @@ SPE.ShaderAttribute.prototype._createBufferAttribute = function( size ) { * Returns the length of the typed array associated with this attribute. * @return {Number} The length of the typed array. Will be 0 if no typed array has been created yet. */ -SPE.ShaderAttribute.prototype.getLength = function() { +ShaderAttribute.prototype.getLength = function() { 'use strict'; if ( this.typedArray === null ) { @@ -216,4 +219,6 @@ SPE.ShaderAttribute.prototype.getLength = function() { } return this.typedArray.array.length; -}; \ No newline at end of file +}; + +module.exports = ShaderAttribute; diff --git a/src/helpers/SPE.TypedArrayHelper.js b/src/helpers/TypedArrayHelper.js similarity index 79% rename from src/helpers/SPE.TypedArrayHelper.js rename to src/helpers/TypedArrayHelper.js index 8eb76fa..f9e79b8 100644 --- a/src/helpers/SPE.TypedArrayHelper.js +++ b/src/helpers/TypedArrayHelper.js @@ -13,7 +13,7 @@ * @param {Number} componentSize The number of components per-value (ie. 3 for a vec3, 9 for a Mat3, etc.) * @param {Number} indexOffset The index in the array from which to start assigning values. Default `0` if none provided */ -SPE.TypedArrayHelper = function( TypedArrayConstructor, size, componentSize, indexOffset ) { +function TypedArrayHelper( TypedArrayConstructor, size, componentSize, indexOffset ) { 'use strict'; this.componentSize = componentSize || 1; @@ -23,7 +23,7 @@ SPE.TypedArrayHelper = function( TypedArrayConstructor, size, componentSize, ind this.indexOffset = indexOffset || 0; }; -SPE.TypedArrayHelper.constructor = SPE.TypedArrayHelper; +TypedArrayHelper.constructor = TypedArrayHelper; /** * Sets the size of the internal array. @@ -35,7 +35,7 @@ SPE.TypedArrayHelper.constructor = SPE.TypedArrayHelper; * * @param {Number} size The new size of the array. */ -SPE.TypedArrayHelper.prototype.setSize = function( size, noComponentMultiply ) { +TypedArrayHelper.prototype.setSize = function( size, noComponentMultiply ) { 'use strict'; var currentArraySize = this.array.length; @@ -59,9 +59,9 @@ SPE.TypedArrayHelper.prototype.setSize = function( size, noComponentMultiply ) { * Shrinks the internal array. * * @param {Number} size The new size of the typed array. Must be smaller than `this.array.length`. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.shrink = function( size ) { +TypedArrayHelper.prototype.shrink = function( size ) { 'use strict'; this.array = this.array.subarray( 0, size ); @@ -72,9 +72,9 @@ SPE.TypedArrayHelper.prototype.shrink = function( size ) { /** * Grows the internal array. * @param {Number} size The new size of the typed array. Must be larger than `this.array.length`. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.grow = function( size ) { +TypedArrayHelper.prototype.grow = function( size ) { 'use strict'; var existingArray = this.array, @@ -92,9 +92,9 @@ SPE.TypedArrayHelper.prototype.grow = function( size ) { * Perform a splice operation on this array's buffer. * @param {Number} start The start index of the splice. Will be multiplied by the number of components for this attribute. * @param {Number} end The end index of the splice. Will be multiplied by the number of components for this attribute. - * @returns {Object} The SPE.TypedArrayHelper instance. + * @returns {Object} The TypedArrayHelper instance. */ -SPE.TypedArrayHelper.prototype.splice = function( start, end ) { +TypedArrayHelper.prototype.splice = function( start, end ) { 'use strict'; start *= this.componentSize; end *= this.componentSize; @@ -123,9 +123,9 @@ SPE.TypedArrayHelper.prototype.splice = function( start, end ) { * * @param {Number} index The start position from which to copy into this array. * @param {TypedArray} array The array from which to copy; the source array. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setFromArray = function( index, array ) { +TypedArrayHelper.prototype.setFromArray = function( index, array ) { 'use strict'; var sourceArraySize = array.length, @@ -148,9 +148,9 @@ SPE.TypedArrayHelper.prototype.setFromArray = function( index, array ) { * * @param {Number} index The index at which to set the vec2 values from. * @param {Vector2} vec2 Any object that has `x` and `y` properties. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec2 = function( index, vec2 ) { +TypedArrayHelper.prototype.setVec2 = function( index, vec2 ) { 'use strict'; return this.setVec2Components( index, vec2.x, vec2.y ); @@ -162,9 +162,9 @@ SPE.TypedArrayHelper.prototype.setVec2 = function( index, vec2 ) { * @param {Number} index The index at which to set the vec2 values from. * @param {Number} x The Vec2's `x` component. * @param {Number} y The Vec2's `y` component. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec2Components = function( index, x, y ) { +TypedArrayHelper.prototype.setVec2Components = function( index, x, y ) { 'use strict'; var array = this.array, @@ -180,9 +180,9 @@ SPE.TypedArrayHelper.prototype.setVec2Components = function( index, x, y ) { * * @param {Number} index The index at which to set the vec3 values from. * @param {Vector3} vec2 Any object that has `x`, `y`, and `z` properties. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec3 = function( index, vec3 ) { +TypedArrayHelper.prototype.setVec3 = function( index, vec3 ) { 'use strict'; return this.setVec3Components( index, vec3.x, vec3.y, vec3.z ); @@ -195,9 +195,9 @@ SPE.TypedArrayHelper.prototype.setVec3 = function( index, vec3 ) { * @param {Number} x The Vec3's `x` component. * @param {Number} y The Vec3's `y` component. * @param {Number} z The Vec3's `z` component. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec3Components = function( index, x, y, z ) { +TypedArrayHelper.prototype.setVec3Components = function( index, x, y, z ) { 'use strict'; var array = this.array, @@ -214,9 +214,9 @@ SPE.TypedArrayHelper.prototype.setVec3Components = function( index, x, y, z ) { * * @param {Number} index The index at which to set the vec4 values from. * @param {Vector4} vec2 Any object that has `x`, `y`, `z`, and `w` properties. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec4 = function( index, vec4 ) { +TypedArrayHelper.prototype.setVec4 = function( index, vec4 ) { 'use strict'; return this.setVec4Components( index, vec4.x, vec4.y, vec4.z, vec4.w ); @@ -230,9 +230,9 @@ SPE.TypedArrayHelper.prototype.setVec4 = function( index, vec4 ) { * @param {Number} y The Vec4's `y` component. * @param {Number} z The Vec4's `z` component. * @param {Number} w The Vec4's `w` component. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setVec4Components = function( index, x, y, z, w ) { +TypedArrayHelper.prototype.setVec4Components = function( index, x, y, z, w ) { 'use strict'; var array = this.array, @@ -250,9 +250,9 @@ SPE.TypedArrayHelper.prototype.setVec4Components = function( index, x, y, z, w ) * * @param {Number} index The index at which to set the matrix values from. * @param {Matrix3} mat3 The 3x3 matrix to set from. Must have a TypedArray property named `elements` to copy from. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setMat3 = function( index, mat3 ) { +TypedArrayHelper.prototype.setMat3 = function( index, mat3 ) { 'use strict'; return this.setFromArray( this.indexOffset + ( index * this.componentSize ), mat3.elements ); @@ -263,9 +263,9 @@ SPE.TypedArrayHelper.prototype.setMat3 = function( index, mat3 ) { * * @param {Number} index The index at which to set the matrix values from. * @param {Matrix4} mat3 The 4x4 matrix to set from. Must have a TypedArray property named `elements` to copy from. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setMat4 = function( index, mat4 ) { +TypedArrayHelper.prototype.setMat4 = function( index, mat4 ) { 'use strict'; return this.setFromArray( this.indexOffset + ( index * this.componentSize ), mat4.elements ); @@ -276,9 +276,9 @@ SPE.TypedArrayHelper.prototype.setMat4 = function( index, mat4 ) { * * @param {Number} index The index at which to set the vec3 values from. * @param {Color} color Any object that has `r`, `g`, and `b` properties. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setColor = function( index, color ) { +TypedArrayHelper.prototype.setColor = function( index, color ) { 'use strict'; return this.setVec3Components( index, color.r, color.g, color.b ); @@ -289,9 +289,9 @@ SPE.TypedArrayHelper.prototype.setColor = function( index, color ) { * * @param {Number} index The index at which to set the vec3 values from. * @param {Number} numericValue The number to assign to this index in the array. - * @return {SPE.TypedArrayHelper} Instance of this class. + * @return {TypedArrayHelper} Instance of this class. */ -SPE.TypedArrayHelper.prototype.setNumber = function( index, numericValue ) { +TypedArrayHelper.prototype.setNumber = function( index, numericValue ) { 'use strict'; this.array[ this.indexOffset + ( index * this.componentSize ) ] = numericValue; @@ -308,7 +308,7 @@ SPE.TypedArrayHelper.prototype.setNumber = function( index, numericValue ) { * @param {Number} index The index in the array to fetch. * @return {Number} The value at the given index. */ -SPE.TypedArrayHelper.prototype.getValueAtIndex = function( index ) { +TypedArrayHelper.prototype.getValueAtIndex = function( index ) { 'use strict'; return this.array[ this.indexOffset + index ]; @@ -324,8 +324,10 @@ SPE.TypedArrayHelper.prototype.getValueAtIndex = function( index ) { * @param {Number} index The index in the array to fetch. * @return {TypedArray} The component value at the given index. */ -SPE.TypedArrayHelper.prototype.getComponentValueAtIndex = function( index ) { +TypedArrayHelper.prototype.getComponentValueAtIndex = function( index ) { 'use strict'; return this.array.subarray( this.indexOffset + ( index * this.componentSize ) ); -}; \ No newline at end of file +}; + +module.exports = TypedArrayHelper; diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..eb5e182 --- /dev/null +++ b/src/index.js @@ -0,0 +1,24 @@ +var variables = require('./core/variables.js'); +var Emitter = require('./core/Emitter.js'); +var Group = require('./core/Group.js'); +var utils = require('./core/utils.js'); +var ShaderAttribute = require('./helpers/ShaderAttribute.js'); +var TypedArrayHelper = require('./helpers/TypedArrayHelper.js'); +var shaderChunks = require('./shaders/shaderChunks.js'); +var shaders = require('./shaders/shaders.js'); + +// Merge variables into the core export for now so they are available as +// SPE.distributions, etc +module.exports = Object.assign( + {}, + variables, + { + Emitter: Emitter, + Group: Group, + utils: utils, + Shader: ShaderAttribute, + TypedAr: TypedArrayHelper, + shader: shaderChunks, + shader: shaders, + } +); diff --git a/src/shaders/SPE.shaderChunks.js b/src/shaders/shaderChunks.js similarity index 99% rename from src/shaders/SPE.shaderChunks.js rename to src/shaders/shaderChunks.js index 2257739..97e8f63 100644 --- a/src/shaders/SPE.shaderChunks.js +++ b/src/shaders/shaderChunks.js @@ -1,4 +1,4 @@ -SPE.shaderChunks = { +var shaderChunks = { // Register color-packing define statements. defines: [ '#define PACKED_COLOR_SIZE 256.0', @@ -252,4 +252,6 @@ SPE.shaderChunks = { '', ' vec4 rotatedTexture = texture2D( texture, vUv );', ].join( '\n' ) -}; \ No newline at end of file +}; + +module.exports = shaderChunks; diff --git a/src/shaders/SPE.shaders.js b/src/shaders/shaders.js similarity index 87% rename from src/shaders/SPE.shaders.js rename to src/shaders/shaders.js index f0e7e43..ca09ae7 100644 --- a/src/shaders/SPE.shaders.js +++ b/src/shaders/shaders.js @@ -1,21 +1,24 @@ -SPE.shaders = { +var THREE = require('three'); +var shaderChunks = require('./shaderChunks.js'); + +var shaders = { vertex: [ - SPE.shaderChunks.defines, - SPE.shaderChunks.uniforms, - SPE.shaderChunks.attributes, - SPE.shaderChunks.varyings, + shaderChunks.defines, + shaderChunks.uniforms, + shaderChunks.attributes, + shaderChunks.varyings, THREE.ShaderChunk.common, THREE.ShaderChunk.logdepthbuf_pars_vertex, - SPE.shaderChunks.branchAvoidanceFunctions, - SPE.shaderChunks.unpackColor, - SPE.shaderChunks.unpackRotationAxis, - SPE.shaderChunks.floatOverLifetime, - SPE.shaderChunks.colorOverLifetime, - SPE.shaderChunks.paramFetchingFunctions, - SPE.shaderChunks.forceFetchingFunctions, - SPE.shaderChunks.rotationFunctions, + shaderChunks.branchAvoidanceFunctions, + shaderChunks.unpackColor, + shaderChunks.unpackRotationAxis, + shaderChunks.floatOverLifetime, + shaderChunks.colorOverLifetime, + shaderChunks.paramFetchingFunctions, + shaderChunks.forceFetchingFunctions, + shaderChunks.rotationFunctions, 'void main() {', @@ -148,15 +151,15 @@ SPE.shaders = { ].join( '\n' ), fragment: [ - SPE.shaderChunks.uniforms, + shaderChunks.uniforms, THREE.ShaderChunk.common, THREE.ShaderChunk.fog_pars_fragment, THREE.ShaderChunk.logdepthbuf_pars_fragment, - SPE.shaderChunks.varyings, + shaderChunks.varyings, - SPE.shaderChunks.branchAvoidanceFunctions, + shaderChunks.branchAvoidanceFunctions, 'void main() {', ' vec3 outgoingLight = vColor.xyz;', @@ -165,7 +168,7 @@ SPE.shaders = { ' if ( vColor.w < float(ALPHATEST) ) discard;', ' #endif', - SPE.shaderChunks.rotateTexture, + shaderChunks.rotateTexture, THREE.ShaderChunk.logdepthbuf_fragment, @@ -176,4 +179,6 @@ SPE.shaders = { ' gl_FragColor = vec4( outgoingLight.xyz, rotatedTexture.w * vColor.w );', '}' ].join( '\n' ) -}; \ No newline at end of file +}; + +module.exports = shaders;