diff --git a/web-service/package.json b/web-service/package.json
index d4daa130f31951bb46813a42601614aab5483fd7..384597f3461e64b2041471258661b826d198ae1d 100644
--- a/web-service/package.json
+++ b/web-service/package.json
@@ -18,15 +18,13 @@
     "body-parser": "^1.19.0",
     "express": "^4.16.4",
     "express-ws": "^4.0.0",
-    "h264-converter": "^0.1.0",
     "mongoose": "^5.7.3",
     "msgpack5": "^4.2.1",
+    "mux.js": "^5.6.2",
     "rematrix": "^0.7.0",
     "three": "^0.116.1",
     "url-parse": "^1.4.7",
-    "uuid": "^3.3.3",
-    "video.js": "^7.7.6",
-    "videojs-vr": "^1.7.1"
+    "uuid": "^3.3.3"
   },
   "devDependencies": {
     "browserify": "^16.5.0"
diff --git a/web-service/public/js/bundle.js b/web-service/public/js/bundle.js
index 1d13b2af775d500f70dd42d705edfa12d2e94b13..27541275475c7f28d37409231344105733e9acd5 100644
--- a/web-service/public/js/bundle.js
+++ b/web-service/public/js/bundle.js
@@ -508,7 +508,7 @@ var objectKeys = Object.keys || function (obj) {
 };
 
 }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
-},{"object-assign":19,"util/":4}],2:[function(require,module,exports){
+},{"object-assign":61,"util/":4}],2:[function(require,module,exports){
 if (typeof Object.create === 'function') {
   // implementation from standard node.js 'util' module
   module.exports = function inherits(ctor, superCtor) {
@@ -1130,7 +1130,7 @@ function hasOwnProperty(obj, prop) {
 }
 
 }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
-},{"./support/isBuffer":3,"_process":21,"inherits":2}],5:[function(require,module,exports){
+},{"./support/isBuffer":3,"_process":63,"inherits":2}],5:[function(require,module,exports){
 'use strict'
 
 exports.byteLength = byteLength
@@ -1669,7 +1669,7 @@ BufferList.prototype._match = function(offset, search) {
 
 module.exports = BufferList
 
-},{"readable-stream":30,"safe-buffer":32,"util":38}],7:[function(require,module,exports){
+},{"readable-stream":72,"safe-buffer":74,"util":80}],7:[function(require,module,exports){
 
 },{}],8:[function(require,module,exports){
 (function (Buffer){
@@ -4293,7 +4293,7 @@ function msgpack (options) {
 
 module.exports = msgpack
 
-},{"./lib/decoder":16,"./lib/encoder":17,"./lib/streams":18,"assert":1,"bl":6,"safe-buffer":32}],16:[function(require,module,exports){
+},{"./lib/decoder":16,"./lib/encoder":17,"./lib/streams":18,"assert":1,"bl":6,"safe-buffer":74}],16:[function(require,module,exports){
 'use strict'
 
 var bl = require('bl')
@@ -4731,7 +4731,7 @@ module.exports = function buildDecode (decodingTypes) {
 
 module.exports.IncompleteBufferError = IncompleteBufferError
 
-},{"bl":6,"util":38}],17:[function(require,module,exports){
+},{"bl":6,"util":80}],17:[function(require,module,exports){
 'use strict'
 
 var Buffer = require('safe-buffer').Buffer
@@ -5076,7 +5076,7 @@ function encodeFloat (obj, forceFloat64) {
   return buf
 }
 
-},{"bl":6,"safe-buffer":32}],18:[function(require,module,exports){
+},{"bl":6,"safe-buffer":74}],18:[function(require,module,exports){
 'use strict'
 
 var Transform = require('readable-stream').Transform
@@ -5168,6055 +5168,14209 @@ Decoder.prototype._transform = function (buf, enc, done) {
 module.exports.decoder = Decoder
 module.exports.encoder = Encoder
 
-},{"bl":6,"inherits":12,"readable-stream":30}],19:[function(require,module,exports){
-/*
-object-assign
-(c) Sindre Sorhus
-@license MIT
-*/
-
+},{"bl":6,"inherits":12,"readable-stream":72}],19:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * A stream-based aac to mp4 converter. This utility can be used to
+ * deliver mp4s to a SourceBuffer on platforms that support native
+ * Media Source Extensions.
+ */
 'use strict';
-/* eslint-disable no-unused-vars */
-var getOwnPropertySymbols = Object.getOwnPropertySymbols;
-var hasOwnProperty = Object.prototype.hasOwnProperty;
-var propIsEnumerable = Object.prototype.propertyIsEnumerable;
+var Stream = require('../utils/stream.js');
+var aacUtils = require('./utils');
 
-function toObject(val) {
-	if (val === null || val === undefined) {
-		throw new TypeError('Object.assign cannot be called with null or undefined');
-	}
+// Constants
+var AacStream;
 
-	return Object(val);
-}
+/**
+ * Splits an incoming stream of binary data into ADTS and ID3 Frames.
+ */
 
-function shouldUseNative() {
-	try {
-		if (!Object.assign) {
-			return false;
-		}
+AacStream = function() {
+  var
+    everything = new Uint8Array(),
+    timeStamp = 0;
 
-		// Detect buggy property enumeration order in older V8 versions.
+  AacStream.prototype.init.call(this);
 
-		// https://bugs.chromium.org/p/v8/issues/detail?id=4118
-		var test1 = new String('abc');  // eslint-disable-line no-new-wrappers
-		test1[5] = 'de';
-		if (Object.getOwnPropertyNames(test1)[0] === '5') {
-			return false;
-		}
+  this.setTimestamp = function(timestamp) {
+    timeStamp = timestamp;
+  };
 
-		// https://bugs.chromium.org/p/v8/issues/detail?id=3056
-		var test2 = {};
-		for (var i = 0; i < 10; i++) {
-			test2['_' + String.fromCharCode(i)] = i;
-		}
-		var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
-			return test2[n];
-		});
-		if (order2.join('') !== '0123456789') {
-			return false;
-		}
+  this.push = function(bytes) {
+    var
+      frameSize = 0,
+      byteIndex = 0,
+      bytesLeft,
+      chunk,
+      packet,
+      tempLength;
+
+    // If there are bytes remaining from the last segment, prepend them to the
+    // bytes that were pushed in
+    if (everything.length) {
+      tempLength = everything.length;
+      everything = new Uint8Array(bytes.byteLength + tempLength);
+      everything.set(everything.subarray(0, tempLength));
+      everything.set(bytes, tempLength);
+    } else {
+      everything = bytes;
+    }
 
-		// https://bugs.chromium.org/p/v8/issues/detail?id=3056
-		var test3 = {};
-		'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
-			test3[letter] = letter;
-		});
-		if (Object.keys(Object.assign({}, test3)).join('') !==
-				'abcdefghijklmnopqrst') {
-			return false;
-		}
+    while (everything.length - byteIndex >= 3) {
+      if ((everything[byteIndex] === 'I'.charCodeAt(0)) &&
+          (everything[byteIndex + 1] === 'D'.charCodeAt(0)) &&
+          (everything[byteIndex + 2] === '3'.charCodeAt(0))) {
 
-		return true;
-	} catch (err) {
-		// We don't expect any of the above to throw, but better to be safe.
-		return false;
-	}
-}
+        // Exit early because we don't have enough to parse
+        // the ID3 tag header
+        if (everything.length - byteIndex < 10) {
+          break;
+        }
 
-module.exports = shouldUseNative() ? Object.assign : function (target, source) {
-	var from;
-	var to = toObject(target);
-	var symbols;
+        // check framesize
+        frameSize = aacUtils.parseId3TagSize(everything, byteIndex);
 
-	for (var s = 1; s < arguments.length; s++) {
-		from = Object(arguments[s]);
+        // Exit early if we don't have enough in the buffer
+        // to emit a full packet
+        // Add to byteIndex to support multiple ID3 tags in sequence
+        if (byteIndex + frameSize > everything.length) {
+          break;
+        }
+        chunk = {
+          type: 'timed-metadata',
+          data: everything.subarray(byteIndex, byteIndex + frameSize)
+        };
+        this.trigger('data', chunk);
+        byteIndex += frameSize;
+        continue;
+      } else if (((everything[byteIndex] & 0xff) === 0xff) &&
+                 ((everything[byteIndex + 1] & 0xf0) === 0xf0)) {
+
+        // Exit early because we don't have enough to parse
+        // the ADTS frame header
+        if (everything.length - byteIndex < 7) {
+          break;
+        }
 
-		for (var key in from) {
-			if (hasOwnProperty.call(from, key)) {
-				to[key] = from[key];
-			}
-		}
+        frameSize = aacUtils.parseAdtsSize(everything, byteIndex);
 
-		if (getOwnPropertySymbols) {
-			symbols = getOwnPropertySymbols(from);
-			for (var i = 0; i < symbols.length; i++) {
-				if (propIsEnumerable.call(from, symbols[i])) {
-					to[symbols[i]] = from[symbols[i]];
-				}
-			}
-		}
-	}
+        // Exit early if we don't have enough in the buffer
+        // to emit a full packet
+        if (byteIndex + frameSize > everything.length) {
+          break;
+        }
 
-	return to;
+        packet = {
+          type: 'audio',
+          data: everything.subarray(byteIndex, byteIndex + frameSize),
+          pts: timeStamp,
+          dts: timeStamp
+        };
+        this.trigger('data', packet);
+        byteIndex += frameSize;
+        continue;
+      }
+      byteIndex++;
+    }
+    bytesLeft = everything.length - byteIndex;
+
+    if (bytesLeft > 0) {
+      everything = everything.subarray(byteIndex);
+    } else {
+      everything = new Uint8Array();
+    }
+  };
+
+  this.reset = function() {
+    everything = new Uint8Array();
+    this.trigger('reset');
+  };
+
+  this.endTimeline = function() {
+    everything = new Uint8Array();
+    this.trigger('endedtimeline');
+  };
 };
 
-},{}],20:[function(require,module,exports){
-(function (process){
+AacStream.prototype = new Stream();
+
+module.exports = AacStream;
+
+},{"../utils/stream.js":60,"./utils":20}],20:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Utilities to detect basic properties and metadata about Aac data.
+ */
 'use strict';
 
-if (typeof process === 'undefined' ||
-    !process.version ||
-    process.version.indexOf('v0.') === 0 ||
-    process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
-  module.exports = { nextTick: nextTick };
-} else {
-  module.exports = process
-}
+var ADTS_SAMPLING_FREQUENCIES = [
+  96000,
+  88200,
+  64000,
+  48000,
+  44100,
+  32000,
+  24000,
+  22050,
+  16000,
+  12000,
+  11025,
+  8000,
+  7350
+];
+
+var isLikelyAacData = function(data) {
+  if ((data[0] === 'I'.charCodeAt(0)) &&
+      (data[1] === 'D'.charCodeAt(0)) &&
+      (data[2] === '3'.charCodeAt(0))) {
+    return true;
+  }
+  return false;
+};
 
-function nextTick(fn, arg1, arg2, arg3) {
-  if (typeof fn !== 'function') {
-    throw new TypeError('"callback" argument must be a function');
+var parseSyncSafeInteger = function(data) {
+  return (data[0] << 21) |
+          (data[1] << 14) |
+          (data[2] << 7) |
+          (data[3]);
+};
+
+// return a percent-encoded representation of the specified byte range
+// @see http://en.wikipedia.org/wiki/Percent-encoding
+var percentEncode = function(bytes, start, end) {
+  var i, result = '';
+  for (i = start; i < end; i++) {
+    result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
   }
-  var len = arguments.length;
-  var args, i;
-  switch (len) {
-  case 0:
-  case 1:
-    return process.nextTick(fn);
-  case 2:
-    return process.nextTick(function afterTickOne() {
-      fn.call(null, arg1);
-    });
-  case 3:
-    return process.nextTick(function afterTickTwo() {
-      fn.call(null, arg1, arg2);
-    });
-  case 4:
-    return process.nextTick(function afterTickThree() {
-      fn.call(null, arg1, arg2, arg3);
-    });
-  default:
-    args = new Array(len - 1);
-    i = 0;
-    while (i < args.length) {
-      args[i++] = arguments[i];
-    }
-    return process.nextTick(function afterTick() {
-      fn.apply(null, args);
-    });
+  return result;
+};
+
+// return the string representation of the specified byte range,
+// interpreted as ISO-8859-1.
+var parseIso88591 = function(bytes, start, end) {
+  return unescape(percentEncode(bytes, start, end)); // jshint ignore:line
+};
+
+var parseId3TagSize = function(header, byteIndex) {
+  var
+    returnSize = (header[byteIndex + 6] << 21) |
+                 (header[byteIndex + 7] << 14) |
+                 (header[byteIndex + 8] << 7) |
+                 (header[byteIndex + 9]),
+    flags = header[byteIndex + 5],
+    footerPresent = (flags & 16) >> 4;
+
+  if (footerPresent) {
+    return returnSize + 20;
   }
-}
+  return returnSize + 10;
+};
 
+var parseAdtsSize = function(header, byteIndex) {
+  var
+    lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
+    middle = header[byteIndex + 4] << 3,
+    highTwo = header[byteIndex + 3] & 0x3 << 11;
 
-}).call(this,require('_process'))
-},{"_process":21}],21:[function(require,module,exports){
-// shim for using process in browser
-var process = module.exports = {};
+  return (highTwo | middle) | lowThree;
+};
 
-// cached from whatever global is present so that test runners that stub it
-// don't break things.  But we need to wrap it in a try catch in case it is
-// wrapped in strict mode code which doesn't define any globals.  It's inside a
-// function because try/catches deoptimize in certain engines.
+var parseType = function(header, byteIndex) {
+  if ((header[byteIndex] === 'I'.charCodeAt(0)) &&
+      (header[byteIndex + 1] === 'D'.charCodeAt(0)) &&
+      (header[byteIndex + 2] === '3'.charCodeAt(0))) {
+    return 'timed-metadata';
+  } else if ((header[byteIndex] & 0xff === 0xff) &&
+             ((header[byteIndex + 1] & 0xf0) === 0xf0)) {
+    return 'audio';
+  }
+  return null;
+};
 
-var cachedSetTimeout;
-var cachedClearTimeout;
+var parseSampleRate = function(packet) {
+  var i = 0;
 
-function defaultSetTimout() {
-    throw new Error('setTimeout has not been defined');
-}
-function defaultClearTimeout () {
-    throw new Error('clearTimeout has not been defined');
-}
-(function () {
-    try {
-        if (typeof setTimeout === 'function') {
-            cachedSetTimeout = setTimeout;
-        } else {
-            cachedSetTimeout = defaultSetTimout;
-        }
-    } catch (e) {
-        cachedSetTimeout = defaultSetTimout;
-    }
-    try {
-        if (typeof clearTimeout === 'function') {
-            cachedClearTimeout = clearTimeout;
-        } else {
-            cachedClearTimeout = defaultClearTimeout;
-        }
-    } catch (e) {
-        cachedClearTimeout = defaultClearTimeout;
-    }
-} ())
-function runTimeout(fun) {
-    if (cachedSetTimeout === setTimeout) {
-        //normal enviroments in sane situations
-        return setTimeout(fun, 0);
+  while (i + 5 < packet.length) {
+    if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
+      // If a valid header was not found,  jump one forward and attempt to
+      // find a valid ADTS header starting at the next byte
+      i++;
+      continue;
     }
-    // if setTimeout wasn't available but was latter defined
-    if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
-        cachedSetTimeout = setTimeout;
-        return setTimeout(fun, 0);
+    return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
+  }
+
+  return null;
+};
+
+var parseAacTimestamp = function(packet) {
+  var frameStart, frameSize, frame, frameHeader;
+
+  // find the start of the first frame and the end of the tag
+  frameStart = 10;
+  if (packet[5] & 0x40) {
+    // advance the frame start past the extended header
+    frameStart += 4; // header size field
+    frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
+  }
+
+  // parse one or more ID3 frames
+  // http://id3.org/id3v2.3.0#ID3v2_frame_overview
+  do {
+    // determine the number of bytes in this frame
+    frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
+    if (frameSize < 1) {
+      return null;
     }
-    try {
-        // when when somebody has screwed with setTimeout but no I.E. maddness
-        return cachedSetTimeout(fun, 0);
-    } catch(e){
-        try {
-            // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
-            return cachedSetTimeout.call(null, fun, 0);
-        } catch(e){
-            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
-            return cachedSetTimeout.call(this, fun, 0);
+    frameHeader = String.fromCharCode(packet[frameStart],
+                                      packet[frameStart + 1],
+                                      packet[frameStart + 2],
+                                      packet[frameStart + 3]);
+
+    if (frameHeader === 'PRIV') {
+      frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
+
+      for (var i = 0; i < frame.byteLength; i++) {
+        if (frame[i] === 0) {
+          var owner = parseIso88591(frame, 0, i);
+          if (owner === 'com.apple.streaming.transportStreamTimestamp') {
+            var d = frame.subarray(i + 1);
+            var size = ((d[3] & 0x01)  << 30) |
+                       (d[4]  << 22) |
+                       (d[5] << 14) |
+                       (d[6] << 6) |
+                       (d[7] >>> 2);
+            size *= 4;
+            size += d[7] & 0x03;
+
+            return size;
+          }
+          break;
         }
+      }
     }
 
+    frameStart += 10; // advance past the frame header
+    frameStart += frameSize; // advance past the frame body
+  } while (frameStart < packet.byteLength);
+  return null;
+};
 
-}
-function runClearTimeout(marker) {
-    if (cachedClearTimeout === clearTimeout) {
-        //normal enviroments in sane situations
-        return clearTimeout(marker);
+module.exports = {
+  isLikelyAacData: isLikelyAacData,
+  parseId3TagSize: parseId3TagSize,
+  parseAdtsSize: parseAdtsSize,
+  parseType: parseType,
+  parseSampleRate: parseSampleRate,
+  parseAacTimestamp: parseAacTimestamp
+};
+
+},{}],21:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
+
+var Stream = require('../utils/stream.js');
+var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
+
+var AdtsStream;
+
+var
+  ADTS_SAMPLING_FREQUENCIES = [
+    96000,
+    88200,
+    64000,
+    48000,
+    44100,
+    32000,
+    24000,
+    22050,
+    16000,
+    12000,
+    11025,
+    8000,
+    7350
+  ];
+
+/*
+ * Accepts a ElementaryStream and emits data events with parsed
+ * AAC Audio Frames of the individual packets. Input audio in ADTS
+ * format is unpacked and re-emitted as AAC frames.
+ *
+ * @see http://wiki.multimedia.cx/index.php?title=ADTS
+ * @see http://wiki.multimedia.cx/?title=Understanding_AAC
+ */
+AdtsStream = function(handlePartialSegments) {
+  var
+    buffer,
+    frameNum = 0;
+
+  AdtsStream.prototype.init.call(this);
+
+  this.push = function(packet) {
+    var
+      i = 0,
+      frameLength,
+      protectionSkipBytes,
+      frameEnd,
+      oldBuffer,
+      sampleCount,
+      adtsFrameDuration;
+
+    if (!handlePartialSegments) {
+      frameNum = 0;
     }
-    // if clearTimeout wasn't available but was latter defined
-    if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
-        cachedClearTimeout = clearTimeout;
-        return clearTimeout(marker);
+
+    if (packet.type !== 'audio') {
+      // ignore non-audio data
+      return;
     }
-    try {
-        // when when somebody has screwed with setTimeout but no I.E. maddness
-        return cachedClearTimeout(marker);
-    } catch (e){
-        try {
-            // When we are in I.E. but the script has been evaled so I.E. doesn't  trust the global object when called normally
-            return cachedClearTimeout.call(null, marker);
-        } catch (e){
-            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
-            // Some versions of I.E. have different rules for clearTimeout vs setTimeout
-            return cachedClearTimeout.call(this, marker);
-        }
+
+    // Prepend any data in the buffer to the input data so that we can parse
+    // aac frames the cross a PES packet boundary
+    if (buffer) {
+      oldBuffer = buffer;
+      buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
+      buffer.set(oldBuffer);
+      buffer.set(packet.data, oldBuffer.byteLength);
+    } else {
+      buffer = packet.data;
     }
 
+    // unpack any ADTS frames which have been fully received
+    // for details on the ADTS header, see http://wiki.multimedia.cx/index.php?title=ADTS
+    while (i + 5 < buffer.length) {
 
+      // Look for the start of an ADTS header..
+      if ((buffer[i] !== 0xFF) || (buffer[i + 1] & 0xF6) !== 0xF0) {
+        // If a valid header was not found,  jump one forward and attempt to
+        // find a valid ADTS header starting at the next byte
+        i++;
+        continue;
+      }
 
-}
-var queue = [];
-var draining = false;
-var currentQueue;
-var queueIndex = -1;
+      // The protection skip bit tells us if we have 2 bytes of CRC data at the
+      // end of the ADTS header
+      protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2;
 
-function cleanUpNextTick() {
-    if (!draining || !currentQueue) {
+      // Frame length is a 13 bit integer starting 16 bits from the
+      // end of the sync sequence
+      frameLength = ((buffer[i + 3] & 0x03) << 11) |
+        (buffer[i + 4] << 3) |
+        ((buffer[i + 5] & 0xe0) >> 5);
+
+      sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
+      adtsFrameDuration = (sampleCount * ONE_SECOND_IN_TS) /
+        ADTS_SAMPLING_FREQUENCIES[(buffer[i + 2] & 0x3c) >>> 2];
+
+      frameEnd = i + frameLength;
+
+      // If we don't have enough data to actually finish this ADTS frame, return
+      // and wait for more data
+      if (buffer.byteLength < frameEnd) {
+        return;
+      }
+
+      // Otherwise, deliver the complete AAC frame
+      this.trigger('data', {
+        pts: packet.pts + (frameNum * adtsFrameDuration),
+        dts: packet.dts + (frameNum * adtsFrameDuration),
+        sampleCount: sampleCount,
+        audioobjecttype: ((buffer[i + 2] >>> 6) & 0x03) + 1,
+        channelcount: ((buffer[i + 2] & 1) << 2) |
+          ((buffer[i + 3] & 0xc0) >>> 6),
+        samplerate: ADTS_SAMPLING_FREQUENCIES[(buffer[i + 2] & 0x3c) >>> 2],
+        samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
+        // assume ISO/IEC 14496-12 AudioSampleEntry default of 16
+        samplesize: 16,
+        data: buffer.subarray(i + 7 + protectionSkipBytes, frameEnd)
+      });
+
+      frameNum++;
+
+      // If the buffer is empty, clear it and return
+      if (buffer.byteLength === frameEnd) {
+        buffer = undefined;
         return;
+      }
+
+      // Remove the finished frame from the buffer and start the process again
+      buffer = buffer.subarray(frameEnd);
     }
-    draining = false;
-    if (currentQueue.length) {
-        queue = currentQueue.concat(queue);
+  };
+
+  this.flush = function() {
+    frameNum = 0;
+    this.trigger('done');
+  };
+
+  this.reset = function() {
+    buffer = void 0;
+    this.trigger('reset');
+  };
+
+  this.endTimeline = function() {
+    buffer = void 0;
+    this.trigger('endedtimeline');
+  };
+};
+
+AdtsStream.prototype = new Stream();
+
+module.exports = AdtsStream;
+
+},{"../utils/clock":58,"../utils/stream.js":60}],22:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
+
+var Stream = require('../utils/stream.js');
+var ExpGolomb = require('../utils/exp-golomb.js');
+
+var H264Stream, NalByteStream;
+var PROFILES_WITH_OPTIONAL_SPS_DATA;
+
+/**
+ * Accepts a NAL unit byte stream and unpacks the embedded NAL units.
+ */
+NalByteStream = function() {
+  var
+    syncPoint = 0,
+    i,
+    buffer;
+  NalByteStream.prototype.init.call(this);
+
+  /*
+   * Scans a byte stream and triggers a data event with the NAL units found.
+   * @param {Object} data Event received from H264Stream
+   * @param {Uint8Array} data.data The h264 byte stream to be scanned
+   *
+   * @see H264Stream.push
+   */
+  this.push = function(data) {
+    var swapBuffer;
+
+    if (!buffer) {
+      buffer = data.data;
     } else {
-        queueIndex = -1;
+      swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
+      swapBuffer.set(buffer);
+      swapBuffer.set(data.data, buffer.byteLength);
+      buffer = swapBuffer;
     }
-    if (queue.length) {
-        drainQueue();
+    var len = buffer.byteLength;
+
+    // Rec. ITU-T H.264, Annex B
+    // scan for NAL unit boundaries
+
+    // a match looks like this:
+    // 0 0 1 .. NAL .. 0 0 1
+    // ^ sync point        ^ i
+    // or this:
+    // 0 0 1 .. NAL .. 0 0 0
+    // ^ sync point        ^ i
+
+    // advance the sync point to a NAL start, if necessary
+    for (; syncPoint < len - 3; syncPoint++) {
+      if (buffer[syncPoint + 2] === 1) {
+        // the sync point is properly aligned
+        i = syncPoint + 5;
+        break;
+      }
     }
-}
 
-function drainQueue() {
-    if (draining) {
-        return;
-    }
-    var timeout = runTimeout(cleanUpNextTick);
-    draining = true;
+    while (i < len) {
+      // look at the current byte to determine if we've hit the end of
+      // a NAL unit boundary
+      switch (buffer[i]) {
+      case 0:
+        // skip past non-sync sequences
+        if (buffer[i - 1] !== 0) {
+          i += 2;
+          break;
+        } else if (buffer[i - 2] !== 0) {
+          i++;
+          break;
+        }
 
-    var len = queue.length;
-    while(len) {
-        currentQueue = queue;
-        queue = [];
-        while (++queueIndex < len) {
-            if (currentQueue) {
-                currentQueue[queueIndex].run();
-            }
+        // deliver the NAL unit if it isn't empty
+        if (syncPoint + 3 !== i - 2) {
+          this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
         }
-        queueIndex = -1;
-        len = queue.length;
-    }
-    currentQueue = null;
-    draining = false;
-    runClearTimeout(timeout);
-}
 
-process.nextTick = function (fun) {
-    var args = new Array(arguments.length - 1);
-    if (arguments.length > 1) {
-        for (var i = 1; i < arguments.length; i++) {
-            args[i - 1] = arguments[i];
+        // drop trailing zeroes
+        do {
+          i++;
+        } while (buffer[i] !== 1 && i < len);
+        syncPoint = i - 2;
+        i += 3;
+        break;
+      case 1:
+        // skip past non-sync sequences
+        if (buffer[i - 1] !== 0 ||
+            buffer[i - 2] !== 0) {
+          i += 3;
+          break;
         }
+
+        // deliver the NAL unit
+        this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
+        syncPoint = i - 2;
+        i += 3;
+        break;
+      default:
+        // the current byte isn't a one or zero, so it cannot be part
+        // of a sync sequence
+        i += 3;
+        break;
+      }
     }
-    queue.push(new Item(fun, args));
-    if (queue.length === 1 && !draining) {
-        runTimeout(drainQueue);
+    // filter out the NAL units that were delivered
+    buffer = buffer.subarray(syncPoint);
+    i -= syncPoint;
+    syncPoint = 0;
+  };
+
+  this.reset = function() {
+    buffer = null;
+    syncPoint = 0;
+    this.trigger('reset');
+  };
+
+  this.flush = function() {
+    // deliver the last buffered NAL unit
+    if (buffer && buffer.byteLength > 3) {
+      this.trigger('data', buffer.subarray(syncPoint + 3));
     }
-};
+    // reset the stream state
+    buffer = null;
+    syncPoint = 0;
+    this.trigger('done');
+  };
 
-// v8 likes predictible objects
-function Item(fun, array) {
-    this.fun = fun;
-    this.array = array;
-}
-Item.prototype.run = function () {
-    this.fun.apply(null, this.array);
+  this.endTimeline = function() {
+    this.flush();
+    this.trigger('endedtimeline');
+  };
+};
+NalByteStream.prototype = new Stream();
+
+// values of profile_idc that indicate additional fields are included in the SPS
+// see Recommendation ITU-T H.264 (4/2013),
+// 7.3.2.1.1 Sequence parameter set data syntax
+PROFILES_WITH_OPTIONAL_SPS_DATA = {
+  100: true,
+  110: true,
+  122: true,
+  244: true,
+  44: true,
+  83: true,
+  86: true,
+  118: true,
+  128: true,
+  138: true,
+  139: true,
+  134: true
 };
-process.title = 'browser';
-process.browser = true;
-process.env = {};
-process.argv = [];
-process.version = ''; // empty string to avoid regexp issues
-process.versions = {};
 
-function noop() {}
+/**
+ * Accepts input from a ElementaryStream and produces H.264 NAL unit data
+ * events.
+ */
+H264Stream = function() {
+  var
+    nalByteStream = new NalByteStream(),
+    self,
+    trackId,
+    currentPts,
+    currentDts,
+
+    discardEmulationPreventionBytes,
+    readSequenceParameterSet,
+    skipScalingList;
+
+  H264Stream.prototype.init.call(this);
+  self = this;
+
+  /*
+   * Pushes a packet from a stream onto the NalByteStream
+   *
+   * @param {Object} packet - A packet received from a stream
+   * @param {Uint8Array} packet.data - The raw bytes of the packet
+   * @param {Number} packet.dts - Decode timestamp of the packet
+   * @param {Number} packet.pts - Presentation timestamp of the packet
+   * @param {Number} packet.trackId - The id of the h264 track this packet came from
+   * @param {('video'|'audio')} packet.type - The type of packet
+   *
+   */
+  this.push = function(packet) {
+    if (packet.type !== 'video') {
+      return;
+    }
+    trackId = packet.trackId;
+    currentPts = packet.pts;
+    currentDts = packet.dts;
 
-process.on = noop;
-process.addListener = noop;
-process.once = noop;
-process.off = noop;
-process.removeListener = noop;
-process.removeAllListeners = noop;
-process.emit = noop;
-process.prependListener = noop;
-process.prependOnceListener = noop;
+    nalByteStream.push(packet);
+  };
 
-process.listeners = function (name) { return [] }
+  /*
+   * Identify NAL unit types and pass on the NALU, trackId, presentation and decode timestamps
+   * for the NALUs to the next stream component.
+   * Also, preprocess caption and sequence parameter NALUs.
+   *
+   * @param {Uint8Array} data - A NAL unit identified by `NalByteStream.push`
+   * @see NalByteStream.push
+   */
+  nalByteStream.on('data', function(data) {
+    var
+      event = {
+        trackId: trackId,
+        pts: currentPts,
+        dts: currentDts,
+        data: data
+      };
 
-process.binding = function (name) {
-    throw new Error('process.binding is not supported');
-};
+    switch (data[0] & 0x1f) {
+    case 0x05:
+      event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
+      break;
+    case 0x06:
+      event.nalUnitType = 'sei_rbsp';
+      event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
+      break;
+    case 0x07:
+      event.nalUnitType = 'seq_parameter_set_rbsp';
+      event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
+      event.config = readSequenceParameterSet(event.escapedRBSP);
+      break;
+    case 0x08:
+      event.nalUnitType = 'pic_parameter_set_rbsp';
+      break;
+    case 0x09:
+      event.nalUnitType = 'access_unit_delimiter_rbsp';
+      break;
 
-process.cwd = function () { return '/' };
-process.chdir = function (dir) {
-    throw new Error('process.chdir is not supported');
-};
-process.umask = function() { return 0; };
+    default:
+      break;
+    }
+    // This triggers data on the H264Stream
+    self.trigger('data', event);
+  });
+  nalByteStream.on('done', function() {
+    self.trigger('done');
+  });
+  nalByteStream.on('partialdone', function() {
+    self.trigger('partialdone');
+  });
+  nalByteStream.on('reset', function() {
+    self.trigger('reset');
+  });
+  nalByteStream.on('endedtimeline', function() {
+    self.trigger('endedtimeline');
+  });
 
-},{}],22:[function(require,module,exports){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
+  this.flush = function() {
+    nalByteStream.flush();
+  };
 
-// a duplex stream is just a stream that is both readable and writable.
-// Since JS doesn't have multiple prototypal inheritance, this class
-// prototypally inherits from Readable, and then parasitically from
-// Writable.
+  this.partialFlush = function() {
+    nalByteStream.partialFlush();
+  };
 
-'use strict';
+  this.reset = function() {
+    nalByteStream.reset();
+  };
 
-/*<replacement>*/
+  this.endTimeline = function() {
+    nalByteStream.endTimeline();
+  };
 
-var pna = require('process-nextick-args');
-/*</replacement>*/
+  /**
+   * Advance the ExpGolomb decoder past a scaling list. The scaling
+   * list is optionally transmitted as part of a sequence parameter
+   * set and is not relevant to transmuxing.
+   * @param count {number} the number of entries in this scaling list
+   * @param expGolombDecoder {object} an ExpGolomb pointed to the
+   * start of a scaling list
+   * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
+   */
+  skipScalingList = function(count, expGolombDecoder) {
+    var
+      lastScale = 8,
+      nextScale = 8,
+      j,
+      deltaScale;
+
+    for (j = 0; j < count; j++) {
+      if (nextScale !== 0) {
+        deltaScale = expGolombDecoder.readExpGolomb();
+        nextScale = (lastScale + deltaScale + 256) % 256;
+      }
 
-/*<replacement>*/
-var objectKeys = Object.keys || function (obj) {
-  var keys = [];
-  for (var key in obj) {
-    keys.push(key);
-  }return keys;
-};
-/*</replacement>*/
+      lastScale = (nextScale === 0) ? lastScale : nextScale;
+    }
+  };
 
-module.exports = Duplex;
+  /**
+   * Expunge any "Emulation Prevention" bytes from a "Raw Byte
+   * Sequence Payload"
+   * @param data {Uint8Array} the bytes of a RBSP from a NAL
+   * unit
+   * @return {Uint8Array} the RBSP without any Emulation
+   * Prevention Bytes
+   */
+  discardEmulationPreventionBytes = function(data) {
+    var
+      length = data.byteLength,
+      emulationPreventionBytesPositions = [],
+      i = 1,
+      newLength, newData;
+
+    // Find all `Emulation Prevention Bytes`
+    while (i < length - 2) {
+      if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
+        emulationPreventionBytesPositions.push(i + 2);
+        i += 2;
+      } else {
+        i++;
+      }
+    }
 
-/*<replacement>*/
-var util = Object.create(require('core-util-is'));
-util.inherits = require('inherits');
-/*</replacement>*/
+    // If no Emulation Prevention Bytes were found just return the original
+    // array
+    if (emulationPreventionBytesPositions.length === 0) {
+      return data;
+    }
 
-var Readable = require('./_stream_readable');
-var Writable = require('./_stream_writable');
+    // Create a new array to hold the NAL unit data
+    newLength = length - emulationPreventionBytesPositions.length;
+    newData = new Uint8Array(newLength);
+    var sourceIndex = 0;
+
+    for (i = 0; i < newLength; sourceIndex++, i++) {
+      if (sourceIndex === emulationPreventionBytesPositions[0]) {
+        // Skip this byte
+        sourceIndex++;
+        // Remove this position index
+        emulationPreventionBytesPositions.shift();
+      }
+      newData[i] = data[sourceIndex];
+    }
 
-util.inherits(Duplex, Readable);
+    return newData;
+  };
 
-{
-  // avoid scope creep, the keys array can then be collected
-  var keys = objectKeys(Writable.prototype);
-  for (var v = 0; v < keys.length; v++) {
-    var method = keys[v];
-    if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
-  }
-}
+  /**
+   * Read a sequence parameter set and return some interesting video
+   * properties. A sequence parameter set is the H264 metadata that
+   * describes the properties of upcoming video frames.
+   * @param data {Uint8Array} the bytes of a sequence parameter set
+   * @return {object} an object with configuration parsed from the
+   * sequence parameter set, including the dimensions of the
+   * associated video frames.
+   */
+  readSequenceParameterSet = function(data) {
+    var
+      frameCropLeftOffset = 0,
+      frameCropRightOffset = 0,
+      frameCropTopOffset = 0,
+      frameCropBottomOffset = 0,
+      sarScale = 1,
+      expGolombDecoder, profileIdc, levelIdc, profileCompatibility,
+      chromaFormatIdc, picOrderCntType,
+      numRefFramesInPicOrderCntCycle, picWidthInMbsMinus1,
+      picHeightInMapUnitsMinus1,
+      frameMbsOnlyFlag,
+      scalingListCount,
+      sarRatio,
+      aspectRatioIdc,
+      i;
+
+    expGolombDecoder = new ExpGolomb(data);
+    profileIdc = expGolombDecoder.readUnsignedByte(); // profile_idc
+    profileCompatibility = expGolombDecoder.readUnsignedByte(); // constraint_set[0-5]_flag
+    levelIdc = expGolombDecoder.readUnsignedByte(); // level_idc u(8)
+    expGolombDecoder.skipUnsignedExpGolomb(); // seq_parameter_set_id
+
+    // some profiles have more optional data we don't need
+    if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
+      chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
+      if (chromaFormatIdc === 3) {
+        expGolombDecoder.skipBits(1); // separate_colour_plane_flag
+      }
+      expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_luma_minus8
+      expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_chroma_minus8
+      expGolombDecoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
+      if (expGolombDecoder.readBoolean()) { // seq_scaling_matrix_present_flag
+        scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12;
+        for (i = 0; i < scalingListCount; i++) {
+          if (expGolombDecoder.readBoolean()) { // seq_scaling_list_present_flag[ i ]
+            if (i < 6) {
+              skipScalingList(16, expGolombDecoder);
+            } else {
+              skipScalingList(64, expGolombDecoder);
+            }
+          }
+        }
+      }
+    }
 
-function Duplex(options) {
-  if (!(this instanceof Duplex)) return new Duplex(options);
+    expGolombDecoder.skipUnsignedExpGolomb(); // log2_max_frame_num_minus4
+    picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
+
+    if (picOrderCntType === 0) {
+      expGolombDecoder.readUnsignedExpGolomb(); // log2_max_pic_order_cnt_lsb_minus4
+    } else if (picOrderCntType === 1) {
+      expGolombDecoder.skipBits(1); // delta_pic_order_always_zero_flag
+      expGolombDecoder.skipExpGolomb(); // offset_for_non_ref_pic
+      expGolombDecoder.skipExpGolomb(); // offset_for_top_to_bottom_field
+      numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
+      for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
+        expGolombDecoder.skipExpGolomb(); // offset_for_ref_frame[ i ]
+      }
+    }
 
-  Readable.call(this, options);
-  Writable.call(this, options);
+    expGolombDecoder.skipUnsignedExpGolomb(); // max_num_ref_frames
+    expGolombDecoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
 
-  if (options && options.readable === false) this.readable = false;
+    picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
+    picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
 
-  if (options && options.writable === false) this.writable = false;
+    frameMbsOnlyFlag = expGolombDecoder.readBits(1);
+    if (frameMbsOnlyFlag === 0) {
+      expGolombDecoder.skipBits(1); // mb_adaptive_frame_field_flag
+    }
 
-  this.allowHalfOpen = true;
-  if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
+    expGolombDecoder.skipBits(1); // direct_8x8_inference_flag
+    if (expGolombDecoder.readBoolean()) { // frame_cropping_flag
+      frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
+      frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
+      frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
+      frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
+    }
+    if (expGolombDecoder.readBoolean()) {
+      // vui_parameters_present_flag
+      if (expGolombDecoder.readBoolean()) {
+        // aspect_ratio_info_present_flag
+        aspectRatioIdc = expGolombDecoder.readUnsignedByte();
+        switch (aspectRatioIdc) {
+          case 1: sarRatio = [1, 1]; break;
+          case 2: sarRatio = [12, 11]; break;
+          case 3: sarRatio = [10, 11]; break;
+          case 4: sarRatio = [16, 11]; break;
+          case 5: sarRatio = [40, 33]; break;
+          case 6: sarRatio = [24, 11]; break;
+          case 7: sarRatio = [20, 11]; break;
+          case 8: sarRatio = [32, 11]; break;
+          case 9: sarRatio = [80, 33]; break;
+          case 10: sarRatio = [18, 11]; break;
+          case 11: sarRatio = [15, 11]; break;
+          case 12: sarRatio = [64, 33]; break;
+          case 13: sarRatio = [160, 99]; break;
+          case 14: sarRatio = [4, 3]; break;
+          case 15: sarRatio = [3, 2]; break;
+          case 16: sarRatio = [2, 1]; break;
+          case 255: {
+            sarRatio = [expGolombDecoder.readUnsignedByte() << 8 |
+                        expGolombDecoder.readUnsignedByte(),
+                        expGolombDecoder.readUnsignedByte() << 8 |
+                        expGolombDecoder.readUnsignedByte() ];
+            break;
+          }
+        }
+        if (sarRatio) {
+          sarScale = sarRatio[0] / sarRatio[1];
+        }
+      }
+    }
+    return {
+      profileIdc: profileIdc,
+      levelIdc: levelIdc,
+      profileCompatibility: profileCompatibility,
+      width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
+      height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) - (frameCropTopOffset * 2) - (frameCropBottomOffset * 2),
+      sarRatio: sarRatio
+    };
+  };
 
-  this.once('end', onend);
-}
+};
+H264Stream.prototype = new Stream();
 
-Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
-  // making it explicit this property is not enumerable
-  // because otherwise some prototype manipulation in
-  // userland will fail
-  enumerable: false,
-  get: function () {
-    return this._writableState.highWaterMark;
-  }
-});
+module.exports = {
+  H264Stream: H264Stream,
+  NalByteStream: NalByteStream
+};
 
-// the no-half-open enforcer
-function onend() {
-  // if we allow half-open state, or if the writable side ended,
-  // then we're ok.
-  if (this.allowHalfOpen || this._writableState.ended) return;
+},{"../utils/exp-golomb.js":59,"../utils/stream.js":60}],23:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+module.exports = {
+  Adts: require('./adts'),
+  h264: require('./h264')
+};
 
-  // no more data can be written.
-  // But allow more writes to happen in this tick.
-  pna.nextTick(onEndNT, this);
-}
+},{"./adts":21,"./h264":22}],24:[function(require,module,exports){
+// constants
+var AUDIO_PROPERTIES = [
+  'audioobjecttype',
+  'channelcount',
+  'samplerate',
+  'samplingfrequencyindex',
+  'samplesize'
+];
 
-function onEndNT(self) {
-  self.end();
-}
+module.exports = AUDIO_PROPERTIES;
 
-Object.defineProperty(Duplex.prototype, 'destroyed', {
-  get: function () {
-    if (this._readableState === undefined || this._writableState === undefined) {
-      return false;
+},{}],25:[function(require,module,exports){
+var VIDEO_PROPERTIES = [
+  'width',
+  'height',
+  'profileIdc',
+  'levelIdc',
+  'profileCompatibility',
+  'sarRatio'
+];
+
+
+module.exports = VIDEO_PROPERTIES;
+
+},{}],26:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+var highPrefix = [33, 16, 5, 32, 164, 27];
+var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
+var zeroFill = function(count) {
+  var a = [];
+  while (count--) {
+    a.push(0);
+  }
+  return a;
+};
+
+var makeTable = function(metaTable) {
+  return Object.keys(metaTable).reduce(function(obj, key) {
+    obj[key] = new Uint8Array(metaTable[key].reduce(function(arr, part) {
+      return arr.concat(part);
+    }, []));
+    return obj;
+  }, {});
+};
+
+
+var silence;
+
+module.exports = function() {
+  if (!silence) {
+    // Frames-of-silence to use for filling in missing AAC frames
+    var coneOfSilence = {
+      96000: [highPrefix, [227, 64], zeroFill(154), [56]],
+      88200: [highPrefix, [231], zeroFill(170), [56]],
+      64000: [highPrefix, [248, 192], zeroFill(240), [56]],
+      48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
+      44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
+      32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
+      24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
+      16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
+      12000: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 253, 128], zeroFill(259), [56]],
+      11025: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 255, 192], zeroFill(268), [55, 175, 128], zeroFill(108), [112]],
+      8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
+    };
+    silence = makeTable(coneOfSilence);
+  }
+  return silence;
+};
+
+},{}],27:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
+
+var Stream = require('../utils/stream.js');
+
+/**
+ * The final stage of the transmuxer that emits the flv tags
+ * for audio, video, and metadata. Also tranlates in time and
+ * outputs caption data and id3 cues.
+ */
+var CoalesceStream = function(options) {
+  // Number of Tracks per output segment
+  // If greater than 1, we combine multiple
+  // tracks into a single segment
+  this.numberOfTracks = 0;
+  this.metadataStream = options.metadataStream;
+
+  this.videoTags = [];
+  this.audioTags = [];
+  this.videoTrack = null;
+  this.audioTrack = null;
+  this.pendingCaptions = [];
+  this.pendingMetadata = [];
+  this.pendingTracks = 0;
+  this.processedTracks = 0;
+
+  CoalesceStream.prototype.init.call(this);
+
+  // Take output from multiple
+  this.push = function(output) {
+    // buffer incoming captions until the associated video segment
+    // finishes
+    if (output.text) {
+      return this.pendingCaptions.push(output);
     }
-    return this._readableState.destroyed && this._writableState.destroyed;
-  },
-  set: function (value) {
-    // we ignore the value if the stream
-    // has not been initialized yet
-    if (this._readableState === undefined || this._writableState === undefined) {
+    // buffer incoming id3 tags until the final flush
+    if (output.frames) {
+      return this.pendingMetadata.push(output);
+    }
+
+    if (output.track.type === 'video') {
+      this.videoTrack = output.track;
+      this.videoTags = output.tags;
+      this.pendingTracks++;
+    }
+    if (output.track.type === 'audio') {
+      this.audioTrack = output.track;
+      this.audioTags = output.tags;
+      this.pendingTracks++;
+    }
+  };
+};
+
+CoalesceStream.prototype = new Stream();
+CoalesceStream.prototype.flush = function(flushSource) {
+  var
+    id3,
+    caption,
+    i,
+    timelineStartPts,
+    event = {
+      tags: {},
+      captions: [],
+      captionStreams: {},
+      metadata: []
+    };
+
+  if (this.pendingTracks < this.numberOfTracks) {
+    if (flushSource !== 'VideoSegmentStream' &&
+        flushSource !== 'AudioSegmentStream') {
+      // Return because we haven't received a flush from a data-generating
+      // portion of the segment (meaning that we have only recieved meta-data
+      // or captions.)
       return;
+    } else if (this.pendingTracks === 0) {
+      // In the case where we receive a flush without any data having been
+      // received we consider it an emitted track for the purposes of coalescing
+      // `done` events.
+      // We do this for the case where there is an audio and video track in the
+      // segment but no audio data. (seen in several playlists with alternate
+      // audio tracks and no audio present in the main TS segments.)
+      this.processedTracks++;
+
+      if (this.processedTracks < this.numberOfTracks) {
+        return;
+      }
     }
+  }
 
-    // backward compatibility, the user is explicitly
-    // managing destroyed
-    this._readableState.destroyed = value;
-    this._writableState.destroyed = value;
+  this.processedTracks += this.pendingTracks;
+  this.pendingTracks = 0;
+
+  if (this.processedTracks < this.numberOfTracks) {
+    return;
   }
-});
 
-Duplex.prototype._destroy = function (err, cb) {
-  this.push(null);
-  this.end();
+  if (this.videoTrack) {
+    timelineStartPts = this.videoTrack.timelineStartInfo.pts;
+  } else if (this.audioTrack) {
+    timelineStartPts = this.audioTrack.timelineStartInfo.pts;
+  }
 
-  pna.nextTick(cb, err);
+  event.tags.videoTags = this.videoTags;
+  event.tags.audioTags = this.audioTags;
+
+  // Translate caption PTS times into second offsets into the
+  // video timeline for the segment, and add track info
+  for (i = 0; i < this.pendingCaptions.length; i++) {
+    caption = this.pendingCaptions[i];
+    caption.startTime = caption.startPts - timelineStartPts;
+    caption.startTime /= 90e3;
+    caption.endTime = caption.endPts - timelineStartPts;
+    caption.endTime /= 90e3;
+    event.captionStreams[caption.stream] = true;
+    event.captions.push(caption);
+  }
+
+  // Translate ID3 frame PTS times into second offsets into the
+  // video timeline for the segment
+  for (i = 0; i < this.pendingMetadata.length; i++) {
+    id3 = this.pendingMetadata[i];
+    id3.cueTime = id3.pts - timelineStartPts;
+    id3.cueTime /= 90e3;
+    event.metadata.push(id3);
+  }
+  // We add this to every single emitted segment even though we only need
+  // it for the first
+  event.metadata.dispatchType = this.metadataStream.dispatchType;
+
+  // Reset stream state
+  this.videoTrack = null;
+  this.audioTrack = null;
+  this.videoTags = [];
+  this.audioTags = [];
+  this.pendingCaptions.length = 0;
+  this.pendingMetadata.length = 0;
+  this.pendingTracks = 0;
+  this.processedTracks = 0;
+
+  // Emit the final segment
+  this.trigger('data', event);
+
+  this.trigger('done');
 };
-},{"./_stream_readable":24,"./_stream_writable":26,"core-util-is":9,"inherits":12,"process-nextick-args":20}],23:[function(require,module,exports){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-// a passthrough stream.
-// basically just the most minimal sort of Transform stream.
-// Every written chunk gets output as-is.
+module.exports = CoalesceStream;
 
+},{"../utils/stream.js":60}],28:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
 'use strict';
 
-module.exports = PassThrough;
+var FlvTag = require('./flv-tag.js');
+
+// For information on the FLV format, see
+// http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf.
+// Technically, this function returns the header and a metadata FLV tag
+// if duration is greater than zero
+// duration in seconds
+// @return {object} the bytes of the FLV header as a Uint8Array
+var getFlvHeader = function(duration, audio, video) { // :ByteArray {
+  var
+    headBytes = new Uint8Array(3 + 1 + 1 + 4),
+    head = new DataView(headBytes.buffer),
+    metadata,
+    result,
+    metadataLength;
+
+  // default arguments
+  duration = duration || 0;
+  audio = audio === undefined ? true : audio;
+  video = video === undefined ? true : video;
+
+  // signature
+  head.setUint8(0, 0x46); // 'F'
+  head.setUint8(1, 0x4c); // 'L'
+  head.setUint8(2, 0x56); // 'V'
+
+  // version
+  head.setUint8(3, 0x01);
+
+  // flags
+  head.setUint8(4, (audio ? 0x04 : 0x00) | (video ? 0x01 : 0x00));
+
+  // data offset, should be 9 for FLV v1
+  head.setUint32(5, headBytes.byteLength);
+
+  // init the first FLV tag
+  if (duration <= 0) {
+    // no duration available so just write the first field of the first
+    // FLV tag
+    result = new Uint8Array(headBytes.byteLength + 4);
+    result.set(headBytes);
+    result.set([0, 0, 0, 0], headBytes.byteLength);
+    return result;
+  }
 
-var Transform = require('./_stream_transform');
+  // write out the duration metadata tag
+  metadata = new FlvTag(FlvTag.METADATA_TAG);
+  metadata.pts = metadata.dts = 0;
+  metadata.writeMetaDataDouble('duration', duration);
+  metadataLength = metadata.finalize().length;
+  result = new Uint8Array(headBytes.byteLength + metadataLength);
+  result.set(headBytes);
+  result.set(head.byteLength, metadataLength);
 
-/*<replacement>*/
-var util = Object.create(require('core-util-is'));
-util.inherits = require('inherits');
-/*</replacement>*/
+  return result;
+};
 
-util.inherits(PassThrough, Transform);
+module.exports = getFlvHeader;
 
-function PassThrough(options) {
-  if (!(this instanceof PassThrough)) return new PassThrough(options);
+},{"./flv-tag.js":29}],29:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * An object that stores the bytes of an FLV tag and methods for
+ * querying and manipulating that data.
+ * @see http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf
+ */
+'use strict';
 
-  Transform.call(this, options);
-}
+var FlvTag;
+
+// (type:uint, extraData:Boolean = false) extends ByteArray
+FlvTag = function(type, extraData) {
+  var
+    // Counter if this is a metadata tag, nal start marker if this is a video
+    // tag. unused if this is an audio tag
+    adHoc = 0, // :uint
+
+    // The default size is 16kb but this is not enough to hold iframe
+    // data and the resizing algorithm costs a bit so we create a larger
+    // starting buffer for video tags
+    bufferStartSize = 16384,
+
+    // checks whether the FLV tag has enough capacity to accept the proposed
+    // write and re-allocates the internal buffers if necessary
+    prepareWrite = function(flv, count) {
+      var
+        bytes,
+        minLength = flv.position + count;
+      if (minLength < flv.bytes.byteLength) {
+        // there's enough capacity so do nothing
+        return;
+      }
 
-PassThrough.prototype._transform = function (chunk, encoding, cb) {
-  cb(null, chunk);
-};
-},{"./_stream_transform":25,"core-util-is":9,"inherits":12}],24:[function(require,module,exports){
-(function (process,global){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
+      // allocate a new buffer and copy over the data that will not be modified
+      bytes = new Uint8Array(minLength * 2);
+      bytes.set(flv.bytes.subarray(0, flv.position), 0);
+      flv.bytes = bytes;
+      flv.view = new DataView(flv.bytes.buffer);
+    },
 
-'use strict';
+    // commonly used metadata properties
+    widthBytes = FlvTag.widthBytes || new Uint8Array('width'.length),
+    heightBytes = FlvTag.heightBytes || new Uint8Array('height'.length),
+    videocodecidBytes = FlvTag.videocodecidBytes || new Uint8Array('videocodecid'.length),
+    i;
+
+  if (!FlvTag.widthBytes) {
+    // calculating the bytes of common metadata names ahead of time makes the
+    // corresponding writes faster because we don't have to loop over the
+    // characters
+    // re-test with test/perf.html if you're planning on changing this
+    for (i = 0; i < 'width'.length; i++) {
+      widthBytes[i] = 'width'.charCodeAt(i);
+    }
+    for (i = 0; i < 'height'.length; i++) {
+      heightBytes[i] = 'height'.charCodeAt(i);
+    }
+    for (i = 0; i < 'videocodecid'.length; i++) {
+      videocodecidBytes[i] = 'videocodecid'.charCodeAt(i);
+    }
 
-/*<replacement>*/
+    FlvTag.widthBytes = widthBytes;
+    FlvTag.heightBytes = heightBytes;
+    FlvTag.videocodecidBytes = videocodecidBytes;
+  }
 
-var pna = require('process-nextick-args');
-/*</replacement>*/
+  this.keyFrame = false; // :Boolean
+
+  switch (type) {
+  case FlvTag.VIDEO_TAG:
+    this.length = 16;
+    // Start the buffer at 256k
+    bufferStartSize *= 6;
+    break;
+  case FlvTag.AUDIO_TAG:
+    this.length = 13;
+    this.keyFrame = true;
+    break;
+  case FlvTag.METADATA_TAG:
+    this.length = 29;
+    this.keyFrame = true;
+    break;
+  default:
+    throw new Error('Unknown FLV tag type');
+  }
 
-module.exports = Readable;
+  this.bytes = new Uint8Array(bufferStartSize);
+  this.view = new DataView(this.bytes.buffer);
+  this.bytes[0] = type;
+  this.position = this.length;
+  this.keyFrame = extraData; // Defaults to false
+
+  // presentation timestamp
+  this.pts = 0;
+  // decoder timestamp
+  this.dts = 0;
+
+  // ByteArray#writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0)
+  this.writeBytes = function(bytes, offset, length) {
+    var
+      start = offset || 0,
+      end;
+    length = length || bytes.byteLength;
+    end = start + length;
+
+    prepareWrite(this, length);
+    this.bytes.set(bytes.subarray(start, end), this.position);
+
+    this.position += length;
+    this.length = Math.max(this.length, this.position);
+  };
 
-/*<replacement>*/
-var isArray = require('isarray');
-/*</replacement>*/
+  // ByteArray#writeByte(value:int):void
+  this.writeByte = function(byte) {
+    prepareWrite(this, 1);
+    this.bytes[this.position] = byte;
+    this.position++;
+    this.length = Math.max(this.length, this.position);
+  };
 
-/*<replacement>*/
-var Duplex;
-/*</replacement>*/
+  // ByteArray#writeShort(value:int):void
+  this.writeShort = function(short) {
+    prepareWrite(this, 2);
+    this.view.setUint16(this.position, short);
+    this.position += 2;
+    this.length = Math.max(this.length, this.position);
+  };
 
-Readable.ReadableState = ReadableState;
+  // Negative index into array
+  // (pos:uint):int
+  this.negIndex = function(pos) {
+    return this.bytes[this.length - pos];
+  };
 
-/*<replacement>*/
-var EE = require('events').EventEmitter;
+  // The functions below ONLY work when this[0] == VIDEO_TAG.
+  // We are not going to check for that because we dont want the overhead
+  // (nal:ByteArray = null):int
+  this.nalUnitSize = function() {
+    if (adHoc === 0) {
+      return 0;
+    }
 
-var EElistenerCount = function (emitter, type) {
-  return emitter.listeners(type).length;
-};
-/*</replacement>*/
+    return this.length - (adHoc + 4);
+  };
 
-/*<replacement>*/
-var Stream = require('./internal/streams/stream');
-/*</replacement>*/
+  this.startNalUnit = function() {
+    // remember position and add 4 bytes
+    if (adHoc > 0) {
+      throw new Error('Attempted to create new NAL wihout closing the old one');
+    }
 
-/*<replacement>*/
+    // reserve 4 bytes for nal unit size
+    adHoc = this.length;
+    this.length += 4;
+    this.position = this.length;
+  };
 
-var Buffer = require('safe-buffer').Buffer;
-var OurUint8Array = global.Uint8Array || function () {};
-function _uint8ArrayToBuffer(chunk) {
-  return Buffer.from(chunk);
-}
-function _isUint8Array(obj) {
-  return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
-}
+  // (nal:ByteArray = null):void
+  this.endNalUnit = function(nalContainer) {
+    var
+      nalStart, // :uint
+      nalLength; // :uint
+
+    // Rewind to the marker and write the size
+    if (this.length === adHoc + 4) {
+      // we started a nal unit, but didnt write one, so roll back the 4 byte size value
+      this.length -= 4;
+    } else if (adHoc > 0) {
+      nalStart = adHoc + 4;
+      nalLength = this.length - nalStart;
+
+      this.position = adHoc;
+      this.view.setUint32(this.position, nalLength);
+      this.position = this.length;
+
+      if (nalContainer) {
+        // Add the tag to the NAL unit
+        nalContainer.push(this.bytes.subarray(nalStart, nalStart + nalLength));
+      }
+    }
 
-/*</replacement>*/
+    adHoc = 0;
+  };
 
-/*<replacement>*/
-var util = Object.create(require('core-util-is'));
-util.inherits = require('inherits');
-/*</replacement>*/
+  /**
+   * Write out a 64-bit floating point valued metadata property. This method is
+   * called frequently during a typical parse and needs to be fast.
+   */
+  // (key:String, val:Number):void
+  this.writeMetaDataDouble = function(key, val) {
+    var i;
+    prepareWrite(this, 2 + key.length + 9);
+
+    // write size of property name
+    this.view.setUint16(this.position, key.length);
+    this.position += 2;
+
+    // this next part looks terrible but it improves parser throughput by
+    // 10kB/s in my testing
+
+    // write property name
+    if (key === 'width') {
+      this.bytes.set(widthBytes, this.position);
+      this.position += 5;
+    } else if (key === 'height') {
+      this.bytes.set(heightBytes, this.position);
+      this.position += 6;
+    } else if (key === 'videocodecid') {
+      this.bytes.set(videocodecidBytes, this.position);
+      this.position += 12;
+    } else {
+      for (i = 0; i < key.length; i++) {
+        this.bytes[this.position] = key.charCodeAt(i);
+        this.position++;
+      }
+    }
 
-/*<replacement>*/
-var debugUtil = require('util');
-var debug = void 0;
-if (debugUtil && debugUtil.debuglog) {
-  debug = debugUtil.debuglog('stream');
-} else {
-  debug = function () {};
-}
-/*</replacement>*/
+    // skip null byte
+    this.position++;
 
-var BufferList = require('./internal/streams/BufferList');
-var destroyImpl = require('./internal/streams/destroy');
-var StringDecoder;
+    // write property value
+    this.view.setFloat64(this.position, val);
+    this.position += 8;
 
-util.inherits(Readable, Stream);
+    // update flv tag length
+    this.length = Math.max(this.length, this.position);
+    ++adHoc;
+  };
 
-var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
+  // (key:String, val:Boolean):void
+  this.writeMetaDataBoolean = function(key, val) {
+    var i;
+    prepareWrite(this, 2);
+    this.view.setUint16(this.position, key.length);
+    this.position += 2;
+    for (i = 0; i < key.length; i++) {
+      // if key.charCodeAt(i) >= 255, handle error
+      prepareWrite(this, 1);
+      this.bytes[this.position] = key.charCodeAt(i);
+      this.position++;
+    }
+    prepareWrite(this, 2);
+    this.view.setUint8(this.position, 0x01);
+    this.position++;
+    this.view.setUint8(this.position, val ? 0x01 : 0x00);
+    this.position++;
+    this.length = Math.max(this.length, this.position);
+    ++adHoc;
+  };
 
-function prependListener(emitter, event, fn) {
-  // Sadly this is not cacheable as some libraries bundle their own
-  // event emitter implementation with them.
-  if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn);
+  // ():ByteArray
+  this.finalize = function() {
+    var
+      dtsDelta, // :int
+      len; // :int
+
+    switch (this.bytes[0]) {
+      // Video Data
+    case FlvTag.VIDEO_TAG:
+       // We only support AVC, 1 = key frame (for AVC, a seekable
+       // frame), 2 = inter frame (for AVC, a non-seekable frame)
+      this.bytes[11] = ((this.keyFrame || extraData) ? 0x10 : 0x20) | 0x07;
+      this.bytes[12] = extraData ?  0x00 : 0x01;
+
+      dtsDelta = this.pts - this.dts;
+      this.bytes[13] = (dtsDelta & 0x00FF0000) >>> 16;
+      this.bytes[14] = (dtsDelta & 0x0000FF00) >>>  8;
+      this.bytes[15] = (dtsDelta & 0x000000FF) >>>  0;
+      break;
 
-  // This is a hack to make sure that our error handler is attached before any
-  // userland ones.  NEVER DO THIS. This is here only because this code needs
-  // to continue to work with older versions of Node.js that do not include
-  // the prependListener() method. The goal is to eventually remove this hack.
-  if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
-}
+    case FlvTag.AUDIO_TAG:
+      this.bytes[11] = 0xAF; // 44 kHz, 16-bit stereo
+      this.bytes[12] = extraData ? 0x00 : 0x01;
+      break;
 
-function ReadableState(options, stream) {
-  Duplex = Duplex || require('./_stream_duplex');
+    case FlvTag.METADATA_TAG:
+      this.position = 11;
+      this.view.setUint8(this.position, 0x02); // String type
+      this.position++;
+      this.view.setUint16(this.position, 0x0A); // 10 Bytes
+      this.position += 2;
+      // set "onMetaData"
+      this.bytes.set([0x6f, 0x6e, 0x4d, 0x65,
+                      0x74, 0x61, 0x44, 0x61,
+                      0x74, 0x61], this.position);
+      this.position += 10;
+      this.bytes[this.position] = 0x08; // Array type
+      this.position++;
+      this.view.setUint32(this.position, adHoc);
+      this.position = this.length;
+      this.bytes.set([0, 0, 9], this.position);
+      this.position += 3; // End Data Tag
+      this.length = this.position;
+      break;
+    }
 
-  options = options || {};
+    len = this.length - 11;
+
+    // write the DataSize field
+    this.bytes[ 1] = (len & 0x00FF0000) >>> 16;
+    this.bytes[ 2] = (len & 0x0000FF00) >>>  8;
+    this.bytes[ 3] = (len & 0x000000FF) >>>  0;
+    // write the Timestamp
+    this.bytes[ 4] = (this.dts & 0x00FF0000) >>> 16;
+    this.bytes[ 5] = (this.dts & 0x0000FF00) >>>  8;
+    this.bytes[ 6] = (this.dts & 0x000000FF) >>>  0;
+    this.bytes[ 7] = (this.dts & 0xFF000000) >>> 24;
+    // write the StreamID
+    this.bytes[ 8] = 0;
+    this.bytes[ 9] = 0;
+    this.bytes[10] = 0;
+
+    // Sometimes we're at the end of the view and have one slot to write a
+    // uint32, so, prepareWrite of count 4, since, view is uint8
+    prepareWrite(this, 4);
+    this.view.setUint32(this.length, this.length);
+    this.length += 4;
+    this.position += 4;
+
+    // trim down the byte buffer to what is actually being used
+    this.bytes = this.bytes.subarray(0, this.length);
+    this.frameTime = FlvTag.frameTime(this.bytes);
+    // if bytes.bytelength isn't equal to this.length, handle error
+    return this;
+  };
+};
 
-  // Duplex streams are both readable and writable, but share
-  // the same options object.
-  // However, some cases require setting options to different
-  // values for the readable and the writable sides of the duplex stream.
-  // These options can be provided separately as readableXXX and writableXXX.
-  var isDuplex = stream instanceof Duplex;
+FlvTag.AUDIO_TAG = 0x08; // == 8, :uint
+FlvTag.VIDEO_TAG = 0x09; // == 9, :uint
+FlvTag.METADATA_TAG = 0x12; // == 18, :uint
 
-  // object stream flag. Used to make read(n) ignore n and to
-  // make all the buffer merging and length checks go away
-  this.objectMode = !!options.objectMode;
+// (tag:ByteArray):Boolean {
+FlvTag.isAudioFrame = function(tag) {
+  return FlvTag.AUDIO_TAG === tag[0];
+};
 
-  if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
+// (tag:ByteArray):Boolean {
+FlvTag.isVideoFrame = function(tag) {
+  return FlvTag.VIDEO_TAG === tag[0];
+};
 
-  // the point at which it stops calling _read() to fill the buffer
-  // Note: 0 is a valid value, means "don't call _read preemptively ever"
-  var hwm = options.highWaterMark;
-  var readableHwm = options.readableHighWaterMark;
-  var defaultHwm = this.objectMode ? 16 : 16 * 1024;
+// (tag:ByteArray):Boolean {
+FlvTag.isMetaData = function(tag) {
+  return FlvTag.METADATA_TAG === tag[0];
+};
 
-  if (hwm || hwm === 0) this.highWaterMark = hwm;else if (isDuplex && (readableHwm || readableHwm === 0)) this.highWaterMark = readableHwm;else this.highWaterMark = defaultHwm;
+// (tag:ByteArray):Boolean {
+FlvTag.isKeyFrame = function(tag) {
+  if (FlvTag.isVideoFrame(tag)) {
+    return tag[11] === 0x17;
+  }
 
-  // cast to ints.
-  this.highWaterMark = Math.floor(this.highWaterMark);
+  if (FlvTag.isAudioFrame(tag)) {
+    return true;
+  }
 
-  // A linked list is used to store data chunks instead of an array because the
-  // linked list can remove elements from the beginning faster than
-  // array.shift()
-  this.buffer = new BufferList();
-  this.length = 0;
-  this.pipes = null;
-  this.pipesCount = 0;
-  this.flowing = null;
-  this.ended = false;
-  this.endEmitted = false;
-  this.reading = false;
+  if (FlvTag.isMetaData(tag)) {
+    return true;
+  }
 
-  // a flag to be able to tell if the event 'readable'/'data' is emitted
-  // immediately, or on a later tick.  We set this to true at first, because
-  // any actions that shouldn't happen until "later" should generally also
-  // not happen before the first read call.
-  this.sync = true;
+  return false;
+};
 
-  // whenever we return null, then we set a flag to say
-  // that we're awaiting a 'readable' event emission.
-  this.needReadable = false;
-  this.emittedReadable = false;
-  this.readableListening = false;
-  this.resumeScheduled = false;
+// (tag:ByteArray):uint {
+FlvTag.frameTime = function(tag) {
+  var pts = tag[ 4] << 16; // :uint
+  pts |= tag[ 5] <<  8;
+  pts |= tag[ 6] <<  0;
+  pts |= tag[ 7] << 24;
+  return pts;
+};
 
-  // has it been destroyed
-  this.destroyed = false;
+module.exports = FlvTag;
 
-  // Crypto is kind of old and crusty.  Historically, its default string
-  // encoding is 'binary' so we have to make this configurable.
-  // Everything else in the universe uses 'utf8', though.
-  this.defaultEncoding = options.defaultEncoding || 'utf8';
+},{}],30:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+module.exports = {
+  tag: require('./flv-tag'),
+  Transmuxer: require('./transmuxer'),
+  getFlvHeader: require('./flv-header')
+};
 
-  // the number of writers that are awaiting a drain event in .pipe()s
-  this.awaitDrain = 0;
+},{"./flv-header":28,"./flv-tag":29,"./transmuxer":32}],31:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
 
-  // if true, a maybeReadMore has been scheduled
-  this.readingMore = false;
+var TagList = function() {
+  var self = this;
 
-  this.decoder = null;
-  this.encoding = null;
-  if (options.encoding) {
-    if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
-    this.decoder = new StringDecoder(options.encoding);
-    this.encoding = options.encoding;
+  this.list = [];
+
+  this.push = function(tag) {
+    this.list.push({
+      bytes: tag.bytes,
+      dts: tag.dts,
+      pts: tag.pts,
+      keyFrame: tag.keyFrame,
+      metaDataTag: tag.metaDataTag
+    });
+  };
+
+  Object.defineProperty(this, 'length', {
+    get: function() {
+      return self.list.length;
+    }
+  });
+};
+
+module.exports = TagList;
+
+},{}],32:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
+
+var Stream = require('../utils/stream.js');
+var FlvTag = require('./flv-tag.js');
+var m2ts = require('../m2ts/m2ts.js');
+var AdtsStream = require('../codecs/adts.js');
+var H264Stream = require('../codecs/h264').H264Stream;
+var CoalesceStream = require('./coalesce-stream.js');
+var TagList = require('./tag-list.js');
+
+var
+  Transmuxer,
+  VideoSegmentStream,
+  AudioSegmentStream,
+  collectTimelineInfo,
+  metaDataTag,
+  extraDataTag;
+
+/**
+ * Store information about the start and end of the tracka and the
+ * duration for each frame/sample we process in order to calculate
+ * the baseMediaDecodeTime
+ */
+collectTimelineInfo = function(track, data) {
+  if (typeof data.pts === 'number') {
+    if (track.timelineStartInfo.pts === undefined) {
+      track.timelineStartInfo.pts = data.pts;
+    } else {
+      track.timelineStartInfo.pts =
+        Math.min(track.timelineStartInfo.pts, data.pts);
+    }
   }
-}
 
-function Readable(options) {
-  Duplex = Duplex || require('./_stream_duplex');
+  if (typeof data.dts === 'number') {
+    if (track.timelineStartInfo.dts === undefined) {
+      track.timelineStartInfo.dts = data.dts;
+    } else {
+      track.timelineStartInfo.dts =
+        Math.min(track.timelineStartInfo.dts, data.dts);
+    }
+  }
+};
 
-  if (!(this instanceof Readable)) return new Readable(options);
+metaDataTag = function(track, pts) {
+  var
+    tag = new FlvTag(FlvTag.METADATA_TAG); // :FlvTag
 
-  this._readableState = new ReadableState(options, this);
+  tag.dts = pts;
+  tag.pts = pts;
 
-  // legacy
-  this.readable = true;
+  tag.writeMetaDataDouble('videocodecid', 7);
+  tag.writeMetaDataDouble('width', track.width);
+  tag.writeMetaDataDouble('height', track.height);
 
-  if (options) {
-    if (typeof options.read === 'function') this._read = options.read;
+  return tag;
+};
 
-    if (typeof options.destroy === 'function') this._destroy = options.destroy;
+extraDataTag = function(track, pts) {
+  var
+    i,
+    tag = new FlvTag(FlvTag.VIDEO_TAG, true);
+
+  tag.dts = pts;
+  tag.pts = pts;
+
+  tag.writeByte(0x01);// version
+  tag.writeByte(track.profileIdc);// profile
+  tag.writeByte(track.profileCompatibility);// compatibility
+  tag.writeByte(track.levelIdc);// level
+  tag.writeByte(0xFC | 0x03); // reserved (6 bits), NULA length size - 1 (2 bits)
+  tag.writeByte(0xE0 | 0x01); // reserved (3 bits), num of SPS (5 bits)
+  tag.writeShort(track.sps[0].length); // data of SPS
+  tag.writeBytes(track.sps[0]); // SPS
+
+  tag.writeByte(track.pps.length); // num of PPS (will there ever be more that 1 PPS?)
+  for (i = 0; i < track.pps.length; ++i) {
+    tag.writeShort(track.pps[i].length); // 2 bytes for length of PPS
+    tag.writeBytes(track.pps[i]); // data of PPS
   }
 
-  Stream.call(this);
-}
+  return tag;
+};
 
-Object.defineProperty(Readable.prototype, 'destroyed', {
-  get: function () {
-    if (this._readableState === undefined) {
-      return false;
+/**
+ * Constructs a single-track, media segment from AAC data
+ * events. The output of this stream can be fed to flash.
+ */
+AudioSegmentStream = function(track) {
+  var
+    adtsFrames = [],
+    videoKeyFrames = [],
+    oldExtraData;
+
+  AudioSegmentStream.prototype.init.call(this);
+
+  this.push = function(data) {
+    collectTimelineInfo(track, data);
+
+    if (track) {
+      track.audioobjecttype = data.audioobjecttype;
+      track.channelcount = data.channelcount;
+      track.samplerate = data.samplerate;
+      track.samplingfrequencyindex = data.samplingfrequencyindex;
+      track.samplesize = data.samplesize;
+      track.extraData = (track.audioobjecttype << 11) |
+                        (track.samplingfrequencyindex << 7) |
+                        (track.channelcount << 3);
     }
-    return this._readableState.destroyed;
-  },
-  set: function (value) {
-    // we ignore the value if the stream
-    // has not been initialized yet
-    if (!this._readableState) {
+
+    data.pts = Math.round(data.pts / 90);
+    data.dts = Math.round(data.dts / 90);
+
+    // buffer audio data until end() is called
+    adtsFrames.push(data);
+  };
+
+  this.flush = function() {
+    var currentFrame, adtsFrame, lastMetaPts, tags = new TagList();
+    // return early if no audio data has been observed
+    if (adtsFrames.length === 0) {
+      this.trigger('done', 'AudioSegmentStream');
       return;
     }
 
-    // backward compatibility, the user is explicitly
-    // managing destroyed
-    this._readableState.destroyed = value;
-  }
-});
+    lastMetaPts = -Infinity;
 
-Readable.prototype.destroy = destroyImpl.destroy;
-Readable.prototype._undestroy = destroyImpl.undestroy;
-Readable.prototype._destroy = function (err, cb) {
-  this.push(null);
-  cb(err);
-};
+    while (adtsFrames.length) {
+      currentFrame = adtsFrames.shift();
 
-// Manually shove something into the read() buffer.
-// This returns true if the highWaterMark has not been hit yet,
-// similar to how Writable.write() returns true if you should
-// write() some more.
-Readable.prototype.push = function (chunk, encoding) {
-  var state = this._readableState;
-  var skipChunkCheck;
+      // write out a metadata frame at every video key frame
+      if (videoKeyFrames.length && currentFrame.pts >= videoKeyFrames[0]) {
+        lastMetaPts = videoKeyFrames.shift();
+        this.writeMetaDataTags(tags, lastMetaPts);
+      }
 
-  if (!state.objectMode) {
-    if (typeof chunk === 'string') {
-      encoding = encoding || state.defaultEncoding;
-      if (encoding !== state.encoding) {
-        chunk = Buffer.from(chunk, encoding);
-        encoding = '';
+      // also write out metadata tags every 1 second so that the decoder
+      // is re-initialized quickly after seeking into a different
+      // audio configuration.
+      if (track.extraData !== oldExtraData || currentFrame.pts - lastMetaPts >= 1000) {
+        this.writeMetaDataTags(tags, currentFrame.pts);
+        oldExtraData = track.extraData;
+        lastMetaPts = currentFrame.pts;
       }
-      skipChunkCheck = true;
+
+      adtsFrame = new FlvTag(FlvTag.AUDIO_TAG);
+      adtsFrame.pts = currentFrame.pts;
+      adtsFrame.dts = currentFrame.dts;
+
+      adtsFrame.writeBytes(currentFrame.data);
+
+      tags.push(adtsFrame.finalize());
     }
-  } else {
-    skipChunkCheck = true;
-  }
 
-  return readableAddChunk(this, chunk, encoding, false, skipChunkCheck);
-};
+    videoKeyFrames.length = 0;
+    oldExtraData = null;
+    this.trigger('data', {track: track, tags: tags.list});
 
-// Unshift should *always* be something directly out of read()
-Readable.prototype.unshift = function (chunk) {
-  return readableAddChunk(this, chunk, null, true, false);
+    this.trigger('done', 'AudioSegmentStream');
+  };
+
+  this.writeMetaDataTags = function(tags, pts) {
+    var adtsFrame;
+
+    adtsFrame = new FlvTag(FlvTag.METADATA_TAG);
+    // For audio, DTS is always the same as PTS. We want to set the DTS
+    // however so we can compare with video DTS to determine approximate
+    // packet order
+    adtsFrame.pts = pts;
+    adtsFrame.dts = pts;
+
+    // AAC is always 10
+    adtsFrame.writeMetaDataDouble('audiocodecid', 10);
+    adtsFrame.writeMetaDataBoolean('stereo', track.channelcount === 2);
+    adtsFrame.writeMetaDataDouble('audiosamplerate', track.samplerate);
+    // Is AAC always 16 bit?
+    adtsFrame.writeMetaDataDouble('audiosamplesize', 16);
+
+    tags.push(adtsFrame.finalize());
+
+    adtsFrame = new FlvTag(FlvTag.AUDIO_TAG, true);
+    // For audio, DTS is always the same as PTS. We want to set the DTS
+    // however so we can compare with video DTS to determine approximate
+    // packet order
+    adtsFrame.pts = pts;
+    adtsFrame.dts = pts;
+
+    adtsFrame.view.setUint16(adtsFrame.position, track.extraData);
+    adtsFrame.position += 2;
+    adtsFrame.length = Math.max(adtsFrame.length, adtsFrame.position);
+
+    tags.push(adtsFrame.finalize());
+  };
+
+  this.onVideoKeyFrame = function(pts) {
+    videoKeyFrames.push(pts);
+  };
 };
+AudioSegmentStream.prototype = new Stream();
 
-function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
-  var state = stream._readableState;
-  if (chunk === null) {
-    state.reading = false;
-    onEofChunk(stream, state);
-  } else {
-    var er;
-    if (!skipChunkCheck) er = chunkInvalid(state, chunk);
-    if (er) {
-      stream.emit('error', er);
-    } else if (state.objectMode || chunk && chunk.length > 0) {
-      if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
-        chunk = _uint8ArrayToBuffer(chunk);
+/**
+ * Store FlvTags for the h264 stream
+ * @param track {object} track metadata configuration
+ */
+VideoSegmentStream = function(track) {
+  var
+    nalUnits = [],
+    config,
+    h264Frame;
+  VideoSegmentStream.prototype.init.call(this);
+
+  this.finishFrame = function(tags, frame) {
+    if (!frame) {
+      return;
+    }
+    // Check if keyframe and the length of tags.
+    // This makes sure we write metadata on the first frame of a segment.
+    if (config && track && track.newMetadata &&
+        (frame.keyFrame || tags.length === 0)) {
+      // Push extra data on every IDR frame in case we did a stream change + seek
+      var metaTag = metaDataTag(config, frame.dts).finalize();
+      var extraTag = extraDataTag(track, frame.dts).finalize();
+
+      metaTag.metaDataTag = extraTag.metaDataTag = true;
+
+      tags.push(metaTag);
+      tags.push(extraTag);
+      track.newMetadata = false;
+
+      this.trigger('keyframe', frame.dts);
+    }
+
+    frame.endNalUnit();
+    tags.push(frame.finalize());
+    h264Frame = null;
+  };
+
+  this.push = function(data) {
+    collectTimelineInfo(track, data);
+
+    data.pts = Math.round(data.pts / 90);
+    data.dts = Math.round(data.dts / 90);
+
+    // buffer video until flush() is called
+    nalUnits.push(data);
+  };
+
+  this.flush = function() {
+    var
+      currentNal,
+      tags = new TagList();
+
+    // Throw away nalUnits at the start of the byte stream until we find
+    // the first AUD
+    while (nalUnits.length) {
+      if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
+        break;
       }
+      nalUnits.shift();
+    }
 
-      if (addToFront) {
-        if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event'));else addChunk(stream, state, chunk, true);
-      } else if (state.ended) {
-        stream.emit('error', new Error('stream.push() after EOF'));
+    // return early if no video data has been observed
+    if (nalUnits.length === 0) {
+      this.trigger('done', 'VideoSegmentStream');
+      return;
+    }
+
+    while (nalUnits.length) {
+      currentNal = nalUnits.shift();
+
+      // record the track config
+      if (currentNal.nalUnitType === 'seq_parameter_set_rbsp') {
+        track.newMetadata = true;
+        config = currentNal.config;
+        track.width = config.width;
+        track.height = config.height;
+        track.sps = [currentNal.data];
+        track.profileIdc = config.profileIdc;
+        track.levelIdc = config.levelIdc;
+        track.profileCompatibility = config.profileCompatibility;
+        h264Frame.endNalUnit();
+      } else if (currentNal.nalUnitType === 'pic_parameter_set_rbsp') {
+        track.newMetadata = true;
+        track.pps = [currentNal.data];
+        h264Frame.endNalUnit();
+      } else if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
+        if (h264Frame) {
+          this.finishFrame(tags, h264Frame);
+        }
+        h264Frame = new FlvTag(FlvTag.VIDEO_TAG);
+        h264Frame.pts = currentNal.pts;
+        h264Frame.dts = currentNal.dts;
       } else {
-        state.reading = false;
-        if (state.decoder && !encoding) {
-          chunk = state.decoder.write(chunk);
-          if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
-        } else {
-          addChunk(stream, state, chunk, false);
+        if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
+          // the current sample is a key frame
+          h264Frame.keyFrame = true;
         }
+        h264Frame.endNalUnit();
       }
-    } else if (!addToFront) {
-      state.reading = false;
+      h264Frame.startNalUnit();
+      h264Frame.writeBytes(currentNal.data);
+    }
+    if (h264Frame) {
+      this.finishFrame(tags, h264Frame);
     }
-  }
 
-  return needMoreData(state);
-}
+    this.trigger('data', {track: track, tags: tags.list});
 
-function addChunk(stream, state, chunk, addToFront) {
-  if (state.flowing && state.length === 0 && !state.sync) {
-    stream.emit('data', chunk);
-    stream.read(0);
-  } else {
-    // update the buffer info.
-    state.length += state.objectMode ? 1 : chunk.length;
-    if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
+    // Continue with the flush process now
+    this.trigger('done', 'VideoSegmentStream');
+  };
+};
 
-    if (state.needReadable) emitReadable(stream);
-  }
-  maybeReadMore(stream, state);
-}
+VideoSegmentStream.prototype = new Stream();
 
-function chunkInvalid(state, chunk) {
-  var er;
-  if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
-    er = new TypeError('Invalid non-string/buffer chunk');
-  }
-  return er;
-}
+/**
+ * An object that incrementally transmuxes MPEG2 Trasport Stream
+ * chunks into an FLV.
+ */
+Transmuxer = function(options) {
+  var
+    self = this,
 
-// if it's past the high water mark, we can push in some more.
-// Also, if we have no data yet, we can stand some
-// more bytes.  This is to work around cases where hwm=0,
-// such as the repl.  Also, if the push() triggered a
-// readable event, and the user called read(largeNumber) such that
-// needReadable was set, then we ought to push more, so that another
-// 'readable' event will be triggered.
-function needMoreData(state) {
-  return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
-}
+    packetStream, parseStream, elementaryStream,
+    videoTimestampRolloverStream, audioTimestampRolloverStream,
+    timedMetadataTimestampRolloverStream,
+    adtsStream, h264Stream,
+    videoSegmentStream, audioSegmentStream, captionStream,
+    coalesceStream;
 
-Readable.prototype.isPaused = function () {
-  return this._readableState.flowing === false;
-};
+  Transmuxer.prototype.init.call(this);
 
-// backwards compatibility.
-Readable.prototype.setEncoding = function (enc) {
-  if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
-  this._readableState.decoder = new StringDecoder(enc);
-  this._readableState.encoding = enc;
-  return this;
-};
+  options = options || {};
 
-// Don't raise the hwm > 8MB
-var MAX_HWM = 0x800000;
-function computeNewHighWaterMark(n) {
-  if (n >= MAX_HWM) {
-    n = MAX_HWM;
-  } else {
-    // Get the next highest power of 2 to prevent increasing hwm excessively in
-    // tiny amounts
-    n--;
-    n |= n >>> 1;
-    n |= n >>> 2;
-    n |= n >>> 4;
-    n |= n >>> 8;
-    n |= n >>> 16;
-    n++;
-  }
-  return n;
-}
+  // expose the metadata stream
+  this.metadataStream = new m2ts.MetadataStream();
+
+  options.metadataStream = this.metadataStream;
+
+  // set up the parsing pipeline
+  packetStream = new m2ts.TransportPacketStream();
+  parseStream = new m2ts.TransportParseStream();
+  elementaryStream = new m2ts.ElementaryStream();
+  videoTimestampRolloverStream = new m2ts.TimestampRolloverStream('video');
+  audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
+  timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
+
+  adtsStream = new AdtsStream();
+  h264Stream = new H264Stream();
+  coalesceStream = new CoalesceStream(options);
+
+  // disassemble MPEG2-TS packets into elementary streams
+  packetStream
+    .pipe(parseStream)
+    .pipe(elementaryStream);
+
+  // !!THIS ORDER IS IMPORTANT!!
+  // demux the streams
+  elementaryStream
+    .pipe(videoTimestampRolloverStream)
+    .pipe(h264Stream);
+  elementaryStream
+    .pipe(audioTimestampRolloverStream)
+    .pipe(adtsStream);
+
+  elementaryStream
+    .pipe(timedMetadataTimestampRolloverStream)
+    .pipe(this.metadataStream)
+    .pipe(coalesceStream);
+  // if CEA-708 parsing is available, hook up a caption stream
+  captionStream = new m2ts.CaptionStream();
+  h264Stream.pipe(captionStream)
+    .pipe(coalesceStream);
+
+  // hook up the segment streams once track metadata is delivered
+  elementaryStream.on('data', function(data) {
+    var i, videoTrack, audioTrack;
+
+    if (data.type === 'metadata') {
+      i = data.tracks.length;
+
+      // scan the tracks listed in the metadata
+      while (i--) {
+        if (data.tracks[i].type === 'video') {
+          videoTrack = data.tracks[i];
+        } else if (data.tracks[i].type === 'audio') {
+          audioTrack = data.tracks[i];
+        }
+      }
 
-// This function is designed to be inlinable, so please take care when making
-// changes to the function body.
-function howMuchToRead(n, state) {
-  if (n <= 0 || state.length === 0 && state.ended) return 0;
-  if (state.objectMode) return 1;
-  if (n !== n) {
-    // Only flow one buffer at a time
-    if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
-  }
-  // If we're asking for more than the current hwm, then raise the hwm.
-  if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
-  if (n <= state.length) return n;
-  // Don't have enough
-  if (!state.ended) {
-    state.needReadable = true;
-    return 0;
-  }
-  return state.length;
-}
+      // hook up the video segment stream to the first track with h264 data
+      if (videoTrack && !videoSegmentStream) {
+        coalesceStream.numberOfTracks++;
+        videoSegmentStream = new VideoSegmentStream(videoTrack);
 
-// you can override either this method, or the async _read(n) below.
-Readable.prototype.read = function (n) {
-  debug('read', n);
-  n = parseInt(n, 10);
-  var state = this._readableState;
-  var nOrig = n;
+        // Set up the final part of the video pipeline
+        h264Stream
+          .pipe(videoSegmentStream)
+          .pipe(coalesceStream);
+      }
 
-  if (n !== 0) state.emittedReadable = false;
+      if (audioTrack && !audioSegmentStream) {
+        // hook up the audio segment stream to the first track with aac data
+        coalesceStream.numberOfTracks++;
+        audioSegmentStream = new AudioSegmentStream(audioTrack);
 
-  // if we're doing read(0) to trigger a readable event, but we
-  // already have a bunch of data in the buffer, then just trigger
-  // the 'readable' event and move on.
-  if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
-    debug('read: emitReadable', state.length, state.ended);
-    if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
-    return null;
-  }
+        // Set up the final part of the audio pipeline
+        adtsStream
+          .pipe(audioSegmentStream)
+          .pipe(coalesceStream);
 
-  n = howMuchToRead(n, state);
+        if (videoSegmentStream) {
+          videoSegmentStream.on('keyframe', audioSegmentStream.onVideoKeyFrame);
+        }
+      }
+    }
+  });
 
-  // if we've ended, and we're now clear, then finish it up.
-  if (n === 0 && state.ended) {
-    if (state.length === 0) endReadable(this);
-    return null;
-  }
+  // feed incoming data to the front of the parsing pipeline
+  this.push = function(data) {
+    packetStream.push(data);
+  };
 
-  // All the actual chunk generation logic needs to be
-  // *below* the call to _read.  The reason is that in certain
-  // synthetic stream cases, such as passthrough streams, _read
-  // may be a completely synchronous operation which may change
-  // the state of the read buffer, providing enough data when
-  // before there was *not* enough.
-  //
-  // So, the steps are:
-  // 1. Figure out what the state of things will be after we do
-  // a read from the buffer.
-  //
-  // 2. If that resulting state will trigger a _read, then call _read.
-  // Note that this may be asynchronous, or synchronous.  Yes, it is
-  // deeply ugly to write APIs this way, but that still doesn't mean
-  // that the Readable class should behave improperly, as streams are
-  // designed to be sync/async agnostic.
-  // Take note if the _read call is sync or async (ie, if the read call
-  // has returned yet), so that we know whether or not it's safe to emit
-  // 'readable' etc.
-  //
-  // 3. Actually pull the requested chunks out of the buffer and return.
+  // flush any buffered data
+  this.flush = function() {
+    // Start at the top of the pipeline and flush all pending work
+    packetStream.flush();
+  };
 
-  // if we need a readable event, then we need to do some reading.
-  var doRead = state.needReadable;
-  debug('need readable', doRead);
+  // Caption data has to be reset when seeking outside buffered range
+  this.resetCaptions = function() {
+    captionStream.reset();
+  };
 
-  // if we currently have less than the highWaterMark, then also read some
-  if (state.length === 0 || state.length - n < state.highWaterMark) {
-    doRead = true;
-    debug('length less than watermark', doRead);
-  }
+  // Re-emit any data coming from the coalesce stream to the outside world
+  coalesceStream.on('data', function(event) {
+    self.trigger('data', event);
+  });
 
-  // however, if we've ended, then there's no point, and if we're already
-  // reading, then it's unnecessary.
-  if (state.ended || state.reading) {
-    doRead = false;
-    debug('reading or ended', doRead);
-  } else if (doRead) {
-    debug('do read');
-    state.reading = true;
-    state.sync = true;
-    // if the length is currently zero, then we *need* a readable event.
-    if (state.length === 0) state.needReadable = true;
-    // call internal read method
-    this._read(state.highWaterMark);
-    state.sync = false;
-    // If _read pushed data synchronously, then `reading` will be false,
-    // and we need to re-evaluate how much data we can return to the user.
-    if (!state.reading) n = howMuchToRead(nOrig, state);
-  }
+  // Let the consumer know we have finished flushing the entire pipeline
+  coalesceStream.on('done', function() {
+    self.trigger('done');
+  });
+};
+Transmuxer.prototype = new Stream();
 
-  var ret;
-  if (n > 0) ret = fromList(n, state);else ret = null;
+// forward compatibility
+module.exports = Transmuxer;
 
-  if (ret === null) {
-    state.needReadable = true;
-    n = 0;
-  } else {
-    state.length -= n;
-  }
+},{"../codecs/adts.js":21,"../codecs/h264":22,"../m2ts/m2ts.js":36,"../utils/stream.js":60,"./coalesce-stream.js":27,"./flv-tag.js":29,"./tag-list.js":31}],33:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
 
-  if (state.length === 0) {
-    // If we have nothing in the buffer, then we want to know
-    // as soon as we *do* get something into the buffer.
-    if (!state.ended) state.needReadable = true;
+var muxjs = {
+  codecs: require('./codecs'),
+  mp4: require('./mp4'),
+  flv: require('./flv'),
+  mp2t: require('./m2ts'),
+  partial: require('./partial')
+};
 
-    // If we tried to read() past the EOF, then emit end on the next tick.
-    if (nOrig !== n && state.ended) endReadable(this);
-  }
+// include all the tools when the full library is required
+muxjs.mp4.tools = require('./tools/mp4-inspector');
+muxjs.flv.tools = require('./tools/flv-inspector');
+muxjs.mp2t.tools = require('./tools/ts-inspector');
 
-  if (ret !== null) this.emit('data', ret);
 
-  return ret;
-};
+module.exports = muxjs;
 
-function onEofChunk(stream, state) {
-  if (state.ended) return;
-  if (state.decoder) {
-    var chunk = state.decoder.end();
-    if (chunk && chunk.length) {
-      state.buffer.push(chunk);
-      state.length += state.objectMode ? 1 : chunk.length;
-    }
-  }
-  state.ended = true;
+},{"./codecs":23,"./flv":30,"./m2ts":35,"./mp4":44,"./partial":50,"./tools/flv-inspector":54,"./tools/mp4-inspector":55,"./tools/ts-inspector":56}],34:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Reads in-band caption information from a video elementary
+ * stream. Captions must follow the CEA-708 standard for injection
+ * into an MPEG-2 transport streams.
+ * @see https://en.wikipedia.org/wiki/CEA-708
+ * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
+ */
 
-  // emit 'readable' now to make sure it gets picked up.
-  emitReadable(stream);
-}
+'use strict';
 
-// Don't emit readable right away in sync mode, because this can trigger
-// another read() call => stack overflow.  This way, it might trigger
-// a nextTick recursion warning, but that's not so bad.
-function emitReadable(stream) {
-  var state = stream._readableState;
-  state.needReadable = false;
-  if (!state.emittedReadable) {
-    debug('emitReadable', state.flowing);
-    state.emittedReadable = true;
-    if (state.sync) pna.nextTick(emitReadable_, stream);else emitReadable_(stream);
-  }
-}
+// -----------------
+// Link To Transport
+// -----------------
 
-function emitReadable_(stream) {
-  debug('emit readable');
-  stream.emit('readable');
-  flow(stream);
-}
+var Stream = require('../utils/stream');
+var cea708Parser = require('../tools/caption-packet-parser');
 
-// at this point, the user has presumably seen the 'readable' event,
-// and called read() to consume some data.  that may have triggered
-// in turn another _read(n) call, in which case reading = true if
-// it's in progress.
-// However, if we're not ended, or reading, and the length < hwm,
-// then go ahead and try to read some more preemptively.
-function maybeReadMore(stream, state) {
-  if (!state.readingMore) {
-    state.readingMore = true;
-    pna.nextTick(maybeReadMore_, stream, state);
-  }
-}
+var CaptionStream = function() {
 
-function maybeReadMore_(stream, state) {
-  var len = state.length;
-  while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
-    debug('maybeReadMore read 0');
-    stream.read(0);
-    if (len === state.length)
-      // didn't get any data, stop spinning.
-      break;else len = state.length;
-  }
-  state.readingMore = false;
-}
+  CaptionStream.prototype.init.call(this);
 
-// abstract method.  to be overridden in specific implementation classes.
-// call cb(er, data) where data is <= n in length.
-// for virtual (non-string, non-buffer) streams, "length" is somewhat
-// arbitrary, and perhaps not very meaningful.
-Readable.prototype._read = function (n) {
-  this.emit('error', new Error('_read() is not implemented'));
-};
+  this.captionPackets_ = [];
 
-Readable.prototype.pipe = function (dest, pipeOpts) {
-  var src = this;
-  var state = this._readableState;
+  this.ccStreams_ = [
+    new Cea608Stream(0, 0), // eslint-disable-line no-use-before-define
+    new Cea608Stream(0, 1), // eslint-disable-line no-use-before-define
+    new Cea608Stream(1, 0), // eslint-disable-line no-use-before-define
+    new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
+  ];
 
-  switch (state.pipesCount) {
-    case 0:
-      state.pipes = dest;
-      break;
-    case 1:
-      state.pipes = [state.pipes, dest];
-      break;
-    default:
-      state.pipes.push(dest);
-      break;
-  }
-  state.pipesCount += 1;
-  debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
+  this.reset();
 
-  var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
+  // forward data and done events from CCs to this CaptionStream
+  this.ccStreams_.forEach(function(cc) {
+    cc.on('data', this.trigger.bind(this, 'data'));
+    cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
+    cc.on('done', this.trigger.bind(this, 'done'));
+  }, this);
 
-  var endFn = doEnd ? onend : unpipe;
-  if (state.endEmitted) pna.nextTick(endFn);else src.once('end', endFn);
+};
 
-  dest.on('unpipe', onunpipe);
-  function onunpipe(readable, unpipeInfo) {
-    debug('onunpipe');
-    if (readable === src) {
-      if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
-        unpipeInfo.hasUnpiped = true;
-        cleanup();
-      }
-    }
-  }
+CaptionStream.prototype = new Stream();
+CaptionStream.prototype.push = function(event) {
+  var sei, userData, newCaptionPackets;
 
-  function onend() {
-    debug('onend');
-    dest.end();
+  // only examine SEI NALs
+  if (event.nalUnitType !== 'sei_rbsp') {
+    return;
   }
 
-  // when the dest drains, it reduces the awaitDrain counter
-  // on the source.  This would be more elegant with a .once()
-  // handler in flow(), but adding and removing repeatedly is
-  // too slow.
-  var ondrain = pipeOnDrain(src);
-  dest.on('drain', ondrain);
+  // parse the sei
+  sei = cea708Parser.parseSei(event.escapedRBSP);
 
-  var cleanedUp = false;
-  function cleanup() {
-    debug('cleanup');
-    // cleanup event handlers once the pipe is broken
-    dest.removeListener('close', onclose);
-    dest.removeListener('finish', onfinish);
-    dest.removeListener('drain', ondrain);
-    dest.removeListener('error', onerror);
-    dest.removeListener('unpipe', onunpipe);
-    src.removeListener('end', onend);
-    src.removeListener('end', unpipe);
-    src.removeListener('data', ondata);
+  // ignore everything but user_data_registered_itu_t_t35
+  if (sei.payloadType !== cea708Parser.USER_DATA_REGISTERED_ITU_T_T35) {
+    return;
+  }
 
-    cleanedUp = true;
+  // parse out the user data payload
+  userData = cea708Parser.parseUserData(sei);
 
-    // if the reader is waiting for a drain event from this
-    // specific writer, then it would cause it to never start
-    // flowing again.
-    // So, if this is awaiting a drain, then we just call it now.
-    // If we don't know, then assume that we are waiting for one.
-    if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
+  // ignore unrecognized userData
+  if (!userData) {
+    return;
   }
 
-  // If the user pushes more data while we're writing to dest then we'll end up
-  // in ondata again. However, we only want to increase awaitDrain once because
-  // dest will only emit one 'drain' event for the multiple writes.
-  // => Introduce a guard on increasing awaitDrain.
-  var increasedAwaitDrain = false;
-  src.on('data', ondata);
-  function ondata(chunk) {
-    debug('ondata');
-    increasedAwaitDrain = false;
-    var ret = dest.write(chunk);
-    if (false === ret && !increasedAwaitDrain) {
-      // If the user unpiped during `dest.write()`, it is possible
-      // to get stuck in a permanently paused state if that write
-      // also returned false.
-      // => Check whether `dest` is still a piping destination.
-      if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
-        debug('false write response, pause', src._readableState.awaitDrain);
-        src._readableState.awaitDrain++;
-        increasedAwaitDrain = true;
-      }
-      src.pause();
+  // Sometimes, the same segment # will be downloaded twice. To stop the
+  // caption data from being processed twice, we track the latest dts we've
+  // received and ignore everything with a dts before that. However, since
+  // data for a specific dts can be split across packets on either side of
+  // a segment boundary, we need to make sure we *don't* ignore the packets
+  // from the *next* segment that have dts === this.latestDts_. By constantly
+  // tracking the number of packets received with dts === this.latestDts_, we
+  // know how many should be ignored once we start receiving duplicates.
+  if (event.dts < this.latestDts_) {
+    // We've started getting older data, so set the flag.
+    this.ignoreNextEqualDts_ = true;
+    return;
+  } else if ((event.dts === this.latestDts_) && (this.ignoreNextEqualDts_)) {
+    this.numSameDts_--;
+    if (!this.numSameDts_) {
+      // We've received the last duplicate packet, time to start processing again
+      this.ignoreNextEqualDts_ = false;
     }
+    return;
   }
 
-  // if the dest has an error, then stop piping into it.
-  // however, don't suppress the throwing behavior for this.
-  function onerror(er) {
-    debug('onerror', er);
-    unpipe();
-    dest.removeListener('error', onerror);
-    if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
+  // parse out CC data packets and save them for later
+  newCaptionPackets = cea708Parser.parseCaptionPackets(event.pts, userData);
+  this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
+  if (this.latestDts_ !== event.dts) {
+    this.numSameDts_ = 0;
   }
+  this.numSameDts_++;
+  this.latestDts_ = event.dts;
+};
 
-  // Make sure our error handler is attached before userland ones.
-  prependListener(dest, 'error', onerror);
+CaptionStream.prototype.flushCCStreams = function(flushType) {
+  this.ccStreams_.forEach(function(cc) {
+    return flushType === 'flush' ? cc.flush() : cc.partialFlush();
+  }, this);
+};
 
-  // Both close and finish should trigger unpipe, but only once.
-  function onclose() {
-    dest.removeListener('finish', onfinish);
-    unpipe();
-  }
-  dest.once('close', onclose);
-  function onfinish() {
-    debug('onfinish');
-    dest.removeListener('close', onclose);
-    unpipe();
+CaptionStream.prototype.flushStream = function(flushType) {
+  // make sure we actually parsed captions before proceeding
+  if (!this.captionPackets_.length) {
+    this.flushCCStreams(flushType);
+    return;
   }
-  dest.once('finish', onfinish);
 
-  function unpipe() {
-    debug('unpipe');
-    src.unpipe(dest);
-  }
+  // In Chrome, the Array#sort function is not stable so add a
+  // presortIndex that we can use to ensure we get a stable-sort
+  this.captionPackets_.forEach(function(elem, idx) {
+    elem.presortIndex = idx;
+  });
 
-  // tell the dest that it's being piped to
-  dest.emit('pipe', src);
+  // sort caption byte-pairs based on their PTS values
+  this.captionPackets_.sort(function(a, b) {
+    if (a.pts === b.pts) {
+      return a.presortIndex - b.presortIndex;
+    }
+    return a.pts - b.pts;
+  });
 
-  // start the flow if it hasn't been started already.
-  if (!state.flowing) {
-    debug('pipe resume');
-    src.resume();
-  }
+  this.captionPackets_.forEach(function(packet) {
+    if (packet.type < 2) {
+      // Dispatch packet to the right Cea608Stream
+      this.dispatchCea608Packet(packet);
+    }
+    // this is where an 'else' would go for a dispatching packets
+    // to a theoretical Cea708Stream that handles SERVICEn data
+  }, this);
 
-  return dest;
+  this.captionPackets_.length = 0;
+  this.flushCCStreams(flushType);
 };
 
-function pipeOnDrain(src) {
-  return function () {
-    var state = src._readableState;
-    debug('pipeOnDrain', state.awaitDrain);
-    if (state.awaitDrain) state.awaitDrain--;
-    if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
-      state.flowing = true;
-      flow(src);
-    }
-  };
-}
+CaptionStream.prototype.flush = function() {
+  return this.flushStream('flush');
+};
 
-Readable.prototype.unpipe = function (dest) {
-  var state = this._readableState;
-  var unpipeInfo = { hasUnpiped: false };
+// Only called if handling partial data
+CaptionStream.prototype.partialFlush = function() {
+  return this.flushStream('partialFlush');
+};
 
-  // if we're not piping anywhere, then do nothing.
-  if (state.pipesCount === 0) return this;
+CaptionStream.prototype.reset = function() {
+  this.latestDts_ = null;
+  this.ignoreNextEqualDts_ = false;
+  this.numSameDts_ = 0;
+  this.activeCea608Channel_ = [null, null];
+  this.ccStreams_.forEach(function(ccStream) {
+    ccStream.reset();
+  });
+};
 
-  // just one destination.  most common case.
-  if (state.pipesCount === 1) {
-    // passed in one, but it's not the right one.
-    if (dest && dest !== state.pipes) return this;
+// From the CEA-608 spec:
+/*
+ * When XDS sub-packets are interleaved with other services, the end of each sub-packet shall be followed
+ * by a control pair to change to a different service. When any of the control codes from 0x10 to 0x1F is
+ * used to begin a control code pair, it indicates the return to captioning or Text data. The control code pair
+ * and subsequent data should then be processed according to the FCC rules. It may be necessary for the
+ * line 21 data encoder to automatically insert a control code pair (i.e. RCL, RU2, RU3, RU4, RDC, or RTD)
+ * to switch to captioning or Text.
+*/
+// With that in mind, we ignore any data between an XDS control code and a
+// subsequent closed-captioning control code.
+CaptionStream.prototype.dispatchCea608Packet = function(packet) {
+  // NOTE: packet.type is the CEA608 field
+  if (this.setsTextOrXDSActive(packet)) {
+    this.activeCea608Channel_[packet.type] = null;
+  } else if (this.setsChannel1Active(packet)) {
+    this.activeCea608Channel_[packet.type] = 0;
+  } else if (this.setsChannel2Active(packet)) {
+    this.activeCea608Channel_[packet.type] = 1;
+  }
+  if (this.activeCea608Channel_[packet.type] === null) {
+    // If we haven't received anything to set the active channel, or the
+    // packets are Text/XDS data, discard the data; we don't want jumbled
+    // captions
+    return;
+  }
+  this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
+};
 
-    if (!dest) dest = state.pipes;
+CaptionStream.prototype.setsChannel1Active = function(packet) {
+  return ((packet.ccData & 0x7800) === 0x1000);
+};
+CaptionStream.prototype.setsChannel2Active = function(packet) {
+  return ((packet.ccData & 0x7800) === 0x1800);
+};
+CaptionStream.prototype.setsTextOrXDSActive = function(packet) {
+  return ((packet.ccData & 0x7100) === 0x0100) ||
+    ((packet.ccData & 0x78fe) === 0x102a) ||
+    ((packet.ccData & 0x78fe) === 0x182a);
+};
 
-    // got a match.
-    state.pipes = null;
-    state.pipesCount = 0;
-    state.flowing = false;
-    if (dest) dest.emit('unpipe', this, unpipeInfo);
-    return this;
+// ----------------------
+// Session to Application
+// ----------------------
+
+// This hash maps non-ASCII, special, and extended character codes to their
+// proper Unicode equivalent. The first keys that are only a single byte
+// are the non-standard ASCII characters, which simply map the CEA608 byte
+// to the standard ASCII/Unicode. The two-byte keys that follow are the CEA608
+// character codes, but have their MSB bitmasked with 0x03 so that a lookup
+// can be performed regardless of the field and data channel on which the
+// character code was received.
+var CHARACTER_TRANSLATION = {
+  0x2a: 0xe1,     // á
+  0x5c: 0xe9,     // é
+  0x5e: 0xed,     // í
+  0x5f: 0xf3,     // ó
+  0x60: 0xfa,     // ú
+  0x7b: 0xe7,     // ç
+  0x7c: 0xf7,     // ÷
+  0x7d: 0xd1,     // Ñ
+  0x7e: 0xf1,     // ñ
+  0x7f: 0x2588,   // â–ˆ
+  0x0130: 0xae,   // ®
+  0x0131: 0xb0,   // °
+  0x0132: 0xbd,   // ½
+  0x0133: 0xbf,   // ¿
+  0x0134: 0x2122, // â„¢
+  0x0135: 0xa2,   // ¢
+  0x0136: 0xa3,   // £
+  0x0137: 0x266a, // ♪
+  0x0138: 0xe0,   // à
+  0x0139: 0xa0,   //
+  0x013a: 0xe8,   // è
+  0x013b: 0xe2,   // â
+  0x013c: 0xea,   // ê
+  0x013d: 0xee,   // î
+  0x013e: 0xf4,   // ô
+  0x013f: 0xfb,   // û
+  0x0220: 0xc1,   // Á
+  0x0221: 0xc9,   // É
+  0x0222: 0xd3,   // Ó
+  0x0223: 0xda,   // Ú
+  0x0224: 0xdc,   // Ü
+  0x0225: 0xfc,   // ü
+  0x0226: 0x2018, // ‘
+  0x0227: 0xa1,   // ¡
+  0x0228: 0x2a,   // *
+  0x0229: 0x27,   // '
+  0x022a: 0x2014, // —
+  0x022b: 0xa9,   // ©
+  0x022c: 0x2120, // â„ 
+  0x022d: 0x2022, // •
+  0x022e: 0x201c, // “
+  0x022f: 0x201d, // ”
+  0x0230: 0xc0,   // À
+  0x0231: 0xc2,   // Â
+  0x0232: 0xc7,   // Ç
+  0x0233: 0xc8,   // È
+  0x0234: 0xca,   // Ê
+  0x0235: 0xcb,   // Ë
+  0x0236: 0xeb,   // ë
+  0x0237: 0xce,   // ÃŽ
+  0x0238: 0xcf,   // Ï
+  0x0239: 0xef,   // ï
+  0x023a: 0xd4,   // Ô
+  0x023b: 0xd9,   // Ù
+  0x023c: 0xf9,   // ù
+  0x023d: 0xdb,   // Û
+  0x023e: 0xab,   // «
+  0x023f: 0xbb,   // »
+  0x0320: 0xc3,   // Ã
+  0x0321: 0xe3,   // ã
+  0x0322: 0xcd,   // Í
+  0x0323: 0xcc,   // Ì
+  0x0324: 0xec,   // ì
+  0x0325: 0xd2,   // Ã’
+  0x0326: 0xf2,   // ò
+  0x0327: 0xd5,   // Õ
+  0x0328: 0xf5,   // õ
+  0x0329: 0x7b,   // {
+  0x032a: 0x7d,   // }
+  0x032b: 0x5c,   // \
+  0x032c: 0x5e,   // ^
+  0x032d: 0x5f,   // _
+  0x032e: 0x7c,   // |
+  0x032f: 0x7e,   // ~
+  0x0330: 0xc4,   // Ä
+  0x0331: 0xe4,   // ä
+  0x0332: 0xd6,   // Ö
+  0x0333: 0xf6,   // ö
+  0x0334: 0xdf,   // ß
+  0x0335: 0xa5,   // ¥
+  0x0336: 0xa4,   // ¤
+  0x0337: 0x2502, // │
+  0x0338: 0xc5,   // Ã…
+  0x0339: 0xe5,   // å
+  0x033a: 0xd8,   // Ø
+  0x033b: 0xf8,   // ø
+  0x033c: 0x250c, // ┌
+  0x033d: 0x2510, // ┐
+  0x033e: 0x2514, // â””
+  0x033f: 0x2518  // ┘
+};
+
+var getCharFromCode = function(code) {
+  if (code === null) {
+    return '';
   }
+  code = CHARACTER_TRANSLATION[code] || code;
+  return String.fromCharCode(code);
+};
 
-  // slow case. multiple pipe destinations.
+// the index of the last row in a CEA-608 display buffer
+var BOTTOM_ROW = 14;
 
-  if (!dest) {
-    // remove all.
-    var dests = state.pipes;
-    var len = state.pipesCount;
-    state.pipes = null;
-    state.pipesCount = 0;
-    state.flowing = false;
+// This array is used for mapping PACs -> row #, since there's no way of
+// getting it through bit logic.
+var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620,
+            0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420];
 
-    for (var i = 0; i < len; i++) {
-      dests[i].emit('unpipe', this, unpipeInfo);
-    }return this;
+// CEA-608 captions are rendered onto a 34x15 matrix of character
+// cells. The "bottom" row is the last element in the outer array.
+var createDisplayBuffer = function() {
+  var result = [], i = BOTTOM_ROW + 1;
+  while (i--) {
+    result.push('');
   }
+  return result;
+};
 
-  // try to find the right one.
-  var index = indexOf(state.pipes, dest);
-  if (index === -1) return this;
+var Cea608Stream = function(field, dataChannel) {
+  Cea608Stream.prototype.init.call(this);
 
-  state.pipes.splice(index, 1);
-  state.pipesCount -= 1;
-  if (state.pipesCount === 1) state.pipes = state.pipes[0];
+  this.field_ = field || 0;
+  this.dataChannel_ = dataChannel || 0;
 
-  dest.emit('unpipe', this, unpipeInfo);
+  this.name_ = 'CC' + (((this.field_ << 1) | this.dataChannel_) + 1);
 
-  return this;
-};
+  this.setConstants();
+  this.reset();
 
-// set up data events if they are asked for
-// Ensure readable listeners eventually get something
-Readable.prototype.on = function (ev, fn) {
-  var res = Stream.prototype.on.call(this, ev, fn);
+  this.push = function(packet) {
+    var data, swap, char0, char1, text;
+    // remove the parity bits
+    data = packet.ccData & 0x7f7f;
 
-  if (ev === 'data') {
-    // Start flowing on next tick if stream isn't explicitly paused
-    if (this._readableState.flowing !== false) this.resume();
-  } else if (ev === 'readable') {
-    var state = this._readableState;
-    if (!state.endEmitted && !state.readableListening) {
-      state.readableListening = state.needReadable = true;
-      state.emittedReadable = false;
-      if (!state.reading) {
-        pna.nextTick(nReadingNextTick, this);
-      } else if (state.length) {
-        emitReadable(this);
-      }
+    // ignore duplicate control codes; the spec demands they're sent twice
+    if (data === this.lastControlCode_) {
+      this.lastControlCode_ = null;
+      return;
     }
-  }
 
-  return res;
-};
-Readable.prototype.addListener = Readable.prototype.on;
+    // Store control codes
+    if ((data & 0xf000) === 0x1000) {
+      this.lastControlCode_ = data;
+    } else if (data !== this.PADDING_) {
+      this.lastControlCode_ = null;
+    }
 
-function nReadingNextTick(self) {
-  debug('readable nexttick read 0');
-  self.read(0);
-}
+    char0 = data >>> 8;
+    char1 = data & 0xff;
 
-// pause() and resume() are remnants of the legacy readable stream API
-// If the user uses them, then switch into old mode.
-Readable.prototype.resume = function () {
-  var state = this._readableState;
-  if (!state.flowing) {
-    debug('resume');
-    state.flowing = true;
-    resume(this, state);
-  }
-  return this;
-};
+    if (data === this.PADDING_) {
+      return;
 
-function resume(stream, state) {
-  if (!state.resumeScheduled) {
-    state.resumeScheduled = true;
-    pna.nextTick(resume_, stream, state);
-  }
-}
+    } else if (data === this.RESUME_CAPTION_LOADING_) {
+      this.mode_ = 'popOn';
+
+    } else if (data === this.END_OF_CAPTION_) {
+      // If an EOC is received while in paint-on mode, the displayed caption
+      // text should be swapped to non-displayed memory as if it was a pop-on
+      // caption. Because of that, we should explicitly switch back to pop-on
+      // mode
+      this.mode_ = 'popOn';
+      this.clearFormatting(packet.pts);
+      // if a caption was being displayed, it's gone now
+      this.flushDisplayed(packet.pts);
+
+      // flip memory
+      swap = this.displayed_;
+      this.displayed_ = this.nonDisplayed_;
+      this.nonDisplayed_ = swap;
+
+      // start measuring the time to display the caption
+      this.startPts_ = packet.pts;
+
+    } else if (data === this.ROLL_UP_2_ROWS_) {
+      this.rollUpRows_ = 2;
+      this.setRollUp(packet.pts);
+    } else if (data === this.ROLL_UP_3_ROWS_) {
+      this.rollUpRows_ = 3;
+      this.setRollUp(packet.pts);
+    } else if (data === this.ROLL_UP_4_ROWS_) {
+      this.rollUpRows_ = 4;
+      this.setRollUp(packet.pts);
+    } else if (data === this.CARRIAGE_RETURN_) {
+      this.clearFormatting(packet.pts);
+      this.flushDisplayed(packet.pts);
+      this.shiftRowsUp_();
+      this.startPts_ = packet.pts;
+
+    } else if (data === this.BACKSPACE_) {
+      if (this.mode_ === 'popOn') {
+        this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
+      } else {
+        this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
+      }
+    } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
+      this.flushDisplayed(packet.pts);
+      this.displayed_ = createDisplayBuffer();
+    } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
+      this.nonDisplayed_ = createDisplayBuffer();
+
+    } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
+      if (this.mode_ !== 'paintOn') {
+        // NOTE: This should be removed when proper caption positioning is
+        // implemented
+        this.flushDisplayed(packet.pts);
+        this.displayed_ = createDisplayBuffer();
+      }
+      this.mode_ = 'paintOn';
+      this.startPts_ = packet.pts;
+
+    // Append special characters to caption text
+    } else if (this.isSpecialCharacter(char0, char1)) {
+      // Bitmask char0 so that we can apply character transformations
+      // regardless of field and data channel.
+      // Then byte-shift to the left and OR with char1 so we can pass the
+      // entire character code to `getCharFromCode`.
+      char0 = (char0 & 0x03) << 8;
+      text = getCharFromCode(char0 | char1);
+      this[this.mode_](packet.pts, text);
+      this.column_++;
+
+    // Append extended characters to caption text
+    } else if (this.isExtCharacter(char0, char1)) {
+      // Extended characters always follow their "non-extended" equivalents.
+      // IE if a "è" is desired, you'll always receive "eè"; non-compliant
+      // decoders are supposed to drop the "è", while compliant decoders
+      // backspace the "e" and insert "è".
+
+      // Delete the previous character
+      if (this.mode_ === 'popOn') {
+        this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
+      } else {
+        this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
+      }
 
-function resume_(stream, state) {
-  if (!state.reading) {
-    debug('resume read 0');
-    stream.read(0);
-  }
+      // Bitmask char0 so that we can apply character transformations
+      // regardless of field and data channel.
+      // Then byte-shift to the left and OR with char1 so we can pass the
+      // entire character code to `getCharFromCode`.
+      char0 = (char0 & 0x03) << 8;
+      text = getCharFromCode(char0 | char1);
+      this[this.mode_](packet.pts, text);
+      this.column_++;
+
+    // Process mid-row codes
+    } else if (this.isMidRowCode(char0, char1)) {
+      // Attributes are not additive, so clear all formatting
+      this.clearFormatting(packet.pts);
+
+      // According to the standard, mid-row codes
+      // should be replaced with spaces, so add one now
+      this[this.mode_](packet.pts, ' ');
+      this.column_++;
+
+      if ((char1 & 0xe) === 0xe) {
+        this.addFormatting(packet.pts, ['i']);
+      }
 
-  state.resumeScheduled = false;
-  state.awaitDrain = 0;
-  stream.emit('resume');
-  flow(stream);
-  if (state.flowing && !state.reading) stream.read(0);
-}
+      if ((char1 & 0x1) === 0x1) {
+        this.addFormatting(packet.pts, ['u']);
+      }
 
-Readable.prototype.pause = function () {
-  debug('call pause flowing=%j', this._readableState.flowing);
-  if (false !== this._readableState.flowing) {
-    debug('pause');
-    this._readableState.flowing = false;
-    this.emit('pause');
-  }
-  return this;
-};
+    // Detect offset control codes and adjust cursor
+    } else if (this.isOffsetControlCode(char0, char1)) {
+      // Cursor position is set by indent PAC (see below) in 4-column
+      // increments, with an additional offset code of 1-3 to reach any
+      // of the 32 columns specified by CEA-608. So all we need to do
+      // here is increment the column cursor by the given offset.
+      this.column_ += (char1 & 0x03);
+
+    // Detect PACs (Preamble Address Codes)
+    } else if (this.isPAC(char0, char1)) {
+
+      // There's no logic for PAC -> row mapping, so we have to just
+      // find the row code in an array and use its index :(
+      var row = ROWS.indexOf(data & 0x1f20);
+
+      // Configure the caption window if we're in roll-up mode
+      if (this.mode_ === 'rollUp') {
+        // This implies that the base row is incorrectly set.
+        // As per the recommendation in CEA-608(Base Row Implementation), defer to the number
+        // of roll-up rows set.
+        if (row - this.rollUpRows_ + 1 < 0) {
+          row = this.rollUpRows_ - 1;
+        }
 
-function flow(stream) {
-  var state = stream._readableState;
-  debug('flow', state.flowing);
-  while (state.flowing && stream.read() !== null) {}
-}
+        this.setRollUp(packet.pts, row);
+      }
 
-// wrap an old-style stream as the async data source.
-// This is *not* part of the readable stream interface.
-// It is an ugly unfortunate mess of history.
-Readable.prototype.wrap = function (stream) {
-  var _this = this;
+      if (row !== this.row_) {
+        // formatting is only persistent for current row
+        this.clearFormatting(packet.pts);
+        this.row_ = row;
+      }
+      // All PACs can apply underline, so detect and apply
+      // (All odd-numbered second bytes set underline)
+      if ((char1 & 0x1) && (this.formatting_.indexOf('u') === -1)) {
+          this.addFormatting(packet.pts, ['u']);
+      }
 
-  var state = this._readableState;
-  var paused = false;
+      if ((data & 0x10) === 0x10) {
+        // We've got an indent level code. Each successive even number
+        // increments the column cursor by 4, so we can get the desired
+        // column position by bit-shifting to the right (to get n/2)
+        // and multiplying by 4.
+        this.column_ = ((data & 0xe) >> 1) * 4;
+      }
 
-  stream.on('end', function () {
-    debug('wrapped end');
-    if (state.decoder && !state.ended) {
-      var chunk = state.decoder.end();
-      if (chunk && chunk.length) _this.push(chunk);
-    }
+      if (this.isColorPAC(char1)) {
+        // it's a color code, though we only support white, which
+        // can be either normal or italicized. white italics can be
+        // either 0x4e or 0x6e depending on the row, so we just
+        // bitwise-and with 0xe to see if italics should be turned on
+        if ((char1 & 0xe) === 0xe) {
+          this.addFormatting(packet.pts, ['i']);
+        }
+      }
 
-    _this.push(null);
-  });
+    // We have a normal character in char0, and possibly one in char1
+    } else if (this.isNormalChar(char0)) {
+      if (char1 === 0x00) {
+        char1 = null;
+      }
+      text = getCharFromCode(char0);
+      text += getCharFromCode(char1);
+      this[this.mode_](packet.pts, text);
+      this.column_ += text.length;
 
-  stream.on('data', function (chunk) {
-    debug('wrapped data');
-    if (state.decoder) chunk = state.decoder.write(chunk);
+    } // finish data processing
 
-    // don't skip over falsy values in objectMode
-    if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
+  };
+};
+Cea608Stream.prototype = new Stream();
+// Trigger a cue point that captures the current state of the
+// display buffer
+Cea608Stream.prototype.flushDisplayed = function(pts) {
+  var content = this.displayed_
+    // remove spaces from the start and end of the string
+    .map(function(row) {
+      try {
+        return row.trim();
+      } catch (e) {
+        // Ordinarily, this shouldn't happen. However, caption
+        // parsing errors should not throw exceptions and
+        // break playback.
+        // eslint-disable-next-line no-console
+        console.error('Skipping malformed caption.');
+        return '';
+      }
+    })
+    // combine all text rows to display in one cue
+    .join('\n')
+    // and remove blank rows from the start and end, but not the middle
+    .replace(/^\n+|\n+$/g, '');
+
+  if (content.length) {
+    this.trigger('data', {
+      startPts: this.startPts_,
+      endPts: pts,
+      text: content,
+      stream: this.name_
+    });
+  }
+};
 
-    var ret = _this.push(chunk);
-    if (!ret) {
-      paused = true;
-      stream.pause();
-    }
-  });
+/**
+ * Zero out the data, used for startup and on seek
+ */
+Cea608Stream.prototype.reset = function() {
+  this.mode_ = 'popOn';
+  // When in roll-up mode, the index of the last row that will
+  // actually display captions. If a caption is shifted to a row
+  // with a lower index than this, it is cleared from the display
+  // buffer
+  this.topRow_ = 0;
+  this.startPts_ = 0;
+  this.displayed_ = createDisplayBuffer();
+  this.nonDisplayed_ = createDisplayBuffer();
+  this.lastControlCode_ = null;
+
+  // Track row and column for proper line-breaking and spacing
+  this.column_ = 0;
+  this.row_ = BOTTOM_ROW;
+  this.rollUpRows_ = 2;
+
+  // This variable holds currently-applied formatting
+  this.formatting_ = [];
+};
 
-  // proxy all the other methods.
-  // important when wrapping filters and duplexes.
-  for (var i in stream) {
-    if (this[i] === undefined && typeof stream[i] === 'function') {
-      this[i] = function (method) {
-        return function () {
-          return stream[method].apply(stream, arguments);
-        };
-      }(i);
-    }
+/**
+ * Sets up control code and related constants for this instance
+ */
+Cea608Stream.prototype.setConstants = function() {
+  // The following attributes have these uses:
+  // ext_ :    char0 for mid-row codes, and the base for extended
+  //           chars (ext_+0, ext_+1, and ext_+2 are char0s for
+  //           extended codes)
+  // control_: char0 for control codes, except byte-shifted to the
+  //           left so that we can do this.control_ | CONTROL_CODE
+  // offset_:  char0 for tab offset codes
+  //
+  // It's also worth noting that control codes, and _only_ control codes,
+  // differ between field 1 and field2. Field 2 control codes are always
+  // their field 1 value plus 1. That's why there's the "| field" on the
+  // control value.
+  if (this.dataChannel_ === 0) {
+    this.BASE_     = 0x10;
+    this.EXT_      = 0x11;
+    this.CONTROL_  = (0x14 | this.field_) << 8;
+    this.OFFSET_   = 0x17;
+  } else if (this.dataChannel_ === 1) {
+    this.BASE_     = 0x18;
+    this.EXT_      = 0x19;
+    this.CONTROL_  = (0x1c | this.field_) << 8;
+    this.OFFSET_   = 0x1f;
   }
 
-  // proxy certain important events.
-  for (var n = 0; n < kProxyEvents.length; n++) {
-    stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
-  }
+  // Constants for the LSByte command codes recognized by Cea608Stream. This
+  // list is not exhaustive. For a more comprehensive listing and semantics see
+  // http://www.gpo.gov/fdsys/pkg/CFR-2010-title47-vol1/pdf/CFR-2010-title47-vol1-sec15-119.pdf
+  // Padding
+  this.PADDING_                    = 0x0000;
+  // Pop-on Mode
+  this.RESUME_CAPTION_LOADING_     = this.CONTROL_ | 0x20;
+  this.END_OF_CAPTION_             = this.CONTROL_ | 0x2f;
+  // Roll-up Mode
+  this.ROLL_UP_2_ROWS_             = this.CONTROL_ | 0x25;
+  this.ROLL_UP_3_ROWS_             = this.CONTROL_ | 0x26;
+  this.ROLL_UP_4_ROWS_             = this.CONTROL_ | 0x27;
+  this.CARRIAGE_RETURN_            = this.CONTROL_ | 0x2d;
+  // paint-on mode
+  this.RESUME_DIRECT_CAPTIONING_   = this.CONTROL_ | 0x29;
+  // Erasure
+  this.BACKSPACE_                  = this.CONTROL_ | 0x21;
+  this.ERASE_DISPLAYED_MEMORY_     = this.CONTROL_ | 0x2c;
+  this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
+};
 
-  // when we try to consume some more bytes, simply unpause the
-  // underlying stream.
-  this._read = function (n) {
-    debug('wrapped _read', n);
-    if (paused) {
-      paused = false;
-      stream.resume();
-    }
-  };
+/**
+ * Detects if the 2-byte packet data is a special character
+ *
+ * Special characters have a second byte in the range 0x30 to 0x3f,
+ * with the first byte being 0x11 (for data channel 1) or 0x19 (for
+ * data channel 2).
+ *
+ * @param  {Integer} char0 The first byte
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the 2 bytes are an special character
+ */
+Cea608Stream.prototype.isSpecialCharacter = function(char0, char1) {
+  return (char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f);
+};
 
-  return this;
+/**
+ * Detects if the 2-byte packet data is an extended character
+ *
+ * Extended characters have a second byte in the range 0x20 to 0x3f,
+ * with the first byte being 0x12 or 0x13 (for data channel 1) or
+ * 0x1a or 0x1b (for data channel 2).
+ *
+ * @param  {Integer} char0 The first byte
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the 2 bytes are an extended character
+ */
+Cea608Stream.prototype.isExtCharacter = function(char0, char1) {
+  return ((char0 === (this.EXT_ + 1) || char0 === (this.EXT_ + 2)) &&
+    (char1 >= 0x20 && char1 <= 0x3f));
 };
 
-Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
-  // making it explicit this property is not enumerable
-  // because otherwise some prototype manipulation in
-  // userland will fail
-  enumerable: false,
-  get: function () {
-    return this._readableState.highWaterMark;
-  }
-});
+/**
+ * Detects if the 2-byte packet is a mid-row code
+ *
+ * Mid-row codes have a second byte in the range 0x20 to 0x2f, with
+ * the first byte being 0x11 (for data channel 1) or 0x19 (for data
+ * channel 2).
+ *
+ * @param  {Integer} char0 The first byte
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the 2 bytes are a mid-row code
+ */
+Cea608Stream.prototype.isMidRowCode = function(char0, char1) {
+  return (char0 === this.EXT_ && (char1 >= 0x20 && char1 <= 0x2f));
+};
 
-// exposed for testing purposes only.
-Readable._fromList = fromList;
+/**
+ * Detects if the 2-byte packet is an offset control code
+ *
+ * Offset control codes have a second byte in the range 0x21 to 0x23,
+ * with the first byte being 0x17 (for data channel 1) or 0x1f (for
+ * data channel 2).
+ *
+ * @param  {Integer} char0 The first byte
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the 2 bytes are an offset control code
+ */
+Cea608Stream.prototype.isOffsetControlCode = function(char0, char1) {
+  return (char0 === this.OFFSET_ && (char1 >= 0x21 && char1 <= 0x23));
+};
 
-// Pluck off n bytes from an array of buffers.
-// Length is the combined lengths of all the buffers in the list.
-// This function is designed to be inlinable, so please take care when making
-// changes to the function body.
-function fromList(n, state) {
-  // nothing buffered
-  if (state.length === 0) return null;
+/**
+ * Detects if the 2-byte packet is a Preamble Address Code
+ *
+ * PACs have a first byte in the range 0x10 to 0x17 (for data channel 1)
+ * or 0x18 to 0x1f (for data channel 2), with the second byte in the
+ * range 0x40 to 0x7f.
+ *
+ * @param  {Integer} char0 The first byte
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the 2 bytes are a PAC
+ */
+Cea608Stream.prototype.isPAC = function(char0, char1) {
+  return (char0 >= this.BASE_ && char0 < (this.BASE_ + 8) &&
+    (char1 >= 0x40 && char1 <= 0x7f));
+};
 
-  var ret;
-  if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
-    // read it all, truncate the list
-    if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length);
-    state.buffer.clear();
-  } else {
-    // read part of list
-    ret = fromListPartial(n, state.buffer, state.decoder);
-  }
+/**
+ * Detects if a packet's second byte is in the range of a PAC color code
+ *
+ * PAC color codes have the second byte be in the range 0x40 to 0x4f, or
+ * 0x60 to 0x6f.
+ *
+ * @param  {Integer} char1 The second byte
+ * @return {Boolean}       Whether the byte is a color PAC
+ */
+Cea608Stream.prototype.isColorPAC = function(char1) {
+  return ((char1 >= 0x40 && char1 <= 0x4f) || (char1 >= 0x60 && char1 <= 0x7f));
+};
 
-  return ret;
-}
+/**
+ * Detects if a single byte is in the range of a normal character
+ *
+ * Normal text bytes are in the range 0x20 to 0x7f.
+ *
+ * @param  {Integer} char  The byte
+ * @return {Boolean}       Whether the byte is a normal character
+ */
+Cea608Stream.prototype.isNormalChar = function(char) {
+  return (char >= 0x20 && char <= 0x7f);
+};
 
-// Extracts only enough buffered data to satisfy the amount requested.
-// This function is designed to be inlinable, so please take care when making
-// changes to the function body.
-function fromListPartial(n, list, hasStrings) {
-  var ret;
-  if (n < list.head.data.length) {
-    // slice is the same for buffers and strings
-    ret = list.head.data.slice(0, n);
-    list.head.data = list.head.data.slice(n);
-  } else if (n === list.head.data.length) {
-    // first chunk is a perfect match
-    ret = list.shift();
-  } else {
-    // result spans more than one buffer
-    ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
+/**
+ * Configures roll-up
+ *
+ * @param  {Integer} pts         Current PTS
+ * @param  {Integer} newBaseRow  Used by PACs to slide the current window to
+ *                               a new position
+ */
+Cea608Stream.prototype.setRollUp = function(pts, newBaseRow) {
+  // Reset the base row to the bottom row when switching modes
+  if (this.mode_ !== 'rollUp') {
+    this.row_ = BOTTOM_ROW;
+    this.mode_ = 'rollUp';
+    // Spec says to wipe memories when switching to roll-up
+    this.flushDisplayed(pts);
+    this.nonDisplayed_ = createDisplayBuffer();
+    this.displayed_ = createDisplayBuffer();
   }
-  return ret;
-}
 
-// Copies a specified amount of characters from the list of buffered data
-// chunks.
-// This function is designed to be inlinable, so please take care when making
-// changes to the function body.
-function copyFromBufferString(n, list) {
-  var p = list.head;
-  var c = 1;
-  var ret = p.data;
-  n -= ret.length;
-  while (p = p.next) {
-    var str = p.data;
-    var nb = n > str.length ? str.length : n;
-    if (nb === str.length) ret += str;else ret += str.slice(0, n);
-    n -= nb;
-    if (n === 0) {
-      if (nb === str.length) {
-        ++c;
-        if (p.next) list.head = p.next;else list.head = list.tail = null;
-      } else {
-        list.head = p;
-        p.data = str.slice(nb);
-      }
-      break;
+  if (newBaseRow !== undefined && newBaseRow !== this.row_) {
+    // move currently displayed captions (up or down) to the new base row
+    for (var i = 0; i < this.rollUpRows_; i++) {
+      this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
+      this.displayed_[this.row_ - i] = '';
     }
-    ++c;
   }
-  list.length -= c;
-  return ret;
-}
 
-// Copies a specified amount of bytes from the list of buffered data chunks.
-// This function is designed to be inlinable, so please take care when making
-// changes to the function body.
-function copyFromBuffer(n, list) {
-  var ret = Buffer.allocUnsafe(n);
-  var p = list.head;
-  var c = 1;
-  p.data.copy(ret);
-  n -= p.data.length;
-  while (p = p.next) {
-    var buf = p.data;
-    var nb = n > buf.length ? buf.length : n;
-    buf.copy(ret, ret.length - n, 0, nb);
-    n -= nb;
-    if (n === 0) {
-      if (nb === buf.length) {
-        ++c;
-        if (p.next) list.head = p.next;else list.head = list.tail = null;
-      } else {
-        list.head = p;
-        p.data = buf.slice(nb);
-      }
-      break;
-    }
-    ++c;
+  if (newBaseRow === undefined) {
+    newBaseRow = this.row_;
   }
-  list.length -= c;
-  return ret;
-}
 
-function endReadable(stream) {
-  var state = stream._readableState;
+  this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
+};
 
-  // If we get here before consuming all the bytes, then that is a
-  // bug in node.  Should never happen.
-  if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
+// Adds the opening HTML tag for the passed character to the caption text,
+// and keeps track of it for later closing
+Cea608Stream.prototype.addFormatting = function(pts, format) {
+  this.formatting_ = this.formatting_.concat(format);
+  var text = format.reduce(function(text, format) {
+    return text + '<' + format + '>';
+  }, '');
+  this[this.mode_](pts, text);
+};
 
-  if (!state.endEmitted) {
-    state.ended = true;
-    pna.nextTick(endReadableNT, state, stream);
+// Adds HTML closing tags for current formatting to caption text and
+// clears remembered formatting
+Cea608Stream.prototype.clearFormatting = function(pts) {
+  if (!this.formatting_.length) {
+    return;
   }
-}
+  var text = this.formatting_.reverse().reduce(function(text, format) {
+    return text + '</' + format + '>';
+  }, '');
+  this.formatting_ = [];
+  this[this.mode_](pts, text);
+};
 
-function endReadableNT(state, stream) {
-  // Check that we didn't get one last unshift.
-  if (!state.endEmitted && state.length === 0) {
-    state.endEmitted = true;
-    stream.readable = false;
-    stream.emit('end');
-  }
-}
+// Mode Implementations
+Cea608Stream.prototype.popOn = function(pts, text) {
+  var baseRow = this.nonDisplayed_[this.row_];
 
-function indexOf(xs, x) {
-  for (var i = 0, l = xs.length; i < l; i++) {
-    if (xs[i] === x) return i;
-  }
-  return -1;
-}
-}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
-},{"./_stream_duplex":22,"./internal/streams/BufferList":27,"./internal/streams/destroy":28,"./internal/streams/stream":29,"_process":21,"core-util-is":9,"events":10,"inherits":12,"isarray":14,"process-nextick-args":20,"safe-buffer":32,"string_decoder/":33,"util":7}],25:[function(require,module,exports){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
+  // buffer characters
+  baseRow += text;
+  this.nonDisplayed_[this.row_] = baseRow;
+};
 
-// a transform stream is a readable/writable stream where you do
-// something with the data.  Sometimes it's called a "filter",
-// but that's not a great name for it, since that implies a thing where
-// some bits pass through, and others are simply ignored.  (That would
-// be a valid example of a transform, of course.)
-//
-// While the output is causally related to the input, it's not a
-// necessarily symmetric or synchronous transformation.  For example,
-// a zlib stream might take multiple plain-text writes(), and then
-// emit a single compressed chunk some time in the future.
-//
-// Here's how this works:
-//
-// The Transform stream has all the aspects of the readable and writable
-// stream classes.  When you write(chunk), that calls _write(chunk,cb)
-// internally, and returns false if there's a lot of pending writes
-// buffered up.  When you call read(), that calls _read(n) until
-// there's enough pending readable data buffered up.
-//
-// In a transform stream, the written data is placed in a buffer.  When
-// _read(n) is called, it transforms the queued up data, calling the
-// buffered _write cb's as it consumes chunks.  If consuming a single
-// written chunk would result in multiple output chunks, then the first
-// outputted bit calls the readcb, and subsequent chunks just go into
-// the read buffer, and will cause it to emit 'readable' if necessary.
-//
-// This way, back-pressure is actually determined by the reading side,
-// since _read has to be called to start processing a new chunk.  However,
-// a pathological inflate type of transform can cause excessive buffering
-// here.  For example, imagine a stream where every byte of input is
-// interpreted as an integer from 0-255, and then results in that many
-// bytes of output.  Writing the 4 bytes {ff,ff,ff,ff} would result in
-// 1kb of data being output.  In this case, you could write a very small
-// amount of input, and end up with a very large amount of output.  In
-// such a pathological inflating mechanism, there'd be no way to tell
-// the system to stop doing the transform.  A single 4MB write could
-// cause the system to run out of memory.
-//
-// However, even in such a pathological case, only a single written chunk
-// would be consumed, and then the rest would wait (un-transformed) until
-// the results of the previous transformed chunk were consumed.
+Cea608Stream.prototype.rollUp = function(pts, text) {
+  var baseRow = this.displayed_[this.row_];
 
-'use strict';
+  baseRow += text;
+  this.displayed_[this.row_] = baseRow;
 
-module.exports = Transform;
+};
 
-var Duplex = require('./_stream_duplex');
+Cea608Stream.prototype.shiftRowsUp_ = function() {
+  var i;
+  // clear out inactive rows
+  for (i = 0; i < this.topRow_; i++) {
+    this.displayed_[i] = '';
+  }
+  for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
+    this.displayed_[i] = '';
+  }
+  // shift displayed rows up
+  for (i = this.topRow_; i < this.row_; i++) {
+    this.displayed_[i] = this.displayed_[i + 1];
+  }
+  // clear out the bottom row
+  this.displayed_[this.row_] = '';
+};
 
-/*<replacement>*/
-var util = Object.create(require('core-util-is'));
-util.inherits = require('inherits');
-/*</replacement>*/
+Cea608Stream.prototype.paintOn = function(pts, text) {
+  var baseRow = this.displayed_[this.row_];
 
-util.inherits(Transform, Duplex);
+  baseRow += text;
+  this.displayed_[this.row_] = baseRow;
+};
 
-function afterTransform(er, data) {
-  var ts = this._transformState;
-  ts.transforming = false;
+// exports
+module.exports = {
+  CaptionStream: CaptionStream,
+  Cea608Stream: Cea608Stream
+};
 
-  var cb = ts.writecb;
+},{"../tools/caption-packet-parser":53,"../utils/stream":60}],35:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+module.exports = require('./m2ts');
 
-  if (!cb) {
-    return this.emit('error', new Error('write callback called multiple times'));
-  }
+},{"./m2ts":36}],36:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * A stream-based mp2t to mp4 converter. This utility can be used to
+ * deliver mp4s to a SourceBuffer on platforms that support native
+ * Media Source Extensions.
+ */
+'use strict';
+var Stream = require('../utils/stream.js'),
+  CaptionStream = require('./caption-stream'),
+  StreamTypes = require('./stream-types'),
+  TimestampRolloverStream = require('./timestamp-rollover-stream').TimestampRolloverStream;
 
-  ts.writechunk = null;
-  ts.writecb = null;
+// object types
+var TransportPacketStream, TransportParseStream, ElementaryStream;
 
-  if (data != null) // single equals check for both `null` and `undefined`
-    this.push(data);
+// constants
+var
+  MP2T_PACKET_LENGTH = 188, // bytes
+  SYNC_BYTE = 0x47;
 
-  cb(er);
+/**
+ * Splits an incoming stream of binary data into MPEG-2 Transport
+ * Stream packets.
+ */
+TransportPacketStream = function() {
+  var
+    buffer = new Uint8Array(MP2T_PACKET_LENGTH),
+    bytesInBuffer = 0;
+
+  TransportPacketStream.prototype.init.call(this);
+
+   // Deliver new bytes to the stream.
+
+  /**
+   * Split a stream of data into M2TS packets
+  **/
+  this.push = function(bytes) {
+    var
+      startIndex = 0,
+      endIndex = MP2T_PACKET_LENGTH,
+      everything;
+
+    // If there are bytes remaining from the last segment, prepend them to the
+    // bytes that were pushed in
+    if (bytesInBuffer) {
+      everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
+      everything.set(buffer.subarray(0, bytesInBuffer));
+      everything.set(bytes, bytesInBuffer);
+      bytesInBuffer = 0;
+    } else {
+      everything = bytes;
+    }
 
-  var rs = this._readableState;
-  rs.reading = false;
-  if (rs.needReadable || rs.length < rs.highWaterMark) {
-    this._read(rs.highWaterMark);
-  }
-}
+    // While we have enough data for a packet
+    while (endIndex < everything.byteLength) {
+      // Look for a pair of start and end sync bytes in the data..
+      if (everything[startIndex] === SYNC_BYTE && everything[endIndex] === SYNC_BYTE) {
+        // We found a packet so emit it and jump one whole packet forward in
+        // the stream
+        this.trigger('data', everything.subarray(startIndex, endIndex));
+        startIndex += MP2T_PACKET_LENGTH;
+        endIndex += MP2T_PACKET_LENGTH;
+        continue;
+      }
+      // If we get here, we have somehow become de-synchronized and we need to step
+      // forward one byte at a time until we find a pair of sync bytes that denote
+      // a packet
+      startIndex++;
+      endIndex++;
+    }
 
-function Transform(options) {
-  if (!(this instanceof Transform)) return new Transform(options);
+    // If there was some data left over at the end of the segment that couldn't
+    // possibly be a whole packet, keep it because it might be the start of a packet
+    // that continues in the next segment
+    if (startIndex < everything.byteLength) {
+      buffer.set(everything.subarray(startIndex), 0);
+      bytesInBuffer = everything.byteLength - startIndex;
+    }
+  };
 
-  Duplex.call(this, options);
+  /**
+   * Passes identified M2TS packets to the TransportParseStream to be parsed
+  **/
+  this.flush = function() {
+    // If the buffer contains a whole packet when we are being flushed, emit it
+    // and empty the buffer. Otherwise hold onto the data because it may be
+    // important for decoding the next segment
+    if (bytesInBuffer === MP2T_PACKET_LENGTH && buffer[0] === SYNC_BYTE) {
+      this.trigger('data', buffer);
+      bytesInBuffer = 0;
+    }
+    this.trigger('done');
+  };
 
-  this._transformState = {
-    afterTransform: afterTransform.bind(this),
-    needTransform: false,
-    transforming: false,
-    writecb: null,
-    writechunk: null,
-    writeencoding: null
+  this.endTimeline = function() {
+    this.flush();
+    this.trigger('endedtimeline');
   };
 
-  // start out asking for a readable event once data is transformed.
-  this._readableState.needReadable = true;
+  this.reset = function() {
+    bytesInBuffer = 0;
+    this.trigger('reset');
+  };
+};
+TransportPacketStream.prototype = new Stream();
 
-  // we have implemented the _read method, and done the other things
-  // that Readable wants before the first _read call, so unset the
-  // sync guard flag.
-  this._readableState.sync = false;
+/**
+ * Accepts an MP2T TransportPacketStream and emits data events with parsed
+ * forms of the individual transport stream packets.
+ */
+TransportParseStream = function() {
+  var parsePsi, parsePat, parsePmt, self;
+  TransportParseStream.prototype.init.call(this);
+  self = this;
+
+  this.packetsWaitingForPmt = [];
+  this.programMapTable = undefined;
+
+  parsePsi = function(payload, psi) {
+    var offset = 0;
+
+    // PSI packets may be split into multiple sections and those
+    // sections may be split into multiple packets. If a PSI
+    // section starts in this packet, the payload_unit_start_indicator
+    // will be true and the first byte of the payload will indicate
+    // the offset from the current position to the start of the
+    // section.
+    if (psi.payloadUnitStartIndicator) {
+      offset += payload[offset] + 1;
+    }
 
-  if (options) {
-    if (typeof options.transform === 'function') this._transform = options.transform;
+    if (psi.type === 'pat') {
+      parsePat(payload.subarray(offset), psi);
+    } else {
+      parsePmt(payload.subarray(offset), psi);
+    }
+  };
 
-    if (typeof options.flush === 'function') this._flush = options.flush;
-  }
+  parsePat = function(payload, pat) {
+    pat.section_number = payload[7]; // eslint-disable-line camelcase
+    pat.last_section_number = payload[8]; // eslint-disable-line camelcase
 
-  // When the writable side finishes, then flush out anything remaining.
-  this.on('prefinish', prefinish);
-}
+    // skip the PSI header and parse the first PMT entry
+    self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
+    pat.pmtPid = self.pmtPid;
+  };
 
-function prefinish() {
-  var _this = this;
+  /**
+   * Parse out the relevant fields of a Program Map Table (PMT).
+   * @param payload {Uint8Array} the PMT-specific portion of an MP2T
+   * packet. The first byte in this array should be the table_id
+   * field.
+   * @param pmt {object} the object that should be decorated with
+   * fields parsed from the PMT.
+   */
+  parsePmt = function(payload, pmt) {
+    var sectionLength, tableEnd, programInfoLength, offset;
+
+    // PMTs can be sent ahead of the time when they should actually
+    // take effect. We don't believe this should ever be the case
+    // for HLS but we'll ignore "forward" PMT declarations if we see
+    // them. Future PMT declarations have the current_next_indicator
+    // set to zero.
+    if (!(payload[5] & 0x01)) {
+      return;
+    }
 
-  if (typeof this._flush === 'function') {
-    this._flush(function (er, data) {
-      done(_this, er, data);
-    });
-  } else {
-    done(this, null, null);
-  }
-}
+    // overwrite any existing program map table
+    self.programMapTable = {
+      video: null,
+      audio: null,
+      'timed-metadata': {}
+    };
 
-Transform.prototype.push = function (chunk, encoding) {
-  this._transformState.needTransform = false;
-  return Duplex.prototype.push.call(this, chunk, encoding);
-};
+    // the mapping table ends at the end of the current section
+    sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
+    tableEnd = 3 + sectionLength - 4;
+
+    // to determine where the table is, we have to figure out how
+    // long the program info descriptors are
+    programInfoLength = (payload[10] & 0x0f) << 8 | payload[11];
+
+    // advance the offset to the first entry in the mapping table
+    offset = 12 + programInfoLength;
+    while (offset < tableEnd) {
+      var streamType = payload[offset];
+      var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2];
+
+      // only map a single elementary_pid for audio and video stream types
+      // TODO: should this be done for metadata too? for now maintain behavior of
+      //       multiple metadata streams
+      if (streamType === StreamTypes.H264_STREAM_TYPE &&
+          self.programMapTable.video === null) {
+        self.programMapTable.video = pid;
+      } else if (streamType === StreamTypes.ADTS_STREAM_TYPE &&
+                 self.programMapTable.audio === null) {
+        self.programMapTable.audio = pid;
+      } else if (streamType === StreamTypes.METADATA_STREAM_TYPE) {
+        // map pid to stream type for metadata streams
+        self.programMapTable['timed-metadata'][pid] = streamType;
+      }
 
-// This is the part where you do stuff!
-// override this function in implementation classes.
-// 'chunk' is an input chunk.
-//
-// Call `push(newChunk)` to pass along transformed output
-// to the readable side.  You may call 'push' zero or more times.
-//
-// Call `cb(err)` when you are done with this chunk.  If you pass
-// an error, then that'll put the hurt on the whole operation.  If you
-// never call cb(), then you'll never get another chunk.
-Transform.prototype._transform = function (chunk, encoding, cb) {
-  throw new Error('_transform() is not implemented');
-};
+      // move to the next table entry
+      // skip past the elementary stream descriptors, if present
+      offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
+    }
 
-Transform.prototype._write = function (chunk, encoding, cb) {
-  var ts = this._transformState;
-  ts.writecb = cb;
-  ts.writechunk = chunk;
-  ts.writeencoding = encoding;
-  if (!ts.transforming) {
-    var rs = this._readableState;
-    if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
-  }
-};
+    // record the map on the packet as well
+    pmt.programMapTable = self.programMapTable;
+  };
 
-// Doesn't matter what the args are here.
-// _transform does all the work.
-// That we got here means that the readable side wants more data.
-Transform.prototype._read = function (n) {
-  var ts = this._transformState;
+  /**
+   * Deliver a new MP2T packet to the next stream in the pipeline.
+   */
+  this.push = function(packet) {
+    var
+      result = {},
+      offset = 4;
+
+    result.payloadUnitStartIndicator = !!(packet[1] & 0x40);
+
+    // pid is a 13-bit field starting at the last bit of packet[1]
+    result.pid = packet[1] & 0x1f;
+    result.pid <<= 8;
+    result.pid |= packet[2];
+
+    // if an adaption field is present, its length is specified by the
+    // fifth byte of the TS packet header. The adaptation field is
+    // used to add stuffing to PES packets that don't fill a complete
+    // TS packet, and to specify some forms of timing and control data
+    // that we do not currently use.
+    if (((packet[3] & 0x30) >>> 4) > 0x01) {
+      offset += packet[offset] + 1;
+    }
 
-  if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
-    ts.transforming = true;
-    this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
-  } else {
-    // mark that we need a transform, so that any data that comes in
-    // will get processed, now that we've asked for it.
-    ts.needTransform = true;
-  }
-};
+    // parse the rest of the packet based on the type
+    if (result.pid === 0) {
+      result.type = 'pat';
+      parsePsi(packet.subarray(offset), result);
+      this.trigger('data', result);
+    } else if (result.pid === this.pmtPid) {
+      result.type = 'pmt';
+      parsePsi(packet.subarray(offset), result);
+      this.trigger('data', result);
+
+      // if there are any packets waiting for a PMT to be found, process them now
+      while (this.packetsWaitingForPmt.length) {
+        this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
+      }
+    } else if (this.programMapTable === undefined) {
+      // When we have not seen a PMT yet, defer further processing of
+      // PES packets until one has been parsed
+      this.packetsWaitingForPmt.push([packet, offset, result]);
+    } else {
+      this.processPes_(packet, offset, result);
+    }
+  };
 
-Transform.prototype._destroy = function (err, cb) {
-  var _this2 = this;
+  this.processPes_ = function(packet, offset, result) {
+    // set the appropriate stream type
+    if (result.pid === this.programMapTable.video) {
+      result.streamType = StreamTypes.H264_STREAM_TYPE;
+    } else if (result.pid === this.programMapTable.audio) {
+      result.streamType = StreamTypes.ADTS_STREAM_TYPE;
+    } else {
+      // if not video or audio, it is timed-metadata or unknown
+      // if unknown, streamType will be undefined
+      result.streamType = this.programMapTable['timed-metadata'][result.pid];
+    }
 
-  Duplex.prototype._destroy.call(this, err, function (err2) {
-    cb(err2);
-    _this2.emit('close');
-  });
+    result.type = 'pes';
+    result.data = packet.subarray(offset);
+    this.trigger('data', result);
+  };
+};
+TransportParseStream.prototype = new Stream();
+TransportParseStream.STREAM_TYPES  = {
+  h264: 0x1b,
+  adts: 0x0f
 };
 
-function done(stream, er, data) {
-  if (er) return stream.emit('error', er);
-
-  if (data != null) // single equals check for both `null` and `undefined`
-    stream.push(data);
-
-  // if there's nothing in the write buffer, then that means
-  // that nothing more will ever be provided
-  if (stream._writableState.length) throw new Error('Calling transform done when ws.length != 0');
+/**
+ * Reconsistutes program elementary stream (PES) packets from parsed
+ * transport stream packets. That is, if you pipe an
+ * mp2t.TransportParseStream into a mp2t.ElementaryStream, the output
+ * events will be events which capture the bytes for individual PES
+ * packets plus relevant metadata that has been extracted from the
+ * container.
+ */
+ElementaryStream = function() {
+  var
+    self = this,
+    // PES packet fragments
+    video = {
+      data: [],
+      size: 0
+    },
+    audio = {
+      data: [],
+      size: 0
+    },
+    timedMetadata = {
+      data: [],
+      size: 0
+    },
+    programMapTable,
+    parsePes = function(payload, pes) {
+      var ptsDtsFlags;
+
+      // get the packet length, this will be 0 for video
+      pes.packetLength = 6 + ((payload[4] << 8) | payload[5]);
+
+      // find out if this packets starts a new keyframe
+      pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0;
+      // PES packets may be annotated with a PTS value, or a PTS value
+      // and a DTS value. Determine what combination of values is
+      // available to work with.
+      ptsDtsFlags = payload[7];
+
+      // PTS and DTS are normally stored as a 33-bit number.  Javascript
+      // performs all bitwise operations on 32-bit integers but javascript
+      // supports a much greater range (52-bits) of integer using standard
+      // mathematical operations.
+      // We construct a 31-bit value using bitwise operators over the 31
+      // most significant bits and then multiply by 4 (equal to a left-shift
+      // of 2) before we add the final 2 least significant bits of the
+      // timestamp (equal to an OR.)
+      if (ptsDtsFlags & 0xC0) {
+        // the PTS and DTS are not written out directly. For information
+        // on how they are encoded, see
+        // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
+        pes.pts = (payload[9] & 0x0E) << 27 |
+          (payload[10] & 0xFF) << 20 |
+          (payload[11] & 0xFE) << 12 |
+          (payload[12] & 0xFF) <<  5 |
+          (payload[13] & 0xFE) >>>  3;
+        pes.pts *= 4; // Left shift by 2
+        pes.pts += (payload[13] & 0x06) >>> 1; // OR by the two LSBs
+        pes.dts = pes.pts;
+        if (ptsDtsFlags & 0x40) {
+          pes.dts = (payload[14] & 0x0E) << 27 |
+            (payload[15] & 0xFF) << 20 |
+            (payload[16] & 0xFE) << 12 |
+            (payload[17] & 0xFF) << 5 |
+            (payload[18] & 0xFE) >>> 3;
+          pes.dts *= 4; // Left shift by 2
+          pes.dts += (payload[18] & 0x06) >>> 1; // OR by the two LSBs
+        }
+      }
+      // the data section starts immediately after the PES header.
+      // pes_header_data_length specifies the number of header bytes
+      // that follow the last byte of the field.
+      pes.data = payload.subarray(9 + payload[8]);
+    },
+    /**
+      * Pass completely parsed PES packets to the next stream in the pipeline
+     **/
+    flushStream = function(stream, type, forceFlush) {
+      var
+        packetData = new Uint8Array(stream.size),
+        event = {
+          type: type
+        },
+        i = 0,
+        offset = 0,
+        packetFlushable = false,
+        fragment;
+
+      // do nothing if there is not enough buffered data for a complete
+      // PES header
+      if (!stream.data.length || stream.size < 9) {
+        return;
+      }
+      event.trackId = stream.data[0].pid;
 
-  if (stream._transformState.transforming) throw new Error('Calling transform done when still transforming');
+      // reassemble the packet
+      for (i = 0; i < stream.data.length; i++) {
+        fragment = stream.data[i];
 
-  return stream.push(null);
-}
-},{"./_stream_duplex":22,"core-util-is":9,"inherits":12}],26:[function(require,module,exports){
-(function (process,global,setImmediate){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
+        packetData.set(fragment.data, offset);
+        offset += fragment.data.byteLength;
+      }
 
-// A bit simpler than readable streams.
-// Implement an async ._write(chunk, encoding, cb), and it'll handle all
-// the drain event emission and buffering.
+      // parse assembled packet's PES header
+      parsePes(packetData, event);
 
-'use strict';
+      // non-video PES packets MUST have a non-zero PES_packet_length
+      // check that there is enough stream data to fill the packet
+      packetFlushable = type === 'video' || event.packetLength <= stream.size;
 
-/*<replacement>*/
+      // flush pending packets if the conditions are right
+      if (forceFlush || packetFlushable) {
+        stream.size = 0;
+        stream.data.length = 0;
+      }
 
-var pna = require('process-nextick-args');
-/*</replacement>*/
+      // only emit packets that are complete. this is to avoid assembling
+      // incomplete PES packets due to poor segmentation
+      if (packetFlushable) {
+        self.trigger('data', event);
+      }
+    };
 
-module.exports = Writable;
+  ElementaryStream.prototype.init.call(this);
+
+  /**
+   * Identifies M2TS packet types and parses PES packets using metadata
+   * parsed from the PMT
+   **/
+  this.push = function(data) {
+    ({
+      pat: function() {
+        // we have to wait for the PMT to arrive as well before we
+        // have any meaningful metadata
+      },
+      pes: function() {
+        var stream, streamType;
+
+        switch (data.streamType) {
+        case StreamTypes.H264_STREAM_TYPE:
+          stream = video;
+          streamType = 'video';
+          break;
+        case StreamTypes.ADTS_STREAM_TYPE:
+          stream = audio;
+          streamType = 'audio';
+          break;
+        case StreamTypes.METADATA_STREAM_TYPE:
+          stream = timedMetadata;
+          streamType = 'timed-metadata';
+          break;
+        default:
+          // ignore unknown stream types
+          return;
+        }
 
-/* <replacement> */
-function WriteReq(chunk, encoding, cb) {
-  this.chunk = chunk;
-  this.encoding = encoding;
-  this.callback = cb;
-  this.next = null;
-}
+        // if a new packet is starting, we can flush the completed
+        // packet
+        if (data.payloadUnitStartIndicator) {
+          flushStream(stream, streamType, true);
+        }
 
-// It seems a linked list but it is not
-// there will be only 2 of these for each stream
-function CorkedRequest(state) {
-  var _this = this;
+        // buffer this fragment until we are sure we've received the
+        // complete payload
+        stream.data.push(data);
+        stream.size += data.data.byteLength;
+      },
+      pmt: function() {
+        var
+          event = {
+            type: 'metadata',
+            tracks: []
+          };
+
+        programMapTable = data.programMapTable;
+
+        // translate audio and video streams to tracks
+        if (programMapTable.video !== null) {
+          event.tracks.push({
+            timelineStartInfo: {
+              baseMediaDecodeTime: 0
+            },
+            id: +programMapTable.video,
+            codec: 'avc',
+            type: 'video'
+          });
+        }
+        if (programMapTable.audio !== null) {
+          event.tracks.push({
+            timelineStartInfo: {
+              baseMediaDecodeTime: 0
+            },
+            id: +programMapTable.audio,
+            codec: 'adts',
+            type: 'audio'
+          });
+        }
 
-  this.next = null;
-  this.entry = null;
-  this.finish = function () {
-    onCorkedFinish(_this, state);
+        self.trigger('data', event);
+      }
+    })[data.type]();
   };
-}
-/* </replacement> */
 
-/*<replacement>*/
-var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : pna.nextTick;
-/*</replacement>*/
+  this.reset = function() {
+    video.size = 0;
+    video.data.length = 0;
+    audio.size = 0;
+    audio.data.length = 0;
+    this.trigger('reset');
+  };
 
-/*<replacement>*/
-var Duplex;
-/*</replacement>*/
+  /**
+   * Flush any remaining input. Video PES packets may be of variable
+   * length. Normally, the start of a new video packet can trigger the
+   * finalization of the previous packet. That is not possible if no
+   * more video is forthcoming, however. In that case, some other
+   * mechanism (like the end of the file) has to be employed. When it is
+   * clear that no additional data is forthcoming, calling this method
+   * will flush the buffered packets.
+   */
+  this.flushStreams_ = function() {
+    // !!THIS ORDER IS IMPORTANT!!
+    // video first then audio
+    flushStream(video, 'video');
+    flushStream(audio, 'audio');
+    flushStream(timedMetadata, 'timed-metadata');
+  };
 
-Writable.WritableState = WritableState;
+  this.flush = function() {
+    this.flushStreams_();
+    this.trigger('done');
+  };
+};
+ElementaryStream.prototype = new Stream();
+
+var m2ts = {
+  PAT_PID: 0x0000,
+  MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH,
+  TransportPacketStream: TransportPacketStream,
+  TransportParseStream: TransportParseStream,
+  ElementaryStream: ElementaryStream,
+  TimestampRolloverStream: TimestampRolloverStream,
+  CaptionStream: CaptionStream.CaptionStream,
+  Cea608Stream: CaptionStream.Cea608Stream,
+  MetadataStream: require('./metadata-stream')
+};
 
-/*<replacement>*/
-var util = Object.create(require('core-util-is'));
-util.inherits = require('inherits');
-/*</replacement>*/
+for (var type in StreamTypes) {
+  if (StreamTypes.hasOwnProperty(type)) {
+    m2ts[type] = StreamTypes[type];
+  }
+}
 
-/*<replacement>*/
-var internalUtil = {
-  deprecate: require('util-deprecate')
-};
-/*</replacement>*/
+module.exports = m2ts;
 
-/*<replacement>*/
-var Stream = require('./internal/streams/stream');
-/*</replacement>*/
+},{"../utils/stream.js":60,"./caption-stream":34,"./metadata-stream":37,"./stream-types":39,"./timestamp-rollover-stream":40}],37:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Accepts program elementary stream (PES) data events and parses out
+ * ID3 metadata from them, if present.
+ * @see http://id3.org/id3v2.3.0
+ */
+'use strict';
+var
+  Stream = require('../utils/stream'),
+  StreamTypes = require('./stream-types'),
+  // return a percent-encoded representation of the specified byte range
+  // @see http://en.wikipedia.org/wiki/Percent-encoding
+  percentEncode = function(bytes, start, end) {
+    var i, result = '';
+    for (i = start; i < end; i++) {
+      result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
+    }
+    return result;
+  },
+  // return the string representation of the specified byte range,
+  // interpreted as UTf-8.
+  parseUtf8 = function(bytes, start, end) {
+    return decodeURIComponent(percentEncode(bytes, start, end));
+  },
+  // return the string representation of the specified byte range,
+  // interpreted as ISO-8859-1.
+  parseIso88591 = function(bytes, start, end) {
+    return unescape(percentEncode(bytes, start, end)); // jshint ignore:line
+  },
+  parseSyncSafeInteger = function(data) {
+    return (data[0] << 21) |
+            (data[1] << 14) |
+            (data[2] << 7) |
+            (data[3]);
+  },
+  tagParsers = {
+    TXXX: function(tag) {
+      var i;
+      if (tag.data[0] !== 3) {
+        // ignore frames with unrecognized character encodings
+        return;
+      }
 
-/*<replacement>*/
+      for (i = 1; i < tag.data.length; i++) {
+        if (tag.data[i] === 0) {
+          // parse the text fields
+          tag.description = parseUtf8(tag.data, 1, i);
+          // do not include the null terminator in the tag value
+          tag.value = parseUtf8(tag.data, i + 1, tag.data.length).replace(/\0*$/, '');
+          break;
+        }
+      }
+      tag.data = tag.value;
+    },
+    WXXX: function(tag) {
+      var i;
+      if (tag.data[0] !== 3) {
+        // ignore frames with unrecognized character encodings
+        return;
+      }
 
-var Buffer = require('safe-buffer').Buffer;
-var OurUint8Array = global.Uint8Array || function () {};
-function _uint8ArrayToBuffer(chunk) {
-  return Buffer.from(chunk);
-}
-function _isUint8Array(obj) {
-  return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
-}
+      for (i = 1; i < tag.data.length; i++) {
+        if (tag.data[i] === 0) {
+          // parse the description and URL fields
+          tag.description = parseUtf8(tag.data, 1, i);
+          tag.url = parseUtf8(tag.data, i + 1, tag.data.length);
+          break;
+        }
+      }
+    },
+    PRIV: function(tag) {
+      var i;
+
+      for (i = 0; i < tag.data.length; i++) {
+        if (tag.data[i] === 0) {
+          // parse the description and URL fields
+          tag.owner = parseIso88591(tag.data, 0, i);
+          break;
+        }
+      }
+      tag.privateData = tag.data.subarray(i + 1);
+      tag.data = tag.privateData;
+    }
+  },
+  MetadataStream;
 
-/*</replacement>*/
+MetadataStream = function(options) {
+  var
+    settings = {
+      debug: !!(options && options.debug),
 
-var destroyImpl = require('./internal/streams/destroy');
+      // the bytes of the program-level descriptor field in MP2T
+      // see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
+      // program element descriptors"
+      descriptor: options && options.descriptor
+    },
+    // the total size in bytes of the ID3 tag being parsed
+    tagSize = 0,
+    // tag data that is not complete enough to be parsed
+    buffer = [],
+    // the total number of bytes currently in the buffer
+    bufferSize = 0,
+    i;
+
+  MetadataStream.prototype.init.call(this);
+
+  // calculate the text track in-band metadata track dispatch type
+  // https://html.spec.whatwg.org/multipage/embedded-content.html#steps-to-expose-a-media-resource-specific-text-track
+  this.dispatchType = StreamTypes.METADATA_STREAM_TYPE.toString(16);
+  if (settings.descriptor) {
+    for (i = 0; i < settings.descriptor.length; i++) {
+      this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
+    }
+  }
 
-util.inherits(Writable, Stream);
+  this.push = function(chunk) {
+    var tag, frameStart, frameSize, frame, i, frameHeader;
+    if (chunk.type !== 'timed-metadata') {
+      return;
+    }
 
-function nop() {}
+    // if data_alignment_indicator is set in the PES header,
+    // we must have the start of a new ID3 tag. Assume anything
+    // remaining in the buffer was malformed and throw it out
+    if (chunk.dataAlignmentIndicator) {
+      bufferSize = 0;
+      buffer.length = 0;
+    }
 
-function WritableState(options, stream) {
-  Duplex = Duplex || require('./_stream_duplex');
+    // ignore events that don't look like ID3 data
+    if (buffer.length === 0 &&
+        (chunk.data.length < 10 ||
+          chunk.data[0] !== 'I'.charCodeAt(0) ||
+          chunk.data[1] !== 'D'.charCodeAt(0) ||
+          chunk.data[2] !== '3'.charCodeAt(0))) {
+      if (settings.debug) {
+        // eslint-disable-next-line no-console
+        console.log('Skipping unrecognized metadata packet');
+      }
+      return;
+    }
 
-  options = options || {};
+    // add this chunk to the data we've collected so far
 
-  // Duplex streams are both readable and writable, but share
-  // the same options object.
-  // However, some cases require setting options to different
-  // values for the readable and the writable sides of the duplex stream.
-  // These options can be provided separately as readableXXX and writableXXX.
-  var isDuplex = stream instanceof Duplex;
+    buffer.push(chunk);
+    bufferSize += chunk.data.byteLength;
 
-  // object stream flag to indicate whether or not this stream
-  // contains buffers or objects.
-  this.objectMode = !!options.objectMode;
+    // grab the size of the entire frame from the ID3 header
+    if (buffer.length === 1) {
+      // the frame size is transmitted as a 28-bit integer in the
+      // last four bytes of the ID3 header.
+      // The most significant bit of each byte is dropped and the
+      // results concatenated to recover the actual value.
+      tagSize = parseSyncSafeInteger(chunk.data.subarray(6, 10));
 
-  if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode;
+      // ID3 reports the tag size excluding the header but it's more
+      // convenient for our comparisons to include it
+      tagSize += 10;
+    }
 
-  // the point at which write() starts returning false
-  // Note: 0 is a valid value, means that we always return false if
-  // the entire buffer is not flushed immediately on write()
-  var hwm = options.highWaterMark;
-  var writableHwm = options.writableHighWaterMark;
-  var defaultHwm = this.objectMode ? 16 : 16 * 1024;
+    // if the entire frame has not arrived, wait for more data
+    if (bufferSize < tagSize) {
+      return;
+    }
 
-  if (hwm || hwm === 0) this.highWaterMark = hwm;else if (isDuplex && (writableHwm || writableHwm === 0)) this.highWaterMark = writableHwm;else this.highWaterMark = defaultHwm;
+    // collect the entire frame so it can be parsed
+    tag = {
+      data: new Uint8Array(tagSize),
+      frames: [],
+      pts: buffer[0].pts,
+      dts: buffer[0].dts
+    };
+    for (i = 0; i < tagSize;) {
+      tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
+      i += buffer[0].data.byteLength;
+      bufferSize -= buffer[0].data.byteLength;
+      buffer.shift();
+    }
 
-  // cast to ints.
-  this.highWaterMark = Math.floor(this.highWaterMark);
+    // find the start of the first frame and the end of the tag
+    frameStart = 10;
+    if (tag.data[5] & 0x40) {
+      // advance the frame start past the extended header
+      frameStart += 4; // header size field
+      frameStart += parseSyncSafeInteger(tag.data.subarray(10, 14));
 
-  // if _final has been called
-  this.finalCalled = false;
+      // clip any padding off the end
+      tagSize -= parseSyncSafeInteger(tag.data.subarray(16, 20));
+    }
 
-  // drain event flag.
-  this.needDrain = false;
-  // at the start of calling end()
-  this.ending = false;
-  // when end() has been called, and returned
-  this.ended = false;
-  // when 'finish' is emitted
-  this.finished = false;
+    // parse one or more ID3 frames
+    // http://id3.org/id3v2.3.0#ID3v2_frame_overview
+    do {
+      // determine the number of bytes in this frame
+      frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
+      if (frameSize < 1) {
+         // eslint-disable-next-line no-console
+        return console.log('Malformed ID3 frame encountered. Skipping metadata parsing.');
+      }
+      frameHeader = String.fromCharCode(tag.data[frameStart],
+                                        tag.data[frameStart + 1],
+                                        tag.data[frameStart + 2],
+                                        tag.data[frameStart + 3]);
 
-  // has it been destroyed
-  this.destroyed = false;
 
-  // should we decode strings into buffers before passing to _write?
-  // this is here so that some node-core streams can optimize string
-  // handling at a lower level.
-  var noDecode = options.decodeStrings === false;
-  this.decodeStrings = !noDecode;
+      frame = {
+        id: frameHeader,
+        data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
+      };
+      frame.key = frame.id;
+      if (tagParsers[frame.id]) {
+        tagParsers[frame.id](frame);
+
+        // handle the special PRIV frame used to indicate the start
+        // time for raw AAC data
+        if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
+          var
+            d = frame.data,
+            size = ((d[3] & 0x01)  << 30) |
+                   (d[4]  << 22) |
+                   (d[5] << 14) |
+                   (d[6] << 6) |
+                   (d[7] >>> 2);
+
+          size *= 4;
+          size += d[7] & 0x03;
+          frame.timeStamp = size;
+          // in raw AAC, all subsequent data will be timestamped based
+          // on the value of this frame
+          // we couldn't have known the appropriate pts and dts before
+          // parsing this ID3 tag so set those values now
+          if (tag.pts === undefined && tag.dts === undefined) {
+            tag.pts = frame.timeStamp;
+            tag.dts = frame.timeStamp;
+          }
+          this.trigger('timestamp', frame);
+        }
+      }
+      tag.frames.push(frame);
 
-  // Crypto is kind of old and crusty.  Historically, its default string
-  // encoding is 'binary' so we have to make this configurable.
-  // Everything else in the universe uses 'utf8', though.
-  this.defaultEncoding = options.defaultEncoding || 'utf8';
+      frameStart += 10; // advance past the frame header
+      frameStart += frameSize; // advance past the frame body
+    } while (frameStart < tagSize);
+    this.trigger('data', tag);
+  };
+};
+MetadataStream.prototype = new Stream();
 
-  // not an actual buffer we keep track of, but a measurement
-  // of how much we're waiting to get pushed to some underlying
-  // socket or file.
-  this.length = 0;
+module.exports = MetadataStream;
 
-  // a flag to see when we're in the middle of a write.
-  this.writing = false;
+},{"../utils/stream":60,"./stream-types":39}],38:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Utilities to detect basic properties and metadata about TS Segments.
+ */
+'use strict';
 
-  // when true all writes will be buffered until .uncork() call
-  this.corked = 0;
+var StreamTypes = require('./stream-types.js');
 
-  // a flag to be able to tell if the onwrite cb is called immediately,
-  // or on a later tick.  We set this to true at first, because any
-  // actions that shouldn't happen until "later" should generally also
-  // not happen before the first write call.
-  this.sync = true;
+var parsePid = function(packet) {
+  var pid = packet[1] & 0x1f;
+  pid <<= 8;
+  pid |= packet[2];
+  return pid;
+};
 
-  // a flag to know if we're processing previously buffered items, which
-  // may call the _write() callback in the same tick, so that we don't
-  // end up in an overlapped onwrite situation.
-  this.bufferProcessing = false;
+var parsePayloadUnitStartIndicator = function(packet) {
+  return !!(packet[1] & 0x40);
+};
 
-  // the callback that's passed to _write(chunk,cb)
-  this.onwrite = function (er) {
-    onwrite(stream, er);
-  };
+var parseAdaptionField = function(packet) {
+  var offset = 0;
+  // if an adaption field is present, its length is specified by the
+  // fifth byte of the TS packet header. The adaptation field is
+  // used to add stuffing to PES packets that don't fill a complete
+  // TS packet, and to specify some forms of timing and control data
+  // that we do not currently use.
+  if (((packet[3] & 0x30) >>> 4) > 0x01) {
+    offset += packet[4] + 1;
+  }
+  return offset;
+};
 
-  // the callback that the user supplies to write(chunk,encoding,cb)
-  this.writecb = null;
+var parseType = function(packet, pmtPid) {
+  var pid = parsePid(packet);
+  if (pid === 0) {
+    return 'pat';
+  } else if (pid === pmtPid) {
+    return 'pmt';
+  } else if (pmtPid) {
+    return 'pes';
+  }
+  return null;
+};
 
-  // the amount that is being written when _write is called.
-  this.writelen = 0;
+var parsePat = function(packet) {
+  var pusi = parsePayloadUnitStartIndicator(packet);
+  var offset = 4 + parseAdaptionField(packet);
 
-  this.bufferedRequest = null;
-  this.lastBufferedRequest = null;
+  if (pusi) {
+    offset += packet[offset] + 1;
+  }
 
-  // number of pending user-supplied write callbacks
-  // this must be 0 before 'finish' can be emitted
-  this.pendingcb = 0;
+  return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
+};
 
-  // emit prefinish if the only thing we're waiting for is _write cbs
-  // This is relevant for synchronous Transform streams
-  this.prefinished = false;
+var parsePmt = function(packet) {
+  var programMapTable = {};
+  var pusi = parsePayloadUnitStartIndicator(packet);
+  var payloadOffset = 4 + parseAdaptionField(packet);
 
-  // True if the error was already emitted and should not be thrown again
-  this.errorEmitted = false;
+  if (pusi) {
+    payloadOffset += packet[payloadOffset] + 1;
+  }
 
-  // count buffered requests
-  this.bufferedRequestCount = 0;
+  // PMTs can be sent ahead of the time when they should actually
+  // take effect. We don't believe this should ever be the case
+  // for HLS but we'll ignore "forward" PMT declarations if we see
+  // them. Future PMT declarations have the current_next_indicator
+  // set to zero.
+  if (!(packet[payloadOffset + 5] & 0x01)) {
+    return;
+  }
 
-  // allocate the first CorkedRequest, there is always
-  // one allocated and free to use, and we maintain at most two
-  this.corkedRequestsFree = new CorkedRequest(this);
-}
+  var sectionLength, tableEnd, programInfoLength;
+  // the mapping table ends at the end of the current section
+  sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
+  tableEnd = 3 + sectionLength - 4;
+
+  // to determine where the table is, we have to figure out how
+  // long the program info descriptors are
+  programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11];
+
+  // advance the offset to the first entry in the mapping table
+  var offset = 12 + programInfoLength;
+  while (offset < tableEnd) {
+    var i = payloadOffset + offset;
+    // add an entry that maps the elementary_pid to the stream_type
+    programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i];
+
+    // move to the next table entry
+    // skip past the elementary stream descriptors, if present
+    offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
+  }
+  return programMapTable;
+};
 
-WritableState.prototype.getBuffer = function getBuffer() {
-  var current = this.bufferedRequest;
-  var out = [];
-  while (current) {
-    out.push(current);
-    current = current.next;
+var parsePesType = function(packet, programMapTable) {
+  var pid = parsePid(packet);
+  var type = programMapTable[pid];
+  switch (type) {
+    case StreamTypes.H264_STREAM_TYPE:
+      return 'video';
+    case StreamTypes.ADTS_STREAM_TYPE:
+      return 'audio';
+    case StreamTypes.METADATA_STREAM_TYPE:
+      return 'timed-metadata';
+    default:
+      return null;
   }
-  return out;
 };
 
-(function () {
-  try {
-    Object.defineProperty(WritableState.prototype, 'buffer', {
-      get: internalUtil.deprecate(function () {
-        return this.getBuffer();
-      }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003')
-    });
-  } catch (_) {}
-})();
+var parsePesTime = function(packet) {
+  var pusi = parsePayloadUnitStartIndicator(packet);
+  if (!pusi) {
+    return null;
+  }
 
-// Test _writableState for inheritance to account for Duplex streams,
-// whose prototype chain only points to Readable.
-var realHasInstance;
-if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
-  realHasInstance = Function.prototype[Symbol.hasInstance];
-  Object.defineProperty(Writable, Symbol.hasInstance, {
-    value: function (object) {
-      if (realHasInstance.call(this, object)) return true;
-      if (this !== Writable) return false;
+  var offset = 4 + parseAdaptionField(packet);
+
+  if (offset >= packet.byteLength) {
+    // From the H 222.0 MPEG-TS spec
+    // "For transport stream packets carrying PES packets, stuffing is needed when there
+    //  is insufficient PES packet data to completely fill the transport stream packet
+    //  payload bytes. Stuffing is accomplished by defining an adaptation field longer than
+    //  the sum of the lengths of the data elements in it, so that the payload bytes
+    //  remaining after the adaptation field exactly accommodates the available PES packet
+    //  data."
+    //
+    // If the offset is >= the length of the packet, then the packet contains no data
+    // and instead is just adaption field stuffing bytes
+    return null;
+  }
 
-      return object && object._writableState instanceof WritableState;
+  var pes = null;
+  var ptsDtsFlags;
+
+  // PES packets may be annotated with a PTS value, or a PTS value
+  // and a DTS value. Determine what combination of values is
+  // available to work with.
+  ptsDtsFlags = packet[offset + 7];
+
+  // PTS and DTS are normally stored as a 33-bit number.  Javascript
+  // performs all bitwise operations on 32-bit integers but javascript
+  // supports a much greater range (52-bits) of integer using standard
+  // mathematical operations.
+  // We construct a 31-bit value using bitwise operators over the 31
+  // most significant bits and then multiply by 4 (equal to a left-shift
+  // of 2) before we add the final 2 least significant bits of the
+  // timestamp (equal to an OR.)
+  if (ptsDtsFlags & 0xC0) {
+    pes = {};
+    // the PTS and DTS are not written out directly. For information
+    // on how they are encoded, see
+    // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
+    pes.pts = (packet[offset + 9] & 0x0E) << 27 |
+      (packet[offset + 10] & 0xFF) << 20 |
+      (packet[offset + 11] & 0xFE) << 12 |
+      (packet[offset + 12] & 0xFF) <<  5 |
+      (packet[offset + 13] & 0xFE) >>>  3;
+    pes.pts *= 4; // Left shift by 2
+    pes.pts += (packet[offset + 13] & 0x06) >>> 1; // OR by the two LSBs
+    pes.dts = pes.pts;
+    if (ptsDtsFlags & 0x40) {
+      pes.dts = (packet[offset + 14] & 0x0E) << 27 |
+        (packet[offset + 15] & 0xFF) << 20 |
+        (packet[offset + 16] & 0xFE) << 12 |
+        (packet[offset + 17] & 0xFF) << 5 |
+        (packet[offset + 18] & 0xFE) >>> 3;
+      pes.dts *= 4; // Left shift by 2
+      pes.dts += (packet[offset + 18] & 0x06) >>> 1; // OR by the two LSBs
     }
-  });
-} else {
-  realHasInstance = function (object) {
-    return object instanceof this;
-  };
-}
-
-function Writable(options) {
-  Duplex = Duplex || require('./_stream_duplex');
+  }
+  return pes;
+};
 
-  // Writable ctor is applied to Duplexes, too.
-  // `realHasInstance` is necessary because using plain `instanceof`
-  // would return false, as no `_writableState` property is attached.
+var parseNalUnitType = function(type) {
+  switch (type) {
+    case 0x05:
+      return 'slice_layer_without_partitioning_rbsp_idr';
+    case 0x06:
+      return 'sei_rbsp';
+    case 0x07:
+      return 'seq_parameter_set_rbsp';
+    case 0x08:
+      return 'pic_parameter_set_rbsp';
+    case 0x09:
+      return 'access_unit_delimiter_rbsp';
+    default:
+      return null;
+  }
+};
 
-  // Trying to use the custom `instanceof` for Writable here will also break the
-  // Node.js LazyTransform implementation, which has a non-trivial getter for
-  // `_writableState` that would lead to infinite recursion.
-  if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) {
-    return new Writable(options);
+var videoPacketContainsKeyFrame = function(packet) {
+  var offset = 4 + parseAdaptionField(packet);
+  var frameBuffer = packet.subarray(offset);
+  var frameI = 0;
+  var frameSyncPoint = 0;
+  var foundKeyFrame = false;
+  var nalType;
+
+  // advance the sync point to a NAL start, if necessary
+  for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
+    if (frameBuffer[frameSyncPoint + 2] === 1) {
+      // the sync point is properly aligned
+      frameI = frameSyncPoint + 5;
+      break;
+    }
   }
 
-  this._writableState = new WritableState(options, this);
+  while (frameI < frameBuffer.byteLength) {
+    // look at the current byte to determine if we've hit the end of
+    // a NAL unit boundary
+    switch (frameBuffer[frameI]) {
+    case 0:
+      // skip past non-sync sequences
+      if (frameBuffer[frameI - 1] !== 0) {
+        frameI += 2;
+        break;
+      } else if (frameBuffer[frameI - 2] !== 0) {
+        frameI++;
+        break;
+      }
 
-  // legacy.
-  this.writable = true;
+      if (frameSyncPoint + 3 !== frameI - 2) {
+        nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
+        if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
+          foundKeyFrame = true;
+        }
+      }
 
-  if (options) {
-    if (typeof options.write === 'function') this._write = options.write;
+      // drop trailing zeroes
+      do {
+        frameI++;
+      } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
+      frameSyncPoint = frameI - 2;
+      frameI += 3;
+      break;
+    case 1:
+      // skip past non-sync sequences
+      if (frameBuffer[frameI - 1] !== 0 ||
+          frameBuffer[frameI - 2] !== 0) {
+        frameI += 3;
+        break;
+      }
 
-    if (typeof options.writev === 'function') this._writev = options.writev;
+      nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
+      if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
+        foundKeyFrame = true;
+      }
+      frameSyncPoint = frameI - 2;
+      frameI += 3;
+      break;
+    default:
+      // the current byte isn't a one or zero, so it cannot be part
+      // of a sync sequence
+      frameI += 3;
+      break;
+    }
+  }
+  frameBuffer = frameBuffer.subarray(frameSyncPoint);
+  frameI -= frameSyncPoint;
+  frameSyncPoint = 0;
+  // parse the final nal
+  if (frameBuffer && frameBuffer.byteLength > 3) {
+    nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
+    if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
+      foundKeyFrame = true;
+    }
+  }
 
-    if (typeof options.destroy === 'function') this._destroy = options.destroy;
+  return foundKeyFrame;
+};
 
-    if (typeof options.final === 'function') this._final = options.final;
-  }
 
-  Stream.call(this);
-}
+module.exports = {
+  parseType: parseType,
+  parsePat: parsePat,
+  parsePmt: parsePmt,
+  parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
+  parsePesType: parsePesType,
+  parsePesTime: parsePesTime,
+  videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
+};
 
-// Otherwise people can pipe Writable streams, which is just wrong.
-Writable.prototype.pipe = function () {
-  this.emit('error', new Error('Cannot pipe, not readable'));
+},{"./stream-types.js":39}],39:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
+
+module.exports = {
+  H264_STREAM_TYPE: 0x1B,
+  ADTS_STREAM_TYPE: 0x0F,
+  METADATA_STREAM_TYPE: 0x15
 };
 
-function writeAfterEnd(stream, cb) {
-  var er = new Error('write after end');
-  // TODO: defer error events consistently everywhere, not just the cb
-  stream.emit('error', er);
-  pna.nextTick(cb, er);
-}
+},{}],40:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Accepts program elementary stream (PES) data events and corrects
+ * decode and presentation time stamps to account for a rollover
+ * of the 33 bit value.
+ */
 
-// Checks that a user-supplied chunk is valid, especially for the particular
-// mode the stream is in. Currently this means that `null` is never accepted
-// and undefined/non-string values are only allowed in object mode.
-function validChunk(stream, state, chunk, cb) {
-  var valid = true;
-  var er = false;
+'use strict';
 
-  if (chunk === null) {
-    er = new TypeError('May not write null values to stream');
-  } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
-    er = new TypeError('Invalid non-string/buffer chunk');
-  }
-  if (er) {
-    stream.emit('error', er);
-    pna.nextTick(cb, er);
-    valid = false;
-  }
-  return valid;
-}
+var Stream = require('../utils/stream');
 
-Writable.prototype.write = function (chunk, encoding, cb) {
-  var state = this._writableState;
-  var ret = false;
-  var isBuf = !state.objectMode && _isUint8Array(chunk);
+var MAX_TS = 8589934592;
 
-  if (isBuf && !Buffer.isBuffer(chunk)) {
-    chunk = _uint8ArrayToBuffer(chunk);
-  }
+var RO_THRESH = 4294967296;
 
-  if (typeof encoding === 'function') {
-    cb = encoding;
-    encoding = null;
-  }
+var TYPE_SHARED = 'shared';
 
-  if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
+var handleRollover = function(value, reference) {
+  var direction = 1;
 
-  if (typeof cb !== 'function') cb = nop;
+  if (value > reference) {
+    // If the current timestamp value is greater than our reference timestamp and we detect a
+    // timestamp rollover, this means the roll over is happening in the opposite direction.
+    // Example scenario: Enter a long stream/video just after a rollover occurred. The reference
+    // point will be set to a small number, e.g. 1. The user then seeks backwards over the
+    // rollover point. In loading this segment, the timestamp values will be very large,
+    // e.g. 2^33 - 1. Since this comes before the data we loaded previously, we want to adjust
+    // the time stamp to be `value - 2^33`.
+    direction = -1;
+  }
 
-  if (state.ended) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) {
-    state.pendingcb++;
-    ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
+  // Note: A seek forwards or back that is greater than the RO_THRESH (2^32, ~13 hours) will
+  // cause an incorrect adjustment.
+  while (Math.abs(reference - value) > RO_THRESH) {
+    value += (direction * MAX_TS);
   }
 
-  return ret;
+  return value;
 };
 
-Writable.prototype.cork = function () {
-  var state = this._writableState;
+var TimestampRolloverStream = function(type) {
+  var lastDTS, referenceDTS;
 
-  state.corked++;
-};
+  TimestampRolloverStream.prototype.init.call(this);
 
-Writable.prototype.uncork = function () {
-  var state = this._writableState;
+  // The "shared" type is used in cases where a stream will contain muxed
+  // video and audio. We could use `undefined` here, but having a string
+  // makes debugging a little clearer.
+  this.type_ = type || TYPE_SHARED;
 
-  if (state.corked) {
-    state.corked--;
+  this.push = function(data) {
 
-    if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
-  }
+    // Any "shared" rollover streams will accept _all_ data. Otherwise,
+    // streams will only accept data that matches their type.
+    if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
+      return;
+    }
+
+    if (referenceDTS === undefined) {
+      referenceDTS = data.dts;
+    }
+
+    data.dts = handleRollover(data.dts, referenceDTS);
+    data.pts = handleRollover(data.pts, referenceDTS);
+
+    lastDTS = data.dts;
+
+    this.trigger('data', data);
+  };
+
+  this.flush = function() {
+    referenceDTS = lastDTS;
+    this.trigger('done');
+  };
+
+  this.endTimeline = function() {
+    this.flush();
+    this.trigger('endedtimeline');
+  };
+
+  this.discontinuity = function() {
+    referenceDTS = void 0;
+    lastDTS = void 0;
+  };
+
+  this.reset = function() {
+    this.discontinuity();
+    this.trigger('reset');
+  };
 };
 
-Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
-  // node::ParseEncoding() requires lower case.
-  if (typeof encoding === 'string') encoding = encoding.toLowerCase();
-  if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding);
-  this._writableState.defaultEncoding = encoding;
-  return this;
+TimestampRolloverStream.prototype = new Stream();
+
+module.exports = {
+  TimestampRolloverStream: TimestampRolloverStream,
+  handleRollover: handleRollover
 };
 
-function decodeChunk(state, chunk, encoding) {
-  if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
-    chunk = Buffer.from(chunk, encoding);
+},{"../utils/stream":60}],41:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+var coneOfSilence = require('../data/silence');
+var clock = require('../utils/clock');
+
+/**
+ * Sum the `byteLength` properties of the data in each AAC frame
+ */
+var sumFrameByteLengths = function(array) {
+  var
+    i,
+    currentObj,
+    sum = 0;
+
+  // sum the byteLength's all each nal unit in the frame
+  for (i = 0; i < array.length; i++) {
+    currentObj = array[i];
+    sum += currentObj.data.byteLength;
   }
-  return chunk;
-}
 
-Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
-  // making it explicit this property is not enumerable
-  // because otherwise some prototype manipulation in
-  // userland will fail
-  enumerable: false,
-  get: function () {
-    return this._writableState.highWaterMark;
+  return sum;
+};
+
+// Possibly pad (prefix) the audio track with silence if appending this track
+// would lead to the introduction of a gap in the audio buffer
+var prefixWithSilence = function(
+  track,
+  frames,
+  audioAppendStartTs,
+  videoBaseMediaDecodeTime
+) {
+  var
+    baseMediaDecodeTimeTs,
+    frameDuration = 0,
+    audioGapDuration = 0,
+    audioFillFrameCount = 0,
+    audioFillDuration = 0,
+    silentFrame,
+    i,
+    firstFrame;
+
+  if (!frames.length) {
+    return;
   }
-});
 
-// if we're already writing something, then just put this
-// in the queue, and wait our turn.  Otherwise, call _write
-// If we return false, then we need a drain event, so set that flag.
-function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
-  if (!isBuf) {
-    var newChunk = decodeChunk(state, chunk, encoding);
-    if (chunk !== newChunk) {
-      isBuf = true;
-      encoding = 'buffer';
-      chunk = newChunk;
-    }
+  baseMediaDecodeTimeTs =
+    clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate);
+  // determine frame clock duration based on sample rate, round up to avoid overfills
+  frameDuration = Math.ceil(clock.ONE_SECOND_IN_TS / (track.samplerate / 1024));
+
+  if (audioAppendStartTs && videoBaseMediaDecodeTime) {
+    // insert the shortest possible amount (audio gap or audio to video gap)
+    audioGapDuration =
+      baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime);
+    // number of full frames in the audio gap
+    audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
+    audioFillDuration = audioFillFrameCount * frameDuration;
   }
-  var len = state.objectMode ? 1 : chunk.length;
 
-  state.length += len;
+  // don't attempt to fill gaps smaller than a single frame or larger
+  // than a half second
+  if (audioFillFrameCount < 1 || audioFillDuration > clock.ONE_SECOND_IN_TS / 2) {
+    return;
+  }
 
-  var ret = state.length < state.highWaterMark;
-  // we must ensure that previous needDrain will not be reset to false.
-  if (!ret) state.needDrain = true;
+  silentFrame = coneOfSilence()[track.samplerate];
 
-  if (state.writing || state.corked) {
-    var last = state.lastBufferedRequest;
-    state.lastBufferedRequest = {
-      chunk: chunk,
-      encoding: encoding,
-      isBuf: isBuf,
-      callback: cb,
-      next: null
-    };
-    if (last) {
-      last.next = state.lastBufferedRequest;
-    } else {
-      state.bufferedRequest = state.lastBufferedRequest;
-    }
-    state.bufferedRequestCount += 1;
-  } else {
-    doWrite(stream, state, false, len, chunk, encoding, cb);
+  if (!silentFrame) {
+    // we don't have a silent frame pregenerated for the sample rate, so use a frame
+    // from the content instead
+    silentFrame = frames[0].data;
   }
 
-  return ret;
-}
+  for (i = 0; i < audioFillFrameCount; i++) {
+    firstFrame = frames[0];
 
-function doWrite(stream, state, writev, len, chunk, encoding, cb) {
-  state.writelen = len;
-  state.writecb = cb;
-  state.writing = true;
-  state.sync = true;
-  if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
-  state.sync = false;
-}
+    frames.splice(0, 0, {
+      data: silentFrame,
+      dts: firstFrame.dts - frameDuration,
+      pts: firstFrame.pts - frameDuration
+    });
+  }
 
-function onwriteError(stream, state, sync, er, cb) {
-  --state.pendingcb;
+  track.baseMediaDecodeTime -=
+    Math.floor(clock.videoTsToAudioTs(audioFillDuration, track.samplerate));
+};
 
-  if (sync) {
-    // defer the callback if we are being called synchronously
-    // to avoid piling up things on the stack
-    pna.nextTick(cb, er);
-    // this can emit finish, and it will always happen
-    // after error
-    pna.nextTick(finishMaybe, stream, state);
-    stream._writableState.errorEmitted = true;
-    stream.emit('error', er);
-  } else {
-    // the caller expect this to happen before if
-    // it is async
-    cb(er);
-    stream._writableState.errorEmitted = true;
-    stream.emit('error', er);
-    // this can emit finish, but finish must
-    // always follow error
-    finishMaybe(stream, state);
+// If the audio segment extends before the earliest allowed dts
+// value, remove AAC frames until starts at or after the earliest
+// allowed DTS so that we don't end up with a negative baseMedia-
+// DecodeTime for the audio track
+var trimAdtsFramesByEarliestDts = function(adtsFrames, track, earliestAllowedDts) {
+  if (track.minSegmentDts >= earliestAllowedDts) {
+    return adtsFrames;
   }
-}
 
-function onwriteStateUpdate(state) {
-  state.writing = false;
-  state.writecb = null;
-  state.length -= state.writelen;
-  state.writelen = 0;
-}
+  // We will need to recalculate the earliest segment Dts
+  track.minSegmentDts = Infinity;
 
-function onwrite(stream, er) {
-  var state = stream._writableState;
-  var sync = state.sync;
-  var cb = state.writecb;
+  return adtsFrames.filter(function(currentFrame) {
+    // If this is an allowed frame, keep it and record it's Dts
+    if (currentFrame.dts >= earliestAllowedDts) {
+      track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
+      track.minSegmentPts = track.minSegmentDts;
+      return true;
+    }
+    // Otherwise, discard it
+    return false;
+  });
+};
 
-  onwriteStateUpdate(state);
+// generate the track's raw mdat data from an array of frames
+var generateSampleTable = function(frames) {
+  var
+    i,
+    currentFrame,
+    samples = [];
+
+  for (i = 0; i < frames.length; i++) {
+    currentFrame = frames[i];
+    samples.push({
+      size: currentFrame.data.byteLength,
+      duration: 1024 // For AAC audio, all samples contain 1024 samples
+    });
+  }
+  return samples;
+};
 
-  if (er) onwriteError(stream, state, sync, er, cb);else {
-    // Check if we're actually ready to finish, but don't emit yet
-    var finished = needFinish(state);
+// generate the track's sample table from an array of frames
+var concatenateFrameData = function(frames) {
+  var
+    i,
+    currentFrame,
+    dataOffset = 0,
+    data = new Uint8Array(sumFrameByteLengths(frames));
 
-    if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
-      clearBuffer(stream, state);
-    }
+  for (i = 0; i < frames.length; i++) {
+    currentFrame = frames[i];
 
-    if (sync) {
-      /*<replacement>*/
-      asyncWrite(afterWrite, stream, state, finished, cb);
-      /*</replacement>*/
-    } else {
-      afterWrite(stream, state, finished, cb);
-    }
+    data.set(currentFrame.data, dataOffset);
+    dataOffset += currentFrame.data.byteLength;
   }
-}
+  return data;
+};
 
-function afterWrite(stream, state, finished, cb) {
-  if (!finished) onwriteDrain(stream, state);
-  state.pendingcb--;
-  cb();
-  finishMaybe(stream, state);
-}
+module.exports = {
+  prefixWithSilence: prefixWithSilence,
+  trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
+  generateSampleTable: generateSampleTable,
+  concatenateFrameData: concatenateFrameData
+};
 
-// Must force callback to be called on nextTick, so that we don't
-// emit 'drain' before the write() consumer gets the 'false' return
-// value, and has a chance to attach a 'drain' listener.
-function onwriteDrain(stream, state) {
-  if (state.length === 0 && state.needDrain) {
-    state.needDrain = false;
-    stream.emit('drain');
-  }
-}
+},{"../data/silence":26,"../utils/clock":58}],42:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Reads in-band CEA-708 captions out of FMP4 segments.
+ * @see https://en.wikipedia.org/wiki/CEA-708
+ */
+'use strict';
 
-// if there's something in the buffer waiting, then process it
-function clearBuffer(stream, state) {
-  state.bufferProcessing = true;
-  var entry = state.bufferedRequest;
+var discardEmulationPreventionBytes = require('../tools/caption-packet-parser').discardEmulationPreventionBytes;
+var CaptionStream = require('../m2ts/caption-stream').CaptionStream;
+var probe = require('./probe');
+var inspect = require('../tools/mp4-inspector');
 
-  if (stream._writev && entry && entry.next) {
-    // Fast case, write everything using _writev()
-    var l = state.bufferedRequestCount;
-    var buffer = new Array(l);
-    var holder = state.corkedRequestsFree;
-    holder.entry = entry;
+/**
+  * Maps an offset in the mdat to a sample based on the the size of the samples.
+  * Assumes that `parseSamples` has been called first.
+  *
+  * @param {Number} offset - The offset into the mdat
+  * @param {Object[]} samples - An array of samples, parsed using `parseSamples`
+  * @return {?Object} The matching sample, or null if no match was found.
+  *
+  * @see ISO-BMFF-12/2015, Section 8.8.8
+ **/
+var mapToSample = function(offset, samples) {
+  var approximateOffset = offset;
 
-    var count = 0;
-    var allBuffers = true;
-    while (entry) {
-      buffer[count] = entry;
-      if (!entry.isBuf) allBuffers = false;
-      entry = entry.next;
-      count += 1;
+  for (var i = 0; i < samples.length; i++) {
+    var sample = samples[i];
+
+    if (approximateOffset < sample.size) {
+      return sample;
     }
-    buffer.allBuffers = allBuffers;
 
-    doWrite(stream, state, true, state.length, buffer, '', holder.finish);
+    approximateOffset -= sample.size;
+  }
 
-    // doWrite is almost always async, defer these to save a bit of time
-    // as the hot path ends with doWrite
-    state.pendingcb++;
-    state.lastBufferedRequest = null;
-    if (holder.next) {
-      state.corkedRequestsFree = holder.next;
-      holder.next = null;
-    } else {
-      state.corkedRequestsFree = new CorkedRequest(state);
+  return null;
+};
+
+/**
+  * Finds SEI nal units contained in a Media Data Box.
+  * Assumes that `parseSamples` has been called first.
+  *
+  * @param {Uint8Array} avcStream - The bytes of the mdat
+  * @param {Object[]} samples - The samples parsed out by `parseSamples`
+  * @param {Number} trackId - The trackId of this video track
+  * @return {Object[]} seiNals - the parsed SEI NALUs found.
+  *   The contents of the seiNal should match what is expected by
+  *   CaptionStream.push (nalUnitType, size, data, escapedRBSP, pts, dts)
+  *
+  * @see ISO-BMFF-12/2015, Section 8.1.1
+  * @see Rec. ITU-T H.264, 7.3.2.3.1
+ **/
+var findSeiNals = function(avcStream, samples, trackId) {
+  var
+    avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
+    result = [],
+    seiNal,
+    i,
+    length,
+    lastMatchedSample;
+
+  for (i = 0; i + 4 < avcStream.length; i += length) {
+    length = avcView.getUint32(i);
+    i += 4;
+
+    // Bail if this doesn't appear to be an H264 stream
+    if (length <= 0) {
+      continue;
     }
-    state.bufferedRequestCount = 0;
-  } else {
-    // Slow case, write chunks one-by-one
-    while (entry) {
-      var chunk = entry.chunk;
-      var encoding = entry.encoding;
-      var cb = entry.callback;
-      var len = state.objectMode ? 1 : chunk.length;
 
-      doWrite(stream, state, false, len, chunk, encoding, cb);
-      entry = entry.next;
-      state.bufferedRequestCount--;
-      // if we didn't call the onwrite immediately, then
-      // it means that we need to wait until it does.
-      // also, that means that the chunk and cb are currently
-      // being processed, so move the buffer counter past them.
-      if (state.writing) {
+    switch (avcStream[i] & 0x1F) {
+    case 0x06:
+      var data = avcStream.subarray(i + 1, i + 1 + length);
+      var matchingSample = mapToSample(i, samples);
+
+      seiNal = {
+        nalUnitType: 'sei_rbsp',
+        size: length,
+        data: data,
+        escapedRBSP: discardEmulationPreventionBytes(data),
+        trackId: trackId
+      };
+
+      if (matchingSample) {
+        seiNal.pts = matchingSample.pts;
+        seiNal.dts = matchingSample.dts;
+        lastMatchedSample = matchingSample;
+      } else if (lastMatchedSample) {
+        // If a matching sample cannot be found, use the last
+        // sample's values as they should be as close as possible
+        seiNal.pts = lastMatchedSample.pts;
+        seiNal.dts = lastMatchedSample.dts;
+      } else {
+        // eslint-disable-next-line no-console
+        console.log("We've encountered a nal unit without data. See mux.js#233.");
         break;
       }
-    }
 
-    if (entry === null) state.lastBufferedRequest = null;
+      result.push(seiNal);
+      break;
+    default:
+      break;
+    }
   }
 
-  state.bufferedRequest = entry;
-  state.bufferProcessing = false;
-}
-
-Writable.prototype._write = function (chunk, encoding, cb) {
-  cb(new Error('_write() is not implemented'));
+  return result;
 };
 
-Writable.prototype._writev = null;
+/**
+  * Parses sample information out of Track Run Boxes and calculates
+  * the absolute presentation and decode timestamps of each sample.
+  *
+  * @param {Array<Uint8Array>} truns - The Trun Run boxes to be parsed
+  * @param {Number} baseMediaDecodeTime - base media decode time from tfdt
+      @see ISO-BMFF-12/2015, Section 8.8.12
+  * @param {Object} tfhd - The parsed Track Fragment Header
+  *   @see inspect.parseTfhd
+  * @return {Object[]} the parsed samples
+  *
+  * @see ISO-BMFF-12/2015, Section 8.8.8
+ **/
+var parseSamples = function(truns, baseMediaDecodeTime, tfhd) {
+  var currentDts = baseMediaDecodeTime;
+  var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
+  var defaultSampleSize = tfhd.defaultSampleSize || 0;
+  var trackId = tfhd.trackId;
+  var allSamples = [];
+
+  truns.forEach(function(trun) {
+    // Note: We currently do not parse the sample table as well
+    // as the trun. It's possible some sources will require this.
+    // moov > trak > mdia > minf > stbl
+    var trackRun = inspect.parseTrun(trun);
+    var samples = trackRun.samples;
+
+    samples.forEach(function(sample) {
+      if (sample.duration === undefined) {
+        sample.duration = defaultSampleDuration;
+      }
+      if (sample.size === undefined) {
+        sample.size = defaultSampleSize;
+      }
+      sample.trackId = trackId;
+      sample.dts = currentDts;
+      if (sample.compositionTimeOffset === undefined) {
+        sample.compositionTimeOffset = 0;
+      }
+      sample.pts = currentDts + sample.compositionTimeOffset;
 
-Writable.prototype.end = function (chunk, encoding, cb) {
-  var state = this._writableState;
+      currentDts += sample.duration;
+    });
 
-  if (typeof chunk === 'function') {
-    cb = chunk;
-    chunk = null;
-    encoding = null;
-  } else if (typeof encoding === 'function') {
-    cb = encoding;
-    encoding = null;
-  }
+    allSamples = allSamples.concat(samples);
+  });
 
-  if (chunk !== null && chunk !== undefined) this.write(chunk, encoding);
+  return allSamples;
+};
 
-  // .end() fully uncorks
-  if (state.corked) {
-    state.corked = 1;
-    this.uncork();
-  }
+/**
+  * Parses out caption nals from an FMP4 segment's video tracks.
+  *
+  * @param {Uint8Array} segment - The bytes of a single segment
+  * @param {Number} videoTrackId - The trackId of a video track in the segment
+  * @return {Object.<Number, Object[]>} A mapping of video trackId to
+  *   a list of seiNals found in that track
+ **/
+var parseCaptionNals = function(segment, videoTrackId) {
+  // To get the samples
+  var trafs = probe.findBox(segment, ['moof', 'traf']);
+  // To get SEI NAL units
+  var mdats = probe.findBox(segment, ['mdat']);
+  var captionNals = {};
+  var mdatTrafPairs = [];
+
+  // Pair up each traf with a mdat as moofs and mdats are in pairs
+  mdats.forEach(function(mdat, index) {
+    var matchingTraf = trafs[index];
+    mdatTrafPairs.push({
+      mdat: mdat,
+      traf: matchingTraf
+    });
+  });
 
-  // ignore unnecessary end() calls.
-  if (!state.ending && !state.finished) endWritable(this, state, cb);
-};
+  mdatTrafPairs.forEach(function(pair) {
+    var mdat = pair.mdat;
+    var traf = pair.traf;
+    var tfhd = probe.findBox(traf, ['tfhd']);
+    // Exactly 1 tfhd per traf
+    var headerInfo = inspect.parseTfhd(tfhd[0]);
+    var trackId = headerInfo.trackId;
+    var tfdt = probe.findBox(traf, ['tfdt']);
+    // Either 0 or 1 tfdt per traf
+    var baseMediaDecodeTime = (tfdt.length > 0) ? inspect.parseTfdt(tfdt[0]).baseMediaDecodeTime : 0;
+    var truns = probe.findBox(traf, ['trun']);
+    var samples;
+    var seiNals;
+
+    // Only parse video data for the chosen video track
+    if (videoTrackId === trackId && truns.length > 0) {
+      samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
+
+      seiNals = findSeiNals(mdat, samples, trackId);
+
+      if (!captionNals[trackId]) {
+        captionNals[trackId] = [];
+      }
 
-function needFinish(state) {
-  return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
-}
-function callFinal(stream, state) {
-  stream._final(function (err) {
-    state.pendingcb--;
-    if (err) {
-      stream.emit('error', err);
+      captionNals[trackId] = captionNals[trackId].concat(seiNals);
     }
-    state.prefinished = true;
-    stream.emit('prefinish');
-    finishMaybe(stream, state);
   });
-}
-function prefinish(stream, state) {
-  if (!state.prefinished && !state.finalCalled) {
-    if (typeof stream._final === 'function') {
-      state.pendingcb++;
-      state.finalCalled = true;
-      pna.nextTick(callFinal, stream, state);
-    } else {
-      state.prefinished = true;
-      stream.emit('prefinish');
-    }
-  }
-}
 
-function finishMaybe(stream, state) {
-  var need = needFinish(state);
-  if (need) {
-    prefinish(stream, state);
-    if (state.pendingcb === 0) {
-      state.finished = true;
-      stream.emit('finish');
-    }
-  }
-  return need;
-}
+  return captionNals;
+};
 
-function endWritable(stream, state, cb) {
-  state.ending = true;
-  finishMaybe(stream, state);
-  if (cb) {
-    if (state.finished) pna.nextTick(cb);else stream.once('finish', cb);
-  }
-  state.ended = true;
-  stream.writable = false;
-}
+/**
+  * Parses out inband captions from an MP4 container and returns
+  * caption objects that can be used by WebVTT and the TextTrack API.
+  * @see https://developer.mozilla.org/en-US/docs/Web/API/VTTCue
+  * @see https://developer.mozilla.org/en-US/docs/Web/API/TextTrack
+  * Assumes that `probe.getVideoTrackIds` and `probe.timescale` have been called first
+  *
+  * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
+  * @param {Number} trackId - The id of the video track to parse
+  * @param {Number} timescale - The timescale for the video track from the init segment
+  *
+  * @return {?Object[]} parsedCaptions - A list of captions or null if no video tracks
+  * @return {Number} parsedCaptions[].startTime - The time to show the caption in seconds
+  * @return {Number} parsedCaptions[].endTime - The time to stop showing the caption in seconds
+  * @return {String} parsedCaptions[].text - The visible content of the caption
+ **/
+var parseEmbeddedCaptions = function(segment, trackId, timescale) {
+  var seiNals;
 
-function onCorkedFinish(corkReq, state, err) {
-  var entry = corkReq.entry;
-  corkReq.entry = null;
-  while (entry) {
-    var cb = entry.callback;
-    state.pendingcb--;
-    cb(err);
-    entry = entry.next;
-  }
-  if (state.corkedRequestsFree) {
-    state.corkedRequestsFree.next = corkReq;
-  } else {
-    state.corkedRequestsFree = corkReq;
+  // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
+  if (trackId === null) {
+    return null;
   }
-}
 
-Object.defineProperty(Writable.prototype, 'destroyed', {
-  get: function () {
-    if (this._writableState === undefined) {
+  seiNals = parseCaptionNals(segment, trackId);
+
+  return {
+    seiNals: seiNals[trackId],
+    timescale: timescale
+  };
+};
+
+/**
+  * Converts SEI NALUs into captions that can be used by video.js
+ **/
+var CaptionParser = function() {
+  var isInitialized = false;
+  var captionStream;
+
+  // Stores segments seen before trackId and timescale are set
+  var segmentCache;
+  // Stores video track ID of the track being parsed
+  var trackId;
+  // Stores the timescale of the track being parsed
+  var timescale;
+  // Stores captions parsed so far
+  var parsedCaptions;
+  // Stores whether we are receiving partial data or not
+  var parsingPartial;
+
+  /**
+    * A method to indicate whether a CaptionParser has been initalized
+    * @returns {Boolean}
+   **/
+  this.isInitialized = function() {
+    return isInitialized;
+  };
+
+  /**
+    * Initializes the underlying CaptionStream, SEI NAL parsing
+    * and management, and caption collection
+   **/
+  this.init = function(options) {
+    captionStream = new CaptionStream();
+    isInitialized = true;
+    parsingPartial = options ? options.isPartial : false;
+
+    // Collect dispatched captions
+    captionStream.on('data', function(event) {
+      // Convert to seconds in the source's timescale
+      event.startTime = event.startPts / timescale;
+      event.endTime = event.endPts / timescale;
+
+      parsedCaptions.captions.push(event);
+      parsedCaptions.captionStreams[event.stream] = true;
+    });
+  };
+
+  /**
+    * Determines if a new video track will be selected
+    * or if the timescale changed
+    * @return {Boolean}
+   **/
+  this.isNewInit = function(videoTrackIds, timescales) {
+    if ((videoTrackIds && videoTrackIds.length === 0) ||
+        (timescales && typeof timescales === 'object' &&
+          Object.keys(timescales).length === 0)) {
       return false;
     }
-    return this._writableState.destroyed;
-  },
-  set: function (value) {
-    // we ignore the value if the stream
-    // has not been initialized yet
-    if (!this._writableState) {
-      return;
-    }
 
-    // backward compatibility, the user is explicitly
-    // managing destroyed
-    this._writableState.destroyed = value;
-  }
-});
+    return trackId !== videoTrackIds[0] ||
+      timescale !== timescales[trackId];
+  };
 
-Writable.prototype.destroy = destroyImpl.destroy;
-Writable.prototype._undestroy = destroyImpl.undestroy;
-Writable.prototype._destroy = function (err, cb) {
-  this.end();
-  cb(err);
-};
-}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("timers").setImmediate)
-},{"./_stream_duplex":22,"./internal/streams/destroy":28,"./internal/streams/stream":29,"_process":21,"core-util-is":9,"inherits":12,"process-nextick-args":20,"safe-buffer":32,"timers":35,"util-deprecate":36}],27:[function(require,module,exports){
-'use strict';
+  /**
+    * Parses out SEI captions and interacts with underlying
+    * CaptionStream to return dispatched captions
+    *
+    * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
+    * @param {Number[]} videoTrackIds - A list of video tracks found in the init segment
+    * @param {Object.<Number, Number>} timescales - The timescales found in the init segment
+    * @see parseEmbeddedCaptions
+    * @see m2ts/caption-stream.js
+   **/
+  this.parse = function(segment, videoTrackIds, timescales) {
+    var parsedData;
+
+    if (!this.isInitialized()) {
+      return null;
+
+    // This is not likely to be a video segment
+    } else if (!videoTrackIds || !timescales) {
+      return null;
+
+    } else if (this.isNewInit(videoTrackIds, timescales)) {
+      // Use the first video track only as there is no
+      // mechanism to switch to other video tracks
+      trackId = videoTrackIds[0];
+      timescale = timescales[trackId];
+
+    // If an init segment has not been seen yet, hold onto segment
+    // data until we have one.
+    // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
+    } else if (trackId === null || !timescale) {
+      segmentCache.push(segment);
+      return null;
+    }
 
-function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
+    // Now that a timescale and trackId is set, parse cached segments
+    while (segmentCache.length > 0) {
+      var cachedSegment = segmentCache.shift();
 
-var Buffer = require('safe-buffer').Buffer;
-var util = require('util');
+      this.parse(cachedSegment, videoTrackIds, timescales);
+    }
 
-function copyBuffer(src, target, offset) {
-  src.copy(target, offset);
-}
+    parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
 
-module.exports = function () {
-  function BufferList() {
-    _classCallCheck(this, BufferList);
+    if (parsedData === null || !parsedData.seiNals) {
+      return null;
+    }
 
-    this.head = null;
-    this.tail = null;
-    this.length = 0;
-  }
+    this.pushNals(parsedData.seiNals);
+    // Force the parsed captions to be dispatched
+    this.flushStream();
 
-  BufferList.prototype.push = function push(v) {
-    var entry = { data: v, next: null };
-    if (this.length > 0) this.tail.next = entry;else this.head = entry;
-    this.tail = entry;
-    ++this.length;
+    return parsedCaptions;
   };
 
-  BufferList.prototype.unshift = function unshift(v) {
-    var entry = { data: v, next: this.head };
-    if (this.length === 0) this.tail = entry;
-    this.head = entry;
-    ++this.length;
-  };
+  /**
+    * Pushes SEI NALUs onto CaptionStream
+    * @param {Object[]} nals - A list of SEI nals parsed using `parseCaptionNals`
+    * Assumes that `parseCaptionNals` has been called first
+    * @see m2ts/caption-stream.js
+    **/
+  this.pushNals = function(nals) {
+    if (!this.isInitialized() || !nals || nals.length === 0) {
+      return null;
+    }
 
-  BufferList.prototype.shift = function shift() {
-    if (this.length === 0) return;
-    var ret = this.head.data;
-    if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
-    --this.length;
-    return ret;
+    nals.forEach(function(nal) {
+      captionStream.push(nal);
+    });
   };
 
-  BufferList.prototype.clear = function clear() {
-    this.head = this.tail = null;
-    this.length = 0;
+  /**
+    * Flushes underlying CaptionStream to dispatch processed, displayable captions
+    * @see m2ts/caption-stream.js
+   **/
+  this.flushStream = function() {
+    if (!this.isInitialized()) {
+      return null;
+    }
+
+    if (!parsingPartial) {
+      captionStream.flush();
+    } else {
+      captionStream.partialFlush();
+    }
   };
 
-  BufferList.prototype.join = function join(s) {
-    if (this.length === 0) return '';
-    var p = this.head;
-    var ret = '' + p.data;
-    while (p = p.next) {
-      ret += s + p.data;
-    }return ret;
+  /**
+    * Reset caption buckets for new data
+   **/
+  this.clearParsedCaptions = function() {
+    parsedCaptions.captions = [];
+    parsedCaptions.captionStreams = {};
   };
 
-  BufferList.prototype.concat = function concat(n) {
-    if (this.length === 0) return Buffer.alloc(0);
-    if (this.length === 1) return this.head.data;
-    var ret = Buffer.allocUnsafe(n >>> 0);
-    var p = this.head;
-    var i = 0;
-    while (p) {
-      copyBuffer(p.data, ret, i);
-      i += p.data.length;
-      p = p.next;
+  /**
+    * Resets underlying CaptionStream
+    * @see m2ts/caption-stream.js
+   **/
+  this.resetCaptionStream = function() {
+    if (!this.isInitialized()) {
+      return null;
     }
-    return ret;
-  };
 
-  return BufferList;
-}();
+    captionStream.reset();
+  };
 
-if (util && util.inspect && util.inspect.custom) {
-  module.exports.prototype[util.inspect.custom] = function () {
-    var obj = util.inspect({ length: this.length });
-    return this.constructor.name + ' ' + obj;
+  /**
+    * Convenience method to clear all captions flushed from the
+    * CaptionStream and still being parsed
+    * @see m2ts/caption-stream.js
+   **/
+  this.clearAllCaptions = function() {
+    this.clearParsedCaptions();
+    this.resetCaptionStream();
   };
-}
-},{"safe-buffer":32,"util":7}],28:[function(require,module,exports){
-'use strict';
 
-/*<replacement>*/
+  /**
+    * Reset caption parser
+   **/
+  this.reset = function() {
+    segmentCache = [];
+    trackId = null;
+    timescale = null;
+
+    if (!parsedCaptions) {
+      parsedCaptions = {
+        captions: [],
+        // CC1, CC2, CC3, CC4
+        captionStreams: {}
+      };
+    } else {
+      this.clearParsedCaptions();
+    }
 
-var pna = require('process-nextick-args');
-/*</replacement>*/
+    this.resetCaptionStream();
+  };
 
-// undocumented cb() API, needed for core, not for public API
-function destroy(err, cb) {
-  var _this = this;
+  this.reset();
+};
 
-  var readableDestroyed = this._readableState && this._readableState.destroyed;
-  var writableDestroyed = this._writableState && this._writableState.destroyed;
+module.exports = CaptionParser;
 
-  if (readableDestroyed || writableDestroyed) {
-    if (cb) {
-      cb(err);
-    } else if (err && (!this._writableState || !this._writableState.errorEmitted)) {
-      pna.nextTick(emitErrorNT, this, err);
+},{"../m2ts/caption-stream":34,"../tools/caption-packet-parser":53,"../tools/mp4-inspector":55,"./probe":46}],43:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+// Convert an array of nal units into an array of frames with each frame being
+// composed of the nal units that make up that frame
+// Also keep track of cummulative data about the frame from the nal units such
+// as the frame duration, starting pts, etc.
+var groupNalsIntoFrames = function(nalUnits) {
+  var
+    i,
+    currentNal,
+    currentFrame = [],
+    frames = [];
+
+  // TODO added for LHLS, make sure this is OK
+  frames.byteLength = 0;
+  frames.nalCount = 0;
+  frames.duration = 0;
+
+  currentFrame.byteLength = 0;
+
+  for (i = 0; i < nalUnits.length; i++) {
+    currentNal = nalUnits[i];
+
+    // Split on 'aud'-type nal units
+    if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
+      // Since the very first nal unit is expected to be an AUD
+      // only push to the frames array when currentFrame is not empty
+      if (currentFrame.length) {
+        currentFrame.duration = currentNal.dts - currentFrame.dts;
+        // TODO added for LHLS, make sure this is OK
+        frames.byteLength += currentFrame.byteLength;
+        frames.nalCount += currentFrame.length;
+        frames.duration += currentFrame.duration;
+        frames.push(currentFrame);
+      }
+      currentFrame = [currentNal];
+      currentFrame.byteLength = currentNal.data.byteLength;
+      currentFrame.pts = currentNal.pts;
+      currentFrame.dts = currentNal.dts;
+    } else {
+      // Specifically flag key frames for ease of use later
+      if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
+        currentFrame.keyFrame = true;
+      }
+      currentFrame.duration = currentNal.dts - currentFrame.dts;
+      currentFrame.byteLength += currentNal.data.byteLength;
+      currentFrame.push(currentNal);
     }
-    return this;
   }
 
-  // we set destroyed to true before firing error callbacks in order
-  // to make it re-entrance safe in case destroy() is called within callbacks
-
-  if (this._readableState) {
-    this._readableState.destroyed = true;
+  // For the last frame, use the duration of the previous frame if we
+  // have nothing better to go on
+  if (frames.length &&
+      (!currentFrame.duration ||
+       currentFrame.duration <= 0)) {
+    currentFrame.duration = frames[frames.length - 1].duration;
   }
 
-  // if this is a duplex stream mark the writable part as destroyed as well
-  if (this._writableState) {
-    this._writableState.destroyed = true;
-  }
+  // Push the final frame
+  // TODO added for LHLS, make sure this is OK
+  frames.byteLength += currentFrame.byteLength;
+  frames.nalCount += currentFrame.length;
+  frames.duration += currentFrame.duration;
 
-  this._destroy(err || null, function (err) {
-    if (!cb && err) {
-      pna.nextTick(emitErrorNT, _this, err);
-      if (_this._writableState) {
-        _this._writableState.errorEmitted = true;
+  frames.push(currentFrame);
+  return frames;
+};
+
+// Convert an array of frames into an array of Gop with each Gop being composed
+// of the frames that make up that Gop
+// Also keep track of cummulative data about the Gop from the frames such as the
+// Gop duration, starting pts, etc.
+var groupFramesIntoGops = function(frames) {
+  var
+    i,
+    currentFrame,
+    currentGop = [],
+    gops = [];
+
+  // We must pre-set some of the values on the Gop since we
+  // keep running totals of these values
+  currentGop.byteLength = 0;
+  currentGop.nalCount = 0;
+  currentGop.duration = 0;
+  currentGop.pts = frames[0].pts;
+  currentGop.dts = frames[0].dts;
+
+  // store some metadata about all the Gops
+  gops.byteLength = 0;
+  gops.nalCount = 0;
+  gops.duration = 0;
+  gops.pts = frames[0].pts;
+  gops.dts = frames[0].dts;
+
+  for (i = 0; i < frames.length; i++) {
+    currentFrame = frames[i];
+
+    if (currentFrame.keyFrame) {
+      // Since the very first frame is expected to be an keyframe
+      // only push to the gops array when currentGop is not empty
+      if (currentGop.length) {
+        gops.push(currentGop);
+        gops.byteLength += currentGop.byteLength;
+        gops.nalCount += currentGop.nalCount;
+        gops.duration += currentGop.duration;
       }
-    } else if (cb) {
-      cb(err);
-    }
-  });
 
-  return this;
-}
+      currentGop = [currentFrame];
+      currentGop.nalCount = currentFrame.length;
+      currentGop.byteLength = currentFrame.byteLength;
+      currentGop.pts = currentFrame.pts;
+      currentGop.dts = currentFrame.dts;
+      currentGop.duration = currentFrame.duration;
+    } else {
+      currentGop.duration += currentFrame.duration;
+      currentGop.nalCount += currentFrame.length;
+      currentGop.byteLength += currentFrame.byteLength;
+      currentGop.push(currentFrame);
+    }
+  }
 
-function undestroy() {
-  if (this._readableState) {
-    this._readableState.destroyed = false;
-    this._readableState.reading = false;
-    this._readableState.ended = false;
-    this._readableState.endEmitted = false;
+  if (gops.length && currentGop.duration <= 0) {
+    currentGop.duration = gops[gops.length - 1].duration;
   }
+  gops.byteLength += currentGop.byteLength;
+  gops.nalCount += currentGop.nalCount;
+  gops.duration += currentGop.duration;
 
-  if (this._writableState) {
-    this._writableState.destroyed = false;
-    this._writableState.ended = false;
-    this._writableState.ending = false;
-    this._writableState.finished = false;
-    this._writableState.errorEmitted = false;
+  // push the final Gop
+  gops.push(currentGop);
+  return gops;
+};
+
+/*
+ * Search for the first keyframe in the GOPs and throw away all frames
+ * until that keyframe. Then extend the duration of the pulled keyframe
+ * and pull the PTS and DTS of the keyframe so that it covers the time
+ * range of the frames that were disposed.
+ *
+ * @param {Array} gops video GOPs
+ * @returns {Array} modified video GOPs
+ */
+var extendFirstKeyFrame = function(gops) {
+  var currentGop;
+
+  if (!gops[0][0].keyFrame && gops.length > 1) {
+    // Remove the first GOP
+    currentGop = gops.shift();
+
+    gops.byteLength -= currentGop.byteLength;
+    gops.nalCount -= currentGop.nalCount;
+
+    // Extend the first frame of what is now the
+    // first gop to cover the time period of the
+    // frames we just removed
+    gops[0][0].dts = currentGop.dts;
+    gops[0][0].pts = currentGop.pts;
+    gops[0][0].duration += currentGop.duration;
   }
-}
 
-function emitErrorNT(self, err) {
-  self.emit('error', err);
-}
+  return gops;
+};
 
-module.exports = {
-  destroy: destroy,
-  undestroy: undestroy
+/**
+ * Default sample object
+ * see ISO/IEC 14496-12:2012, section 8.6.4.3
+ */
+var createDefaultSample = function() {
+  return {
+    size: 0,
+    flags: {
+      isLeading: 0,
+      dependsOn: 1,
+      isDependedOn: 0,
+      hasRedundancy: 0,
+      degradationPriority: 0,
+      isNonSyncSample: 1
+    }
+  };
 };
-},{"process-nextick-args":20}],29:[function(require,module,exports){
-module.exports = require('events').EventEmitter;
 
-},{"events":10}],30:[function(require,module,exports){
-exports = module.exports = require('./lib/_stream_readable.js');
-exports.Stream = exports;
-exports.Readable = exports;
-exports.Writable = require('./lib/_stream_writable.js');
-exports.Duplex = require('./lib/_stream_duplex.js');
-exports.Transform = require('./lib/_stream_transform.js');
-exports.PassThrough = require('./lib/_stream_passthrough.js');
+/*
+ * Collates information from a video frame into an object for eventual
+ * entry into an MP4 sample table.
+ *
+ * @param {Object} frame the video frame
+ * @param {Number} dataOffset the byte offset to position the sample
+ * @return {Object} object containing sample table info for a frame
+ */
+var sampleForFrame = function(frame, dataOffset) {
+  var sample = createDefaultSample();
+
+  sample.dataOffset = dataOffset;
+  sample.compositionTimeOffset = frame.pts - frame.dts;
+  sample.duration = frame.duration;
+  sample.size = 4 * frame.length; // Space for nal unit size
+  sample.size += frame.byteLength;
+
+  if (frame.keyFrame) {
+    sample.flags.dependsOn = 2;
+    sample.flags.isNonSyncSample = 0;
+  }
 
-},{"./lib/_stream_duplex.js":22,"./lib/_stream_passthrough.js":23,"./lib/_stream_readable.js":24,"./lib/_stream_transform.js":25,"./lib/_stream_writable.js":26}],31:[function(require,module,exports){
-/*! @license Rematrix v0.7.0
+  return sample;
+};
 
-	Copyright 2020 Julian Lloyd.
+// generate the track's sample table from an array of gops
+var generateSampleTable = function(gops, baseDataOffset) {
+  var
+    h, i,
+    sample,
+    currentGop,
+    currentFrame,
+    dataOffset = baseDataOffset || 0,
+    samples = [];
 
-	Permission is hereby granted, free of charge, to any person obtaining a copy
-	of this software and associated documentation files (the "Software"), to deal
-	in the Software without restriction, including without limitation the rights
-	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-	copies of the Software, and to permit persons to whom the Software is
-	furnished to do so, subject to the following conditions:
+  for (h = 0; h < gops.length; h++) {
+    currentGop = gops[h];
 
-	The above copyright notice and this permission notice shall be included in
-	all copies or substantial portions of the Software.
+    for (i = 0; i < currentGop.length; i++) {
+      currentFrame = currentGop[i];
 
-	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-	THE SOFTWARE.
-*/
-(function (global, factory) {
-  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
-  typeof define === 'function' && define.amd ? define(['exports'], factory) :
-  (global = global || self, factory(global.Rematrix = {}));
-}(this, (function (exports) { 'use strict';
+      sample = sampleForFrame(currentFrame, dataOffset);
 
-  function format(source) {
-    if (source && source.constructor === Array) {
-      var values = source
-        .filter(function (value) { return typeof value === 'number'; })
-        .filter(function (value) { return !isNaN(value); });
+      dataOffset += sample.size;
 
-      if (source.length === 6 && values.length === 6) {
-        var matrix = identity();
-        matrix[0] = values[0];
-        matrix[1] = values[1];
-        matrix[4] = values[2];
-        matrix[5] = values[3];
-        matrix[12] = values[4];
-        matrix[13] = values[5];
-        return matrix
-      } else if (source.length === 16 && values.length === 16) {
-        return source
-      }
+      samples.push(sample);
     }
-    throw new TypeError('Expected a `number[]` with length 6 or 16.')
   }
+  return samples;
+};
 
-  function fromString(source) {
-    if (typeof source === 'string') {
-      var match = source.match(/matrix(3d)?\(([^)]+)\)/);
-      if (match) {
-        var raw = match[2].split(', ').map(parseFloat);
-        return format(raw)
+// generate the track's raw mdat data from an array of gops
+var concatenateNalData = function(gops) {
+  var
+    h, i, j,
+    currentGop,
+    currentFrame,
+    currentNal,
+    dataOffset = 0,
+    nalsByteLength = gops.byteLength,
+    numberOfNals = gops.nalCount,
+    totalByteLength = nalsByteLength + 4 * numberOfNals,
+    data = new Uint8Array(totalByteLength),
+    view = new DataView(data.buffer);
+
+  // For each Gop..
+  for (h = 0; h < gops.length; h++) {
+    currentGop = gops[h];
+
+    // For each Frame..
+    for (i = 0; i < currentGop.length; i++) {
+      currentFrame = currentGop[i];
+
+      // For each NAL..
+      for (j = 0; j < currentFrame.length; j++) {
+        currentNal = currentFrame[j];
+
+        view.setUint32(dataOffset, currentNal.data.byteLength);
+        dataOffset += 4;
+        data.set(currentNal.data, dataOffset);
+        dataOffset += currentNal.data.byteLength;
       }
     }
-    throw new TypeError('Expected a string containing `matrix()` or `matrix3d()')
-  }
-
-  function identity() {
-    var matrix = [];
-    for (var i = 0; i < 16; i++) {
-      i % 5 == 0 ? matrix.push(1) : matrix.push(0);
-    }
-    return matrix
   }
+  return data;
+};
 
-  function inverse(source) {
-    var m = format(source);
-
-    var s0 = m[0] * m[5] - m[4] * m[1];
-    var s1 = m[0] * m[6] - m[4] * m[2];
-    var s2 = m[0] * m[7] - m[4] * m[3];
-    var s3 = m[1] * m[6] - m[5] * m[2];
-    var s4 = m[1] * m[7] - m[5] * m[3];
-    var s5 = m[2] * m[7] - m[6] * m[3];
-
-    var c5 = m[10] * m[15] - m[14] * m[11];
-    var c4 = m[9] * m[15] - m[13] * m[11];
-    var c3 = m[9] * m[14] - m[13] * m[10];
-    var c2 = m[8] * m[15] - m[12] * m[11];
-    var c1 = m[8] * m[14] - m[12] * m[10];
-    var c0 = m[8] * m[13] - m[12] * m[9];
+// generate the track's sample table from a frame
+var generateSampleTableForFrame = function(frame, baseDataOffset) {
+  var
+    sample,
+    dataOffset = baseDataOffset || 0,
+    samples = [];
 
-    var determinant = 1 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0);
+  sample = sampleForFrame(frame, dataOffset);
+  samples.push(sample);
 
-    if (isNaN(determinant) || determinant === Infinity) {
-      throw new Error('Inverse determinant attempted to divide by zero.')
-    }
+  return samples;
+};
 
-    return [
-      (m[5] * c5 - m[6] * c4 + m[7] * c3) * determinant,
-      (-m[1] * c5 + m[2] * c4 - m[3] * c3) * determinant,
-      (m[13] * s5 - m[14] * s4 + m[15] * s3) * determinant,
-      (-m[9] * s5 + m[10] * s4 - m[11] * s3) * determinant,
+// generate the track's raw mdat data from a frame
+var concatenateNalDataForFrame = function(frame) {
+  var
+    i,
+    currentNal,
+    dataOffset = 0,
+    nalsByteLength = frame.byteLength,
+    numberOfNals = frame.length,
+    totalByteLength = nalsByteLength + 4 * numberOfNals,
+    data = new Uint8Array(totalByteLength),
+    view = new DataView(data.buffer);
+
+  // For each NAL..
+  for (i = 0; i < frame.length; i++) {
+    currentNal = frame[i];
+
+    view.setUint32(dataOffset, currentNal.data.byteLength);
+    dataOffset += 4;
+    data.set(currentNal.data, dataOffset);
+    dataOffset += currentNal.data.byteLength;
+  }
 
-      (-m[4] * c5 + m[6] * c2 - m[7] * c1) * determinant,
-      (m[0] * c5 - m[2] * c2 + m[3] * c1) * determinant,
-      (-m[12] * s5 + m[14] * s2 - m[15] * s1) * determinant,
-      (m[8] * s5 - m[10] * s2 + m[11] * s1) * determinant,
+  return data;
+};
 
-      (m[4] * c4 - m[5] * c2 + m[7] * c0) * determinant,
-      (-m[0] * c4 + m[1] * c2 - m[3] * c0) * determinant,
-      (m[12] * s4 - m[13] * s2 + m[15] * s0) * determinant,
-      (-m[8] * s4 + m[9] * s2 - m[11] * s0) * determinant,
+module.exports = {
+  groupNalsIntoFrames: groupNalsIntoFrames,
+  groupFramesIntoGops: groupFramesIntoGops,
+  extendFirstKeyFrame: extendFirstKeyFrame,
+  generateSampleTable: generateSampleTable,
+  concatenateNalData: concatenateNalData,
+  generateSampleTableForFrame: generateSampleTableForFrame,
+  concatenateNalDataForFrame: concatenateNalDataForFrame
+};
 
-      (-m[4] * c3 + m[5] * c1 - m[6] * c0) * determinant,
-      (m[0] * c3 - m[1] * c1 + m[2] * c0) * determinant,
-      (-m[12] * s3 + m[13] * s1 - m[14] * s0) * determinant,
-      (m[8] * s3 - m[9] * s1 + m[10] * s0) * determinant ]
-  }
+},{}],44:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+module.exports = {
+  generator: require('./mp4-generator'),
+  probe: require('./probe'),
+  Transmuxer: require('./transmuxer').Transmuxer,
+  AudioSegmentStream: require('./transmuxer').AudioSegmentStream,
+  VideoSegmentStream: require('./transmuxer').VideoSegmentStream,
+  CaptionParser: require('./caption-parser')
+};
 
-  function multiply(matrixA, matrixB) {
-    var fma = format(matrixA);
-    var fmb = format(matrixB);
-    var product = [];
+},{"./caption-parser":42,"./mp4-generator":45,"./probe":46,"./transmuxer":48}],45:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Functions that generate fragmented MP4s suitable for use with Media
+ * Source Extensions.
+ */
+'use strict';
 
-    for (var i = 0; i < 4; i++) {
-      var row = [fma[i], fma[i + 4], fma[i + 8], fma[i + 12]];
-      for (var j = 0; j < 4; j++) {
-        var k = j * 4;
-        var col = [fmb[k], fmb[k + 1], fmb[k + 2], fmb[k + 3]];
-        var result = row[0] * col[0] + row[1] * col[1] + row[2] * col[2] + row[3] * col[3];
+var UINT32_MAX = Math.pow(2, 32) - 1;
 
-        product[i + k] = result;
-      }
-    }
+var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd,
+    trak, tkhd, mdia, mdhd, hdlr, sdtp, stbl, stsd, traf, trex,
+    trun, types, MAJOR_BRAND, MINOR_VERSION, AVC1_BRAND, VIDEO_HDLR,
+    AUDIO_HDLR, HDLR_TYPES, VMHD, SMHD, DREF, STCO, STSC, STSZ, STTS;
 
-    return product
-  }
+// pre-calculate constants
+(function() {
+  var i;
+  types = {
+    avc1: [], // codingname
+    avcC: [],
+    btrt: [],
+    dinf: [],
+    dref: [],
+    esds: [],
+    ftyp: [],
+    hdlr: [],
+    mdat: [],
+    mdhd: [],
+    mdia: [],
+    mfhd: [],
+    minf: [],
+    moof: [],
+    moov: [],
+    mp4a: [], // codingname
+    mvex: [],
+    mvhd: [],
+    pasp: [],
+    sdtp: [],
+    smhd: [],
+    stbl: [],
+    stco: [],
+    stsc: [],
+    stsd: [],
+    stsz: [],
+    stts: [],
+    styp: [],
+    tfdt: [],
+    tfhd: [],
+    traf: [],
+    trak: [],
+    trun: [],
+    trex: [],
+    tkhd: [],
+    vmhd: []
+  };
 
-  function perspective(distance) {
-    var matrix = identity();
-    matrix[11] = -1 / distance;
-    return matrix
+  // In environments where Uint8Array is undefined (e.g., IE8), skip set up so that we
+  // don't throw an error
+  if (typeof Uint8Array === 'undefined') {
+    return;
   }
 
-  function rotate(angle) {
-    return rotateZ(angle)
+  for (i in types) {
+    if (types.hasOwnProperty(i)) {
+      types[i] = [
+        i.charCodeAt(0),
+        i.charCodeAt(1),
+        i.charCodeAt(2),
+        i.charCodeAt(3)
+      ];
+    }
   }
 
-  function rotateX(angle) {
-    var theta = (Math.PI / 180) * angle;
-    var matrix = identity();
+  MAJOR_BRAND = new Uint8Array([
+    'i'.charCodeAt(0),
+    's'.charCodeAt(0),
+    'o'.charCodeAt(0),
+    'm'.charCodeAt(0)
+  ]);
+  AVC1_BRAND = new Uint8Array([
+    'a'.charCodeAt(0),
+    'v'.charCodeAt(0),
+    'c'.charCodeAt(0),
+    '1'.charCodeAt(0)
+  ]);
+  MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
+  VIDEO_HDLR = new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00, 0x00, 0x00, // pre_defined
+    0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x56, 0x69, 0x64, 0x65,
+    0x6f, 0x48, 0x61, 0x6e,
+    0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
+  ]);
+  AUDIO_HDLR = new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00, 0x00, 0x00, // pre_defined
+    0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x53, 0x6f, 0x75, 0x6e,
+    0x64, 0x48, 0x61, 0x6e,
+    0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
+  ]);
+  HDLR_TYPES = {
+    video: VIDEO_HDLR,
+    audio: AUDIO_HDLR
+  };
+  DREF = new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00, 0x00, 0x01, // entry_count
+    0x00, 0x00, 0x00, 0x0c, // entry_size
+    0x75, 0x72, 0x6c, 0x20, // 'url' type
+    0x00, // version 0
+    0x00, 0x00, 0x01 // entry_flags
+  ]);
+  SMHD = new Uint8Array([
+    0x00,             // version
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00,       // balance, 0 means centered
+    0x00, 0x00        // reserved
+  ]);
+  STCO = new Uint8Array([
+    0x00, // version
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00, 0x00, 0x00 // entry_count
+  ]);
+  STSC = STCO;
+  STSZ = new Uint8Array([
+    0x00, // version
+    0x00, 0x00, 0x00, // flags
+    0x00, 0x00, 0x00, 0x00, // sample_size
+    0x00, 0x00, 0x00, 0x00 // sample_count
+  ]);
+  STTS = STCO;
+  VMHD = new Uint8Array([
+    0x00, // version
+    0x00, 0x00, 0x01, // flags
+    0x00, 0x00, // graphicsmode
+    0x00, 0x00,
+    0x00, 0x00,
+    0x00, 0x00 // opcolor
+  ]);
+}());
 
-    matrix[5] = matrix[10] = Math.cos(theta);
-    matrix[6] = matrix[9] = Math.sin(theta);
-    matrix[9] *= -1;
+box = function(type) {
+  var
+    payload = [],
+    size = 0,
+    i,
+    result,
+    view;
 
-    return matrix
+  for (i = 1; i < arguments.length; i++) {
+    payload.push(arguments[i]);
   }
 
-  function rotateY(angle) {
-    var theta = (Math.PI / 180) * angle;
-    var matrix = identity();
-
-    matrix[0] = matrix[10] = Math.cos(theta);
-    matrix[2] = matrix[8] = Math.sin(theta);
-    matrix[2] *= -1;
+  i = payload.length;
 
-    return matrix
+  // calculate the total size we need to allocate
+  while (i--) {
+    size += payload[i].byteLength;
+  }
+  result = new Uint8Array(size + 8);
+  view = new DataView(result.buffer, result.byteOffset, result.byteLength);
+  view.setUint32(0, result.byteLength);
+  result.set(type, 4);
+
+  // copy the payload into the result
+  for (i = 0, size = 8; i < payload.length; i++) {
+    result.set(payload[i], size);
+    size += payload[i].byteLength;
   }
+  return result;
+};
 
-  function rotateZ(angle) {
-    var theta = (Math.PI / 180) * angle;
-    var matrix = identity();
+dinf = function() {
+  return box(types.dinf, box(types.dref, DREF));
+};
 
-    matrix[0] = matrix[5] = Math.cos(theta);
-    matrix[1] = matrix[4] = Math.sin(theta);
-    matrix[4] *= -1;
+esds = function(track) {
+  return box(types.esds, new Uint8Array([
+    0x00, // version
+    0x00, 0x00, 0x00, // flags
+
+    // ES_Descriptor
+    0x03, // tag, ES_DescrTag
+    0x19, // length
+    0x00, 0x00, // ES_ID
+    0x00, // streamDependenceFlag, URL_flag, reserved, streamPriority
+
+    // DecoderConfigDescriptor
+    0x04, // tag, DecoderConfigDescrTag
+    0x11, // length
+    0x40, // object type
+    0x15,  // streamType
+    0x00, 0x06, 0x00, // bufferSizeDB
+    0x00, 0x00, 0xda, 0xc0, // maxBitrate
+    0x00, 0x00, 0xda, 0xc0, // avgBitrate
+
+    // DecoderSpecificInfo
+    0x05, // tag, DecoderSpecificInfoTag
+    0x02, // length
+    // ISO/IEC 14496-3, AudioSpecificConfig
+    // for samplingFrequencyIndex see ISO/IEC 13818-7:2006, 8.1.3.2.2, Table 35
+    (track.audioobjecttype << 3) | (track.samplingfrequencyindex >>> 1),
+    (track.samplingfrequencyindex << 7) | (track.channelcount << 3),
+    0x06, 0x01, 0x02 // GASpecificConfig
+  ]));
+};
 
-    return matrix
-  }
+ftyp = function() {
+  return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
+};
 
-  function scale(scalar, scalarY) {
-    var matrix = identity();
+hdlr = function(type) {
+  return box(types.hdlr, HDLR_TYPES[type]);
+};
+mdat = function(data) {
+  return box(types.mdat, data);
+};
+mdhd = function(track) {
+  var result = new Uint8Array([
+    0x00,                   // version 0
+    0x00, 0x00, 0x00,       // flags
+    0x00, 0x00, 0x00, 0x02, // creation_time
+    0x00, 0x00, 0x00, 0x03, // modification_time
+    0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
+
+    (track.duration >>> 24) & 0xFF,
+    (track.duration >>> 16) & 0xFF,
+    (track.duration >>>  8) & 0xFF,
+    track.duration & 0xFF,  // duration
+    0x55, 0xc4,             // 'und' language (undetermined)
+    0x00, 0x00
+  ]);
+
+  // Use the sample rate from the track metadata, when it is
+  // defined. The sample rate can be parsed out of an ADTS header, for
+  // instance.
+  if (track.samplerate) {
+    result[12] = (track.samplerate >>> 24) & 0xFF;
+    result[13] = (track.samplerate >>> 16) & 0xFF;
+    result[14] = (track.samplerate >>>  8) & 0xFF;
+    result[15] = (track.samplerate)        & 0xFF;
+  }
 
-    matrix[0] = scalar;
-    matrix[5] = typeof scalarY === 'number' ? scalarY : scalar;
-
-    return matrix
+  return box(types.mdhd, result);
+};
+mdia = function(track) {
+  return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
+};
+mfhd = function(sequenceNumber) {
+  return box(types.mfhd, new Uint8Array([
+    0x00,
+    0x00, 0x00, 0x00, // flags
+    (sequenceNumber & 0xFF000000) >> 24,
+    (sequenceNumber & 0xFF0000) >> 16,
+    (sequenceNumber & 0xFF00) >> 8,
+    sequenceNumber & 0xFF // sequence_number
+  ]));
+};
+minf = function(track) {
+  return box(types.minf,
+             track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD),
+             dinf(),
+             stbl(track));
+};
+moof = function(sequenceNumber, tracks) {
+  var
+    trackFragments = [],
+    i = tracks.length;
+  // build traf boxes for each track fragment
+  while (i--) {
+    trackFragments[i] = traf(tracks[i]);
   }
+  return box.apply(null, [
+    types.moof,
+    mfhd(sequenceNumber)
+  ].concat(trackFragments));
+};
+/**
+ * Returns a movie box.
+ * @param tracks {array} the tracks associated with this movie
+ * @see ISO/IEC 14496-12:2012(E), section 8.2.1
+ */
+moov = function(tracks) {
+  var
+    i = tracks.length,
+    boxes = [];
 
-  function scaleX(scalar) {
-    var matrix = identity();
-    matrix[0] = scalar;
-    return matrix
+  while (i--) {
+    boxes[i] = trak(tracks[i]);
   }
 
-  function scaleY(scalar) {
-    var matrix = identity();
-    matrix[5] = scalar;
-    return matrix
-  }
+  return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
+};
+mvex = function(tracks) {
+  var
+    i = tracks.length,
+    boxes = [];
 
-  function scaleZ(scalar) {
-    var matrix = identity();
-    matrix[10] = scalar;
-    return matrix
+  while (i--) {
+    boxes[i] = trex(tracks[i]);
   }
+  return box.apply(null, [types.mvex].concat(boxes));
+};
+mvhd = function(duration) {
+  var
+    bytes = new Uint8Array([
+      0x00, // version 0
+      0x00, 0x00, 0x00, // flags
+      0x00, 0x00, 0x00, 0x01, // creation_time
+      0x00, 0x00, 0x00, 0x02, // modification_time
+      0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
+      (duration & 0xFF000000) >> 24,
+      (duration & 0xFF0000) >> 16,
+      (duration & 0xFF00) >> 8,
+      duration & 0xFF, // duration
+      0x00, 0x01, 0x00, 0x00, // 1.0 rate
+      0x01, 0x00, // 1.0 volume
+      0x00, 0x00, // reserved
+      0x00, 0x00, 0x00, 0x00, // reserved
+      0x00, 0x00, 0x00, 0x00, // reserved
+      0x00, 0x01, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x01, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, // pre_defined
+      0xff, 0xff, 0xff, 0xff // next_track_ID
+    ]);
+  return box(types.mvhd, bytes);
+};
 
-  function skew(angleX, angleY) {
-    var thetaX = (Math.PI / 180) * angleX;
-    var matrix = identity();
-
-    matrix[4] = Math.tan(thetaX);
-
-    if (angleY) {
-      var thetaY = (Math.PI / 180) * angleY;
-      matrix[1] = Math.tan(thetaY);
-    }
-
-    return matrix
-  }
+sdtp = function(track) {
+  var
+    samples = track.samples || [],
+    bytes = new Uint8Array(4 + samples.length),
+    flags,
+    i;
 
-  function skewX(angle) {
-    var theta = (Math.PI / 180) * angle;
-    var matrix = identity();
+  // leave the full box header (4 bytes) all zero
 
-    matrix[4] = Math.tan(theta);
+  // write the sample table
+  for (i = 0; i < samples.length; i++) {
+    flags = samples[i].flags;
 
-    return matrix
+    bytes[i + 4] = (flags.dependsOn << 4) |
+      (flags.isDependedOn << 2) |
+      (flags.hasRedundancy);
   }
 
-  function skewY(angle) {
-    var theta = (Math.PI / 180) * angle;
-    var matrix = identity();
+  return box(types.sdtp,
+             bytes);
+};
 
-    matrix[1] = Math.tan(theta);
+stbl = function(track) {
+  return box(types.stbl,
+             stsd(track),
+             box(types.stts, STTS),
+             box(types.stsc, STSC),
+             box(types.stsz, STSZ),
+             box(types.stco, STCO));
+};
 
-    return matrix
-  }
+(function() {
+  var videoSample, audioSample;
 
-  function toString(source) {
-    return ("matrix3d(" + (format(source).join(', ')) + ")")
-  }
+  stsd = function(track) {
 
-  function translate(distanceX, distanceY) {
-    var matrix = identity();
-    matrix[12] = distanceX;
+    return box(types.stsd, new Uint8Array([
+      0x00, // version 0
+      0x00, 0x00, 0x00, // flags
+      0x00, 0x00, 0x00, 0x01
+    ]), track.type === 'video' ? videoSample(track) : audioSample(track));
+  };
 
-    if (distanceY) {
-      matrix[13] = distanceY;
+  videoSample = function(track) {
+    var
+      sps = track.sps || [],
+      pps = track.pps || [],
+      sequenceParameterSets = [],
+      pictureParameterSets = [],
+      i,
+      avc1Box;
+
+    // assemble the SPSs
+    for (i = 0; i < sps.length; i++) {
+      sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
+      sequenceParameterSets.push((sps[i].byteLength & 0xFF)); // sequenceParameterSetLength
+      sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i])); // SPS
     }
 
-    return matrix
-  }
+    // assemble the PPSs
+    for (i = 0; i < pps.length; i++) {
+      pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
+      pictureParameterSets.push((pps[i].byteLength & 0xFF));
+      pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
+    }
 
-  function translate3d(distanceX, distanceY, distanceZ) {
-    var matrix = identity();
-    if (distanceX !== undefined && distanceY !== undefined && distanceZ !== undefined) {
-      matrix[12] = distanceX;
-      matrix[13] = distanceY;
-      matrix[14] = distanceZ;
+    avc1Box = [
+      types.avc1, new Uint8Array([
+        0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, // reserved
+        0x00, 0x01, // data_reference_index
+        0x00, 0x00, // pre_defined
+        0x00, 0x00, // reserved
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, // pre_defined
+        (track.width & 0xff00) >> 8,
+        track.width & 0xff, // width
+        (track.height & 0xff00) >> 8,
+        track.height & 0xff, // height
+        0x00, 0x48, 0x00, 0x00, // horizresolution
+        0x00, 0x48, 0x00, 0x00, // vertresolution
+        0x00, 0x00, 0x00, 0x00, // reserved
+        0x00, 0x01, // frame_count
+        0x13,
+        0x76, 0x69, 0x64, 0x65,
+        0x6f, 0x6a, 0x73, 0x2d,
+        0x63, 0x6f, 0x6e, 0x74,
+        0x72, 0x69, 0x62, 0x2d,
+        0x68, 0x6c, 0x73, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, // compressorname
+        0x00, 0x18, // depth = 24
+        0x11, 0x11 // pre_defined = -1
+      ]),
+      box(types.avcC, new Uint8Array([
+        0x01, // configurationVersion
+        track.profileIdc, // AVCProfileIndication
+        track.profileCompatibility, // profile_compatibility
+        track.levelIdc, // AVCLevelIndication
+        0xff // lengthSizeMinusOne, hard-coded to 4 bytes
+      ].concat(
+        [sps.length], // numOfSequenceParameterSets
+        sequenceParameterSets, // "SPS"
+        [pps.length], // numOfPictureParameterSets
+        pictureParameterSets // "PPS"
+      ))),
+      box(types.btrt, new Uint8Array([
+        0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
+        0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
+        0x00, 0x2d, 0xc6, 0xc0 // avgBitrate
+      ]))
+    ];
+
+    if (track.sarRatio) {
+      var
+        hSpacing = track.sarRatio[0],
+        vSpacing = track.sarRatio[1];
+
+        avc1Box.push(
+          box(types.pasp, new Uint8Array([
+            (hSpacing & 0xFF000000) >> 24,
+            (hSpacing & 0xFF0000) >> 16,
+            (hSpacing & 0xFF00) >> 8,
+            hSpacing & 0xFF,
+            (vSpacing & 0xFF000000) >> 24,
+            (vSpacing & 0xFF0000) >> 16,
+            (vSpacing & 0xFF00) >> 8,
+            vSpacing & 0xFF
+          ]))
+        );
     }
-    return matrix
-  }
 
-  function translateX(distance) {
-    var matrix = identity();
-    matrix[12] = distance;
-    return matrix
-  }
+    return box.apply(null, avc1Box);
+  };
 
-  function translateY(distance) {
-    var matrix = identity();
-    matrix[13] = distance;
-    return matrix
-  }
+  audioSample = function(track) {
+    return box(types.mp4a, new Uint8Array([
 
-  function translateZ(distance) {
-    var matrix = identity();
-    matrix[14] = distance;
-    return matrix
-  }
+      // SampleEntry, ISO/IEC 14496-12
+      0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, // reserved
+      0x00, 0x01, // data_reference_index
 
-  exports.format = format;
-  exports.fromString = fromString;
-  exports.identity = identity;
-  exports.inverse = inverse;
-  exports.multiply = multiply;
-  exports.perspective = perspective;
-  exports.rotate = rotate;
-  exports.rotateX = rotateX;
-  exports.rotateY = rotateY;
-  exports.rotateZ = rotateZ;
-  exports.scale = scale;
-  exports.scaleX = scaleX;
-  exports.scaleY = scaleY;
-  exports.scaleZ = scaleZ;
-  exports.skew = skew;
-  exports.skewX = skewX;
-  exports.skewY = skewY;
-  exports.toString = toString;
-  exports.translate = translate;
-  exports.translate3d = translate3d;
-  exports.translateX = translateX;
-  exports.translateY = translateY;
-  exports.translateZ = translateZ;
+      // AudioSampleEntry, ISO/IEC 14496-12
+      0x00, 0x00, 0x00, 0x00, // reserved
+      0x00, 0x00, 0x00, 0x00, // reserved
+      (track.channelcount & 0xff00) >> 8,
+      (track.channelcount & 0xff), // channelcount
 
-  Object.defineProperty(exports, '__esModule', { value: true });
+      (track.samplesize & 0xff00) >> 8,
+      (track.samplesize & 0xff), // samplesize
+      0x00, 0x00, // pre_defined
+      0x00, 0x00, // reserved
 
-})));
+      (track.samplerate & 0xff00) >> 8,
+      (track.samplerate & 0xff),
+      0x00, 0x00 // samplerate, 16.16
 
-},{}],32:[function(require,module,exports){
-/* eslint-disable node/no-deprecated-api */
-var buffer = require('buffer')
-var Buffer = buffer.Buffer
+      // MP4AudioSampleEntry, ISO/IEC 14496-14
+    ]), esds(track));
+  };
+}());
 
-// alternative to using Object.keys for old browsers
-function copyProps (src, dst) {
-  for (var key in src) {
-    dst[key] = src[key]
+tkhd = function(track) {
+  var result = new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x07, // flags
+    0x00, 0x00, 0x00, 0x00, // creation_time
+    0x00, 0x00, 0x00, 0x00, // modification_time
+    (track.id & 0xFF000000) >> 24,
+    (track.id & 0xFF0000) >> 16,
+    (track.id & 0xFF00) >> 8,
+    track.id & 0xFF, // track_ID
+    0x00, 0x00, 0x00, 0x00, // reserved
+    (track.duration & 0xFF000000) >> 24,
+    (track.duration & 0xFF0000) >> 16,
+    (track.duration & 0xFF00) >> 8,
+    track.duration & 0xFF, // duration
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, // reserved
+    0x00, 0x00, // layer
+    0x00, 0x00, // alternate_group
+    0x01, 0x00, // non-audio track volume
+    0x00, 0x00, // reserved
+    0x00, 0x01, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x01, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
+    (track.width & 0xFF00) >> 8,
+    track.width & 0xFF,
+    0x00, 0x00, // width
+    (track.height & 0xFF00) >> 8,
+    track.height & 0xFF,
+    0x00, 0x00 // height
+  ]);
+
+  return box(types.tkhd, result);
+};
+
+/**
+ * Generate a track fragment (traf) box. A traf box collects metadata
+ * about tracks in a movie fragment (moof) box.
+ */
+traf = function(track) {
+  var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun,
+      sampleDependencyTable, dataOffset,
+      upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
+
+  trackFragmentHeader = box(types.tfhd, new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x3a, // flags
+    (track.id & 0xFF000000) >> 24,
+    (track.id & 0xFF0000) >> 16,
+    (track.id & 0xFF00) >> 8,
+    (track.id & 0xFF), // track_ID
+    0x00, 0x00, 0x00, 0x01, // sample_description_index
+    0x00, 0x00, 0x00, 0x00, // default_sample_duration
+    0x00, 0x00, 0x00, 0x00, // default_sample_size
+    0x00, 0x00, 0x00, 0x00  // default_sample_flags
+  ]));
+
+  upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / (UINT32_MAX + 1));
+  lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % (UINT32_MAX + 1));
+
+  trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([
+    0x01, // version 1
+    0x00, 0x00, 0x00, // flags
+    // baseMediaDecodeTime
+    (upperWordBaseMediaDecodeTime >>> 24) & 0xFF,
+    (upperWordBaseMediaDecodeTime >>> 16) & 0xFF,
+    (upperWordBaseMediaDecodeTime >>>  8) & 0xFF,
+    upperWordBaseMediaDecodeTime & 0xFF,
+    (lowerWordBaseMediaDecodeTime >>> 24) & 0xFF,
+    (lowerWordBaseMediaDecodeTime >>> 16) & 0xFF,
+    (lowerWordBaseMediaDecodeTime >>>  8) & 0xFF,
+    lowerWordBaseMediaDecodeTime & 0xFF
+  ]));
+
+  // the data offset specifies the number of bytes from the start of
+  // the containing moof to the first payload byte of the associated
+  // mdat
+  dataOffset = (32 + // tfhd
+                20 + // tfdt
+                8 +  // traf header
+                16 + // mfhd
+                8 +  // moof header
+                8);  // mdat header
+
+  // audio tracks require less metadata
+  if (track.type === 'audio') {
+    trackFragmentRun = trun(track, dataOffset);
+    return box(types.traf,
+               trackFragmentHeader,
+               trackFragmentDecodeTime,
+               trackFragmentRun);
   }
-}
-if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
-  module.exports = buffer
-} else {
-  // Copy properties from require('buffer')
-  copyProps(buffer, exports)
-  exports.Buffer = SafeBuffer
-}
 
-function SafeBuffer (arg, encodingOrOffset, length) {
-  return Buffer(arg, encodingOrOffset, length)
-}
+  // video tracks should contain an independent and disposable samples
+  // box (sdtp)
+  // generate one and adjust offsets to match
+  sampleDependencyTable = sdtp(track);
+  trackFragmentRun = trun(track,
+                          sampleDependencyTable.length + dataOffset);
+  return box(types.traf,
+             trackFragmentHeader,
+             trackFragmentDecodeTime,
+             trackFragmentRun,
+             sampleDependencyTable);
+};
 
-// Copy static methods from Buffer
-copyProps(Buffer, SafeBuffer)
+/**
+ * Generate a track box.
+ * @param track {object} a track definition
+ * @return {Uint8Array} the track box
+ */
+trak = function(track) {
+  track.duration = track.duration || 0xffffffff;
+  return box(types.trak,
+             tkhd(track),
+             mdia(track));
+};
 
-SafeBuffer.from = function (arg, encodingOrOffset, length) {
-  if (typeof arg === 'number') {
-    throw new TypeError('Argument must not be a number')
+trex = function(track) {
+  var result = new Uint8Array([
+    0x00, // version 0
+    0x00, 0x00, 0x00, // flags
+    (track.id & 0xFF000000) >> 24,
+    (track.id & 0xFF0000) >> 16,
+    (track.id & 0xFF00) >> 8,
+    (track.id & 0xFF), // track_ID
+    0x00, 0x00, 0x00, 0x01, // default_sample_description_index
+    0x00, 0x00, 0x00, 0x00, // default_sample_duration
+    0x00, 0x00, 0x00, 0x00, // default_sample_size
+    0x00, 0x01, 0x00, 0x01 // default_sample_flags
+  ]);
+  // the last two bytes of default_sample_flags is the sample
+  // degradation priority, a hint about the importance of this sample
+  // relative to others. Lower the degradation priority for all sample
+  // types other than video.
+  if (track.type !== 'video') {
+    result[result.length - 1] = 0x00;
   }
-  return Buffer(arg, encodingOrOffset, length)
-}
 
-SafeBuffer.alloc = function (size, fill, encoding) {
-  if (typeof size !== 'number') {
-    throw new TypeError('Argument must be a number')
-  }
-  var buf = Buffer(size)
-  if (fill !== undefined) {
-    if (typeof encoding === 'string') {
-      buf.fill(fill, encoding)
-    } else {
-      buf.fill(fill)
+  return box(types.trex, result);
+};
+
+(function() {
+  var audioTrun, videoTrun, trunHeader;
+
+  // This method assumes all samples are uniform. That is, if a
+  // duration is present for the first sample, it will be present for
+  // all subsequent samples.
+  // see ISO/IEC 14496-12:2012, Section 8.8.8.1
+  trunHeader = function(samples, offset) {
+    var durationPresent = 0, sizePresent = 0,
+        flagsPresent = 0, compositionTimeOffset = 0;
+
+    // trun flag constants
+    if (samples.length) {
+      if (samples[0].duration !== undefined) {
+        durationPresent = 0x1;
+      }
+      if (samples[0].size !== undefined) {
+        sizePresent = 0x2;
+      }
+      if (samples[0].flags !== undefined) {
+        flagsPresent = 0x4;
+      }
+      if (samples[0].compositionTimeOffset !== undefined) {
+        compositionTimeOffset = 0x8;
+      }
     }
-  } else {
-    buf.fill(0)
-  }
-  return buf
-}
 
-SafeBuffer.allocUnsafe = function (size) {
-  if (typeof size !== 'number') {
-    throw new TypeError('Argument must be a number')
-  }
-  return Buffer(size)
-}
+    return [
+      0x00, // version 0
+      0x00,
+      durationPresent | sizePresent | flagsPresent | compositionTimeOffset,
+      0x01, // flags
+      (samples.length & 0xFF000000) >>> 24,
+      (samples.length & 0xFF0000) >>> 16,
+      (samples.length & 0xFF00) >>> 8,
+      samples.length & 0xFF, // sample_count
+      (offset & 0xFF000000) >>> 24,
+      (offset & 0xFF0000) >>> 16,
+      (offset & 0xFF00) >>> 8,
+      offset & 0xFF // data_offset
+    ];
+  };
 
-SafeBuffer.allocUnsafeSlow = function (size) {
-  if (typeof size !== 'number') {
-    throw new TypeError('Argument must be a number')
-  }
-  return buffer.SlowBuffer(size)
-}
+  videoTrun = function(track, offset) {
+    var bytesOffest, bytes, header, samples, sample, i;
+
+    samples = track.samples || [];
+    offset += 8 + 12 + (16 * samples.length);
+    header = trunHeader(samples, offset);
+    bytes = new Uint8Array(header.length + samples.length * 16);
+    bytes.set(header);
+    bytesOffest = header.length;
+
+    for (i = 0; i < samples.length; i++) {
+      sample = samples[i];
+
+      bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
+      bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
+      bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
+      bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
+      bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
+      bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
+      bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
+      bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
+      bytes[bytesOffest++] = (sample.flags.isLeading << 2) | sample.flags.dependsOn;
+      bytes[bytesOffest++] = (sample.flags.isDependedOn << 6) |
+          (sample.flags.hasRedundancy << 4) |
+          (sample.flags.paddingValue << 1) |
+          sample.flags.isNonSyncSample;
+      bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
+      bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F; // sample_flags
+      bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
+      bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
+      bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
+      bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF; // sample_composition_time_offset
+    }
+    return box(types.trun, bytes);
+  };
 
-},{"buffer":8}],33:[function(require,module,exports){
-// Copyright Joyent, Inc. and other Node contributors.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to permit
-// persons to whom the Software is furnished to do so, subject to the
-// following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-// USE OR OTHER DEALINGS IN THE SOFTWARE.
+  audioTrun = function(track, offset) {
+    var bytes, bytesOffest, header, samples, sample, i;
+
+    samples = track.samples || [];
+    offset += 8 + 12 + (8 * samples.length);
+
+    header = trunHeader(samples, offset);
+    bytes = new Uint8Array(header.length + samples.length * 8);
+    bytes.set(header);
+    bytesOffest = header.length;
+
+    for (i = 0; i < samples.length; i++) {
+      sample = samples[i];
+      bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
+      bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
+      bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
+      bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
+      bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
+      bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
+      bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
+      bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
+    }
 
-'use strict';
+    return box(types.trun, bytes);
+  };
 
-/*<replacement>*/
+  trun = function(track, offset) {
+    if (track.type === 'audio') {
+      return audioTrun(track, offset);
+    }
 
-var Buffer = require('safe-buffer').Buffer;
-/*</replacement>*/
+    return videoTrun(track, offset);
+  };
+}());
 
-var isEncoding = Buffer.isEncoding || function (encoding) {
-  encoding = '' + encoding;
-  switch (encoding && encoding.toLowerCase()) {
-    case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
-      return true;
-    default:
-      return false;
+module.exports = {
+  ftyp: ftyp,
+  mdat: mdat,
+  moof: moof,
+  moov: moov,
+  initSegment: function(tracks) {
+    var
+      fileType = ftyp(),
+      movie = moov(tracks),
+      result;
+
+    result = new Uint8Array(fileType.byteLength + movie.byteLength);
+    result.set(fileType);
+    result.set(movie, fileType.byteLength);
+    return result;
   }
 };
 
-function _normalizeEncoding(enc) {
-  if (!enc) return 'utf8';
-  var retried;
-  while (true) {
-    switch (enc) {
-      case 'utf8':
-      case 'utf-8':
-        return 'utf8';
-      case 'ucs2':
-      case 'ucs-2':
-      case 'utf16le':
-      case 'utf-16le':
-        return 'utf16le';
-      case 'latin1':
-      case 'binary':
-        return 'latin1';
-      case 'base64':
-      case 'ascii':
-      case 'hex':
-        return enc;
-      default:
-        if (retried) return; // undefined
-        enc = ('' + enc).toLowerCase();
-        retried = true;
+},{}],46:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Utilities to detect basic properties and metadata about MP4s.
+ */
+'use strict';
+
+var toUnsigned = require('../utils/bin').toUnsigned;
+var toHexString = require('../utils/bin').toHexString;
+var mp4Inspector = require('../tools/mp4-inspector.js');
+var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks,
+  getTimescaleFromMediaHeader;
+
+/**
+ * Parses an MP4 initialization segment and extracts the timescale
+ * values for any declared tracks. Timescale values indicate the
+ * number of clock ticks per second to assume for time-based values
+ * elsewhere in the MP4.
+ *
+ * To determine the start time of an MP4, you need two pieces of
+ * information: the timescale unit and the earliest base media decode
+ * time. Multiple timescales can be specified within an MP4 but the
+ * base media decode time is always expressed in the timescale from
+ * the media header box for the track:
+ * ```
+ * moov > trak > mdia > mdhd.timescale
+ * ```
+ * @param init {Uint8Array} the bytes of the init segment
+ * @return {object} a hash of track ids to timescale values or null if
+ * the init segment is malformed.
+ */
+timescale = function(init) {
+  var
+    result = {},
+    traks = mp4Inspector.findBox(init, ['moov', 'trak']);
+
+  // mdhd timescale
+  return traks.reduce(function(result, trak) {
+    var tkhd, version, index, id, mdhd;
+
+    tkhd = mp4Inspector.findBox(trak, ['tkhd'])[0];
+    if (!tkhd) {
+      return null;
     }
-  }
+    version = tkhd[0];
+    index = version === 0 ? 12 : 20;
+    id = toUnsigned(tkhd[index]     << 24 |
+                    tkhd[index + 1] << 16 |
+                    tkhd[index + 2] <<  8 |
+                    tkhd[index + 3]);
+
+    mdhd = mp4Inspector.findBox(trak, ['mdia', 'mdhd'])[0];
+    if (!mdhd) {
+      return null;
+    }
+    version = mdhd[0];
+    index = version === 0 ? 12 : 20;
+    result[id] = toUnsigned(mdhd[index]     << 24 |
+                            mdhd[index + 1] << 16 |
+                            mdhd[index + 2] <<  8 |
+                            mdhd[index + 3]);
+    return result;
+  }, result);
 };
 
-// Do not cache `Buffer.isEncoding` when checking encoding names as some
-// modules monkey-patch it to support additional encodings
-function normalizeEncoding(enc) {
-  var nenc = _normalizeEncoding(enc);
-  if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
-  return nenc || enc;
-}
+/**
+ * Determine the base media decode start time, in seconds, for an MP4
+ * fragment. If multiple fragments are specified, the earliest time is
+ * returned.
+ *
+ * The base media decode time can be parsed from track fragment
+ * metadata:
+ * ```
+ * moof > traf > tfdt.baseMediaDecodeTime
+ * ```
+ * It requires the timescale value from the mdhd to interpret.
+ *
+ * @param timescale {object} a hash of track ids to timescale values.
+ * @return {number} the earliest base media decode start time for the
+ * fragment, in seconds
+ */
+startTime = function(timescale, fragment) {
+  var trafs, baseTimes, result;
+
+  // we need info from two childrend of each track fragment box
+  trafs = mp4Inspector.findBox(fragment, ['moof', 'traf']);
+
+  // determine the start times for each track
+  baseTimes = [].concat.apply([], trafs.map(function(traf) {
+    return mp4Inspector.findBox(traf, ['tfhd']).map(function(tfhd) {
+      var id, scale, baseTime;
+
+      // get the track id from the tfhd
+      id = toUnsigned(tfhd[4] << 24 |
+                      tfhd[5] << 16 |
+                      tfhd[6] <<  8 |
+                      tfhd[7]);
+      // assume a 90kHz clock if no timescale was specified
+      scale = timescale[id] || 90e3;
+
+      // get the base media decode time from the tfdt
+      baseTime = mp4Inspector.findBox(traf, ['tfdt']).map(function(tfdt) {
+        var version, result;
+
+        version = tfdt[0];
+        result = toUnsigned(tfdt[4] << 24 |
+                            tfdt[5] << 16 |
+                            tfdt[6] <<  8 |
+                            tfdt[7]);
+        if (version ===  1) {
+          result *= Math.pow(2, 32);
+          result += toUnsigned(tfdt[8]  << 24 |
+                               tfdt[9]  << 16 |
+                               tfdt[10] <<  8 |
+                               tfdt[11]);
+        }
+        return result;
+      })[0];
+      baseTime = baseTime || Infinity;
 
-// StringDecoder provides an interface for efficiently splitting a series of
-// buffers into a series of JS strings without breaking apart multi-byte
-// characters.
-exports.StringDecoder = StringDecoder;
-function StringDecoder(encoding) {
-  this.encoding = normalizeEncoding(encoding);
-  var nb;
-  switch (this.encoding) {
-    case 'utf16le':
-      this.text = utf16Text;
-      this.end = utf16End;
-      nb = 4;
-      break;
-    case 'utf8':
-      this.fillLast = utf8FillLast;
-      nb = 4;
-      break;
-    case 'base64':
-      this.text = base64Text;
-      this.end = base64End;
-      nb = 3;
-      break;
-    default:
-      this.write = simpleWrite;
-      this.end = simpleEnd;
-      return;
-  }
-  this.lastNeed = 0;
-  this.lastTotal = 0;
-  this.lastChar = Buffer.allocUnsafe(nb);
-}
+      // convert base time to seconds
+      return baseTime / scale;
+    });
+  }));
 
-StringDecoder.prototype.write = function (buf) {
-  if (buf.length === 0) return '';
-  var r;
-  var i;
-  if (this.lastNeed) {
-    r = this.fillLast(buf);
-    if (r === undefined) return '';
-    i = this.lastNeed;
-    this.lastNeed = 0;
-  } else {
-    i = 0;
+  // return the minimum
+  result = Math.min.apply(null, baseTimes);
+  return isFinite(result) ? result : 0;
+};
+
+/**
+ * Determine the composition start, in seconds, for an MP4
+ * fragment.
+ *
+ * The composition start time of a fragment can be calculated using the base
+ * media decode time, composition time offset, and timescale, as follows:
+ *
+ * compositionStartTime = (baseMediaDecodeTime + compositionTimeOffset) / timescale
+ *
+ * All of the aforementioned information is contained within a media fragment's
+ * `traf` box, except for timescale info, which comes from the initialization
+ * segment, so a track id (also contained within a `traf`) is also necessary to
+ * associate it with a timescale
+ *
+ *
+ * @param timescales {object} - a hash of track ids to timescale values.
+ * @param fragment {Unit8Array} - the bytes of a media segment
+ * @return {number} the composition start time for the fragment, in seconds
+ **/
+compositionStartTime = function(timescales, fragment) {
+  var trafBoxes = mp4Inspector.findBox(fragment, ['moof', 'traf']);
+  var baseMediaDecodeTime = 0;
+  var compositionTimeOffset = 0;
+  var trackId;
+
+  if (trafBoxes && trafBoxes.length) {
+    // The spec states that track run samples contained within a `traf` box are contiguous, but
+    // it does not explicitly state whether the `traf` boxes themselves are contiguous.
+    // We will assume that they are, so we only need the first to calculate start time.
+    var parsedTraf = mp4Inspector.parseTraf(trafBoxes[0]);
+
+    for (var i = 0; i < parsedTraf.boxes.length; i++) {
+      if (parsedTraf.boxes[i].type === 'tfhd') {
+        trackId = parsedTraf.boxes[i].trackId;
+      } else if (parsedTraf.boxes[i].type === 'tfdt') {
+        baseMediaDecodeTime = parsedTraf.boxes[i].baseMediaDecodeTime;
+      } else if (parsedTraf.boxes[i].type === 'trun' && parsedTraf.boxes[i].samples.length) {
+        compositionTimeOffset = parsedTraf.boxes[i].samples[0].compositionTimeOffset || 0;
+      }
+    }
   }
-  if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
-  return r || '';
+
+  // Get timescale for this specific track. Assume a 90kHz clock if no timescale was
+  // specified.
+  var timescale = timescales[trackId] || 90e3;
+
+  // return the composition start time, in seconds
+  return (baseMediaDecodeTime + compositionTimeOffset) / timescale;
 };
 
-StringDecoder.prototype.end = utf8End;
+/**
+  * Find the trackIds of the video tracks in this source.
+  * Found by parsing the Handler Reference and Track Header Boxes:
+  *   moov > trak > mdia > hdlr
+  *   moov > trak > tkhd
+  *
+  * @param {Uint8Array} init - The bytes of the init segment for this source
+  * @return {Number[]} A list of trackIds
+  *
+  * @see ISO-BMFF-12/2015, Section 8.4.3
+ **/
+getVideoTrackIds = function(init) {
+  var traks = mp4Inspector.findBox(init, ['moov', 'trak']);
+  var videoTrackIds = [];
+
+  traks.forEach(function(trak) {
+    var hdlrs = mp4Inspector.findBox(trak, ['mdia', 'hdlr']);
+    var tkhds = mp4Inspector.findBox(trak, ['tkhd']);
+
+    hdlrs.forEach(function(hdlr, index) {
+      var handlerType = mp4Inspector.parseType(hdlr.subarray(8, 12));
+      var tkhd = tkhds[index];
+      var view;
+      var version;
+      var trackId;
+
+      if (handlerType === 'vide') {
+        view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
+        version = view.getUint8(0);
+        trackId = (version === 0) ? view.getUint32(12) : view.getUint32(20);
+
+        videoTrackIds.push(trackId);
+      }
+    });
+  });
 
-// Returns only complete characters in a Buffer
-StringDecoder.prototype.text = utf8Text;
+  return videoTrackIds;
+};
 
-// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
-StringDecoder.prototype.fillLast = function (buf) {
-  if (this.lastNeed <= buf.length) {
-    buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
-    return this.lastChar.toString(this.encoding, 0, this.lastTotal);
-  }
-  buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
-  this.lastNeed -= buf.length;
+getTimescaleFromMediaHeader = function(mdhd) {
+  // mdhd is a FullBox, meaning it will have its own version as the first byte
+  var version = mdhd[0];
+  var index = version === 0 ? 12 : 20;
+
+  return toUnsigned(
+    mdhd[index]     << 24 |
+    mdhd[index + 1] << 16 |
+    mdhd[index + 2] <<  8 |
+    mdhd[index + 3]
+  );
 };
 
-// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
-// continuation byte. If an invalid byte is detected, -2 is returned.
-function utf8CheckByte(byte) {
-  if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
-  return byte >> 6 === 0x02 ? -1 : -2;
-}
+/**
+ * Get all the video, audio, and hint tracks from a non fragmented
+ * mp4 segment
+ */
+getTracks = function(init) {
+  var traks = mp4Inspector.findBox(init, ['moov', 'trak']);
+  var tracks = [];
 
-// Checks at most 3 bytes at the end of a Buffer in order to detect an
-// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
-// needed to complete the UTF-8 character (if applicable) are returned.
-function utf8CheckIncomplete(self, buf, i) {
-  var j = buf.length - 1;
-  if (j < i) return 0;
-  var nb = utf8CheckByte(buf[j]);
-  if (nb >= 0) {
-    if (nb > 0) self.lastNeed = nb - 1;
-    return nb;
-  }
-  if (--j < i || nb === -2) return 0;
-  nb = utf8CheckByte(buf[j]);
-  if (nb >= 0) {
-    if (nb > 0) self.lastNeed = nb - 2;
-    return nb;
-  }
-  if (--j < i || nb === -2) return 0;
-  nb = utf8CheckByte(buf[j]);
-  if (nb >= 0) {
-    if (nb > 0) {
-      if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
-    }
-    return nb;
-  }
-  return 0;
-}
+  traks.forEach(function(trak) {
+    var track = {};
+    var tkhd = mp4Inspector.findBox(trak, ['tkhd'])[0];
+    var view, tkhdVersion;
 
-// Validates as many continuation bytes for a multi-byte UTF-8 character as
-// needed or are available. If we see a non-continuation byte where we expect
-// one, we "replace" the validated continuation bytes we've seen so far with
-// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
-// behavior. The continuation byte check is included three times in the case
-// where all of the continuation bytes for a character exist in the same buffer.
-// It is also done this way as a slight performance increase instead of using a
-// loop.
-function utf8CheckExtraBytes(self, buf, p) {
-  if ((buf[0] & 0xC0) !== 0x80) {
-    self.lastNeed = 0;
-    return '\ufffd';
-  }
-  if (self.lastNeed > 1 && buf.length > 1) {
-    if ((buf[1] & 0xC0) !== 0x80) {
-      self.lastNeed = 1;
-      return '\ufffd';
+    // id
+    if (tkhd) {
+      view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
+      tkhdVersion = view.getUint8(0);
+
+      track.id = (tkhdVersion === 0) ? view.getUint32(12) : view.getUint32(20);
     }
-    if (self.lastNeed > 2 && buf.length > 2) {
-      if ((buf[2] & 0xC0) !== 0x80) {
-        self.lastNeed = 2;
-        return '\ufffd';
+
+    var hdlr = mp4Inspector.findBox(trak, ['mdia', 'hdlr'])[0];
+
+    // type
+    if (hdlr) {
+      var type = mp4Inspector.parseType(hdlr.subarray(8, 12));
+
+      if (type === 'vide') {
+        track.type = 'video';
+      } else if (type === 'soun') {
+        track.type = 'audio';
+      } else {
+        track.type = type;
       }
     }
-  }
-}
 
-// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
-function utf8FillLast(buf) {
-  var p = this.lastTotal - this.lastNeed;
-  var r = utf8CheckExtraBytes(this, buf, p);
-  if (r !== undefined) return r;
-  if (this.lastNeed <= buf.length) {
-    buf.copy(this.lastChar, p, 0, this.lastNeed);
-    return this.lastChar.toString(this.encoding, 0, this.lastTotal);
-  }
-  buf.copy(this.lastChar, p, 0, buf.length);
-  this.lastNeed -= buf.length;
-}
 
-// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
-// partial character, the character's bytes are buffered until the required
-// number of bytes are available.
-function utf8Text(buf, i) {
-  var total = utf8CheckIncomplete(this, buf, i);
-  if (!this.lastNeed) return buf.toString('utf8', i);
-  this.lastTotal = total;
-  var end = buf.length - (total - this.lastNeed);
-  buf.copy(this.lastChar, 0, end);
-  return buf.toString('utf8', i, end);
-}
+    // codec
+    var stsd = mp4Inspector.findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
+
+    if (stsd) {
+      var sampleDescriptions = stsd.subarray(8);
+      // gives the codec type string
+      track.codec = mp4Inspector.parseType(sampleDescriptions.subarray(4, 8));
+
+      var codecBox = mp4Inspector.findBox(sampleDescriptions, [track.codec])[0];
+      var codecConfig, codecConfigType;
+
+      if (codecBox) {
+        // https://tools.ietf.org/html/rfc6381#section-3.3
+        if ((/^[a-z]vc[1-9]$/i).test(track.codec)) {
+          // we don't need anything but the "config" parameter of the
+          // avc1 codecBox
+          codecConfig = codecBox.subarray(78);
+          codecConfigType = mp4Inspector.parseType(codecConfig.subarray(4, 8));
+
+          if (codecConfigType === 'avcC' && codecConfig.length > 11) {
+            track.codec += '.';
+
+            // left padded with zeroes for single digit hex
+            // profile idc
+            track.codec +=  toHexString(codecConfig[9]);
+            // the byte containing the constraint_set flags
+            track.codec += toHexString(codecConfig[10]);
+            // level idc
+            track.codec += toHexString(codecConfig[11]);
+          } else {
+            // TODO: show a warning that we couldn't parse the codec
+            // and are using the default
+            track.codec = 'avc1.4d400d';
+          }
+        } else if ((/^mp4[a,v]$/i).test(track.codec)) {
+          // we do not need anything but the streamDescriptor of the mp4a codecBox
+          codecConfig = codecBox.subarray(28);
+          codecConfigType = mp4Inspector.parseType(codecConfig.subarray(4, 8));
+
+          if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
+            track.codec += '.' + toHexString(codecConfig[19]);
+            // this value is only a single digit
+            track.codec += '.' + toHexString((codecConfig[20] >>> 2) & 0x3f).replace(/^0/, '');
+          } else {
+            // TODO: show a warning that we couldn't parse the codec
+            // and are using the default
+            track.codec = 'mp4a.40.2';
+          }
+        } else {
+          // TODO: show a warning? for unknown codec type
+        }
+      }
+    }
 
-// For UTF-8, a replacement character is added when ending on a partial
-// character.
-function utf8End(buf) {
-  var r = buf && buf.length ? this.write(buf) : '';
-  if (this.lastNeed) return r + '\ufffd';
-  return r;
-}
+    var mdhd = mp4Inspector.findBox(trak, ['mdia', 'mdhd'])[0];
 
-// UTF-16LE typically needs two bytes per character, but even if we have an even
-// number of bytes available, we need to check if we end on a leading/high
-// surrogate. In that case, we need to wait for the next two bytes in order to
-// decode the last character properly.
-function utf16Text(buf, i) {
-  if ((buf.length - i) % 2 === 0) {
-    var r = buf.toString('utf16le', i);
-    if (r) {
-      var c = r.charCodeAt(r.length - 1);
-      if (c >= 0xD800 && c <= 0xDBFF) {
-        this.lastNeed = 2;
-        this.lastTotal = 4;
-        this.lastChar[0] = buf[buf.length - 2];
-        this.lastChar[1] = buf[buf.length - 1];
-        return r.slice(0, -1);
-      }
+    if (mdhd) {
+      track.timescale = getTimescaleFromMediaHeader(mdhd);
     }
-    return r;
-  }
-  this.lastNeed = 1;
-  this.lastTotal = 2;
-  this.lastChar[0] = buf[buf.length - 1];
-  return buf.toString('utf16le', i, buf.length - 1);
-}
 
-// For UTF-16LE we do not explicitly append special replacement characters if we
-// end on a partial character, we simply let v8 handle that.
-function utf16End(buf) {
-  var r = buf && buf.length ? this.write(buf) : '';
-  if (this.lastNeed) {
-    var end = this.lastTotal - this.lastNeed;
-    return r + this.lastChar.toString('utf16le', 0, end);
-  }
-  return r;
-}
+    tracks.push(track);
+  });
 
-function base64Text(buf, i) {
-  var n = (buf.length - i) % 3;
-  if (n === 0) return buf.toString('base64', i);
-  this.lastNeed = 3 - n;
-  this.lastTotal = 3;
-  if (n === 1) {
-    this.lastChar[0] = buf[buf.length - 1];
-  } else {
-    this.lastChar[0] = buf[buf.length - 2];
-    this.lastChar[1] = buf[buf.length - 1];
-  }
-  return buf.toString('base64', i, buf.length - n);
-}
+  return tracks;
+};
 
-function base64End(buf) {
-  var r = buf && buf.length ? this.write(buf) : '';
-  if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
-  return r;
-}
+module.exports = {
+  // export mp4 inspector's findBox and parseType for backwards compatibility
+  findBox: mp4Inspector.findBox,
+  parseType: mp4Inspector.parseType,
+  timescale: timescale,
+  startTime: startTime,
+  compositionStartTime: compositionStartTime,
+  videoTrackIds: getVideoTrackIds,
+  tracks: getTracks,
+  getTimescaleFromMediaHeader: getTimescaleFromMediaHeader
+};
 
-// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
-function simpleWrite(buf) {
-  return buf.toString(this.encoding);
-}
+},{"../tools/mp4-inspector.js":55,"../utils/bin":57}],47:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
 
-function simpleEnd(buf) {
-  return buf && buf.length ? this.write(buf) : '';
-}
-},{"safe-buffer":32}],34:[function(require,module,exports){
-(function (global, factory) {
-	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
-	typeof define === 'function' && define.amd ? define(['exports'], factory) :
-	(global = global || self, factory(global.THREE = {}));
-}(this, (function (exports) { 'use strict';
+/**
+ * Store information about the start and end of the track and the
+ * duration for each frame/sample we process in order to calculate
+ * the baseMediaDecodeTime
+ */
+var collectDtsInfo = function(track, data) {
+  if (typeof data.pts === 'number') {
+    if (track.timelineStartInfo.pts === undefined) {
+      track.timelineStartInfo.pts = data.pts;
+    }
 
-	// Polyfills
+    if (track.minSegmentPts === undefined) {
+      track.minSegmentPts = data.pts;
+    } else {
+      track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
+    }
 
-	if ( Number.EPSILON === undefined ) {
+    if (track.maxSegmentPts === undefined) {
+      track.maxSegmentPts = data.pts;
+    } else {
+      track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
+    }
+  }
 
-		Number.EPSILON = Math.pow( 2, - 52 );
+  if (typeof data.dts === 'number') {
+    if (track.timelineStartInfo.dts === undefined) {
+      track.timelineStartInfo.dts = data.dts;
+    }
 
-	}
+    if (track.minSegmentDts === undefined) {
+      track.minSegmentDts = data.dts;
+    } else {
+      track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
+    }
 
-	if ( Number.isInteger === undefined ) {
+    if (track.maxSegmentDts === undefined) {
+      track.maxSegmentDts = data.dts;
+    } else {
+      track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
+    }
+  }
+};
 
-		// Missing in IE
-		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
+/**
+ * Clear values used to calculate the baseMediaDecodeTime between
+ * tracks
+ */
+var clearDtsInfo = function(track) {
+  delete track.minSegmentDts;
+  delete track.maxSegmentDts;
+  delete track.minSegmentPts;
+  delete track.maxSegmentPts;
+};
 
-		Number.isInteger = function ( value ) {
+/**
+ * Calculate the track's baseMediaDecodeTime based on the earliest
+ * DTS the transmuxer has ever seen and the minimum DTS for the
+ * current track
+ * @param track {object} track metadata configuration
+ * @param keepOriginalTimestamps {boolean} If true, keep the timestamps
+ *        in the source; false to adjust the first segment to start at 0.
+ */
+var calculateTrackBaseMediaDecodeTime = function(track, keepOriginalTimestamps) {
+  var
+    baseMediaDecodeTime,
+    scale,
+    minSegmentDts = track.minSegmentDts;
+
+  // Optionally adjust the time so the first segment starts at zero.
+  if (!keepOriginalTimestamps) {
+    minSegmentDts -= track.timelineStartInfo.dts;
+  }
 
-			return typeof value === 'number' && isFinite( value ) && Math.floor( value ) === value;
+  // track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
+  // we want the start of the first segment to be placed
+  baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;
 
-		};
+  // Add to that the distance this segment is from the very first
+  baseMediaDecodeTime += minSegmentDts;
 
-	}
+  // baseMediaDecodeTime must not become negative
+  baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
 
-	//
+  if (track.type === 'audio') {
+    // Audio has a different clock equal to the sampling_rate so we need to
+    // scale the PTS values into the clock rate of the track
+    scale = track.samplerate / ONE_SECOND_IN_TS;
+    baseMediaDecodeTime *= scale;
+    baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
+  }
 
-	if ( Math.sign === undefined ) {
+  return baseMediaDecodeTime;
+};
 
-		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
+module.exports = {
+  clearDtsInfo: clearDtsInfo,
+  calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
+  collectDtsInfo: collectDtsInfo
+};
 
-		Math.sign = function ( x ) {
+},{"../utils/clock":58}],48:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * A stream-based mp2t to mp4 converter. This utility can be used to
+ * deliver mp4s to a SourceBuffer on platforms that support native
+ * Media Source Extensions.
+ */
+'use strict';
 
-			return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : + x;
+var Stream = require('../utils/stream.js');
+var mp4 = require('./mp4-generator.js');
+var frameUtils = require('./frame-utils');
+var audioFrameUtils = require('./audio-frame-utils');
+var trackDecodeInfo = require('./track-decode-info');
+var m2ts = require('../m2ts/m2ts.js');
+var clock = require('../utils/clock');
+var AdtsStream = require('../codecs/adts.js');
+var H264Stream = require('../codecs/h264').H264Stream;
+var AacStream = require('../aac');
+var isLikelyAacData = require('../aac/utils').isLikelyAacData;
+var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
+var AUDIO_PROPERTIES = require('../constants/audio-properties.js');
+var VIDEO_PROPERTIES = require('../constants/video-properties.js');
+
+// object types
+var VideoSegmentStream, AudioSegmentStream, Transmuxer, CoalesceStream;
 
-		};
+/**
+ * Compare two arrays (even typed) for same-ness
+ */
+var arrayEquals = function(a, b) {
+  var
+    i;
 
-	}
+  if (a.length !== b.length) {
+    return false;
+  }
 
-	if ( 'name' in Function.prototype === false ) {
+  // compare the value of each element in the array
+  for (i = 0; i < a.length; i++) {
+    if (a[i] !== b[i]) {
+      return false;
+    }
+  }
 
-		// Missing in IE
-		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
+  return true;
+};
 
-		Object.defineProperty( Function.prototype, 'name', {
+var generateVideoSegmentTimingInfo = function(
+  baseMediaDecodeTime,
+  startDts,
+  startPts,
+  endDts,
+  endPts,
+  prependedContentDuration
+) {
+  var
+    ptsOffsetFromDts = startPts - startDts,
+    decodeDuration = endDts - startDts,
+    presentationDuration = endPts - startPts;
+
+  // The PTS and DTS values are based on the actual stream times from the segment,
+  // however, the player time values will reflect a start from the baseMediaDecodeTime.
+  // In order to provide relevant values for the player times, base timing info on the
+  // baseMediaDecodeTime and the DTS and PTS durations of the segment.
+  return {
+    start: {
+      dts: baseMediaDecodeTime,
+      pts: baseMediaDecodeTime + ptsOffsetFromDts
+    },
+    end: {
+      dts: baseMediaDecodeTime + decodeDuration,
+      pts: baseMediaDecodeTime + presentationDuration
+    },
+    prependedContentDuration: prependedContentDuration,
+    baseMediaDecodeTime: baseMediaDecodeTime
+  };
+};
 
-			get: function () {
+/**
+ * Constructs a single-track, ISO BMFF media segment from AAC data
+ * events. The output of this stream can be fed to a SourceBuffer
+ * configured with a suitable initialization segment.
+ * @param track {object} track metadata configuration
+ * @param options {object} transmuxer options object
+ * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
+ *        in the source; false to adjust the first segment to start at 0.
+ */
+AudioSegmentStream = function(track, options) {
+  var
+    adtsFrames = [],
+    sequenceNumber = 0,
+    earliestAllowedDts = 0,
+    audioAppendStartTs = 0,
+    videoBaseMediaDecodeTime = Infinity;
 
-				return this.toString().match( /^\s*function\s*([^\(\s]*)/ )[ 1 ];
+  options = options || {};
 
-			}
+  AudioSegmentStream.prototype.init.call(this);
 
-		} );
+  this.push = function(data) {
+    trackDecodeInfo.collectDtsInfo(track, data);
 
-	}
+    if (track) {
+      AUDIO_PROPERTIES.forEach(function(prop) {
+        track[prop] = data[prop];
+      });
+    }
 
-	if ( Object.assign === undefined ) {
+    // buffer audio data until end() is called
+    adtsFrames.push(data);
+  };
 
-		// Missing in IE
-		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
+  this.setEarliestDts = function(earliestDts) {
+    earliestAllowedDts = earliestDts;
+  };
 
-		Object.assign = function ( target ) {
+  this.setVideoBaseMediaDecodeTime = function(baseMediaDecodeTime) {
+    videoBaseMediaDecodeTime = baseMediaDecodeTime;
+  };
 
-			if ( target === undefined || target === null ) {
+  this.setAudioAppendStart = function(timestamp) {
+    audioAppendStartTs = timestamp;
+  };
 
-				throw new TypeError( 'Cannot convert undefined or null to object' );
+  this.flush = function() {
+    var
+      frames,
+      moof,
+      mdat,
+      boxes,
+      frameDuration;
+
+    // return early if no audio data has been observed
+    if (adtsFrames.length === 0) {
+      this.trigger('done', 'AudioSegmentStream');
+      return;
+    }
 
-			}
+    frames = audioFrameUtils.trimAdtsFramesByEarliestDts(
+      adtsFrames, track, earliestAllowedDts);
+    track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(
+      track, options.keepOriginalTimestamps);
 
-			var output = Object( target );
+    audioFrameUtils.prefixWithSilence(
+      track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
 
-			for ( var index = 1; index < arguments.length; index ++ ) {
+    // we have to build the index from byte locations to
+    // samples (that is, adts frames) in the audio data
+    track.samples = audioFrameUtils.generateSampleTable(frames);
 
-				var source = arguments[ index ];
+    // concatenate the audio data to constuct the mdat
+    mdat = mp4.mdat(audioFrameUtils.concatenateFrameData(frames));
 
-				if ( source !== undefined && source !== null ) {
+    adtsFrames = [];
 
-					for ( var nextKey in source ) {
+    moof = mp4.moof(sequenceNumber, [track]);
+    boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
 
-						if ( Object.prototype.hasOwnProperty.call( source, nextKey ) ) {
+    // bump the sequence number for next time
+    sequenceNumber++;
 
-							output[ nextKey ] = source[ nextKey ];
+    boxes.set(moof);
+    boxes.set(mdat, moof.byteLength);
 
-						}
+    trackDecodeInfo.clearDtsInfo(track);
 
-					}
+    frameDuration = Math.ceil(ONE_SECOND_IN_TS * 1024 / track.samplerate);
 
-				}
+    // TODO this check was added to maintain backwards compatibility (particularly with
+    // tests) on adding the timingInfo event. However, it seems unlikely that there's a
+    // valid use-case where an init segment/data should be triggered without associated
+    // frames. Leaving for now, but should be looked into.
+    if (frames.length) {
+      this.trigger('timingInfo', {
+        start: frames[0].pts,
+        end: frames[0].pts + (frames.length * frameDuration)
+      });
+    }
+    this.trigger('data', {track: track, boxes: boxes});
+    this.trigger('done', 'AudioSegmentStream');
+  };
 
-			}
+  this.reset = function() {
+    trackDecodeInfo.clearDtsInfo(track);
+    adtsFrames = [];
+    this.trigger('reset');
+  };
+};
 
-			return output;
+AudioSegmentStream.prototype = new Stream();
 
-		};
+/**
+ * Constructs a single-track, ISO BMFF media segment from H264 data
+ * events. The output of this stream can be fed to a SourceBuffer
+ * configured with a suitable initialization segment.
+ * @param track {object} track metadata configuration
+ * @param options {object} transmuxer options object
+ * @param options.alignGopsAtEnd {boolean} If true, start from the end of the
+ *        gopsToAlignWith list when attempting to align gop pts
+ * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
+ *        in the source; false to adjust the first segment to start at 0.
+ */
+VideoSegmentStream = function(track, options) {
+  var
+    sequenceNumber = 0,
+    nalUnits = [],
+    gopsToAlignWith = [],
+    config,
+    pps;
 
-	}
+  options = options || {};
 
-	var REVISION = '116';
-	var MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2, ROTATE: 0, DOLLY: 1, PAN: 2 };
-	var TOUCH = { ROTATE: 0, PAN: 1, DOLLY_PAN: 2, DOLLY_ROTATE: 3 };
-	var CullFaceNone = 0;
-	var CullFaceBack = 1;
-	var CullFaceFront = 2;
-	var CullFaceFrontBack = 3;
-	var FrontFaceDirectionCW = 0;
-	var FrontFaceDirectionCCW = 1;
-	var BasicShadowMap = 0;
-	var PCFShadowMap = 1;
-	var PCFSoftShadowMap = 2;
-	var VSMShadowMap = 3;
-	var FrontSide = 0;
-	var BackSide = 1;
-	var DoubleSide = 2;
-	var FlatShading = 1;
-	var SmoothShading = 2;
-	var NoBlending = 0;
-	var NormalBlending = 1;
-	var AdditiveBlending = 2;
-	var SubtractiveBlending = 3;
-	var MultiplyBlending = 4;
-	var CustomBlending = 5;
-	var AddEquation = 100;
-	var SubtractEquation = 101;
-	var ReverseSubtractEquation = 102;
-	var MinEquation = 103;
-	var MaxEquation = 104;
-	var ZeroFactor = 200;
-	var OneFactor = 201;
-	var SrcColorFactor = 202;
-	var OneMinusSrcColorFactor = 203;
-	var SrcAlphaFactor = 204;
-	var OneMinusSrcAlphaFactor = 205;
-	var DstAlphaFactor = 206;
-	var OneMinusDstAlphaFactor = 207;
-	var DstColorFactor = 208;
-	var OneMinusDstColorFactor = 209;
-	var SrcAlphaSaturateFactor = 210;
-	var NeverDepth = 0;
-	var AlwaysDepth = 1;
-	var LessDepth = 2;
-	var LessEqualDepth = 3;
-	var EqualDepth = 4;
-	var GreaterEqualDepth = 5;
-	var GreaterDepth = 6;
-	var NotEqualDepth = 7;
-	var MultiplyOperation = 0;
-	var MixOperation = 1;
-	var AddOperation = 2;
-	var NoToneMapping = 0;
-	var LinearToneMapping = 1;
-	var ReinhardToneMapping = 2;
-	var Uncharted2ToneMapping = 3;
-	var CineonToneMapping = 4;
-	var ACESFilmicToneMapping = 5;
+  VideoSegmentStream.prototype.init.call(this);
 
-	var UVMapping = 300;
-	var CubeReflectionMapping = 301;
-	var CubeRefractionMapping = 302;
-	var EquirectangularReflectionMapping = 303;
-	var EquirectangularRefractionMapping = 304;
-	var SphericalReflectionMapping = 305;
-	var CubeUVReflectionMapping = 306;
-	var CubeUVRefractionMapping = 307;
-	var RepeatWrapping = 1000;
-	var ClampToEdgeWrapping = 1001;
-	var MirroredRepeatWrapping = 1002;
-	var NearestFilter = 1003;
-	var NearestMipmapNearestFilter = 1004;
-	var NearestMipMapNearestFilter = 1004;
-	var NearestMipmapLinearFilter = 1005;
-	var NearestMipMapLinearFilter = 1005;
-	var LinearFilter = 1006;
-	var LinearMipmapNearestFilter = 1007;
-	var LinearMipMapNearestFilter = 1007;
-	var LinearMipmapLinearFilter = 1008;
-	var LinearMipMapLinearFilter = 1008;
-	var UnsignedByteType = 1009;
-	var ByteType = 1010;
-	var ShortType = 1011;
-	var UnsignedShortType = 1012;
-	var IntType = 1013;
-	var UnsignedIntType = 1014;
-	var FloatType = 1015;
-	var HalfFloatType = 1016;
-	var UnsignedShort4444Type = 1017;
-	var UnsignedShort5551Type = 1018;
-	var UnsignedShort565Type = 1019;
-	var UnsignedInt248Type = 1020;
-	var AlphaFormat = 1021;
-	var RGBFormat = 1022;
-	var RGBAFormat = 1023;
-	var LuminanceFormat = 1024;
-	var LuminanceAlphaFormat = 1025;
-	var RGBEFormat = RGBAFormat;
-	var DepthFormat = 1026;
-	var DepthStencilFormat = 1027;
-	var RedFormat = 1028;
-	var RedIntegerFormat = 1029;
-	var RGFormat = 1030;
-	var RGIntegerFormat = 1031;
-	var RGBIntegerFormat = 1032;
-	var RGBAIntegerFormat = 1033;
+  delete track.minPTS;
 
-	var RGB_S3TC_DXT1_Format = 33776;
-	var RGBA_S3TC_DXT1_Format = 33777;
-	var RGBA_S3TC_DXT3_Format = 33778;
-	var RGBA_S3TC_DXT5_Format = 33779;
-	var RGB_PVRTC_4BPPV1_Format = 35840;
-	var RGB_PVRTC_2BPPV1_Format = 35841;
-	var RGBA_PVRTC_4BPPV1_Format = 35842;
-	var RGBA_PVRTC_2BPPV1_Format = 35843;
-	var RGB_ETC1_Format = 36196;
-	var RGB_ETC2_Format = 37492;
-	var RGBA_ETC2_EAC_Format = 37496;
-	var RGBA_ASTC_4x4_Format = 37808;
-	var RGBA_ASTC_5x4_Format = 37809;
-	var RGBA_ASTC_5x5_Format = 37810;
-	var RGBA_ASTC_6x5_Format = 37811;
-	var RGBA_ASTC_6x6_Format = 37812;
-	var RGBA_ASTC_8x5_Format = 37813;
-	var RGBA_ASTC_8x6_Format = 37814;
-	var RGBA_ASTC_8x8_Format = 37815;
-	var RGBA_ASTC_10x5_Format = 37816;
-	var RGBA_ASTC_10x6_Format = 37817;
-	var RGBA_ASTC_10x8_Format = 37818;
-	var RGBA_ASTC_10x10_Format = 37819;
-	var RGBA_ASTC_12x10_Format = 37820;
-	var RGBA_ASTC_12x12_Format = 37821;
-	var RGBA_BPTC_Format = 36492;
-	var SRGB8_ALPHA8_ASTC_4x4_Format = 37840;
-	var SRGB8_ALPHA8_ASTC_5x4_Format = 37841;
-	var SRGB8_ALPHA8_ASTC_5x5_Format = 37842;
-	var SRGB8_ALPHA8_ASTC_6x5_Format = 37843;
-	var SRGB8_ALPHA8_ASTC_6x6_Format = 37844;
-	var SRGB8_ALPHA8_ASTC_8x5_Format = 37845;
-	var SRGB8_ALPHA8_ASTC_8x6_Format = 37846;
-	var SRGB8_ALPHA8_ASTC_8x8_Format = 37847;
-	var SRGB8_ALPHA8_ASTC_10x5_Format = 37848;
-	var SRGB8_ALPHA8_ASTC_10x6_Format = 37849;
-	var SRGB8_ALPHA8_ASTC_10x8_Format = 37850;
-	var SRGB8_ALPHA8_ASTC_10x10_Format = 37851;
-	var SRGB8_ALPHA8_ASTC_12x10_Format = 37852;
-	var SRGB8_ALPHA8_ASTC_12x12_Format = 37853;
-	var LoopOnce = 2200;
-	var LoopRepeat = 2201;
-	var LoopPingPong = 2202;
-	var InterpolateDiscrete = 2300;
-	var InterpolateLinear = 2301;
-	var InterpolateSmooth = 2302;
-	var ZeroCurvatureEnding = 2400;
-	var ZeroSlopeEnding = 2401;
-	var WrapAroundEnding = 2402;
-	var NormalAnimationBlendMode = 2500;
-	var AdditiveAnimationBlendMode = 2501;
-	var TrianglesDrawMode = 0;
-	var TriangleStripDrawMode = 1;
-	var TriangleFanDrawMode = 2;
-	var LinearEncoding = 3000;
-	var sRGBEncoding = 3001;
-	var GammaEncoding = 3007;
-	var RGBEEncoding = 3002;
-	var LogLuvEncoding = 3003;
-	var RGBM7Encoding = 3004;
-	var RGBM16Encoding = 3005;
-	var RGBDEncoding = 3006;
-	var BasicDepthPacking = 3200;
-	var RGBADepthPacking = 3201;
-	var TangentSpaceNormalMap = 0;
-	var ObjectSpaceNormalMap = 1;
+  this.gopCache_ = [];
 
-	var ZeroStencilOp = 0;
-	var KeepStencilOp = 7680;
-	var ReplaceStencilOp = 7681;
-	var IncrementStencilOp = 7682;
-	var DecrementStencilOp = 7683;
-	var IncrementWrapStencilOp = 34055;
-	var DecrementWrapStencilOp = 34056;
-	var InvertStencilOp = 5386;
+  /**
+    * Constructs a ISO BMFF segment given H264 nalUnits
+    * @param {Object} nalUnit A data event representing a nalUnit
+    * @param {String} nalUnit.nalUnitType
+    * @param {Object} nalUnit.config Properties for a mp4 track
+    * @param {Uint8Array} nalUnit.data The nalUnit bytes
+    * @see lib/codecs/h264.js
+   **/
+  this.push = function(nalUnit) {
+    trackDecodeInfo.collectDtsInfo(track, nalUnit);
 
-	var NeverStencilFunc = 512;
-	var LessStencilFunc = 513;
-	var EqualStencilFunc = 514;
-	var LessEqualStencilFunc = 515;
-	var GreaterStencilFunc = 516;
-	var NotEqualStencilFunc = 517;
-	var GreaterEqualStencilFunc = 518;
-	var AlwaysStencilFunc = 519;
+    // record the track config
+    if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
+      config = nalUnit.config;
+      track.sps = [nalUnit.data];
 
-	var StaticDrawUsage = 35044;
-	var DynamicDrawUsage = 35048;
-	var StreamDrawUsage = 35040;
-	var StaticReadUsage = 35045;
-	var DynamicReadUsage = 35049;
-	var StreamReadUsage = 35041;
-	var StaticCopyUsage = 35046;
-	var DynamicCopyUsage = 35050;
-	var StreamCopyUsage = 35042;
+      VIDEO_PROPERTIES.forEach(function(prop) {
+        track[prop] = config[prop];
+      }, this);
+    }
 
-	/**
-	 * https://github.com/mrdoob/eventdispatcher.js/
-	 */
+    if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' &&
+        !pps) {
+      pps = nalUnit.data;
+      track.pps = [nalUnit.data];
+    }
 
-	function EventDispatcher() {}
+    // buffer video until flush() is called
+    nalUnits.push(nalUnit);
+  };
 
-	Object.assign( EventDispatcher.prototype, {
+  /**
+    * Pass constructed ISO BMFF track and boxes on to the
+    * next stream in the pipeline
+   **/
+  this.flush = function() {
+    var
+      frames,
+      gopForFusion,
+      gops,
+      moof,
+      mdat,
+      boxes,
+      prependedContentDuration = 0,
+      firstGop,
+      lastGop;
+
+    // Throw away nalUnits at the start of the byte stream until
+    // we find the first AUD
+    while (nalUnits.length) {
+      if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
+        break;
+      }
+      nalUnits.shift();
+    }
 
-		addEventListener: function ( type, listener ) {
+    // Return early if no video data has been observed
+    if (nalUnits.length === 0) {
+      this.resetStream_();
+      this.trigger('done', 'VideoSegmentStream');
+      return;
+    }
 
-			if ( this._listeners === undefined ) { this._listeners = {}; }
+    // Organize the raw nal-units into arrays that represent
+    // higher-level constructs such as frames and gops
+    // (group-of-pictures)
+    frames = frameUtils.groupNalsIntoFrames(nalUnits);
+    gops = frameUtils.groupFramesIntoGops(frames);
+
+    // If the first frame of this fragment is not a keyframe we have
+    // a problem since MSE (on Chrome) requires a leading keyframe.
+    //
+    // We have two approaches to repairing this situation:
+    // 1) GOP-FUSION:
+    //    This is where we keep track of the GOPS (group-of-pictures)
+    //    from previous fragments and attempt to find one that we can
+    //    prepend to the current fragment in order to create a valid
+    //    fragment.
+    // 2) KEYFRAME-PULLING:
+    //    Here we search for the first keyframe in the fragment and
+    //    throw away all the frames between the start of the fragment
+    //    and that keyframe. We then extend the duration and pull the
+    //    PTS of the keyframe forward so that it covers the time range
+    //    of the frames that were disposed of.
+    //
+    // #1 is far prefereable over #2 which can cause "stuttering" but
+    // requires more things to be just right.
+    if (!gops[0][0].keyFrame) {
+      // Search for a gop for fusion from our gopCache
+      gopForFusion = this.getGopForFusion_(nalUnits[0], track);
+
+      if (gopForFusion) {
+        // in order to provide more accurate timing information about the segment, save
+        // the number of seconds prepended to the original segment due to GOP fusion
+        prependedContentDuration = gopForFusion.duration;
+
+        gops.unshift(gopForFusion);
+        // Adjust Gops' metadata to account for the inclusion of the
+        // new gop at the beginning
+        gops.byteLength += gopForFusion.byteLength;
+        gops.nalCount += gopForFusion.nalCount;
+        gops.pts = gopForFusion.pts;
+        gops.dts = gopForFusion.dts;
+        gops.duration += gopForFusion.duration;
+      } else {
+        // If we didn't find a candidate gop fall back to keyframe-pulling
+        gops = frameUtils.extendFirstKeyFrame(gops);
+      }
+    }
 
-			var listeners = this._listeners;
+    // Trim gops to align with gopsToAlignWith
+    if (gopsToAlignWith.length) {
+      var alignedGops;
 
-			if ( listeners[ type ] === undefined ) {
+      if (options.alignGopsAtEnd) {
+        alignedGops = this.alignGopsAtEnd_(gops);
+      } else {
+        alignedGops = this.alignGopsAtStart_(gops);
+      }
 
-				listeners[ type ] = [];
+      if (!alignedGops) {
+        // save all the nals in the last GOP into the gop cache
+        this.gopCache_.unshift({
+          gop: gops.pop(),
+          pps: track.pps,
+          sps: track.sps
+        });
 
-			}
+        // Keep a maximum of 6 GOPs in the cache
+        this.gopCache_.length = Math.min(6, this.gopCache_.length);
 
-			if ( listeners[ type ].indexOf( listener ) === - 1 ) {
+        // Clear nalUnits
+        nalUnits = [];
 
-				listeners[ type ].push( listener );
+        // return early no gops can be aligned with desired gopsToAlignWith
+        this.resetStream_();
+        this.trigger('done', 'VideoSegmentStream');
+        return;
+      }
 
-			}
+      // Some gops were trimmed. clear dts info so minSegmentDts and pts are correct
+      // when recalculated before sending off to CoalesceStream
+      trackDecodeInfo.clearDtsInfo(track);
 
-		},
+      gops = alignedGops;
+    }
 
-		hasEventListener: function ( type, listener ) {
+    trackDecodeInfo.collectDtsInfo(track, gops);
 
-			if ( this._listeners === undefined ) { return false; }
+    // First, we have to build the index from byte locations to
+    // samples (that is, frames) in the video data
+    track.samples = frameUtils.generateSampleTable(gops);
 
-			var listeners = this._listeners;
+    // Concatenate the video data and construct the mdat
+    mdat = mp4.mdat(frameUtils.concatenateNalData(gops));
 
-			return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1;
+    track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(
+      track, options.keepOriginalTimestamps);
 
-		},
+    this.trigger('processedGopsInfo', gops.map(function(gop) {
+      return {
+        pts: gop.pts,
+        dts: gop.dts,
+        byteLength: gop.byteLength
+      };
+    }));
+
+    firstGop = gops[0];
+    lastGop = gops[gops.length - 1];
+
+    this.trigger(
+      'segmentTimingInfo',
+      generateVideoSegmentTimingInfo(
+        track.baseMediaDecodeTime,
+        firstGop.dts,
+        firstGop.pts,
+        lastGop.dts + lastGop.duration,
+        lastGop.pts + lastGop.duration,
+        prependedContentDuration));
+
+    this.trigger('timingInfo', {
+      start: gops[0].pts,
+      end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
+    });
 
-		removeEventListener: function ( type, listener ) {
+    // save all the nals in the last GOP into the gop cache
+    this.gopCache_.unshift({
+      gop: gops.pop(),
+      pps: track.pps,
+      sps: track.sps
+    });
 
-			if ( this._listeners === undefined ) { return; }
+    // Keep a maximum of 6 GOPs in the cache
+    this.gopCache_.length = Math.min(6, this.gopCache_.length);
 
-			var listeners = this._listeners;
-			var listenerArray = listeners[ type ];
+    // Clear nalUnits
+    nalUnits = [];
 
-			if ( listenerArray !== undefined ) {
+    this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
+    this.trigger('timelineStartInfo', track.timelineStartInfo);
 
-				var index = listenerArray.indexOf( listener );
+    moof = mp4.moof(sequenceNumber, [track]);
 
-				if ( index !== - 1 ) {
+    // it would be great to allocate this array up front instead of
+    // throwing away hundreds of media segment fragments
+    boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
 
-					listenerArray.splice( index, 1 );
+    // Bump the sequence number for next time
+    sequenceNumber++;
 
-				}
+    boxes.set(moof);
+    boxes.set(mdat, moof.byteLength);
 
-			}
+    this.trigger('data', {track: track, boxes: boxes});
 
-		},
+    this.resetStream_();
 
-		dispatchEvent: function ( event ) {
+    // Continue with the flush process now
+    this.trigger('done', 'VideoSegmentStream');
+  };
 
-			if ( this._listeners === undefined ) { return; }
+  this.reset = function() {
+    this.resetStream_();
+    nalUnits = [];
+    this.gopCache_.length = 0;
+    gopsToAlignWith.length = 0;
+    this.trigger('reset');
+  };
 
-			var listeners = this._listeners;
-			var listenerArray = listeners[ event.type ];
+  this.resetStream_ = function() {
+    trackDecodeInfo.clearDtsInfo(track);
 
-			if ( listenerArray !== undefined ) {
+    // reset config and pps because they may differ across segments
+    // for instance, when we are rendition switching
+    config = undefined;
+    pps = undefined;
+  };
 
-				event.target = this;
+  // Search for a candidate Gop for gop-fusion from the gop cache and
+  // return it or return null if no good candidate was found
+  this.getGopForFusion_ = function(nalUnit) {
+    var
+      halfSecond = 45000, // Half-a-second in a 90khz clock
+      allowableOverlap = 10000, // About 3 frames @ 30fps
+      nearestDistance = Infinity,
+      dtsDistance,
+      nearestGopObj,
+      currentGop,
+      currentGopObj,
+      i;
+
+    // Search for the GOP nearest to the beginning of this nal unit
+    for (i = 0; i < this.gopCache_.length; i++) {
+      currentGopObj = this.gopCache_[i];
+      currentGop = currentGopObj.gop;
+
+      // Reject Gops with different SPS or PPS
+      if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) ||
+          !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
+        continue;
+      }
 
-				// Make a copy, in case listeners are removed while iterating.
-				var array = listenerArray.slice( 0 );
+      // Reject Gops that would require a negative baseMediaDecodeTime
+      if (currentGop.dts < track.timelineStartInfo.dts) {
+        continue;
+      }
 
-				for ( var i = 0, l = array.length; i < l; i ++ ) {
+      // The distance between the end of the gop and the start of the nalUnit
+      dtsDistance = (nalUnit.dts - currentGop.dts) - currentGop.duration;
 
-					array[ i ].call( this, event );
+      // Only consider GOPS that start before the nal unit and end within
+      // a half-second of the nal unit
+      if (dtsDistance >= -allowableOverlap &&
+          dtsDistance <= halfSecond) {
 
-				}
+        // Always use the closest GOP we found if there is more than
+        // one candidate
+        if (!nearestGopObj ||
+            nearestDistance > dtsDistance) {
+          nearestGopObj = currentGopObj;
+          nearestDistance = dtsDistance;
+        }
+      }
+    }
 
-			}
+    if (nearestGopObj) {
+      return nearestGopObj.gop;
+    }
+    return null;
+  };
 
-		}
+  // trim gop list to the first gop found that has a matching pts with a gop in the list
+  // of gopsToAlignWith starting from the START of the list
+  this.alignGopsAtStart_ = function(gops) {
+    var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
 
-	} );
+    byteLength = gops.byteLength;
+    nalCount = gops.nalCount;
+    duration = gops.duration;
+    alignIndex = gopIndex = 0;
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author thezwap
-	 */
+    while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
+      align = gopsToAlignWith[alignIndex];
+      gop = gops[gopIndex];
 
-	var _lut = [];
+      if (align.pts === gop.pts) {
+        break;
+      }
 
-	for ( var i = 0; i < 256; i ++ ) {
+      if (gop.pts > align.pts) {
+        // this current gop starts after the current gop we want to align on, so increment
+        // align index
+        alignIndex++;
+        continue;
+      }
 
-		_lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
+      // current gop starts before the current gop we want to align on. so increment gop
+      // index
+      gopIndex++;
+      byteLength -= gop.byteLength;
+      nalCount -= gop.nalCount;
+      duration -= gop.duration;
+    }
 
-	}
+    if (gopIndex === 0) {
+      // no gops to trim
+      return gops;
+    }
 
-	var MathUtils = {
+    if (gopIndex === gops.length) {
+      // all gops trimmed, skip appending all gops
+      return null;
+    }
 
-		DEG2RAD: Math.PI / 180,
-		RAD2DEG: 180 / Math.PI,
+    alignedGops = gops.slice(gopIndex);
+    alignedGops.byteLength = byteLength;
+    alignedGops.duration = duration;
+    alignedGops.nalCount = nalCount;
+    alignedGops.pts = alignedGops[0].pts;
+    alignedGops.dts = alignedGops[0].dts;
 
-		generateUUID: function () {
+    return alignedGops;
+  };
 
-			// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
+  // trim gop list to the first gop found that has a matching pts with a gop in the list
+  // of gopsToAlignWith starting from the END of the list
+  this.alignGopsAtEnd_ = function(gops) {
+    var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
 
-			var d0 = Math.random() * 0xffffffff | 0;
-			var d1 = Math.random() * 0xffffffff | 0;
-			var d2 = Math.random() * 0xffffffff | 0;
-			var d3 = Math.random() * 0xffffffff | 0;
-			var uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
-				_lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
-				_lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
-				_lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
+    alignIndex = gopsToAlignWith.length - 1;
+    gopIndex = gops.length - 1;
+    alignEndIndex = null;
+    matchFound = false;
 
-			// .toUpperCase() here flattens concatenated strings to save heap memory space.
-			return uuid.toUpperCase();
+    while (alignIndex >= 0 && gopIndex >= 0) {
+      align = gopsToAlignWith[alignIndex];
+      gop = gops[gopIndex];
 
-		},
+      if (align.pts === gop.pts) {
+        matchFound = true;
+        break;
+      }
 
-		clamp: function ( value, min, max ) {
+      if (align.pts > gop.pts) {
+        alignIndex--;
+        continue;
+      }
 
-			return Math.max( min, Math.min( max, value ) );
+      if (alignIndex === gopsToAlignWith.length - 1) {
+        // gop.pts is greater than the last alignment candidate. If no match is found
+        // by the end of this loop, we still want to append gops that come after this
+        // point
+        alignEndIndex = gopIndex;
+      }
 
-		},
+      gopIndex--;
+    }
 
-		// compute euclidian modulo of m % n
-		// https://en.wikipedia.org/wiki/Modulo_operation
+    if (!matchFound && alignEndIndex === null) {
+      return null;
+    }
 
-		euclideanModulo: function ( n, m ) {
+    var trimIndex;
 
-			return ( ( n % m ) + m ) % m;
+    if (matchFound) {
+      trimIndex = gopIndex;
+    } else {
+      trimIndex = alignEndIndex;
+    }
 
-		},
+    if (trimIndex === 0) {
+      return gops;
+    }
 
-		// Linear mapping from range <a1, a2> to range <b1, b2>
+    var alignedGops = gops.slice(trimIndex);
+    var metadata = alignedGops.reduce(function(total, gop) {
+      total.byteLength += gop.byteLength;
+      total.duration += gop.duration;
+      total.nalCount += gop.nalCount;
+      return total;
+    }, { byteLength: 0, duration: 0, nalCount: 0 });
+
+    alignedGops.byteLength = metadata.byteLength;
+    alignedGops.duration = metadata.duration;
+    alignedGops.nalCount = metadata.nalCount;
+    alignedGops.pts = alignedGops[0].pts;
+    alignedGops.dts = alignedGops[0].dts;
+
+    return alignedGops;
+  };
 
-		mapLinear: function ( x, a1, a2, b1, b2 ) {
+  this.alignGopsWith = function(newGopsToAlignWith) {
+    gopsToAlignWith = newGopsToAlignWith;
+  };
+};
 
-			return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
+VideoSegmentStream.prototype = new Stream();
 
-		},
+/**
+ * A Stream that can combine multiple streams (ie. audio & video)
+ * into a single output segment for MSE. Also supports audio-only
+ * and video-only streams.
+ * @param options {object} transmuxer options object
+ * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
+ *        in the source; false to adjust the first segment to start at media timeline start.
+ */
+CoalesceStream = function(options, metadataStream) {
+  // Number of Tracks per output segment
+  // If greater than 1, we combine multiple
+  // tracks into a single segment
+  this.numberOfTracks = 0;
+  this.metadataStream = metadataStream;
 
-		// https://en.wikipedia.org/wiki/Linear_interpolation
+  options = options || {};
 
-		lerp: function ( x, y, t ) {
+  if (typeof options.remux !== 'undefined') {
+    this.remuxTracks = !!options.remux;
+  } else {
+    this.remuxTracks = true;
+  }
 
-			return ( 1 - t ) * x + t * y;
+  if (typeof options.keepOriginalTimestamps === 'boolean') {
+    this.keepOriginalTimestamps = options.keepOriginalTimestamps;
+  } else {
+    this.keepOriginalTimestamps = false;
+  }
 
-		},
+  this.pendingTracks = [];
+  this.videoTrack = null;
+  this.pendingBoxes = [];
+  this.pendingCaptions = [];
+  this.pendingMetadata = [];
+  this.pendingBytes = 0;
+  this.emittedTracks = 0;
+
+  CoalesceStream.prototype.init.call(this);
+
+  // Take output from multiple
+  this.push = function(output) {
+    // buffer incoming captions until the associated video segment
+    // finishes
+    if (output.text) {
+      return this.pendingCaptions.push(output);
+    }
+    // buffer incoming id3 tags until the final flush
+    if (output.frames) {
+      return this.pendingMetadata.push(output);
+    }
 
-		// http://en.wikipedia.org/wiki/Smoothstep
+    // Add this track to the list of pending tracks and store
+    // important information required for the construction of
+    // the final segment
+    this.pendingTracks.push(output.track);
+    this.pendingBytes += output.boxes.byteLength;
+
+    // TODO: is there an issue for this against chrome?
+    // We unshift audio and push video because
+    // as of Chrome 75 when switching from
+    // one init segment to another if the video
+    // mdat does not appear after the audio mdat
+    // only audio will play for the duration of our transmux.
+    if (output.track.type === 'video') {
+      this.videoTrack = output.track;
+      this.pendingBoxes.push(output.boxes);
+    }
+    if (output.track.type === 'audio') {
+      this.audioTrack = output.track;
+      this.pendingBoxes.unshift(output.boxes);
+    }
+  };
+};
 
-		smoothstep: function ( x, min, max ) {
+CoalesceStream.prototype = new Stream();
+CoalesceStream.prototype.flush = function(flushSource) {
+  var
+    offset = 0,
+    event = {
+      captions: [],
+      captionStreams: {},
+      metadata: [],
+      info: {}
+    },
+    caption,
+    id3,
+    initSegment,
+    timelineStartPts = 0,
+    i;
+
+  if (this.pendingTracks.length < this.numberOfTracks) {
+    if (flushSource !== 'VideoSegmentStream' &&
+        flushSource !== 'AudioSegmentStream') {
+      // Return because we haven't received a flush from a data-generating
+      // portion of the segment (meaning that we have only recieved meta-data
+      // or captions.)
+      return;
+    } else if (this.remuxTracks) {
+      // Return until we have enough tracks from the pipeline to remux (if we
+      // are remuxing audio and video into a single MP4)
+      return;
+    } else if (this.pendingTracks.length === 0) {
+      // In the case where we receive a flush without any data having been
+      // received we consider it an emitted track for the purposes of coalescing
+      // `done` events.
+      // We do this for the case where there is an audio and video track in the
+      // segment but no audio data. (seen in several playlists with alternate
+      // audio tracks and no audio present in the main TS segments.)
+      this.emittedTracks++;
+
+      if (this.emittedTracks >= this.numberOfTracks) {
+        this.trigger('done');
+        this.emittedTracks = 0;
+      }
+      return;
+    }
+  }
 
-			if ( x <= min ) { return 0; }
-			if ( x >= max ) { return 1; }
+  if (this.videoTrack) {
+    timelineStartPts = this.videoTrack.timelineStartInfo.pts;
+    VIDEO_PROPERTIES.forEach(function(prop) {
+      event.info[prop] = this.videoTrack[prop];
+    }, this);
+  } else if (this.audioTrack) {
+    timelineStartPts = this.audioTrack.timelineStartInfo.pts;
+    AUDIO_PROPERTIES.forEach(function(prop) {
+      event.info[prop] = this.audioTrack[prop];
+    }, this);
+  }
 
-			x = ( x - min ) / ( max - min );
+  if (this.videoTrack || this.audioTrack) {
+    if (this.pendingTracks.length === 1) {
+      event.type = this.pendingTracks[0].type;
+    } else {
+      event.type = 'combined';
+    }
 
-			return x * x * ( 3 - 2 * x );
+    this.emittedTracks += this.pendingTracks.length;
 
-		},
+    initSegment = mp4.initSegment(this.pendingTracks);
 
-		smootherstep: function ( x, min, max ) {
+    // Create a new typed array to hold the init segment
+    event.initSegment = new Uint8Array(initSegment.byteLength);
 
-			if ( x <= min ) { return 0; }
-			if ( x >= max ) { return 1; }
+    // Create an init segment containing a moov
+    // and track definitions
+    event.initSegment.set(initSegment);
 
-			x = ( x - min ) / ( max - min );
+    // Create a new typed array to hold the moof+mdats
+    event.data = new Uint8Array(this.pendingBytes);
 
-			return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
+    // Append each moof+mdat (one per track) together
+    for (i = 0; i < this.pendingBoxes.length; i++) {
+      event.data.set(this.pendingBoxes[i], offset);
+      offset += this.pendingBoxes[i].byteLength;
+    }
 
-		},
+    // Translate caption PTS times into second offsets to match the
+    // video timeline for the segment, and add track info
+    for (i = 0; i < this.pendingCaptions.length; i++) {
+      caption = this.pendingCaptions[i];
+      caption.startTime = clock.metadataTsToSeconds(
+        caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
+      caption.endTime = clock.metadataTsToSeconds(
+        caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
+
+      event.captionStreams[caption.stream] = true;
+      event.captions.push(caption);
+    }
 
-		// Random integer from <low, high> interval
+    // Translate ID3 frame PTS times into second offsets to match the
+    // video timeline for the segment
+    for (i = 0; i < this.pendingMetadata.length; i++) {
+      id3 = this.pendingMetadata[i];
+      id3.cueTime = clock.metadataTsToSeconds(
+        id3.pts, timelineStartPts, this.keepOriginalTimestamps);
 
-		randInt: function ( low, high ) {
+      event.metadata.push(id3);
+    }
 
-			return low + Math.floor( Math.random() * ( high - low + 1 ) );
+    // We add this to every single emitted segment even though we only need
+    // it for the first
+    event.metadata.dispatchType = this.metadataStream.dispatchType;
+
+    // Reset stream state
+    this.pendingTracks.length = 0;
+    this.videoTrack = null;
+    this.pendingBoxes.length = 0;
+    this.pendingCaptions.length = 0;
+    this.pendingBytes = 0;
+    this.pendingMetadata.length = 0;
+
+    // Emit the built segment
+    // We include captions and ID3 tags for backwards compatibility,
+    // ideally we should send only video and audio in the data event
+    this.trigger('data', event);
+    // Emit each caption to the outside world
+    // Ideally, this would happen immediately on parsing captions,
+    // but we need to ensure that video data is sent back first
+    // so that caption timing can be adjusted to match video timing
+    for (i = 0; i < event.captions.length; i++) {
+      caption = event.captions[i];
+
+      this.trigger('caption', caption);
+    }
+    // Emit each id3 tag to the outside world
+    // Ideally, this would happen immediately on parsing the tag,
+    // but we need to ensure that video data is sent back first
+    // so that ID3 frame timing can be adjusted to match video timing
+    for (i = 0; i < event.metadata.length; i++) {
+      id3 = event.metadata[i];
+
+      this.trigger('id3Frame', id3);
+    }
+  }
 
-		},
+  // Only emit `done` if all tracks have been flushed and emitted
+  if (this.emittedTracks >= this.numberOfTracks) {
+    this.trigger('done');
+    this.emittedTracks = 0;
+  }
+};
 
-		// Random float from <low, high> interval
+CoalesceStream.prototype.setRemux = function(val) {
+  this.remuxTracks = val;
+};
+/**
+ * A Stream that expects MP2T binary data as input and produces
+ * corresponding media segments, suitable for use with Media Source
+ * Extension (MSE) implementations that support the ISO BMFF byte
+ * stream format, like Chrome.
+ */
+Transmuxer = function(options) {
+  var
+    self = this,
+    hasFlushed = true,
+    videoTrack,
+    audioTrack;
 
-		randFloat: function ( low, high ) {
+  Transmuxer.prototype.init.call(this);
 
-			return low + Math.random() * ( high - low );
+  options = options || {};
+  this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
+  this.transmuxPipeline_ = {};
+
+  this.setupAacPipeline = function() {
+    var pipeline = {};
+    this.transmuxPipeline_ = pipeline;
+
+    pipeline.type = 'aac';
+    pipeline.metadataStream = new m2ts.MetadataStream();
+
+    // set up the parsing pipeline
+    pipeline.aacStream = new AacStream();
+    pipeline.audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
+    pipeline.timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
+    pipeline.adtsStream = new AdtsStream();
+    pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
+    pipeline.headOfPipeline = pipeline.aacStream;
+
+    pipeline.aacStream
+      .pipe(pipeline.audioTimestampRolloverStream)
+      .pipe(pipeline.adtsStream);
+    pipeline.aacStream
+      .pipe(pipeline.timedMetadataTimestampRolloverStream)
+      .pipe(pipeline.metadataStream)
+      .pipe(pipeline.coalesceStream);
+
+    pipeline.metadataStream.on('timestamp', function(frame) {
+      pipeline.aacStream.setTimestamp(frame.timeStamp);
+    });
 
-		},
+    pipeline.aacStream.on('data', function(data) {
+      if (data.type === 'timed-metadata' && !pipeline.audioSegmentStream) {
+        audioTrack = audioTrack || {
+          timelineStartInfo: {
+            baseMediaDecodeTime: self.baseMediaDecodeTime
+          },
+          codec: 'adts',
+          type: 'audio'
+        };
+        // hook up the audio segment stream to the first track with aac data
+        pipeline.coalesceStream.numberOfTracks++;
+        pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
 
-		// Random float from <-range/2, range/2> interval
+        pipeline.audioSegmentStream.on('timingInfo',
+          self.trigger.bind(self, 'audioTimingInfo'));
 
-		randFloatSpread: function ( range ) {
+        // Set up the final part of the audio pipeline
+        pipeline.adtsStream
+          .pipe(pipeline.audioSegmentStream)
+          .pipe(pipeline.coalesceStream);
+      }
 
-			return range * ( 0.5 - Math.random() );
+      // emit pmt info
+      self.trigger('trackinfo', {
+        hasAudio: !!audioTrack,
+        hasVideo: !!videoTrack
+      });
+    });
 
-		},
-
-		degToRad: function ( degrees ) {
-
-			return degrees * MathUtils.DEG2RAD;
+    // Re-emit any data coming from the coalesce stream to the outside world
+    pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
+    // Let the consumer know we have finished flushing the entire pipeline
+    pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
+  };
 
-		},
+  this.setupTsPipeline = function() {
+    var pipeline = {};
+    this.transmuxPipeline_ = pipeline;
+
+    pipeline.type = 'ts';
+    pipeline.metadataStream = new m2ts.MetadataStream();
+
+    // set up the parsing pipeline
+    pipeline.packetStream = new m2ts.TransportPacketStream();
+    pipeline.parseStream = new m2ts.TransportParseStream();
+    pipeline.elementaryStream = new m2ts.ElementaryStream();
+    pipeline.timestampRolloverStream = new m2ts.TimestampRolloverStream();
+    pipeline.adtsStream = new AdtsStream();
+    pipeline.h264Stream = new H264Stream();
+    pipeline.captionStream = new m2ts.CaptionStream();
+    pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
+    pipeline.headOfPipeline = pipeline.packetStream;
+
+    // disassemble MPEG2-TS packets into elementary streams
+    pipeline.packetStream
+      .pipe(pipeline.parseStream)
+      .pipe(pipeline.elementaryStream)
+      .pipe(pipeline.timestampRolloverStream);
+
+    // !!THIS ORDER IS IMPORTANT!!
+    // demux the streams
+    pipeline.timestampRolloverStream
+      .pipe(pipeline.h264Stream);
+
+    pipeline.timestampRolloverStream
+      .pipe(pipeline.adtsStream);
+
+    pipeline.timestampRolloverStream
+      .pipe(pipeline.metadataStream)
+      .pipe(pipeline.coalesceStream);
+
+    // Hook up CEA-608/708 caption stream
+    pipeline.h264Stream.pipe(pipeline.captionStream)
+      .pipe(pipeline.coalesceStream);
+
+    pipeline.elementaryStream.on('data', function(data) {
+      var i;
+
+      if (data.type === 'metadata') {
+        i = data.tracks.length;
+
+        // scan the tracks listed in the metadata
+        while (i--) {
+          if (!videoTrack && data.tracks[i].type === 'video') {
+            videoTrack = data.tracks[i];
+            videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
+          } else if (!audioTrack && data.tracks[i].type === 'audio') {
+            audioTrack = data.tracks[i];
+            audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
+          }
+        }
 
-		radToDeg: function ( radians ) {
+        // hook up the video segment stream to the first track with h264 data
+        if (videoTrack && !pipeline.videoSegmentStream) {
+          pipeline.coalesceStream.numberOfTracks++;
+          pipeline.videoSegmentStream = new VideoSegmentStream(videoTrack, options);
+
+          pipeline.videoSegmentStream.on('timelineStartInfo', function(timelineStartInfo) {
+            // When video emits timelineStartInfo data after a flush, we forward that
+            // info to the AudioSegmentStream, if it exists, because video timeline
+            // data takes precedence.  Do not do this if keepOriginalTimestamps is set,
+            // because this is a particularly subtle form of timestamp alteration.
+            if (audioTrack && !options.keepOriginalTimestamps) {
+              audioTrack.timelineStartInfo = timelineStartInfo;
+              // On the first segment we trim AAC frames that exist before the
+              // very earliest DTS we have seen in video because Chrome will
+              // interpret any video track with a baseMediaDecodeTime that is
+              // non-zero as a gap.
+              pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
+            }
+          });
 
-			return radians * MathUtils.RAD2DEG;
+          pipeline.videoSegmentStream.on('processedGopsInfo',
+            self.trigger.bind(self, 'gopInfo'));
+          pipeline.videoSegmentStream.on('segmentTimingInfo',
+            self.trigger.bind(self, 'videoSegmentTimingInfo'));
 
-		},
+          pipeline.videoSegmentStream.on('baseMediaDecodeTime', function(baseMediaDecodeTime) {
+            if (audioTrack) {
+              pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
+            }
+          });
 
-		isPowerOfTwo: function ( value ) {
+          pipeline.videoSegmentStream.on('timingInfo',
+            self.trigger.bind(self, 'videoTimingInfo'));
 
-			return ( value & ( value - 1 ) ) === 0 && value !== 0;
+          // Set up the final part of the video pipeline
+          pipeline.h264Stream
+            .pipe(pipeline.videoSegmentStream)
+            .pipe(pipeline.coalesceStream);
+        }
 
-		},
+        if (audioTrack && !pipeline.audioSegmentStream) {
+          // hook up the audio segment stream to the first track with aac data
+          pipeline.coalesceStream.numberOfTracks++;
+          pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
 
-		ceilPowerOfTwo: function ( value ) {
+          pipeline.audioSegmentStream.on('timingInfo',
+            self.trigger.bind(self, 'audioTimingInfo'));
 
-			return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
+          // Set up the final part of the audio pipeline
+          pipeline.adtsStream
+            .pipe(pipeline.audioSegmentStream)
+            .pipe(pipeline.coalesceStream);
+        }
 
-		},
+        // emit pmt info
+        self.trigger('trackinfo', {
+          hasAudio: !!audioTrack,
+          hasVideo: !!videoTrack
+        });
+      }
+    });
 
-		floorPowerOfTwo: function ( value ) {
+    // Re-emit any data coming from the coalesce stream to the outside world
+    pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
+    pipeline.coalesceStream.on('id3Frame', function(id3Frame) {
+      id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
 
-			return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
+      self.trigger('id3Frame', id3Frame);
+    });
+    pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
+    // Let the consumer know we have finished flushing the entire pipeline
+    pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
+  };
 
-		},
+  // hook up the segment streams once track metadata is delivered
+  this.setBaseMediaDecodeTime = function(baseMediaDecodeTime) {
+    var pipeline = this.transmuxPipeline_;
 
-		setQuaternionFromProperEuler: function ( q, a, b, c, order ) {
+    if (!options.keepOriginalTimestamps) {
+      this.baseMediaDecodeTime = baseMediaDecodeTime;
+    }
 
-			// Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
+    if (audioTrack) {
+      audioTrack.timelineStartInfo.dts = undefined;
+      audioTrack.timelineStartInfo.pts = undefined;
+      trackDecodeInfo.clearDtsInfo(audioTrack);
+      if (pipeline.audioTimestampRolloverStream) {
+        pipeline.audioTimestampRolloverStream.discontinuity();
+      }
+    }
+    if (videoTrack) {
+      if (pipeline.videoSegmentStream) {
+        pipeline.videoSegmentStream.gopCache_ = [];
+      }
+      videoTrack.timelineStartInfo.dts = undefined;
+      videoTrack.timelineStartInfo.pts = undefined;
+      trackDecodeInfo.clearDtsInfo(videoTrack);
+      pipeline.captionStream.reset();
+    }
 
-			// rotations are applied to the axes in the order specified by 'order'
-			// rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
-			// angles are in radians
+    if (pipeline.timestampRolloverStream) {
+      pipeline.timestampRolloverStream.discontinuity();
+    }
+  };
 
-			var cos = Math.cos;
-			var sin = Math.sin;
+  this.setAudioAppendStart = function(timestamp) {
+    if (audioTrack) {
+      this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
+    }
+  };
 
-			var c2 = cos( b / 2 );
-			var s2 = sin( b / 2 );
+  this.setRemux = function(val) {
+    var pipeline = this.transmuxPipeline_;
 
-			var c13 = cos( ( a + c ) / 2 );
-			var s13 = sin( ( a + c ) / 2 );
+    options.remux = val;
 
-			var c1_3 = cos( ( a - c ) / 2 );
-			var s1_3 = sin( ( a - c ) / 2 );
+    if (pipeline && pipeline.coalesceStream) {
+      pipeline.coalesceStream.setRemux(val);
+    }
+  };
 
-			var c3_1 = cos( ( c - a ) / 2 );
-			var s3_1 = sin( ( c - a ) / 2 );
+  this.alignGopsWith = function(gopsToAlignWith) {
+    if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
+      this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
+    }
+  };
 
-			switch ( order ) {
+  // feed incoming data to the front of the parsing pipeline
+  this.push = function(data) {
+    if (hasFlushed) {
+      var isAac = isLikelyAacData(data);
 
-				case 'XYX':
-					q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
-					break;
+      if (isAac && this.transmuxPipeline_.type !== 'aac') {
+        this.setupAacPipeline();
+      } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
+        this.setupTsPipeline();
+      }
+      hasFlushed = false;
+    }
+    this.transmuxPipeline_.headOfPipeline.push(data);
+  };
 
-				case 'YZY':
-					q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
-					break;
+  // flush any buffered data
+  this.flush = function() {
+    hasFlushed = true;
+    // Start at the top of the pipeline and flush all pending work
+    this.transmuxPipeline_.headOfPipeline.flush();
+  };
 
-				case 'ZXZ':
-					q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
-					break;
+  this.endTimeline = function() {
+    this.transmuxPipeline_.headOfPipeline.endTimeline();
+  };
 
-				case 'XZX':
-					q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
-					break;
+  this.reset = function() {
+    if (this.transmuxPipeline_.headOfPipeline) {
+      this.transmuxPipeline_.headOfPipeline.reset();
+    }
+  };
 
-				case 'YXY':
-					q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
-					break;
+  // Caption data has to be reset when seeking outside buffered range
+  this.resetCaptions = function() {
+    if (this.transmuxPipeline_.captionStream) {
+      this.transmuxPipeline_.captionStream.reset();
+    }
+  };
 
-				case 'ZYZ':
-					q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
-					break;
+};
+Transmuxer.prototype = new Stream();
 
-				default:
-					console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
+module.exports = {
+  Transmuxer: Transmuxer,
+  VideoSegmentStream: VideoSegmentStream,
+  AudioSegmentStream: AudioSegmentStream,
+  AUDIO_PROPERTIES: AUDIO_PROPERTIES,
+  VIDEO_PROPERTIES: VIDEO_PROPERTIES,
+  // exported for testing
+  generateVideoSegmentTimingInfo: generateVideoSegmentTimingInfo
+};
 
-			}
+},{"../aac":19,"../aac/utils":20,"../codecs/adts.js":21,"../codecs/h264":22,"../constants/audio-properties.js":24,"../constants/video-properties.js":25,"../m2ts/m2ts.js":36,"../utils/clock":58,"../utils/stream.js":60,"./audio-frame-utils":41,"./frame-utils":43,"./mp4-generator.js":45,"./track-decode-info":47}],49:[function(require,module,exports){
+'use strict';
 
-		}
+var Stream = require('../utils/stream.js');
+var mp4 = require('../mp4/mp4-generator.js');
+var audioFrameUtils = require('../mp4/audio-frame-utils');
+var trackInfo = require('../mp4/track-decode-info.js');
+var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
+var AUDIO_PROPERTIES = require('../constants/audio-properties.js');
 
-	};
+/**
+ * Constructs a single-track, ISO BMFF media segment from AAC data
+ * events. The output of this stream can be fed to a SourceBuffer
+ * configured with a suitable initialization segment.
+ */
+var AudioSegmentStream = function(track, options) {
+  var
+    adtsFrames = [],
+    sequenceNumber = 0,
+    earliestAllowedDts = 0,
+    audioAppendStartTs = 0,
+    videoBaseMediaDecodeTime = Infinity,
+    segmentStartPts = null,
+    segmentEndPts = null;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author philogb / http://blog.thejit.org/
-	 * @author egraether / http://egraether.com/
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 */
+  options = options || {};
 
-	function Vector2( x, y ) {
+  AudioSegmentStream.prototype.init.call(this);
 
-		this.x = x || 0;
-		this.y = y || 0;
+  this.push = function(data) {
+    trackInfo.collectDtsInfo(track, data);
 
-	}
+    if (track) {
+      AUDIO_PROPERTIES.forEach(function(prop) {
+        track[prop] = data[prop];
+      });
+    }
 
-	Object.defineProperties( Vector2.prototype, {
+    // buffer audio data until end() is called
+    adtsFrames.push(data);
+  };
 
-		"width": {
+  this.setEarliestDts = function(earliestDts) {
+    earliestAllowedDts = earliestDts;
+  };
 
-			get: function () {
+  this.setVideoBaseMediaDecodeTime = function(baseMediaDecodeTime) {
+    videoBaseMediaDecodeTime = baseMediaDecodeTime;
+  };
 
-				return this.x;
+  this.setAudioAppendStart = function(timestamp) {
+    audioAppendStartTs = timestamp;
+  };
 
-			},
+  this.processFrames_ = function() {
+    var
+      frames,
+      moof,
+      mdat,
+      boxes,
+      timingInfo;
 
-			set: function ( value ) {
+    // return early if no audio data has been observed
+    if (adtsFrames.length === 0) {
+      return;
+    }
 
-				this.x = value;
+    frames = audioFrameUtils.trimAdtsFramesByEarliestDts(
+      adtsFrames, track, earliestAllowedDts);
+    if (frames.length === 0) {
+      // return early if the frames are all after the earliest allowed DTS
+      // TODO should we clear the adtsFrames?
+      return;
+    }
 
-			}
+    track.baseMediaDecodeTime = trackInfo.calculateTrackBaseMediaDecodeTime(
+      track, options.keepOriginalTimestamps);
 
-		},
+    audioFrameUtils.prefixWithSilence(
+      track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
 
-		"height": {
+    // we have to build the index from byte locations to
+    // samples (that is, adts frames) in the audio data
+    track.samples = audioFrameUtils.generateSampleTable(frames);
 
-			get: function () {
+    // concatenate the audio data to constuct the mdat
+    mdat = mp4.mdat(audioFrameUtils.concatenateFrameData(frames));
 
-				return this.y;
+    adtsFrames = [];
 
-			},
+    moof = mp4.moof(sequenceNumber, [track]);
 
-			set: function ( value ) {
+    // bump the sequence number for next time
+    sequenceNumber++;
 
-				this.y = value;
+    track.initSegment = mp4.initSegment([track]);
 
-			}
+    // it would be great to allocate this array up front instead of
+    // throwing away hundreds of media segment fragments
+    boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
 
-		}
+    boxes.set(moof);
+    boxes.set(mdat, moof.byteLength);
 
-	} );
+    trackInfo.clearDtsInfo(track);
 
-	Object.assign( Vector2.prototype, {
+    if (segmentStartPts === null) {
+      segmentEndPts = segmentStartPts = frames[0].pts;
+    }
 
-		isVector2: true,
+    segmentEndPts += frames.length * (ONE_SECOND_IN_TS * 1024 / track.samplerate);
 
-		set: function ( x, y ) {
+    timingInfo = { start: segmentStartPts };
 
-			this.x = x;
-			this.y = y;
+    this.trigger('timingInfo', timingInfo);
+    this.trigger('data', {track: track, boxes: boxes});
+  };
 
-			return this;
+  this.flush = function() {
+    this.processFrames_();
+    // trigger final timing info
+    this.trigger('timingInfo', {
+      start: segmentStartPts,
+      end: segmentEndPts
+    });
+    this.resetTiming_();
+    this.trigger('done', 'AudioSegmentStream');
+  };
 
-		},
+  this.partialFlush = function() {
+    this.processFrames_();
+    this.trigger('partialdone', 'AudioSegmentStream');
+  };
 
-		setScalar: function ( scalar ) {
+  this.endTimeline = function() {
+    this.flush();
+    this.trigger('endedtimeline', 'AudioSegmentStream');
+  };
 
-			this.x = scalar;
-			this.y = scalar;
+  this.resetTiming_ = function() {
+    trackInfo.clearDtsInfo(track);
+    segmentStartPts = null;
+    segmentEndPts = null;
+  };
 
-			return this;
+  this.reset = function() {
+    this.resetTiming_();
+    adtsFrames = [];
+    this.trigger('reset');
+  };
+};
 
-		},
+AudioSegmentStream.prototype = new Stream();
 
-		setX: function ( x ) {
+module.exports = AudioSegmentStream;
 
-			this.x = x;
+},{"../constants/audio-properties.js":24,"../mp4/audio-frame-utils":41,"../mp4/mp4-generator.js":45,"../mp4/track-decode-info.js":47,"../utils/clock":58,"../utils/stream.js":60}],50:[function(require,module,exports){
+module.exports = {
+  Transmuxer: require('./transmuxer')
+};
 
-			return this;
+},{"./transmuxer":51}],51:[function(require,module,exports){
+var Stream = require('../utils/stream.js');
+var m2ts = require('../m2ts/m2ts.js');
+var codecs = require('../codecs/index.js');
+var AudioSegmentStream = require('./audio-segment-stream.js');
+var VideoSegmentStream = require('./video-segment-stream.js');
+var trackInfo = require('../mp4/track-decode-info.js');
+var isLikelyAacData = require('../aac/utils').isLikelyAacData;
+var AdtsStream = require('../codecs/adts');
+var AacStream = require('../aac/index');
+var clock = require('../utils/clock');
+
+var createPipeline = function(object) {
+  object.prototype = new Stream();
+  object.prototype.init.call(object);
+
+  return object;
+};
 
-		},
+var tsPipeline = function(options) {
+  var
+    pipeline = {
+      type: 'ts',
+      tracks: {
+        audio: null,
+        video: null
+      },
+      packet: new m2ts.TransportPacketStream(),
+      parse: new m2ts.TransportParseStream(),
+      elementary: new m2ts.ElementaryStream(),
+      timestampRollover: new m2ts.TimestampRolloverStream(),
+      adts: new codecs.Adts(),
+      h264: new codecs.h264.H264Stream(),
+      captionStream: new m2ts.CaptionStream(),
+      metadataStream: new m2ts.MetadataStream()
+  };
 
-		setY: function ( y ) {
+  pipeline.headOfPipeline = pipeline.packet;
 
-			this.y = y;
+  // Transport Stream
+  pipeline.packet
+    .pipe(pipeline.parse)
+    .pipe(pipeline.elementary)
+    .pipe(pipeline.timestampRollover);
 
-			return this;
+  // H264
+  pipeline.timestampRollover
+    .pipe(pipeline.h264);
 
-		},
+  // Hook up CEA-608/708 caption stream
+  pipeline.h264
+    .pipe(pipeline.captionStream);
 
-		setComponent: function ( index, value ) {
+  pipeline.timestampRollover
+    .pipe(pipeline.metadataStream);
 
-			switch ( index ) {
+  // ADTS
+  pipeline.timestampRollover
+    .pipe(pipeline.adts);
 
-				case 0: this.x = value; break;
-				case 1: this.y = value; break;
-				default: throw new Error( 'index is out of range: ' + index );
+  pipeline.elementary.on('data', function(data) {
+    if (data.type !== 'metadata') {
+      return;
+    }
 
-			}
+    for (var i = 0; i < data.tracks.length; i++) {
+      if (!pipeline.tracks[data.tracks[i].type]) {
+        pipeline.tracks[data.tracks[i].type] = data.tracks[i];
+        pipeline.tracks[data.tracks[i].type].timelineStartInfo.baseMediaDecodeTime = options.baseMediaDecodeTime;
+      }
+    }
 
-			return this;
+    if (pipeline.tracks.video && !pipeline.videoSegmentStream) {
+      pipeline.videoSegmentStream = new VideoSegmentStream(pipeline.tracks.video, options);
 
-		},
+      pipeline.videoSegmentStream.on('timelineStartInfo', function(timelineStartInfo) {
+        if (pipeline.tracks.audio && !options.keepOriginalTimestamps) {
+          pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - options.baseMediaDecodeTime);
+        }
+      });
 
-		getComponent: function ( index ) {
+      pipeline.videoSegmentStream.on('timingInfo',
+                                     pipeline.trigger.bind(pipeline, 'videoTimingInfo'));
 
-			switch ( index ) {
+      pipeline.videoSegmentStream.on('data', function(data) {
+        pipeline.trigger('data', {
+          type: 'video',
+          data: data
+        });
+      });
 
-				case 0: return this.x;
-				case 1: return this.y;
-				default: throw new Error( 'index is out of range: ' + index );
+      pipeline.videoSegmentStream.on('done',
+                                     pipeline.trigger.bind(pipeline, 'done'));
+      pipeline.videoSegmentStream.on('partialdone',
+                                     pipeline.trigger.bind(pipeline, 'partialdone'));
+      pipeline.videoSegmentStream.on('endedtimeline',
+                                     pipeline.trigger.bind(pipeline, 'endedtimeline'));
 
-			}
+      pipeline.h264
+        .pipe(pipeline.videoSegmentStream);
+    }
 
-		},
+    if (pipeline.tracks.audio && !pipeline.audioSegmentStream) {
+      pipeline.audioSegmentStream = new AudioSegmentStream(pipeline.tracks.audio, options);
 
-		clone: function () {
+      pipeline.audioSegmentStream.on('data', function(data) {
+        pipeline.trigger('data', {
+          type: 'audio',
+          data: data
+        });
+      });
 
-			return new this.constructor( this.x, this.y );
+      pipeline.audioSegmentStream.on('done',
+                                     pipeline.trigger.bind(pipeline, 'done'));
+      pipeline.audioSegmentStream.on('partialdone',
+                                     pipeline.trigger.bind(pipeline, 'partialdone'));
+      pipeline.audioSegmentStream.on('endedtimeline',
+                                     pipeline.trigger.bind(pipeline, 'endedtimeline'));
 
-		},
+      pipeline.audioSegmentStream.on('timingInfo',
+                                     pipeline.trigger.bind(pipeline, 'audioTimingInfo'));
 
-		copy: function ( v ) {
+      pipeline.adts
+        .pipe(pipeline.audioSegmentStream);
+    }
 
-			this.x = v.x;
-			this.y = v.y;
+    // emit pmt info
+    pipeline.trigger('trackinfo', {
+      hasAudio: !!pipeline.tracks.audio,
+      hasVideo: !!pipeline.tracks.video
+    });
+  });
 
-			return this;
+  pipeline.captionStream.on('data', function(caption) {
+    var timelineStartPts;
 
-		},
+    if (pipeline.tracks.video) {
+      timelineStartPts = pipeline.tracks.video.timelineStartInfo.pts || 0;
+    } else {
+      // This will only happen if we encounter caption packets before
+      // video data in a segment. This is an unusual/unlikely scenario,
+      // so we assume the timeline starts at zero for now.
+      timelineStartPts = 0;
+    }
 
-		add: function ( v, w ) {
+    // Translate caption PTS times into second offsets into the
+    // video timeline for the segment
+    caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, options.keepOriginalTimestamps);
+    caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, options.keepOriginalTimestamps);
 
-			if ( w !== undefined ) {
+    pipeline.trigger('caption', caption);
+  });
 
-				console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
-				return this.addVectors( v, w );
+  pipeline = createPipeline(pipeline);
 
-			}
+  pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
 
-			this.x += v.x;
-			this.y += v.y;
+  return pipeline;
+};
 
-			return this;
+var aacPipeline = function(options) {
+  var
+    pipeline = {
+    type: 'aac',
+    tracks: {
+      audio: null
+    },
+    metadataStream: new m2ts.MetadataStream(),
+    aacStream: new AacStream(),
+    audioRollover: new m2ts.TimestampRolloverStream('audio'),
+    timedMetadataRollover: new m2ts.TimestampRolloverStream('timed-metadata'),
+    adtsStream: new AdtsStream(true)
+  };
 
-		},
+  // set up the parsing pipeline
+  pipeline.headOfPipeline = pipeline.aacStream;
 
-		addScalar: function ( s ) {
+  pipeline.aacStream
+    .pipe(pipeline.audioRollover)
+    .pipe(pipeline.adtsStream);
+  pipeline.aacStream
+    .pipe(pipeline.timedMetadataRollover)
+    .pipe(pipeline.metadataStream);
 
-			this.x += s;
-			this.y += s;
+  pipeline.metadataStream.on('timestamp', function(frame) {
+    pipeline.aacStream.setTimestamp(frame.timeStamp);
+  });
 
-			return this;
+  pipeline.aacStream.on('data', function(data) {
+    if (data.type !== 'timed-metadata' || pipeline.audioSegmentStream) {
+      return;
+    }
 
-		},
+    pipeline.tracks.audio = pipeline.tracks.audio || {
+      timelineStartInfo: {
+        baseMediaDecodeTime: options.baseMediaDecodeTime
+      },
+      codec: 'adts',
+      type: 'audio'
+    };
 
-		addVectors: function ( a, b ) {
+    // hook up the audio segment stream to the first track with aac data
+    pipeline.audioSegmentStream = new AudioSegmentStream(pipeline.tracks.audio, options);
 
-			this.x = a.x + b.x;
-			this.y = a.y + b.y;
+    pipeline.audioSegmentStream.on('data', function(data) {
+      pipeline.trigger('data', {
+        type: 'audio',
+        data: data
+      });
+    });
 
-			return this;
+    pipeline.audioSegmentStream.on('partialdone',
+                                   pipeline.trigger.bind(pipeline, 'partialdone'));
+    pipeline.audioSegmentStream.on('done', pipeline.trigger.bind(pipeline, 'done'));
+    pipeline.audioSegmentStream.on('endedtimeline',
+                                   pipeline.trigger.bind(pipeline, 'endedtimeline'));
+    pipeline.audioSegmentStream.on('timingInfo',
+                                   pipeline.trigger.bind(pipeline, 'audioTimingInfo'));
+
+    // Set up the final part of the audio pipeline
+    pipeline.adtsStream
+      .pipe(pipeline.audioSegmentStream);
+
+    pipeline.trigger('trackinfo', {
+      hasAudio: !!pipeline.tracks.audio,
+      hasVideo: !!pipeline.tracks.video
+    });
+  });
 
-		},
+  // set the pipeline up as a stream before binding to get access to the trigger function
+  pipeline = createPipeline(pipeline);
 
-		addScaledVector: function ( v, s ) {
+  pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
 
-			this.x += v.x * s;
-			this.y += v.y * s;
+  return pipeline;
+};
 
-			return this;
+var setupPipelineListeners = function(pipeline, transmuxer) {
+  pipeline.on('data', transmuxer.trigger.bind(transmuxer, 'data'));
+  pipeline.on('done', transmuxer.trigger.bind(transmuxer, 'done'));
+  pipeline.on('partialdone', transmuxer.trigger.bind(transmuxer, 'partialdone'));
+  pipeline.on('endedtimeline', transmuxer.trigger.bind(transmuxer, 'endedtimeline'));
+  pipeline.on('audioTimingInfo', transmuxer.trigger.bind(transmuxer, 'audioTimingInfo'));
+  pipeline.on('videoTimingInfo', transmuxer.trigger.bind(transmuxer, 'videoTimingInfo'));
+  pipeline.on('trackinfo', transmuxer.trigger.bind(transmuxer, 'trackinfo'));
+  pipeline.on('id3Frame', function(event) {
+    // add this to every single emitted segment even though it's only needed for the first
+    event.dispatchType = pipeline.metadataStream.dispatchType;
+    // keep original time, can be adjusted if needed at a higher level
+    event.cueTime = clock.videoTsToSeconds(event.pts);
+
+    transmuxer.trigger('id3Frame', event);
+  });
+  pipeline.on('caption', function(event) {
+    transmuxer.trigger('caption', event);
+  });
+};
 
-		},
+var Transmuxer = function(options) {
+  var
+    pipeline = null,
+    hasFlushed = true;
 
-		sub: function ( v, w ) {
+  options = options || {};
 
-			if ( w !== undefined ) {
+  Transmuxer.prototype.init.call(this);
+  options.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
 
-				console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
-				return this.subVectors( v, w );
+  this.push = function(bytes) {
+    if (hasFlushed) {
+      var isAac = isLikelyAacData(bytes);
 
-			}
+      if (isAac && (!pipeline || pipeline.type !== 'aac')) {
+        pipeline = aacPipeline(options);
+        setupPipelineListeners(pipeline, this);
+      } else if (!isAac && (!pipeline || pipeline.type !== 'ts')) {
+        pipeline = tsPipeline(options);
+        setupPipelineListeners(pipeline, this);
+      }
+      hasFlushed = false;
+    }
 
-			this.x -= v.x;
-			this.y -= v.y;
+    pipeline.headOfPipeline.push(bytes);
+  };
 
-			return this;
+  this.flush = function() {
+    if (!pipeline) {
+      return;
+    }
 
-		},
+    hasFlushed = true;
+    pipeline.headOfPipeline.flush();
+  };
 
-		subScalar: function ( s ) {
+  this.partialFlush = function() {
+    if (!pipeline) {
+      return;
+    }
 
-			this.x -= s;
-			this.y -= s;
+    pipeline.headOfPipeline.partialFlush();
+  };
 
-			return this;
+  this.endTimeline = function() {
+    if (!pipeline) {
+      return;
+    }
 
-		},
+    pipeline.headOfPipeline.endTimeline();
+  };
 
-		subVectors: function ( a, b ) {
+  this.reset = function() {
+    if (!pipeline) {
+      return;
+    }
 
-			this.x = a.x - b.x;
-			this.y = a.y - b.y;
+    pipeline.headOfPipeline.reset();
+  };
 
-			return this;
+  this.setBaseMediaDecodeTime = function(baseMediaDecodeTime) {
+    if (!options.keepOriginalTimestamps) {
+      options.baseMediaDecodeTime = baseMediaDecodeTime;
+    }
 
-		},
+    if (!pipeline) {
+      return;
+    }
 
-		multiply: function ( v ) {
+    if (pipeline.tracks.audio) {
+      pipeline.tracks.audio.timelineStartInfo.dts = undefined;
+      pipeline.tracks.audio.timelineStartInfo.pts = undefined;
+      trackInfo.clearDtsInfo(pipeline.tracks.audio);
+      if (pipeline.audioRollover) {
+        pipeline.audioRollover.discontinuity();
+      }
+    }
+    if (pipeline.tracks.video) {
+      if (pipeline.videoSegmentStream) {
+        pipeline.videoSegmentStream.gopCache_ = [];
+      }
+      pipeline.tracks.video.timelineStartInfo.dts = undefined;
+      pipeline.tracks.video.timelineStartInfo.pts = undefined;
+      trackInfo.clearDtsInfo(pipeline.tracks.video);
+      // pipeline.captionStream.reset();
+    }
 
-			this.x *= v.x;
-			this.y *= v.y;
+    if (pipeline.timestampRollover) {
+      pipeline.timestampRollover.discontinuity();
 
-			return this;
+    }
+  };
 
-		},
+  this.setRemux = function(val) {
+    options.remux = val;
 
-		multiplyScalar: function ( scalar ) {
+    if (pipeline && pipeline.coalesceStream) {
+      pipeline.coalesceStream.setRemux(val);
+    }
+  };
 
-			this.x *= scalar;
-			this.y *= scalar;
 
-			return this;
+  this.setAudioAppendStart = function(audioAppendStart) {
+    if (!pipeline || !pipeline.tracks.audio || !pipeline.audioSegmentStream) {
+      return;
+    }
 
-		},
+    pipeline.audioSegmentStream.setAudioAppendStart(audioAppendStart);
+  };
 
-		divide: function ( v ) {
+  // TODO GOP alignment support
+  // Support may be a bit trickier than with full segment appends, as GOPs may be split
+  // and processed in a more granular fashion
+  this.alignGopsWith = function(gopsToAlignWith) {
+    return;
+  };
+};
 
-			this.x /= v.x;
-			this.y /= v.y;
+Transmuxer.prototype = new Stream();
 
-			return this;
+module.exports = Transmuxer;
 
-		},
+},{"../aac/index":19,"../aac/utils":20,"../codecs/adts":21,"../codecs/index.js":23,"../m2ts/m2ts.js":36,"../mp4/track-decode-info.js":47,"../utils/clock":58,"../utils/stream.js":60,"./audio-segment-stream.js":49,"./video-segment-stream.js":52}],52:[function(require,module,exports){
+/**
+ * Constructs a single-track, ISO BMFF media segment from H264 data
+ * events. The output of this stream can be fed to a SourceBuffer
+ * configured with a suitable initialization segment.
+ * @param track {object} track metadata configuration
+ * @param options {object} transmuxer options object
+ * @param options.alignGopsAtEnd {boolean} If true, start from the end of the
+ *        gopsToAlignWith list when attempting to align gop pts
+ */
+'use strict';
 
-		divideScalar: function ( scalar ) {
+var Stream = require('../utils/stream.js');
+var mp4 = require('../mp4/mp4-generator.js');
+var trackInfo = require('../mp4/track-decode-info.js');
+var frameUtils = require('../mp4/frame-utils');
+var VIDEO_PROPERTIES = require('../constants/video-properties.js');
+
+var VideoSegmentStream = function(track, options) {
+  var
+    sequenceNumber = 0,
+    nalUnits = [],
+    frameCache = [],
+    // gopsToAlignWith = [],
+    config,
+    pps,
+    segmentStartPts = null,
+    segmentEndPts = null,
+    gops,
+    ensureNextFrameIsKeyFrame = true;
 
-			return this.multiplyScalar( 1 / scalar );
+  options = options || {};
 
-		},
+  VideoSegmentStream.prototype.init.call(this);
 
-		applyMatrix3: function ( m ) {
+  this.push = function(nalUnit) {
+    trackInfo.collectDtsInfo(track, nalUnit);
+    if (typeof track.timelineStartInfo.dts === 'undefined') {
+      track.timelineStartInfo.dts = nalUnit.dts;
+    }
 
-			var x = this.x, y = this.y;
-			var e = m.elements;
+    // record the track config
+    if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
+      config = nalUnit.config;
+      track.sps = [nalUnit.data];
 
-			this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
-			this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
+      VIDEO_PROPERTIES.forEach(function(prop) {
+        track[prop] = config[prop];
+      }, this);
+    }
 
-			return this;
+    if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' &&
+        !pps) {
+      pps = nalUnit.data;
+      track.pps = [nalUnit.data];
+    }
 
-		},
+    // buffer video until flush() is called
+    nalUnits.push(nalUnit);
+  };
 
-		min: function ( v ) {
+  this.processNals_ = function(cacheLastFrame) {
+    var i;
 
-			this.x = Math.min( this.x, v.x );
-			this.y = Math.min( this.y, v.y );
+    nalUnits = frameCache.concat(nalUnits);
 
-			return this;
+    // Throw away nalUnits at the start of the byte stream until
+    // we find the first AUD
+    while (nalUnits.length) {
+      if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
+        break;
+      }
+      nalUnits.shift();
+    }
 
-		},
+    // Return early if no video data has been observed
+    if (nalUnits.length === 0) {
+      return;
+    }
 
-		max: function ( v ) {
+    var frames = frameUtils.groupNalsIntoFrames(nalUnits);
 
-			this.x = Math.max( this.x, v.x );
-			this.y = Math.max( this.y, v.y );
+    if (!frames.length) {
+      return;
+    }
 
-			return this;
+    // note that the frame cache may also protect us from cases where we haven't
+    // pushed data for the entire first or last frame yet
+    frameCache = frames[frames.length - 1];
 
-		},
+    if (cacheLastFrame) {
+      frames.pop();
+      frames.duration -= frameCache.duration;
+      frames.nalCount -= frameCache.length;
+      frames.byteLength -= frameCache.byteLength;
+    }
 
-		clamp: function ( min, max ) {
+    if (!frames.length) {
+      nalUnits = [];
+      return;
+    }
 
-			// assumes min < max, componentwise
+    this.trigger('timelineStartInfo', track.timelineStartInfo);
 
-			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
-			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
+    if (ensureNextFrameIsKeyFrame) {
+      gops = frameUtils.groupFramesIntoGops(frames);
 
-			return this;
+      if (!gops[0][0].keyFrame) {
+        gops = frameUtils.extendFirstKeyFrame(gops);
 
-		},
+        if (!gops[0][0].keyFrame) {
+          // we haven't yet gotten a key frame, so reset nal units to wait for more nal
+          // units
+          nalUnits = ([].concat.apply([], frames)).concat(frameCache);
+          frameCache = [];
+          return;
+        }
 
-		clampScalar: function ( minVal, maxVal ) {
+        frames = [].concat.apply([], gops);
+        frames.duration = gops.duration;
+      }
+      ensureNextFrameIsKeyFrame = false;
+    }
 
-			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
-			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
+    if (segmentStartPts === null) {
+      segmentStartPts = frames[0].pts;
+      segmentEndPts = segmentStartPts;
+    }
 
-			return this;
+    segmentEndPts += frames.duration;
 
-		},
+    this.trigger('timingInfo', {
+      start: segmentStartPts,
+      end: segmentEndPts
+    });
 
-		clampLength: function ( min, max ) {
+    for (i = 0; i < frames.length; i++) {
+      var frame = frames[i];
 
-			var length = this.length();
+      track.samples = frameUtils.generateSampleTableForFrame(frame);
 
-			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
+      var mdat = mp4.mdat(frameUtils.concatenateNalDataForFrame(frame));
 
-		},
+      trackInfo.clearDtsInfo(track);
+      trackInfo.collectDtsInfo(track, frame);
 
-		floor: function () {
+      track.baseMediaDecodeTime = trackInfo.calculateTrackBaseMediaDecodeTime(
+        track, options.keepOriginalTimestamps);
 
-			this.x = Math.floor( this.x );
-			this.y = Math.floor( this.y );
+      var moof = mp4.moof(sequenceNumber, [track]);
 
-			return this;
+      sequenceNumber++;
 
-		},
+      track.initSegment = mp4.initSegment([track]);
 
-		ceil: function () {
+      var boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
 
-			this.x = Math.ceil( this.x );
-			this.y = Math.ceil( this.y );
+      boxes.set(moof);
+      boxes.set(mdat, moof.byteLength);
 
-			return this;
+      this.trigger('data', {
+        track: track,
+        boxes: boxes,
+        sequence: sequenceNumber,
+        videoFrameDts: frame.dts,
+        videoFramePts: frame.pts
+      });
+    }
 
-		},
+    nalUnits = [];
+  };
 
-		round: function () {
+  this.resetTimingAndConfig_ = function() {
+    config = undefined;
+    pps = undefined;
+    segmentStartPts = null;
+    segmentEndPts = null;
+  };
 
-			this.x = Math.round( this.x );
-			this.y = Math.round( this.y );
+  this.partialFlush = function() {
+    this.processNals_(true);
+    this.trigger('partialdone', 'VideoSegmentStream');
+  };
 
-			return this;
+  this.flush = function() {
+    this.processNals_(false);
+    // reset config and pps because they may differ across segments
+    // for instance, when we are rendition switching
+    this.resetTimingAndConfig_();
+    this.trigger('done', 'VideoSegmentStream');
+  };
 
-		},
+  this.endTimeline = function() {
+    this.flush();
+    this.trigger('endedtimeline', 'VideoSegmentStream');
+  };
 
-		roundToZero: function () {
+  this.reset = function() {
+    this.resetTimingAndConfig_();
+    frameCache = [];
+    nalUnits = [];
+    ensureNextFrameIsKeyFrame = true;
+    this.trigger('reset');
+  };
+};
 
-			this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
-			this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
+VideoSegmentStream.prototype = new Stream();
 
-			return this;
+module.exports = VideoSegmentStream;
 
-		},
+},{"../constants/video-properties.js":25,"../mp4/frame-utils":43,"../mp4/mp4-generator.js":45,"../mp4/track-decode-info.js":47,"../utils/stream.js":60}],53:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Reads in-band caption information from a video elementary
+ * stream. Captions must follow the CEA-708 standard for injection
+ * into an MPEG-2 transport streams.
+ * @see https://en.wikipedia.org/wiki/CEA-708
+ * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
+ */
 
-		negate: function () {
+'use strict';
 
-			this.x = - this.x;
-			this.y = - this.y;
+// Supplemental enhancement information (SEI) NAL units have a
+// payload type field to indicate how they are to be
+// interpreted. CEAS-708 caption content is always transmitted with
+// payload type 0x04.
+var USER_DATA_REGISTERED_ITU_T_T35 = 4,
+    RBSP_TRAILING_BITS = 128;
 
-			return this;
+/**
+  * Parse a supplemental enhancement information (SEI) NAL unit.
+  * Stops parsing once a message of type ITU T T35 has been found.
+  *
+  * @param bytes {Uint8Array} the bytes of a SEI NAL unit
+  * @return {object} the parsed SEI payload
+  * @see Rec. ITU-T H.264, 7.3.2.3.1
+  */
+var parseSei = function(bytes) {
+  var
+    i = 0,
+    result = {
+      payloadType: -1,
+      payloadSize: 0
+    },
+    payloadType = 0,
+    payloadSize = 0;
 
-		},
+  // go through the sei_rbsp parsing each each individual sei_message
+  while (i < bytes.byteLength) {
+    // stop once we have hit the end of the sei_rbsp
+    if (bytes[i] === RBSP_TRAILING_BITS) {
+      break;
+    }
 
-		dot: function ( v ) {
+    // Parse payload type
+    while (bytes[i] === 0xFF) {
+      payloadType += 255;
+      i++;
+    }
+    payloadType += bytes[i++];
 
-			return this.x * v.x + this.y * v.y;
+    // Parse payload size
+    while (bytes[i] === 0xFF) {
+      payloadSize += 255;
+      i++;
+    }
+    payloadSize += bytes[i++];
+
+    // this sei_message is a 608/708 caption so save it and break
+    // there can only ever be one caption message in a frame's sei
+    if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
+      var userIdentifier = String.fromCharCode(
+        bytes[i + 3],
+        bytes[i + 4],
+        bytes[i + 5],
+        bytes[i + 6]);
+
+      if (userIdentifier === 'GA94') {
+        result.payloadType = payloadType;
+        result.payloadSize = payloadSize;
+        result.payload = bytes.subarray(i, i + payloadSize);
+        break;
+      } else {
+        result.payload = void 0;
+      }
+    }
 
-		},
+    // skip the payload and parse the next message
+    i += payloadSize;
+    payloadType = 0;
+    payloadSize = 0;
+  }
 
-		cross: function ( v ) {
+  return result;
+};
 
-			return this.x * v.y - this.y * v.x;
+// see ANSI/SCTE 128-1 (2013), section 8.1
+var parseUserData = function(sei) {
+  // itu_t_t35_contry_code must be 181 (United States) for
+  // captions
+  if (sei.payload[0] !== 181) {
+    return null;
+  }
 
-		},
+  // itu_t_t35_provider_code should be 49 (ATSC) for captions
+  if (((sei.payload[1] << 8) | sei.payload[2]) !== 49) {
+    return null;
+  }
 
-		lengthSq: function () {
+  // the user_identifier should be "GA94" to indicate ATSC1 data
+  if (String.fromCharCode(sei.payload[3],
+                          sei.payload[4],
+                          sei.payload[5],
+                          sei.payload[6]) !== 'GA94') {
+    return null;
+  }
 
-			return this.x * this.x + this.y * this.y;
+  // finally, user_data_type_code should be 0x03 for caption data
+  if (sei.payload[7] !== 0x03) {
+    return null;
+  }
 
-		},
+  // return the user_data_type_structure and strip the trailing
+  // marker bits
+  return sei.payload.subarray(8, sei.payload.length - 1);
+};
 
-		length: function () {
+// see CEA-708-D, section 4.4
+var parseCaptionPackets = function(pts, userData) {
+  var results = [], i, count, offset, data;
 
-			return Math.sqrt( this.x * this.x + this.y * this.y );
+  // if this is just filler, return immediately
+  if (!(userData[0] & 0x40)) {
+    return results;
+  }
 
-		},
+  // parse out the cc_data_1 and cc_data_2 fields
+  count = userData[0] & 0x1f;
+  for (i = 0; i < count; i++) {
+    offset = i * 3;
+    data = {
+      type: userData[offset + 2] & 0x03,
+      pts: pts
+    };
 
-		manhattanLength: function () {
+    // capture cc data when cc_valid is 1
+    if (userData[offset + 2] & 0x04) {
+      data.ccData = (userData[offset + 3] << 8) | userData[offset + 4];
+      results.push(data);
+    }
+  }
+  return results;
+};
 
-			return Math.abs( this.x ) + Math.abs( this.y );
+var discardEmulationPreventionBytes = function(data) {
+    var
+      length = data.byteLength,
+      emulationPreventionBytesPositions = [],
+      i = 1,
+      newLength, newData;
+
+    // Find all `Emulation Prevention Bytes`
+    while (i < length - 2) {
+      if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
+        emulationPreventionBytesPositions.push(i + 2);
+        i += 2;
+      } else {
+        i++;
+      }
+    }
 
-		},
+    // If no Emulation Prevention Bytes were found just return the original
+    // array
+    if (emulationPreventionBytesPositions.length === 0) {
+      return data;
+    }
 
-		normalize: function () {
+    // Create a new array to hold the NAL unit data
+    newLength = length - emulationPreventionBytesPositions.length;
+    newData = new Uint8Array(newLength);
+    var sourceIndex = 0;
+
+    for (i = 0; i < newLength; sourceIndex++, i++) {
+      if (sourceIndex === emulationPreventionBytesPositions[0]) {
+        // Skip this byte
+        sourceIndex++;
+        // Remove this position index
+        emulationPreventionBytesPositions.shift();
+      }
+      newData[i] = data[sourceIndex];
+    }
 
-			return this.divideScalar( this.length() || 1 );
+    return newData;
+};
 
-		},
+// exports
+module.exports = {
+  parseSei: parseSei,
+  parseUserData: parseUserData,
+  parseCaptionPackets: parseCaptionPackets,
+  discardEmulationPreventionBytes: discardEmulationPreventionBytes,
+  USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
+};
 
-		angle: function () {
+},{}],54:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
 
-			// computes the angle in radians with respect to the positive x-axis
+var
+  tagTypes = {
+    0x08: 'audio',
+    0x09: 'video',
+    0x12: 'metadata'
+  },
+  hex = function(val) {
+    return '0x' + ('00' + val.toString(16)).slice(-2).toUpperCase();
+  },
+  hexStringList = function(data) {
+    var arr = [], i;
 
-			var angle = Math.atan2( - this.y, - this.x ) + Math.PI;
+    while (data.byteLength > 0) {
+      i = 0;
+      arr.push(hex(data[i++]));
+      data = data.subarray(i);
+    }
+    return arr.join(' ');
+  },
+  parseAVCTag = function(tag, obj) {
+    var
+      avcPacketTypes = [
+        'AVC Sequence Header',
+        'AVC NALU',
+        'AVC End-of-Sequence'
+      ],
+      compositionTime = (tag[1] & parseInt('01111111', 2) << 16) | (tag[2] << 8) | tag[3];
+
+    obj = obj || {};
+
+    obj.avcPacketType = avcPacketTypes[tag[0]];
+    obj.CompositionTime = (tag[1] & parseInt('10000000', 2)) ? -compositionTime : compositionTime;
+
+    if (tag[0] === 1) {
+      obj.nalUnitTypeRaw = hexStringList(tag.subarray(4, 100));
+    } else {
+      obj.data = hexStringList(tag.subarray(4));
+    }
 
-			return angle;
+    return obj;
+  },
+  parseVideoTag = function(tag, obj) {
+    var
+      frameTypes = [
+        'Unknown',
+        'Keyframe (for AVC, a seekable frame)',
+        'Inter frame (for AVC, a nonseekable frame)',
+        'Disposable inter frame (H.263 only)',
+        'Generated keyframe (reserved for server use only)',
+        'Video info/command frame'
+      ],
+      codecID = tag[0] & parseInt('00001111', 2);
+
+    obj = obj || {};
+
+    obj.frameType = frameTypes[(tag[0] & parseInt('11110000', 2)) >>> 4];
+    obj.codecID = codecID;
+
+    if (codecID === 7) {
+      return parseAVCTag(tag.subarray(1), obj);
+    }
+    return obj;
+  },
+  parseAACTag = function(tag, obj) {
+    var packetTypes = [
+      'AAC Sequence Header',
+      'AAC Raw'
+    ];
 
-		},
+    obj = obj || {};
 
-		distanceTo: function ( v ) {
+    obj.aacPacketType = packetTypes[tag[0]];
+    obj.data = hexStringList(tag.subarray(1));
 
-			return Math.sqrt( this.distanceToSquared( v ) );
+    return obj;
+  },
+  parseAudioTag = function(tag, obj) {
+    var
+      formatTable = [
+        'Linear PCM, platform endian',
+        'ADPCM',
+        'MP3',
+        'Linear PCM, little endian',
+        'Nellymoser 16-kHz mono',
+        'Nellymoser 8-kHz mono',
+        'Nellymoser',
+        'G.711 A-law logarithmic PCM',
+        'G.711 mu-law logarithmic PCM',
+        'reserved',
+        'AAC',
+        'Speex',
+        'MP3 8-Khz',
+        'Device-specific sound'
+      ],
+      samplingRateTable = [
+        '5.5-kHz',
+        '11-kHz',
+        '22-kHz',
+        '44-kHz'
+      ],
+      soundFormat = (tag[0] & parseInt('11110000', 2)) >>> 4;
+
+    obj = obj || {};
+
+    obj.soundFormat = formatTable[soundFormat];
+    obj.soundRate = samplingRateTable[(tag[0] & parseInt('00001100', 2)) >>> 2];
+    obj.soundSize = ((tag[0] & parseInt('00000010', 2)) >>> 1) ? '16-bit' : '8-bit';
+    obj.soundType = (tag[0] & parseInt('00000001', 2)) ? 'Stereo' : 'Mono';
+
+    if (soundFormat === 10) {
+      return parseAACTag(tag.subarray(1), obj);
+    }
+    return obj;
+  },
+  parseGenericTag = function(tag) {
+    return {
+      tagType: tagTypes[tag[0]],
+      dataSize: (tag[1] << 16) | (tag[2] << 8) | tag[3],
+      timestamp: (tag[7] << 24) | (tag[4] << 16) | (tag[5] << 8) | tag[6],
+      streamID: (tag[8] << 16) | (tag[9] << 8) | tag[10]
+    };
+  },
+  inspectFlvTag = function(tag) {
+    var header = parseGenericTag(tag);
+    switch (tag[0]) {
+      case 0x08:
+        parseAudioTag(tag.subarray(11), header);
+        break;
+      case 0x09:
+        parseVideoTag(tag.subarray(11), header);
+        break;
+      case 0x12:
+    }
+    return header;
+  },
+  inspectFlv = function(bytes) {
+    var i = 9, // header
+        dataSize,
+        parsedResults = [],
+        tag;
+
+    // traverse the tags
+    i += 4; // skip previous tag size
+    while (i < bytes.byteLength) {
+      dataSize = bytes[i + 1] << 16;
+      dataSize |= bytes[i + 2] << 8;
+      dataSize |= bytes[i + 3];
+      dataSize += 11;
+
+      tag = bytes.subarray(i, i + dataSize);
+      parsedResults.push(inspectFlvTag(tag));
+      i += dataSize + 4;
+    }
+    return parsedResults;
+  },
+  textifyFlv = function(flvTagArray) {
+    return JSON.stringify(flvTagArray, null, 2);
+  };
 
-		},
+module.exports = {
+  inspectTag: inspectFlvTag,
+  inspect: inspectFlv,
+  textify: textifyFlv
+};
 
-		distanceToSquared: function ( v ) {
+},{}],55:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Parse the internal MP4 structure into an equivalent javascript
+ * object.
+ */
+'use strict';
 
-			var dx = this.x - v.x, dy = this.y - v.y;
-			return dx * dx + dy * dy;
+var
+  inspectMp4,
+  textifyMp4,
+  toUnsigned = require('../utils/bin').toUnsigned,
+  parseMp4Date = function(seconds) {
+    return new Date(seconds * 1000 - 2082844800000);
+  },
+  parseSampleFlags = function(flags) {
+    return {
+      isLeading: (flags[0] & 0x0c) >>> 2,
+      dependsOn: flags[0] & 0x03,
+      isDependedOn: (flags[1] & 0xc0) >>> 6,
+      hasRedundancy: (flags[1] & 0x30) >>> 4,
+      paddingValue: (flags[1] & 0x0e) >>> 1,
+      isNonSyncSample: flags[1] & 0x01,
+      degradationPriority: (flags[2] << 8) | flags[3]
+    };
+  },
+  /**
+   * Returns the string representation of an ASCII encoded four byte buffer.
+   * @param buffer {Uint8Array} a four-byte buffer to translate
+   * @return {string} the corresponding string
+   */
+  parseType = function(buffer) {
+    var result = '';
+    result += String.fromCharCode(buffer[0]);
+    result += String.fromCharCode(buffer[1]);
+    result += String.fromCharCode(buffer[2]);
+    result += String.fromCharCode(buffer[3]);
+    return result;
+  },
+  // Find the data for a box specified by its path
+  findBox = function(data, path) {
+    var results = [],
+        i, size, type, end, subresults;
+
+    if (!path.length) {
+      // short-circuit the search for empty paths
+      return null;
+    }
 
-		},
+    for (i = 0; i < data.byteLength;) {
+      size  = toUnsigned(data[i]     << 24 |
+                         data[i + 1] << 16 |
+                         data[i + 2] <<  8 |
+                         data[i + 3]);
 
-		manhattanDistanceTo: function ( v ) {
+      type = parseType(data.subarray(i + 4, i + 8));
 
-			return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
+      end = size > 1 ? i + size : data.byteLength;
 
-		},
+      if (type === path[0]) {
+        if (path.length === 1) {
+          // this is the end of the path and we've found the box we were
+          // looking for
+          results.push(data.subarray(i + 8, end));
+        } else {
+          // recursively search for the next box along the path
+          subresults = findBox(data.subarray(i + 8, end), path.slice(1));
+          if (subresults.length) {
+            results = results.concat(subresults);
+          }
+        }
+      }
+      i = end;
+    }
 
-		setLength: function ( length ) {
+    // we've finished searching all of data
+    return results;
+  },
+  nalParse = function(avcStream) {
+    var
+      avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
+      result = [],
+      i,
+      length;
+    for (i = 0; i + 4 < avcStream.length; i += length) {
+      length = avcView.getUint32(i);
+      i += 4;
+
+      // bail if this doesn't appear to be an H264 stream
+      if (length <= 0) {
+        result.push('<span style=\'color:red;\'>MALFORMED DATA</span>');
+        continue;
+      }
 
-			return this.normalize().multiplyScalar( length );
+      switch (avcStream[i] & 0x1F) {
+      case 0x01:
+        result.push('slice_layer_without_partitioning_rbsp');
+        break;
+      case 0x05:
+        result.push('slice_layer_without_partitioning_rbsp_idr');
+        break;
+      case 0x06:
+        result.push('sei_rbsp');
+        break;
+      case 0x07:
+        result.push('seq_parameter_set_rbsp');
+        break;
+      case 0x08:
+        result.push('pic_parameter_set_rbsp');
+        break;
+      case 0x09:
+        result.push('access_unit_delimiter_rbsp');
+        break;
+      default:
+        result.push('UNKNOWN NAL - ' + avcStream[i] & 0x1F);
+        break;
+      }
+    }
+    return result;
+  },
 
-		},
+  // registry of handlers for individual mp4 box types
+  parse = {
+    // codingname, not a first-class box type. stsd entries share the
+    // same format as real boxes so the parsing infrastructure can be
+    // shared
+    avc1: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
+      return {
+        dataReferenceIndex: view.getUint16(6),
+        width: view.getUint16(24),
+        height: view.getUint16(26),
+        horizresolution: view.getUint16(28) + (view.getUint16(30) / 16),
+        vertresolution: view.getUint16(32) + (view.getUint16(34) / 16),
+        frameCount: view.getUint16(40),
+        depth: view.getUint16(74),
+        config: inspectMp4(data.subarray(78, data.byteLength))
+      };
+    },
+    avcC: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          configurationVersion: data[0],
+          avcProfileIndication: data[1],
+          profileCompatibility: data[2],
+          avcLevelIndication: data[3],
+          lengthSizeMinusOne: data[4] & 0x03,
+          sps: [],
+          pps: []
+        },
+        numOfSequenceParameterSets = data[5] & 0x1f,
+        numOfPictureParameterSets,
+        nalSize,
+        offset,
+        i;
+
+      // iterate past any SPSs
+      offset = 6;
+      for (i = 0; i < numOfSequenceParameterSets; i++) {
+        nalSize = view.getUint16(offset);
+        offset += 2;
+        result.sps.push(new Uint8Array(data.subarray(offset, offset + nalSize)));
+        offset += nalSize;
+      }
+      // iterate past any PPSs
+      numOfPictureParameterSets = data[offset];
+      offset++;
+      for (i = 0; i < numOfPictureParameterSets; i++) {
+        nalSize = view.getUint16(offset);
+        offset += 2;
+        result.pps.push(new Uint8Array(data.subarray(offset, offset + nalSize)));
+        offset += nalSize;
+      }
+      return result;
+    },
+    btrt: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
+      return {
+        bufferSizeDB: view.getUint32(0),
+        maxBitrate: view.getUint32(4),
+        avgBitrate: view.getUint32(8)
+      };
+    },
+    esds: function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        esId: (data[6] << 8) | data[7],
+        streamPriority: data[8] & 0x1f,
+        decoderConfig: {
+          objectProfileIndication: data[11],
+          streamType: (data[12] >>> 2) & 0x3f,
+          bufferSize: (data[13] << 16) | (data[14] << 8) | data[15],
+          maxBitrate: (data[16] << 24) |
+            (data[17] << 16) |
+            (data[18] <<  8) |
+            data[19],
+          avgBitrate: (data[20] << 24) |
+            (data[21] << 16) |
+            (data[22] <<  8) |
+            data[23],
+          decoderConfigDescriptor: {
+            tag: data[24],
+            length: data[25],
+            audioObjectType: (data[26] >>> 3) & 0x1f,
+            samplingFrequencyIndex: ((data[26] & 0x07) << 1) |
+              ((data[27] >>> 7) & 0x01),
+            channelConfiguration: (data[27] >>> 3) & 0x0f
+          }
+        }
+      };
+    },
+    ftyp: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          majorBrand: parseType(data.subarray(0, 4)),
+          minorVersion: view.getUint32(4),
+          compatibleBrands: []
+        },
+        i = 8;
+      while (i < data.byteLength) {
+        result.compatibleBrands.push(parseType(data.subarray(i, i + 4)));
+        i += 4;
+      }
+      return result;
+    },
+    dinf: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    dref: function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        dataReferences: inspectMp4(data.subarray(8))
+      };
+    },
+    hdlr: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          version: view.getUint8(0),
+          flags: new Uint8Array(data.subarray(1, 4)),
+          handlerType: parseType(data.subarray(8, 12)),
+          name: ''
+        },
+        i = 8;
+
+      // parse out the name field
+      for (i = 24; i < data.byteLength; i++) {
+        if (data[i] === 0x00) {
+          // the name field is null-terminated
+          i++;
+          break;
+        }
+        result.name += String.fromCharCode(data[i]);
+      }
+      // decode UTF-8 to javascript's internal representation
+      // see http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
+      result.name = decodeURIComponent(escape(result.name));
 
-		lerp: function ( v, alpha ) {
+      return result;
+    },
+    mdat: function(data) {
+      return {
+        byteLength: data.byteLength,
+        nals: nalParse(data)
+      };
+    },
+    mdhd: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        i = 4,
+        language,
+        result = {
+          version: view.getUint8(0),
+          flags: new Uint8Array(data.subarray(1, 4)),
+          language: ''
+        };
+      if (result.version === 1) {
+        i += 4;
+        result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 8;
+        result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 4;
+        result.timescale = view.getUint32(i);
+        i += 8;
+        result.duration = view.getUint32(i); // truncating top 4 bytes
+      } else {
+        result.creationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.modificationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.timescale = view.getUint32(i);
+        i += 4;
+        result.duration = view.getUint32(i);
+      }
+      i += 4;
+      // language is stored as an ISO-639-2/T code in an array of three 5-bit fields
+      // each field is the packed difference between its ASCII value and 0x60
+      language = view.getUint16(i);
+      result.language += String.fromCharCode((language >> 10) + 0x60);
+      result.language += String.fromCharCode(((language & 0x03e0) >> 5) + 0x60);
+      result.language += String.fromCharCode((language & 0x1f) + 0x60);
+
+      return result;
+    },
+    mdia: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    mfhd: function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        sequenceNumber: (data[4] << 24) |
+          (data[5] << 16) |
+          (data[6] << 8) |
+          (data[7])
+      };
+    },
+    minf: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    // codingname, not a first-class box type. stsd entries share the
+    // same format as real boxes so the parsing infrastructure can be
+    // shared
+    mp4a: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          // 6 bytes reserved
+          dataReferenceIndex: view.getUint16(6),
+          // 4 + 4 bytes reserved
+          channelcount: view.getUint16(16),
+          samplesize: view.getUint16(18),
+          // 2 bytes pre_defined
+          // 2 bytes reserved
+          samplerate: view.getUint16(24) + (view.getUint16(26) / 65536)
+        };
 
-			this.x += ( v.x - this.x ) * alpha;
-			this.y += ( v.y - this.y ) * alpha;
+      // if there are more bytes to process, assume this is an ISO/IEC
+      // 14496-14 MP4AudioSampleEntry and parse the ESDBox
+      if (data.byteLength > 28) {
+        result.streamDescriptor = inspectMp4(data.subarray(28))[0];
+      }
+      return result;
+    },
+    moof: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    moov: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    mvex: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    mvhd: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        i = 4,
+        result = {
+          version: view.getUint8(0),
+          flags: new Uint8Array(data.subarray(1, 4))
+        };
 
-			return this;
+      if (result.version === 1) {
+        i += 4;
+        result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 8;
+        result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 4;
+        result.timescale = view.getUint32(i);
+        i += 8;
+        result.duration = view.getUint32(i); // truncating top 4 bytes
+      } else {
+        result.creationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.modificationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.timescale = view.getUint32(i);
+        i += 4;
+        result.duration = view.getUint32(i);
+      }
+      i += 4;
+
+      // convert fixed-point, base 16 back to a number
+      result.rate = view.getUint16(i) + (view.getUint16(i + 2) / 16);
+      i += 4;
+      result.volume = view.getUint8(i) + (view.getUint8(i + 1) / 8);
+      i += 2;
+      i += 2;
+      i += 2 * 4;
+      result.matrix = new Uint32Array(data.subarray(i, i + (9 * 4)));
+      i += 9 * 4;
+      i += 6 * 4;
+      result.nextTrackId = view.getUint32(i);
+      return result;
+    },
+    pdin: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
+      return {
+        version: view.getUint8(0),
+        flags: new Uint8Array(data.subarray(1, 4)),
+        rate: view.getUint32(4),
+        initialDelay: view.getUint32(8)
+      };
+    },
+    sdtp: function(data) {
+      var
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          samples: []
+        }, i;
+
+      for (i = 4; i < data.byteLength; i++) {
+        result.samples.push({
+          dependsOn: (data[i] & 0x30) >> 4,
+          isDependedOn: (data[i] & 0x0c) >> 2,
+          hasRedundancy: data[i] & 0x03
+        });
+      }
+      return result;
+    },
+    sidx: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+          result = {
+            version: data[0],
+            flags: new Uint8Array(data.subarray(1, 4)),
+            references: [],
+            referenceId: view.getUint32(4),
+            timescale: view.getUint32(8),
+            earliestPresentationTime: view.getUint32(12),
+            firstOffset: view.getUint32(16)
+          },
+          referenceCount = view.getUint16(22),
+          i;
+
+      for (i = 24; referenceCount; i += 12, referenceCount--) {
+        result.references.push({
+          referenceType: (data[i] & 0x80) >>> 7,
+          referencedSize: view.getUint32(i) & 0x7FFFFFFF,
+          subsegmentDuration: view.getUint32(i + 4),
+          startsWithSap: !!(data[i + 8] & 0x80),
+          sapType: (data[i + 8] & 0x70) >>> 4,
+          sapDeltaTime: view.getUint32(i + 8) & 0x0FFFFFFF
+        });
+      }
 
-		},
+      return result;
+    },
+    smhd: function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        balance: data[4] + (data[5] / 256)
+      };
+    },
+    stbl: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    stco: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          chunkOffsets: []
+        },
+        entryCount = view.getUint32(4),
+        i;
+      for (i = 8; entryCount; i += 4, entryCount--) {
+        result.chunkOffsets.push(view.getUint32(i));
+      }
+      return result;
+    },
+    stsc: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        entryCount = view.getUint32(4),
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          sampleToChunks: []
+        },
+        i;
+      for (i = 8; entryCount; i += 12, entryCount--) {
+        result.sampleToChunks.push({
+          firstChunk: view.getUint32(i),
+          samplesPerChunk: view.getUint32(i + 4),
+          sampleDescriptionIndex: view.getUint32(i + 8)
+        });
+      }
+      return result;
+    },
+    stsd: function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        sampleDescriptions: inspectMp4(data.subarray(8))
+      };
+    },
+    stsz: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          sampleSize: view.getUint32(4),
+          entries: []
+        },
+        i;
+      for (i = 12; i < data.byteLength; i += 4) {
+        result.entries.push(view.getUint32(i));
+      }
+      return result;
+    },
+    stts: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          timeToSamples: []
+        },
+        entryCount = view.getUint32(4),
+        i;
 
-		lerpVectors: function ( v1, v2, alpha ) {
+      for (i = 8; entryCount; i += 8, entryCount--) {
+        result.timeToSamples.push({
+          sampleCount: view.getUint32(i),
+          sampleDelta: view.getUint32(i + 4)
+        });
+      }
+      return result;
+    },
+    styp: function(data) {
+      return parse.ftyp(data);
+    },
+    tfdt: function(data) {
+      var result = {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        baseMediaDecodeTime: toUnsigned(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7])
+      };
+      if (result.version === 1) {
+        result.baseMediaDecodeTime *= Math.pow(2, 32);
+        result.baseMediaDecodeTime += toUnsigned(data[8] << 24 | data[9] << 16 | data[10] << 8 | data[11]);
+      }
+      return result;
+    },
+    tfhd: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          trackId: view.getUint32(4)
+        },
+        baseDataOffsetPresent = result.flags[2] & 0x01,
+        sampleDescriptionIndexPresent = result.flags[2] & 0x02,
+        defaultSampleDurationPresent = result.flags[2] & 0x08,
+        defaultSampleSizePresent = result.flags[2] & 0x10,
+        defaultSampleFlagsPresent = result.flags[2] & 0x20,
+        durationIsEmpty = result.flags[0] & 0x010000,
+        defaultBaseIsMoof =  result.flags[0] & 0x020000,
+        i;
+
+      i = 8;
+      if (baseDataOffsetPresent) {
+        i += 4; // truncate top 4 bytes
+        // FIXME: should we read the full 64 bits?
+        result.baseDataOffset = view.getUint32(12);
+        i += 4;
+      }
+      if (sampleDescriptionIndexPresent) {
+        result.sampleDescriptionIndex = view.getUint32(i);
+        i += 4;
+      }
+      if (defaultSampleDurationPresent) {
+        result.defaultSampleDuration = view.getUint32(i);
+        i += 4;
+      }
+      if (defaultSampleSizePresent) {
+        result.defaultSampleSize = view.getUint32(i);
+        i += 4;
+      }
+      if (defaultSampleFlagsPresent) {
+        result.defaultSampleFlags = view.getUint32(i);
+      }
+      if (durationIsEmpty) {
+        result.durationIsEmpty = true;
+      }
+      if (!baseDataOffsetPresent && defaultBaseIsMoof) {
+        result.baseDataOffsetIsMoof = true;
+      }
+      return result;
+    },
+    tkhd: function(data) {
+      var
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        i = 4,
+        result = {
+          version: view.getUint8(0),
+          flags: new Uint8Array(data.subarray(1, 4))
+        };
+      if (result.version === 1) {
+        i += 4;
+        result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 8;
+        result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
+        i += 4;
+        result.trackId = view.getUint32(i);
+        i += 4;
+        i += 8;
+        result.duration = view.getUint32(i); // truncating top 4 bytes
+      } else {
+        result.creationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.modificationTime = parseMp4Date(view.getUint32(i));
+        i += 4;
+        result.trackId = view.getUint32(i);
+        i += 4;
+        i += 4;
+        result.duration = view.getUint32(i);
+      }
+      i += 4;
+      i += 2 * 4;
+      result.layer = view.getUint16(i);
+      i += 2;
+      result.alternateGroup = view.getUint16(i);
+      i += 2;
+      // convert fixed-point, base 16 back to a number
+      result.volume = view.getUint8(i) + (view.getUint8(i + 1) / 8);
+      i += 2;
+      i += 2;
+      result.matrix = new Uint32Array(data.subarray(i, i + (9 * 4)));
+      i += 9 * 4;
+      result.width = view.getUint16(i) + (view.getUint16(i + 2) / 65536);
+      i += 4;
+      result.height = view.getUint16(i) + (view.getUint16(i + 2) / 65536);
+      return result;
+    },
+    traf: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    trak: function(data) {
+      return {
+        boxes: inspectMp4(data)
+      };
+    },
+    trex: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        trackId: view.getUint32(4),
+        defaultSampleDescriptionIndex: view.getUint32(8),
+        defaultSampleDuration: view.getUint32(12),
+        defaultSampleSize: view.getUint32(16),
+        sampleDependsOn: data[20] & 0x03,
+        sampleIsDependedOn: (data[21] & 0xc0) >> 6,
+        sampleHasRedundancy: (data[21] & 0x30) >> 4,
+        samplePaddingValue: (data[21] & 0x0e) >> 1,
+        sampleIsDifferenceSample: !!(data[21] & 0x01),
+        sampleDegradationPriority: view.getUint16(22)
+      };
+    },
+    trun: function(data) {
+      var
+        result = {
+          version: data[0],
+          flags: new Uint8Array(data.subarray(1, 4)),
+          samples: []
+        },
+        view = new DataView(data.buffer, data.byteOffset, data.byteLength),
+        // Flag interpretation
+        dataOffsetPresent = result.flags[2] & 0x01, // compare with 2nd byte of 0x1
+        firstSampleFlagsPresent = result.flags[2] & 0x04, // compare with 2nd byte of 0x4
+        sampleDurationPresent = result.flags[1] & 0x01, // compare with 2nd byte of 0x100
+        sampleSizePresent = result.flags[1] & 0x02, // compare with 2nd byte of 0x200
+        sampleFlagsPresent = result.flags[1] & 0x04, // compare with 2nd byte of 0x400
+        sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08, // compare with 2nd byte of 0x800
+        sampleCount = view.getUint32(4),
+        offset = 8,
+        sample;
+
+      if (dataOffsetPresent) {
+        // 32 bit signed integer
+        result.dataOffset = view.getInt32(offset);
+        offset += 4;
+      }
 
-			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
+      // Overrides the flags for the first sample only. The order of
+      // optional values will be: duration, size, compositionTimeOffset
+      if (firstSampleFlagsPresent && sampleCount) {
+        sample = {
+          flags: parseSampleFlags(data.subarray(offset, offset + 4))
+        };
+        offset += 4;
+        if (sampleDurationPresent) {
+          sample.duration = view.getUint32(offset);
+          offset += 4;
+        }
+        if (sampleSizePresent) {
+          sample.size = view.getUint32(offset);
+          offset += 4;
+        }
+        if (sampleCompositionTimeOffsetPresent) {
+          // Note: this should be a signed int if version is 1
+          sample.compositionTimeOffset = view.getUint32(offset);
+          offset += 4;
+        }
+        result.samples.push(sample);
+        sampleCount--;
+      }
 
-		},
+      while (sampleCount--) {
+        sample = {};
+        if (sampleDurationPresent) {
+          sample.duration = view.getUint32(offset);
+          offset += 4;
+        }
+        if (sampleSizePresent) {
+          sample.size = view.getUint32(offset);
+          offset += 4;
+        }
+        if (sampleFlagsPresent) {
+          sample.flags = parseSampleFlags(data.subarray(offset, offset + 4));
+          offset += 4;
+        }
+        if (sampleCompositionTimeOffsetPresent) {
+          // Note: this should be a signed int if version is 1
+          sample.compositionTimeOffset = view.getUint32(offset);
+          offset += 4;
+        }
+        result.samples.push(sample);
+      }
+      return result;
+    },
+    'url ': function(data) {
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4))
+      };
+    },
+    vmhd: function(data) {
+      var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
+      return {
+        version: data[0],
+        flags: new Uint8Array(data.subarray(1, 4)),
+        graphicsmode: view.getUint16(4),
+        opcolor: new Uint16Array([view.getUint16(6),
+                                  view.getUint16(8),
+                                  view.getUint16(10)])
+      };
+    }
+  };
 
-		equals: function ( v ) {
 
-			return ( ( v.x === this.x ) && ( v.y === this.y ) );
+/**
+ * Return a javascript array of box objects parsed from an ISO base
+ * media file.
+ * @param data {Uint8Array} the binary data of the media to be inspected
+ * @return {array} a javascript array of potentially nested box objects
+ */
+inspectMp4 = function(data) {
+  var
+    i = 0,
+    result = [],
+    view,
+    size,
+    type,
+    end,
+    box;
+
+  // Convert data from Uint8Array to ArrayBuffer, to follow Dataview API
+  var ab = new ArrayBuffer(data.length);
+  var v = new Uint8Array(ab);
+  for (var z = 0; z < data.length; ++z) {
+      v[z] = data[z];
+  }
+  view = new DataView(ab);
+
+  while (i < data.byteLength) {
+    // parse box data
+    size = view.getUint32(i);
+    type =  parseType(data.subarray(i + 4, i + 8));
+    end = size > 1 ? i + size : data.byteLength;
+
+    // parse type-specific data
+    box = (parse[type] || function(data) {
+      return {
+        data: data
+      };
+    })(data.subarray(i + 8, end));
+    box.size = size;
+    box.type = type;
 
-		},
+    // store this box and move to the next
+    result.push(box);
+    i = end;
+  }
+  return result;
+};
 
-		fromArray: function ( array, offset ) {
+/**
+ * Returns a textual representation of the javascript represtentation
+ * of an MP4 file. You can use it as an alternative to
+ * JSON.stringify() to compare inspected MP4s.
+ * @param inspectedMp4 {array} the parsed array of boxes in an MP4
+ * file
+ * @param depth {number} (optional) the number of ancestor boxes of
+ * the elements of inspectedMp4. Assumed to be zero if unspecified.
+ * @return {string} a text representation of the parsed MP4
+ */
+textifyMp4 = function(inspectedMp4, depth) {
+  var indent;
+  depth = depth || 0;
+  indent = new Array(depth * 2 + 1).join(' ');
+
+  // iterate over all the boxes
+  return inspectedMp4.map(function(box, index) {
+
+    // list the box type first at the current indentation level
+    return indent + box.type + '\n' +
+
+      // the type is already included and handle child boxes separately
+      Object.keys(box).filter(function(key) {
+        return key !== 'type' && key !== 'boxes';
+
+      // output all the box properties
+      }).map(function(key) {
+        var prefix = indent + '  ' + key + ': ',
+            value = box[key];
+
+        // print out raw bytes as hexademical
+        if (value instanceof Uint8Array || value instanceof Uint32Array) {
+          var bytes = Array.prototype.slice.call(new Uint8Array(value.buffer, value.byteOffset, value.byteLength))
+              .map(function(byte) {
+                return ' ' + ('00' + byte.toString(16)).slice(-2);
+              }).join('').match(/.{1,24}/g);
+          if (!bytes) {
+            return prefix + '<>';
+          }
+          if (bytes.length === 1) {
+            return prefix + '<' + bytes.join('').slice(1) + '>';
+          }
+          return prefix + '<\n' + bytes.map(function(line) {
+            return indent + '  ' + line;
+          }).join('\n') + '\n' + indent + '  >';
+        }
 
-			if ( offset === undefined ) { offset = 0; }
+        // stringify generic objects
+        return prefix +
+            JSON.stringify(value, null, 2)
+              .split('\n').map(function(line, index) {
+                if (index === 0) {
+                  return line;
+                }
+                return indent + '  ' + line;
+              }).join('\n');
+      }).join('\n') +
 
-			this.x = array[ offset ];
-			this.y = array[ offset + 1 ];
+    // recursively textify the child boxes
+    (box.boxes ? '\n' + textifyMp4(box.boxes, depth + 1) : '');
+  }).join('\n');
+};
 
-			return this;
+module.exports = {
+  inspect: inspectMp4,
+  textify: textifyMp4,
+  parseType: parseType,
+  findBox: findBox,
+  parseTraf: parse.traf,
+  parseTfdt: parse.tfdt,
+  parseHdlr: parse.hdlr,
+  parseTfhd: parse.tfhd,
+  parseTrun: parse.trun,
+  parseSidx: parse.sidx
+};
 
-		},
+},{"../utils/bin":57}],56:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * Parse mpeg2 transport stream packets to extract basic timing information
+ */
+'use strict';
 
-		toArray: function ( array, offset ) {
+var StreamTypes = require('../m2ts/stream-types.js');
+var handleRollover = require('../m2ts/timestamp-rollover-stream.js').handleRollover;
+var probe = {};
+probe.ts = require('../m2ts/probe.js');
+probe.aac = require('../aac/utils.js');
+var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+var
+  MP2T_PACKET_LENGTH = 188, // bytes
+  SYNC_BYTE = 0x47;
 
-			array[ offset ] = this.x;
-			array[ offset + 1 ] = this.y;
+/**
+ * walks through segment data looking for pat and pmt packets to parse out
+ * program map table information
+ */
+var parsePsi_ = function(bytes, pmt) {
+  var
+    startIndex = 0,
+    endIndex = MP2T_PACKET_LENGTH,
+    packet, type;
+
+  while (endIndex < bytes.byteLength) {
+    // Look for a pair of start and end sync bytes in the data..
+    if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
+      // We found a packet
+      packet = bytes.subarray(startIndex, endIndex);
+      type = probe.ts.parseType(packet, pmt.pid);
 
-			return array;
+      switch (type) {
+        case 'pat':
+          if (!pmt.pid) {
+            pmt.pid = probe.ts.parsePat(packet);
+          }
+          break;
+        case 'pmt':
+          if (!pmt.table) {
+            pmt.table = probe.ts.parsePmt(packet);
+          }
+          break;
+        default:
+          break;
+      }
 
-		},
+      // Found the pat and pmt, we can stop walking the segment
+      if (pmt.pid && pmt.table) {
+        return;
+      }
 
-		fromBufferAttribute: function ( attribute, index, offset ) {
+      startIndex += MP2T_PACKET_LENGTH;
+      endIndex += MP2T_PACKET_LENGTH;
+      continue;
+    }
 
-			if ( offset !== undefined ) {
+    // If we get here, we have somehow become de-synchronized and we need to step
+    // forward one byte at a time until we find a pair of sync bytes that denote
+    // a packet
+    startIndex++;
+    endIndex++;
+  }
+};
 
-				console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' );
+/**
+ * walks through the segment data from the start and end to get timing information
+ * for the first and last audio pes packets
+ */
+var parseAudioPes_ = function(bytes, pmt, result) {
+  var
+    startIndex = 0,
+    endIndex = MP2T_PACKET_LENGTH,
+    packet, type, pesType, pusi, parsed;
+
+  var endLoop = false;
+
+  // Start walking from start of segment to get first audio packet
+  while (endIndex <= bytes.byteLength) {
+    // Look for a pair of start and end sync bytes in the data..
+    if (bytes[startIndex] === SYNC_BYTE &&
+        (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
+      // We found a packet
+      packet = bytes.subarray(startIndex, endIndex);
+      type = probe.ts.parseType(packet, pmt.pid);
 
-			}
+      switch (type) {
+        case 'pes':
+          pesType = probe.ts.parsePesType(packet, pmt.table);
+          pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
+          if (pesType === 'audio' && pusi) {
+            parsed = probe.ts.parsePesTime(packet);
+            if (parsed) {
+              parsed.type = 'audio';
+              result.audio.push(parsed);
+              endLoop = true;
+            }
+          }
+          break;
+        default:
+          break;
+      }
 
-			this.x = attribute.getX( index );
-			this.y = attribute.getY( index );
+      if (endLoop) {
+        break;
+      }
 
-			return this;
+      startIndex += MP2T_PACKET_LENGTH;
+      endIndex += MP2T_PACKET_LENGTH;
+      continue;
+    }
 
-		},
+    // If we get here, we have somehow become de-synchronized and we need to step
+    // forward one byte at a time until we find a pair of sync bytes that denote
+    // a packet
+    startIndex++;
+    endIndex++;
+  }
 
-		rotateAround: function ( center, angle ) {
+  // Start walking from end of segment to get last audio packet
+  endIndex = bytes.byteLength;
+  startIndex = endIndex - MP2T_PACKET_LENGTH;
+  endLoop = false;
+  while (startIndex >= 0) {
+    // Look for a pair of start and end sync bytes in the data..
+    if (bytes[startIndex] === SYNC_BYTE &&
+        (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
+      // We found a packet
+      packet = bytes.subarray(startIndex, endIndex);
+      type = probe.ts.parseType(packet, pmt.pid);
 
-			var c = Math.cos( angle ), s = Math.sin( angle );
+      switch (type) {
+        case 'pes':
+          pesType = probe.ts.parsePesType(packet, pmt.table);
+          pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
+          if (pesType === 'audio' && pusi) {
+            parsed = probe.ts.parsePesTime(packet);
+            if (parsed) {
+              parsed.type = 'audio';
+              result.audio.push(parsed);
+              endLoop = true;
+            }
+          }
+          break;
+        default:
+          break;
+      }
 
-			var x = this.x - center.x;
-			var y = this.y - center.y;
+      if (endLoop) {
+        break;
+      }
 
-			this.x = x * c - y * s + center.x;
-			this.y = x * s + y * c + center.y;
+      startIndex -= MP2T_PACKET_LENGTH;
+      endIndex -= MP2T_PACKET_LENGTH;
+      continue;
+    }
 
-			return this;
+    // If we get here, we have somehow become de-synchronized and we need to step
+    // forward one byte at a time until we find a pair of sync bytes that denote
+    // a packet
+    startIndex--;
+    endIndex--;
+  }
+};
 
-		},
+/**
+ * walks through the segment data from the start and end to get timing information
+ * for the first and last video pes packets as well as timing information for the first
+ * key frame.
+ */
+var parseVideoPes_ = function(bytes, pmt, result) {
+  var
+    startIndex = 0,
+    endIndex = MP2T_PACKET_LENGTH,
+    packet, type, pesType, pusi, parsed, frame, i, pes;
 
-		random: function () {
+  var endLoop = false;
 
-			this.x = Math.random();
-			this.y = Math.random();
+  var currentFrame = {
+    data: [],
+    size: 0
+  };
 
-			return this;
+  // Start walking from start of segment to get first video packet
+  while (endIndex < bytes.byteLength) {
+    // Look for a pair of start and end sync bytes in the data..
+    if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
+      // We found a packet
+      packet = bytes.subarray(startIndex, endIndex);
+      type = probe.ts.parseType(packet, pmt.pid);
 
-		}
+      switch (type) {
+        case 'pes':
+          pesType = probe.ts.parsePesType(packet, pmt.table);
+          pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
+          if (pesType === 'video') {
+            if (pusi && !endLoop) {
+              parsed = probe.ts.parsePesTime(packet);
+              if (parsed) {
+                parsed.type = 'video';
+                result.video.push(parsed);
+                endLoop = true;
+              }
+            }
+            if (!result.firstKeyFrame) {
+              if (pusi) {
+                if (currentFrame.size !== 0) {
+                  frame = new Uint8Array(currentFrame.size);
+                  i = 0;
+                  while (currentFrame.data.length) {
+                    pes = currentFrame.data.shift();
+                    frame.set(pes, i);
+                    i += pes.byteLength;
+                  }
+                  if (probe.ts.videoPacketContainsKeyFrame(frame)) {
+                    var firstKeyFrame = probe.ts.parsePesTime(frame);
+
+                    // PTS/DTS may not be available. Simply *not* setting
+                    // the keyframe seems to work fine with HLS playback
+                    // and definitely preferable to a crash with TypeError...
+                    if (firstKeyFrame) {
+                      result.firstKeyFrame = firstKeyFrame;
+                      result.firstKeyFrame.type = 'video';
+                    } else {
+                      // eslint-disable-next-line
+                      console.warn(
+                        'Failed to extract PTS/DTS from PES at first keyframe. ' +
+                        'This could be an unusual TS segment, or else mux.js did not ' +
+                        'parse your TS segment correctly. If you know your TS ' +
+                        'segments do contain PTS/DTS on keyframes please file a bug ' +
+                        'report! You can try ffprobe to double check for yourself.'
+                      );
+                    }
+                  }
+                  currentFrame.size = 0;
+                }
+              }
+              currentFrame.data.push(packet);
+              currentFrame.size += packet.byteLength;
+            }
+          }
+          break;
+        default:
+          break;
+      }
 
-	} );
+      if (endLoop && result.firstKeyFrame) {
+        break;
+      }
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author bhouston / http://clara.io
-	 * @author tschw
-	 */
+      startIndex += MP2T_PACKET_LENGTH;
+      endIndex += MP2T_PACKET_LENGTH;
+      continue;
+    }
 
-	function Matrix3() {
+    // If we get here, we have somehow become de-synchronized and we need to step
+    // forward one byte at a time until we find a pair of sync bytes that denote
+    // a packet
+    startIndex++;
+    endIndex++;
+  }
 
-		this.elements = [
+  // Start walking from end of segment to get last video packet
+  endIndex = bytes.byteLength;
+  startIndex = endIndex - MP2T_PACKET_LENGTH;
+  endLoop = false;
+  while (startIndex >= 0) {
+    // Look for a pair of start and end sync bytes in the data..
+    if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
+      // We found a packet
+      packet = bytes.subarray(startIndex, endIndex);
+      type = probe.ts.parseType(packet, pmt.pid);
 
-			1, 0, 0,
-			0, 1, 0,
-			0, 0, 1
+      switch (type) {
+        case 'pes':
+          pesType = probe.ts.parsePesType(packet, pmt.table);
+          pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
+          if (pesType === 'video' && pusi) {
+              parsed = probe.ts.parsePesTime(packet);
+              if (parsed) {
+                parsed.type = 'video';
+                result.video.push(parsed);
+                endLoop = true;
+              }
+          }
+          break;
+        default:
+          break;
+      }
 
-		];
+      if (endLoop) {
+        break;
+      }
 
-		if ( arguments.length > 0 ) {
+      startIndex -= MP2T_PACKET_LENGTH;
+      endIndex -= MP2T_PACKET_LENGTH;
+      continue;
+    }
 
-			console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' );
+    // If we get here, we have somehow become de-synchronized and we need to step
+    // forward one byte at a time until we find a pair of sync bytes that denote
+    // a packet
+    startIndex--;
+    endIndex--;
+  }
+};
 
-		}
+/**
+ * Adjusts the timestamp information for the segment to account for
+ * rollover and convert to seconds based on pes packet timescale (90khz clock)
+ */
+var adjustTimestamp_ = function(segmentInfo, baseTimestamp) {
+  if (segmentInfo.audio && segmentInfo.audio.length) {
+    var audioBaseTimestamp = baseTimestamp;
+    if (typeof audioBaseTimestamp === 'undefined') {
+      audioBaseTimestamp = segmentInfo.audio[0].dts;
+    }
+    segmentInfo.audio.forEach(function(info) {
+      info.dts = handleRollover(info.dts, audioBaseTimestamp);
+      info.pts = handleRollover(info.pts, audioBaseTimestamp);
+      // time in seconds
+      info.dtsTime = info.dts / ONE_SECOND_IN_TS;
+      info.ptsTime = info.pts / ONE_SECOND_IN_TS;
+    });
+  }
 
-	}
+  if (segmentInfo.video && segmentInfo.video.length) {
+    var videoBaseTimestamp = baseTimestamp;
+    if (typeof videoBaseTimestamp === 'undefined') {
+      videoBaseTimestamp = segmentInfo.video[0].dts;
+    }
+    segmentInfo.video.forEach(function(info) {
+      info.dts = handleRollover(info.dts, videoBaseTimestamp);
+      info.pts = handleRollover(info.pts, videoBaseTimestamp);
+      // time in seconds
+      info.dtsTime = info.dts / ONE_SECOND_IN_TS;
+      info.ptsTime = info.pts / ONE_SECOND_IN_TS;
+    });
+    if (segmentInfo.firstKeyFrame) {
+      var frame = segmentInfo.firstKeyFrame;
+      frame.dts = handleRollover(frame.dts, videoBaseTimestamp);
+      frame.pts = handleRollover(frame.pts, videoBaseTimestamp);
+      // time in seconds
+      frame.dtsTime = frame.dts / ONE_SECOND_IN_TS;
+      frame.ptsTime = frame.dts / ONE_SECOND_IN_TS;
+    }
+  }
+};
 
-	Object.assign( Matrix3.prototype, {
+/**
+ * inspects the aac data stream for start and end time information
+ */
+var inspectAac_ = function(bytes) {
+  var
+    endLoop = false,
+    audioCount = 0,
+    sampleRate = null,
+    timestamp = null,
+    frameSize = 0,
+    byteIndex = 0,
+    packet;
+
+  while (bytes.length - byteIndex >= 3) {
+    var type = probe.aac.parseType(bytes, byteIndex);
+    switch (type) {
+      case 'timed-metadata':
+        // Exit early because we don't have enough to parse
+        // the ID3 tag header
+        if (bytes.length - byteIndex < 10) {
+          endLoop = true;
+          break;
+        }
 
-		isMatrix3: true,
+        frameSize = probe.aac.parseId3TagSize(bytes, byteIndex);
 
-		set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
+        // Exit early if we don't have enough in the buffer
+        // to emit a full packet
+        if (frameSize > bytes.length) {
+          endLoop = true;
+          break;
+        }
+        if (timestamp === null) {
+          packet = bytes.subarray(byteIndex, byteIndex + frameSize);
+          timestamp = probe.aac.parseAacTimestamp(packet);
+        }
+        byteIndex += frameSize;
+        break;
+      case 'audio':
+        // Exit early because we don't have enough to parse
+        // the ADTS frame header
+        if (bytes.length - byteIndex < 7) {
+          endLoop = true;
+          break;
+        }
 
-			var te = this.elements;
+        frameSize = probe.aac.parseAdtsSize(bytes, byteIndex);
 
-			te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31;
-			te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32;
-			te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33;
+        // Exit early if we don't have enough in the buffer
+        // to emit a full packet
+        if (frameSize > bytes.length) {
+          endLoop = true;
+          break;
+        }
+        if (sampleRate === null) {
+          packet = bytes.subarray(byteIndex, byteIndex + frameSize);
+          sampleRate = probe.aac.parseSampleRate(packet);
+        }
+        audioCount++;
+        byteIndex += frameSize;
+        break;
+      default:
+        byteIndex++;
+        break;
+    }
+    if (endLoop) {
+      return null;
+    }
+  }
+  if (sampleRate === null || timestamp === null) {
+    return null;
+  }
 
-			return this;
+  var audioTimescale = ONE_SECOND_IN_TS / sampleRate;
+
+  var result = {
+    audio: [
+      {
+        type: 'audio',
+        dts: timestamp,
+        pts: timestamp
+      },
+      {
+        type: 'audio',
+        dts: timestamp + (audioCount * 1024 * audioTimescale),
+        pts: timestamp + (audioCount * 1024 * audioTimescale)
+      }
+    ]
+  };
 
-		},
+  return result;
+};
 
-		identity: function () {
+/**
+ * inspects the transport stream segment data for start and end time information
+ * of the audio and video tracks (when present) as well as the first key frame's
+ * start time.
+ */
+var inspectTs_ = function(bytes) {
+  var pmt = {
+    pid: null,
+    table: null
+  };
 
-			this.set(
+  var result = {};
 
-				1, 0, 0,
-				0, 1, 0,
-				0, 0, 1
+  parsePsi_(bytes, pmt);
 
-			);
+  for (var pid in pmt.table) {
+    if (pmt.table.hasOwnProperty(pid)) {
+      var type = pmt.table[pid];
+      switch (type) {
+        case StreamTypes.H264_STREAM_TYPE:
+          result.video = [];
+          parseVideoPes_(bytes, pmt, result);
+          if (result.video.length === 0) {
+            delete result.video;
+          }
+          break;
+        case StreamTypes.ADTS_STREAM_TYPE:
+          result.audio = [];
+          parseAudioPes_(bytes, pmt, result);
+          if (result.audio.length === 0) {
+            delete result.audio;
+          }
+          break;
+        default:
+          break;
+      }
+    }
+  }
+  return result;
+};
 
-			return this;
+/**
+ * Inspects segment byte data and returns an object with start and end timing information
+ *
+ * @param {Uint8Array} bytes The segment byte data
+ * @param {Number} baseTimestamp Relative reference timestamp used when adjusting frame
+ *  timestamps for rollover. This value must be in 90khz clock.
+ * @return {Object} Object containing start and end frame timing info of segment.
+ */
+var inspect = function(bytes, baseTimestamp) {
+  var isAacData = probe.aac.isLikelyAacData(bytes);
 
-		},
+  var result;
 
-		clone: function () {
+  if (isAacData) {
+    result = inspectAac_(bytes);
+  } else {
+    result = inspectTs_(bytes);
+  }
 
-			return new this.constructor().fromArray( this.elements );
+  if (!result || (!result.audio && !result.video)) {
+    return null;
+  }
 
-		},
+  adjustTimestamp_(result, baseTimestamp);
 
-		copy: function ( m ) {
+  return result;
+};
 
-			var te = this.elements;
-			var me = m.elements;
+module.exports = {
+  inspect: inspect,
+  parseAudioPes_: parseAudioPes_
+};
 
-			te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ];
-			te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ];
-			te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ];
+},{"../aac/utils.js":20,"../m2ts/probe.js":38,"../m2ts/stream-types.js":39,"../m2ts/timestamp-rollover-stream.js":40,"../utils/clock":58}],57:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+var toUnsigned = function(value) {
+  return value >>> 0;
+};
 
-			return this;
+var toHexString = function(value) {
+  return ('00' + value.toString(16)).slice(-2);
+};
 
-		},
+module.exports = {
+  toUnsigned: toUnsigned,
+  toHexString: toHexString
+};
 
-		extractBasis: function ( xAxis, yAxis, zAxis ) {
+},{}],58:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+var
+  ONE_SECOND_IN_TS = 90000, // 90kHz clock
+  secondsToVideoTs,
+  secondsToAudioTs,
+  videoTsToSeconds,
+  audioTsToSeconds,
+  audioTsToVideoTs,
+  videoTsToAudioTs,
+  metadataTsToSeconds;
+
+secondsToVideoTs = function(seconds) {
+  return seconds * ONE_SECOND_IN_TS;
+};
 
-			xAxis.setFromMatrix3Column( this, 0 );
-			yAxis.setFromMatrix3Column( this, 1 );
-			zAxis.setFromMatrix3Column( this, 2 );
+secondsToAudioTs = function(seconds, sampleRate) {
+  return seconds * sampleRate;
+};
 
-			return this;
+videoTsToSeconds = function(timestamp) {
+  return timestamp / ONE_SECOND_IN_TS;
+};
 
-		},
+audioTsToSeconds = function(timestamp, sampleRate) {
+  return timestamp / sampleRate;
+};
 
-		setFromMatrix4: function ( m ) {
+audioTsToVideoTs = function(timestamp, sampleRate) {
+  return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
+};
 
-			var me = m.elements;
+videoTsToAudioTs = function(timestamp, sampleRate) {
+  return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
+};
 
-			this.set(
+/**
+ * Adjust ID3 tag or caption timing information by the timeline pts values
+ * (if keepOriginalTimestamps is false) and convert to seconds
+ */
+metadataTsToSeconds = function(timestamp, timelineStartPts, keepOriginalTimestamps) {
+  return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
+};
 
-				me[ 0 ], me[ 4 ], me[ 8 ],
-				me[ 1 ], me[ 5 ], me[ 9 ],
-				me[ 2 ], me[ 6 ], me[ 10 ]
+module.exports = {
+  ONE_SECOND_IN_TS: ONE_SECOND_IN_TS,
+  secondsToVideoTs: secondsToVideoTs,
+  secondsToAudioTs: secondsToAudioTs,
+  videoTsToSeconds: videoTsToSeconds,
+  audioTsToSeconds: audioTsToSeconds,
+  audioTsToVideoTs: audioTsToVideoTs,
+  videoTsToAudioTs: videoTsToAudioTs,
+  metadataTsToSeconds: metadataTsToSeconds
+};
 
-			);
+},{}],59:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ */
+'use strict';
 
-			return this;
+var ExpGolomb;
 
-		},
+/**
+ * Parser for exponential Golomb codes, a variable-bitwidth number encoding
+ * scheme used by h264.
+ */
+ExpGolomb = function(workingData) {
+  var
+    // the number of bytes left to examine in workingData
+    workingBytesAvailable = workingData.byteLength,
 
-		multiply: function ( m ) {
+    // the current word being examined
+    workingWord = 0, // :uint
 
-			return this.multiplyMatrices( this, m );
+    // the number of bits left to examine in the current word
+    workingBitsAvailable = 0; // :uint;
 
-		},
+  // ():uint
+  this.length = function() {
+    return (8 * workingBytesAvailable);
+  };
 
-		premultiply: function ( m ) {
+  // ():uint
+  this.bitsAvailable = function() {
+    return (8 * workingBytesAvailable) + workingBitsAvailable;
+  };
 
-			return this.multiplyMatrices( m, this );
+  // ():void
+  this.loadWord = function() {
+    var
+      position = workingData.byteLength - workingBytesAvailable,
+      workingBytes = new Uint8Array(4),
+      availableBytes = Math.min(4, workingBytesAvailable);
 
-		},
+    if (availableBytes === 0) {
+      throw new Error('no bytes available');
+    }
 
-		multiplyMatrices: function ( a, b ) {
+    workingBytes.set(workingData.subarray(position,
+                                          position + availableBytes));
+    workingWord = new DataView(workingBytes.buffer).getUint32(0);
 
-			var ae = a.elements;
-			var be = b.elements;
-			var te = this.elements;
+    // track the amount of workingData that has been processed
+    workingBitsAvailable = availableBytes * 8;
+    workingBytesAvailable -= availableBytes;
+  };
 
-			var a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ];
-			var a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ];
-			var a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ];
+  // (count:int):void
+  this.skipBits = function(count) {
+    var skipBytes; // :int
+    if (workingBitsAvailable > count) {
+      workingWord          <<= count;
+      workingBitsAvailable -= count;
+    } else {
+      count -= workingBitsAvailable;
+      skipBytes = Math.floor(count / 8);
 
-			var b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ];
-			var b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ];
-			var b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ];
+      count -= (skipBytes * 8);
+      workingBytesAvailable -= skipBytes;
 
-			te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31;
-			te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32;
-			te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33;
+      this.loadWord();
 
-			te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31;
-			te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32;
-			te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33;
+      workingWord <<= count;
+      workingBitsAvailable -= count;
+    }
+  };
 
-			te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31;
-			te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32;
-			te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33;
+  // (size:int):uint
+  this.readBits = function(size) {
+    var
+      bits = Math.min(workingBitsAvailable, size), // :uint
+      valu = workingWord >>> (32 - bits); // :uint
+    // if size > 31, handle error
+    workingBitsAvailable -= bits;
+    if (workingBitsAvailable > 0) {
+      workingWord <<= bits;
+    } else if (workingBytesAvailable > 0) {
+      this.loadWord();
+    }
 
-			return this;
+    bits = size - bits;
+    if (bits > 0) {
+      return valu << bits | this.readBits(bits);
+    }
+    return valu;
+  };
 
-		},
+  // ():uint
+  this.skipLeadingZeros = function() {
+    var leadingZeroCount; // :uint
+    for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
+      if ((workingWord & (0x80000000 >>> leadingZeroCount)) !== 0) {
+        // the first bit of working word is 1
+        workingWord <<= leadingZeroCount;
+        workingBitsAvailable -= leadingZeroCount;
+        return leadingZeroCount;
+      }
+    }
 
-		multiplyScalar: function ( s ) {
+    // we exhausted workingWord and still have not found a 1
+    this.loadWord();
+    return leadingZeroCount + this.skipLeadingZeros();
+  };
 
-			var te = this.elements;
+  // ():void
+  this.skipUnsignedExpGolomb = function() {
+    this.skipBits(1 + this.skipLeadingZeros());
+  };
 
-			te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
-			te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
-			te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
+  // ():void
+  this.skipExpGolomb = function() {
+    this.skipBits(1 + this.skipLeadingZeros());
+  };
 
-			return this;
+  // ():uint
+  this.readUnsignedExpGolomb = function() {
+    var clz = this.skipLeadingZeros(); // :uint
+    return this.readBits(clz + 1) - 1;
+  };
 
-		},
+  // ():int
+  this.readExpGolomb = function() {
+    var valu = this.readUnsignedExpGolomb(); // :int
+    if (0x01 & valu) {
+      // the number is odd if the low order bit is set
+      return (1 + valu) >>> 1; // add 1 to make it even, and divide by 2
+    }
+    return -1 * (valu >>> 1); // divide by two then make it negative
+  };
 
-		determinant: function () {
+  // Some convenience functions
+  // :Boolean
+  this.readBoolean = function() {
+    return this.readBits(1) === 1;
+  };
 
-			var te = this.elements;
+  // ():int
+  this.readUnsignedByte = function() {
+    return this.readBits(8);
+  };
 
-			var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
-				d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
-				g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
+  this.loadWord();
+};
 
-			return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
+module.exports = ExpGolomb;
 
-		},
+},{}],60:[function(require,module,exports){
+/**
+ * mux.js
+ *
+ * Copyright (c) Brightcove
+ * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
+ *
+ * A lightweight readable stream implemention that handles event dispatching.
+ * Objects that inherit from streams should call init in their constructors.
+ */
+'use strict';
 
-		getInverse: function ( matrix, throwOnDegenerate ) {
+var Stream = function() {
+  this.init = function() {
+    var listeners = {};
+    /**
+     * Add a listener for a specified event type.
+     * @param type {string} the event name
+     * @param listener {function} the callback to be invoked when an event of
+     * the specified type occurs
+     */
+    this.on = function(type, listener) {
+      if (!listeners[type]) {
+        listeners[type] = [];
+      }
+      listeners[type] = listeners[type].concat(listener);
+    };
+    /**
+     * Remove a listener for a specified event type.
+     * @param type {string} the event name
+     * @param listener {function} a function previously registered for this
+     * type of event through `on`
+     */
+    this.off = function(type, listener) {
+      var index;
+      if (!listeners[type]) {
+        return false;
+      }
+      index = listeners[type].indexOf(listener);
+      listeners[type] = listeners[type].slice();
+      listeners[type].splice(index, 1);
+      return index > -1;
+    };
+    /**
+     * Trigger an event of the specified type on this stream. Any additional
+     * arguments to this function are passed as parameters to event listeners.
+     * @param type {string} the event name
+     */
+    this.trigger = function(type) {
+      var callbacks, i, length, args;
+      callbacks = listeners[type];
+      if (!callbacks) {
+        return;
+      }
+      // Slicing the arguments on every invocation of this method
+      // can add a significant amount of overhead. Avoid the
+      // intermediate object creation for the common case of a
+      // single callback argument
+      if (arguments.length === 2) {
+        length = callbacks.length;
+        for (i = 0; i < length; ++i) {
+          callbacks[i].call(this, arguments[1]);
+        }
+      } else {
+        args = [];
+        i = arguments.length;
+        for (i = 1; i < arguments.length; ++i) {
+          args.push(arguments[i]);
+        }
+        length = callbacks.length;
+        for (i = 0; i < length; ++i) {
+          callbacks[i].apply(this, args);
+        }
+      }
+    };
+    /**
+     * Destroys the stream and cleans up.
+     */
+    this.dispose = function() {
+      listeners = {};
+    };
+  };
+};
 
-			if ( throwOnDegenerate !== undefined ) {
+/**
+ * Forwards all `data` events on this stream to the destination stream. The
+ * destination stream should provide a method `push` to receive the data
+ * events as they arrive.
+ * @param destination {stream} the stream that will receive all `data` events
+ * @param autoFlush {boolean} if false, we will not call `flush` on the destination
+ *                            when the current stream emits a 'done' event
+ * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
+ */
+Stream.prototype.pipe = function(destination) {
+  this.on('data', function(data) {
+    destination.push(data);
+  });
 
-				console.warn( "THREE.Matrix3: .getInverse() can no longer be configured to throw on degenerate." );
+  this.on('done', function(flushSource) {
+    destination.flush(flushSource);
+  });
 
-			}
+  this.on('partialdone', function(flushSource) {
+    destination.partialFlush(flushSource);
+  });
 
-			var me = matrix.elements,
-				te = this.elements,
+  this.on('endedtimeline', function(flushSource) {
+    destination.endTimeline(flushSource);
+  });
 
-				n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ],
-				n12 = me[ 3 ], n22 = me[ 4 ], n32 = me[ 5 ],
-				n13 = me[ 6 ], n23 = me[ 7 ], n33 = me[ 8 ],
+  this.on('reset', function(flushSource) {
+    destination.reset(flushSource);
+  });
 
-				t11 = n33 * n22 - n32 * n23,
-				t12 = n32 * n13 - n33 * n12,
-				t13 = n23 * n12 - n22 * n13,
+  return destination;
+};
 
-				det = n11 * t11 + n21 * t12 + n31 * t13;
+// Default stream functions that are expected to be overridden to perform
+// actual work. These are provided by the prototype as a sort of no-op
+// implementation so that we don't have to check for their existence in the
+// `pipe` function above.
+Stream.prototype.push = function(data) {
+  this.trigger('data', data);
+};
 
-			if ( det === 0 ) { return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); }
+Stream.prototype.flush = function(flushSource) {
+  this.trigger('done', flushSource);
+};
 
-			var detInv = 1 / det;
+Stream.prototype.partialFlush = function(flushSource) {
+  this.trigger('partialdone', flushSource);
+};
 
-			te[ 0 ] = t11 * detInv;
-			te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
-			te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
+Stream.prototype.endTimeline = function(flushSource) {
+  this.trigger('endedtimeline', flushSource);
+};
 
-			te[ 3 ] = t12 * detInv;
-			te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
-			te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
+Stream.prototype.reset = function(flushSource) {
+  this.trigger('reset', flushSource);
+};
 
-			te[ 6 ] = t13 * detInv;
-			te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
-			te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
-
-			return this;
-
-		},
-
-		transpose: function () {
-
-			var tmp, m = this.elements;
-
-			tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
-			tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
-			tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
-
-			return this;
+module.exports = Stream;
 
-		},
-
-		getNormalMatrix: function ( matrix4 ) {
-
-			return this.setFromMatrix4( matrix4 ).getInverse( this ).transpose();
-
-		},
-
-		transposeIntoArray: function ( r ) {
+},{}],61:[function(require,module,exports){
+/*
+object-assign
+(c) Sindre Sorhus
+@license MIT
+*/
 
-			var m = this.elements;
+'use strict';
+/* eslint-disable no-unused-vars */
+var getOwnPropertySymbols = Object.getOwnPropertySymbols;
+var hasOwnProperty = Object.prototype.hasOwnProperty;
+var propIsEnumerable = Object.prototype.propertyIsEnumerable;
 
-			r[ 0 ] = m[ 0 ];
-			r[ 1 ] = m[ 3 ];
-			r[ 2 ] = m[ 6 ];
-			r[ 3 ] = m[ 1 ];
-			r[ 4 ] = m[ 4 ];
-			r[ 5 ] = m[ 7 ];
-			r[ 6 ] = m[ 2 ];
-			r[ 7 ] = m[ 5 ];
-			r[ 8 ] = m[ 8 ];
+function toObject(val) {
+	if (val === null || val === undefined) {
+		throw new TypeError('Object.assign cannot be called with null or undefined');
+	}
 
-			return this;
+	return Object(val);
+}
 
-		},
+function shouldUseNative() {
+	try {
+		if (!Object.assign) {
+			return false;
+		}
 
-		setUvTransform: function ( tx, ty, sx, sy, rotation, cx, cy ) {
+		// Detect buggy property enumeration order in older V8 versions.
 
-			var c = Math.cos( rotation );
-			var s = Math.sin( rotation );
+		// https://bugs.chromium.org/p/v8/issues/detail?id=4118
+		var test1 = new String('abc');  // eslint-disable-line no-new-wrappers
+		test1[5] = 'de';
+		if (Object.getOwnPropertyNames(test1)[0] === '5') {
+			return false;
+		}
 
-			this.set(
-				sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
-				- sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
-				0, 0, 1
-			);
+		// https://bugs.chromium.org/p/v8/issues/detail?id=3056
+		var test2 = {};
+		for (var i = 0; i < 10; i++) {
+			test2['_' + String.fromCharCode(i)] = i;
+		}
+		var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
+			return test2[n];
+		});
+		if (order2.join('') !== '0123456789') {
+			return false;
+		}
 
-		},
+		// https://bugs.chromium.org/p/v8/issues/detail?id=3056
+		var test3 = {};
+		'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
+			test3[letter] = letter;
+		});
+		if (Object.keys(Object.assign({}, test3)).join('') !==
+				'abcdefghijklmnopqrst') {
+			return false;
+		}
 
-		scale: function ( sx, sy ) {
+		return true;
+	} catch (err) {
+		// We don't expect any of the above to throw, but better to be safe.
+		return false;
+	}
+}
 
-			var te = this.elements;
+module.exports = shouldUseNative() ? Object.assign : function (target, source) {
+	var from;
+	var to = toObject(target);
+	var symbols;
 
-			te[ 0 ] *= sx; te[ 3 ] *= sx; te[ 6 ] *= sx;
-			te[ 1 ] *= sy; te[ 4 ] *= sy; te[ 7 ] *= sy;
+	for (var s = 1; s < arguments.length; s++) {
+		from = Object(arguments[s]);
 
-			return this;
+		for (var key in from) {
+			if (hasOwnProperty.call(from, key)) {
+				to[key] = from[key];
+			}
+		}
 
-		},
+		if (getOwnPropertySymbols) {
+			symbols = getOwnPropertySymbols(from);
+			for (var i = 0; i < symbols.length; i++) {
+				if (propIsEnumerable.call(from, symbols[i])) {
+					to[symbols[i]] = from[symbols[i]];
+				}
+			}
+		}
+	}
 
-		rotate: function ( theta ) {
+	return to;
+};
 
-			var c = Math.cos( theta );
-			var s = Math.sin( theta );
+},{}],62:[function(require,module,exports){
+(function (process){
+'use strict';
 
-			var te = this.elements;
+if (typeof process === 'undefined' ||
+    !process.version ||
+    process.version.indexOf('v0.') === 0 ||
+    process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
+  module.exports = { nextTick: nextTick };
+} else {
+  module.exports = process
+}
 
-			var a11 = te[ 0 ], a12 = te[ 3 ], a13 = te[ 6 ];
-			var a21 = te[ 1 ], a22 = te[ 4 ], a23 = te[ 7 ];
+function nextTick(fn, arg1, arg2, arg3) {
+  if (typeof fn !== 'function') {
+    throw new TypeError('"callback" argument must be a function');
+  }
+  var len = arguments.length;
+  var args, i;
+  switch (len) {
+  case 0:
+  case 1:
+    return process.nextTick(fn);
+  case 2:
+    return process.nextTick(function afterTickOne() {
+      fn.call(null, arg1);
+    });
+  case 3:
+    return process.nextTick(function afterTickTwo() {
+      fn.call(null, arg1, arg2);
+    });
+  case 4:
+    return process.nextTick(function afterTickThree() {
+      fn.call(null, arg1, arg2, arg3);
+    });
+  default:
+    args = new Array(len - 1);
+    i = 0;
+    while (i < args.length) {
+      args[i++] = arguments[i];
+    }
+    return process.nextTick(function afterTick() {
+      fn.apply(null, args);
+    });
+  }
+}
 
-			te[ 0 ] = c * a11 + s * a21;
-			te[ 3 ] = c * a12 + s * a22;
-			te[ 6 ] = c * a13 + s * a23;
 
-			te[ 1 ] = - s * a11 + c * a21;
-			te[ 4 ] = - s * a12 + c * a22;
-			te[ 7 ] = - s * a13 + c * a23;
+}).call(this,require('_process'))
+},{"_process":63}],63:[function(require,module,exports){
+// shim for using process in browser
+var process = module.exports = {};
 
-			return this;
+// cached from whatever global is present so that test runners that stub it
+// don't break things.  But we need to wrap it in a try catch in case it is
+// wrapped in strict mode code which doesn't define any globals.  It's inside a
+// function because try/catches deoptimize in certain engines.
 
-		},
+var cachedSetTimeout;
+var cachedClearTimeout;
 
-		translate: function ( tx, ty ) {
+function defaultSetTimout() {
+    throw new Error('setTimeout has not been defined');
+}
+function defaultClearTimeout () {
+    throw new Error('clearTimeout has not been defined');
+}
+(function () {
+    try {
+        if (typeof setTimeout === 'function') {
+            cachedSetTimeout = setTimeout;
+        } else {
+            cachedSetTimeout = defaultSetTimout;
+        }
+    } catch (e) {
+        cachedSetTimeout = defaultSetTimout;
+    }
+    try {
+        if (typeof clearTimeout === 'function') {
+            cachedClearTimeout = clearTimeout;
+        } else {
+            cachedClearTimeout = defaultClearTimeout;
+        }
+    } catch (e) {
+        cachedClearTimeout = defaultClearTimeout;
+    }
+} ())
+function runTimeout(fun) {
+    if (cachedSetTimeout === setTimeout) {
+        //normal enviroments in sane situations
+        return setTimeout(fun, 0);
+    }
+    // if setTimeout wasn't available but was latter defined
+    if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
+        cachedSetTimeout = setTimeout;
+        return setTimeout(fun, 0);
+    }
+    try {
+        // when when somebody has screwed with setTimeout but no I.E. maddness
+        return cachedSetTimeout(fun, 0);
+    } catch(e){
+        try {
+            // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
+            return cachedSetTimeout.call(null, fun, 0);
+        } catch(e){
+            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
+            return cachedSetTimeout.call(this, fun, 0);
+        }
+    }
 
-			var te = this.elements;
 
-			te[ 0 ] += tx * te[ 2 ]; te[ 3 ] += tx * te[ 5 ]; te[ 6 ] += tx * te[ 8 ];
-			te[ 1 ] += ty * te[ 2 ]; te[ 4 ] += ty * te[ 5 ]; te[ 7 ] += ty * te[ 8 ];
+}
+function runClearTimeout(marker) {
+    if (cachedClearTimeout === clearTimeout) {
+        //normal enviroments in sane situations
+        return clearTimeout(marker);
+    }
+    // if clearTimeout wasn't available but was latter defined
+    if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
+        cachedClearTimeout = clearTimeout;
+        return clearTimeout(marker);
+    }
+    try {
+        // when when somebody has screwed with setTimeout but no I.E. maddness
+        return cachedClearTimeout(marker);
+    } catch (e){
+        try {
+            // When we are in I.E. but the script has been evaled so I.E. doesn't  trust the global object when called normally
+            return cachedClearTimeout.call(null, marker);
+        } catch (e){
+            // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
+            // Some versions of I.E. have different rules for clearTimeout vs setTimeout
+            return cachedClearTimeout.call(this, marker);
+        }
+    }
 
-			return this;
 
-		},
 
-		equals: function ( matrix ) {
+}
+var queue = [];
+var draining = false;
+var currentQueue;
+var queueIndex = -1;
 
-			var te = this.elements;
-			var me = matrix.elements;
+function cleanUpNextTick() {
+    if (!draining || !currentQueue) {
+        return;
+    }
+    draining = false;
+    if (currentQueue.length) {
+        queue = currentQueue.concat(queue);
+    } else {
+        queueIndex = -1;
+    }
+    if (queue.length) {
+        drainQueue();
+    }
+}
 
-			for ( var i = 0; i < 9; i ++ ) {
+function drainQueue() {
+    if (draining) {
+        return;
+    }
+    var timeout = runTimeout(cleanUpNextTick);
+    draining = true;
 
-				if ( te[ i ] !== me[ i ] ) { return false; }
+    var len = queue.length;
+    while(len) {
+        currentQueue = queue;
+        queue = [];
+        while (++queueIndex < len) {
+            if (currentQueue) {
+                currentQueue[queueIndex].run();
+            }
+        }
+        queueIndex = -1;
+        len = queue.length;
+    }
+    currentQueue = null;
+    draining = false;
+    runClearTimeout(timeout);
+}
 
-			}
+process.nextTick = function (fun) {
+    var args = new Array(arguments.length - 1);
+    if (arguments.length > 1) {
+        for (var i = 1; i < arguments.length; i++) {
+            args[i - 1] = arguments[i];
+        }
+    }
+    queue.push(new Item(fun, args));
+    if (queue.length === 1 && !draining) {
+        runTimeout(drainQueue);
+    }
+};
 
-			return true;
+// v8 likes predictible objects
+function Item(fun, array) {
+    this.fun = fun;
+    this.array = array;
+}
+Item.prototype.run = function () {
+    this.fun.apply(null, this.array);
+};
+process.title = 'browser';
+process.browser = true;
+process.env = {};
+process.argv = [];
+process.version = ''; // empty string to avoid regexp issues
+process.versions = {};
 
-		},
+function noop() {}
 
-		fromArray: function ( array, offset ) {
+process.on = noop;
+process.addListener = noop;
+process.once = noop;
+process.off = noop;
+process.removeListener = noop;
+process.removeAllListeners = noop;
+process.emit = noop;
+process.prependListener = noop;
+process.prependOnceListener = noop;
 
-			if ( offset === undefined ) { offset = 0; }
+process.listeners = function (name) { return [] }
 
-			for ( var i = 0; i < 9; i ++ ) {
+process.binding = function (name) {
+    throw new Error('process.binding is not supported');
+};
 
-				this.elements[ i ] = array[ i + offset ];
+process.cwd = function () { return '/' };
+process.chdir = function (dir) {
+    throw new Error('process.chdir is not supported');
+};
+process.umask = function() { return 0; };
 
-			}
+},{}],64:[function(require,module,exports){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-			return this;
+// a duplex stream is just a stream that is both readable and writable.
+// Since JS doesn't have multiple prototypal inheritance, this class
+// prototypally inherits from Readable, and then parasitically from
+// Writable.
 
-		},
+'use strict';
 
-		toArray: function ( array, offset ) {
+/*<replacement>*/
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+var pna = require('process-nextick-args');
+/*</replacement>*/
 
-			var te = this.elements;
+/*<replacement>*/
+var objectKeys = Object.keys || function (obj) {
+  var keys = [];
+  for (var key in obj) {
+    keys.push(key);
+  }return keys;
+};
+/*</replacement>*/
 
-			array[ offset ] = te[ 0 ];
-			array[ offset + 1 ] = te[ 1 ];
-			array[ offset + 2 ] = te[ 2 ];
+module.exports = Duplex;
 
-			array[ offset + 3 ] = te[ 3 ];
-			array[ offset + 4 ] = te[ 4 ];
-			array[ offset + 5 ] = te[ 5 ];
+/*<replacement>*/
+var util = Object.create(require('core-util-is'));
+util.inherits = require('inherits');
+/*</replacement>*/
 
-			array[ offset + 6 ] = te[ 6 ];
-			array[ offset + 7 ] = te[ 7 ];
-			array[ offset + 8 ] = te[ 8 ];
+var Readable = require('./_stream_readable');
+var Writable = require('./_stream_writable');
 
-			return array;
+util.inherits(Duplex, Readable);
 
-		}
+{
+  // avoid scope creep, the keys array can then be collected
+  var keys = objectKeys(Writable.prototype);
+  for (var v = 0; v < keys.length; v++) {
+    var method = keys[v];
+    if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
+  }
+}
 
-	} );
+function Duplex(options) {
+  if (!(this instanceof Duplex)) return new Duplex(options);
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author szimek / https://github.com/szimek/
-	 */
+  Readable.call(this, options);
+  Writable.call(this, options);
 
-	var _canvas;
+  if (options && options.readable === false) this.readable = false;
 
-	var ImageUtils = {
+  if (options && options.writable === false) this.writable = false;
 
-		getDataURL: function ( image ) {
+  this.allowHalfOpen = true;
+  if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
 
-			var canvas;
+  this.once('end', onend);
+}
 
-			if ( typeof HTMLCanvasElement == 'undefined' ) {
+Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
+  // making it explicit this property is not enumerable
+  // because otherwise some prototype manipulation in
+  // userland will fail
+  enumerable: false,
+  get: function () {
+    return this._writableState.highWaterMark;
+  }
+});
 
-				return image.src;
+// the no-half-open enforcer
+function onend() {
+  // if we allow half-open state, or if the writable side ended,
+  // then we're ok.
+  if (this.allowHalfOpen || this._writableState.ended) return;
 
-			} else if ( image instanceof HTMLCanvasElement ) {
+  // no more data can be written.
+  // But allow more writes to happen in this tick.
+  pna.nextTick(onEndNT, this);
+}
 
-				canvas = image;
+function onEndNT(self) {
+  self.end();
+}
 
-			} else {
+Object.defineProperty(Duplex.prototype, 'destroyed', {
+  get: function () {
+    if (this._readableState === undefined || this._writableState === undefined) {
+      return false;
+    }
+    return this._readableState.destroyed && this._writableState.destroyed;
+  },
+  set: function (value) {
+    // we ignore the value if the stream
+    // has not been initialized yet
+    if (this._readableState === undefined || this._writableState === undefined) {
+      return;
+    }
 
-				if ( _canvas === undefined ) { _canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ); }
+    // backward compatibility, the user is explicitly
+    // managing destroyed
+    this._readableState.destroyed = value;
+    this._writableState.destroyed = value;
+  }
+});
 
-				_canvas.width = image.width;
-				_canvas.height = image.height;
+Duplex.prototype._destroy = function (err, cb) {
+  this.push(null);
+  this.end();
 
-				var context = _canvas.getContext( '2d' );
-
-				if ( image instanceof ImageData ) {
-
-					context.putImageData( image, 0, 0 );
-
-				} else {
-
-					context.drawImage( image, 0, 0, image.width, image.height );
-
-				}
-
-				canvas = _canvas;
-
-			}
-
-			if ( canvas.width > 2048 || canvas.height > 2048 ) {
+  pna.nextTick(cb, err);
+};
+},{"./_stream_readable":66,"./_stream_writable":68,"core-util-is":9,"inherits":12,"process-nextick-args":62}],65:[function(require,module,exports){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-				return canvas.toDataURL( 'image/jpeg', 0.6 );
+// a passthrough stream.
+// basically just the most minimal sort of Transform stream.
+// Every written chunk gets output as-is.
 
-			} else {
+'use strict';
 
-				return canvas.toDataURL( 'image/png' );
+module.exports = PassThrough;
 
-			}
+var Transform = require('./_stream_transform');
 
-		}
+/*<replacement>*/
+var util = Object.create(require('core-util-is'));
+util.inherits = require('inherits');
+/*</replacement>*/
 
-	};
+util.inherits(PassThrough, Transform);
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author szimek / https://github.com/szimek/
-	 */
+function PassThrough(options) {
+  if (!(this instanceof PassThrough)) return new PassThrough(options);
 
-	var textureId = 0;
+  Transform.call(this, options);
+}
 
-	function Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
+PassThrough.prototype._transform = function (chunk, encoding, cb) {
+  cb(null, chunk);
+};
+},{"./_stream_transform":67,"core-util-is":9,"inherits":12}],66:[function(require,module,exports){
+(function (process,global){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-		Object.defineProperty( this, 'id', { value: textureId ++ } );
+'use strict';
 
-		this.uuid = MathUtils.generateUUID();
+/*<replacement>*/
 
-		this.name = '';
+var pna = require('process-nextick-args');
+/*</replacement>*/
 
-		this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
-		this.mipmaps = [];
+module.exports = Readable;
 
-		this.mapping = mapping !== undefined ? mapping : Texture.DEFAULT_MAPPING;
+/*<replacement>*/
+var isArray = require('isarray');
+/*</replacement>*/
 
-		this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
-		this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
+/*<replacement>*/
+var Duplex;
+/*</replacement>*/
 
-		this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
-		this.minFilter = minFilter !== undefined ? minFilter : LinearMipmapLinearFilter;
+Readable.ReadableState = ReadableState;
 
-		this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
+/*<replacement>*/
+var EE = require('events').EventEmitter;
 
-		this.format = format !== undefined ? format : RGBAFormat;
-		this.internalFormat = null;
-		this.type = type !== undefined ? type : UnsignedByteType;
+var EElistenerCount = function (emitter, type) {
+  return emitter.listeners(type).length;
+};
+/*</replacement>*/
 
-		this.offset = new Vector2( 0, 0 );
-		this.repeat = new Vector2( 1, 1 );
-		this.center = new Vector2( 0, 0 );
-		this.rotation = 0;
+/*<replacement>*/
+var Stream = require('./internal/streams/stream');
+/*</replacement>*/
 
-		this.matrixAutoUpdate = true;
-		this.matrix = new Matrix3();
+/*<replacement>*/
 
-		this.generateMipmaps = true;
-		this.premultiplyAlpha = false;
-		this.flipY = true;
-		this.unpackAlignment = 4;	// valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
+var Buffer = require('safe-buffer').Buffer;
+var OurUint8Array = global.Uint8Array || function () {};
+function _uint8ArrayToBuffer(chunk) {
+  return Buffer.from(chunk);
+}
+function _isUint8Array(obj) {
+  return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
+}
 
-		// Values of encoding !== THREE.LinearEncoding only supported on map, envMap and emissiveMap.
-		//
-		// Also changing the encoding after already used by a Material will not automatically make the Material
-		// update. You need to explicitly call Material.needsUpdate to trigger it to recompile.
-		this.encoding = encoding !== undefined ? encoding : LinearEncoding;
+/*</replacement>*/
 
-		this.version = 0;
-		this.onUpdate = null;
+/*<replacement>*/
+var util = Object.create(require('core-util-is'));
+util.inherits = require('inherits');
+/*</replacement>*/
 
-	}
+/*<replacement>*/
+var debugUtil = require('util');
+var debug = void 0;
+if (debugUtil && debugUtil.debuglog) {
+  debug = debugUtil.debuglog('stream');
+} else {
+  debug = function () {};
+}
+/*</replacement>*/
 
-	Texture.DEFAULT_IMAGE = undefined;
-	Texture.DEFAULT_MAPPING = UVMapping;
+var BufferList = require('./internal/streams/BufferList');
+var destroyImpl = require('./internal/streams/destroy');
+var StringDecoder;
 
-	Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+util.inherits(Readable, Stream);
 
-		constructor: Texture,
+var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
 
-		isTexture: true,
+function prependListener(emitter, event, fn) {
+  // Sadly this is not cacheable as some libraries bundle their own
+  // event emitter implementation with them.
+  if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn);
 
-		updateMatrix: function () {
+  // This is a hack to make sure that our error handler is attached before any
+  // userland ones.  NEVER DO THIS. This is here only because this code needs
+  // to continue to work with older versions of Node.js that do not include
+  // the prependListener() method. The goal is to eventually remove this hack.
+  if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
+}
 
-			this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
+function ReadableState(options, stream) {
+  Duplex = Duplex || require('./_stream_duplex');
 
-		},
+  options = options || {};
 
-		clone: function () {
+  // Duplex streams are both readable and writable, but share
+  // the same options object.
+  // However, some cases require setting options to different
+  // values for the readable and the writable sides of the duplex stream.
+  // These options can be provided separately as readableXXX and writableXXX.
+  var isDuplex = stream instanceof Duplex;
 
-			return new this.constructor().copy( this );
+  // object stream flag. Used to make read(n) ignore n and to
+  // make all the buffer merging and length checks go away
+  this.objectMode = !!options.objectMode;
 
-		},
+  if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
 
-		copy: function ( source ) {
+  // the point at which it stops calling _read() to fill the buffer
+  // Note: 0 is a valid value, means "don't call _read preemptively ever"
+  var hwm = options.highWaterMark;
+  var readableHwm = options.readableHighWaterMark;
+  var defaultHwm = this.objectMode ? 16 : 16 * 1024;
 
-			this.name = source.name;
+  if (hwm || hwm === 0) this.highWaterMark = hwm;else if (isDuplex && (readableHwm || readableHwm === 0)) this.highWaterMark = readableHwm;else this.highWaterMark = defaultHwm;
 
-			this.image = source.image;
-			this.mipmaps = source.mipmaps.slice( 0 );
+  // cast to ints.
+  this.highWaterMark = Math.floor(this.highWaterMark);
 
-			this.mapping = source.mapping;
+  // A linked list is used to store data chunks instead of an array because the
+  // linked list can remove elements from the beginning faster than
+  // array.shift()
+  this.buffer = new BufferList();
+  this.length = 0;
+  this.pipes = null;
+  this.pipesCount = 0;
+  this.flowing = null;
+  this.ended = false;
+  this.endEmitted = false;
+  this.reading = false;
 
-			this.wrapS = source.wrapS;
-			this.wrapT = source.wrapT;
+  // a flag to be able to tell if the event 'readable'/'data' is emitted
+  // immediately, or on a later tick.  We set this to true at first, because
+  // any actions that shouldn't happen until "later" should generally also
+  // not happen before the first read call.
+  this.sync = true;
 
-			this.magFilter = source.magFilter;
-			this.minFilter = source.minFilter;
+  // whenever we return null, then we set a flag to say
+  // that we're awaiting a 'readable' event emission.
+  this.needReadable = false;
+  this.emittedReadable = false;
+  this.readableListening = false;
+  this.resumeScheduled = false;
 
-			this.anisotropy = source.anisotropy;
+  // has it been destroyed
+  this.destroyed = false;
 
-			this.format = source.format;
-			this.internalFormat = source.internalFormat;
-			this.type = source.type;
+  // Crypto is kind of old and crusty.  Historically, its default string
+  // encoding is 'binary' so we have to make this configurable.
+  // Everything else in the universe uses 'utf8', though.
+  this.defaultEncoding = options.defaultEncoding || 'utf8';
 
-			this.offset.copy( source.offset );
-			this.repeat.copy( source.repeat );
-			this.center.copy( source.center );
-			this.rotation = source.rotation;
+  // the number of writers that are awaiting a drain event in .pipe()s
+  this.awaitDrain = 0;
 
-			this.matrixAutoUpdate = source.matrixAutoUpdate;
-			this.matrix.copy( source.matrix );
+  // if true, a maybeReadMore has been scheduled
+  this.readingMore = false;
 
-			this.generateMipmaps = source.generateMipmaps;
-			this.premultiplyAlpha = source.premultiplyAlpha;
-			this.flipY = source.flipY;
-			this.unpackAlignment = source.unpackAlignment;
-			this.encoding = source.encoding;
+  this.decoder = null;
+  this.encoding = null;
+  if (options.encoding) {
+    if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
+    this.decoder = new StringDecoder(options.encoding);
+    this.encoding = options.encoding;
+  }
+}
 
-			return this;
+function Readable(options) {
+  Duplex = Duplex || require('./_stream_duplex');
 
-		},
+  if (!(this instanceof Readable)) return new Readable(options);
 
-		toJSON: function ( meta ) {
+  this._readableState = new ReadableState(options, this);
 
-			var isRootObject = ( meta === undefined || typeof meta === 'string' );
+  // legacy
+  this.readable = true;
 
-			if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
+  if (options) {
+    if (typeof options.read === 'function') this._read = options.read;
 
-				return meta.textures[ this.uuid ];
+    if (typeof options.destroy === 'function') this._destroy = options.destroy;
+  }
 
-			}
+  Stream.call(this);
+}
 
-			var output = {
+Object.defineProperty(Readable.prototype, 'destroyed', {
+  get: function () {
+    if (this._readableState === undefined) {
+      return false;
+    }
+    return this._readableState.destroyed;
+  },
+  set: function (value) {
+    // we ignore the value if the stream
+    // has not been initialized yet
+    if (!this._readableState) {
+      return;
+    }
 
-				metadata: {
-					version: 4.5,
-					type: 'Texture',
-					generator: 'Texture.toJSON'
-				},
+    // backward compatibility, the user is explicitly
+    // managing destroyed
+    this._readableState.destroyed = value;
+  }
+});
 
-				uuid: this.uuid,
-				name: this.name,
+Readable.prototype.destroy = destroyImpl.destroy;
+Readable.prototype._undestroy = destroyImpl.undestroy;
+Readable.prototype._destroy = function (err, cb) {
+  this.push(null);
+  cb(err);
+};
 
-				mapping: this.mapping,
+// Manually shove something into the read() buffer.
+// This returns true if the highWaterMark has not been hit yet,
+// similar to how Writable.write() returns true if you should
+// write() some more.
+Readable.prototype.push = function (chunk, encoding) {
+  var state = this._readableState;
+  var skipChunkCheck;
 
-				repeat: [ this.repeat.x, this.repeat.y ],
-				offset: [ this.offset.x, this.offset.y ],
-				center: [ this.center.x, this.center.y ],
-				rotation: this.rotation,
+  if (!state.objectMode) {
+    if (typeof chunk === 'string') {
+      encoding = encoding || state.defaultEncoding;
+      if (encoding !== state.encoding) {
+        chunk = Buffer.from(chunk, encoding);
+        encoding = '';
+      }
+      skipChunkCheck = true;
+    }
+  } else {
+    skipChunkCheck = true;
+  }
 
-				wrap: [ this.wrapS, this.wrapT ],
+  return readableAddChunk(this, chunk, encoding, false, skipChunkCheck);
+};
 
-				format: this.format,
-				type: this.type,
-				encoding: this.encoding,
+// Unshift should *always* be something directly out of read()
+Readable.prototype.unshift = function (chunk) {
+  return readableAddChunk(this, chunk, null, true, false);
+};
 
-				minFilter: this.minFilter,
-				magFilter: this.magFilter,
-				anisotropy: this.anisotropy,
+function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
+  var state = stream._readableState;
+  if (chunk === null) {
+    state.reading = false;
+    onEofChunk(stream, state);
+  } else {
+    var er;
+    if (!skipChunkCheck) er = chunkInvalid(state, chunk);
+    if (er) {
+      stream.emit('error', er);
+    } else if (state.objectMode || chunk && chunk.length > 0) {
+      if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
+        chunk = _uint8ArrayToBuffer(chunk);
+      }
 
-				flipY: this.flipY,
+      if (addToFront) {
+        if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event'));else addChunk(stream, state, chunk, true);
+      } else if (state.ended) {
+        stream.emit('error', new Error('stream.push() after EOF'));
+      } else {
+        state.reading = false;
+        if (state.decoder && !encoding) {
+          chunk = state.decoder.write(chunk);
+          if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
+        } else {
+          addChunk(stream, state, chunk, false);
+        }
+      }
+    } else if (!addToFront) {
+      state.reading = false;
+    }
+  }
 
-				premultiplyAlpha: this.premultiplyAlpha,
-				unpackAlignment: this.unpackAlignment
+  return needMoreData(state);
+}
 
-			};
+function addChunk(stream, state, chunk, addToFront) {
+  if (state.flowing && state.length === 0 && !state.sync) {
+    stream.emit('data', chunk);
+    stream.read(0);
+  } else {
+    // update the buffer info.
+    state.length += state.objectMode ? 1 : chunk.length;
+    if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
 
-			if ( this.image !== undefined ) {
+    if (state.needReadable) emitReadable(stream);
+  }
+  maybeReadMore(stream, state);
+}
 
-				// TODO: Move to THREE.Image
+function chunkInvalid(state, chunk) {
+  var er;
+  if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
+    er = new TypeError('Invalid non-string/buffer chunk');
+  }
+  return er;
+}
 
-				var image = this.image;
+// if it's past the high water mark, we can push in some more.
+// Also, if we have no data yet, we can stand some
+// more bytes.  This is to work around cases where hwm=0,
+// such as the repl.  Also, if the push() triggered a
+// readable event, and the user called read(largeNumber) such that
+// needReadable was set, then we ought to push more, so that another
+// 'readable' event will be triggered.
+function needMoreData(state) {
+  return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
+}
 
-				if ( image.uuid === undefined ) {
+Readable.prototype.isPaused = function () {
+  return this._readableState.flowing === false;
+};
 
-					image.uuid = MathUtils.generateUUID(); // UGH
+// backwards compatibility.
+Readable.prototype.setEncoding = function (enc) {
+  if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
+  this._readableState.decoder = new StringDecoder(enc);
+  this._readableState.encoding = enc;
+  return this;
+};
 
-				}
+// Don't raise the hwm > 8MB
+var MAX_HWM = 0x800000;
+function computeNewHighWaterMark(n) {
+  if (n >= MAX_HWM) {
+    n = MAX_HWM;
+  } else {
+    // Get the next highest power of 2 to prevent increasing hwm excessively in
+    // tiny amounts
+    n--;
+    n |= n >>> 1;
+    n |= n >>> 2;
+    n |= n >>> 4;
+    n |= n >>> 8;
+    n |= n >>> 16;
+    n++;
+  }
+  return n;
+}
 
-				if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
+// This function is designed to be inlinable, so please take care when making
+// changes to the function body.
+function howMuchToRead(n, state) {
+  if (n <= 0 || state.length === 0 && state.ended) return 0;
+  if (state.objectMode) return 1;
+  if (n !== n) {
+    // Only flow one buffer at a time
+    if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
+  }
+  // If we're asking for more than the current hwm, then raise the hwm.
+  if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
+  if (n <= state.length) return n;
+  // Don't have enough
+  if (!state.ended) {
+    state.needReadable = true;
+    return 0;
+  }
+  return state.length;
+}
 
-					var url;
+// you can override either this method, or the async _read(n) below.
+Readable.prototype.read = function (n) {
+  debug('read', n);
+  n = parseInt(n, 10);
+  var state = this._readableState;
+  var nOrig = n;
 
-					if ( Array.isArray( image ) ) {
+  if (n !== 0) state.emittedReadable = false;
 
-						// process array of images e.g. CubeTexture
+  // if we're doing read(0) to trigger a readable event, but we
+  // already have a bunch of data in the buffer, then just trigger
+  // the 'readable' event and move on.
+  if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
+    debug('read: emitReadable', state.length, state.ended);
+    if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
+    return null;
+  }
 
-						url = [];
+  n = howMuchToRead(n, state);
 
-						for ( var i = 0, l = image.length; i < l; i ++ ) {
+  // if we've ended, and we're now clear, then finish it up.
+  if (n === 0 && state.ended) {
+    if (state.length === 0) endReadable(this);
+    return null;
+  }
 
-							url.push( ImageUtils.getDataURL( image[ i ] ) );
-
-						}
-
-					} else {
+  // All the actual chunk generation logic needs to be
+  // *below* the call to _read.  The reason is that in certain
+  // synthetic stream cases, such as passthrough streams, _read
+  // may be a completely synchronous operation which may change
+  // the state of the read buffer, providing enough data when
+  // before there was *not* enough.
+  //
+  // So, the steps are:
+  // 1. Figure out what the state of things will be after we do
+  // a read from the buffer.
+  //
+  // 2. If that resulting state will trigger a _read, then call _read.
+  // Note that this may be asynchronous, or synchronous.  Yes, it is
+  // deeply ugly to write APIs this way, but that still doesn't mean
+  // that the Readable class should behave improperly, as streams are
+  // designed to be sync/async agnostic.
+  // Take note if the _read call is sync or async (ie, if the read call
+  // has returned yet), so that we know whether or not it's safe to emit
+  // 'readable' etc.
+  //
+  // 3. Actually pull the requested chunks out of the buffer and return.
 
-						// process single image
+  // if we need a readable event, then we need to do some reading.
+  var doRead = state.needReadable;
+  debug('need readable', doRead);
 
-						url = ImageUtils.getDataURL( image );
+  // if we currently have less than the highWaterMark, then also read some
+  if (state.length === 0 || state.length - n < state.highWaterMark) {
+    doRead = true;
+    debug('length less than watermark', doRead);
+  }
 
-					}
+  // however, if we've ended, then there's no point, and if we're already
+  // reading, then it's unnecessary.
+  if (state.ended || state.reading) {
+    doRead = false;
+    debug('reading or ended', doRead);
+  } else if (doRead) {
+    debug('do read');
+    state.reading = true;
+    state.sync = true;
+    // if the length is currently zero, then we *need* a readable event.
+    if (state.length === 0) state.needReadable = true;
+    // call internal read method
+    this._read(state.highWaterMark);
+    state.sync = false;
+    // If _read pushed data synchronously, then `reading` will be false,
+    // and we need to re-evaluate how much data we can return to the user.
+    if (!state.reading) n = howMuchToRead(nOrig, state);
+  }
 
-					meta.images[ image.uuid ] = {
-						uuid: image.uuid,
-						url: url
-					};
+  var ret;
+  if (n > 0) ret = fromList(n, state);else ret = null;
 
-				}
+  if (ret === null) {
+    state.needReadable = true;
+    n = 0;
+  } else {
+    state.length -= n;
+  }
 
-				output.image = image.uuid;
+  if (state.length === 0) {
+    // If we have nothing in the buffer, then we want to know
+    // as soon as we *do* get something into the buffer.
+    if (!state.ended) state.needReadable = true;
 
-			}
+    // If we tried to read() past the EOF, then emit end on the next tick.
+    if (nOrig !== n && state.ended) endReadable(this);
+  }
 
-			if ( ! isRootObject ) {
+  if (ret !== null) this.emit('data', ret);
 
-				meta.textures[ this.uuid ] = output;
+  return ret;
+};
 
-			}
+function onEofChunk(stream, state) {
+  if (state.ended) return;
+  if (state.decoder) {
+    var chunk = state.decoder.end();
+    if (chunk && chunk.length) {
+      state.buffer.push(chunk);
+      state.length += state.objectMode ? 1 : chunk.length;
+    }
+  }
+  state.ended = true;
 
-			return output;
+  // emit 'readable' now to make sure it gets picked up.
+  emitReadable(stream);
+}
 
-		},
+// Don't emit readable right away in sync mode, because this can trigger
+// another read() call => stack overflow.  This way, it might trigger
+// a nextTick recursion warning, but that's not so bad.
+function emitReadable(stream) {
+  var state = stream._readableState;
+  state.needReadable = false;
+  if (!state.emittedReadable) {
+    debug('emitReadable', state.flowing);
+    state.emittedReadable = true;
+    if (state.sync) pna.nextTick(emitReadable_, stream);else emitReadable_(stream);
+  }
+}
 
-		dispose: function () {
+function emitReadable_(stream) {
+  debug('emit readable');
+  stream.emit('readable');
+  flow(stream);
+}
 
-			this.dispatchEvent( { type: 'dispose' } );
+// at this point, the user has presumably seen the 'readable' event,
+// and called read() to consume some data.  that may have triggered
+// in turn another _read(n) call, in which case reading = true if
+// it's in progress.
+// However, if we're not ended, or reading, and the length < hwm,
+// then go ahead and try to read some more preemptively.
+function maybeReadMore(stream, state) {
+  if (!state.readingMore) {
+    state.readingMore = true;
+    pna.nextTick(maybeReadMore_, stream, state);
+  }
+}
 
-		},
+function maybeReadMore_(stream, state) {
+  var len = state.length;
+  while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
+    debug('maybeReadMore read 0');
+    stream.read(0);
+    if (len === state.length)
+      // didn't get any data, stop spinning.
+      break;else len = state.length;
+  }
+  state.readingMore = false;
+}
 
-		transformUv: function ( uv ) {
+// abstract method.  to be overridden in specific implementation classes.
+// call cb(er, data) where data is <= n in length.
+// for virtual (non-string, non-buffer) streams, "length" is somewhat
+// arbitrary, and perhaps not very meaningful.
+Readable.prototype._read = function (n) {
+  this.emit('error', new Error('_read() is not implemented'));
+};
 
-			if ( this.mapping !== UVMapping ) { return uv; }
+Readable.prototype.pipe = function (dest, pipeOpts) {
+  var src = this;
+  var state = this._readableState;
 
-			uv.applyMatrix3( this.matrix );
+  switch (state.pipesCount) {
+    case 0:
+      state.pipes = dest;
+      break;
+    case 1:
+      state.pipes = [state.pipes, dest];
+      break;
+    default:
+      state.pipes.push(dest);
+      break;
+  }
+  state.pipesCount += 1;
+  debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
 
-			if ( uv.x < 0 || uv.x > 1 ) {
+  var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
 
-				switch ( this.wrapS ) {
+  var endFn = doEnd ? onend : unpipe;
+  if (state.endEmitted) pna.nextTick(endFn);else src.once('end', endFn);
 
-					case RepeatWrapping:
+  dest.on('unpipe', onunpipe);
+  function onunpipe(readable, unpipeInfo) {
+    debug('onunpipe');
+    if (readable === src) {
+      if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
+        unpipeInfo.hasUnpiped = true;
+        cleanup();
+      }
+    }
+  }
 
-						uv.x = uv.x - Math.floor( uv.x );
-						break;
+  function onend() {
+    debug('onend');
+    dest.end();
+  }
 
-					case ClampToEdgeWrapping:
+  // when the dest drains, it reduces the awaitDrain counter
+  // on the source.  This would be more elegant with a .once()
+  // handler in flow(), but adding and removing repeatedly is
+  // too slow.
+  var ondrain = pipeOnDrain(src);
+  dest.on('drain', ondrain);
 
-						uv.x = uv.x < 0 ? 0 : 1;
-						break;
+  var cleanedUp = false;
+  function cleanup() {
+    debug('cleanup');
+    // cleanup event handlers once the pipe is broken
+    dest.removeListener('close', onclose);
+    dest.removeListener('finish', onfinish);
+    dest.removeListener('drain', ondrain);
+    dest.removeListener('error', onerror);
+    dest.removeListener('unpipe', onunpipe);
+    src.removeListener('end', onend);
+    src.removeListener('end', unpipe);
+    src.removeListener('data', ondata);
 
-					case MirroredRepeatWrapping:
+    cleanedUp = true;
 
-						if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
+    // if the reader is waiting for a drain event from this
+    // specific writer, then it would cause it to never start
+    // flowing again.
+    // So, if this is awaiting a drain, then we just call it now.
+    // If we don't know, then assume that we are waiting for one.
+    if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
+  }
 
-							uv.x = Math.ceil( uv.x ) - uv.x;
+  // If the user pushes more data while we're writing to dest then we'll end up
+  // in ondata again. However, we only want to increase awaitDrain once because
+  // dest will only emit one 'drain' event for the multiple writes.
+  // => Introduce a guard on increasing awaitDrain.
+  var increasedAwaitDrain = false;
+  src.on('data', ondata);
+  function ondata(chunk) {
+    debug('ondata');
+    increasedAwaitDrain = false;
+    var ret = dest.write(chunk);
+    if (false === ret && !increasedAwaitDrain) {
+      // If the user unpiped during `dest.write()`, it is possible
+      // to get stuck in a permanently paused state if that write
+      // also returned false.
+      // => Check whether `dest` is still a piping destination.
+      if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
+        debug('false write response, pause', src._readableState.awaitDrain);
+        src._readableState.awaitDrain++;
+        increasedAwaitDrain = true;
+      }
+      src.pause();
+    }
+  }
 
-						} else {
+  // if the dest has an error, then stop piping into it.
+  // however, don't suppress the throwing behavior for this.
+  function onerror(er) {
+    debug('onerror', er);
+    unpipe();
+    dest.removeListener('error', onerror);
+    if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
+  }
 
-							uv.x = uv.x - Math.floor( uv.x );
+  // Make sure our error handler is attached before userland ones.
+  prependListener(dest, 'error', onerror);
 
-						}
+  // Both close and finish should trigger unpipe, but only once.
+  function onclose() {
+    dest.removeListener('finish', onfinish);
+    unpipe();
+  }
+  dest.once('close', onclose);
+  function onfinish() {
+    debug('onfinish');
+    dest.removeListener('close', onclose);
+    unpipe();
+  }
+  dest.once('finish', onfinish);
 
-						break;
+  function unpipe() {
+    debug('unpipe');
+    src.unpipe(dest);
+  }
 
-				}
+  // tell the dest that it's being piped to
+  dest.emit('pipe', src);
 
-			}
+  // start the flow if it hasn't been started already.
+  if (!state.flowing) {
+    debug('pipe resume');
+    src.resume();
+  }
 
-			if ( uv.y < 0 || uv.y > 1 ) {
+  return dest;
+};
 
-				switch ( this.wrapT ) {
+function pipeOnDrain(src) {
+  return function () {
+    var state = src._readableState;
+    debug('pipeOnDrain', state.awaitDrain);
+    if (state.awaitDrain) state.awaitDrain--;
+    if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
+      state.flowing = true;
+      flow(src);
+    }
+  };
+}
 
-					case RepeatWrapping:
+Readable.prototype.unpipe = function (dest) {
+  var state = this._readableState;
+  var unpipeInfo = { hasUnpiped: false };
 
-						uv.y = uv.y - Math.floor( uv.y );
-						break;
+  // if we're not piping anywhere, then do nothing.
+  if (state.pipesCount === 0) return this;
 
-					case ClampToEdgeWrapping:
+  // just one destination.  most common case.
+  if (state.pipesCount === 1) {
+    // passed in one, but it's not the right one.
+    if (dest && dest !== state.pipes) return this;
 
-						uv.y = uv.y < 0 ? 0 : 1;
-						break;
+    if (!dest) dest = state.pipes;
 
-					case MirroredRepeatWrapping:
+    // got a match.
+    state.pipes = null;
+    state.pipesCount = 0;
+    state.flowing = false;
+    if (dest) dest.emit('unpipe', this, unpipeInfo);
+    return this;
+  }
 
-						if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
+  // slow case. multiple pipe destinations.
 
-							uv.y = Math.ceil( uv.y ) - uv.y;
+  if (!dest) {
+    // remove all.
+    var dests = state.pipes;
+    var len = state.pipesCount;
+    state.pipes = null;
+    state.pipesCount = 0;
+    state.flowing = false;
 
-						} else {
+    for (var i = 0; i < len; i++) {
+      dests[i].emit('unpipe', this, unpipeInfo);
+    }return this;
+  }
 
-							uv.y = uv.y - Math.floor( uv.y );
+  // try to find the right one.
+  var index = indexOf(state.pipes, dest);
+  if (index === -1) return this;
 
-						}
+  state.pipes.splice(index, 1);
+  state.pipesCount -= 1;
+  if (state.pipesCount === 1) state.pipes = state.pipes[0];
 
-						break;
+  dest.emit('unpipe', this, unpipeInfo);
 
-				}
+  return this;
+};
 
-			}
+// set up data events if they are asked for
+// Ensure readable listeners eventually get something
+Readable.prototype.on = function (ev, fn) {
+  var res = Stream.prototype.on.call(this, ev, fn);
 
-			if ( this.flipY ) {
+  if (ev === 'data') {
+    // Start flowing on next tick if stream isn't explicitly paused
+    if (this._readableState.flowing !== false) this.resume();
+  } else if (ev === 'readable') {
+    var state = this._readableState;
+    if (!state.endEmitted && !state.readableListening) {
+      state.readableListening = state.needReadable = true;
+      state.emittedReadable = false;
+      if (!state.reading) {
+        pna.nextTick(nReadingNextTick, this);
+      } else if (state.length) {
+        emitReadable(this);
+      }
+    }
+  }
 
-				uv.y = 1 - uv.y;
+  return res;
+};
+Readable.prototype.addListener = Readable.prototype.on;
 
-			}
+function nReadingNextTick(self) {
+  debug('readable nexttick read 0');
+  self.read(0);
+}
 
-			return uv;
+// pause() and resume() are remnants of the legacy readable stream API
+// If the user uses them, then switch into old mode.
+Readable.prototype.resume = function () {
+  var state = this._readableState;
+  if (!state.flowing) {
+    debug('resume');
+    state.flowing = true;
+    resume(this, state);
+  }
+  return this;
+};
 
-		}
+function resume(stream, state) {
+  if (!state.resumeScheduled) {
+    state.resumeScheduled = true;
+    pna.nextTick(resume_, stream, state);
+  }
+}
 
-	} );
+function resume_(stream, state) {
+  if (!state.reading) {
+    debug('resume read 0');
+    stream.read(0);
+  }
 
-	Object.defineProperty( Texture.prototype, "needsUpdate", {
+  state.resumeScheduled = false;
+  state.awaitDrain = 0;
+  stream.emit('resume');
+  flow(stream);
+  if (state.flowing && !state.reading) stream.read(0);
+}
 
-		set: function ( value ) {
+Readable.prototype.pause = function () {
+  debug('call pause flowing=%j', this._readableState.flowing);
+  if (false !== this._readableState.flowing) {
+    debug('pause');
+    this._readableState.flowing = false;
+    this.emit('pause');
+  }
+  return this;
+};
 
-			if ( value === true ) { this.version ++; }
+function flow(stream) {
+  var state = stream._readableState;
+  debug('flow', state.flowing);
+  while (state.flowing && stream.read() !== null) {}
+}
 
-		}
+// wrap an old-style stream as the async data source.
+// This is *not* part of the readable stream interface.
+// It is an ugly unfortunate mess of history.
+Readable.prototype.wrap = function (stream) {
+  var _this = this;
 
-	} );
+  var state = this._readableState;
+  var paused = false;
 
-	/**
-	 * @author supereggbert / http://www.paulbrunt.co.uk/
-	 * @author philogb / http://blog.thejit.org/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author egraether / http://egraether.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+  stream.on('end', function () {
+    debug('wrapped end');
+    if (state.decoder && !state.ended) {
+      var chunk = state.decoder.end();
+      if (chunk && chunk.length) _this.push(chunk);
+    }
 
-	function Vector4( x, y, z, w ) {
+    _this.push(null);
+  });
 
-		this.x = x || 0;
-		this.y = y || 0;
-		this.z = z || 0;
-		this.w = ( w !== undefined ) ? w : 1;
+  stream.on('data', function (chunk) {
+    debug('wrapped data');
+    if (state.decoder) chunk = state.decoder.write(chunk);
 
-	}
+    // don't skip over falsy values in objectMode
+    if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
 
-	Object.defineProperties( Vector4.prototype, {
+    var ret = _this.push(chunk);
+    if (!ret) {
+      paused = true;
+      stream.pause();
+    }
+  });
 
-		"width": {
+  // proxy all the other methods.
+  // important when wrapping filters and duplexes.
+  for (var i in stream) {
+    if (this[i] === undefined && typeof stream[i] === 'function') {
+      this[i] = function (method) {
+        return function () {
+          return stream[method].apply(stream, arguments);
+        };
+      }(i);
+    }
+  }
 
-			get: function () {
+  // proxy certain important events.
+  for (var n = 0; n < kProxyEvents.length; n++) {
+    stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
+  }
 
-				return this.z;
+  // when we try to consume some more bytes, simply unpause the
+  // underlying stream.
+  this._read = function (n) {
+    debug('wrapped _read', n);
+    if (paused) {
+      paused = false;
+      stream.resume();
+    }
+  };
 
-			},
+  return this;
+};
 
-			set: function ( value ) {
+Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
+  // making it explicit this property is not enumerable
+  // because otherwise some prototype manipulation in
+  // userland will fail
+  enumerable: false,
+  get: function () {
+    return this._readableState.highWaterMark;
+  }
+});
 
-				this.z = value;
+// exposed for testing purposes only.
+Readable._fromList = fromList;
 
-			}
-
-		},
-
-		"height": {
+// Pluck off n bytes from an array of buffers.
+// Length is the combined lengths of all the buffers in the list.
+// This function is designed to be inlinable, so please take care when making
+// changes to the function body.
+function fromList(n, state) {
+  // nothing buffered
+  if (state.length === 0) return null;
 
-			get: function () {
+  var ret;
+  if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
+    // read it all, truncate the list
+    if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length);
+    state.buffer.clear();
+  } else {
+    // read part of list
+    ret = fromListPartial(n, state.buffer, state.decoder);
+  }
 
-				return this.w;
+  return ret;
+}
 
-			},
+// Extracts only enough buffered data to satisfy the amount requested.
+// This function is designed to be inlinable, so please take care when making
+// changes to the function body.
+function fromListPartial(n, list, hasStrings) {
+  var ret;
+  if (n < list.head.data.length) {
+    // slice is the same for buffers and strings
+    ret = list.head.data.slice(0, n);
+    list.head.data = list.head.data.slice(n);
+  } else if (n === list.head.data.length) {
+    // first chunk is a perfect match
+    ret = list.shift();
+  } else {
+    // result spans more than one buffer
+    ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
+  }
+  return ret;
+}
 
-			set: function ( value ) {
+// Copies a specified amount of characters from the list of buffered data
+// chunks.
+// This function is designed to be inlinable, so please take care when making
+// changes to the function body.
+function copyFromBufferString(n, list) {
+  var p = list.head;
+  var c = 1;
+  var ret = p.data;
+  n -= ret.length;
+  while (p = p.next) {
+    var str = p.data;
+    var nb = n > str.length ? str.length : n;
+    if (nb === str.length) ret += str;else ret += str.slice(0, n);
+    n -= nb;
+    if (n === 0) {
+      if (nb === str.length) {
+        ++c;
+        if (p.next) list.head = p.next;else list.head = list.tail = null;
+      } else {
+        list.head = p;
+        p.data = str.slice(nb);
+      }
+      break;
+    }
+    ++c;
+  }
+  list.length -= c;
+  return ret;
+}
 
-				this.w = value;
+// Copies a specified amount of bytes from the list of buffered data chunks.
+// This function is designed to be inlinable, so please take care when making
+// changes to the function body.
+function copyFromBuffer(n, list) {
+  var ret = Buffer.allocUnsafe(n);
+  var p = list.head;
+  var c = 1;
+  p.data.copy(ret);
+  n -= p.data.length;
+  while (p = p.next) {
+    var buf = p.data;
+    var nb = n > buf.length ? buf.length : n;
+    buf.copy(ret, ret.length - n, 0, nb);
+    n -= nb;
+    if (n === 0) {
+      if (nb === buf.length) {
+        ++c;
+        if (p.next) list.head = p.next;else list.head = list.tail = null;
+      } else {
+        list.head = p;
+        p.data = buf.slice(nb);
+      }
+      break;
+    }
+    ++c;
+  }
+  list.length -= c;
+  return ret;
+}
 
-			}
+function endReadable(stream) {
+  var state = stream._readableState;
 
-		}
+  // If we get here before consuming all the bytes, then that is a
+  // bug in node.  Should never happen.
+  if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
 
-	} );
+  if (!state.endEmitted) {
+    state.ended = true;
+    pna.nextTick(endReadableNT, state, stream);
+  }
+}
 
-	Object.assign( Vector4.prototype, {
+function endReadableNT(state, stream) {
+  // Check that we didn't get one last unshift.
+  if (!state.endEmitted && state.length === 0) {
+    state.endEmitted = true;
+    stream.readable = false;
+    stream.emit('end');
+  }
+}
 
-		isVector4: true,
+function indexOf(xs, x) {
+  for (var i = 0, l = xs.length; i < l; i++) {
+    if (xs[i] === x) return i;
+  }
+  return -1;
+}
+}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
+},{"./_stream_duplex":64,"./internal/streams/BufferList":69,"./internal/streams/destroy":70,"./internal/streams/stream":71,"_process":63,"core-util-is":9,"events":10,"inherits":12,"isarray":14,"process-nextick-args":62,"safe-buffer":74,"string_decoder/":75,"util":7}],67:[function(require,module,exports){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-		set: function ( x, y, z, w ) {
+// a transform stream is a readable/writable stream where you do
+// something with the data.  Sometimes it's called a "filter",
+// but that's not a great name for it, since that implies a thing where
+// some bits pass through, and others are simply ignored.  (That would
+// be a valid example of a transform, of course.)
+//
+// While the output is causally related to the input, it's not a
+// necessarily symmetric or synchronous transformation.  For example,
+// a zlib stream might take multiple plain-text writes(), and then
+// emit a single compressed chunk some time in the future.
+//
+// Here's how this works:
+//
+// The Transform stream has all the aspects of the readable and writable
+// stream classes.  When you write(chunk), that calls _write(chunk,cb)
+// internally, and returns false if there's a lot of pending writes
+// buffered up.  When you call read(), that calls _read(n) until
+// there's enough pending readable data buffered up.
+//
+// In a transform stream, the written data is placed in a buffer.  When
+// _read(n) is called, it transforms the queued up data, calling the
+// buffered _write cb's as it consumes chunks.  If consuming a single
+// written chunk would result in multiple output chunks, then the first
+// outputted bit calls the readcb, and subsequent chunks just go into
+// the read buffer, and will cause it to emit 'readable' if necessary.
+//
+// This way, back-pressure is actually determined by the reading side,
+// since _read has to be called to start processing a new chunk.  However,
+// a pathological inflate type of transform can cause excessive buffering
+// here.  For example, imagine a stream where every byte of input is
+// interpreted as an integer from 0-255, and then results in that many
+// bytes of output.  Writing the 4 bytes {ff,ff,ff,ff} would result in
+// 1kb of data being output.  In this case, you could write a very small
+// amount of input, and end up with a very large amount of output.  In
+// such a pathological inflating mechanism, there'd be no way to tell
+// the system to stop doing the transform.  A single 4MB write could
+// cause the system to run out of memory.
+//
+// However, even in such a pathological case, only a single written chunk
+// would be consumed, and then the rest would wait (un-transformed) until
+// the results of the previous transformed chunk were consumed.
 
-			this.x = x;
-			this.y = y;
-			this.z = z;
-			this.w = w;
+'use strict';
 
-			return this;
+module.exports = Transform;
 
-		},
+var Duplex = require('./_stream_duplex');
 
-		setScalar: function ( scalar ) {
+/*<replacement>*/
+var util = Object.create(require('core-util-is'));
+util.inherits = require('inherits');
+/*</replacement>*/
 
-			this.x = scalar;
-			this.y = scalar;
-			this.z = scalar;
-			this.w = scalar;
+util.inherits(Transform, Duplex);
 
-			return this;
+function afterTransform(er, data) {
+  var ts = this._transformState;
+  ts.transforming = false;
 
-		},
+  var cb = ts.writecb;
 
-		setX: function ( x ) {
+  if (!cb) {
+    return this.emit('error', new Error('write callback called multiple times'));
+  }
 
-			this.x = x;
+  ts.writechunk = null;
+  ts.writecb = null;
 
-			return this;
+  if (data != null) // single equals check for both `null` and `undefined`
+    this.push(data);
 
-		},
+  cb(er);
 
-		setY: function ( y ) {
+  var rs = this._readableState;
+  rs.reading = false;
+  if (rs.needReadable || rs.length < rs.highWaterMark) {
+    this._read(rs.highWaterMark);
+  }
+}
 
-			this.y = y;
+function Transform(options) {
+  if (!(this instanceof Transform)) return new Transform(options);
 
-			return this;
+  Duplex.call(this, options);
 
-		},
+  this._transformState = {
+    afterTransform: afterTransform.bind(this),
+    needTransform: false,
+    transforming: false,
+    writecb: null,
+    writechunk: null,
+    writeencoding: null
+  };
 
-		setZ: function ( z ) {
+  // start out asking for a readable event once data is transformed.
+  this._readableState.needReadable = true;
 
-			this.z = z;
+  // we have implemented the _read method, and done the other things
+  // that Readable wants before the first _read call, so unset the
+  // sync guard flag.
+  this._readableState.sync = false;
 
-			return this;
+  if (options) {
+    if (typeof options.transform === 'function') this._transform = options.transform;
 
-		},
+    if (typeof options.flush === 'function') this._flush = options.flush;
+  }
 
-		setW: function ( w ) {
+  // When the writable side finishes, then flush out anything remaining.
+  this.on('prefinish', prefinish);
+}
 
-			this.w = w;
+function prefinish() {
+  var _this = this;
 
-			return this;
+  if (typeof this._flush === 'function') {
+    this._flush(function (er, data) {
+      done(_this, er, data);
+    });
+  } else {
+    done(this, null, null);
+  }
+}
 
-		},
+Transform.prototype.push = function (chunk, encoding) {
+  this._transformState.needTransform = false;
+  return Duplex.prototype.push.call(this, chunk, encoding);
+};
 
-		setComponent: function ( index, value ) {
+// This is the part where you do stuff!
+// override this function in implementation classes.
+// 'chunk' is an input chunk.
+//
+// Call `push(newChunk)` to pass along transformed output
+// to the readable side.  You may call 'push' zero or more times.
+//
+// Call `cb(err)` when you are done with this chunk.  If you pass
+// an error, then that'll put the hurt on the whole operation.  If you
+// never call cb(), then you'll never get another chunk.
+Transform.prototype._transform = function (chunk, encoding, cb) {
+  throw new Error('_transform() is not implemented');
+};
 
-			switch ( index ) {
+Transform.prototype._write = function (chunk, encoding, cb) {
+  var ts = this._transformState;
+  ts.writecb = cb;
+  ts.writechunk = chunk;
+  ts.writeencoding = encoding;
+  if (!ts.transforming) {
+    var rs = this._readableState;
+    if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
+  }
+};
 
-				case 0: this.x = value; break;
-				case 1: this.y = value; break;
-				case 2: this.z = value; break;
-				case 3: this.w = value; break;
-				default: throw new Error( 'index is out of range: ' + index );
+// Doesn't matter what the args are here.
+// _transform does all the work.
+// That we got here means that the readable side wants more data.
+Transform.prototype._read = function (n) {
+  var ts = this._transformState;
 
-			}
+  if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
+    ts.transforming = true;
+    this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
+  } else {
+    // mark that we need a transform, so that any data that comes in
+    // will get processed, now that we've asked for it.
+    ts.needTransform = true;
+  }
+};
 
-			return this;
+Transform.prototype._destroy = function (err, cb) {
+  var _this2 = this;
 
-		},
+  Duplex.prototype._destroy.call(this, err, function (err2) {
+    cb(err2);
+    _this2.emit('close');
+  });
+};
 
-		getComponent: function ( index ) {
+function done(stream, er, data) {
+  if (er) return stream.emit('error', er);
 
-			switch ( index ) {
+  if (data != null) // single equals check for both `null` and `undefined`
+    stream.push(data);
 
-				case 0: return this.x;
-				case 1: return this.y;
-				case 2: return this.z;
-				case 3: return this.w;
-				default: throw new Error( 'index is out of range: ' + index );
+  // if there's nothing in the write buffer, then that means
+  // that nothing more will ever be provided
+  if (stream._writableState.length) throw new Error('Calling transform done when ws.length != 0');
 
-			}
+  if (stream._transformState.transforming) throw new Error('Calling transform done when still transforming');
 
-		},
+  return stream.push(null);
+}
+},{"./_stream_duplex":64,"core-util-is":9,"inherits":12}],68:[function(require,module,exports){
+(function (process,global,setImmediate){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-		clone: function () {
+// A bit simpler than readable streams.
+// Implement an async ._write(chunk, encoding, cb), and it'll handle all
+// the drain event emission and buffering.
 
-			return new this.constructor( this.x, this.y, this.z, this.w );
+'use strict';
 
-		},
+/*<replacement>*/
 
-		copy: function ( v ) {
+var pna = require('process-nextick-args');
+/*</replacement>*/
 
-			this.x = v.x;
-			this.y = v.y;
-			this.z = v.z;
-			this.w = ( v.w !== undefined ) ? v.w : 1;
+module.exports = Writable;
 
-			return this;
+/* <replacement> */
+function WriteReq(chunk, encoding, cb) {
+  this.chunk = chunk;
+  this.encoding = encoding;
+  this.callback = cb;
+  this.next = null;
+}
 
-		},
+// It seems a linked list but it is not
+// there will be only 2 of these for each stream
+function CorkedRequest(state) {
+  var _this = this;
 
-		add: function ( v, w ) {
+  this.next = null;
+  this.entry = null;
+  this.finish = function () {
+    onCorkedFinish(_this, state);
+  };
+}
+/* </replacement> */
 
-			if ( w !== undefined ) {
+/*<replacement>*/
+var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : pna.nextTick;
+/*</replacement>*/
 
-				console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
-				return this.addVectors( v, w );
+/*<replacement>*/
+var Duplex;
+/*</replacement>*/
 
-			}
+Writable.WritableState = WritableState;
 
-			this.x += v.x;
-			this.y += v.y;
-			this.z += v.z;
-			this.w += v.w;
+/*<replacement>*/
+var util = Object.create(require('core-util-is'));
+util.inherits = require('inherits');
+/*</replacement>*/
 
-			return this;
+/*<replacement>*/
+var internalUtil = {
+  deprecate: require('util-deprecate')
+};
+/*</replacement>*/
 
-		},
+/*<replacement>*/
+var Stream = require('./internal/streams/stream');
+/*</replacement>*/
 
-		addScalar: function ( s ) {
+/*<replacement>*/
 
-			this.x += s;
-			this.y += s;
-			this.z += s;
-			this.w += s;
+var Buffer = require('safe-buffer').Buffer;
+var OurUint8Array = global.Uint8Array || function () {};
+function _uint8ArrayToBuffer(chunk) {
+  return Buffer.from(chunk);
+}
+function _isUint8Array(obj) {
+  return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
+}
 
-			return this;
+/*</replacement>*/
 
-		},
+var destroyImpl = require('./internal/streams/destroy');
 
-		addVectors: function ( a, b ) {
+util.inherits(Writable, Stream);
 
-			this.x = a.x + b.x;
-			this.y = a.y + b.y;
-			this.z = a.z + b.z;
-			this.w = a.w + b.w;
+function nop() {}
 
-			return this;
+function WritableState(options, stream) {
+  Duplex = Duplex || require('./_stream_duplex');
 
-		},
+  options = options || {};
 
-		addScaledVector: function ( v, s ) {
+  // Duplex streams are both readable and writable, but share
+  // the same options object.
+  // However, some cases require setting options to different
+  // values for the readable and the writable sides of the duplex stream.
+  // These options can be provided separately as readableXXX and writableXXX.
+  var isDuplex = stream instanceof Duplex;
 
-			this.x += v.x * s;
-			this.y += v.y * s;
-			this.z += v.z * s;
-			this.w += v.w * s;
+  // object stream flag to indicate whether or not this stream
+  // contains buffers or objects.
+  this.objectMode = !!options.objectMode;
 
-			return this;
+  if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode;
 
-		},
+  // the point at which write() starts returning false
+  // Note: 0 is a valid value, means that we always return false if
+  // the entire buffer is not flushed immediately on write()
+  var hwm = options.highWaterMark;
+  var writableHwm = options.writableHighWaterMark;
+  var defaultHwm = this.objectMode ? 16 : 16 * 1024;
 
-		sub: function ( v, w ) {
+  if (hwm || hwm === 0) this.highWaterMark = hwm;else if (isDuplex && (writableHwm || writableHwm === 0)) this.highWaterMark = writableHwm;else this.highWaterMark = defaultHwm;
 
-			if ( w !== undefined ) {
+  // cast to ints.
+  this.highWaterMark = Math.floor(this.highWaterMark);
 
-				console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
-				return this.subVectors( v, w );
+  // if _final has been called
+  this.finalCalled = false;
 
-			}
+  // drain event flag.
+  this.needDrain = false;
+  // at the start of calling end()
+  this.ending = false;
+  // when end() has been called, and returned
+  this.ended = false;
+  // when 'finish' is emitted
+  this.finished = false;
 
-			this.x -= v.x;
-			this.y -= v.y;
-			this.z -= v.z;
-			this.w -= v.w;
+  // has it been destroyed
+  this.destroyed = false;
 
-			return this;
+  // should we decode strings into buffers before passing to _write?
+  // this is here so that some node-core streams can optimize string
+  // handling at a lower level.
+  var noDecode = options.decodeStrings === false;
+  this.decodeStrings = !noDecode;
 
-		},
+  // Crypto is kind of old and crusty.  Historically, its default string
+  // encoding is 'binary' so we have to make this configurable.
+  // Everything else in the universe uses 'utf8', though.
+  this.defaultEncoding = options.defaultEncoding || 'utf8';
 
-		subScalar: function ( s ) {
+  // not an actual buffer we keep track of, but a measurement
+  // of how much we're waiting to get pushed to some underlying
+  // socket or file.
+  this.length = 0;
 
-			this.x -= s;
-			this.y -= s;
-			this.z -= s;
-			this.w -= s;
+  // a flag to see when we're in the middle of a write.
+  this.writing = false;
 
-			return this;
+  // when true all writes will be buffered until .uncork() call
+  this.corked = 0;
 
-		},
+  // a flag to be able to tell if the onwrite cb is called immediately,
+  // or on a later tick.  We set this to true at first, because any
+  // actions that shouldn't happen until "later" should generally also
+  // not happen before the first write call.
+  this.sync = true;
 
-		subVectors: function ( a, b ) {
+  // a flag to know if we're processing previously buffered items, which
+  // may call the _write() callback in the same tick, so that we don't
+  // end up in an overlapped onwrite situation.
+  this.bufferProcessing = false;
 
-			this.x = a.x - b.x;
-			this.y = a.y - b.y;
-			this.z = a.z - b.z;
-			this.w = a.w - b.w;
+  // the callback that's passed to _write(chunk,cb)
+  this.onwrite = function (er) {
+    onwrite(stream, er);
+  };
 
-			return this;
+  // the callback that the user supplies to write(chunk,encoding,cb)
+  this.writecb = null;
 
-		},
+  // the amount that is being written when _write is called.
+  this.writelen = 0;
 
-		multiplyScalar: function ( scalar ) {
+  this.bufferedRequest = null;
+  this.lastBufferedRequest = null;
 
-			this.x *= scalar;
-			this.y *= scalar;
-			this.z *= scalar;
-			this.w *= scalar;
+  // number of pending user-supplied write callbacks
+  // this must be 0 before 'finish' can be emitted
+  this.pendingcb = 0;
 
-			return this;
+  // emit prefinish if the only thing we're waiting for is _write cbs
+  // This is relevant for synchronous Transform streams
+  this.prefinished = false;
 
-		},
+  // True if the error was already emitted and should not be thrown again
+  this.errorEmitted = false;
 
-		applyMatrix4: function ( m ) {
+  // count buffered requests
+  this.bufferedRequestCount = 0;
 
-			var x = this.x, y = this.y, z = this.z, w = this.w;
-			var e = m.elements;
+  // allocate the first CorkedRequest, there is always
+  // one allocated and free to use, and we maintain at most two
+  this.corkedRequestsFree = new CorkedRequest(this);
+}
 
-			this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
-			this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
-			this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
-			this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
+WritableState.prototype.getBuffer = function getBuffer() {
+  var current = this.bufferedRequest;
+  var out = [];
+  while (current) {
+    out.push(current);
+    current = current.next;
+  }
+  return out;
+};
 
-			return this;
+(function () {
+  try {
+    Object.defineProperty(WritableState.prototype, 'buffer', {
+      get: internalUtil.deprecate(function () {
+        return this.getBuffer();
+      }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003')
+    });
+  } catch (_) {}
+})();
 
-		},
+// Test _writableState for inheritance to account for Duplex streams,
+// whose prototype chain only points to Readable.
+var realHasInstance;
+if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
+  realHasInstance = Function.prototype[Symbol.hasInstance];
+  Object.defineProperty(Writable, Symbol.hasInstance, {
+    value: function (object) {
+      if (realHasInstance.call(this, object)) return true;
+      if (this !== Writable) return false;
 
-		divideScalar: function ( scalar ) {
+      return object && object._writableState instanceof WritableState;
+    }
+  });
+} else {
+  realHasInstance = function (object) {
+    return object instanceof this;
+  };
+}
 
-			return this.multiplyScalar( 1 / scalar );
+function Writable(options) {
+  Duplex = Duplex || require('./_stream_duplex');
 
-		},
+  // Writable ctor is applied to Duplexes, too.
+  // `realHasInstance` is necessary because using plain `instanceof`
+  // would return false, as no `_writableState` property is attached.
 
-		setAxisAngleFromQuaternion: function ( q ) {
+  // Trying to use the custom `instanceof` for Writable here will also break the
+  // Node.js LazyTransform implementation, which has a non-trivial getter for
+  // `_writableState` that would lead to infinite recursion.
+  if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) {
+    return new Writable(options);
+  }
 
-			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
+  this._writableState = new WritableState(options, this);
 
-			// q is assumed to be normalized
+  // legacy.
+  this.writable = true;
 
-			this.w = 2 * Math.acos( q.w );
+  if (options) {
+    if (typeof options.write === 'function') this._write = options.write;
 
-			var s = Math.sqrt( 1 - q.w * q.w );
+    if (typeof options.writev === 'function') this._writev = options.writev;
 
-			if ( s < 0.0001 ) {
+    if (typeof options.destroy === 'function') this._destroy = options.destroy;
 
-				this.x = 1;
-				this.y = 0;
-				this.z = 0;
+    if (typeof options.final === 'function') this._final = options.final;
+  }
 
-			} else {
+  Stream.call(this);
+}
 
-				this.x = q.x / s;
-				this.y = q.y / s;
-				this.z = q.z / s;
+// Otherwise people can pipe Writable streams, which is just wrong.
+Writable.prototype.pipe = function () {
+  this.emit('error', new Error('Cannot pipe, not readable'));
+};
 
-			}
+function writeAfterEnd(stream, cb) {
+  var er = new Error('write after end');
+  // TODO: defer error events consistently everywhere, not just the cb
+  stream.emit('error', er);
+  pna.nextTick(cb, er);
+}
 
-			return this;
+// Checks that a user-supplied chunk is valid, especially for the particular
+// mode the stream is in. Currently this means that `null` is never accepted
+// and undefined/non-string values are only allowed in object mode.
+function validChunk(stream, state, chunk, cb) {
+  var valid = true;
+  var er = false;
 
-		},
+  if (chunk === null) {
+    er = new TypeError('May not write null values to stream');
+  } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
+    er = new TypeError('Invalid non-string/buffer chunk');
+  }
+  if (er) {
+    stream.emit('error', er);
+    pna.nextTick(cb, er);
+    valid = false;
+  }
+  return valid;
+}
 
-		setAxisAngleFromRotationMatrix: function ( m ) {
+Writable.prototype.write = function (chunk, encoding, cb) {
+  var state = this._writableState;
+  var ret = false;
+  var isBuf = !state.objectMode && _isUint8Array(chunk);
 
-			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
+  if (isBuf && !Buffer.isBuffer(chunk)) {
+    chunk = _uint8ArrayToBuffer(chunk);
+  }
 
-			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
+  if (typeof encoding === 'function') {
+    cb = encoding;
+    encoding = null;
+  }
 
-			var angle, x, y, z,		// variables for result
-				epsilon = 0.01,		// margin to allow for rounding errors
-				epsilon2 = 0.1,		// margin to distinguish between 0 and 180 degrees
+  if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
 
-				te = m.elements,
+  if (typeof cb !== 'function') cb = nop;
 
-				m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
-				m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
-				m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
+  if (state.ended) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) {
+    state.pendingcb++;
+    ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
+  }
 
-			if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
-			     ( Math.abs( m13 - m31 ) < epsilon ) &&
-			     ( Math.abs( m23 - m32 ) < epsilon ) ) {
+  return ret;
+};
 
-				// singularity found
-				// first check for identity matrix which must have +1 for all terms
-				// in leading diagonal and zero in other terms
+Writable.prototype.cork = function () {
+  var state = this._writableState;
 
-				if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
-				     ( Math.abs( m13 + m31 ) < epsilon2 ) &&
-				     ( Math.abs( m23 + m32 ) < epsilon2 ) &&
-				     ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
+  state.corked++;
+};
 
-					// this singularity is identity matrix so angle = 0
+Writable.prototype.uncork = function () {
+  var state = this._writableState;
 
-					this.set( 1, 0, 0, 0 );
+  if (state.corked) {
+    state.corked--;
 
-					return this; // zero angle, arbitrary axis
+    if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
+  }
+};
 
-				}
+Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
+  // node::ParseEncoding() requires lower case.
+  if (typeof encoding === 'string') encoding = encoding.toLowerCase();
+  if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding);
+  this._writableState.defaultEncoding = encoding;
+  return this;
+};
 
-				// otherwise this singularity is angle = 180
+function decodeChunk(state, chunk, encoding) {
+  if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
+    chunk = Buffer.from(chunk, encoding);
+  }
+  return chunk;
+}
 
-				angle = Math.PI;
+Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
+  // making it explicit this property is not enumerable
+  // because otherwise some prototype manipulation in
+  // userland will fail
+  enumerable: false,
+  get: function () {
+    return this._writableState.highWaterMark;
+  }
+});
 
-				var xx = ( m11 + 1 ) / 2;
-				var yy = ( m22 + 1 ) / 2;
-				var zz = ( m33 + 1 ) / 2;
-				var xy = ( m12 + m21 ) / 4;
-				var xz = ( m13 + m31 ) / 4;
-				var yz = ( m23 + m32 ) / 4;
+// if we're already writing something, then just put this
+// in the queue, and wait our turn.  Otherwise, call _write
+// If we return false, then we need a drain event, so set that flag.
+function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
+  if (!isBuf) {
+    var newChunk = decodeChunk(state, chunk, encoding);
+    if (chunk !== newChunk) {
+      isBuf = true;
+      encoding = 'buffer';
+      chunk = newChunk;
+    }
+  }
+  var len = state.objectMode ? 1 : chunk.length;
 
-				if ( ( xx > yy ) && ( xx > zz ) ) {
+  state.length += len;
 
-					// m11 is the largest diagonal term
+  var ret = state.length < state.highWaterMark;
+  // we must ensure that previous needDrain will not be reset to false.
+  if (!ret) state.needDrain = true;
 
-					if ( xx < epsilon ) {
+  if (state.writing || state.corked) {
+    var last = state.lastBufferedRequest;
+    state.lastBufferedRequest = {
+      chunk: chunk,
+      encoding: encoding,
+      isBuf: isBuf,
+      callback: cb,
+      next: null
+    };
+    if (last) {
+      last.next = state.lastBufferedRequest;
+    } else {
+      state.bufferedRequest = state.lastBufferedRequest;
+    }
+    state.bufferedRequestCount += 1;
+  } else {
+    doWrite(stream, state, false, len, chunk, encoding, cb);
+  }
 
-						x = 0;
-						y = 0.707106781;
-						z = 0.707106781;
+  return ret;
+}
 
-					} else {
+function doWrite(stream, state, writev, len, chunk, encoding, cb) {
+  state.writelen = len;
+  state.writecb = cb;
+  state.writing = true;
+  state.sync = true;
+  if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
+  state.sync = false;
+}
 
-						x = Math.sqrt( xx );
-						y = xy / x;
-						z = xz / x;
+function onwriteError(stream, state, sync, er, cb) {
+  --state.pendingcb;
 
-					}
+  if (sync) {
+    // defer the callback if we are being called synchronously
+    // to avoid piling up things on the stack
+    pna.nextTick(cb, er);
+    // this can emit finish, and it will always happen
+    // after error
+    pna.nextTick(finishMaybe, stream, state);
+    stream._writableState.errorEmitted = true;
+    stream.emit('error', er);
+  } else {
+    // the caller expect this to happen before if
+    // it is async
+    cb(er);
+    stream._writableState.errorEmitted = true;
+    stream.emit('error', er);
+    // this can emit finish, but finish must
+    // always follow error
+    finishMaybe(stream, state);
+  }
+}
 
-				} else if ( yy > zz ) {
+function onwriteStateUpdate(state) {
+  state.writing = false;
+  state.writecb = null;
+  state.length -= state.writelen;
+  state.writelen = 0;
+}
 
-					// m22 is the largest diagonal term
+function onwrite(stream, er) {
+  var state = stream._writableState;
+  var sync = state.sync;
+  var cb = state.writecb;
 
-					if ( yy < epsilon ) {
+  onwriteStateUpdate(state);
 
-						x = 0.707106781;
-						y = 0;
-						z = 0.707106781;
+  if (er) onwriteError(stream, state, sync, er, cb);else {
+    // Check if we're actually ready to finish, but don't emit yet
+    var finished = needFinish(state);
 
-					} else {
+    if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
+      clearBuffer(stream, state);
+    }
 
-						y = Math.sqrt( yy );
-						x = xy / y;
-						z = yz / y;
+    if (sync) {
+      /*<replacement>*/
+      asyncWrite(afterWrite, stream, state, finished, cb);
+      /*</replacement>*/
+    } else {
+      afterWrite(stream, state, finished, cb);
+    }
+  }
+}
 
-					}
+function afterWrite(stream, state, finished, cb) {
+  if (!finished) onwriteDrain(stream, state);
+  state.pendingcb--;
+  cb();
+  finishMaybe(stream, state);
+}
 
-				} else {
+// Must force callback to be called on nextTick, so that we don't
+// emit 'drain' before the write() consumer gets the 'false' return
+// value, and has a chance to attach a 'drain' listener.
+function onwriteDrain(stream, state) {
+  if (state.length === 0 && state.needDrain) {
+    state.needDrain = false;
+    stream.emit('drain');
+  }
+}
 
-					// m33 is the largest diagonal term so base result on this
+// if there's something in the buffer waiting, then process it
+function clearBuffer(stream, state) {
+  state.bufferProcessing = true;
+  var entry = state.bufferedRequest;
 
-					if ( zz < epsilon ) {
+  if (stream._writev && entry && entry.next) {
+    // Fast case, write everything using _writev()
+    var l = state.bufferedRequestCount;
+    var buffer = new Array(l);
+    var holder = state.corkedRequestsFree;
+    holder.entry = entry;
 
-						x = 0.707106781;
-						y = 0.707106781;
-						z = 0;
+    var count = 0;
+    var allBuffers = true;
+    while (entry) {
+      buffer[count] = entry;
+      if (!entry.isBuf) allBuffers = false;
+      entry = entry.next;
+      count += 1;
+    }
+    buffer.allBuffers = allBuffers;
 
-					} else {
+    doWrite(stream, state, true, state.length, buffer, '', holder.finish);
 
-						z = Math.sqrt( zz );
-						x = xz / z;
-						y = yz / z;
+    // doWrite is almost always async, defer these to save a bit of time
+    // as the hot path ends with doWrite
+    state.pendingcb++;
+    state.lastBufferedRequest = null;
+    if (holder.next) {
+      state.corkedRequestsFree = holder.next;
+      holder.next = null;
+    } else {
+      state.corkedRequestsFree = new CorkedRequest(state);
+    }
+    state.bufferedRequestCount = 0;
+  } else {
+    // Slow case, write chunks one-by-one
+    while (entry) {
+      var chunk = entry.chunk;
+      var encoding = entry.encoding;
+      var cb = entry.callback;
+      var len = state.objectMode ? 1 : chunk.length;
 
-					}
+      doWrite(stream, state, false, len, chunk, encoding, cb);
+      entry = entry.next;
+      state.bufferedRequestCount--;
+      // if we didn't call the onwrite immediately, then
+      // it means that we need to wait until it does.
+      // also, that means that the chunk and cb are currently
+      // being processed, so move the buffer counter past them.
+      if (state.writing) {
+        break;
+      }
+    }
 
-				}
+    if (entry === null) state.lastBufferedRequest = null;
+  }
 
-				this.set( x, y, z, angle );
+  state.bufferedRequest = entry;
+  state.bufferProcessing = false;
+}
 
-				return this; // return 180 deg rotation
+Writable.prototype._write = function (chunk, encoding, cb) {
+  cb(new Error('_write() is not implemented'));
+};
 
-			}
+Writable.prototype._writev = null;
 
-			// as we have reached here there are no singularities so we can handle normally
+Writable.prototype.end = function (chunk, encoding, cb) {
+  var state = this._writableState;
 
-			var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
-			                   ( m13 - m31 ) * ( m13 - m31 ) +
-			                   ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
+  if (typeof chunk === 'function') {
+    cb = chunk;
+    chunk = null;
+    encoding = null;
+  } else if (typeof encoding === 'function') {
+    cb = encoding;
+    encoding = null;
+  }
 
-			if ( Math.abs( s ) < 0.001 ) { s = 1; }
+  if (chunk !== null && chunk !== undefined) this.write(chunk, encoding);
 
-			// prevent divide by zero, should not happen if matrix is orthogonal and should be
-			// caught by singularity test above, but I've left it in just in case
+  // .end() fully uncorks
+  if (state.corked) {
+    state.corked = 1;
+    this.uncork();
+  }
 
-			this.x = ( m32 - m23 ) / s;
-			this.y = ( m13 - m31 ) / s;
-			this.z = ( m21 - m12 ) / s;
-			this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
+  // ignore unnecessary end() calls.
+  if (!state.ending && !state.finished) endWritable(this, state, cb);
+};
 
-			return this;
+function needFinish(state) {
+  return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
+}
+function callFinal(stream, state) {
+  stream._final(function (err) {
+    state.pendingcb--;
+    if (err) {
+      stream.emit('error', err);
+    }
+    state.prefinished = true;
+    stream.emit('prefinish');
+    finishMaybe(stream, state);
+  });
+}
+function prefinish(stream, state) {
+  if (!state.prefinished && !state.finalCalled) {
+    if (typeof stream._final === 'function') {
+      state.pendingcb++;
+      state.finalCalled = true;
+      pna.nextTick(callFinal, stream, state);
+    } else {
+      state.prefinished = true;
+      stream.emit('prefinish');
+    }
+  }
+}
 
-		},
+function finishMaybe(stream, state) {
+  var need = needFinish(state);
+  if (need) {
+    prefinish(stream, state);
+    if (state.pendingcb === 0) {
+      state.finished = true;
+      stream.emit('finish');
+    }
+  }
+  return need;
+}
 
-		min: function ( v ) {
+function endWritable(stream, state, cb) {
+  state.ending = true;
+  finishMaybe(stream, state);
+  if (cb) {
+    if (state.finished) pna.nextTick(cb);else stream.once('finish', cb);
+  }
+  state.ended = true;
+  stream.writable = false;
+}
 
-			this.x = Math.min( this.x, v.x );
-			this.y = Math.min( this.y, v.y );
-			this.z = Math.min( this.z, v.z );
-			this.w = Math.min( this.w, v.w );
+function onCorkedFinish(corkReq, state, err) {
+  var entry = corkReq.entry;
+  corkReq.entry = null;
+  while (entry) {
+    var cb = entry.callback;
+    state.pendingcb--;
+    cb(err);
+    entry = entry.next;
+  }
+  if (state.corkedRequestsFree) {
+    state.corkedRequestsFree.next = corkReq;
+  } else {
+    state.corkedRequestsFree = corkReq;
+  }
+}
 
-			return this;
+Object.defineProperty(Writable.prototype, 'destroyed', {
+  get: function () {
+    if (this._writableState === undefined) {
+      return false;
+    }
+    return this._writableState.destroyed;
+  },
+  set: function (value) {
+    // we ignore the value if the stream
+    // has not been initialized yet
+    if (!this._writableState) {
+      return;
+    }
 
-		},
+    // backward compatibility, the user is explicitly
+    // managing destroyed
+    this._writableState.destroyed = value;
+  }
+});
 
-		max: function ( v ) {
+Writable.prototype.destroy = destroyImpl.destroy;
+Writable.prototype._undestroy = destroyImpl.undestroy;
+Writable.prototype._destroy = function (err, cb) {
+  this.end();
+  cb(err);
+};
+}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("timers").setImmediate)
+},{"./_stream_duplex":64,"./internal/streams/destroy":70,"./internal/streams/stream":71,"_process":63,"core-util-is":9,"inherits":12,"process-nextick-args":62,"safe-buffer":74,"timers":77,"util-deprecate":78}],69:[function(require,module,exports){
+'use strict';
 
-			this.x = Math.max( this.x, v.x );
-			this.y = Math.max( this.y, v.y );
-			this.z = Math.max( this.z, v.z );
-			this.w = Math.max( this.w, v.w );
+function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
-			return this;
+var Buffer = require('safe-buffer').Buffer;
+var util = require('util');
 
-		},
+function copyBuffer(src, target, offset) {
+  src.copy(target, offset);
+}
 
-		clamp: function ( min, max ) {
+module.exports = function () {
+  function BufferList() {
+    _classCallCheck(this, BufferList);
 
-			// assumes min < max, componentwise
+    this.head = null;
+    this.tail = null;
+    this.length = 0;
+  }
 
-			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
-			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
-			this.z = Math.max( min.z, Math.min( max.z, this.z ) );
-			this.w = Math.max( min.w, Math.min( max.w, this.w ) );
+  BufferList.prototype.push = function push(v) {
+    var entry = { data: v, next: null };
+    if (this.length > 0) this.tail.next = entry;else this.head = entry;
+    this.tail = entry;
+    ++this.length;
+  };
 
-			return this;
+  BufferList.prototype.unshift = function unshift(v) {
+    var entry = { data: v, next: this.head };
+    if (this.length === 0) this.tail = entry;
+    this.head = entry;
+    ++this.length;
+  };
 
-		},
+  BufferList.prototype.shift = function shift() {
+    if (this.length === 0) return;
+    var ret = this.head.data;
+    if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
+    --this.length;
+    return ret;
+  };
 
-		clampScalar: function ( minVal, maxVal ) {
+  BufferList.prototype.clear = function clear() {
+    this.head = this.tail = null;
+    this.length = 0;
+  };
 
-			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
-			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
-			this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
-			this.w = Math.max( minVal, Math.min( maxVal, this.w ) );
+  BufferList.prototype.join = function join(s) {
+    if (this.length === 0) return '';
+    var p = this.head;
+    var ret = '' + p.data;
+    while (p = p.next) {
+      ret += s + p.data;
+    }return ret;
+  };
 
-			return this;
+  BufferList.prototype.concat = function concat(n) {
+    if (this.length === 0) return Buffer.alloc(0);
+    if (this.length === 1) return this.head.data;
+    var ret = Buffer.allocUnsafe(n >>> 0);
+    var p = this.head;
+    var i = 0;
+    while (p) {
+      copyBuffer(p.data, ret, i);
+      i += p.data.length;
+      p = p.next;
+    }
+    return ret;
+  };
 
-		},
+  return BufferList;
+}();
 
-		clampLength: function ( min, max ) {
+if (util && util.inspect && util.inspect.custom) {
+  module.exports.prototype[util.inspect.custom] = function () {
+    var obj = util.inspect({ length: this.length });
+    return this.constructor.name + ' ' + obj;
+  };
+}
+},{"safe-buffer":74,"util":7}],70:[function(require,module,exports){
+'use strict';
 
-			var length = this.length();
+/*<replacement>*/
 
-			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
+var pna = require('process-nextick-args');
+/*</replacement>*/
 
-		},
+// undocumented cb() API, needed for core, not for public API
+function destroy(err, cb) {
+  var _this = this;
 
-		floor: function () {
+  var readableDestroyed = this._readableState && this._readableState.destroyed;
+  var writableDestroyed = this._writableState && this._writableState.destroyed;
 
-			this.x = Math.floor( this.x );
-			this.y = Math.floor( this.y );
-			this.z = Math.floor( this.z );
-			this.w = Math.floor( this.w );
+  if (readableDestroyed || writableDestroyed) {
+    if (cb) {
+      cb(err);
+    } else if (err && (!this._writableState || !this._writableState.errorEmitted)) {
+      pna.nextTick(emitErrorNT, this, err);
+    }
+    return this;
+  }
 
-			return this;
+  // we set destroyed to true before firing error callbacks in order
+  // to make it re-entrance safe in case destroy() is called within callbacks
 
-		},
+  if (this._readableState) {
+    this._readableState.destroyed = true;
+  }
 
-		ceil: function () {
+  // if this is a duplex stream mark the writable part as destroyed as well
+  if (this._writableState) {
+    this._writableState.destroyed = true;
+  }
 
-			this.x = Math.ceil( this.x );
-			this.y = Math.ceil( this.y );
-			this.z = Math.ceil( this.z );
-			this.w = Math.ceil( this.w );
+  this._destroy(err || null, function (err) {
+    if (!cb && err) {
+      pna.nextTick(emitErrorNT, _this, err);
+      if (_this._writableState) {
+        _this._writableState.errorEmitted = true;
+      }
+    } else if (cb) {
+      cb(err);
+    }
+  });
 
-			return this;
+  return this;
+}
 
-		},
+function undestroy() {
+  if (this._readableState) {
+    this._readableState.destroyed = false;
+    this._readableState.reading = false;
+    this._readableState.ended = false;
+    this._readableState.endEmitted = false;
+  }
 
-		round: function () {
+  if (this._writableState) {
+    this._writableState.destroyed = false;
+    this._writableState.ended = false;
+    this._writableState.ending = false;
+    this._writableState.finished = false;
+    this._writableState.errorEmitted = false;
+  }
+}
 
-			this.x = Math.round( this.x );
-			this.y = Math.round( this.y );
-			this.z = Math.round( this.z );
-			this.w = Math.round( this.w );
+function emitErrorNT(self, err) {
+  self.emit('error', err);
+}
 
-			return this;
+module.exports = {
+  destroy: destroy,
+  undestroy: undestroy
+};
+},{"process-nextick-args":62}],71:[function(require,module,exports){
+module.exports = require('events').EventEmitter;
 
-		},
+},{"events":10}],72:[function(require,module,exports){
+exports = module.exports = require('./lib/_stream_readable.js');
+exports.Stream = exports;
+exports.Readable = exports;
+exports.Writable = require('./lib/_stream_writable.js');
+exports.Duplex = require('./lib/_stream_duplex.js');
+exports.Transform = require('./lib/_stream_transform.js');
+exports.PassThrough = require('./lib/_stream_passthrough.js');
 
-		roundToZero: function () {
+},{"./lib/_stream_duplex.js":64,"./lib/_stream_passthrough.js":65,"./lib/_stream_readable.js":66,"./lib/_stream_transform.js":67,"./lib/_stream_writable.js":68}],73:[function(require,module,exports){
+/*! @license Rematrix v0.7.0
 
-			this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
-			this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
-			this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
-			this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
+	Copyright 2020 Julian Lloyd.
 
-			return this;
+	Permission is hereby granted, free of charge, to any person obtaining a copy
+	of this software and associated documentation files (the "Software"), to deal
+	in the Software without restriction, including without limitation the rights
+	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+	copies of the Software, and to permit persons to whom the Software is
+	furnished to do so, subject to the following conditions:
 
-		},
+	The above copyright notice and this permission notice shall be included in
+	all copies or substantial portions of the Software.
 
-		negate: function () {
+	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+	THE SOFTWARE.
+*/
+(function (global, factory) {
+  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+  typeof define === 'function' && define.amd ? define(['exports'], factory) :
+  (global = global || self, factory(global.Rematrix = {}));
+}(this, (function (exports) { 'use strict';
 
-			this.x = - this.x;
-			this.y = - this.y;
-			this.z = - this.z;
-			this.w = - this.w;
+  function format(source) {
+    if (source && source.constructor === Array) {
+      var values = source
+        .filter(function (value) { return typeof value === 'number'; })
+        .filter(function (value) { return !isNaN(value); });
 
-			return this;
+      if (source.length === 6 && values.length === 6) {
+        var matrix = identity();
+        matrix[0] = values[0];
+        matrix[1] = values[1];
+        matrix[4] = values[2];
+        matrix[5] = values[3];
+        matrix[12] = values[4];
+        matrix[13] = values[5];
+        return matrix
+      } else if (source.length === 16 && values.length === 16) {
+        return source
+      }
+    }
+    throw new TypeError('Expected a `number[]` with length 6 or 16.')
+  }
 
-		},
+  function fromString(source) {
+    if (typeof source === 'string') {
+      var match = source.match(/matrix(3d)?\(([^)]+)\)/);
+      if (match) {
+        var raw = match[2].split(', ').map(parseFloat);
+        return format(raw)
+      }
+    }
+    throw new TypeError('Expected a string containing `matrix()` or `matrix3d()')
+  }
 
-		dot: function ( v ) {
+  function identity() {
+    var matrix = [];
+    for (var i = 0; i < 16; i++) {
+      i % 5 == 0 ? matrix.push(1) : matrix.push(0);
+    }
+    return matrix
+  }
 
-			return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
+  function inverse(source) {
+    var m = format(source);
 
-		},
+    var s0 = m[0] * m[5] - m[4] * m[1];
+    var s1 = m[0] * m[6] - m[4] * m[2];
+    var s2 = m[0] * m[7] - m[4] * m[3];
+    var s3 = m[1] * m[6] - m[5] * m[2];
+    var s4 = m[1] * m[7] - m[5] * m[3];
+    var s5 = m[2] * m[7] - m[6] * m[3];
 
-		lengthSq: function () {
+    var c5 = m[10] * m[15] - m[14] * m[11];
+    var c4 = m[9] * m[15] - m[13] * m[11];
+    var c3 = m[9] * m[14] - m[13] * m[10];
+    var c2 = m[8] * m[15] - m[12] * m[11];
+    var c1 = m[8] * m[14] - m[12] * m[10];
+    var c0 = m[8] * m[13] - m[12] * m[9];
 
-			return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
+    var determinant = 1 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0);
 
-		},
+    if (isNaN(determinant) || determinant === Infinity) {
+      throw new Error('Inverse determinant attempted to divide by zero.')
+    }
 
-		length: function () {
+    return [
+      (m[5] * c5 - m[6] * c4 + m[7] * c3) * determinant,
+      (-m[1] * c5 + m[2] * c4 - m[3] * c3) * determinant,
+      (m[13] * s5 - m[14] * s4 + m[15] * s3) * determinant,
+      (-m[9] * s5 + m[10] * s4 - m[11] * s3) * determinant,
 
-			return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
+      (-m[4] * c5 + m[6] * c2 - m[7] * c1) * determinant,
+      (m[0] * c5 - m[2] * c2 + m[3] * c1) * determinant,
+      (-m[12] * s5 + m[14] * s2 - m[15] * s1) * determinant,
+      (m[8] * s5 - m[10] * s2 + m[11] * s1) * determinant,
 
-		},
+      (m[4] * c4 - m[5] * c2 + m[7] * c0) * determinant,
+      (-m[0] * c4 + m[1] * c2 - m[3] * c0) * determinant,
+      (m[12] * s4 - m[13] * s2 + m[15] * s0) * determinant,
+      (-m[8] * s4 + m[9] * s2 - m[11] * s0) * determinant,
 
-		manhattanLength: function () {
+      (-m[4] * c3 + m[5] * c1 - m[6] * c0) * determinant,
+      (m[0] * c3 - m[1] * c1 + m[2] * c0) * determinant,
+      (-m[12] * s3 + m[13] * s1 - m[14] * s0) * determinant,
+      (m[8] * s3 - m[9] * s1 + m[10] * s0) * determinant ]
+  }
 
-			return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
+  function multiply(matrixA, matrixB) {
+    var fma = format(matrixA);
+    var fmb = format(matrixB);
+    var product = [];
 
-		},
+    for (var i = 0; i < 4; i++) {
+      var row = [fma[i], fma[i + 4], fma[i + 8], fma[i + 12]];
+      for (var j = 0; j < 4; j++) {
+        var k = j * 4;
+        var col = [fmb[k], fmb[k + 1], fmb[k + 2], fmb[k + 3]];
+        var result = row[0] * col[0] + row[1] * col[1] + row[2] * col[2] + row[3] * col[3];
 
-		normalize: function () {
+        product[i + k] = result;
+      }
+    }
 
-			return this.divideScalar( this.length() || 1 );
+    return product
+  }
 
-		},
+  function perspective(distance) {
+    var matrix = identity();
+    matrix[11] = -1 / distance;
+    return matrix
+  }
 
-		setLength: function ( length ) {
+  function rotate(angle) {
+    return rotateZ(angle)
+  }
 
-			return this.normalize().multiplyScalar( length );
+  function rotateX(angle) {
+    var theta = (Math.PI / 180) * angle;
+    var matrix = identity();
 
-		},
+    matrix[5] = matrix[10] = Math.cos(theta);
+    matrix[6] = matrix[9] = Math.sin(theta);
+    matrix[9] *= -1;
 
-		lerp: function ( v, alpha ) {
+    return matrix
+  }
 
-			this.x += ( v.x - this.x ) * alpha;
-			this.y += ( v.y - this.y ) * alpha;
-			this.z += ( v.z - this.z ) * alpha;
-			this.w += ( v.w - this.w ) * alpha;
+  function rotateY(angle) {
+    var theta = (Math.PI / 180) * angle;
+    var matrix = identity();
 
-			return this;
+    matrix[0] = matrix[10] = Math.cos(theta);
+    matrix[2] = matrix[8] = Math.sin(theta);
+    matrix[2] *= -1;
 
-		},
+    return matrix
+  }
 
-		lerpVectors: function ( v1, v2, alpha ) {
+  function rotateZ(angle) {
+    var theta = (Math.PI / 180) * angle;
+    var matrix = identity();
 
-			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
+    matrix[0] = matrix[5] = Math.cos(theta);
+    matrix[1] = matrix[4] = Math.sin(theta);
+    matrix[4] *= -1;
 
-		},
+    return matrix
+  }
 
-		equals: function ( v ) {
+  function scale(scalar, scalarY) {
+    var matrix = identity();
 
-			return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
+    matrix[0] = scalar;
+    matrix[5] = typeof scalarY === 'number' ? scalarY : scalar;
 
-		},
+    return matrix
+  }
 
-		fromArray: function ( array, offset ) {
+  function scaleX(scalar) {
+    var matrix = identity();
+    matrix[0] = scalar;
+    return matrix
+  }
 
-			if ( offset === undefined ) { offset = 0; }
+  function scaleY(scalar) {
+    var matrix = identity();
+    matrix[5] = scalar;
+    return matrix
+  }
 
-			this.x = array[ offset ];
-			this.y = array[ offset + 1 ];
-			this.z = array[ offset + 2 ];
-			this.w = array[ offset + 3 ];
+  function scaleZ(scalar) {
+    var matrix = identity();
+    matrix[10] = scalar;
+    return matrix
+  }
 
-			return this;
+  function skew(angleX, angleY) {
+    var thetaX = (Math.PI / 180) * angleX;
+    var matrix = identity();
 
-		},
+    matrix[4] = Math.tan(thetaX);
 
-		toArray: function ( array, offset ) {
+    if (angleY) {
+      var thetaY = (Math.PI / 180) * angleY;
+      matrix[1] = Math.tan(thetaY);
+    }
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+    return matrix
+  }
 
-			array[ offset ] = this.x;
-			array[ offset + 1 ] = this.y;
-			array[ offset + 2 ] = this.z;
-			array[ offset + 3 ] = this.w;
+  function skewX(angle) {
+    var theta = (Math.PI / 180) * angle;
+    var matrix = identity();
 
-			return array;
+    matrix[4] = Math.tan(theta);
 
-		},
+    return matrix
+  }
 
-		fromBufferAttribute: function ( attribute, index, offset ) {
+  function skewY(angle) {
+    var theta = (Math.PI / 180) * angle;
+    var matrix = identity();
 
-			if ( offset !== undefined ) {
+    matrix[1] = Math.tan(theta);
 
-				console.warn( 'THREE.Vector4: offset has been removed from .fromBufferAttribute().' );
+    return matrix
+  }
 
-			}
+  function toString(source) {
+    return ("matrix3d(" + (format(source).join(', ')) + ")")
+  }
 
-			this.x = attribute.getX( index );
-			this.y = attribute.getY( index );
-			this.z = attribute.getZ( index );
-			this.w = attribute.getW( index );
+  function translate(distanceX, distanceY) {
+    var matrix = identity();
+    matrix[12] = distanceX;
 
-			return this;
+    if (distanceY) {
+      matrix[13] = distanceY;
+    }
 
-		},
+    return matrix
+  }
 
-		random: function () {
+  function translate3d(distanceX, distanceY, distanceZ) {
+    var matrix = identity();
+    if (distanceX !== undefined && distanceY !== undefined && distanceZ !== undefined) {
+      matrix[12] = distanceX;
+      matrix[13] = distanceY;
+      matrix[14] = distanceZ;
+    }
+    return matrix
+  }
 
-			this.x = Math.random();
-			this.y = Math.random();
-			this.z = Math.random();
-			this.w = Math.random();
+  function translateX(distance) {
+    var matrix = identity();
+    matrix[12] = distance;
+    return matrix
+  }
 
-			return this;
+  function translateY(distance) {
+    var matrix = identity();
+    matrix[13] = distance;
+    return matrix
+  }
 
-		}
+  function translateZ(distance) {
+    var matrix = identity();
+    matrix[14] = distance;
+    return matrix
+  }
 
-	} );
+  exports.format = format;
+  exports.fromString = fromString;
+  exports.identity = identity;
+  exports.inverse = inverse;
+  exports.multiply = multiply;
+  exports.perspective = perspective;
+  exports.rotate = rotate;
+  exports.rotateX = rotateX;
+  exports.rotateY = rotateY;
+  exports.rotateZ = rotateZ;
+  exports.scale = scale;
+  exports.scaleX = scaleX;
+  exports.scaleY = scaleY;
+  exports.scaleZ = scaleZ;
+  exports.skew = skew;
+  exports.skewX = skewX;
+  exports.skewY = skewY;
+  exports.toString = toString;
+  exports.translate = translate;
+  exports.translate3d = translate3d;
+  exports.translateX = translateX;
+  exports.translateY = translateY;
+  exports.translateZ = translateZ;
 
-	/**
-	 * @author szimek / https://github.com/szimek/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author Marius Kintel / https://github.com/kintel
-	 */
+  Object.defineProperty(exports, '__esModule', { value: true });
 
-	/*
-	 In options, we can specify:
-	 * Texture parameters for an auto-generated target texture
-	 * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
-	*/
-	function WebGLRenderTarget( width, height, options ) {
+})));
 
-		this.width = width;
-		this.height = height;
+},{}],74:[function(require,module,exports){
+/* eslint-disable node/no-deprecated-api */
+var buffer = require('buffer')
+var Buffer = buffer.Buffer
 
-		this.scissor = new Vector4( 0, 0, width, height );
-		this.scissorTest = false;
+// alternative to using Object.keys for old browsers
+function copyProps (src, dst) {
+  for (var key in src) {
+    dst[key] = src[key]
+  }
+}
+if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
+  module.exports = buffer
+} else {
+  // Copy properties from require('buffer')
+  copyProps(buffer, exports)
+  exports.Buffer = SafeBuffer
+}
 
-		this.viewport = new Vector4( 0, 0, width, height );
+function SafeBuffer (arg, encodingOrOffset, length) {
+  return Buffer(arg, encodingOrOffset, length)
+}
 
-		options = options || {};
+// Copy static methods from Buffer
+copyProps(Buffer, SafeBuffer)
 
-		this.texture = new Texture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.encoding );
+SafeBuffer.from = function (arg, encodingOrOffset, length) {
+  if (typeof arg === 'number') {
+    throw new TypeError('Argument must not be a number')
+  }
+  return Buffer(arg, encodingOrOffset, length)
+}
 
-		this.texture.image = {};
-		this.texture.image.width = width;
-		this.texture.image.height = height;
+SafeBuffer.alloc = function (size, fill, encoding) {
+  if (typeof size !== 'number') {
+    throw new TypeError('Argument must be a number')
+  }
+  var buf = Buffer(size)
+  if (fill !== undefined) {
+    if (typeof encoding === 'string') {
+      buf.fill(fill, encoding)
+    } else {
+      buf.fill(fill)
+    }
+  } else {
+    buf.fill(0)
+  }
+  return buf
+}
 
-		this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
-		this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
+SafeBuffer.allocUnsafe = function (size) {
+  if (typeof size !== 'number') {
+    throw new TypeError('Argument must be a number')
+  }
+  return Buffer(size)
+}
 
-		this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
-		this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
-		this.depthTexture = options.depthTexture !== undefined ? options.depthTexture : null;
+SafeBuffer.allocUnsafeSlow = function (size) {
+  if (typeof size !== 'number') {
+    throw new TypeError('Argument must be a number')
+  }
+  return buffer.SlowBuffer(size)
+}
 
-	}
+},{"buffer":8}],75:[function(require,module,exports){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-	WebGLRenderTarget.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+'use strict';
 
-		constructor: WebGLRenderTarget,
+/*<replacement>*/
 
-		isWebGLRenderTarget: true,
+var Buffer = require('safe-buffer').Buffer;
+/*</replacement>*/
 
-		setSize: function ( width, height ) {
+var isEncoding = Buffer.isEncoding || function (encoding) {
+  encoding = '' + encoding;
+  switch (encoding && encoding.toLowerCase()) {
+    case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
+      return true;
+    default:
+      return false;
+  }
+};
 
-			if ( this.width !== width || this.height !== height ) {
+function _normalizeEncoding(enc) {
+  if (!enc) return 'utf8';
+  var retried;
+  while (true) {
+    switch (enc) {
+      case 'utf8':
+      case 'utf-8':
+        return 'utf8';
+      case 'ucs2':
+      case 'ucs-2':
+      case 'utf16le':
+      case 'utf-16le':
+        return 'utf16le';
+      case 'latin1':
+      case 'binary':
+        return 'latin1';
+      case 'base64':
+      case 'ascii':
+      case 'hex':
+        return enc;
+      default:
+        if (retried) return; // undefined
+        enc = ('' + enc).toLowerCase();
+        retried = true;
+    }
+  }
+};
 
-				this.width = width;
-				this.height = height;
+// Do not cache `Buffer.isEncoding` when checking encoding names as some
+// modules monkey-patch it to support additional encodings
+function normalizeEncoding(enc) {
+  var nenc = _normalizeEncoding(enc);
+  if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
+  return nenc || enc;
+}
 
-				this.texture.image.width = width;
-				this.texture.image.height = height;
+// StringDecoder provides an interface for efficiently splitting a series of
+// buffers into a series of JS strings without breaking apart multi-byte
+// characters.
+exports.StringDecoder = StringDecoder;
+function StringDecoder(encoding) {
+  this.encoding = normalizeEncoding(encoding);
+  var nb;
+  switch (this.encoding) {
+    case 'utf16le':
+      this.text = utf16Text;
+      this.end = utf16End;
+      nb = 4;
+      break;
+    case 'utf8':
+      this.fillLast = utf8FillLast;
+      nb = 4;
+      break;
+    case 'base64':
+      this.text = base64Text;
+      this.end = base64End;
+      nb = 3;
+      break;
+    default:
+      this.write = simpleWrite;
+      this.end = simpleEnd;
+      return;
+  }
+  this.lastNeed = 0;
+  this.lastTotal = 0;
+  this.lastChar = Buffer.allocUnsafe(nb);
+}
 
-				this.dispose();
+StringDecoder.prototype.write = function (buf) {
+  if (buf.length === 0) return '';
+  var r;
+  var i;
+  if (this.lastNeed) {
+    r = this.fillLast(buf);
+    if (r === undefined) return '';
+    i = this.lastNeed;
+    this.lastNeed = 0;
+  } else {
+    i = 0;
+  }
+  if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
+  return r || '';
+};
 
-			}
+StringDecoder.prototype.end = utf8End;
 
-			this.viewport.set( 0, 0, width, height );
-			this.scissor.set( 0, 0, width, height );
+// Returns only complete characters in a Buffer
+StringDecoder.prototype.text = utf8Text;
 
-		},
+// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
+StringDecoder.prototype.fillLast = function (buf) {
+  if (this.lastNeed <= buf.length) {
+    buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
+    return this.lastChar.toString(this.encoding, 0, this.lastTotal);
+  }
+  buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
+  this.lastNeed -= buf.length;
+};
 
-		clone: function () {
+// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
+// continuation byte. If an invalid byte is detected, -2 is returned.
+function utf8CheckByte(byte) {
+  if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
+  return byte >> 6 === 0x02 ? -1 : -2;
+}
 
-			return new this.constructor().copy( this );
+// Checks at most 3 bytes at the end of a Buffer in order to detect an
+// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
+// needed to complete the UTF-8 character (if applicable) are returned.
+function utf8CheckIncomplete(self, buf, i) {
+  var j = buf.length - 1;
+  if (j < i) return 0;
+  var nb = utf8CheckByte(buf[j]);
+  if (nb >= 0) {
+    if (nb > 0) self.lastNeed = nb - 1;
+    return nb;
+  }
+  if (--j < i || nb === -2) return 0;
+  nb = utf8CheckByte(buf[j]);
+  if (nb >= 0) {
+    if (nb > 0) self.lastNeed = nb - 2;
+    return nb;
+  }
+  if (--j < i || nb === -2) return 0;
+  nb = utf8CheckByte(buf[j]);
+  if (nb >= 0) {
+    if (nb > 0) {
+      if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
+    }
+    return nb;
+  }
+  return 0;
+}
 
-		},
+// Validates as many continuation bytes for a multi-byte UTF-8 character as
+// needed or are available. If we see a non-continuation byte where we expect
+// one, we "replace" the validated continuation bytes we've seen so far with
+// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
+// behavior. The continuation byte check is included three times in the case
+// where all of the continuation bytes for a character exist in the same buffer.
+// It is also done this way as a slight performance increase instead of using a
+// loop.
+function utf8CheckExtraBytes(self, buf, p) {
+  if ((buf[0] & 0xC0) !== 0x80) {
+    self.lastNeed = 0;
+    return '\ufffd';
+  }
+  if (self.lastNeed > 1 && buf.length > 1) {
+    if ((buf[1] & 0xC0) !== 0x80) {
+      self.lastNeed = 1;
+      return '\ufffd';
+    }
+    if (self.lastNeed > 2 && buf.length > 2) {
+      if ((buf[2] & 0xC0) !== 0x80) {
+        self.lastNeed = 2;
+        return '\ufffd';
+      }
+    }
+  }
+}
 
-		copy: function ( source ) {
+// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
+function utf8FillLast(buf) {
+  var p = this.lastTotal - this.lastNeed;
+  var r = utf8CheckExtraBytes(this, buf, p);
+  if (r !== undefined) return r;
+  if (this.lastNeed <= buf.length) {
+    buf.copy(this.lastChar, p, 0, this.lastNeed);
+    return this.lastChar.toString(this.encoding, 0, this.lastTotal);
+  }
+  buf.copy(this.lastChar, p, 0, buf.length);
+  this.lastNeed -= buf.length;
+}
 
-			this.width = source.width;
-			this.height = source.height;
+// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
+// partial character, the character's bytes are buffered until the required
+// number of bytes are available.
+function utf8Text(buf, i) {
+  var total = utf8CheckIncomplete(this, buf, i);
+  if (!this.lastNeed) return buf.toString('utf8', i);
+  this.lastTotal = total;
+  var end = buf.length - (total - this.lastNeed);
+  buf.copy(this.lastChar, 0, end);
+  return buf.toString('utf8', i, end);
+}
 
-			this.viewport.copy( source.viewport );
+// For UTF-8, a replacement character is added when ending on a partial
+// character.
+function utf8End(buf) {
+  var r = buf && buf.length ? this.write(buf) : '';
+  if (this.lastNeed) return r + '\ufffd';
+  return r;
+}
 
-			this.texture = source.texture.clone();
+// UTF-16LE typically needs two bytes per character, but even if we have an even
+// number of bytes available, we need to check if we end on a leading/high
+// surrogate. In that case, we need to wait for the next two bytes in order to
+// decode the last character properly.
+function utf16Text(buf, i) {
+  if ((buf.length - i) % 2 === 0) {
+    var r = buf.toString('utf16le', i);
+    if (r) {
+      var c = r.charCodeAt(r.length - 1);
+      if (c >= 0xD800 && c <= 0xDBFF) {
+        this.lastNeed = 2;
+        this.lastTotal = 4;
+        this.lastChar[0] = buf[buf.length - 2];
+        this.lastChar[1] = buf[buf.length - 1];
+        return r.slice(0, -1);
+      }
+    }
+    return r;
+  }
+  this.lastNeed = 1;
+  this.lastTotal = 2;
+  this.lastChar[0] = buf[buf.length - 1];
+  return buf.toString('utf16le', i, buf.length - 1);
+}
 
-			this.depthBuffer = source.depthBuffer;
-			this.stencilBuffer = source.stencilBuffer;
-			this.depthTexture = source.depthTexture;
+// For UTF-16LE we do not explicitly append special replacement characters if we
+// end on a partial character, we simply let v8 handle that.
+function utf16End(buf) {
+  var r = buf && buf.length ? this.write(buf) : '';
+  if (this.lastNeed) {
+    var end = this.lastTotal - this.lastNeed;
+    return r + this.lastChar.toString('utf16le', 0, end);
+  }
+  return r;
+}
 
-			return this;
+function base64Text(buf, i) {
+  var n = (buf.length - i) % 3;
+  if (n === 0) return buf.toString('base64', i);
+  this.lastNeed = 3 - n;
+  this.lastTotal = 3;
+  if (n === 1) {
+    this.lastChar[0] = buf[buf.length - 1];
+  } else {
+    this.lastChar[0] = buf[buf.length - 2];
+    this.lastChar[1] = buf[buf.length - 1];
+  }
+  return buf.toString('base64', i, buf.length - n);
+}
 
-		},
+function base64End(buf) {
+  var r = buf && buf.length ? this.write(buf) : '';
+  if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
+  return r;
+}
 
-		dispose: function () {
+// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
+function simpleWrite(buf) {
+  return buf.toString(this.encoding);
+}
 
-			this.dispatchEvent( { type: 'dispose' } );
+function simpleEnd(buf) {
+  return buf && buf.length ? this.write(buf) : '';
+}
+},{"safe-buffer":74}],76:[function(require,module,exports){
+(function (global, factory) {
+	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+	typeof define === 'function' && define.amd ? define(['exports'], factory) :
+	(global = global || self, factory(global.THREE = {}));
+}(this, (function (exports) { 'use strict';
 
-		}
+	// Polyfills
 
-	} );
+	if ( Number.EPSILON === undefined ) {
 
-	/**
-	 * @author Mugen87 / https://github.com/Mugen87
-	 * @author Matt DesLauriers / @mattdesl
-	 */
+		Number.EPSILON = Math.pow( 2, - 52 );
 
-	function WebGLMultisampleRenderTarget( width, height, options ) {
+	}
 
-		WebGLRenderTarget.call( this, width, height, options );
+	if ( Number.isInteger === undefined ) {
 
-		this.samples = 4;
+		// Missing in IE
+		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
 
-	}
+		Number.isInteger = function ( value ) {
 
-	WebGLMultisampleRenderTarget.prototype = Object.assign( Object.create( WebGLRenderTarget.prototype ), {
+			return typeof value === 'number' && isFinite( value ) && Math.floor( value ) === value;
 
-		constructor: WebGLMultisampleRenderTarget,
+		};
 
-		isWebGLMultisampleRenderTarget: true,
+	}
 
-		copy: function ( source ) {
+	//
 
-			WebGLRenderTarget.prototype.copy.call( this, source );
+	if ( Math.sign === undefined ) {
 
-			this.samples = source.samples;
-
-			return this;
+		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
 
-		}
+		Math.sign = function ( x ) {
 
-	} );
+			return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : + x;
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author bhouston / http://clara.io
-	 */
+		};
 
-	function Quaternion( x, y, z, w ) {
+	}
 
-		this._x = x || 0;
-		this._y = y || 0;
-		this._z = z || 0;
-		this._w = ( w !== undefined ) ? w : 1;
+	if ( 'name' in Function.prototype === false ) {
 
-	}
+		// Missing in IE
+		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
 
-	Object.assign( Quaternion, {
+		Object.defineProperty( Function.prototype, 'name', {
 
-		slerp: function ( qa, qb, qm, t ) {
+			get: function () {
 
-			return qm.copy( qa ).slerp( qb, t );
+				return this.toString().match( /^\s*function\s*([^\(\s]*)/ )[ 1 ];
 
-		},
+			}
 
-		slerpFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) {
+		} );
 
-			// fuzz-free, array-based Quaternion SLERP operation
+	}
 
-			var x0 = src0[ srcOffset0 + 0 ],
-				y0 = src0[ srcOffset0 + 1 ],
-				z0 = src0[ srcOffset0 + 2 ],
-				w0 = src0[ srcOffset0 + 3 ],
+	if ( Object.assign === undefined ) {
 
-				x1 = src1[ srcOffset1 + 0 ],
-				y1 = src1[ srcOffset1 + 1 ],
-				z1 = src1[ srcOffset1 + 2 ],
-				w1 = src1[ srcOffset1 + 3 ];
+		// Missing in IE
+		// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
 
-			if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) {
+		Object.assign = function ( target ) {
 
-				var s = 1 - t,
+			if ( target === undefined || target === null ) {
 
-					cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1,
+				throw new TypeError( 'Cannot convert undefined or null to object' );
 
-					dir = ( cos >= 0 ? 1 : - 1 ),
-					sqrSin = 1 - cos * cos;
+			}
 
-				// Skip the Slerp for tiny steps to avoid numeric problems:
-				if ( sqrSin > Number.EPSILON ) {
+			var output = Object( target );
 
-					var sin = Math.sqrt( sqrSin ),
-						len = Math.atan2( sin, cos * dir );
+			for ( var index = 1; index < arguments.length; index ++ ) {
 
-					s = Math.sin( s * len ) / sin;
-					t = Math.sin( t * len ) / sin;
+				var source = arguments[ index ];
 
-				}
+				if ( source !== undefined && source !== null ) {
 
-				var tDir = t * dir;
+					for ( var nextKey in source ) {
 
-				x0 = x0 * s + x1 * tDir;
-				y0 = y0 * s + y1 * tDir;
-				z0 = z0 * s + z1 * tDir;
-				w0 = w0 * s + w1 * tDir;
+						if ( Object.prototype.hasOwnProperty.call( source, nextKey ) ) {
 
-				// Normalize in case we just did a lerp:
-				if ( s === 1 - t ) {
+							output[ nextKey ] = source[ nextKey ];
 
-					var f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 );
+						}
 
-					x0 *= f;
-					y0 *= f;
-					z0 *= f;
-					w0 *= f;
+					}
 
 				}
 
 			}
 
-			dst[ dstOffset ] = x0;
-			dst[ dstOffset + 1 ] = y0;
-			dst[ dstOffset + 2 ] = z0;
-			dst[ dstOffset + 3 ] = w0;
+			return output;
 
-		},
+		};
 
-		multiplyQuaternionsFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) {
+	}
 
-			var x0 = src0[ srcOffset0 ];
-			var y0 = src0[ srcOffset0 + 1 ];
-			var z0 = src0[ srcOffset0 + 2 ];
-			var w0 = src0[ srcOffset0 + 3 ];
+	var REVISION = '116';
+	var MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2, ROTATE: 0, DOLLY: 1, PAN: 2 };
+	var TOUCH = { ROTATE: 0, PAN: 1, DOLLY_PAN: 2, DOLLY_ROTATE: 3 };
+	var CullFaceNone = 0;
+	var CullFaceBack = 1;
+	var CullFaceFront = 2;
+	var CullFaceFrontBack = 3;
+	var FrontFaceDirectionCW = 0;
+	var FrontFaceDirectionCCW = 1;
+	var BasicShadowMap = 0;
+	var PCFShadowMap = 1;
+	var PCFSoftShadowMap = 2;
+	var VSMShadowMap = 3;
+	var FrontSide = 0;
+	var BackSide = 1;
+	var DoubleSide = 2;
+	var FlatShading = 1;
+	var SmoothShading = 2;
+	var NoBlending = 0;
+	var NormalBlending = 1;
+	var AdditiveBlending = 2;
+	var SubtractiveBlending = 3;
+	var MultiplyBlending = 4;
+	var CustomBlending = 5;
+	var AddEquation = 100;
+	var SubtractEquation = 101;
+	var ReverseSubtractEquation = 102;
+	var MinEquation = 103;
+	var MaxEquation = 104;
+	var ZeroFactor = 200;
+	var OneFactor = 201;
+	var SrcColorFactor = 202;
+	var OneMinusSrcColorFactor = 203;
+	var SrcAlphaFactor = 204;
+	var OneMinusSrcAlphaFactor = 205;
+	var DstAlphaFactor = 206;
+	var OneMinusDstAlphaFactor = 207;
+	var DstColorFactor = 208;
+	var OneMinusDstColorFactor = 209;
+	var SrcAlphaSaturateFactor = 210;
+	var NeverDepth = 0;
+	var AlwaysDepth = 1;
+	var LessDepth = 2;
+	var LessEqualDepth = 3;
+	var EqualDepth = 4;
+	var GreaterEqualDepth = 5;
+	var GreaterDepth = 6;
+	var NotEqualDepth = 7;
+	var MultiplyOperation = 0;
+	var MixOperation = 1;
+	var AddOperation = 2;
+	var NoToneMapping = 0;
+	var LinearToneMapping = 1;
+	var ReinhardToneMapping = 2;
+	var Uncharted2ToneMapping = 3;
+	var CineonToneMapping = 4;
+	var ACESFilmicToneMapping = 5;
 
-			var x1 = src1[ srcOffset1 ];
-			var y1 = src1[ srcOffset1 + 1 ];
-			var z1 = src1[ srcOffset1 + 2 ];
-			var w1 = src1[ srcOffset1 + 3 ];
+	var UVMapping = 300;
+	var CubeReflectionMapping = 301;
+	var CubeRefractionMapping = 302;
+	var EquirectangularReflectionMapping = 303;
+	var EquirectangularRefractionMapping = 304;
+	var SphericalReflectionMapping = 305;
+	var CubeUVReflectionMapping = 306;
+	var CubeUVRefractionMapping = 307;
+	var RepeatWrapping = 1000;
+	var ClampToEdgeWrapping = 1001;
+	var MirroredRepeatWrapping = 1002;
+	var NearestFilter = 1003;
+	var NearestMipmapNearestFilter = 1004;
+	var NearestMipMapNearestFilter = 1004;
+	var NearestMipmapLinearFilter = 1005;
+	var NearestMipMapLinearFilter = 1005;
+	var LinearFilter = 1006;
+	var LinearMipmapNearestFilter = 1007;
+	var LinearMipMapNearestFilter = 1007;
+	var LinearMipmapLinearFilter = 1008;
+	var LinearMipMapLinearFilter = 1008;
+	var UnsignedByteType = 1009;
+	var ByteType = 1010;
+	var ShortType = 1011;
+	var UnsignedShortType = 1012;
+	var IntType = 1013;
+	var UnsignedIntType = 1014;
+	var FloatType = 1015;
+	var HalfFloatType = 1016;
+	var UnsignedShort4444Type = 1017;
+	var UnsignedShort5551Type = 1018;
+	var UnsignedShort565Type = 1019;
+	var UnsignedInt248Type = 1020;
+	var AlphaFormat = 1021;
+	var RGBFormat = 1022;
+	var RGBAFormat = 1023;
+	var LuminanceFormat = 1024;
+	var LuminanceAlphaFormat = 1025;
+	var RGBEFormat = RGBAFormat;
+	var DepthFormat = 1026;
+	var DepthStencilFormat = 1027;
+	var RedFormat = 1028;
+	var RedIntegerFormat = 1029;
+	var RGFormat = 1030;
+	var RGIntegerFormat = 1031;
+	var RGBIntegerFormat = 1032;
+	var RGBAIntegerFormat = 1033;
 
-			dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
-			dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
-			dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
-			dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
+	var RGB_S3TC_DXT1_Format = 33776;
+	var RGBA_S3TC_DXT1_Format = 33777;
+	var RGBA_S3TC_DXT3_Format = 33778;
+	var RGBA_S3TC_DXT5_Format = 33779;
+	var RGB_PVRTC_4BPPV1_Format = 35840;
+	var RGB_PVRTC_2BPPV1_Format = 35841;
+	var RGBA_PVRTC_4BPPV1_Format = 35842;
+	var RGBA_PVRTC_2BPPV1_Format = 35843;
+	var RGB_ETC1_Format = 36196;
+	var RGB_ETC2_Format = 37492;
+	var RGBA_ETC2_EAC_Format = 37496;
+	var RGBA_ASTC_4x4_Format = 37808;
+	var RGBA_ASTC_5x4_Format = 37809;
+	var RGBA_ASTC_5x5_Format = 37810;
+	var RGBA_ASTC_6x5_Format = 37811;
+	var RGBA_ASTC_6x6_Format = 37812;
+	var RGBA_ASTC_8x5_Format = 37813;
+	var RGBA_ASTC_8x6_Format = 37814;
+	var RGBA_ASTC_8x8_Format = 37815;
+	var RGBA_ASTC_10x5_Format = 37816;
+	var RGBA_ASTC_10x6_Format = 37817;
+	var RGBA_ASTC_10x8_Format = 37818;
+	var RGBA_ASTC_10x10_Format = 37819;
+	var RGBA_ASTC_12x10_Format = 37820;
+	var RGBA_ASTC_12x12_Format = 37821;
+	var RGBA_BPTC_Format = 36492;
+	var SRGB8_ALPHA8_ASTC_4x4_Format = 37840;
+	var SRGB8_ALPHA8_ASTC_5x4_Format = 37841;
+	var SRGB8_ALPHA8_ASTC_5x5_Format = 37842;
+	var SRGB8_ALPHA8_ASTC_6x5_Format = 37843;
+	var SRGB8_ALPHA8_ASTC_6x6_Format = 37844;
+	var SRGB8_ALPHA8_ASTC_8x5_Format = 37845;
+	var SRGB8_ALPHA8_ASTC_8x6_Format = 37846;
+	var SRGB8_ALPHA8_ASTC_8x8_Format = 37847;
+	var SRGB8_ALPHA8_ASTC_10x5_Format = 37848;
+	var SRGB8_ALPHA8_ASTC_10x6_Format = 37849;
+	var SRGB8_ALPHA8_ASTC_10x8_Format = 37850;
+	var SRGB8_ALPHA8_ASTC_10x10_Format = 37851;
+	var SRGB8_ALPHA8_ASTC_12x10_Format = 37852;
+	var SRGB8_ALPHA8_ASTC_12x12_Format = 37853;
+	var LoopOnce = 2200;
+	var LoopRepeat = 2201;
+	var LoopPingPong = 2202;
+	var InterpolateDiscrete = 2300;
+	var InterpolateLinear = 2301;
+	var InterpolateSmooth = 2302;
+	var ZeroCurvatureEnding = 2400;
+	var ZeroSlopeEnding = 2401;
+	var WrapAroundEnding = 2402;
+	var NormalAnimationBlendMode = 2500;
+	var AdditiveAnimationBlendMode = 2501;
+	var TrianglesDrawMode = 0;
+	var TriangleStripDrawMode = 1;
+	var TriangleFanDrawMode = 2;
+	var LinearEncoding = 3000;
+	var sRGBEncoding = 3001;
+	var GammaEncoding = 3007;
+	var RGBEEncoding = 3002;
+	var LogLuvEncoding = 3003;
+	var RGBM7Encoding = 3004;
+	var RGBM16Encoding = 3005;
+	var RGBDEncoding = 3006;
+	var BasicDepthPacking = 3200;
+	var RGBADepthPacking = 3201;
+	var TangentSpaceNormalMap = 0;
+	var ObjectSpaceNormalMap = 1;
 
-			return dst;
+	var ZeroStencilOp = 0;
+	var KeepStencilOp = 7680;
+	var ReplaceStencilOp = 7681;
+	var IncrementStencilOp = 7682;
+	var DecrementStencilOp = 7683;
+	var IncrementWrapStencilOp = 34055;
+	var DecrementWrapStencilOp = 34056;
+	var InvertStencilOp = 5386;
 
-		}
+	var NeverStencilFunc = 512;
+	var LessStencilFunc = 513;
+	var EqualStencilFunc = 514;
+	var LessEqualStencilFunc = 515;
+	var GreaterStencilFunc = 516;
+	var NotEqualStencilFunc = 517;
+	var GreaterEqualStencilFunc = 518;
+	var AlwaysStencilFunc = 519;
 
-	} );
+	var StaticDrawUsage = 35044;
+	var DynamicDrawUsage = 35048;
+	var StreamDrawUsage = 35040;
+	var StaticReadUsage = 35045;
+	var DynamicReadUsage = 35049;
+	var StreamReadUsage = 35041;
+	var StaticCopyUsage = 35046;
+	var DynamicCopyUsage = 35050;
+	var StreamCopyUsage = 35042;
 
-	Object.defineProperties( Quaternion.prototype, {
+	/**
+	 * https://github.com/mrdoob/eventdispatcher.js/
+	 */
 
-		x: {
+	function EventDispatcher() {}
 
-			get: function () {
+	Object.assign( EventDispatcher.prototype, {
 
-				return this._x;
+		addEventListener: function ( type, listener ) {
 
-			},
+			if ( this._listeners === undefined ) { this._listeners = {}; }
 
-			set: function ( value ) {
+			var listeners = this._listeners;
 
-				this._x = value;
-				this._onChangeCallback();
+			if ( listeners[ type ] === undefined ) {
+
+				listeners[ type ] = [];
 
 			}
 
-		},
+			if ( listeners[ type ].indexOf( listener ) === - 1 ) {
 
-		y: {
+				listeners[ type ].push( listener );
 
-			get: function () {
+			}
 
-				return this._y;
+		},
 
-			},
+		hasEventListener: function ( type, listener ) {
 
-			set: function ( value ) {
+			if ( this._listeners === undefined ) { return false; }
 
-				this._y = value;
-				this._onChangeCallback();
+			var listeners = this._listeners;
 
-			}
+			return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1;
 
 		},
 
-		z: {
+		removeEventListener: function ( type, listener ) {
 
-			get: function () {
+			if ( this._listeners === undefined ) { return; }
 
-				return this._z;
+			var listeners = this._listeners;
+			var listenerArray = listeners[ type ];
 
-			},
+			if ( listenerArray !== undefined ) {
 
-			set: function ( value ) {
+				var index = listenerArray.indexOf( listener );
 
-				this._z = value;
-				this._onChangeCallback();
+				if ( index !== - 1 ) {
+
+					listenerArray.splice( index, 1 );
+
+				}
 
 			}
 
 		},
 
-		w: {
+		dispatchEvent: function ( event ) {
 
-			get: function () {
+			if ( this._listeners === undefined ) { return; }
 
-				return this._w;
+			var listeners = this._listeners;
+			var listenerArray = listeners[ event.type ];
 
-			},
+			if ( listenerArray !== undefined ) {
 
-			set: function ( value ) {
+				event.target = this;
 
-				this._w = value;
-				this._onChangeCallback();
+				// Make a copy, in case listeners are removed while iterating.
+				var array = listenerArray.slice( 0 );
+
+				for ( var i = 0, l = array.length; i < l; i ++ ) {
+
+					array[ i ].call( this, event );
+
+				}
 
 			}
 
@@ -11224,523 +19378,269 @@ function simpleEnd(buf) {
 
 	} );
 
-	Object.assign( Quaternion.prototype, {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author thezwap
+	 */
 
-		isQuaternion: true,
+	var _lut = [];
 
-		set: function ( x, y, z, w ) {
+	for ( var i = 0; i < 256; i ++ ) {
 
-			this._x = x;
-			this._y = y;
-			this._z = z;
-			this._w = w;
+		_lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
 
-			this._onChangeCallback();
+	}
 
-			return this;
+	var MathUtils = {
 
-		},
+		DEG2RAD: Math.PI / 180,
+		RAD2DEG: 180 / Math.PI,
 
-		clone: function () {
+		generateUUID: function () {
 
-			return new this.constructor( this._x, this._y, this._z, this._w );
+			// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
 
-		},
+			var d0 = Math.random() * 0xffffffff | 0;
+			var d1 = Math.random() * 0xffffffff | 0;
+			var d2 = Math.random() * 0xffffffff | 0;
+			var d3 = Math.random() * 0xffffffff | 0;
+			var uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
+				_lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
+				_lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
+				_lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
 
-		copy: function ( quaternion ) {
+			// .toUpperCase() here flattens concatenated strings to save heap memory space.
+			return uuid.toUpperCase();
 
-			this._x = quaternion.x;
-			this._y = quaternion.y;
-			this._z = quaternion.z;
-			this._w = quaternion.w;
+		},
 
-			this._onChangeCallback();
+		clamp: function ( value, min, max ) {
 
-			return this;
+			return Math.max( min, Math.min( max, value ) );
 
 		},
 
-		setFromEuler: function ( euler, update ) {
+		// compute euclidian modulo of m % n
+		// https://en.wikipedia.org/wiki/Modulo_operation
 
-			if ( ! ( euler && euler.isEuler ) ) {
+		euclideanModulo: function ( n, m ) {
 
-				throw new Error( 'THREE.Quaternion: .setFromEuler() now expects an Euler rotation rather than a Vector3 and order.' );
+			return ( ( n % m ) + m ) % m;
 
-			}
+		},
 
-			var x = euler._x, y = euler._y, z = euler._z, order = euler.order;
+		// Linear mapping from range <a1, a2> to range <b1, b2>
 
-			// http://www.mathworks.com/matlabcentral/fileexchange/
-			// 	20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
-			//	content/SpinCalc.m
+		mapLinear: function ( x, a1, a2, b1, b2 ) {
 
-			var cos = Math.cos;
-			var sin = Math.sin;
+			return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
 
-			var c1 = cos( x / 2 );
-			var c2 = cos( y / 2 );
-			var c3 = cos( z / 2 );
+		},
 
-			var s1 = sin( x / 2 );
-			var s2 = sin( y / 2 );
-			var s3 = sin( z / 2 );
+		// https://en.wikipedia.org/wiki/Linear_interpolation
 
-			switch ( order ) {
+		lerp: function ( x, y, t ) {
 
-				case 'XYZ':
-					this._x = s1 * c2 * c3 + c1 * s2 * s3;
-					this._y = c1 * s2 * c3 - s1 * c2 * s3;
-					this._z = c1 * c2 * s3 + s1 * s2 * c3;
-					this._w = c1 * c2 * c3 - s1 * s2 * s3;
-					break;
+			return ( 1 - t ) * x + t * y;
 
-				case 'YXZ':
-					this._x = s1 * c2 * c3 + c1 * s2 * s3;
-					this._y = c1 * s2 * c3 - s1 * c2 * s3;
-					this._z = c1 * c2 * s3 - s1 * s2 * c3;
-					this._w = c1 * c2 * c3 + s1 * s2 * s3;
-					break;
+		},
 
-				case 'ZXY':
-					this._x = s1 * c2 * c3 - c1 * s2 * s3;
-					this._y = c1 * s2 * c3 + s1 * c2 * s3;
-					this._z = c1 * c2 * s3 + s1 * s2 * c3;
-					this._w = c1 * c2 * c3 - s1 * s2 * s3;
-					break;
+		// http://en.wikipedia.org/wiki/Smoothstep
 
-				case 'ZYX':
-					this._x = s1 * c2 * c3 - c1 * s2 * s3;
-					this._y = c1 * s2 * c3 + s1 * c2 * s3;
-					this._z = c1 * c2 * s3 - s1 * s2 * c3;
-					this._w = c1 * c2 * c3 + s1 * s2 * s3;
-					break;
+		smoothstep: function ( x, min, max ) {
 
-				case 'YZX':
-					this._x = s1 * c2 * c3 + c1 * s2 * s3;
-					this._y = c1 * s2 * c3 + s1 * c2 * s3;
-					this._z = c1 * c2 * s3 - s1 * s2 * c3;
-					this._w = c1 * c2 * c3 - s1 * s2 * s3;
-					break;
+			if ( x <= min ) { return 0; }
+			if ( x >= max ) { return 1; }
 
-				case 'XZY':
-					this._x = s1 * c2 * c3 - c1 * s2 * s3;
-					this._y = c1 * s2 * c3 - s1 * c2 * s3;
-					this._z = c1 * c2 * s3 + s1 * s2 * c3;
-					this._w = c1 * c2 * c3 + s1 * s2 * s3;
-					break;
+			x = ( x - min ) / ( max - min );
 
-				default:
-					console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order );
+			return x * x * ( 3 - 2 * x );
 
-			}
+		},
 
-			if ( update !== false ) { this._onChangeCallback(); }
+		smootherstep: function ( x, min, max ) {
 
-			return this;
+			if ( x <= min ) { return 0; }
+			if ( x >= max ) { return 1; }
 
-		},
+			x = ( x - min ) / ( max - min );
 
-		setFromAxisAngle: function ( axis, angle ) {
-
-			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
-
-			// assumes axis is normalized
-
-			var halfAngle = angle / 2, s = Math.sin( halfAngle );
-
-			this._x = axis.x * s;
-			this._y = axis.y * s;
-			this._z = axis.z * s;
-			this._w = Math.cos( halfAngle );
-
-			this._onChangeCallback();
-
-			return this;
-
-		},
-
-		setFromRotationMatrix: function ( m ) {
-
-			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
-
-			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
-
-			var te = m.elements,
-
-				m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
-				m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
-				m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
-
-				trace = m11 + m22 + m33,
-				s;
-
-			if ( trace > 0 ) {
-
-				s = 0.5 / Math.sqrt( trace + 1.0 );
-
-				this._w = 0.25 / s;
-				this._x = ( m32 - m23 ) * s;
-				this._y = ( m13 - m31 ) * s;
-				this._z = ( m21 - m12 ) * s;
-
-			} else if ( m11 > m22 && m11 > m33 ) {
-
-				s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
-
-				this._w = ( m32 - m23 ) / s;
-				this._x = 0.25 * s;
-				this._y = ( m12 + m21 ) / s;
-				this._z = ( m13 + m31 ) / s;
-
-			} else if ( m22 > m33 ) {
-
-				s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
-
-				this._w = ( m13 - m31 ) / s;
-				this._x = ( m12 + m21 ) / s;
-				this._y = 0.25 * s;
-				this._z = ( m23 + m32 ) / s;
-
-			} else {
-
-				s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
-
-				this._w = ( m21 - m12 ) / s;
-				this._x = ( m13 + m31 ) / s;
-				this._y = ( m23 + m32 ) / s;
-				this._z = 0.25 * s;
-
-			}
-
-			this._onChangeCallback();
-
-			return this;
-
-		},
-
-		setFromUnitVectors: function ( vFrom, vTo ) {
-
-			// assumes direction vectors vFrom and vTo are normalized
-
-			var EPS = 0.000001;
-
-			var r = vFrom.dot( vTo ) + 1;
-
-			if ( r < EPS ) {
-
-				r = 0;
-
-				if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
-
-					this._x = - vFrom.y;
-					this._y = vFrom.x;
-					this._z = 0;
-					this._w = r;
-
-				} else {
-
-					this._x = 0;
-					this._y = - vFrom.z;
-					this._z = vFrom.y;
-					this._w = r;
-
-				}
-
-			} else {
-
-				// crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
-
-				this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
-				this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
-				this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
-				this._w = r;
-
-			}
-
-			return this.normalize();
-
-		},
-
-		angleTo: function ( q ) {
-
-			return 2 * Math.acos( Math.abs( MathUtils.clamp( this.dot( q ), - 1, 1 ) ) );
-
-		},
-
-		rotateTowards: function ( q, step ) {
-
-			var angle = this.angleTo( q );
-
-			if ( angle === 0 ) { return this; }
-
-			var t = Math.min( 1, step / angle );
-
-			this.slerp( q, t );
-
-			return this;
+			return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
 
 		},
 
-		inverse: function () {
+		// Random integer from <low, high> interval
 
-			// quaternion is assumed to have unit length
+		randInt: function ( low, high ) {
 
-			return this.conjugate();
+			return low + Math.floor( Math.random() * ( high - low + 1 ) );
 
 		},
 
-		conjugate: function () {
-
-			this._x *= - 1;
-			this._y *= - 1;
-			this._z *= - 1;
+		// Random float from <low, high> interval
 
-			this._onChangeCallback();
+		randFloat: function ( low, high ) {
 
-			return this;
+			return low + Math.random() * ( high - low );
 
 		},
 
-		dot: function ( v ) {
-
-			return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
-
-		},
+		// Random float from <-range/2, range/2> interval
 
-		lengthSq: function () {
+		randFloatSpread: function ( range ) {
 
-			return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
+			return range * ( 0.5 - Math.random() );
 
 		},
 
-		length: function () {
+		degToRad: function ( degrees ) {
 
-			return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
+			return degrees * MathUtils.DEG2RAD;
 
 		},
 
-		normalize: function () {
-
-			var l = this.length();
-
-			if ( l === 0 ) {
-
-				this._x = 0;
-				this._y = 0;
-				this._z = 0;
-				this._w = 1;
-
-			} else {
-
-				l = 1 / l;
-
-				this._x = this._x * l;
-				this._y = this._y * l;
-				this._z = this._z * l;
-				this._w = this._w * l;
-
-			}
-
-			this._onChangeCallback();
+		radToDeg: function ( radians ) {
 
-			return this;
+			return radians * MathUtils.RAD2DEG;
 
 		},
 
-		multiply: function ( q, p ) {
-
-			if ( p !== undefined ) {
-
-				console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
-				return this.multiplyQuaternions( q, p );
-
-			}
+		isPowerOfTwo: function ( value ) {
 
-			return this.multiplyQuaternions( this, q );
+			return ( value & ( value - 1 ) ) === 0 && value !== 0;
 
 		},
 
-		premultiply: function ( q ) {
+		ceilPowerOfTwo: function ( value ) {
 
-			return this.multiplyQuaternions( q, this );
+			return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
 
 		},
 
-		multiplyQuaternions: function ( a, b ) {
-
-			// from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
-
-			var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
-			var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
-
-			this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
-			this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
-			this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
-			this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
-
-			this._onChangeCallback();
+		floorPowerOfTwo: function ( value ) {
 
-			return this;
+			return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
 
 		},
 
-		slerp: function ( qb, t ) {
-
-			if ( t === 0 ) { return this; }
-			if ( t === 1 ) { return this.copy( qb ); }
-
-			var x = this._x, y = this._y, z = this._z, w = this._w;
-
-			// http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
-
-			var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
+		setQuaternionFromProperEuler: function ( q, a, b, c, order ) {
 
-			if ( cosHalfTheta < 0 ) {
+			// Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
 
-				this._w = - qb._w;
-				this._x = - qb._x;
-				this._y = - qb._y;
-				this._z = - qb._z;
+			// rotations are applied to the axes in the order specified by 'order'
+			// rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
+			// angles are in radians
 
-				cosHalfTheta = - cosHalfTheta;
+			var cos = Math.cos;
+			var sin = Math.sin;
 
-			} else {
+			var c2 = cos( b / 2 );
+			var s2 = sin( b / 2 );
 
-				this.copy( qb );
+			var c13 = cos( ( a + c ) / 2 );
+			var s13 = sin( ( a + c ) / 2 );
 
-			}
+			var c1_3 = cos( ( a - c ) / 2 );
+			var s1_3 = sin( ( a - c ) / 2 );
 
-			if ( cosHalfTheta >= 1.0 ) {
+			var c3_1 = cos( ( c - a ) / 2 );
+			var s3_1 = sin( ( c - a ) / 2 );
 
-				this._w = w;
-				this._x = x;
-				this._y = y;
-				this._z = z;
+			switch ( order ) {
 
-				return this;
+				case 'XYX':
+					q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
+					break;
 
-			}
+				case 'YZY':
+					q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
+					break;
 
-			var sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
+				case 'ZXZ':
+					q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
+					break;
 
-			if ( sqrSinHalfTheta <= Number.EPSILON ) {
+				case 'XZX':
+					q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
+					break;
 
-				var s = 1 - t;
-				this._w = s * w + t * this._w;
-				this._x = s * x + t * this._x;
-				this._y = s * y + t * this._y;
-				this._z = s * z + t * this._z;
+				case 'YXY':
+					q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
+					break;
 
-				this.normalize();
-				this._onChangeCallback();
+				case 'ZYZ':
+					q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
+					break;
 
-				return this;
+				default:
+					console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
 
 			}
 
-			var sinHalfTheta = Math.sqrt( sqrSinHalfTheta );
-			var halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta );
-			var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
-				ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
-
-			this._w = ( w * ratioA + this._w * ratioB );
-			this._x = ( x * ratioA + this._x * ratioB );
-			this._y = ( y * ratioA + this._y * ratioB );
-			this._z = ( z * ratioA + this._z * ratioB );
-
-			this._onChangeCallback();
-
-			return this;
-
-		},
+		}
 
-		equals: function ( quaternion ) {
+	};
 
-			return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author philogb / http://blog.thejit.org/
+	 * @author egraether / http://egraether.com/
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 */
 
-		},
+	function Vector2( x, y ) {
 
-		fromArray: function ( array, offset ) {
+		this.x = x || 0;
+		this.y = y || 0;
 
-			if ( offset === undefined ) { offset = 0; }
+	}
 
-			this._x = array[ offset ];
-			this._y = array[ offset + 1 ];
-			this._z = array[ offset + 2 ];
-			this._w = array[ offset + 3 ];
+	Object.defineProperties( Vector2.prototype, {
 
-			this._onChangeCallback();
+		"width": {
 
-			return this;
+			get: function () {
 
-		},
+				return this.x;
 
-		toArray: function ( array, offset ) {
+			},
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+			set: function ( value ) {
 
-			array[ offset ] = this._x;
-			array[ offset + 1 ] = this._y;
-			array[ offset + 2 ] = this._z;
-			array[ offset + 3 ] = this._w;
+				this.x = value;
 
-			return array;
+			}
 
 		},
 
-		fromBufferAttribute: function ( attribute, index ) {
-
-			this._x = attribute.getX( index );
-			this._y = attribute.getY( index );
-			this._z = attribute.getZ( index );
-			this._w = attribute.getW( index );
+		"height": {
 
-			return this;
+			get: function () {
 
-		},
+				return this.y;
 
-		_onChange: function ( callback ) {
+			},
 
-			this._onChangeCallback = callback;
+			set: function ( value ) {
 
-			return this;
+				this.y = value;
 
-		},
+			}
 
-		_onChangeCallback: function () {}
+		}
 
 	} );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author kile / http://kile.stravaganza.org/
-	 * @author philogb / http://blog.thejit.org/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author egraether / http://egraether.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
-
-	var _vector = new Vector3();
-	var _quaternion = new Quaternion();
-
-	function Vector3( x, y, z ) {
-
-		this.x = x || 0;
-		this.y = y || 0;
-		this.z = z || 0;
-
-	}
-
-	Object.assign( Vector3.prototype, {
+	Object.assign( Vector2.prototype, {
 
-		isVector3: true,
+		isVector2: true,
 
-		set: function ( x, y, z ) {
+		set: function ( x, y ) {
 
 			this.x = x;
 			this.y = y;
-			this.z = z;
 
 			return this;
 
@@ -11750,7 +19650,6 @@ function simpleEnd(buf) {
 
 			this.x = scalar;
 			this.y = scalar;
-			this.z = scalar;
 
 			return this;
 
@@ -11772,21 +19671,12 @@ function simpleEnd(buf) {
 
 		},
 
-		setZ: function ( z ) {
-
-			this.z = z;
-
-			return this;
-
-		},
-
 		setComponent: function ( index, value ) {
 
 			switch ( index ) {
 
 				case 0: this.x = value; break;
 				case 1: this.y = value; break;
-				case 2: this.z = value; break;
 				default: throw new Error( 'index is out of range: ' + index );
 
 			}
@@ -11801,7 +19691,6 @@ function simpleEnd(buf) {
 
 				case 0: return this.x;
 				case 1: return this.y;
-				case 2: return this.z;
 				default: throw new Error( 'index is out of range: ' + index );
 
 			}
@@ -11810,7 +19699,7 @@ function simpleEnd(buf) {
 
 		clone: function () {
 
-			return new this.constructor( this.x, this.y, this.z );
+			return new this.constructor( this.x, this.y );
 
 		},
 
@@ -11818,7 +19707,6 @@ function simpleEnd(buf) {
 
 			this.x = v.x;
 			this.y = v.y;
-			this.z = v.z;
 
 			return this;
 
@@ -11828,14 +19716,13 @@ function simpleEnd(buf) {
 
 			if ( w !== undefined ) {
 
-				console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
+				console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
 				return this.addVectors( v, w );
 
 			}
 
 			this.x += v.x;
 			this.y += v.y;
-			this.z += v.z;
 
 			return this;
 
@@ -11845,7 +19732,6 @@ function simpleEnd(buf) {
 
 			this.x += s;
 			this.y += s;
-			this.z += s;
 
 			return this;
 
@@ -11855,7 +19741,6 @@ function simpleEnd(buf) {
 
 			this.x = a.x + b.x;
 			this.y = a.y + b.y;
-			this.z = a.z + b.z;
 
 			return this;
 
@@ -11865,7 +19750,6 @@ function simpleEnd(buf) {
 
 			this.x += v.x * s;
 			this.y += v.y * s;
-			this.z += v.z * s;
 
 			return this;
 
@@ -11875,14 +19759,13 @@ function simpleEnd(buf) {
 
 			if ( w !== undefined ) {
 
-				console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
+				console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
 				return this.subVectors( v, w );
 
 			}
 
 			this.x -= v.x;
 			this.y -= v.y;
-			this.z -= v.z;
 
 			return this;
 
@@ -11892,7 +19775,6 @@ function simpleEnd(buf) {
 
 			this.x -= s;
 			this.y -= s;
-			this.z -= s;
 
 			return this;
 
@@ -11902,24 +19784,15 @@ function simpleEnd(buf) {
 
 			this.x = a.x - b.x;
 			this.y = a.y - b.y;
-			this.z = a.z - b.z;
 
 			return this;
 
 		},
 
-		multiply: function ( v, w ) {
-
-			if ( w !== undefined ) {
-
-				console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
-				return this.multiplyVectors( v, w );
-
-			}
+		multiply: function ( v ) {
 
 			this.x *= v.x;
 			this.y *= v.y;
-			this.z *= v.z;
 
 			return this;
 
@@ -11929,187 +19802,81 @@ function simpleEnd(buf) {
 
 			this.x *= scalar;
 			this.y *= scalar;
-			this.z *= scalar;
 
 			return this;
 
 		},
 
-		multiplyVectors: function ( a, b ) {
+		divide: function ( v ) {
 
-			this.x = a.x * b.x;
-			this.y = a.y * b.y;
-			this.z = a.z * b.z;
+			this.x /= v.x;
+			this.y /= v.y;
 
 			return this;
 
 		},
 
-		applyEuler: function ( euler ) {
-
-			if ( ! ( euler && euler.isEuler ) ) {
-
-				console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
-
-			}
-
-			return this.applyQuaternion( _quaternion.setFromEuler( euler ) );
-
-		},
-
-		applyAxisAngle: function ( axis, angle ) {
+		divideScalar: function ( scalar ) {
 
-			return this.applyQuaternion( _quaternion.setFromAxisAngle( axis, angle ) );
+			return this.multiplyScalar( 1 / scalar );
 
 		},
 
 		applyMatrix3: function ( m ) {
 
-			var x = this.x, y = this.y, z = this.z;
+			var x = this.x, y = this.y;
 			var e = m.elements;
 
-			this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
-			this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
-			this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
+			this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
+			this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
 
 			return this;
 
 		},
 
-		applyNormalMatrix: function ( m ) {
-
-			return this.applyMatrix3( m ).normalize();
+		min: function ( v ) {
 
-		},
+			this.x = Math.min( this.x, v.x );
+			this.y = Math.min( this.y, v.y );
 
-		applyMatrix4: function ( m ) {
+			return this;
 
-			var x = this.x, y = this.y, z = this.z;
-			var e = m.elements;
+		},
 
-			var w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
+		max: function ( v ) {
 
-			this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
-			this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
-			this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
+			this.x = Math.max( this.x, v.x );
+			this.y = Math.max( this.y, v.y );
 
 			return this;
 
 		},
 
-		applyQuaternion: function ( q ) {
+		clamp: function ( min, max ) {
 
-			var x = this.x, y = this.y, z = this.z;
-			var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
+			// assumes min < max, componentwise
 
-			// calculate quat * vector
+			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
+			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
 
-			var ix = qw * x + qy * z - qz * y;
-			var iy = qw * y + qz * x - qx * z;
-			var iz = qw * z + qx * y - qy * x;
-			var iw = - qx * x - qy * y - qz * z;
+			return this;
 
-			// calculate result * inverse quat
+		},
 
-			this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
-			this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
-			this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
+		clampScalar: function ( minVal, maxVal ) {
+
+			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
+			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
 
 			return this;
 
 		},
 
-		project: function ( camera ) {
+		clampLength: function ( min, max ) {
 
-			return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
-
-		},
-
-		unproject: function ( camera ) {
-
-			return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
-
-		},
-
-		transformDirection: function ( m ) {
-
-			// input: THREE.Matrix4 affine matrix
-			// vector interpreted as a direction
-
-			var x = this.x, y = this.y, z = this.z;
-			var e = m.elements;
-
-			this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
-			this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
-			this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
-
-			return this.normalize();
-
-		},
-
-		divide: function ( v ) {
-
-			this.x /= v.x;
-			this.y /= v.y;
-			this.z /= v.z;
-
-			return this;
-
-		},
-
-		divideScalar: function ( scalar ) {
-
-			return this.multiplyScalar( 1 / scalar );
-
-		},
-
-		min: function ( v ) {
-
-			this.x = Math.min( this.x, v.x );
-			this.y = Math.min( this.y, v.y );
-			this.z = Math.min( this.z, v.z );
-
-			return this;
-
-		},
-
-		max: function ( v ) {
-
-			this.x = Math.max( this.x, v.x );
-			this.y = Math.max( this.y, v.y );
-			this.z = Math.max( this.z, v.z );
-
-			return this;
-
-		},
-
-		clamp: function ( min, max ) {
-
-			// assumes min < max, componentwise
-
-			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
-			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
-			this.z = Math.max( min.z, Math.min( max.z, this.z ) );
-
-			return this;
-
-		},
-
-		clampScalar: function ( minVal, maxVal ) {
-
-			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
-			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
-			this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
-
-			return this;
-
-		},
-
-		clampLength: function ( min, max ) {
-
-			var length = this.length();
-
-			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
+			var length = this.length();
+
+			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
 
 		},
 
@@ -12117,7 +19884,6 @@ function simpleEnd(buf) {
 
 			this.x = Math.floor( this.x );
 			this.y = Math.floor( this.y );
-			this.z = Math.floor( this.z );
 
 			return this;
 
@@ -12127,7 +19893,6 @@ function simpleEnd(buf) {
 
 			this.x = Math.ceil( this.x );
 			this.y = Math.ceil( this.y );
-			this.z = Math.ceil( this.z );
 
 			return this;
 
@@ -12137,7 +19902,6 @@ function simpleEnd(buf) {
 
 			this.x = Math.round( this.x );
 			this.y = Math.round( this.y );
-			this.z = Math.round( this.z );
 
 			return this;
 
@@ -12147,7 +19911,6 @@ function simpleEnd(buf) {
 
 			this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
 			this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
-			this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
 
 			return this;
 
@@ -12157,7 +19920,6 @@ function simpleEnd(buf) {
 
 			this.x = - this.x;
 			this.y = - this.y;
-			this.z = - this.z;
 
 			return this;
 
@@ -12165,27 +19927,31 @@ function simpleEnd(buf) {
 
 		dot: function ( v ) {
 
-			return this.x * v.x + this.y * v.y + this.z * v.z;
+			return this.x * v.x + this.y * v.y;
 
 		},
 
-		// TODO lengthSquared?
+		cross: function ( v ) {
+
+			return this.x * v.y - this.y * v.x;
+
+		},
 
 		lengthSq: function () {
 
-			return this.x * this.x + this.y * this.y + this.z * this.z;
+			return this.x * this.x + this.y * this.y;
 
 		},
 
 		length: function () {
 
-			return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
+			return Math.sqrt( this.x * this.x + this.y * this.y );
 
 		},
 
 		manhattanLength: function () {
 
-			return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
+			return Math.abs( this.x ) + Math.abs( this.y );
 
 		},
 
@@ -12195,94 +19961,13 @@ function simpleEnd(buf) {
 
 		},
 
-		setLength: function ( length ) {
-
-			return this.normalize().multiplyScalar( length );
-
-		},
-
-		lerp: function ( v, alpha ) {
-
-			this.x += ( v.x - this.x ) * alpha;
-			this.y += ( v.y - this.y ) * alpha;
-			this.z += ( v.z - this.z ) * alpha;
-
-			return this;
-
-		},
-
-		lerpVectors: function ( v1, v2, alpha ) {
-
-			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
-
-		},
-
-		cross: function ( v, w ) {
-
-			if ( w !== undefined ) {
-
-				console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
-				return this.crossVectors( v, w );
-
-			}
-
-			return this.crossVectors( this, v );
-
-		},
-
-		crossVectors: function ( a, b ) {
-
-			var ax = a.x, ay = a.y, az = a.z;
-			var bx = b.x, by = b.y, bz = b.z;
-
-			this.x = ay * bz - az * by;
-			this.y = az * bx - ax * bz;
-			this.z = ax * by - ay * bx;
-
-			return this;
-
-		},
-
-		projectOnVector: function ( v ) {
-
-			var denominator = v.lengthSq();
-
-			if ( denominator === 0 ) { return this.set( 0, 0, 0 ); }
-
-			var scalar = v.dot( this ) / denominator;
-
-			return this.copy( v ).multiplyScalar( scalar );
-
-		},
-
-		projectOnPlane: function ( planeNormal ) {
-
-			_vector.copy( this ).projectOnVector( planeNormal );
-
-			return this.sub( _vector );
-
-		},
-
-		reflect: function ( normal ) {
-
-			// reflect incident vector off plane orthogonal to normal
-			// normal is assumed to have unit length
-
-			return this.sub( _vector.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
-
-		},
-
-		angleTo: function ( v ) {
-
-			var denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
-
-			if ( denominator === 0 ) { return Math.PI / 2; }
+		angle: function () {
 
-			var theta = this.dot( v ) / denominator;
+			// computes the angle in radians with respect to the positive x-axis
 
-			// clamp, to handle numerical problems
+			var angle = Math.atan2( - this.y, - this.x ) + Math.PI;
 
-			return Math.acos( MathUtils.clamp( theta, - 1, 1 ) );
+			return angle;
 
 		},
 
@@ -12294,93 +19979,41 @@ function simpleEnd(buf) {
 
 		distanceToSquared: function ( v ) {
 
-			var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
-
-			return dx * dx + dy * dy + dz * dz;
+			var dx = this.x - v.x, dy = this.y - v.y;
+			return dx * dx + dy * dy;
 
 		},
 
 		manhattanDistanceTo: function ( v ) {
 
-			return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
-
-		},
-
-		setFromSpherical: function ( s ) {
-
-			return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
-
-		},
-
-		setFromSphericalCoords: function ( radius, phi, theta ) {
-
-			var sinPhiRadius = Math.sin( phi ) * radius;
-
-			this.x = sinPhiRadius * Math.sin( theta );
-			this.y = Math.cos( phi ) * radius;
-			this.z = sinPhiRadius * Math.cos( theta );
-
-			return this;
-
-		},
-
-		setFromCylindrical: function ( c ) {
-
-			return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
-
-		},
-
-		setFromCylindricalCoords: function ( radius, theta, y ) {
-
-			this.x = radius * Math.sin( theta );
-			this.y = y;
-			this.z = radius * Math.cos( theta );
-
-			return this;
+			return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
 
 		},
 
-		setFromMatrixPosition: function ( m ) {
-
-			var e = m.elements;
-
-			this.x = e[ 12 ];
-			this.y = e[ 13 ];
-			this.z = e[ 14 ];
+		setLength: function ( length ) {
 
-			return this;
+			return this.normalize().multiplyScalar( length );
 
 		},
 
-		setFromMatrixScale: function ( m ) {
-
-			var sx = this.setFromMatrixColumn( m, 0 ).length();
-			var sy = this.setFromMatrixColumn( m, 1 ).length();
-			var sz = this.setFromMatrixColumn( m, 2 ).length();
+		lerp: function ( v, alpha ) {
 
-			this.x = sx;
-			this.y = sy;
-			this.z = sz;
+			this.x += ( v.x - this.x ) * alpha;
+			this.y += ( v.y - this.y ) * alpha;
 
 			return this;
 
 		},
 
-		setFromMatrixColumn: function ( m, index ) {
-
-			return this.fromArray( m.elements, index * 4 );
-
-		},
-
-		setFromMatrix3Column: function ( m, index ) {
+		lerpVectors: function ( v1, v2, alpha ) {
 
-			return this.fromArray( m.elements, index * 3 );
+			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
 
 		},
 
 		equals: function ( v ) {
 
-			return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
+			return ( ( v.x === this.x ) && ( v.y === this.y ) );
 
 		},
 
@@ -12390,7 +20023,6 @@ function simpleEnd(buf) {
 
 			this.x = array[ offset ];
 			this.y = array[ offset + 1 ];
-			this.z = array[ offset + 2 ];
 
 			return this;
 
@@ -12403,7 +20035,6 @@ function simpleEnd(buf) {
 
 			array[ offset ] = this.x;
 			array[ offset + 1 ] = this.y;
-			array[ offset + 2 ] = this.z;
 
 			return array;
 
@@ -12413,13 +20044,26 @@ function simpleEnd(buf) {
 
 			if ( offset !== undefined ) {
 
-				console.warn( 'THREE.Vector3: offset has been removed from .fromBufferAttribute().' );
+				console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' );
 
 			}
 
 			this.x = attribute.getX( index );
 			this.y = attribute.getY( index );
-			this.z = attribute.getZ( index );
+
+			return this;
+
+		},
+
+		rotateAround: function ( center, angle ) {
+
+			var c = Math.cos( angle ), s = Math.sin( angle );
+
+			var x = this.x - center.x;
+			var y = this.y - center.y;
+
+			this.x = x * c - y * s + center.x;
+			this.y = x * s + y * c + center.y;
 
 			return this;
 
@@ -12429,7 +20073,6 @@ function simpleEnd(buf) {
 
 			this.x = Math.random();
 			this.y = Math.random();
-			this.z = Math.random();
 
 			return this;
 
@@ -12437,58 +20080,42 @@ function simpleEnd(buf) {
 
 	} );
 
-	var _v1 = new Vector3();
-	var _m1 = new Matrix4();
-	var _zero = new Vector3( 0, 0, 0 );
-	var _one = new Vector3( 1, 1, 1 );
-	var _x = new Vector3();
-	var _y = new Vector3();
-	var _z = new Vector3();
-
 	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author supereggbert / http://www.paulbrunt.co.uk/
-	 * @author philogb / http://blog.thejit.org/
-	 * @author jordi_ros / http://plattsoft.com
-	 * @author D1plo1d / http://github.com/D1plo1d
 	 * @author alteredq / http://alteredqualia.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author timknip / http://www.floorplanner.com/
-	 * @author bhouston / http://clara.io
 	 * @author WestLangley / http://github.com/WestLangley
+	 * @author bhouston / http://clara.io
+	 * @author tschw
 	 */
 
-	function Matrix4() {
+	function Matrix3() {
 
 		this.elements = [
 
-			1, 0, 0, 0,
-			0, 1, 0, 0,
-			0, 0, 1, 0,
-			0, 0, 0, 1
+			1, 0, 0,
+			0, 1, 0,
+			0, 0, 1
 
 		];
 
 		if ( arguments.length > 0 ) {
 
-			console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
+			console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' );
 
 		}
 
 	}
 
-	Object.assign( Matrix4.prototype, {
+	Object.assign( Matrix3.prototype, {
 
-		isMatrix4: true,
+		isMatrix3: true,
 
-		set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
+		set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
 
 			var te = this.elements;
 
-			te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
-			te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
-			te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
-			te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
+			te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31;
+			te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32;
+			te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33;
 
 			return this;
 
@@ -12498,10 +20125,9 @@ function simpleEnd(buf) {
 
 			this.set(
 
-				1, 0, 0, 0,
-				0, 1, 0, 0,
-				0, 0, 1, 0,
-				0, 0, 0, 1
+				1, 0, 0,
+				0, 1, 0,
+				0, 0, 1
 
 			);
 
@@ -12511,7 +20137,7 @@ function simpleEnd(buf) {
 
 		clone: function () {
 
-			return new Matrix4().fromArray( this.elements );
+			return new this.constructor().fromArray( this.elements );
 
 		},
 
@@ -12520,3766 +20146,3848 @@ function simpleEnd(buf) {
 			var te = this.elements;
 			var me = m.elements;
 
-			te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ];
-			te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ];
-			te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ];
-			te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ];
+			te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ];
+			te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ];
+			te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ];
 
 			return this;
 
 		},
 
-		copyPosition: function ( m ) {
-
-			var te = this.elements, me = m.elements;
+		extractBasis: function ( xAxis, yAxis, zAxis ) {
 
-			te[ 12 ] = me[ 12 ];
-			te[ 13 ] = me[ 13 ];
-			te[ 14 ] = me[ 14 ];
+			xAxis.setFromMatrix3Column( this, 0 );
+			yAxis.setFromMatrix3Column( this, 1 );
+			zAxis.setFromMatrix3Column( this, 2 );
 
 			return this;
 
 		},
 
-		extractBasis: function ( xAxis, yAxis, zAxis ) {
-
-			xAxis.setFromMatrixColumn( this, 0 );
-			yAxis.setFromMatrixColumn( this, 1 );
-			zAxis.setFromMatrixColumn( this, 2 );
+		setFromMatrix4: function ( m ) {
 
-			return this;
+			var me = m.elements;
 
-		},
+			this.set(
 
-		makeBasis: function ( xAxis, yAxis, zAxis ) {
+				me[ 0 ], me[ 4 ], me[ 8 ],
+				me[ 1 ], me[ 5 ], me[ 9 ],
+				me[ 2 ], me[ 6 ], me[ 10 ]
 
-			this.set(
-				xAxis.x, yAxis.x, zAxis.x, 0,
-				xAxis.y, yAxis.y, zAxis.y, 0,
-				xAxis.z, yAxis.z, zAxis.z, 0,
-				0, 0, 0, 1
 			);
 
 			return this;
 
 		},
 
-		extractRotation: function ( m ) {
+		multiply: function ( m ) {
 
-			// this method does not support reflection matrices
+			return this.multiplyMatrices( this, m );
+
+		},
+
+		premultiply: function ( m ) {
+
+			return this.multiplyMatrices( m, this );
+
+		},
+
+		multiplyMatrices: function ( a, b ) {
 
+			var ae = a.elements;
+			var be = b.elements;
 			var te = this.elements;
-			var me = m.elements;
 
-			var scaleX = 1 / _v1.setFromMatrixColumn( m, 0 ).length();
-			var scaleY = 1 / _v1.setFromMatrixColumn( m, 1 ).length();
-			var scaleZ = 1 / _v1.setFromMatrixColumn( m, 2 ).length();
+			var a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ];
+			var a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ];
+			var a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ];
 
-			te[ 0 ] = me[ 0 ] * scaleX;
-			te[ 1 ] = me[ 1 ] * scaleX;
-			te[ 2 ] = me[ 2 ] * scaleX;
-			te[ 3 ] = 0;
+			var b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ];
+			var b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ];
+			var b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ];
 
-			te[ 4 ] = me[ 4 ] * scaleY;
-			te[ 5 ] = me[ 5 ] * scaleY;
-			te[ 6 ] = me[ 6 ] * scaleY;
-			te[ 7 ] = 0;
+			te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31;
+			te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32;
+			te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33;
 
-			te[ 8 ] = me[ 8 ] * scaleZ;
-			te[ 9 ] = me[ 9 ] * scaleZ;
-			te[ 10 ] = me[ 10 ] * scaleZ;
-			te[ 11 ] = 0;
+			te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31;
+			te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32;
+			te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33;
 
-			te[ 12 ] = 0;
-			te[ 13 ] = 0;
-			te[ 14 ] = 0;
-			te[ 15 ] = 1;
+			te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31;
+			te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32;
+			te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33;
 
 			return this;
 
 		},
 
-		makeRotationFromEuler: function ( euler ) {
+		multiplyScalar: function ( s ) {
 
-			if ( ! ( euler && euler.isEuler ) ) {
+			var te = this.elements;
 
-				console.error( 'THREE.Matrix4: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
+			te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
+			te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
+			te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
 
-			}
+			return this;
+
+		},
+
+		determinant: function () {
 
 			var te = this.elements;
 
-			var x = euler.x, y = euler.y, z = euler.z;
-			var a = Math.cos( x ), b = Math.sin( x );
-			var c = Math.cos( y ), d = Math.sin( y );
-			var e = Math.cos( z ), f = Math.sin( z );
+			var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
+				d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
+				g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
 
-			if ( euler.order === 'XYZ' ) {
+			return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
 
-				var ae = a * e, af = a * f, be = b * e, bf = b * f;
+		},
 
-				te[ 0 ] = c * e;
-				te[ 4 ] = - c * f;
-				te[ 8 ] = d;
+		getInverse: function ( matrix, throwOnDegenerate ) {
 
-				te[ 1 ] = af + be * d;
-				te[ 5 ] = ae - bf * d;
-				te[ 9 ] = - b * c;
+			if ( throwOnDegenerate !== undefined ) {
 
-				te[ 2 ] = bf - ae * d;
-				te[ 6 ] = be + af * d;
-				te[ 10 ] = a * c;
+				console.warn( "THREE.Matrix3: .getInverse() can no longer be configured to throw on degenerate." );
 
-			} else if ( euler.order === 'YXZ' ) {
+			}
 
-				var ce = c * e, cf = c * f, de = d * e, df = d * f;
+			var me = matrix.elements,
+				te = this.elements,
 
-				te[ 0 ] = ce + df * b;
-				te[ 4 ] = de * b - cf;
-				te[ 8 ] = a * d;
+				n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ],
+				n12 = me[ 3 ], n22 = me[ 4 ], n32 = me[ 5 ],
+				n13 = me[ 6 ], n23 = me[ 7 ], n33 = me[ 8 ],
 
-				te[ 1 ] = a * f;
-				te[ 5 ] = a * e;
-				te[ 9 ] = - b;
+				t11 = n33 * n22 - n32 * n23,
+				t12 = n32 * n13 - n33 * n12,
+				t13 = n23 * n12 - n22 * n13,
 
-				te[ 2 ] = cf * b - de;
-				te[ 6 ] = df + ce * b;
-				te[ 10 ] = a * c;
+				det = n11 * t11 + n21 * t12 + n31 * t13;
 
-			} else if ( euler.order === 'ZXY' ) {
+			if ( det === 0 ) { return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); }
 
-				var ce = c * e, cf = c * f, de = d * e, df = d * f;
+			var detInv = 1 / det;
 
-				te[ 0 ] = ce - df * b;
-				te[ 4 ] = - a * f;
-				te[ 8 ] = de + cf * b;
+			te[ 0 ] = t11 * detInv;
+			te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
+			te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
 
-				te[ 1 ] = cf + de * b;
-				te[ 5 ] = a * e;
-				te[ 9 ] = df - ce * b;
+			te[ 3 ] = t12 * detInv;
+			te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
+			te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
 
-				te[ 2 ] = - a * d;
-				te[ 6 ] = b;
-				te[ 10 ] = a * c;
+			te[ 6 ] = t13 * detInv;
+			te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
+			te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
 
-			} else if ( euler.order === 'ZYX' ) {
+			return this;
 
-				var ae = a * e, af = a * f, be = b * e, bf = b * f;
+		},
 
-				te[ 0 ] = c * e;
-				te[ 4 ] = be * d - af;
-				te[ 8 ] = ae * d + bf;
-
-				te[ 1 ] = c * f;
-				te[ 5 ] = bf * d + ae;
-				te[ 9 ] = af * d - be;
+		transpose: function () {
 
-				te[ 2 ] = - d;
-				te[ 6 ] = b * c;
-				te[ 10 ] = a * c;
+			var tmp, m = this.elements;
 
-			} else if ( euler.order === 'YZX' ) {
+			tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
+			tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
+			tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
 
-				var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
+			return this;
 
-				te[ 0 ] = c * e;
-				te[ 4 ] = bd - ac * f;
-				te[ 8 ] = bc * f + ad;
+		},
 
-				te[ 1 ] = f;
-				te[ 5 ] = a * e;
-				te[ 9 ] = - b * e;
+		getNormalMatrix: function ( matrix4 ) {
 
-				te[ 2 ] = - d * e;
-				te[ 6 ] = ad * f + bc;
-				te[ 10 ] = ac - bd * f;
+			return this.setFromMatrix4( matrix4 ).getInverse( this ).transpose();
 
-			} else if ( euler.order === 'XZY' ) {
+		},
 
-				var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
+		transposeIntoArray: function ( r ) {
 
-				te[ 0 ] = c * e;
-				te[ 4 ] = - f;
-				te[ 8 ] = d * e;
+			var m = this.elements;
 
-				te[ 1 ] = ac * f + bd;
-				te[ 5 ] = a * e;
-				te[ 9 ] = ad * f - bc;
+			r[ 0 ] = m[ 0 ];
+			r[ 1 ] = m[ 3 ];
+			r[ 2 ] = m[ 6 ];
+			r[ 3 ] = m[ 1 ];
+			r[ 4 ] = m[ 4 ];
+			r[ 5 ] = m[ 7 ];
+			r[ 6 ] = m[ 2 ];
+			r[ 7 ] = m[ 5 ];
+			r[ 8 ] = m[ 8 ];
 
-				te[ 2 ] = bc * f - ad;
-				te[ 6 ] = b * e;
-				te[ 10 ] = bd * f + ac;
+			return this;
 
-			}
+		},
 
-			// bottom row
-			te[ 3 ] = 0;
-			te[ 7 ] = 0;
-			te[ 11 ] = 0;
+		setUvTransform: function ( tx, ty, sx, sy, rotation, cx, cy ) {
 
-			// last column
-			te[ 12 ] = 0;
-			te[ 13 ] = 0;
-			te[ 14 ] = 0;
-			te[ 15 ] = 1;
+			var c = Math.cos( rotation );
+			var s = Math.sin( rotation );
 
-			return this;
+			this.set(
+				sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
+				- sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
+				0, 0, 1
+			);
 
 		},
 
-		makeRotationFromQuaternion: function ( q ) {
+		scale: function ( sx, sy ) {
 
-			return this.compose( _zero, q, _one );
+			var te = this.elements;
+
+			te[ 0 ] *= sx; te[ 3 ] *= sx; te[ 6 ] *= sx;
+			te[ 1 ] *= sy; te[ 4 ] *= sy; te[ 7 ] *= sy;
+
+			return this;
 
 		},
 
-		lookAt: function ( eye, target, up ) {
+		rotate: function ( theta ) {
+
+			var c = Math.cos( theta );
+			var s = Math.sin( theta );
 
 			var te = this.elements;
 
-			_z.subVectors( eye, target );
+			var a11 = te[ 0 ], a12 = te[ 3 ], a13 = te[ 6 ];
+			var a21 = te[ 1 ], a22 = te[ 4 ], a23 = te[ 7 ];
 
-			if ( _z.lengthSq() === 0 ) {
+			te[ 0 ] = c * a11 + s * a21;
+			te[ 3 ] = c * a12 + s * a22;
+			te[ 6 ] = c * a13 + s * a23;
 
-				// eye and target are in the same position
+			te[ 1 ] = - s * a11 + c * a21;
+			te[ 4 ] = - s * a12 + c * a22;
+			te[ 7 ] = - s * a13 + c * a23;
 
-				_z.z = 1;
+			return this;
 
-			}
+		},
 
-			_z.normalize();
-			_x.crossVectors( up, _z );
+		translate: function ( tx, ty ) {
 
-			if ( _x.lengthSq() === 0 ) {
+			var te = this.elements;
 
-				// up and z are parallel
+			te[ 0 ] += tx * te[ 2 ]; te[ 3 ] += tx * te[ 5 ]; te[ 6 ] += tx * te[ 8 ];
+			te[ 1 ] += ty * te[ 2 ]; te[ 4 ] += ty * te[ 5 ]; te[ 7 ] += ty * te[ 8 ];
 
-				if ( Math.abs( up.z ) === 1 ) {
+			return this;
 
-					_z.x += 0.0001;
+		},
 
-				} else {
+		equals: function ( matrix ) {
 
-					_z.z += 0.0001;
+			var te = this.elements;
+			var me = matrix.elements;
 
-				}
+			for ( var i = 0; i < 9; i ++ ) {
 
-				_z.normalize();
-				_x.crossVectors( up, _z );
+				if ( te[ i ] !== me[ i ] ) { return false; }
 
 			}
 
-			_x.normalize();
-			_y.crossVectors( _z, _x );
-
-			te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
-			te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
-			te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;
-
-			return this;
+			return true;
 
 		},
 
-		multiply: function ( m, n ) {
+		fromArray: function ( array, offset ) {
 
-			if ( n !== undefined ) {
+			if ( offset === undefined ) { offset = 0; }
 
-				console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
-				return this.multiplyMatrices( m, n );
+			for ( var i = 0; i < 9; i ++ ) {
+
+				this.elements[ i ] = array[ i + offset ];
 
 			}
 
-			return this.multiplyMatrices( this, m );
+			return this;
 
 		},
 
-		premultiply: function ( m ) {
-
-			return this.multiplyMatrices( m, this );
-
-		},
+		toArray: function ( array, offset ) {
 
-		multiplyMatrices: function ( a, b ) {
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			var ae = a.elements;
-			var be = b.elements;
 			var te = this.elements;
 
-			var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
-			var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
-			var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
-			var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
-
-			var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
-			var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
-			var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
-			var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
-
-			te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
-			te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
-			te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
-			te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
+			array[ offset ] = te[ 0 ];
+			array[ offset + 1 ] = te[ 1 ];
+			array[ offset + 2 ] = te[ 2 ];
 
-			te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
-			te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
-			te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
-			te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
+			array[ offset + 3 ] = te[ 3 ];
+			array[ offset + 4 ] = te[ 4 ];
+			array[ offset + 5 ] = te[ 5 ];
 
-			te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
-			te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
-			te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
-			te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
+			array[ offset + 6 ] = te[ 6 ];
+			array[ offset + 7 ] = te[ 7 ];
+			array[ offset + 8 ] = te[ 8 ];
 
-			te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
-			te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
-			te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
-			te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
+			return array;
 
-			return this;
+		}
 
-		},
+	} );
 
-		multiplyScalar: function ( s ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author szimek / https://github.com/szimek/
+	 */
 
-			var te = this.elements;
+	var _canvas;
 
-			te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
-			te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
-			te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
-			te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
+	var ImageUtils = {
 
-			return this;
+		getDataURL: function ( image ) {
 
-		},
+			var canvas;
 
-		determinant: function () {
+			if ( typeof HTMLCanvasElement == 'undefined' ) {
 
-			var te = this.elements;
+				return image.src;
 
-			var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
-			var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
-			var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
-			var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
+			} else if ( image instanceof HTMLCanvasElement ) {
 
-			//TODO: make this more efficient
-			//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
+				canvas = image;
 
-			return (
-				n41 * (
-					+ n14 * n23 * n32
-					 - n13 * n24 * n32
-					 - n14 * n22 * n33
-					 + n12 * n24 * n33
-					 + n13 * n22 * n34
-					 - n12 * n23 * n34
-				) +
-				n42 * (
-					+ n11 * n23 * n34
-					 - n11 * n24 * n33
-					 + n14 * n21 * n33
-					 - n13 * n21 * n34
-					 + n13 * n24 * n31
-					 - n14 * n23 * n31
-				) +
-				n43 * (
-					+ n11 * n24 * n32
-					 - n11 * n22 * n34
-					 - n14 * n21 * n32
-					 + n12 * n21 * n34
-					 + n14 * n22 * n31
-					 - n12 * n24 * n31
-				) +
-				n44 * (
-					- n13 * n22 * n31
-					 - n11 * n23 * n32
-					 + n11 * n22 * n33
-					 + n13 * n21 * n32
-					 - n12 * n21 * n33
-					 + n12 * n23 * n31
-				)
+			} else {
 
-			);
+				if ( _canvas === undefined ) { _canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ); }
 
-		},
+				_canvas.width = image.width;
+				_canvas.height = image.height;
 
-		transpose: function () {
+				var context = _canvas.getContext( '2d' );
 
-			var te = this.elements;
-			var tmp;
+				if ( image instanceof ImageData ) {
 
-			tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
-			tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
-			tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
+					context.putImageData( image, 0, 0 );
 
-			tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
-			tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
-			tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
+				} else {
 
-			return this;
+					context.drawImage( image, 0, 0, image.width, image.height );
 
-		},
+				}
 
-		setPosition: function ( x, y, z ) {
+				canvas = _canvas;
 
-			var te = this.elements;
+			}
 
-			if ( x.isVector3 ) {
+			if ( canvas.width > 2048 || canvas.height > 2048 ) {
 
-				te[ 12 ] = x.x;
-				te[ 13 ] = x.y;
-				te[ 14 ] = x.z;
+				return canvas.toDataURL( 'image/jpeg', 0.6 );
 
 			} else {
 
-				te[ 12 ] = x;
-				te[ 13 ] = y;
-				te[ 14 ] = z;
+				return canvas.toDataURL( 'image/png' );
 
 			}
 
-			return this;
+		}
 
-		},
+	};
 
-		getInverse: function ( m, throwOnDegenerate ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author szimek / https://github.com/szimek/
+	 */
 
-			if ( throwOnDegenerate !== undefined ) {
+	var textureId = 0;
 
-				console.warn( "THREE.Matrix4: .getInverse() can no longer be configured to throw on degenerate." );
+	function Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
 
-			}
+		Object.defineProperty( this, 'id', { value: textureId ++ } );
 
-			// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
-			var te = this.elements,
-				me = m.elements,
+		this.uuid = MathUtils.generateUUID();
 
-				n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ], n41 = me[ 3 ],
-				n12 = me[ 4 ], n22 = me[ 5 ], n32 = me[ 6 ], n42 = me[ 7 ],
-				n13 = me[ 8 ], n23 = me[ 9 ], n33 = me[ 10 ], n43 = me[ 11 ],
-				n14 = me[ 12 ], n24 = me[ 13 ], n34 = me[ 14 ], n44 = me[ 15 ],
+		this.name = '';
 
-				t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
-				t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
-				t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
-				t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
+		this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
+		this.mipmaps = [];
 
-			var det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
+		this.mapping = mapping !== undefined ? mapping : Texture.DEFAULT_MAPPING;
 
-			if ( det === 0 ) { return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); }
+		this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
+		this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
 
-			var detInv = 1 / det;
+		this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
+		this.minFilter = minFilter !== undefined ? minFilter : LinearMipmapLinearFilter;
 
-			te[ 0 ] = t11 * detInv;
-			te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv;
-			te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv;
-			te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv;
+		this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
 
-			te[ 4 ] = t12 * detInv;
-			te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv;
-			te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv;
-			te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv;
+		this.format = format !== undefined ? format : RGBAFormat;
+		this.internalFormat = null;
+		this.type = type !== undefined ? type : UnsignedByteType;
 
-			te[ 8 ] = t13 * detInv;
-			te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv;
-			te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv;
-			te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv;
+		this.offset = new Vector2( 0, 0 );
+		this.repeat = new Vector2( 1, 1 );
+		this.center = new Vector2( 0, 0 );
+		this.rotation = 0;
 
-			te[ 12 ] = t14 * detInv;
-			te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv;
-			te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv;
-			te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv;
+		this.matrixAutoUpdate = true;
+		this.matrix = new Matrix3();
 
-			return this;
+		this.generateMipmaps = true;
+		this.premultiplyAlpha = false;
+		this.flipY = true;
+		this.unpackAlignment = 4;	// valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
 
-		},
+		// Values of encoding !== THREE.LinearEncoding only supported on map, envMap and emissiveMap.
+		//
+		// Also changing the encoding after already used by a Material will not automatically make the Material
+		// update. You need to explicitly call Material.needsUpdate to trigger it to recompile.
+		this.encoding = encoding !== undefined ? encoding : LinearEncoding;
 
-		scale: function ( v ) {
+		this.version = 0;
+		this.onUpdate = null;
 
-			var te = this.elements;
-			var x = v.x, y = v.y, z = v.z;
+	}
 
-			te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
-			te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
-			te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
-			te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
+	Texture.DEFAULT_IMAGE = undefined;
+	Texture.DEFAULT_MAPPING = UVMapping;
 
-			return this;
+	Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-		},
+		constructor: Texture,
 
-		getMaxScaleOnAxis: function () {
+		isTexture: true,
 
-			var te = this.elements;
+		updateMatrix: function () {
 
-			var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
-			var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
-			var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
+			this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
 
-			return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) );
+		},
+
+		clone: function () {
+
+			return new this.constructor().copy( this );
 
 		},
 
-		makeTranslation: function ( x, y, z ) {
+		copy: function ( source ) {
 
-			this.set(
+			this.name = source.name;
 
-				1, 0, 0, x,
-				0, 1, 0, y,
-				0, 0, 1, z,
-				0, 0, 0, 1
+			this.image = source.image;
+			this.mipmaps = source.mipmaps.slice( 0 );
 
-			);
+			this.mapping = source.mapping;
 
-			return this;
+			this.wrapS = source.wrapS;
+			this.wrapT = source.wrapT;
 
-		},
+			this.magFilter = source.magFilter;
+			this.minFilter = source.minFilter;
 
-		makeRotationX: function ( theta ) {
+			this.anisotropy = source.anisotropy;
 
-			var c = Math.cos( theta ), s = Math.sin( theta );
+			this.format = source.format;
+			this.internalFormat = source.internalFormat;
+			this.type = source.type;
 
-			this.set(
+			this.offset.copy( source.offset );
+			this.repeat.copy( source.repeat );
+			this.center.copy( source.center );
+			this.rotation = source.rotation;
 
-				1, 0, 0, 0,
-				0, c, - s, 0,
-				0, s, c, 0,
-				0, 0, 0, 1
+			this.matrixAutoUpdate = source.matrixAutoUpdate;
+			this.matrix.copy( source.matrix );
 
-			);
+			this.generateMipmaps = source.generateMipmaps;
+			this.premultiplyAlpha = source.premultiplyAlpha;
+			this.flipY = source.flipY;
+			this.unpackAlignment = source.unpackAlignment;
+			this.encoding = source.encoding;
 
 			return this;
 
 		},
 
-		makeRotationY: function ( theta ) {
+		toJSON: function ( meta ) {
 
-			var c = Math.cos( theta ), s = Math.sin( theta );
+			var isRootObject = ( meta === undefined || typeof meta === 'string' );
 
-			this.set(
+			if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
 
-				 c, 0, s, 0,
-				 0, 1, 0, 0,
-				- s, 0, c, 0,
-				 0, 0, 0, 1
+				return meta.textures[ this.uuid ];
 
-			);
+			}
 
-			return this;
+			var output = {
 
-		},
+				metadata: {
+					version: 4.5,
+					type: 'Texture',
+					generator: 'Texture.toJSON'
+				},
 
-		makeRotationZ: function ( theta ) {
+				uuid: this.uuid,
+				name: this.name,
 
-			var c = Math.cos( theta ), s = Math.sin( theta );
+				mapping: this.mapping,
 
-			this.set(
+				repeat: [ this.repeat.x, this.repeat.y ],
+				offset: [ this.offset.x, this.offset.y ],
+				center: [ this.center.x, this.center.y ],
+				rotation: this.rotation,
 
-				c, - s, 0, 0,
-				s, c, 0, 0,
-				0, 0, 1, 0,
-				0, 0, 0, 1
+				wrap: [ this.wrapS, this.wrapT ],
 
-			);
+				format: this.format,
+				type: this.type,
+				encoding: this.encoding,
 
-			return this;
+				minFilter: this.minFilter,
+				magFilter: this.magFilter,
+				anisotropy: this.anisotropy,
 
-		},
+				flipY: this.flipY,
 
-		makeRotationAxis: function ( axis, angle ) {
+				premultiplyAlpha: this.premultiplyAlpha,
+				unpackAlignment: this.unpackAlignment
 
-			// Based on http://www.gamedev.net/reference/articles/article1199.asp
+			};
 
-			var c = Math.cos( angle );
-			var s = Math.sin( angle );
-			var t = 1 - c;
-			var x = axis.x, y = axis.y, z = axis.z;
-			var tx = t * x, ty = t * y;
+			if ( this.image !== undefined ) {
 
-			this.set(
+				// TODO: Move to THREE.Image
 
-				tx * x + c, tx * y - s * z, tx * z + s * y, 0,
-				tx * y + s * z, ty * y + c, ty * z - s * x, 0,
-				tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
-				0, 0, 0, 1
+				var image = this.image;
 
-			);
+				if ( image.uuid === undefined ) {
 
-			 return this;
+					image.uuid = MathUtils.generateUUID(); // UGH
 
-		},
+				}
 
-		makeScale: function ( x, y, z ) {
-
-			this.set(
+				if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
 
-				x, 0, 0, 0,
-				0, y, 0, 0,
-				0, 0, z, 0,
-				0, 0, 0, 1
+					var url;
 
-			);
+					if ( Array.isArray( image ) ) {
 
-			return this;
+						// process array of images e.g. CubeTexture
 
-		},
+						url = [];
 
-		makeShear: function ( x, y, z ) {
+						for ( var i = 0, l = image.length; i < l; i ++ ) {
 
-			this.set(
+							url.push( ImageUtils.getDataURL( image[ i ] ) );
 
-				1, y, z, 0,
-				x, 1, z, 0,
-				x, y, 1, 0,
-				0, 0, 0, 1
+						}
 
-			);
+					} else {
 
-			return this;
+						// process single image
 
-		},
+						url = ImageUtils.getDataURL( image );
 
-		compose: function ( position, quaternion, scale ) {
+					}
 
-			var te = this.elements;
+					meta.images[ image.uuid ] = {
+						uuid: image.uuid,
+						url: url
+					};
 
-			var x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
-			var x2 = x + x,	y2 = y + y, z2 = z + z;
-			var xx = x * x2, xy = x * y2, xz = x * z2;
-			var yy = y * y2, yz = y * z2, zz = z * z2;
-			var wx = w * x2, wy = w * y2, wz = w * z2;
+				}
 
-			var sx = scale.x, sy = scale.y, sz = scale.z;
+				output.image = image.uuid;
 
-			te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
-			te[ 1 ] = ( xy + wz ) * sx;
-			te[ 2 ] = ( xz - wy ) * sx;
-			te[ 3 ] = 0;
+			}
 
-			te[ 4 ] = ( xy - wz ) * sy;
-			te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
-			te[ 6 ] = ( yz + wx ) * sy;
-			te[ 7 ] = 0;
+			if ( ! isRootObject ) {
 
-			te[ 8 ] = ( xz + wy ) * sz;
-			te[ 9 ] = ( yz - wx ) * sz;
-			te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
-			te[ 11 ] = 0;
+				meta.textures[ this.uuid ] = output;
 
-			te[ 12 ] = position.x;
-			te[ 13 ] = position.y;
-			te[ 14 ] = position.z;
-			te[ 15 ] = 1;
+			}
 
-			return this;
+			return output;
 
 		},
 
-		decompose: function ( position, quaternion, scale ) {
-
-			var te = this.elements;
-
-			var sx = _v1.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
-			var sy = _v1.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
-			var sz = _v1.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
-
-			// if determine is negative, we need to invert one scale
-			var det = this.determinant();
-			if ( det < 0 ) { sx = - sx; }
-
-			position.x = te[ 12 ];
-			position.y = te[ 13 ];
-			position.z = te[ 14 ];
-
-			// scale the rotation part
-			_m1.copy( this );
+		dispose: function () {
 
-			var invSX = 1 / sx;
-			var invSY = 1 / sy;
-			var invSZ = 1 / sz;
+			this.dispatchEvent( { type: 'dispose' } );
 
-			_m1.elements[ 0 ] *= invSX;
-			_m1.elements[ 1 ] *= invSX;
-			_m1.elements[ 2 ] *= invSX;
+		},
 
-			_m1.elements[ 4 ] *= invSY;
-			_m1.elements[ 5 ] *= invSY;
-			_m1.elements[ 6 ] *= invSY;
+		transformUv: function ( uv ) {
 
-			_m1.elements[ 8 ] *= invSZ;
-			_m1.elements[ 9 ] *= invSZ;
-			_m1.elements[ 10 ] *= invSZ;
+			if ( this.mapping !== UVMapping ) { return uv; }
 
-			quaternion.setFromRotationMatrix( _m1 );
+			uv.applyMatrix3( this.matrix );
 
-			scale.x = sx;
-			scale.y = sy;
-			scale.z = sz;
+			if ( uv.x < 0 || uv.x > 1 ) {
 
-			return this;
+				switch ( this.wrapS ) {
 
-		},
+					case RepeatWrapping:
 
-		makePerspective: function ( left, right, top, bottom, near, far ) {
+						uv.x = uv.x - Math.floor( uv.x );
+						break;
 
-			if ( far === undefined ) {
+					case ClampToEdgeWrapping:
 
-				console.warn( 'THREE.Matrix4: .makePerspective() has been redefined and has a new signature. Please check the docs.' );
+						uv.x = uv.x < 0 ? 0 : 1;
+						break;
 
-			}
+					case MirroredRepeatWrapping:
 
-			var te = this.elements;
-			var x = 2 * near / ( right - left );
-			var y = 2 * near / ( top - bottom );
+						if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
 
-			var a = ( right + left ) / ( right - left );
-			var b = ( top + bottom ) / ( top - bottom );
-			var c = - ( far + near ) / ( far - near );
-			var d = - 2 * far * near / ( far - near );
+							uv.x = Math.ceil( uv.x ) - uv.x;
 
-			te[ 0 ] = x;	te[ 4 ] = 0;	te[ 8 ] = a;	te[ 12 ] = 0;
-			te[ 1 ] = 0;	te[ 5 ] = y;	te[ 9 ] = b;	te[ 13 ] = 0;
-			te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = c;	te[ 14 ] = d;
-			te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = - 1;	te[ 15 ] = 0;
+						} else {
 
-			return this;
+							uv.x = uv.x - Math.floor( uv.x );
 
-		},
+						}
 
-		makeOrthographic: function ( left, right, top, bottom, near, far ) {
+						break;
 
-			var te = this.elements;
-			var w = 1.0 / ( right - left );
-			var h = 1.0 / ( top - bottom );
-			var p = 1.0 / ( far - near );
+				}
 
-			var x = ( right + left ) * w;
-			var y = ( top + bottom ) * h;
-			var z = ( far + near ) * p;
+			}
 
-			te[ 0 ] = 2 * w;	te[ 4 ] = 0;	te[ 8 ] = 0;	te[ 12 ] = - x;
-			te[ 1 ] = 0;	te[ 5 ] = 2 * h;	te[ 9 ] = 0;	te[ 13 ] = - y;
-			te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = - 2 * p;	te[ 14 ] = - z;
-			te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = 0;	te[ 15 ] = 1;
+			if ( uv.y < 0 || uv.y > 1 ) {
 
-			return this;
+				switch ( this.wrapT ) {
 
-		},
+					case RepeatWrapping:
 
-		equals: function ( matrix ) {
+						uv.y = uv.y - Math.floor( uv.y );
+						break;
 
-			var te = this.elements;
-			var me = matrix.elements;
+					case ClampToEdgeWrapping:
 
-			for ( var i = 0; i < 16; i ++ ) {
+						uv.y = uv.y < 0 ? 0 : 1;
+						break;
 
-				if ( te[ i ] !== me[ i ] ) { return false; }
+					case MirroredRepeatWrapping:
 
-			}
+						if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
 
-			return true;
+							uv.y = Math.ceil( uv.y ) - uv.y;
 
-		},
+						} else {
 
-		fromArray: function ( array, offset ) {
+							uv.y = uv.y - Math.floor( uv.y );
 
-			if ( offset === undefined ) { offset = 0; }
+						}
 
-			for ( var i = 0; i < 16; i ++ ) {
+						break;
 
-				this.elements[ i ] = array[ i + offset ];
+				}
 
 			}
 
-			return this;
-
-		},
+			if ( this.flipY ) {
 
-		toArray: function ( array, offset ) {
+				uv.y = 1 - uv.y;
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+			}
 
-			var te = this.elements;
+			return uv;
 
-			array[ offset ] = te[ 0 ];
-			array[ offset + 1 ] = te[ 1 ];
-			array[ offset + 2 ] = te[ 2 ];
-			array[ offset + 3 ] = te[ 3 ];
+		}
 
-			array[ offset + 4 ] = te[ 4 ];
-			array[ offset + 5 ] = te[ 5 ];
-			array[ offset + 6 ] = te[ 6 ];
-			array[ offset + 7 ] = te[ 7 ];
+	} );
 
-			array[ offset + 8 ] = te[ 8 ];
-			array[ offset + 9 ] = te[ 9 ];
-			array[ offset + 10 ] = te[ 10 ];
-			array[ offset + 11 ] = te[ 11 ];
+	Object.defineProperty( Texture.prototype, "needsUpdate", {
 
-			array[ offset + 12 ] = te[ 12 ];
-			array[ offset + 13 ] = te[ 13 ];
-			array[ offset + 14 ] = te[ 14 ];
-			array[ offset + 15 ] = te[ 15 ];
+		set: function ( value ) {
 
-			return array;
+			if ( value === true ) { this.version ++; }
 
 		}
 
 	} );
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author supereggbert / http://www.paulbrunt.co.uk/
+	 * @author philogb / http://blog.thejit.org/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author egraether / http://egraether.com/
 	 * @author WestLangley / http://github.com/WestLangley
-	 * @author bhouston / http://clara.io
 	 */
 
-	var _matrix = new Matrix4();
-	var _quaternion$1 = new Quaternion();
-
-	function Euler( x, y, z, order ) {
+	function Vector4( x, y, z, w ) {
 
-		this._x = x || 0;
-		this._y = y || 0;
-		this._z = z || 0;
-		this._order = order || Euler.DefaultOrder;
+		this.x = x || 0;
+		this.y = y || 0;
+		this.z = z || 0;
+		this.w = ( w !== undefined ) ? w : 1;
 
 	}
 
-	Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
-
-	Euler.DefaultOrder = 'XYZ';
-
-	Object.defineProperties( Euler.prototype, {
+	Object.defineProperties( Vector4.prototype, {
 
-		x: {
+		"width": {
 
 			get: function () {
 
-				return this._x;
+				return this.z;
 
 			},
 
 			set: function ( value ) {
 
-				this._x = value;
-				this._onChangeCallback();
+				this.z = value;
 
 			}
 
 		},
 
-		y: {
+		"height": {
 
 			get: function () {
 
-				return this._y;
+				return this.w;
 
 			},
 
 			set: function ( value ) {
 
-				this._y = value;
-				this._onChangeCallback();
+				this.w = value;
 
 			}
 
-		},
-
-		z: {
+		}
 
-			get: function () {
+	} );
 
-				return this._z;
+	Object.assign( Vector4.prototype, {
 
-			},
+		isVector4: true,
 
-			set: function ( value ) {
+		set: function ( x, y, z, w ) {
 
-				this._z = value;
-				this._onChangeCallback();
+			this.x = x;
+			this.y = y;
+			this.z = z;
+			this.w = w;
 
-			}
+			return this;
 
 		},
 
-		order: {
-
-			get: function () {
-
-				return this._order;
-
-			},
-
-			set: function ( value ) {
+		setScalar: function ( scalar ) {
 
-				this._order = value;
-				this._onChangeCallback();
+			this.x = scalar;
+			this.y = scalar;
+			this.z = scalar;
+			this.w = scalar;
 
-			}
+			return this;
 
-		}
+		},
 
-	} );
+		setX: function ( x ) {
 
-	Object.assign( Euler.prototype, {
+			this.x = x;
 
-		isEuler: true,
+			return this;
 
-		set: function ( x, y, z, order ) {
+		},
 
-			this._x = x;
-			this._y = y;
-			this._z = z;
-			this._order = order || this._order;
+		setY: function ( y ) {
 
-			this._onChangeCallback();
+			this.y = y;
 
 			return this;
 
 		},
 
-		clone: function () {
+		setZ: function ( z ) {
 
-			return new this.constructor( this._x, this._y, this._z, this._order );
+			this.z = z;
 
-		},
+			return this;
 
-		copy: function ( euler ) {
+		},
 
-			this._x = euler._x;
-			this._y = euler._y;
-			this._z = euler._z;
-			this._order = euler._order;
+		setW: function ( w ) {
 
-			this._onChangeCallback();
+			this.w = w;
 
 			return this;
 
 		},
 
-		setFromRotationMatrix: function ( m, order, update ) {
+		setComponent: function ( index, value ) {
 
-			var clamp = MathUtils.clamp;
+			switch ( index ) {
 
-			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
+				case 0: this.x = value; break;
+				case 1: this.y = value; break;
+				case 2: this.z = value; break;
+				case 3: this.w = value; break;
+				default: throw new Error( 'index is out of range: ' + index );
 
-			var te = m.elements;
-			var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
-			var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
-			var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
+			}
 
-			order = order || this._order;
+			return this;
 
-			switch ( order ) {
+		},
 
-				case 'XYZ':
+		getComponent: function ( index ) {
 
-					this._y = Math.asin( clamp( m13, - 1, 1 ) );
+			switch ( index ) {
 
-					if ( Math.abs( m13 ) < 0.9999999 ) {
+				case 0: return this.x;
+				case 1: return this.y;
+				case 2: return this.z;
+				case 3: return this.w;
+				default: throw new Error( 'index is out of range: ' + index );
 
-						this._x = Math.atan2( - m23, m33 );
-						this._z = Math.atan2( - m12, m11 );
+			}
 
-					} else {
+		},
 
-						this._x = Math.atan2( m32, m22 );
-						this._z = 0;
+		clone: function () {
 
-					}
+			return new this.constructor( this.x, this.y, this.z, this.w );
 
-					break;
+		},
 
-				case 'YXZ':
+		copy: function ( v ) {
 
-					this._x = Math.asin( - clamp( m23, - 1, 1 ) );
+			this.x = v.x;
+			this.y = v.y;
+			this.z = v.z;
+			this.w = ( v.w !== undefined ) ? v.w : 1;
 
-					if ( Math.abs( m23 ) < 0.9999999 ) {
+			return this;
 
-						this._y = Math.atan2( m13, m33 );
-						this._z = Math.atan2( m21, m22 );
+		},
 
-					} else {
+		add: function ( v, w ) {
 
-						this._y = Math.atan2( - m31, m11 );
-						this._z = 0;
+			if ( w !== undefined ) {
 
-					}
+				console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
+				return this.addVectors( v, w );
 
-					break;
+			}
 
-				case 'ZXY':
+			this.x += v.x;
+			this.y += v.y;
+			this.z += v.z;
+			this.w += v.w;
 
-					this._x = Math.asin( clamp( m32, - 1, 1 ) );
+			return this;
 
-					if ( Math.abs( m32 ) < 0.9999999 ) {
+		},
 
-						this._y = Math.atan2( - m31, m33 );
-						this._z = Math.atan2( - m12, m22 );
+		addScalar: function ( s ) {
 
-					} else {
+			this.x += s;
+			this.y += s;
+			this.z += s;
+			this.w += s;
 
-						this._y = 0;
-						this._z = Math.atan2( m21, m11 );
+			return this;
 
-					}
+		},
 
-					break;
+		addVectors: function ( a, b ) {
 
-				case 'ZYX':
+			this.x = a.x + b.x;
+			this.y = a.y + b.y;
+			this.z = a.z + b.z;
+			this.w = a.w + b.w;
 
-					this._y = Math.asin( - clamp( m31, - 1, 1 ) );
+			return this;
 
-					if ( Math.abs( m31 ) < 0.9999999 ) {
+		},
 
-						this._x = Math.atan2( m32, m33 );
-						this._z = Math.atan2( m21, m11 );
+		addScaledVector: function ( v, s ) {
 
-					} else {
+			this.x += v.x * s;
+			this.y += v.y * s;
+			this.z += v.z * s;
+			this.w += v.w * s;
 
-						this._x = 0;
-						this._z = Math.atan2( - m12, m22 );
+			return this;
 
-					}
+		},
 
-					break;
+		sub: function ( v, w ) {
 
-				case 'YZX':
+			if ( w !== undefined ) {
 
-					this._z = Math.asin( clamp( m21, - 1, 1 ) );
+				console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
+				return this.subVectors( v, w );
 
-					if ( Math.abs( m21 ) < 0.9999999 ) {
+			}
 
-						this._x = Math.atan2( - m23, m22 );
-						this._y = Math.atan2( - m31, m11 );
+			this.x -= v.x;
+			this.y -= v.y;
+			this.z -= v.z;
+			this.w -= v.w;
 
-					} else {
+			return this;
 
-						this._x = 0;
-						this._y = Math.atan2( m13, m33 );
+		},
 
-					}
+		subScalar: function ( s ) {
 
-					break;
+			this.x -= s;
+			this.y -= s;
+			this.z -= s;
+			this.w -= s;
 
-				case 'XZY':
+			return this;
 
-					this._z = Math.asin( - clamp( m12, - 1, 1 ) );
+		},
 
-					if ( Math.abs( m12 ) < 0.9999999 ) {
+		subVectors: function ( a, b ) {
 
-						this._x = Math.atan2( m32, m22 );
-						this._y = Math.atan2( m13, m11 );
+			this.x = a.x - b.x;
+			this.y = a.y - b.y;
+			this.z = a.z - b.z;
+			this.w = a.w - b.w;
 
-					} else {
+			return this;
 
-						this._x = Math.atan2( - m23, m33 );
-						this._y = 0;
+		},
 
-					}
+		multiplyScalar: function ( scalar ) {
 
-					break;
+			this.x *= scalar;
+			this.y *= scalar;
+			this.z *= scalar;
+			this.w *= scalar;
 
-				default:
+			return this;
 
-					console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
+		},
 
-			}
+		applyMatrix4: function ( m ) {
 
-			this._order = order;
+			var x = this.x, y = this.y, z = this.z, w = this.w;
+			var e = m.elements;
 
-			if ( update !== false ) { this._onChangeCallback(); }
+			this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
+			this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
+			this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
+			this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
 
 			return this;
 
 		},
 
-		setFromQuaternion: function ( q, order, update ) {
-
-			_matrix.makeRotationFromQuaternion( q );
+		divideScalar: function ( scalar ) {
 
-			return this.setFromRotationMatrix( _matrix, order, update );
+			return this.multiplyScalar( 1 / scalar );
 
 		},
 
-		setFromVector3: function ( v, order ) {
+		setAxisAngleFromQuaternion: function ( q ) {
 
-			return this.set( v.x, v.y, v.z, order || this._order );
+			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
 
-		},
-
-		reorder: function ( newOrder ) {
-
-			// WARNING: this discards revolution information -bhouston
-
-			_quaternion$1.setFromEuler( this );
-
-			return this.setFromQuaternion( _quaternion$1, newOrder );
+			// q is assumed to be normalized
 
-		},
+			this.w = 2 * Math.acos( q.w );
 
-		equals: function ( euler ) {
+			var s = Math.sqrt( 1 - q.w * q.w );
 
-			return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
+			if ( s < 0.0001 ) {
 
-		},
+				this.x = 1;
+				this.y = 0;
+				this.z = 0;
 
-		fromArray: function ( array ) {
+			} else {
 
-			this._x = array[ 0 ];
-			this._y = array[ 1 ];
-			this._z = array[ 2 ];
-			if ( array[ 3 ] !== undefined ) { this._order = array[ 3 ]; }
+				this.x = q.x / s;
+				this.y = q.y / s;
+				this.z = q.z / s;
 
-			this._onChangeCallback();
+			}
 
 			return this;
 
 		},
 
-		toArray: function ( array, offset ) {
-
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
-
-			array[ offset ] = this._x;
-			array[ offset + 1 ] = this._y;
-			array[ offset + 2 ] = this._z;
-			array[ offset + 3 ] = this._order;
+		setAxisAngleFromRotationMatrix: function ( m ) {
 
-			return array;
+			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
 
-		},
+			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
 
-		toVector3: function ( optionalResult ) {
+			var angle, x, y, z,		// variables for result
+				epsilon = 0.01,		// margin to allow for rounding errors
+				epsilon2 = 0.1,		// margin to distinguish between 0 and 180 degrees
 
-			if ( optionalResult ) {
+				te = m.elements,
 
-				return optionalResult.set( this._x, this._y, this._z );
+				m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
+				m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
+				m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
 
-			} else {
+			if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
+			     ( Math.abs( m13 - m31 ) < epsilon ) &&
+			     ( Math.abs( m23 - m32 ) < epsilon ) ) {
 
-				return new Vector3( this._x, this._y, this._z );
+				// singularity found
+				// first check for identity matrix which must have +1 for all terms
+				// in leading diagonal and zero in other terms
 
-			}
+				if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
+				     ( Math.abs( m13 + m31 ) < epsilon2 ) &&
+				     ( Math.abs( m23 + m32 ) < epsilon2 ) &&
+				     ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
 
-		},
+					// this singularity is identity matrix so angle = 0
 
-		_onChange: function ( callback ) {
+					this.set( 1, 0, 0, 0 );
 
-			this._onChangeCallback = callback;
+					return this; // zero angle, arbitrary axis
 
-			return this;
+				}
 
-		},
+				// otherwise this singularity is angle = 180
 
-		_onChangeCallback: function () {}
+				angle = Math.PI;
 
-	} );
+				var xx = ( m11 + 1 ) / 2;
+				var yy = ( m22 + 1 ) / 2;
+				var zz = ( m33 + 1 ) / 2;
+				var xy = ( m12 + m21 ) / 4;
+				var xz = ( m13 + m31 ) / 4;
+				var yz = ( m23 + m32 ) / 4;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				if ( ( xx > yy ) && ( xx > zz ) ) {
 
-	function Layers() {
+					// m11 is the largest diagonal term
 
-		this.mask = 1 | 0;
+					if ( xx < epsilon ) {
 
-	}
+						x = 0;
+						y = 0.707106781;
+						z = 0.707106781;
 
-	Object.assign( Layers.prototype, {
+					} else {
 
-		set: function ( channel ) {
+						x = Math.sqrt( xx );
+						y = xy / x;
+						z = xz / x;
 
-			this.mask = 1 << channel | 0;
+					}
 
-		},
+				} else if ( yy > zz ) {
 
-		enable: function ( channel ) {
+					// m22 is the largest diagonal term
 
-			this.mask |= 1 << channel | 0;
+					if ( yy < epsilon ) {
 
-		},
+						x = 0.707106781;
+						y = 0;
+						z = 0.707106781;
 
-		enableAll: function () {
+					} else {
 
-			this.mask = 0xffffffff | 0;
+						y = Math.sqrt( yy );
+						x = xy / y;
+						z = yz / y;
 
-		},
+					}
 
-		toggle: function ( channel ) {
+				} else {
 
-			this.mask ^= 1 << channel | 0;
+					// m33 is the largest diagonal term so base result on this
 
-		},
+					if ( zz < epsilon ) {
 
-		disable: function ( channel ) {
+						x = 0.707106781;
+						y = 0.707106781;
+						z = 0;
 
-			this.mask &= ~ ( 1 << channel | 0 );
+					} else {
 
-		},
+						z = Math.sqrt( zz );
+						x = xz / z;
+						y = yz / z;
 
-		disableAll: function () {
+					}
 
-			this.mask = 0;
+				}
 
-		},
+				this.set( x, y, z, angle );
 
-		test: function ( layers ) {
+				return this; // return 180 deg rotation
 
-			return ( this.mask & layers.mask ) !== 0;
+			}
 
-		}
+			// as we have reached here there are no singularities so we can handle normally
 
-	} );
+			var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
+			                   ( m13 - m31 ) * ( m13 - m31 ) +
+			                   ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
 
-	var _object3DId = 0;
+			if ( Math.abs( s ) < 0.001 ) { s = 1; }
 
-	var _v1$1 = new Vector3();
-	var _q1 = new Quaternion();
-	var _m1$1 = new Matrix4();
-	var _target = new Vector3();
+			// prevent divide by zero, should not happen if matrix is orthogonal and should be
+			// caught by singularity test above, but I've left it in just in case
 
-	var _position = new Vector3();
-	var _scale = new Vector3();
-	var _quaternion$2 = new Quaternion();
+			this.x = ( m32 - m23 ) / s;
+			this.y = ( m13 - m31 ) / s;
+			this.z = ( m21 - m12 ) / s;
+			this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
 
-	var _xAxis = new Vector3( 1, 0, 0 );
-	var _yAxis = new Vector3( 0, 1, 0 );
-	var _zAxis = new Vector3( 0, 0, 1 );
+			return this;
 
-	var _addedEvent = { type: 'added' };
-	var _removedEvent = { type: 'removed' };
+		},
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author elephantatwork / www.elephantatwork.ch
-	 */
+		min: function ( v ) {
 
-	function Object3D() {
+			this.x = Math.min( this.x, v.x );
+			this.y = Math.min( this.y, v.y );
+			this.z = Math.min( this.z, v.z );
+			this.w = Math.min( this.w, v.w );
 
-		Object.defineProperty( this, 'id', { value: _object3DId ++ } );
+			return this;
 
-		this.uuid = MathUtils.generateUUID();
+		},
 
-		this.name = '';
-		this.type = 'Object3D';
+		max: function ( v ) {
 
-		this.parent = null;
-		this.children = [];
+			this.x = Math.max( this.x, v.x );
+			this.y = Math.max( this.y, v.y );
+			this.z = Math.max( this.z, v.z );
+			this.w = Math.max( this.w, v.w );
 
-		this.up = Object3D.DefaultUp.clone();
+			return this;
 
-		var position = new Vector3();
-		var rotation = new Euler();
-		var quaternion = new Quaternion();
-		var scale = new Vector3( 1, 1, 1 );
+		},
 
-		function onRotationChange() {
+		clamp: function ( min, max ) {
 
-			quaternion.setFromEuler( rotation, false );
+			// assumes min < max, componentwise
 
-		}
+			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
+			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
+			this.z = Math.max( min.z, Math.min( max.z, this.z ) );
+			this.w = Math.max( min.w, Math.min( max.w, this.w ) );
 
-		function onQuaternionChange() {
+			return this;
 
-			rotation.setFromQuaternion( quaternion, undefined, false );
+		},
 
-		}
+		clampScalar: function ( minVal, maxVal ) {
 
-		rotation._onChange( onRotationChange );
-		quaternion._onChange( onQuaternionChange );
+			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
+			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
+			this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
+			this.w = Math.max( minVal, Math.min( maxVal, this.w ) );
 
-		Object.defineProperties( this, {
-			position: {
-				configurable: true,
-				enumerable: true,
-				value: position
-			},
-			rotation: {
-				configurable: true,
-				enumerable: true,
-				value: rotation
-			},
-			quaternion: {
-				configurable: true,
-				enumerable: true,
-				value: quaternion
-			},
-			scale: {
-				configurable: true,
-				enumerable: true,
-				value: scale
-			},
-			modelViewMatrix: {
-				value: new Matrix4()
-			},
-			normalMatrix: {
-				value: new Matrix3()
-			}
-		} );
+			return this;
 
-		this.matrix = new Matrix4();
-		this.matrixWorld = new Matrix4();
+		},
 
-		this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
-		this.matrixWorldNeedsUpdate = false;
+		clampLength: function ( min, max ) {
 
-		this.layers = new Layers();
-		this.visible = true;
+			var length = this.length();
 
-		this.castShadow = false;
-		this.receiveShadow = false;
+			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
 
-		this.frustumCulled = true;
-		this.renderOrder = 0;
+		},
 
-		this.userData = {};
+		floor: function () {
 
-	}
+			this.x = Math.floor( this.x );
+			this.y = Math.floor( this.y );
+			this.z = Math.floor( this.z );
+			this.w = Math.floor( this.w );
 
-	Object3D.DefaultUp = new Vector3( 0, 1, 0 );
-	Object3D.DefaultMatrixAutoUpdate = true;
+			return this;
 
-	Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+		},
 
-		constructor: Object3D,
+		ceil: function () {
 
-		isObject3D: true,
+			this.x = Math.ceil( this.x );
+			this.y = Math.ceil( this.y );
+			this.z = Math.ceil( this.z );
+			this.w = Math.ceil( this.w );
 
-		onBeforeRender: function () {},
-		onAfterRender: function () {},
+			return this;
 
-		applyMatrix4: function ( matrix ) {
+		},
 
-			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
+		round: function () {
 
-			this.matrix.premultiply( matrix );
+			this.x = Math.round( this.x );
+			this.y = Math.round( this.y );
+			this.z = Math.round( this.z );
+			this.w = Math.round( this.w );
 
-			this.matrix.decompose( this.position, this.quaternion, this.scale );
+			return this;
 
 		},
 
-		applyQuaternion: function ( q ) {
+		roundToZero: function () {
 
-			this.quaternion.premultiply( q );
+			this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
+			this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
+			this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
+			this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
 
 			return this;
 
 		},
 
-		setRotationFromAxisAngle: function ( axis, angle ) {
+		negate: function () {
 
-			// assumes axis is normalized
+			this.x = - this.x;
+			this.y = - this.y;
+			this.z = - this.z;
+			this.w = - this.w;
 
-			this.quaternion.setFromAxisAngle( axis, angle );
+			return this;
 
 		},
 
-		setRotationFromEuler: function ( euler ) {
+		dot: function ( v ) {
 
-			this.quaternion.setFromEuler( euler, true );
+			return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
 
 		},
 
-		setRotationFromMatrix: function ( m ) {
-
-			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
+		lengthSq: function () {
 
-			this.quaternion.setFromRotationMatrix( m );
+			return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
 
 		},
 
-		setRotationFromQuaternion: function ( q ) {
-
-			// assumes q is normalized
+		length: function () {
 
-			this.quaternion.copy( q );
+			return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
 
 		},
 
-		rotateOnAxis: function ( axis, angle ) {
+		manhattanLength: function () {
 
-			// rotate object on axis in object space
-			// axis is assumed to be normalized
+			return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
 
-			_q1.setFromAxisAngle( axis, angle );
+		},
 
-			this.quaternion.multiply( _q1 );
+		normalize: function () {
 
-			return this;
+			return this.divideScalar( this.length() || 1 );
 
 		},
 
-		rotateOnWorldAxis: function ( axis, angle ) {
+		setLength: function ( length ) {
 
-			// rotate object on axis in world space
-			// axis is assumed to be normalized
-			// method assumes no rotated parent
+			return this.normalize().multiplyScalar( length );
 
-			_q1.setFromAxisAngle( axis, angle );
+		},
 
-			this.quaternion.premultiply( _q1 );
+		lerp: function ( v, alpha ) {
+
+			this.x += ( v.x - this.x ) * alpha;
+			this.y += ( v.y - this.y ) * alpha;
+			this.z += ( v.z - this.z ) * alpha;
+			this.w += ( v.w - this.w ) * alpha;
 
 			return this;
 
 		},
 
-		rotateX: function ( angle ) {
+		lerpVectors: function ( v1, v2, alpha ) {
 
-			return this.rotateOnAxis( _xAxis, angle );
+			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
 
 		},
 
-		rotateY: function ( angle ) {
+		equals: function ( v ) {
 
-			return this.rotateOnAxis( _yAxis, angle );
+			return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
 
 		},
 
-		rotateZ: function ( angle ) {
+		fromArray: function ( array, offset ) {
 
-			return this.rotateOnAxis( _zAxis, angle );
+			if ( offset === undefined ) { offset = 0; }
 
-		},
+			this.x = array[ offset ];
+			this.y = array[ offset + 1 ];
+			this.z = array[ offset + 2 ];
+			this.w = array[ offset + 3 ];
 
-		translateOnAxis: function ( axis, distance ) {
+			return this;
 
-			// translate object by distance along axis in object space
-			// axis is assumed to be normalized
+		},
 
-			_v1$1.copy( axis ).applyQuaternion( this.quaternion );
+		toArray: function ( array, offset ) {
 
-			this.position.add( _v1$1.multiplyScalar( distance ) );
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			return this;
+			array[ offset ] = this.x;
+			array[ offset + 1 ] = this.y;
+			array[ offset + 2 ] = this.z;
+			array[ offset + 3 ] = this.w;
+
+			return array;
 
 		},
 
-		translateX: function ( distance ) {
+		fromBufferAttribute: function ( attribute, index, offset ) {
 
-			return this.translateOnAxis( _xAxis, distance );
+			if ( offset !== undefined ) {
 
-		},
+				console.warn( 'THREE.Vector4: offset has been removed from .fromBufferAttribute().' );
 
-		translateY: function ( distance ) {
+			}
 
-			return this.translateOnAxis( _yAxis, distance );
+			this.x = attribute.getX( index );
+			this.y = attribute.getY( index );
+			this.z = attribute.getZ( index );
+			this.w = attribute.getW( index );
+
+			return this;
 
 		},
 
-		translateZ: function ( distance ) {
+		random: function () {
 
-			return this.translateOnAxis( _zAxis, distance );
+			this.x = Math.random();
+			this.y = Math.random();
+			this.z = Math.random();
+			this.w = Math.random();
 
-		},
+			return this;
 
-		localToWorld: function ( vector ) {
+		}
 
-			return vector.applyMatrix4( this.matrixWorld );
+	} );
 
-		},
+	/**
+	 * @author szimek / https://github.com/szimek/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author Marius Kintel / https://github.com/kintel
+	 */
 
-		worldToLocal: function ( vector ) {
+	/*
+	 In options, we can specify:
+	 * Texture parameters for an auto-generated target texture
+	 * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
+	*/
+	function WebGLRenderTarget( width, height, options ) {
 
-			return vector.applyMatrix4( _m1$1.getInverse( this.matrixWorld ) );
+		this.width = width;
+		this.height = height;
 
-		},
+		this.scissor = new Vector4( 0, 0, width, height );
+		this.scissorTest = false;
 
-		lookAt: function ( x, y, z ) {
+		this.viewport = new Vector4( 0, 0, width, height );
 
-			// This method does not support objects having non-uniformly-scaled parent(s)
+		options = options || {};
 
-			if ( x.isVector3 ) {
+		this.texture = new Texture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.encoding );
 
-				_target.copy( x );
+		this.texture.image = {};
+		this.texture.image.width = width;
+		this.texture.image.height = height;
 
-			} else {
+		this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
+		this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
 
-				_target.set( x, y, z );
+		this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
+		this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
+		this.depthTexture = options.depthTexture !== undefined ? options.depthTexture : null;
 
-			}
+	}
 
-			var parent = this.parent;
+	WebGLRenderTarget.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-			this.updateWorldMatrix( true, false );
+		constructor: WebGLRenderTarget,
 
-			_position.setFromMatrixPosition( this.matrixWorld );
+		isWebGLRenderTarget: true,
 
-			if ( this.isCamera || this.isLight ) {
+		setSize: function ( width, height ) {
 
-				_m1$1.lookAt( _position, _target, this.up );
+			if ( this.width !== width || this.height !== height ) {
 
-			} else {
+				this.width = width;
+				this.height = height;
 
-				_m1$1.lookAt( _target, _position, this.up );
+				this.texture.image.width = width;
+				this.texture.image.height = height;
+
+				this.dispose();
 
 			}
 
-			this.quaternion.setFromRotationMatrix( _m1$1 );
+			this.viewport.set( 0, 0, width, height );
+			this.scissor.set( 0, 0, width, height );
 
-			if ( parent ) {
+		},
 
-				_m1$1.extractRotation( parent.matrixWorld );
-				_q1.setFromRotationMatrix( _m1$1 );
-				this.quaternion.premultiply( _q1.inverse() );
+		clone: function () {
 
-			}
+			return new this.constructor().copy( this );
 
 		},
 
-		add: function ( object ) {
+		copy: function ( source ) {
 
-			if ( arguments.length > 1 ) {
+			this.width = source.width;
+			this.height = source.height;
 
-				for ( var i = 0; i < arguments.length; i ++ ) {
+			this.viewport.copy( source.viewport );
 
-					this.add( arguments[ i ] );
+			this.texture = source.texture.clone();
 
-				}
+			this.depthBuffer = source.depthBuffer;
+			this.stencilBuffer = source.stencilBuffer;
+			this.depthTexture = source.depthTexture;
 
-				return this;
+			return this;
 
-			}
+		},
 
-			if ( object === this ) {
+		dispose: function () {
 
-				console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
-				return this;
+			this.dispatchEvent( { type: 'dispose' } );
 
-			}
+		}
 
-			if ( ( object && object.isObject3D ) ) {
+	} );
 
-				if ( object.parent !== null ) {
+	/**
+	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author Matt DesLauriers / @mattdesl
+	 */
 
-					object.parent.remove( object );
+	function WebGLMultisampleRenderTarget( width, height, options ) {
 
-				}
+		WebGLRenderTarget.call( this, width, height, options );
 
-				object.parent = this;
-				this.children.push( object );
+		this.samples = 4;
 
-				object.dispatchEvent( _addedEvent );
+	}
 
-			} else {
+	WebGLMultisampleRenderTarget.prototype = Object.assign( Object.create( WebGLRenderTarget.prototype ), {
 
-				console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
+		constructor: WebGLMultisampleRenderTarget,
 
-			}
+		isWebGLMultisampleRenderTarget: true,
 
-			return this;
+		copy: function ( source ) {
 
-		},
+			WebGLRenderTarget.prototype.copy.call( this, source );
 
-		remove: function ( object ) {
+			this.samples = source.samples;
 
-			if ( arguments.length > 1 ) {
+			return this;
 
-				for ( var i = 0; i < arguments.length; i ++ ) {
+		}
 
-					this.remove( arguments[ i ] );
+	} );
 
-				}
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author bhouston / http://clara.io
+	 */
 
-				return this;
+	function Quaternion( x, y, z, w ) {
 
-			}
+		this._x = x || 0;
+		this._y = y || 0;
+		this._z = z || 0;
+		this._w = ( w !== undefined ) ? w : 1;
 
-			var index = this.children.indexOf( object );
+	}
 
-			if ( index !== - 1 ) {
+	Object.assign( Quaternion, {
 
-				object.parent = null;
-				this.children.splice( index, 1 );
+		slerp: function ( qa, qb, qm, t ) {
 
-				object.dispatchEvent( _removedEvent );
+			return qm.copy( qa ).slerp( qb, t );
 
-			}
+		},
 
-			return this;
+		slerpFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) {
 
-		},
+			// fuzz-free, array-based Quaternion SLERP operation
 
-		attach: function ( object ) {
+			var x0 = src0[ srcOffset0 + 0 ],
+				y0 = src0[ srcOffset0 + 1 ],
+				z0 = src0[ srcOffset0 + 2 ],
+				w0 = src0[ srcOffset0 + 3 ],
 
-			// adds object as a child of this, while maintaining the object's world transform
+				x1 = src1[ srcOffset1 + 0 ],
+				y1 = src1[ srcOffset1 + 1 ],
+				z1 = src1[ srcOffset1 + 2 ],
+				w1 = src1[ srcOffset1 + 3 ];
 
-			this.updateWorldMatrix( true, false );
+			if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) {
 
-			_m1$1.getInverse( this.matrixWorld );
+				var s = 1 - t,
 
-			if ( object.parent !== null ) {
+					cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1,
 
-				object.parent.updateWorldMatrix( true, false );
+					dir = ( cos >= 0 ? 1 : - 1 ),
+					sqrSin = 1 - cos * cos;
 
-				_m1$1.multiply( object.parent.matrixWorld );
+				// Skip the Slerp for tiny steps to avoid numeric problems:
+				if ( sqrSin > Number.EPSILON ) {
 
-			}
+					var sin = Math.sqrt( sqrSin ),
+						len = Math.atan2( sin, cos * dir );
 
-			object.applyMatrix4( _m1$1 );
+					s = Math.sin( s * len ) / sin;
+					t = Math.sin( t * len ) / sin;
 
-			object.updateWorldMatrix( false, false );
+				}
 
-			this.add( object );
+				var tDir = t * dir;
 
-			return this;
+				x0 = x0 * s + x1 * tDir;
+				y0 = y0 * s + y1 * tDir;
+				z0 = z0 * s + z1 * tDir;
+				w0 = w0 * s + w1 * tDir;
 
-		},
+				// Normalize in case we just did a lerp:
+				if ( s === 1 - t ) {
 
-		getObjectById: function ( id ) {
+					var f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 );
 
-			return this.getObjectByProperty( 'id', id );
+					x0 *= f;
+					y0 *= f;
+					z0 *= f;
+					w0 *= f;
 
-		},
+				}
 
-		getObjectByName: function ( name ) {
+			}
 
-			return this.getObjectByProperty( 'name', name );
+			dst[ dstOffset ] = x0;
+			dst[ dstOffset + 1 ] = y0;
+			dst[ dstOffset + 2 ] = z0;
+			dst[ dstOffset + 3 ] = w0;
 
 		},
 
-		getObjectByProperty: function ( name, value ) {
-
-			if ( this[ name ] === value ) { return this; }
+		multiplyQuaternionsFlat: function ( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) {
 
-			for ( var i = 0, l = this.children.length; i < l; i ++ ) {
+			var x0 = src0[ srcOffset0 ];
+			var y0 = src0[ srcOffset0 + 1 ];
+			var z0 = src0[ srcOffset0 + 2 ];
+			var w0 = src0[ srcOffset0 + 3 ];
 
-				var child = this.children[ i ];
-				var object = child.getObjectByProperty( name, value );
+			var x1 = src1[ srcOffset1 ];
+			var y1 = src1[ srcOffset1 + 1 ];
+			var z1 = src1[ srcOffset1 + 2 ];
+			var w1 = src1[ srcOffset1 + 3 ];
 
-				if ( object !== undefined ) {
+			dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
+			dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
+			dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
+			dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
 
-					return object;
+			return dst;
 
-				}
+		}
 
-			}
+	} );
 
-			return undefined;
+	Object.defineProperties( Quaternion.prototype, {
 
-		},
+		x: {
 
-		getWorldPosition: function ( target ) {
+			get: function () {
 
-			if ( target === undefined ) {
+				return this._x;
 
-				console.warn( 'THREE.Object3D: .getWorldPosition() target is now required' );
-				target = new Vector3();
+			},
 
-			}
+			set: function ( value ) {
 
-			this.updateMatrixWorld( true );
+				this._x = value;
+				this._onChangeCallback();
 
-			return target.setFromMatrixPosition( this.matrixWorld );
+			}
 
 		},
 
-		getWorldQuaternion: function ( target ) {
+		y: {
 
-			if ( target === undefined ) {
+			get: function () {
 
-				console.warn( 'THREE.Object3D: .getWorldQuaternion() target is now required' );
-				target = new Quaternion();
+				return this._y;
 
-			}
+			},
 
-			this.updateMatrixWorld( true );
+			set: function ( value ) {
 
-			this.matrixWorld.decompose( _position, target, _scale );
+				this._y = value;
+				this._onChangeCallback();
 
-			return target;
+			}
 
 		},
 
-		getWorldScale: function ( target ) {
+		z: {
 
-			if ( target === undefined ) {
+			get: function () {
 
-				console.warn( 'THREE.Object3D: .getWorldScale() target is now required' );
-				target = new Vector3();
+				return this._z;
 
-			}
+			},
 
-			this.updateMatrixWorld( true );
+			set: function ( value ) {
 
-			this.matrixWorld.decompose( _position, _quaternion$2, target );
+				this._z = value;
+				this._onChangeCallback();
 
-			return target;
+			}
 
 		},
 
-		getWorldDirection: function ( target ) {
+		w: {
 
-			if ( target === undefined ) {
+			get: function () {
 
-				console.warn( 'THREE.Object3D: .getWorldDirection() target is now required' );
-				target = new Vector3();
+				return this._w;
 
-			}
+			},
 
-			this.updateMatrixWorld( true );
+			set: function ( value ) {
 
-			var e = this.matrixWorld.elements;
+				this._w = value;
+				this._onChangeCallback();
 
-			return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
+			}
 
-		},
+		}
 
-		raycast: function () {},
+	} );
 
-		traverse: function ( callback ) {
+	Object.assign( Quaternion.prototype, {
 
-			callback( this );
+		isQuaternion: true,
 
-			var children = this.children;
+		set: function ( x, y, z, w ) {
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+			this._x = x;
+			this._y = y;
+			this._z = z;
+			this._w = w;
 
-				children[ i ].traverse( callback );
+			this._onChangeCallback();
 
-			}
+			return this;
 
 		},
 
-		traverseVisible: function ( callback ) {
+		clone: function () {
 
-			if ( this.visible === false ) { return; }
+			return new this.constructor( this._x, this._y, this._z, this._w );
 
-			callback( this );
+		},
 
-			var children = this.children;
+		copy: function ( quaternion ) {
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+			this._x = quaternion.x;
+			this._y = quaternion.y;
+			this._z = quaternion.z;
+			this._w = quaternion.w;
 
-				children[ i ].traverseVisible( callback );
+			this._onChangeCallback();
 
-			}
+			return this;
 
 		},
 
-		traverseAncestors: function ( callback ) {
+		setFromEuler: function ( euler, update ) {
 
-			var parent = this.parent;
+			if ( ! ( euler && euler.isEuler ) ) {
 
-			if ( parent !== null ) {
+				throw new Error( 'THREE.Quaternion: .setFromEuler() now expects an Euler rotation rather than a Vector3 and order.' );
 
-				callback( parent );
+			}
 
-				parent.traverseAncestors( callback );
+			var x = euler._x, y = euler._y, z = euler._z, order = euler.order;
 
-			}
+			// http://www.mathworks.com/matlabcentral/fileexchange/
+			// 	20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
+			//	content/SpinCalc.m
 
-		},
+			var cos = Math.cos;
+			var sin = Math.sin;
 
-		updateMatrix: function () {
+			var c1 = cos( x / 2 );
+			var c2 = cos( y / 2 );
+			var c3 = cos( z / 2 );
 
-			this.matrix.compose( this.position, this.quaternion, this.scale );
+			var s1 = sin( x / 2 );
+			var s2 = sin( y / 2 );
+			var s3 = sin( z / 2 );
 
-			this.matrixWorldNeedsUpdate = true;
+			switch ( order ) {
 
-		},
+				case 'XYZ':
+					this._x = s1 * c2 * c3 + c1 * s2 * s3;
+					this._y = c1 * s2 * c3 - s1 * c2 * s3;
+					this._z = c1 * c2 * s3 + s1 * s2 * c3;
+					this._w = c1 * c2 * c3 - s1 * s2 * s3;
+					break;
 
-		updateMatrixWorld: function ( force ) {
+				case 'YXZ':
+					this._x = s1 * c2 * c3 + c1 * s2 * s3;
+					this._y = c1 * s2 * c3 - s1 * c2 * s3;
+					this._z = c1 * c2 * s3 - s1 * s2 * c3;
+					this._w = c1 * c2 * c3 + s1 * s2 * s3;
+					break;
 
-			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
+				case 'ZXY':
+					this._x = s1 * c2 * c3 - c1 * s2 * s3;
+					this._y = c1 * s2 * c3 + s1 * c2 * s3;
+					this._z = c1 * c2 * s3 + s1 * s2 * c3;
+					this._w = c1 * c2 * c3 - s1 * s2 * s3;
+					break;
 
-			if ( this.matrixWorldNeedsUpdate || force ) {
+				case 'ZYX':
+					this._x = s1 * c2 * c3 - c1 * s2 * s3;
+					this._y = c1 * s2 * c3 + s1 * c2 * s3;
+					this._z = c1 * c2 * s3 - s1 * s2 * c3;
+					this._w = c1 * c2 * c3 + s1 * s2 * s3;
+					break;
 
-				if ( this.parent === null ) {
+				case 'YZX':
+					this._x = s1 * c2 * c3 + c1 * s2 * s3;
+					this._y = c1 * s2 * c3 + s1 * c2 * s3;
+					this._z = c1 * c2 * s3 - s1 * s2 * c3;
+					this._w = c1 * c2 * c3 - s1 * s2 * s3;
+					break;
 
-					this.matrixWorld.copy( this.matrix );
+				case 'XZY':
+					this._x = s1 * c2 * c3 - c1 * s2 * s3;
+					this._y = c1 * s2 * c3 - s1 * c2 * s3;
+					this._z = c1 * c2 * s3 + s1 * s2 * c3;
+					this._w = c1 * c2 * c3 + s1 * s2 * s3;
+					break;
 
-				} else {
+				default:
+					console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order );
 
-					this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
+			}
 
-				}
+			if ( update !== false ) { this._onChangeCallback(); }
 
-				this.matrixWorldNeedsUpdate = false;
+			return this;
 
-				force = true;
+		},
 
-			}
+		setFromAxisAngle: function ( axis, angle ) {
 
-			// update children
+			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
 
-			var children = this.children;
+			// assumes axis is normalized
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+			var halfAngle = angle / 2, s = Math.sin( halfAngle );
 
-				children[ i ].updateMatrixWorld( force );
+			this._x = axis.x * s;
+			this._y = axis.y * s;
+			this._z = axis.z * s;
+			this._w = Math.cos( halfAngle );
 
-			}
+			this._onChangeCallback();
+
+			return this;
 
 		},
 
-		updateWorldMatrix: function ( updateParents, updateChildren ) {
+		setFromRotationMatrix: function ( m ) {
 
-			var parent = this.parent;
+			// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
 
-			if ( updateParents === true && parent !== null ) {
+			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
 
-				parent.updateWorldMatrix( true, false );
+			var te = m.elements,
 
-			}
+				m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
+				m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
+				m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
 
-			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
+				trace = m11 + m22 + m33,
+				s;
 
-			if ( this.parent === null ) {
+			if ( trace > 0 ) {
 
-				this.matrixWorld.copy( this.matrix );
+				s = 0.5 / Math.sqrt( trace + 1.0 );
 
-			} else {
+				this._w = 0.25 / s;
+				this._x = ( m32 - m23 ) * s;
+				this._y = ( m13 - m31 ) * s;
+				this._z = ( m21 - m12 ) * s;
 
-				this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
+			} else if ( m11 > m22 && m11 > m33 ) {
 
-			}
+				s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
 
-			// update children
+				this._w = ( m32 - m23 ) / s;
+				this._x = 0.25 * s;
+				this._y = ( m12 + m21 ) / s;
+				this._z = ( m13 + m31 ) / s;
 
-			if ( updateChildren === true ) {
+			} else if ( m22 > m33 ) {
 
-				var children = this.children;
+				s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
 
-				for ( var i = 0, l = children.length; i < l; i ++ ) {
+				this._w = ( m13 - m31 ) / s;
+				this._x = ( m12 + m21 ) / s;
+				this._y = 0.25 * s;
+				this._z = ( m23 + m32 ) / s;
 
-					children[ i ].updateWorldMatrix( false, true );
+			} else {
 
-				}
+				s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
+
+				this._w = ( m21 - m12 ) / s;
+				this._x = ( m13 + m31 ) / s;
+				this._y = ( m23 + m32 ) / s;
+				this._z = 0.25 * s;
 
 			}
 
-		},
+			this._onChangeCallback();
 
-		toJSON: function ( meta ) {
+			return this;
 
-			// meta is a string when called from JSON.stringify
-			var isRootObject = ( meta === undefined || typeof meta === 'string' );
+		},
 
-			var output = {};
+		setFromUnitVectors: function ( vFrom, vTo ) {
 
-			// meta is a hash used to collect geometries, materials.
-			// not providing it implies that this is the root object
-			// being serialized.
-			if ( isRootObject ) {
+			// assumes direction vectors vFrom and vTo are normalized
 
-				// initialize meta obj
-				meta = {
-					geometries: {},
-					materials: {},
-					textures: {},
-					images: {},
-					shapes: {}
-				};
+			var EPS = 0.000001;
 
-				output.metadata = {
-					version: 4.5,
-					type: 'Object',
-					generator: 'Object3D.toJSON'
-				};
+			var r = vFrom.dot( vTo ) + 1;
 
-			}
+			if ( r < EPS ) {
 
-			// standard Object3D serialization
+				r = 0;
 
-			var object = {};
+				if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
 
-			object.uuid = this.uuid;
-			object.type = this.type;
+					this._x = - vFrom.y;
+					this._y = vFrom.x;
+					this._z = 0;
+					this._w = r;
 
-			if ( this.name !== '' ) { object.name = this.name; }
-			if ( this.castShadow === true ) { object.castShadow = true; }
-			if ( this.receiveShadow === true ) { object.receiveShadow = true; }
-			if ( this.visible === false ) { object.visible = false; }
-			if ( this.frustumCulled === false ) { object.frustumCulled = false; }
-			if ( this.renderOrder !== 0 ) { object.renderOrder = this.renderOrder; }
-			if ( JSON.stringify( this.userData ) !== '{}' ) { object.userData = this.userData; }
+				} else {
 
-			object.layers = this.layers.mask;
-			object.matrix = this.matrix.toArray();
+					this._x = 0;
+					this._y = - vFrom.z;
+					this._z = vFrom.y;
+					this._w = r;
 
-			if ( this.matrixAutoUpdate === false ) { object.matrixAutoUpdate = false; }
+				}
 
-			// object specific properties
+			} else {
 
-			if ( this.isInstancedMesh ) {
+				// crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
 
-				object.type = 'InstancedMesh';
-				object.count = this.count;
-				object.instanceMatrix = this.instanceMatrix.toJSON();
+				this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
+				this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
+				this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
+				this._w = r;
 
 			}
 
-			//
+			return this.normalize();
 
-			function serialize( library, element ) {
+		},
 
-				if ( library[ element.uuid ] === undefined ) {
+		angleTo: function ( q ) {
 
-					library[ element.uuid ] = element.toJSON( meta );
+			return 2 * Math.acos( Math.abs( MathUtils.clamp( this.dot( q ), - 1, 1 ) ) );
 
-				}
+		},
 
-				return element.uuid;
+		rotateTowards: function ( q, step ) {
 
-			}
+			var angle = this.angleTo( q );
 
-			if ( this.isMesh || this.isLine || this.isPoints ) {
+			if ( angle === 0 ) { return this; }
 
-				object.geometry = serialize( meta.geometries, this.geometry );
+			var t = Math.min( 1, step / angle );
 
-				var parameters = this.geometry.parameters;
+			this.slerp( q, t );
 
-				if ( parameters !== undefined && parameters.shapes !== undefined ) {
+			return this;
 
-					var shapes = parameters.shapes;
+		},
 
-					if ( Array.isArray( shapes ) ) {
+		inverse: function () {
 
-						for ( var i = 0, l = shapes.length; i < l; i ++ ) {
+			// quaternion is assumed to have unit length
 
-							var shape = shapes[ i ];
+			return this.conjugate();
 
-							serialize( meta.shapes, shape );
+		},
 
-						}
+		conjugate: function () {
 
-					} else {
+			this._x *= - 1;
+			this._y *= - 1;
+			this._z *= - 1;
 
-						serialize( meta.shapes, shapes );
+			this._onChangeCallback();
 
-					}
-
-				}
-
-			}
+			return this;
 
-			if ( this.material !== undefined ) {
+		},
 
-				if ( Array.isArray( this.material ) ) {
+		dot: function ( v ) {
 
-					var uuids = [];
+			return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
 
-					for ( var i = 0, l = this.material.length; i < l; i ++ ) {
+		},
 
-						uuids.push( serialize( meta.materials, this.material[ i ] ) );
+		lengthSq: function () {
 
-					}
+			return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
 
-					object.material = uuids;
+		},
 
-				} else {
+		length: function () {
 
-					object.material = serialize( meta.materials, this.material );
+			return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
 
-				}
+		},
 
-			}
+		normalize: function () {
 
-			//
+			var l = this.length();
 
-			if ( this.children.length > 0 ) {
+			if ( l === 0 ) {
 
-				object.children = [];
+				this._x = 0;
+				this._y = 0;
+				this._z = 0;
+				this._w = 1;
 
-				for ( var i = 0; i < this.children.length; i ++ ) {
+			} else {
 
-					object.children.push( this.children[ i ].toJSON( meta ).object );
+				l = 1 / l;
 
-				}
+				this._x = this._x * l;
+				this._y = this._y * l;
+				this._z = this._z * l;
+				this._w = this._w * l;
 
 			}
 
-			if ( isRootObject ) {
-
-				var geometries = extractFromCache( meta.geometries );
-				var materials = extractFromCache( meta.materials );
-				var textures = extractFromCache( meta.textures );
-				var images = extractFromCache( meta.images );
-				var shapes = extractFromCache( meta.shapes );
+			this._onChangeCallback();
 
-				if ( geometries.length > 0 ) { output.geometries = geometries; }
-				if ( materials.length > 0 ) { output.materials = materials; }
-				if ( textures.length > 0 ) { output.textures = textures; }
-				if ( images.length > 0 ) { output.images = images; }
-				if ( shapes.length > 0 ) { output.shapes = shapes; }
+			return this;
 
-			}
+		},
 
-			output.object = object;
+		multiply: function ( q, p ) {
 
-			return output;
+			if ( p !== undefined ) {
 
-			// extract data from the cache hash
-			// remove metadata on each item
-			// and return as array
-			function extractFromCache( cache ) {
+				console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
+				return this.multiplyQuaternions( q, p );
 
-				var values = [];
-				for ( var key in cache ) {
+			}
 
-					var data = cache[ key ];
-					delete data.metadata;
-					values.push( data );
+			return this.multiplyQuaternions( this, q );
 
-				}
+		},
 
-				return values;
+		premultiply: function ( q ) {
 
-			}
+			return this.multiplyQuaternions( q, this );
 
 		},
 
-		clone: function ( recursive ) {
-
-			return new this.constructor().copy( this, recursive );
+		multiplyQuaternions: function ( a, b ) {
 
-		},
+			// from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
 
-		copy: function ( source, recursive ) {
+			var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
+			var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
 
-			if ( recursive === undefined ) { recursive = true; }
+			this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
+			this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
+			this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
+			this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
 
-			this.name = source.name;
+			this._onChangeCallback();
 
-			this.up.copy( source.up );
+			return this;
 
-			this.position.copy( source.position );
-			this.quaternion.copy( source.quaternion );
-			this.scale.copy( source.scale );
+		},
 
-			this.matrix.copy( source.matrix );
-			this.matrixWorld.copy( source.matrixWorld );
+		slerp: function ( qb, t ) {
 
-			this.matrixAutoUpdate = source.matrixAutoUpdate;
-			this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
+			if ( t === 0 ) { return this; }
+			if ( t === 1 ) { return this.copy( qb ); }
 
-			this.layers.mask = source.layers.mask;
-			this.visible = source.visible;
+			var x = this._x, y = this._y, z = this._z, w = this._w;
 
-			this.castShadow = source.castShadow;
-			this.receiveShadow = source.receiveShadow;
+			// http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
 
-			this.frustumCulled = source.frustumCulled;
-			this.renderOrder = source.renderOrder;
+			var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
 
-			this.userData = JSON.parse( JSON.stringify( source.userData ) );
+			if ( cosHalfTheta < 0 ) {
 
-			if ( recursive === true ) {
+				this._w = - qb._w;
+				this._x = - qb._x;
+				this._y = - qb._y;
+				this._z = - qb._z;
 
-				for ( var i = 0; i < source.children.length; i ++ ) {
+				cosHalfTheta = - cosHalfTheta;
 
-					var child = source.children[ i ];
-					this.add( child.clone() );
+			} else {
 
-				}
+				this.copy( qb );
 
 			}
 
-			return this;
+			if ( cosHalfTheta >= 1.0 ) {
 
-		}
+				this._w = w;
+				this._x = x;
+				this._y = y;
+				this._z = z;
 
-	} );
+				return this;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			}
 
-	function Scene() {
+			var sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
 
-		Object3D.call( this );
+			if ( sqrSinHalfTheta <= Number.EPSILON ) {
 
-		this.type = 'Scene';
+				var s = 1 - t;
+				this._w = s * w + t * this._w;
+				this._x = s * x + t * this._x;
+				this._y = s * y + t * this._y;
+				this._z = s * z + t * this._z;
 
-		this.background = null;
-		this.environment = null;
-		this.fog = null;
+				this.normalize();
+				this._onChangeCallback();
 
-		this.overrideMaterial = null;
+				return this;
 
-		this.autoUpdate = true; // checked by the renderer
+			}
 
-		if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
+			var sinHalfTheta = Math.sqrt( sqrSinHalfTheta );
+			var halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta );
+			var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
+				ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
 
-			__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef
+			this._w = ( w * ratioA + this._w * ratioB );
+			this._x = ( x * ratioA + this._x * ratioB );
+			this._y = ( y * ratioA + this._y * ratioB );
+			this._z = ( z * ratioA + this._z * ratioB );
 
-		}
+			this._onChangeCallback();
 
-	}
+			return this;
 
-	Scene.prototype = Object.assign( Object.create( Object3D.prototype ), {
+		},
 
-		constructor: Scene,
+		equals: function ( quaternion ) {
 
-		isScene: true,
+			return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
 
-		copy: function ( source, recursive ) {
+		},
 
-			Object3D.prototype.copy.call( this, source, recursive );
+		fromArray: function ( array, offset ) {
 
-			if ( source.background !== null ) { this.background = source.background.clone(); }
-			if ( source.environment !== null ) { this.environment = source.environment.clone(); }
-			if ( source.fog !== null ) { this.fog = source.fog.clone(); }
+			if ( offset === undefined ) { offset = 0; }
 
-			if ( source.overrideMaterial !== null ) { this.overrideMaterial = source.overrideMaterial.clone(); }
+			this._x = array[ offset ];
+			this._y = array[ offset + 1 ];
+			this._z = array[ offset + 2 ];
+			this._w = array[ offset + 3 ];
 
-			this.autoUpdate = source.autoUpdate;
-			this.matrixAutoUpdate = source.matrixAutoUpdate;
+			this._onChangeCallback();
 
 			return this;
 
 		},
 
-		toJSON: function ( meta ) {
+		toArray: function ( array, offset ) {
 
-			var data = Object3D.prototype.toJSON.call( this, meta );
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			if ( this.background !== null ) { data.object.background = this.background.toJSON( meta ); }
-			if ( this.environment !== null ) { data.object.environment = this.environment.toJSON( meta ); }
-			if ( this.fog !== null ) { data.object.fog = this.fog.toJSON(); }
+			array[ offset ] = this._x;
+			array[ offset + 1 ] = this._y;
+			array[ offset + 2 ] = this._z;
+			array[ offset + 3 ] = this._w;
 
-			return data;
+			return array;
 
 		},
 
-		dispose: function () {
-
-			this.dispatchEvent( { type: 'dispose' } );
-
-		}
+		fromBufferAttribute: function ( attribute, index ) {
 
-	} );
+			this._x = attribute.getX( index );
+			this._y = attribute.getY( index );
+			this._z = attribute.getZ( index );
+			this._w = attribute.getW( index );
 
-	var _points = [
-		new Vector3(),
-		new Vector3(),
-		new Vector3(),
-		new Vector3(),
-		new Vector3(),
-		new Vector3(),
-		new Vector3(),
-		new Vector3()
-	];
+			return this;
 
-	var _vector$1 = new Vector3();
+		},
 
-	var _box = new Box3();
+		_onChange: function ( callback ) {
 
-	// triangle centered vertices
+			this._onChangeCallback = callback;
 
-	var _v0 = new Vector3();
-	var _v1$2 = new Vector3();
-	var _v2 = new Vector3();
+			return this;
 
-	// triangle edge vectors
+		},
 
-	var _f0 = new Vector3();
-	var _f1 = new Vector3();
-	var _f2 = new Vector3();
+		_onChangeCallback: function () {}
 
-	var _center = new Vector3();
-	var _extents = new Vector3();
-	var _triangleNormal = new Vector3();
-	var _testAxis = new Vector3();
+	} );
 
 	/**
-	 * @author bhouston / http://clara.io
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author kile / http://kile.stravaganza.org/
+	 * @author philogb / http://blog.thejit.org/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author egraether / http://egraether.com/
 	 * @author WestLangley / http://github.com/WestLangley
 	 */
 
-	function Box3( min, max ) {
+	var _vector = new Vector3();
+	var _quaternion = new Quaternion();
 
-		this.min = ( min !== undefined ) ? min : new Vector3( + Infinity, + Infinity, + Infinity );
-		this.max = ( max !== undefined ) ? max : new Vector3( - Infinity, - Infinity, - Infinity );
+	function Vector3( x, y, z ) {
 
-	}
+		this.x = x || 0;
+		this.y = y || 0;
+		this.z = z || 0;
 
+	}
 
-	Object.assign( Box3.prototype, {
+	Object.assign( Vector3.prototype, {
 
-		isBox3: true,
+		isVector3: true,
 
-		set: function ( min, max ) {
+		set: function ( x, y, z ) {
 
-			this.min.copy( min );
-			this.max.copy( max );
+			this.x = x;
+			this.y = y;
+			this.z = z;
 
 			return this;
 
 		},
 
-		setFromArray: function ( array ) {
+		setScalar: function ( scalar ) {
 
-			var minX = + Infinity;
-			var minY = + Infinity;
-			var minZ = + Infinity;
+			this.x = scalar;
+			this.y = scalar;
+			this.z = scalar;
 
-			var maxX = - Infinity;
-			var maxY = - Infinity;
-			var maxZ = - Infinity;
+			return this;
 
-			for ( var i = 0, l = array.length; i < l; i += 3 ) {
+		},
 
-				var x = array[ i ];
-				var y = array[ i + 1 ];
-				var z = array[ i + 2 ];
+		setX: function ( x ) {
 
-				if ( x < minX ) { minX = x; }
-				if ( y < minY ) { minY = y; }
-				if ( z < minZ ) { minZ = z; }
+			this.x = x;
 
-				if ( x > maxX ) { maxX = x; }
-				if ( y > maxY ) { maxY = y; }
-				if ( z > maxZ ) { maxZ = z; }
+			return this;
 
-			}
+		},
 
-			this.min.set( minX, minY, minZ );
-			this.max.set( maxX, maxY, maxZ );
+		setY: function ( y ) {
+
+			this.y = y;
 
 			return this;
 
 		},
 
-		setFromBufferAttribute: function ( attribute ) {
+		setZ: function ( z ) {
 
-			var minX = + Infinity;
-			var minY = + Infinity;
-			var minZ = + Infinity;
+			this.z = z;
 
-			var maxX = - Infinity;
-			var maxY = - Infinity;
-			var maxZ = - Infinity;
+			return this;
 
-			for ( var i = 0, l = attribute.count; i < l; i ++ ) {
+		},
 
-				var x = attribute.getX( i );
-				var y = attribute.getY( i );
-				var z = attribute.getZ( i );
+		setComponent: function ( index, value ) {
 
-				if ( x < minX ) { minX = x; }
-				if ( y < minY ) { minY = y; }
-				if ( z < minZ ) { minZ = z; }
+			switch ( index ) {
 
-				if ( x > maxX ) { maxX = x; }
-				if ( y > maxY ) { maxY = y; }
-				if ( z > maxZ ) { maxZ = z; }
+				case 0: this.x = value; break;
+				case 1: this.y = value; break;
+				case 2: this.z = value; break;
+				default: throw new Error( 'index is out of range: ' + index );
 
 			}
 
-			this.min.set( minX, minY, minZ );
-			this.max.set( maxX, maxY, maxZ );
-
 			return this;
 
 		},
 
-		setFromPoints: function ( points ) {
-
-			this.makeEmpty();
+		getComponent: function ( index ) {
 
-			for ( var i = 0, il = points.length; i < il; i ++ ) {
+			switch ( index ) {
 
-				this.expandByPoint( points[ i ] );
+				case 0: return this.x;
+				case 1: return this.y;
+				case 2: return this.z;
+				default: throw new Error( 'index is out of range: ' + index );
 
 			}
 
-			return this;
-
 		},
 
-		setFromCenterAndSize: function ( center, size ) {
+		clone: function () {
 
-			var halfSize = _vector$1.copy( size ).multiplyScalar( 0.5 );
+			return new this.constructor( this.x, this.y, this.z );
 
-			this.min.copy( center ).sub( halfSize );
-			this.max.copy( center ).add( halfSize );
+		},
+
+		copy: function ( v ) {
+
+			this.x = v.x;
+			this.y = v.y;
+			this.z = v.z;
 
 			return this;
 
 		},
 
-		setFromObject: function ( object ) {
+		add: function ( v, w ) {
 
-			this.makeEmpty();
+			if ( w !== undefined ) {
 
-			return this.expandByObject( object );
+				console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
+				return this.addVectors( v, w );
 
-		},
+			}
 
-		clone: function () {
+			this.x += v.x;
+			this.y += v.y;
+			this.z += v.z;
 
-			return new this.constructor().copy( this );
+			return this;
 
 		},
 
-		copy: function ( box ) {
+		addScalar: function ( s ) {
 
-			this.min.copy( box.min );
-			this.max.copy( box.max );
+			this.x += s;
+			this.y += s;
+			this.z += s;
 
 			return this;
 
 		},
 
-		makeEmpty: function () {
+		addVectors: function ( a, b ) {
 
-			this.min.x = this.min.y = this.min.z = + Infinity;
-			this.max.x = this.max.y = this.max.z = - Infinity;
+			this.x = a.x + b.x;
+			this.y = a.y + b.y;
+			this.z = a.z + b.z;
 
 			return this;
 
 		},
 
-		isEmpty: function () {
+		addScaledVector: function ( v, s ) {
 
-			// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
+			this.x += v.x * s;
+			this.y += v.y * s;
+			this.z += v.z * s;
 
-			return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
+			return this;
 
 		},
 
-		getCenter: function ( target ) {
+		sub: function ( v, w ) {
 
-			if ( target === undefined ) {
+			if ( w !== undefined ) {
 
-				console.warn( 'THREE.Box3: .getCenter() target is now required' );
-				target = new Vector3();
+				console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
+				return this.subVectors( v, w );
 
 			}
 
-			return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
-
-		},
+			this.x -= v.x;
+			this.y -= v.y;
+			this.z -= v.z;
 
-		getSize: function ( target ) {
+			return this;
 
-			if ( target === undefined ) {
+		},
 
-				console.warn( 'THREE.Box3: .getSize() target is now required' );
-				target = new Vector3();
+		subScalar: function ( s ) {
 
-			}
+			this.x -= s;
+			this.y -= s;
+			this.z -= s;
 
-			return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
+			return this;
 
 		},
 
-		expandByPoint: function ( point ) {
+		subVectors: function ( a, b ) {
 
-			this.min.min( point );
-			this.max.max( point );
+			this.x = a.x - b.x;
+			this.y = a.y - b.y;
+			this.z = a.z - b.z;
 
 			return this;
 
 		},
 
-		expandByVector: function ( vector ) {
+		multiply: function ( v, w ) {
 
-			this.min.sub( vector );
-			this.max.add( vector );
+			if ( w !== undefined ) {
+
+				console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
+				return this.multiplyVectors( v, w );
+
+			}
+
+			this.x *= v.x;
+			this.y *= v.y;
+			this.z *= v.z;
 
 			return this;
 
 		},
 
-		expandByScalar: function ( scalar ) {
+		multiplyScalar: function ( scalar ) {
 
-			this.min.addScalar( - scalar );
-			this.max.addScalar( scalar );
+			this.x *= scalar;
+			this.y *= scalar;
+			this.z *= scalar;
 
 			return this;
 
 		},
 
-		expandByObject: function ( object ) {
+		multiplyVectors: function ( a, b ) {
 
-			// Computes the world-axis-aligned bounding box of an object (including its children),
-			// accounting for both the object's, and children's, world transforms
+			this.x = a.x * b.x;
+			this.y = a.y * b.y;
+			this.z = a.z * b.z;
 
-			object.updateWorldMatrix( false, false );
+			return this;
 
-			var geometry = object.geometry;
+		},
 
-			if ( geometry !== undefined ) {
+		applyEuler: function ( euler ) {
 
-				if ( geometry.boundingBox === null ) {
+			if ( ! ( euler && euler.isEuler ) ) {
 
-					geometry.computeBoundingBox();
+				console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' );
 
-				}
+			}
 
-				_box.copy( geometry.boundingBox );
-				_box.applyMatrix4( object.matrixWorld );
+			return this.applyQuaternion( _quaternion.setFromEuler( euler ) );
 
-				this.union( _box );
+		},
 
-			}
+		applyAxisAngle: function ( axis, angle ) {
 
-			var children = object.children;
+			return this.applyQuaternion( _quaternion.setFromAxisAngle( axis, angle ) );
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+		},
 
-				this.expandByObject( children[ i ] );
+		applyMatrix3: function ( m ) {
 
-			}
+			var x = this.x, y = this.y, z = this.z;
+			var e = m.elements;
+
+			this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
+			this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
+			this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
 
 			return this;
 
 		},
 
-		containsPoint: function ( point ) {
+		applyNormalMatrix: function ( m ) {
 
-			return point.x < this.min.x || point.x > this.max.x ||
-				point.y < this.min.y || point.y > this.max.y ||
-				point.z < this.min.z || point.z > this.max.z ? false : true;
-
-		},
-
-		containsBox: function ( box ) {
-
-			return this.min.x <= box.min.x && box.max.x <= this.max.x &&
-				this.min.y <= box.min.y && box.max.y <= this.max.y &&
-				this.min.z <= box.min.z && box.max.z <= this.max.z;
-
-		},
-
-		getParameter: function ( point, target ) {
-
-			// This can potentially have a divide by zero if the box
-			// has a size dimension of 0.
-
-			if ( target === undefined ) {
-
-				console.warn( 'THREE.Box3: .getParameter() target is now required' );
-				target = new Vector3();
-
-			}
-
-			return target.set(
-				( point.x - this.min.x ) / ( this.max.x - this.min.x ),
-				( point.y - this.min.y ) / ( this.max.y - this.min.y ),
-				( point.z - this.min.z ) / ( this.max.z - this.min.z )
-			);
+			return this.applyMatrix3( m ).normalize();
 
 		},
 
-		intersectsBox: function ( box ) {
-
-			// using 6 splitting planes to rule out intersections.
-			return box.max.x < this.min.x || box.min.x > this.max.x ||
-				box.max.y < this.min.y || box.min.y > this.max.y ||
-				box.max.z < this.min.z || box.min.z > this.max.z ? false : true;
+		applyMatrix4: function ( m ) {
 
-		},
+			var x = this.x, y = this.y, z = this.z;
+			var e = m.elements;
 
-		intersectsSphere: function ( sphere ) {
+			var w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
 
-			// Find the point on the AABB closest to the sphere center.
-			this.clampPoint( sphere.center, _vector$1 );
+			this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
+			this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
+			this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
 
-			// If that point is inside the sphere, the AABB and sphere intersect.
-			return _vector$1.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
+			return this;
 
 		},
 
-		intersectsPlane: function ( plane ) {
+		applyQuaternion: function ( q ) {
 
-			// We compute the minimum and maximum dot product values. If those values
-			// are on the same side (back or front) of the plane, then there is no intersection.
+			var x = this.x, y = this.y, z = this.z;
+			var qx = q.x, qy = q.y, qz = q.z, qw = q.w;
 
-			var min, max;
+			// calculate quat * vector
 
-			if ( plane.normal.x > 0 ) {
+			var ix = qw * x + qy * z - qz * y;
+			var iy = qw * y + qz * x - qx * z;
+			var iz = qw * z + qx * y - qy * x;
+			var iw = - qx * x - qy * y - qz * z;
 
-				min = plane.normal.x * this.min.x;
-				max = plane.normal.x * this.max.x;
+			// calculate result * inverse quat
 
-			} else {
+			this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
+			this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
+			this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
 
-				min = plane.normal.x * this.max.x;
-				max = plane.normal.x * this.min.x;
+			return this;
 
-			}
+		},
 
-			if ( plane.normal.y > 0 ) {
+		project: function ( camera ) {
 
-				min += plane.normal.y * this.min.y;
-				max += plane.normal.y * this.max.y;
+			return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
 
-			} else {
+		},
 
-				min += plane.normal.y * this.max.y;
-				max += plane.normal.y * this.min.y;
+		unproject: function ( camera ) {
 
-			}
+			return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
 
-			if ( plane.normal.z > 0 ) {
+		},
 
-				min += plane.normal.z * this.min.z;
-				max += plane.normal.z * this.max.z;
+		transformDirection: function ( m ) {
 
-			} else {
+			// input: THREE.Matrix4 affine matrix
+			// vector interpreted as a direction
 
-				min += plane.normal.z * this.max.z;
-				max += plane.normal.z * this.min.z;
+			var x = this.x, y = this.y, z = this.z;
+			var e = m.elements;
 
-			}
+			this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
+			this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
+			this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
 
-			return ( min <= - plane.constant && max >= - plane.constant );
+			return this.normalize();
 
 		},
 
-		intersectsTriangle: function ( triangle ) {
-
-			if ( this.isEmpty() ) {
+		divide: function ( v ) {
 
-				return false;
+			this.x /= v.x;
+			this.y /= v.y;
+			this.z /= v.z;
 
-			}
+			return this;
 
-			// compute box center and extents
-			this.getCenter( _center );
-			_extents.subVectors( this.max, _center );
+		},
 
-			// translate triangle to aabb origin
-			_v0.subVectors( triangle.a, _center );
-			_v1$2.subVectors( triangle.b, _center );
-			_v2.subVectors( triangle.c, _center );
+		divideScalar: function ( scalar ) {
 
-			// compute edge vectors for triangle
-			_f0.subVectors( _v1$2, _v0 );
-			_f1.subVectors( _v2, _v1$2 );
-			_f2.subVectors( _v0, _v2 );
+			return this.multiplyScalar( 1 / scalar );
 
-			// test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
-			// make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
-			// axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
-			var axes = [
-				0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
-				_f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
-				- _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
-			];
-			if ( ! satForAxes( axes, _v0, _v1$2, _v2, _extents ) ) {
+		},
 
-				return false;
+		min: function ( v ) {
 
-			}
+			this.x = Math.min( this.x, v.x );
+			this.y = Math.min( this.y, v.y );
+			this.z = Math.min( this.z, v.z );
 
-			// test 3 face normals from the aabb
-			axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
-			if ( ! satForAxes( axes, _v0, _v1$2, _v2, _extents ) ) {
+			return this;
 
-				return false;
+		},
 
-			}
+		max: function ( v ) {
 
-			// finally testing the face normal of the triangle
-			// use already existing triangle edge vectors here
-			_triangleNormal.crossVectors( _f0, _f1 );
-			axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
+			this.x = Math.max( this.x, v.x );
+			this.y = Math.max( this.y, v.y );
+			this.z = Math.max( this.z, v.z );
 
-			return satForAxes( axes, _v0, _v1$2, _v2, _extents );
+			return this;
 
 		},
 
-		clampPoint: function ( point, target ) {
-
-			if ( target === undefined ) {
+		clamp: function ( min, max ) {
 
-				console.warn( 'THREE.Box3: .clampPoint() target is now required' );
-				target = new Vector3();
+			// assumes min < max, componentwise
 
-			}
+			this.x = Math.max( min.x, Math.min( max.x, this.x ) );
+			this.y = Math.max( min.y, Math.min( max.y, this.y ) );
+			this.z = Math.max( min.z, Math.min( max.z, this.z ) );
 
-			return target.copy( point ).clamp( this.min, this.max );
+			return this;
 
 		},
 
-		distanceToPoint: function ( point ) {
+		clampScalar: function ( minVal, maxVal ) {
 
-			var clampedPoint = _vector$1.copy( point ).clamp( this.min, this.max );
+			this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
+			this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
+			this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
 
-			return clampedPoint.sub( point ).length();
+			return this;
 
 		},
 
-		getBoundingSphere: function ( target ) {
+		clampLength: function ( min, max ) {
 
-			if ( target === undefined ) {
+			var length = this.length();
 
-				console.error( 'THREE.Box3: .getBoundingSphere() target is now required' );
-				//target = new Sphere(); // removed to avoid cyclic dependency
+			return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
 
-			}
+		},
 
-			this.getCenter( target.center );
+		floor: function () {
 
-			target.radius = this.getSize( _vector$1 ).length() * 0.5;
+			this.x = Math.floor( this.x );
+			this.y = Math.floor( this.y );
+			this.z = Math.floor( this.z );
 
-			return target;
+			return this;
 
 		},
 
-		intersect: function ( box ) {
-
-			this.min.max( box.min );
-			this.max.min( box.max );
+		ceil: function () {
 
-			// ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values.
-			if ( this.isEmpty() ) { this.makeEmpty(); }
+			this.x = Math.ceil( this.x );
+			this.y = Math.ceil( this.y );
+			this.z = Math.ceil( this.z );
 
 			return this;
 
 		},
 
-		union: function ( box ) {
+		round: function () {
 
-			this.min.min( box.min );
-			this.max.max( box.max );
+			this.x = Math.round( this.x );
+			this.y = Math.round( this.y );
+			this.z = Math.round( this.z );
 
 			return this;
 
 		},
 
-		applyMatrix4: function ( matrix ) {
-
-			// transform of empty box is an empty box.
-			if ( this.isEmpty() ) { return this; }
-
-			// NOTE: I am using a binary pattern to specify all 2^3 combinations below
-			_points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
-			_points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
-			_points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
-			_points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
-			_points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
-			_points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
-			_points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
-			_points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
+		roundToZero: function () {
 
-			this.setFromPoints( _points );
+			this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
+			this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
+			this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
 
 			return this;
 
 		},
 
-		translate: function ( offset ) {
+		negate: function () {
 
-			this.min.add( offset );
-			this.max.add( offset );
+			this.x = - this.x;
+			this.y = - this.y;
+			this.z = - this.z;
 
 			return this;
 
 		},
 
-		equals: function ( box ) {
+		dot: function ( v ) {
 
-			return box.min.equals( this.min ) && box.max.equals( this.max );
+			return this.x * v.x + this.y * v.y + this.z * v.z;
 
-		}
+		},
 
-	} );
+		// TODO lengthSquared?
 
-	function satForAxes( axes, v0, v1, v2, extents ) {
+		lengthSq: function () {
 
-		var i, j;
+			return this.x * this.x + this.y * this.y + this.z * this.z;
 
-		for ( i = 0, j = axes.length - 3; i <= j; i += 3 ) {
+		},
 
-			_testAxis.fromArray( axes, i );
-			// project the aabb onto the seperating axis
-			var r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
-			// project all 3 vertices of the triangle onto the seperating axis
-			var p0 = v0.dot( _testAxis );
-			var p1 = v1.dot( _testAxis );
-			var p2 = v2.dot( _testAxis );
-			// actual test, basically see if either of the most extreme of the triangle points intersects r
-			if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
+		length: function () {
 
-				// points of the projected triangle are outside the projected half-length of the aabb
-				// the axis is seperating and we can exit
-				return false;
+			return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
 
-			}
+		},
 
-		}
+		manhattanLength: function () {
 
-		return true;
+			return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
 
-	}
+		},
 
-	var _box$1 = new Box3();
+		normalize: function () {
 
-	/**
-	 * @author bhouston / http://clara.io
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			return this.divideScalar( this.length() || 1 );
 
-	function Sphere( center, radius ) {
+		},
 
-		this.center = ( center !== undefined ) ? center : new Vector3();
-		this.radius = ( radius !== undefined ) ? radius : - 1;
+		setLength: function ( length ) {
 
-	}
+			return this.normalize().multiplyScalar( length );
 
-	Object.assign( Sphere.prototype, {
+		},
 
-		set: function ( center, radius ) {
+		lerp: function ( v, alpha ) {
 
-			this.center.copy( center );
-			this.radius = radius;
+			this.x += ( v.x - this.x ) * alpha;
+			this.y += ( v.y - this.y ) * alpha;
+			this.z += ( v.z - this.z ) * alpha;
 
 			return this;
 
 		},
 
-		setFromPoints: function ( points, optionalCenter ) {
+		lerpVectors: function ( v1, v2, alpha ) {
 
-			var center = this.center;
+			return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
 
-			if ( optionalCenter !== undefined ) {
+		},
 
-				center.copy( optionalCenter );
+		cross: function ( v, w ) {
 
-			} else {
+			if ( w !== undefined ) {
 
-				_box$1.setFromPoints( points ).getCenter( center );
+				console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
+				return this.crossVectors( v, w );
 
 			}
 
-			var maxRadiusSq = 0;
+			return this.crossVectors( this, v );
 
-			for ( var i = 0, il = points.length; i < il; i ++ ) {
+		},
 
-				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
+		crossVectors: function ( a, b ) {
 
-			}
+			var ax = a.x, ay = a.y, az = a.z;
+			var bx = b.x, by = b.y, bz = b.z;
 
-			this.radius = Math.sqrt( maxRadiusSq );
+			this.x = ay * bz - az * by;
+			this.y = az * bx - ax * bz;
+			this.z = ax * by - ay * bx;
 
 			return this;
 
 		},
 
-		clone: function () {
-
-			return new this.constructor().copy( this );
+		projectOnVector: function ( v ) {
 
-		},
+			var denominator = v.lengthSq();
 
-		copy: function ( sphere ) {
+			if ( denominator === 0 ) { return this.set( 0, 0, 0 ); }
 
-			this.center.copy( sphere.center );
-			this.radius = sphere.radius;
+			var scalar = v.dot( this ) / denominator;
 
-			return this;
+			return this.copy( v ).multiplyScalar( scalar );
 
 		},
 
-		isEmpty: function () {
+		projectOnPlane: function ( planeNormal ) {
 
-			return ( this.radius < 0 );
+			_vector.copy( this ).projectOnVector( planeNormal );
+
+			return this.sub( _vector );
 
 		},
 
-		makeEmpty: function () {
+		reflect: function ( normal ) {
 
-			this.center.set( 0, 0, 0 );
-			this.radius = - 1;
+			// reflect incident vector off plane orthogonal to normal
+			// normal is assumed to have unit length
 
-			return this;
+			return this.sub( _vector.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
 
 		},
 
-		containsPoint: function ( point ) {
+		angleTo: function ( v ) {
 
-			return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
+			var denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
 
-		},
+			if ( denominator === 0 ) { return Math.PI / 2; }
 
-		distanceToPoint: function ( point ) {
+			var theta = this.dot( v ) / denominator;
 
-			return ( point.distanceTo( this.center ) - this.radius );
+			// clamp, to handle numerical problems
 
-		},
+			return Math.acos( MathUtils.clamp( theta, - 1, 1 ) );
 
-		intersectsSphere: function ( sphere ) {
+		},
 
-			var radiusSum = this.radius + sphere.radius;
+		distanceTo: function ( v ) {
 
-			return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
+			return Math.sqrt( this.distanceToSquared( v ) );
 
 		},
 
-		intersectsBox: function ( box ) {
+		distanceToSquared: function ( v ) {
 
-			return box.intersectsSphere( this );
+			var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
+
+			return dx * dx + dy * dy + dz * dz;
 
 		},
 
-		intersectsPlane: function ( plane ) {
+		manhattanDistanceTo: function ( v ) {
 
-			return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
+			return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
 
 		},
 
-		clampPoint: function ( point, target ) {
+		setFromSpherical: function ( s ) {
 
-			var deltaLengthSq = this.center.distanceToSquared( point );
+			return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
 
-			if ( target === undefined ) {
+		},
 
-				console.warn( 'THREE.Sphere: .clampPoint() target is now required' );
-				target = new Vector3();
+		setFromSphericalCoords: function ( radius, phi, theta ) {
 
-			}
+			var sinPhiRadius = Math.sin( phi ) * radius;
 
-			target.copy( point );
+			this.x = sinPhiRadius * Math.sin( theta );
+			this.y = Math.cos( phi ) * radius;
+			this.z = sinPhiRadius * Math.cos( theta );
 
-			if ( deltaLengthSq > ( this.radius * this.radius ) ) {
+			return this;
 
-				target.sub( this.center ).normalize();
-				target.multiplyScalar( this.radius ).add( this.center );
+		},
 
-			}
+		setFromCylindrical: function ( c ) {
 
-			return target;
+			return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
 
 		},
 
-		getBoundingBox: function ( target ) {
-
-			if ( target === undefined ) {
+		setFromCylindricalCoords: function ( radius, theta, y ) {
 
-				console.warn( 'THREE.Sphere: .getBoundingBox() target is now required' );
-				target = new Box3();
+			this.x = radius * Math.sin( theta );
+			this.y = y;
+			this.z = radius * Math.cos( theta );
 
-			}
+			return this;
 
-			if ( this.isEmpty() ) {
+		},
 
-				// Empty sphere produces empty bounding box
-				target.makeEmpty();
-				return target;
+		setFromMatrixPosition: function ( m ) {
 
-			}
+			var e = m.elements;
 
-			target.set( this.center, this.center );
-			target.expandByScalar( this.radius );
+			this.x = e[ 12 ];
+			this.y = e[ 13 ];
+			this.z = e[ 14 ];
 
-			return target;
+			return this;
 
 		},
 
-		applyMatrix4: function ( matrix ) {
+		setFromMatrixScale: function ( m ) {
 
-			this.center.applyMatrix4( matrix );
-			this.radius = this.radius * matrix.getMaxScaleOnAxis();
+			var sx = this.setFromMatrixColumn( m, 0 ).length();
+			var sy = this.setFromMatrixColumn( m, 1 ).length();
+			var sz = this.setFromMatrixColumn( m, 2 ).length();
+
+			this.x = sx;
+			this.y = sy;
+			this.z = sz;
 
 			return this;
 
 		},
 
-		translate: function ( offset ) {
-
-			this.center.add( offset );
+		setFromMatrixColumn: function ( m, index ) {
 
-			return this;
+			return this.fromArray( m.elements, index * 4 );
 
 		},
 
-		equals: function ( sphere ) {
+		setFromMatrix3Column: function ( m, index ) {
 
-			return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
+			return this.fromArray( m.elements, index * 3 );
 
-		}
+		},
 
-	} );
+		equals: function ( v ) {
 
-	var _vector$2 = new Vector3();
-	var _segCenter = new Vector3();
-	var _segDir = new Vector3();
-	var _diff = new Vector3();
+			return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
 
-	var _edge1 = new Vector3();
-	var _edge2 = new Vector3();
-	var _normal = new Vector3();
+		},
 
-	/**
-	 * @author bhouston / http://clara.io
-	 */
+		fromArray: function ( array, offset ) {
 
-	function Ray( origin, direction ) {
+			if ( offset === undefined ) { offset = 0; }
 
-		this.origin = ( origin !== undefined ) ? origin : new Vector3();
-		this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
+			this.x = array[ offset ];
+			this.y = array[ offset + 1 ];
+			this.z = array[ offset + 2 ];
 
-	}
+			return this;
 
-	Object.assign( Ray.prototype, {
+		},
 
-		set: function ( origin, direction ) {
+		toArray: function ( array, offset ) {
 
-			this.origin.copy( origin );
-			this.direction.copy( direction );
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			return this;
+			array[ offset ] = this.x;
+			array[ offset + 1 ] = this.y;
+			array[ offset + 2 ] = this.z;
+
+			return array;
 
 		},
 
-		clone: function () {
+		fromBufferAttribute: function ( attribute, index, offset ) {
 
-			return new this.constructor().copy( this );
+			if ( offset !== undefined ) {
 
-		},
+				console.warn( 'THREE.Vector3: offset has been removed from .fromBufferAttribute().' );
 
-		copy: function ( ray ) {
+			}
 
-			this.origin.copy( ray.origin );
-			this.direction.copy( ray.direction );
+			this.x = attribute.getX( index );
+			this.y = attribute.getY( index );
+			this.z = attribute.getZ( index );
 
 			return this;
 
 		},
 
-		at: function ( t, target ) {
-
-			if ( target === undefined ) {
+		random: function () {
 
-				console.warn( 'THREE.Ray: .at() target is now required' );
-				target = new Vector3();
-
-			}
-
-			return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
-
-		},
-
-		lookAt: function ( v ) {
-
-			this.direction.copy( v ).sub( this.origin ).normalize();
+			this.x = Math.random();
+			this.y = Math.random();
+			this.z = Math.random();
 
 			return this;
 
-		},
+		}
 
-		recast: function ( t ) {
+	} );
 
-			this.origin.copy( this.at( t, _vector$2 ) );
+	var _v1 = new Vector3();
+	var _m1 = new Matrix4();
+	var _zero = new Vector3( 0, 0, 0 );
+	var _one = new Vector3( 1, 1, 1 );
+	var _x = new Vector3();
+	var _y = new Vector3();
+	var _z = new Vector3();
 
-			return this;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author supereggbert / http://www.paulbrunt.co.uk/
+	 * @author philogb / http://blog.thejit.org/
+	 * @author jordi_ros / http://plattsoft.com
+	 * @author D1plo1d / http://github.com/D1plo1d
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author timknip / http://www.floorplanner.com/
+	 * @author bhouston / http://clara.io
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-		},
+	function Matrix4() {
 
-		closestPointToPoint: function ( point, target ) {
+		this.elements = [
 
-			if ( target === undefined ) {
+			1, 0, 0, 0,
+			0, 1, 0, 0,
+			0, 0, 1, 0,
+			0, 0, 0, 1
 
-				console.warn( 'THREE.Ray: .closestPointToPoint() target is now required' );
-				target = new Vector3();
+		];
 
-			}
+		if ( arguments.length > 0 ) {
 
-			target.subVectors( point, this.origin );
+			console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
 
-			var directionDistance = target.dot( this.direction );
+		}
 
-			if ( directionDistance < 0 ) {
+	}
 
-				return target.copy( this.origin );
+	Object.assign( Matrix4.prototype, {
 
-			}
+		isMatrix4: true,
 
-			return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
+		set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
 
-		},
+			var te = this.elements;
 
-		distanceToPoint: function ( point ) {
+			te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
+			te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
+			te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
+			te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
 
-			return Math.sqrt( this.distanceSqToPoint( point ) );
+			return this;
 
 		},
 
-		distanceSqToPoint: function ( point ) {
+		identity: function () {
 
-			var directionDistance = _vector$2.subVectors( point, this.origin ).dot( this.direction );
+			this.set(
 
-			// point behind the ray
+				1, 0, 0, 0,
+				0, 1, 0, 0,
+				0, 0, 1, 0,
+				0, 0, 0, 1
 
-			if ( directionDistance < 0 ) {
+			);
 
-				return this.origin.distanceToSquared( point );
+			return this;
 
-			}
+		},
 
-			_vector$2.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
+		clone: function () {
 
-			return _vector$2.distanceToSquared( point );
+			return new Matrix4().fromArray( this.elements );
 
 		},
 
-		distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
-
-			// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
-			// It returns the min distance between the ray and the segment
-			// defined by v0 and v1
-			// It can also set two optional targets :
-			// - The closest point on the ray
-			// - The closest point on the segment
+		copy: function ( m ) {
 
-			_segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
-			_segDir.copy( v1 ).sub( v0 ).normalize();
-			_diff.copy( this.origin ).sub( _segCenter );
+			var te = this.elements;
+			var me = m.elements;
 
-			var segExtent = v0.distanceTo( v1 ) * 0.5;
-			var a01 = - this.direction.dot( _segDir );
-			var b0 = _diff.dot( this.direction );
-			var b1 = - _diff.dot( _segDir );
-			var c = _diff.lengthSq();
-			var det = Math.abs( 1 - a01 * a01 );
-			var s0, s1, sqrDist, extDet;
+			te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ];
+			te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ];
+			te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ];
+			te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ];
 
-			if ( det > 0 ) {
+			return this;
 
-				// The ray and segment are not parallel.
+		},
 
-				s0 = a01 * b1 - b0;
-				s1 = a01 * b0 - b1;
-				extDet = segExtent * det;
+		copyPosition: function ( m ) {
 
-				if ( s0 >= 0 ) {
+			var te = this.elements, me = m.elements;
 
-					if ( s1 >= - extDet ) {
+			te[ 12 ] = me[ 12 ];
+			te[ 13 ] = me[ 13 ];
+			te[ 14 ] = me[ 14 ];
 
-						if ( s1 <= extDet ) {
+			return this;
 
-							// region 0
-							// Minimum at interior points of ray and segment.
+		},
 
-							var invDet = 1 / det;
-							s0 *= invDet;
-							s1 *= invDet;
-							sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
+		extractBasis: function ( xAxis, yAxis, zAxis ) {
 
-						} else {
+			xAxis.setFromMatrixColumn( this, 0 );
+			yAxis.setFromMatrixColumn( this, 1 );
+			zAxis.setFromMatrixColumn( this, 2 );
 
-							// region 1
+			return this;
 
-							s1 = segExtent;
-							s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
-							sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
+		},
 
-						}
+		makeBasis: function ( xAxis, yAxis, zAxis ) {
 
-					} else {
+			this.set(
+				xAxis.x, yAxis.x, zAxis.x, 0,
+				xAxis.y, yAxis.y, zAxis.y, 0,
+				xAxis.z, yAxis.z, zAxis.z, 0,
+				0, 0, 0, 1
+			);
 
-						// region 5
+			return this;
 
-						s1 = - segExtent;
-						s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
-						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
+		},
 
-					}
+		extractRotation: function ( m ) {
 
-				} else {
+			// this method does not support reflection matrices
 
-					if ( s1 <= - extDet ) {
+			var te = this.elements;
+			var me = m.elements;
 
-						// region 4
+			var scaleX = 1 / _v1.setFromMatrixColumn( m, 0 ).length();
+			var scaleY = 1 / _v1.setFromMatrixColumn( m, 1 ).length();
+			var scaleZ = 1 / _v1.setFromMatrixColumn( m, 2 ).length();
 
-						s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
-						s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
-						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
+			te[ 0 ] = me[ 0 ] * scaleX;
+			te[ 1 ] = me[ 1 ] * scaleX;
+			te[ 2 ] = me[ 2 ] * scaleX;
+			te[ 3 ] = 0;
 
-					} else if ( s1 <= extDet ) {
+			te[ 4 ] = me[ 4 ] * scaleY;
+			te[ 5 ] = me[ 5 ] * scaleY;
+			te[ 6 ] = me[ 6 ] * scaleY;
+			te[ 7 ] = 0;
 
-						// region 3
+			te[ 8 ] = me[ 8 ] * scaleZ;
+			te[ 9 ] = me[ 9 ] * scaleZ;
+			te[ 10 ] = me[ 10 ] * scaleZ;
+			te[ 11 ] = 0;
 
-						s0 = 0;
-						s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
-						sqrDist = s1 * ( s1 + 2 * b1 ) + c;
+			te[ 12 ] = 0;
+			te[ 13 ] = 0;
+			te[ 14 ] = 0;
+			te[ 15 ] = 1;
 
-					} else {
+			return this;
 
-						// region 2
+		},
 
-						s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
-						s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
-						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
+		makeRotationFromEuler: function ( euler ) {
 
-					}
+			if ( ! ( euler && euler.isEuler ) ) {
 
-				}
+				console.error( 'THREE.Matrix4: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
 
-			} else {
+			}
 
-				// Ray and segment are parallel.
+			var te = this.elements;
 
-				s1 = ( a01 > 0 ) ? - segExtent : segExtent;
-				s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
-				sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
+			var x = euler.x, y = euler.y, z = euler.z;
+			var a = Math.cos( x ), b = Math.sin( x );
+			var c = Math.cos( y ), d = Math.sin( y );
+			var e = Math.cos( z ), f = Math.sin( z );
 
-			}
+			if ( euler.order === 'XYZ' ) {
 
-			if ( optionalPointOnRay ) {
+				var ae = a * e, af = a * f, be = b * e, bf = b * f;
 
-				optionalPointOnRay.copy( this.direction ).multiplyScalar( s0 ).add( this.origin );
+				te[ 0 ] = c * e;
+				te[ 4 ] = - c * f;
+				te[ 8 ] = d;
 
-			}
+				te[ 1 ] = af + be * d;
+				te[ 5 ] = ae - bf * d;
+				te[ 9 ] = - b * c;
 
-			if ( optionalPointOnSegment ) {
+				te[ 2 ] = bf - ae * d;
+				te[ 6 ] = be + af * d;
+				te[ 10 ] = a * c;
 
-				optionalPointOnSegment.copy( _segDir ).multiplyScalar( s1 ).add( _segCenter );
+			} else if ( euler.order === 'YXZ' ) {
 
-			}
+				var ce = c * e, cf = c * f, de = d * e, df = d * f;
 
-			return sqrDist;
+				te[ 0 ] = ce + df * b;
+				te[ 4 ] = de * b - cf;
+				te[ 8 ] = a * d;
 
-		},
+				te[ 1 ] = a * f;
+				te[ 5 ] = a * e;
+				te[ 9 ] = - b;
 
-		intersectSphere: function ( sphere, target ) {
+				te[ 2 ] = cf * b - de;
+				te[ 6 ] = df + ce * b;
+				te[ 10 ] = a * c;
 
-			_vector$2.subVectors( sphere.center, this.origin );
-			var tca = _vector$2.dot( this.direction );
-			var d2 = _vector$2.dot( _vector$2 ) - tca * tca;
-			var radius2 = sphere.radius * sphere.radius;
+			} else if ( euler.order === 'ZXY' ) {
 
-			if ( d2 > radius2 ) { return null; }
+				var ce = c * e, cf = c * f, de = d * e, df = d * f;
 
-			var thc = Math.sqrt( radius2 - d2 );
+				te[ 0 ] = ce - df * b;
+				te[ 4 ] = - a * f;
+				te[ 8 ] = de + cf * b;
 
-			// t0 = first intersect point - entrance on front of sphere
-			var t0 = tca - thc;
+				te[ 1 ] = cf + de * b;
+				te[ 5 ] = a * e;
+				te[ 9 ] = df - ce * b;
 
-			// t1 = second intersect point - exit point on back of sphere
-			var t1 = tca + thc;
+				te[ 2 ] = - a * d;
+				te[ 6 ] = b;
+				te[ 10 ] = a * c;
 
-			// test to see if both t0 and t1 are behind the ray - if so, return null
-			if ( t0 < 0 && t1 < 0 ) { return null; }
+			} else if ( euler.order === 'ZYX' ) {
 
-			// test to see if t0 is behind the ray:
-			// if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
-			// in order to always return an intersect point that is in front of the ray.
-			if ( t0 < 0 ) { return this.at( t1, target ); }
+				var ae = a * e, af = a * f, be = b * e, bf = b * f;
 
-			// else t0 is in front of the ray, so return the first collision point scaled by t0
-			return this.at( t0, target );
+				te[ 0 ] = c * e;
+				te[ 4 ] = be * d - af;
+				te[ 8 ] = ae * d + bf;
 
-		},
+				te[ 1 ] = c * f;
+				te[ 5 ] = bf * d + ae;
+				te[ 9 ] = af * d - be;
 
-		intersectsSphere: function ( sphere ) {
+				te[ 2 ] = - d;
+				te[ 6 ] = b * c;
+				te[ 10 ] = a * c;
 
-			return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
+			} else if ( euler.order === 'YZX' ) {
 
-		},
+				var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
 
-		distanceToPlane: function ( plane ) {
+				te[ 0 ] = c * e;
+				te[ 4 ] = bd - ac * f;
+				te[ 8 ] = bc * f + ad;
 
-			var denominator = plane.normal.dot( this.direction );
+				te[ 1 ] = f;
+				te[ 5 ] = a * e;
+				te[ 9 ] = - b * e;
 
-			if ( denominator === 0 ) {
+				te[ 2 ] = - d * e;
+				te[ 6 ] = ad * f + bc;
+				te[ 10 ] = ac - bd * f;
 
-				// line is coplanar, return origin
-				if ( plane.distanceToPoint( this.origin ) === 0 ) {
+			} else if ( euler.order === 'XZY' ) {
 
-					return 0;
+				var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
 
-				}
+				te[ 0 ] = c * e;
+				te[ 4 ] = - f;
+				te[ 8 ] = d * e;
 
-				// Null is preferable to undefined since undefined means.... it is undefined
+				te[ 1 ] = ac * f + bd;
+				te[ 5 ] = a * e;
+				te[ 9 ] = ad * f - bc;
 
-				return null;
+				te[ 2 ] = bc * f - ad;
+				te[ 6 ] = b * e;
+				te[ 10 ] = bd * f + ac;
 
 			}
 
-			var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
+			// bottom row
+			te[ 3 ] = 0;
+			te[ 7 ] = 0;
+			te[ 11 ] = 0;
 
-			// Return if the ray never intersects the plane
+			// last column
+			te[ 12 ] = 0;
+			te[ 13 ] = 0;
+			te[ 14 ] = 0;
+			te[ 15 ] = 1;
 
-			return t >= 0 ? t : null;
+			return this;
 
 		},
 
-		intersectPlane: function ( plane, target ) {
-
-			var t = this.distanceToPlane( plane );
-
-			if ( t === null ) {
-
-				return null;
-
-			}
+		makeRotationFromQuaternion: function ( q ) {
 
-			return this.at( t, target );
+			return this.compose( _zero, q, _one );
 
 		},
 
-		intersectsPlane: function ( plane ) {
-
-			// check if the ray lies on the plane first
-
-			var distToPoint = plane.distanceToPoint( this.origin );
-
-			if ( distToPoint === 0 ) {
+		lookAt: function ( eye, target, up ) {
 
-				return true;
+			var te = this.elements;
 
-			}
+			_z.subVectors( eye, target );
 
-			var denominator = plane.normal.dot( this.direction );
+			if ( _z.lengthSq() === 0 ) {
 
-			if ( denominator * distToPoint < 0 ) {
+				// eye and target are in the same position
 
-				return true;
+				_z.z = 1;
 
 			}
 
-			// ray origin is behind the plane (and is pointing behind it)
-
-			return false;
-
-		},
+			_z.normalize();
+			_x.crossVectors( up, _z );
 
-		intersectBox: function ( box, target ) {
+			if ( _x.lengthSq() === 0 ) {
 
-			var tmin, tmax, tymin, tymax, tzmin, tzmax;
+				// up and z are parallel
 
-			var invdirx = 1 / this.direction.x,
-				invdiry = 1 / this.direction.y,
-				invdirz = 1 / this.direction.z;
+				if ( Math.abs( up.z ) === 1 ) {
 
-			var origin = this.origin;
+					_z.x += 0.0001;
 
-			if ( invdirx >= 0 ) {
+				} else {
 
-				tmin = ( box.min.x - origin.x ) * invdirx;
-				tmax = ( box.max.x - origin.x ) * invdirx;
+					_z.z += 0.0001;
 
-			} else {
+				}
 
-				tmin = ( box.max.x - origin.x ) * invdirx;
-				tmax = ( box.min.x - origin.x ) * invdirx;
+				_z.normalize();
+				_x.crossVectors( up, _z );
 
 			}
 
-			if ( invdiry >= 0 ) {
-
-				tymin = ( box.min.y - origin.y ) * invdiry;
-				tymax = ( box.max.y - origin.y ) * invdiry;
+			_x.normalize();
+			_y.crossVectors( _z, _x );
 
-			} else {
+			te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
+			te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
+			te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;
 
-				tymin = ( box.max.y - origin.y ) * invdiry;
-				tymax = ( box.min.y - origin.y ) * invdiry;
+			return this;
 
-			}
+		},
 
-			if ( ( tmin > tymax ) || ( tymin > tmax ) ) { return null; }
+		multiply: function ( m, n ) {
 
-			// These lines also handle the case where tmin or tmax is NaN
-			// (result of 0 * Infinity). x !== x returns true if x is NaN
+			if ( n !== undefined ) {
 
-			if ( tymin > tmin || tmin !== tmin ) { tmin = tymin; }
+				console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
+				return this.multiplyMatrices( m, n );
 
-			if ( tymax < tmax || tmax !== tmax ) { tmax = tymax; }
+			}
 
-			if ( invdirz >= 0 ) {
+			return this.multiplyMatrices( this, m );
 
-				tzmin = ( box.min.z - origin.z ) * invdirz;
-				tzmax = ( box.max.z - origin.z ) * invdirz;
+		},
 
-			} else {
+		premultiply: function ( m ) {
 
-				tzmin = ( box.max.z - origin.z ) * invdirz;
-				tzmax = ( box.min.z - origin.z ) * invdirz;
+			return this.multiplyMatrices( m, this );
 
-			}
+		},
 
-			if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) { return null; }
+		multiplyMatrices: function ( a, b ) {
 
-			if ( tzmin > tmin || tmin !== tmin ) { tmin = tzmin; }
+			var ae = a.elements;
+			var be = b.elements;
+			var te = this.elements;
 
-			if ( tzmax < tmax || tmax !== tmax ) { tmax = tzmax; }
+			var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
+			var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
+			var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
+			var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
 
-			//return point closest to the ray (positive side)
+			var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
+			var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
+			var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
+			var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
 
-			if ( tmax < 0 ) { return null; }
+			te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
+			te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
+			te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
+			te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
 
-			return this.at( tmin >= 0 ? tmin : tmax, target );
+			te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
+			te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
+			te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
+			te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
 
-		},
+			te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
+			te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
+			te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
+			te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
 
-		intersectsBox: function ( box ) {
+			te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
+			te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
+			te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
+			te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
 
-			return this.intersectBox( box, _vector$2 ) !== null;
+			return this;
 
 		},
 
-		intersectTriangle: function ( a, b, c, backfaceCulling, target ) {
+		multiplyScalar: function ( s ) {
 
-			// Compute the offset origin, edges, and normal.
+			var te = this.elements;
 
-			// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
+			te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
+			te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
+			te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
+			te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
 
-			_edge1.subVectors( b, a );
-			_edge2.subVectors( c, a );
-			_normal.crossVectors( _edge1, _edge2 );
+			return this;
 
-			// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
-			// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
-			//   |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
-			//   |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
-			//   |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
-			var DdN = this.direction.dot( _normal );
-			var sign;
+		},
 
-			if ( DdN > 0 ) {
+		determinant: function () {
 
-				if ( backfaceCulling ) { return null; }
-				sign = 1;
+			var te = this.elements;
 
-			} else if ( DdN < 0 ) {
+			var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
+			var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
+			var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
+			var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
 
-				sign = - 1;
-				DdN = - DdN;
+			//TODO: make this more efficient
+			//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
 
-			} else {
+			return (
+				n41 * (
+					+ n14 * n23 * n32
+					 - n13 * n24 * n32
+					 - n14 * n22 * n33
+					 + n12 * n24 * n33
+					 + n13 * n22 * n34
+					 - n12 * n23 * n34
+				) +
+				n42 * (
+					+ n11 * n23 * n34
+					 - n11 * n24 * n33
+					 + n14 * n21 * n33
+					 - n13 * n21 * n34
+					 + n13 * n24 * n31
+					 - n14 * n23 * n31
+				) +
+				n43 * (
+					+ n11 * n24 * n32
+					 - n11 * n22 * n34
+					 - n14 * n21 * n32
+					 + n12 * n21 * n34
+					 + n14 * n22 * n31
+					 - n12 * n24 * n31
+				) +
+				n44 * (
+					- n13 * n22 * n31
+					 - n11 * n23 * n32
+					 + n11 * n22 * n33
+					 + n13 * n21 * n32
+					 - n12 * n21 * n33
+					 + n12 * n23 * n31
+				)
 
-				return null;
+			);
 
-			}
+		},
 
-			_diff.subVectors( this.origin, a );
-			var DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) );
+		transpose: function () {
 
-			// b1 < 0, no intersection
-			if ( DdQxE2 < 0 ) {
+			var te = this.elements;
+			var tmp;
 
-				return null;
+			tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
+			tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
+			tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
 
-			}
+			tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
+			tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
+			tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
 
-			var DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) );
+			return this;
 
-			// b2 < 0, no intersection
-			if ( DdE1xQ < 0 ) {
+		},
 
-				return null;
+		setPosition: function ( x, y, z ) {
 
-			}
+			var te = this.elements;
 
-			// b1+b2 > 1, no intersection
-			if ( DdQxE2 + DdE1xQ > DdN ) {
+			if ( x.isVector3 ) {
 
-				return null;
+				te[ 12 ] = x.x;
+				te[ 13 ] = x.y;
+				te[ 14 ] = x.z;
+
+			} else {
+
+				te[ 12 ] = x;
+				te[ 13 ] = y;
+				te[ 14 ] = z;
 
 			}
 
-			// Line intersects triangle, check if ray does.
-			var QdN = - sign * _diff.dot( _normal );
+			return this;
 
-			// t < 0, no intersection
-			if ( QdN < 0 ) {
+		},
 
-				return null;
+		getInverse: function ( m, throwOnDegenerate ) {
+
+			if ( throwOnDegenerate !== undefined ) {
+
+				console.warn( "THREE.Matrix4: .getInverse() can no longer be configured to throw on degenerate." );
 
 			}
 
-			// Ray intersects triangle.
-			return this.at( QdN / DdN, target );
+			// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
+			var te = this.elements,
+				me = m.elements,
 
-		},
+				n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ], n41 = me[ 3 ],
+				n12 = me[ 4 ], n22 = me[ 5 ], n32 = me[ 6 ], n42 = me[ 7 ],
+				n13 = me[ 8 ], n23 = me[ 9 ], n33 = me[ 10 ], n43 = me[ 11 ],
+				n14 = me[ 12 ], n24 = me[ 13 ], n34 = me[ 14 ], n44 = me[ 15 ],
 
-		applyMatrix4: function ( matrix4 ) {
+				t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
+				t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
+				t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
+				t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
 
-			this.origin.applyMatrix4( matrix4 );
-			this.direction.transformDirection( matrix4 );
+			var det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
 
-			return this;
+			if ( det === 0 ) { return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); }
 
-		},
+			var detInv = 1 / det;
 
-		equals: function ( ray ) {
+			te[ 0 ] = t11 * detInv;
+			te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv;
+			te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv;
+			te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv;
 
-			return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
+			te[ 4 ] = t12 * detInv;
+			te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv;
+			te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv;
+			te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv;
 
-		}
+			te[ 8 ] = t13 * detInv;
+			te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv;
+			te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv;
+			te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv;
 
-	} );
+			te[ 12 ] = t14 * detInv;
+			te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv;
+			te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv;
+			te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv;
 
-	/**
-	 * @author bhouston / http://clara.io
-	 */
+			return this;
 
-	var _vector1 = new Vector3();
-	var _vector2 = new Vector3();
-	var _normalMatrix = new Matrix3();
+		},
 
-	function Plane( normal, constant ) {
+		scale: function ( v ) {
 
-		// normal is assumed to be normalized
+			var te = this.elements;
+			var x = v.x, y = v.y, z = v.z;
 
-		this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
-		this.constant = ( constant !== undefined ) ? constant : 0;
+			te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
+			te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
+			te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
+			te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
 
-	}
+			return this;
 
-	Object.assign( Plane.prototype, {
+		},
 
-		isPlane: true,
+		getMaxScaleOnAxis: function () {
 
-		set: function ( normal, constant ) {
+			var te = this.elements;
 
-			this.normal.copy( normal );
-			this.constant = constant;
+			var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
+			var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
+			var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
 
-			return this;
+			return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) );
 
 		},
 
-		setComponents: function ( x, y, z, w ) {
+		makeTranslation: function ( x, y, z ) {
 
-			this.normal.set( x, y, z );
-			this.constant = w;
+			this.set(
+
+				1, 0, 0, x,
+				0, 1, 0, y,
+				0, 0, 1, z,
+				0, 0, 0, 1
+
+			);
 
 			return this;
 
 		},
 
-		setFromNormalAndCoplanarPoint: function ( normal, point ) {
+		makeRotationX: function ( theta ) {
 
-			this.normal.copy( normal );
-			this.constant = - point.dot( this.normal );
+			var c = Math.cos( theta ), s = Math.sin( theta );
+
+			this.set(
+
+				1, 0, 0, 0,
+				0, c, - s, 0,
+				0, s, c, 0,
+				0, 0, 0, 1
+
+			);
 
 			return this;
 
 		},
 
-		setFromCoplanarPoints: function ( a, b, c ) {
+		makeRotationY: function ( theta ) {
 
-			var normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
+			var c = Math.cos( theta ), s = Math.sin( theta );
 
-			// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
+			this.set(
 
-			this.setFromNormalAndCoplanarPoint( normal, a );
+				 c, 0, s, 0,
+				 0, 1, 0, 0,
+				- s, 0, c, 0,
+				 0, 0, 0, 1
+
+			);
 
 			return this;
 
 		},
 
-		clone: function () {
+		makeRotationZ: function ( theta ) {
 
-			return new this.constructor().copy( this );
+			var c = Math.cos( theta ), s = Math.sin( theta );
 
-		},
+			this.set(
 
-		copy: function ( plane ) {
+				c, - s, 0, 0,
+				s, c, 0, 0,
+				0, 0, 1, 0,
+				0, 0, 0, 1
 
-			this.normal.copy( plane.normal );
-			this.constant = plane.constant;
+			);
 
 			return this;
 
 		},
 
-		normalize: function () {
+		makeRotationAxis: function ( axis, angle ) {
 
-			// Note: will lead to a divide by zero if the plane is invalid.
+			// Based on http://www.gamedev.net/reference/articles/article1199.asp
 
-			var inverseNormalLength = 1.0 / this.normal.length();
-			this.normal.multiplyScalar( inverseNormalLength );
-			this.constant *= inverseNormalLength;
+			var c = Math.cos( angle );
+			var s = Math.sin( angle );
+			var t = 1 - c;
+			var x = axis.x, y = axis.y, z = axis.z;
+			var tx = t * x, ty = t * y;
 
-			return this;
+			this.set(
+
+				tx * x + c, tx * y - s * z, tx * z + s * y, 0,
+				tx * y + s * z, ty * y + c, ty * z - s * x, 0,
+				tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
+				0, 0, 0, 1
+
+			);
+
+			 return this;
 
 		},
 
-		negate: function () {
+		makeScale: function ( x, y, z ) {
 
-			this.constant *= - 1;
-			this.normal.negate();
+			this.set(
+
+				x, 0, 0, 0,
+				0, y, 0, 0,
+				0, 0, z, 0,
+				0, 0, 0, 1
+
+			);
 
 			return this;
 
 		},
 
-		distanceToPoint: function ( point ) {
+		makeShear: function ( x, y, z ) {
 
-			return this.normal.dot( point ) + this.constant;
+			this.set(
 
-		},
+				1, y, z, 0,
+				x, 1, z, 0,
+				x, y, 1, 0,
+				0, 0, 0, 1
 
-		distanceToSphere: function ( sphere ) {
+			);
 
-			return this.distanceToPoint( sphere.center ) - sphere.radius;
+			return this;
 
 		},
 
-		projectPoint: function ( point, target ) {
+		compose: function ( position, quaternion, scale ) {
 
-			if ( target === undefined ) {
+			var te = this.elements;
 
-				console.warn( 'THREE.Plane: .projectPoint() target is now required' );
-				target = new Vector3();
+			var x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
+			var x2 = x + x,	y2 = y + y, z2 = z + z;
+			var xx = x * x2, xy = x * y2, xz = x * z2;
+			var yy = y * y2, yz = y * z2, zz = z * z2;
+			var wx = w * x2, wy = w * y2, wz = w * z2;
 
-			}
+			var sx = scale.x, sy = scale.y, sz = scale.z;
 
-			return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
+			te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
+			te[ 1 ] = ( xy + wz ) * sx;
+			te[ 2 ] = ( xz - wy ) * sx;
+			te[ 3 ] = 0;
 
-		},
+			te[ 4 ] = ( xy - wz ) * sy;
+			te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
+			te[ 6 ] = ( yz + wx ) * sy;
+			te[ 7 ] = 0;
 
-		intersectLine: function ( line, target ) {
+			te[ 8 ] = ( xz + wy ) * sz;
+			te[ 9 ] = ( yz - wx ) * sz;
+			te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
+			te[ 11 ] = 0;
 
-			if ( target === undefined ) {
+			te[ 12 ] = position.x;
+			te[ 13 ] = position.y;
+			te[ 14 ] = position.z;
+			te[ 15 ] = 1;
 
-				console.warn( 'THREE.Plane: .intersectLine() target is now required' );
-				target = new Vector3();
+			return this;
 
-			}
+		},
 
-			var direction = line.delta( _vector1 );
+		decompose: function ( position, quaternion, scale ) {
 
-			var denominator = this.normal.dot( direction );
+			var te = this.elements;
 
-			if ( denominator === 0 ) {
+			var sx = _v1.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
+			var sy = _v1.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
+			var sz = _v1.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
 
-				// line is coplanar, return origin
-				if ( this.distanceToPoint( line.start ) === 0 ) {
+			// if determine is negative, we need to invert one scale
+			var det = this.determinant();
+			if ( det < 0 ) { sx = - sx; }
 
-					return target.copy( line.start );
+			position.x = te[ 12 ];
+			position.y = te[ 13 ];
+			position.z = te[ 14 ];
 
-				}
+			// scale the rotation part
+			_m1.copy( this );
 
-				// Unsure if this is the correct method to handle this case.
-				return undefined;
+			var invSX = 1 / sx;
+			var invSY = 1 / sy;
+			var invSZ = 1 / sz;
 
-			}
+			_m1.elements[ 0 ] *= invSX;
+			_m1.elements[ 1 ] *= invSX;
+			_m1.elements[ 2 ] *= invSX;
 
-			var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
+			_m1.elements[ 4 ] *= invSY;
+			_m1.elements[ 5 ] *= invSY;
+			_m1.elements[ 6 ] *= invSY;
 
-			if ( t < 0 || t > 1 ) {
+			_m1.elements[ 8 ] *= invSZ;
+			_m1.elements[ 9 ] *= invSZ;
+			_m1.elements[ 10 ] *= invSZ;
 
-				return undefined;
+			quaternion.setFromRotationMatrix( _m1 );
 
-			}
+			scale.x = sx;
+			scale.y = sy;
+			scale.z = sz;
 
-			return target.copy( direction ).multiplyScalar( t ).add( line.start );
+			return this;
 
 		},
 
-		intersectsLine: function ( line ) {
+		makePerspective: function ( left, right, top, bottom, near, far ) {
 
-			// Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
+			if ( far === undefined ) {
 
-			var startSign = this.distanceToPoint( line.start );
-			var endSign = this.distanceToPoint( line.end );
+				console.warn( 'THREE.Matrix4: .makePerspective() has been redefined and has a new signature. Please check the docs.' );
 
-			return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
+			}
 
-		},
+			var te = this.elements;
+			var x = 2 * near / ( right - left );
+			var y = 2 * near / ( top - bottom );
 
-		intersectsBox: function ( box ) {
+			var a = ( right + left ) / ( right - left );
+			var b = ( top + bottom ) / ( top - bottom );
+			var c = - ( far + near ) / ( far - near );
+			var d = - 2 * far * near / ( far - near );
 
-			return box.intersectsPlane( this );
+			te[ 0 ] = x;	te[ 4 ] = 0;	te[ 8 ] = a;	te[ 12 ] = 0;
+			te[ 1 ] = 0;	te[ 5 ] = y;	te[ 9 ] = b;	te[ 13 ] = 0;
+			te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = c;	te[ 14 ] = d;
+			te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = - 1;	te[ 15 ] = 0;
+
+			return this;
 
 		},
 
-		intersectsSphere: function ( sphere ) {
+		makeOrthographic: function ( left, right, top, bottom, near, far ) {
 
-			return sphere.intersectsPlane( this );
+			var te = this.elements;
+			var w = 1.0 / ( right - left );
+			var h = 1.0 / ( top - bottom );
+			var p = 1.0 / ( far - near );
+
+			var x = ( right + left ) * w;
+			var y = ( top + bottom ) * h;
+			var z = ( far + near ) * p;
+
+			te[ 0 ] = 2 * w;	te[ 4 ] = 0;	te[ 8 ] = 0;	te[ 12 ] = - x;
+			te[ 1 ] = 0;	te[ 5 ] = 2 * h;	te[ 9 ] = 0;	te[ 13 ] = - y;
+			te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = - 2 * p;	te[ 14 ] = - z;
+			te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = 0;	te[ 15 ] = 1;
+
+			return this;
 
 		},
 
-		coplanarPoint: function ( target ) {
+		equals: function ( matrix ) {
 
-			if ( target === undefined ) {
+			var te = this.elements;
+			var me = matrix.elements;
 
-				console.warn( 'THREE.Plane: .coplanarPoint() target is now required' );
-				target = new Vector3();
+			for ( var i = 0; i < 16; i ++ ) {
+
+				if ( te[ i ] !== me[ i ] ) { return false; }
 
 			}
 
-			return target.copy( this.normal ).multiplyScalar( - this.constant );
+			return true;
 
 		},
 
-		applyMatrix4: function ( matrix, optionalNormalMatrix ) {
+		fromArray: function ( array, offset ) {
 
-			var normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
+			if ( offset === undefined ) { offset = 0; }
 
-			var referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
+			for ( var i = 0; i < 16; i ++ ) {
 
-			var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
+				this.elements[ i ] = array[ i + offset ];
 
-			this.constant = - referencePoint.dot( normal );
+			}
 
 			return this;
 
 		},
 
-		translate: function ( offset ) {
+		toArray: function ( array, offset ) {
 
-			this.constant -= offset.dot( this.normal );
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			return this;
+			var te = this.elements;
 
-		},
+			array[ offset ] = te[ 0 ];
+			array[ offset + 1 ] = te[ 1 ];
+			array[ offset + 2 ] = te[ 2 ];
+			array[ offset + 3 ] = te[ 3 ];
 
-		equals: function ( plane ) {
+			array[ offset + 4 ] = te[ 4 ];
+			array[ offset + 5 ] = te[ 5 ];
+			array[ offset + 6 ] = te[ 6 ];
+			array[ offset + 7 ] = te[ 7 ];
 
-			return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
+			array[ offset + 8 ] = te[ 8 ];
+			array[ offset + 9 ] = te[ 9 ];
+			array[ offset + 10 ] = te[ 10 ];
+			array[ offset + 11 ] = te[ 11 ];
+
+			array[ offset + 12 ] = te[ 12 ];
+			array[ offset + 13 ] = te[ 13 ];
+			array[ offset + 14 ] = te[ 14 ];
+			array[ offset + 15 ] = te[ 15 ];
+
+			return array;
 
 		}
 
 	} );
 
 	/**
-	 * @author bhouston / http://clara.io
 	 * @author mrdoob / http://mrdoob.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author bhouston / http://clara.io
 	 */
 
-	var _v0$1 = new Vector3();
-	var _v1$3 = new Vector3();
-	var _v2$1 = new Vector3();
-	var _v3 = new Vector3();
-
-	var _vab = new Vector3();
-	var _vac = new Vector3();
-	var _vbc = new Vector3();
-	var _vap = new Vector3();
-	var _vbp = new Vector3();
-	var _vcp = new Vector3();
+	var _matrix = new Matrix4();
+	var _quaternion$1 = new Quaternion();
 
-	function Triangle( a, b, c ) {
+	function Euler( x, y, z, order ) {
 
-		this.a = ( a !== undefined ) ? a : new Vector3();
-		this.b = ( b !== undefined ) ? b : new Vector3();
-		this.c = ( c !== undefined ) ? c : new Vector3();
+		this._x = x || 0;
+		this._y = y || 0;
+		this._z = z || 0;
+		this._order = order || Euler.DefaultOrder;
 
 	}
 
-	Object.assign( Triangle, {
+	Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
 
-		getNormal: function ( a, b, c, target ) {
+	Euler.DefaultOrder = 'XYZ';
 
-			if ( target === undefined ) {
+	Object.defineProperties( Euler.prototype, {
 
-				console.warn( 'THREE.Triangle: .getNormal() target is now required' );
-				target = new Vector3();
+		x: {
 
-			}
+			get: function () {
 
-			target.subVectors( c, b );
-			_v0$1.subVectors( a, b );
-			target.cross( _v0$1 );
+				return this._x;
 
-			var targetLengthSq = target.lengthSq();
-			if ( targetLengthSq > 0 ) {
+			},
 
-				return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
+			set: function ( value ) {
 
-			}
+				this._x = value;
+				this._onChangeCallback();
 
-			return target.set( 0, 0, 0 );
+			}
 
 		},
 
-		// static/instance method to calculate barycentric coordinates
-		// based on: http://www.blackpawn.com/texts/pointinpoly/default.html
-		getBarycoord: function ( point, a, b, c, target ) {
+		y: {
 
-			_v0$1.subVectors( c, a );
-			_v1$3.subVectors( b, a );
-			_v2$1.subVectors( point, a );
+			get: function () {
 
-			var dot00 = _v0$1.dot( _v0$1 );
-			var dot01 = _v0$1.dot( _v1$3 );
-			var dot02 = _v0$1.dot( _v2$1 );
-			var dot11 = _v1$3.dot( _v1$3 );
-			var dot12 = _v1$3.dot( _v2$1 );
+				return this._y;
 
-			var denom = ( dot00 * dot11 - dot01 * dot01 );
+			},
 
-			if ( target === undefined ) {
+			set: function ( value ) {
 
-				console.warn( 'THREE.Triangle: .getBarycoord() target is now required' );
-				target = new Vector3();
+				this._y = value;
+				this._onChangeCallback();
 
 			}
 
-			// collinear or singular triangle
-			if ( denom === 0 ) {
-
-				// arbitrary location outside of triangle?
-				// not sure if this is the best idea, maybe should be returning undefined
-				return target.set( - 2, - 1, - 1 );
+		},
 
-			}
+		z: {
 
-			var invDenom = 1 / denom;
-			var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
-			var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
+			get: function () {
 
-			// barycentric coordinates must always sum to 1
-			return target.set( 1 - u - v, v, u );
+				return this._z;
 
-		},
+			},
 
-		containsPoint: function ( point, a, b, c ) {
+			set: function ( value ) {
 
-			Triangle.getBarycoord( point, a, b, c, _v3 );
+				this._z = value;
+				this._onChangeCallback();
 
-			return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 );
+			}
 
 		},
 
-		getUV: function ( point, p1, p2, p3, uv1, uv2, uv3, target ) {
-
-			this.getBarycoord( point, p1, p2, p3, _v3 );
+		order: {
 
-			target.set( 0, 0 );
-			target.addScaledVector( uv1, _v3.x );
-			target.addScaledVector( uv2, _v3.y );
-			target.addScaledVector( uv3, _v3.z );
+			get: function () {
 
-			return target;
+				return this._order;
 
-		},
+			},
 
-		isFrontFacing: function ( a, b, c, direction ) {
+			set: function ( value ) {
 
-			_v0$1.subVectors( c, b );
-			_v1$3.subVectors( a, b );
+				this._order = value;
+				this._onChangeCallback();
 
-			// strictly front facing
-			return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false;
+			}
 
 		}
 
 	} );
 
-	Object.assign( Triangle.prototype, {
-
-		set: function ( a, b, c ) {
-
-			this.a.copy( a );
-			this.b.copy( b );
-			this.c.copy( c );
+	Object.assign( Euler.prototype, {
 
-			return this;
+		isEuler: true,
 
-		},
+		set: function ( x, y, z, order ) {
 
-		setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
+			this._x = x;
+			this._y = y;
+			this._z = z;
+			this._order = order || this._order;
 
-			this.a.copy( points[ i0 ] );
-			this.b.copy( points[ i1 ] );
-			this.c.copy( points[ i2 ] );
+			this._onChangeCallback();
 
 			return this;
 
@@ -16287,1270 +23995,1147 @@ function simpleEnd(buf) {
 
 		clone: function () {
 
-			return new this.constructor().copy( this );
+			return new this.constructor( this._x, this._y, this._z, this._order );
 
 		},
 
-		copy: function ( triangle ) {
+		copy: function ( euler ) {
 
-			this.a.copy( triangle.a );
-			this.b.copy( triangle.b );
-			this.c.copy( triangle.c );
+			this._x = euler._x;
+			this._y = euler._y;
+			this._z = euler._z;
+			this._order = euler._order;
+
+			this._onChangeCallback();
 
 			return this;
 
 		},
 
-		getArea: function () {
-
-			_v0$1.subVectors( this.c, this.b );
-			_v1$3.subVectors( this.a, this.b );
+		setFromRotationMatrix: function ( m, order, update ) {
 
-			return _v0$1.cross( _v1$3 ).length() * 0.5;
+			var clamp = MathUtils.clamp;
 
-		},
+			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
 
-		getMidpoint: function ( target ) {
+			var te = m.elements;
+			var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
+			var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
+			var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
 
-			if ( target === undefined ) {
+			order = order || this._order;
 
-				console.warn( 'THREE.Triangle: .getMidpoint() target is now required' );
-				target = new Vector3();
+			switch ( order ) {
 
-			}
+				case 'XYZ':
 
-			return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
+					this._y = Math.asin( clamp( m13, - 1, 1 ) );
 
-		},
+					if ( Math.abs( m13 ) < 0.9999999 ) {
 
-		getNormal: function ( target ) {
+						this._x = Math.atan2( - m23, m33 );
+						this._z = Math.atan2( - m12, m11 );
 
-			return Triangle.getNormal( this.a, this.b, this.c, target );
+					} else {
 
-		},
+						this._x = Math.atan2( m32, m22 );
+						this._z = 0;
 
-		getPlane: function ( target ) {
+					}
 
-			if ( target === undefined ) {
+					break;
 
-				console.warn( 'THREE.Triangle: .getPlane() target is now required' );
-				target = new Plane();
+				case 'YXZ':
 
-			}
+					this._x = Math.asin( - clamp( m23, - 1, 1 ) );
 
-			return target.setFromCoplanarPoints( this.a, this.b, this.c );
+					if ( Math.abs( m23 ) < 0.9999999 ) {
 
-		},
+						this._y = Math.atan2( m13, m33 );
+						this._z = Math.atan2( m21, m22 );
 
-		getBarycoord: function ( point, target ) {
+					} else {
 
-			return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
+						this._y = Math.atan2( - m31, m11 );
+						this._z = 0;
 
-		},
+					}
 
-		getUV: function ( point, uv1, uv2, uv3, target ) {
+					break;
 
-			return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, target );
+				case 'ZXY':
 
-		},
+					this._x = Math.asin( clamp( m32, - 1, 1 ) );
 
-		containsPoint: function ( point ) {
+					if ( Math.abs( m32 ) < 0.9999999 ) {
 
-			return Triangle.containsPoint( point, this.a, this.b, this.c );
+						this._y = Math.atan2( - m31, m33 );
+						this._z = Math.atan2( - m12, m22 );
 
-		},
+					} else {
 
-		isFrontFacing: function ( direction ) {
+						this._y = 0;
+						this._z = Math.atan2( m21, m11 );
 
-			return Triangle.isFrontFacing( this.a, this.b, this.c, direction );
+					}
 
-		},
+					break;
 
-		intersectsBox: function ( box ) {
+				case 'ZYX':
 
-			return box.intersectsTriangle( this );
+					this._y = Math.asin( - clamp( m31, - 1, 1 ) );
 
-		},
+					if ( Math.abs( m31 ) < 0.9999999 ) {
 
-		closestPointToPoint: function ( p, target ) {
+						this._x = Math.atan2( m32, m33 );
+						this._z = Math.atan2( m21, m11 );
 
-			if ( target === undefined ) {
+					} else {
 
-				console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
-				target = new Vector3();
+						this._x = 0;
+						this._z = Math.atan2( - m12, m22 );
 
-			}
+					}
 
-			var a = this.a, b = this.b, c = this.c;
-			var v, w;
+					break;
 
-			// algorithm thanks to Real-Time Collision Detection by Christer Ericson,
-			// published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
-			// under the accompanying license; see chapter 5.1.5 for detailed explanation.
-			// basically, we're distinguishing which of the voronoi regions of the triangle
-			// the point lies in with the minimum amount of redundant computation.
+				case 'YZX':
 
-			_vab.subVectors( b, a );
-			_vac.subVectors( c, a );
-			_vap.subVectors( p, a );
-			var d1 = _vab.dot( _vap );
-			var d2 = _vac.dot( _vap );
-			if ( d1 <= 0 && d2 <= 0 ) {
+					this._z = Math.asin( clamp( m21, - 1, 1 ) );
 
-				// vertex region of A; barycentric coords (1, 0, 0)
-				return target.copy( a );
+					if ( Math.abs( m21 ) < 0.9999999 ) {
 
-			}
+						this._x = Math.atan2( - m23, m22 );
+						this._y = Math.atan2( - m31, m11 );
 
-			_vbp.subVectors( p, b );
-			var d3 = _vab.dot( _vbp );
-			var d4 = _vac.dot( _vbp );
-			if ( d3 >= 0 && d4 <= d3 ) {
+					} else {
 
-				// vertex region of B; barycentric coords (0, 1, 0)
-				return target.copy( b );
+						this._x = 0;
+						this._y = Math.atan2( m13, m33 );
 
-			}
+					}
 
-			var vc = d1 * d4 - d3 * d2;
-			if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
+					break;
 
-				v = d1 / ( d1 - d3 );
-				// edge region of AB; barycentric coords (1-v, v, 0)
-				return target.copy( a ).addScaledVector( _vab, v );
+				case 'XZY':
 
-			}
+					this._z = Math.asin( - clamp( m12, - 1, 1 ) );
 
-			_vcp.subVectors( p, c );
-			var d5 = _vab.dot( _vcp );
-			var d6 = _vac.dot( _vcp );
-			if ( d6 >= 0 && d5 <= d6 ) {
+					if ( Math.abs( m12 ) < 0.9999999 ) {
 
-				// vertex region of C; barycentric coords (0, 0, 1)
-				return target.copy( c );
+						this._x = Math.atan2( m32, m22 );
+						this._y = Math.atan2( m13, m11 );
 
-			}
+					} else {
 
-			var vb = d5 * d2 - d1 * d6;
-			if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
+						this._x = Math.atan2( - m23, m33 );
+						this._y = 0;
 
-				w = d2 / ( d2 - d6 );
-				// edge region of AC; barycentric coords (1-w, 0, w)
-				return target.copy( a ).addScaledVector( _vac, w );
+					}
 
-			}
+					break;
 
-			var va = d3 * d6 - d5 * d4;
-			if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
+				default:
 
-				_vbc.subVectors( c, b );
-				w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
-				// edge region of BC; barycentric coords (0, 1-w, w)
-				return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC
+					console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
 
 			}
 
-			// face region
-			var denom = 1 / ( va + vb + vc );
-			// u = va * denom
-			v = vb * denom;
-			w = vc * denom;
-
-			return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w );
+			this._order = order;
 
-		},
+			if ( update !== false ) { this._onChangeCallback(); }
 
-		equals: function ( triangle ) {
+			return this;
 
-			return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
+		},
 
-		}
+		setFromQuaternion: function ( q, order, update ) {
 
-	} );
+			_matrix.makeRotationFromQuaternion( q );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			return this.setFromRotationMatrix( _matrix, order, update );
 
-	var _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
-		'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
-		'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
-		'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
-		'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
-		'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
-		'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
-		'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
-		'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
-		'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
-		'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
-		'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
-		'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
-		'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
-		'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
-		'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
-		'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
-		'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
-		'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
-		'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
-		'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
-		'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
-		'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
-		'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
+		},
 
-	var _hslA = { h: 0, s: 0, l: 0 };
-	var _hslB = { h: 0, s: 0, l: 0 };
+		setFromVector3: function ( v, order ) {
 
-	function Color( r, g, b ) {
+			return this.set( v.x, v.y, v.z, order || this._order );
 
-		if ( g === undefined && b === undefined ) {
+		},
 
-			// r is THREE.Color, hex or string
-			return this.set( r );
+		reorder: function ( newOrder ) {
 
-		}
+			// WARNING: this discards revolution information -bhouston
 
-		return this.setRGB( r, g, b );
+			_quaternion$1.setFromEuler( this );
 
-	}
+			return this.setFromQuaternion( _quaternion$1, newOrder );
 
-	function hue2rgb( p, q, t ) {
+		},
 
-		if ( t < 0 ) { t += 1; }
-		if ( t > 1 ) { t -= 1; }
-		if ( t < 1 / 6 ) { return p + ( q - p ) * 6 * t; }
-		if ( t < 1 / 2 ) { return q; }
-		if ( t < 2 / 3 ) { return p + ( q - p ) * 6 * ( 2 / 3 - t ); }
-		return p;
+		equals: function ( euler ) {
 
-	}
+			return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
 
-	function SRGBToLinear( c ) {
+		},
 
-		return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
+		fromArray: function ( array ) {
 
-	}
+			this._x = array[ 0 ];
+			this._y = array[ 1 ];
+			this._z = array[ 2 ];
+			if ( array[ 3 ] !== undefined ) { this._order = array[ 3 ]; }
 
-	function LinearToSRGB( c ) {
+			this._onChangeCallback();
 
-		return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
+			return this;
 
-	}
+		},
 
-	Object.assign( Color.prototype, {
+		toArray: function ( array, offset ) {
 
-		isColor: true,
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-		r: 1, g: 1, b: 1,
+			array[ offset ] = this._x;
+			array[ offset + 1 ] = this._y;
+			array[ offset + 2 ] = this._z;
+			array[ offset + 3 ] = this._order;
 
-		set: function ( value ) {
+			return array;
 
-			if ( value && value.isColor ) {
+		},
 
-				this.copy( value );
+		toVector3: function ( optionalResult ) {
 
-			} else if ( typeof value === 'number' ) {
+			if ( optionalResult ) {
 
-				this.setHex( value );
+				return optionalResult.set( this._x, this._y, this._z );
 
-			} else if ( typeof value === 'string' ) {
+			} else {
 
-				this.setStyle( value );
+				return new Vector3( this._x, this._y, this._z );
 
 			}
 
-			return this;
-
 		},
 
-		setScalar: function ( scalar ) {
+		_onChange: function ( callback ) {
 
-			this.r = scalar;
-			this.g = scalar;
-			this.b = scalar;
+			this._onChangeCallback = callback;
 
 			return this;
 
 		},
 
-		setHex: function ( hex ) {
+		_onChangeCallback: function () {}
 
-			hex = Math.floor( hex );
+	} );
 
-			this.r = ( hex >> 16 & 255 ) / 255;
-			this.g = ( hex >> 8 & 255 ) / 255;
-			this.b = ( hex & 255 ) / 255;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			return this;
+	function Layers() {
 
-		},
+		this.mask = 1 | 0;
 
-		setRGB: function ( r, g, b ) {
+	}
 
-			this.r = r;
-			this.g = g;
-			this.b = b;
+	Object.assign( Layers.prototype, {
 
-			return this;
+		set: function ( channel ) {
 
-		},
+			this.mask = 1 << channel | 0;
 
-		setHSL: function ( h, s, l ) {
+		},
 
-			// h,s,l ranges are in 0.0 - 1.0
-			h = MathUtils.euclideanModulo( h, 1 );
-			s = MathUtils.clamp( s, 0, 1 );
-			l = MathUtils.clamp( l, 0, 1 );
+		enable: function ( channel ) {
 
-			if ( s === 0 ) {
+			this.mask |= 1 << channel | 0;
 
-				this.r = this.g = this.b = l;
+		},
 
-			} else {
+		enableAll: function () {
 
-				var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
-				var q = ( 2 * l ) - p;
+			this.mask = 0xffffffff | 0;
 
-				this.r = hue2rgb( q, p, h + 1 / 3 );
-				this.g = hue2rgb( q, p, h );
-				this.b = hue2rgb( q, p, h - 1 / 3 );
+		},
 
-			}
+		toggle: function ( channel ) {
 
-			return this;
+			this.mask ^= 1 << channel | 0;
 
 		},
 
-		setStyle: function ( style ) {
-
-			function handleAlpha( string ) {
-
-				if ( string === undefined ) { return; }
+		disable: function ( channel ) {
 
-				if ( parseFloat( string ) < 1 ) {
+			this.mask &= ~ ( 1 << channel | 0 );
 
-					console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' );
+		},
 
-				}
+		disableAll: function () {
 
-			}
+			this.mask = 0;
 
+		},
 
-			var m;
+		test: function ( layers ) {
 
-			if ( m = /^((?:rgb|hsl)a?)\(\s*([^\)]*)\)/.exec( style ) ) {
+			return ( this.mask & layers.mask ) !== 0;
 
-				// rgb / hsl
+		}
 
-				var color;
-				var name = m[ 1 ];
-				var components = m[ 2 ];
+	} );
 
-				switch ( name ) {
+	var _object3DId = 0;
 
-					case 'rgb':
-					case 'rgba':
+	var _v1$1 = new Vector3();
+	var _q1 = new Quaternion();
+	var _m1$1 = new Matrix4();
+	var _target = new Vector3();
 
-						if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
+	var _position = new Vector3();
+	var _scale = new Vector3();
+	var _quaternion$2 = new Quaternion();
 
-							// rgb(255,0,0) rgba(255,0,0,0.5)
-							this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
-							this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
-							this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
+	var _xAxis = new Vector3( 1, 0, 0 );
+	var _yAxis = new Vector3( 0, 1, 0 );
+	var _zAxis = new Vector3( 0, 0, 1 );
 
-							handleAlpha( color[ 5 ] );
+	var _addedEvent = { type: 'added' };
+	var _removedEvent = { type: 'removed' };
 
-							return this;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author elephantatwork / www.elephantatwork.ch
+	 */
 
-						}
+	function Object3D() {
 
-						if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
+		Object.defineProperty( this, 'id', { value: _object3DId ++ } );
 
-							// rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
-							this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
-							this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
-							this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
+		this.uuid = MathUtils.generateUUID();
 
-							handleAlpha( color[ 5 ] );
+		this.name = '';
+		this.type = 'Object3D';
 
-							return this;
+		this.parent = null;
+		this.children = [];
 
-						}
+		this.up = Object3D.DefaultUp.clone();
 
-						break;
+		var position = new Vector3();
+		var rotation = new Euler();
+		var quaternion = new Quaternion();
+		var scale = new Vector3( 1, 1, 1 );
 
-					case 'hsl':
-					case 'hsla':
+		function onRotationChange() {
 
-						if ( color = /^([0-9]*\.?[0-9]+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
+			quaternion.setFromEuler( rotation, false );
 
-							// hsl(120,50%,50%) hsla(120,50%,50%,0.5)
-							var h = parseFloat( color[ 1 ] ) / 360;
-							var s = parseInt( color[ 2 ], 10 ) / 100;
-							var l = parseInt( color[ 3 ], 10 ) / 100;
+		}
 
-							handleAlpha( color[ 5 ] );
+		function onQuaternionChange() {
 
-							return this.setHSL( h, s, l );
+			rotation.setFromQuaternion( quaternion, undefined, false );
 
-						}
+		}
 
-						break;
+		rotation._onChange( onRotationChange );
+		quaternion._onChange( onQuaternionChange );
 
-				}
+		Object.defineProperties( this, {
+			position: {
+				configurable: true,
+				enumerable: true,
+				value: position
+			},
+			rotation: {
+				configurable: true,
+				enumerable: true,
+				value: rotation
+			},
+			quaternion: {
+				configurable: true,
+				enumerable: true,
+				value: quaternion
+			},
+			scale: {
+				configurable: true,
+				enumerable: true,
+				value: scale
+			},
+			modelViewMatrix: {
+				value: new Matrix4()
+			},
+			normalMatrix: {
+				value: new Matrix3()
+			}
+		} );
 
-			} else if ( m = /^\#([A-Fa-f0-9]+)$/.exec( style ) ) {
+		this.matrix = new Matrix4();
+		this.matrixWorld = new Matrix4();
 
-				// hex color
+		this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
+		this.matrixWorldNeedsUpdate = false;
 
-				var hex = m[ 1 ];
-				var size = hex.length;
+		this.layers = new Layers();
+		this.visible = true;
 
-				if ( size === 3 ) {
+		this.castShadow = false;
+		this.receiveShadow = false;
 
-					// #ff0
-					this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 0 ), 16 ) / 255;
-					this.g = parseInt( hex.charAt( 1 ) + hex.charAt( 1 ), 16 ) / 255;
-					this.b = parseInt( hex.charAt( 2 ) + hex.charAt( 2 ), 16 ) / 255;
+		this.frustumCulled = true;
+		this.renderOrder = 0;
 
-					return this;
+		this.userData = {};
 
-				} else if ( size === 6 ) {
+	}
 
-					// #ff0000
-					this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 1 ), 16 ) / 255;
-					this.g = parseInt( hex.charAt( 2 ) + hex.charAt( 3 ), 16 ) / 255;
-					this.b = parseInt( hex.charAt( 4 ) + hex.charAt( 5 ), 16 ) / 255;
+	Object3D.DefaultUp = new Vector3( 0, 1, 0 );
+	Object3D.DefaultMatrixAutoUpdate = true;
 
-					return this;
+	Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-				}
+		constructor: Object3D,
 
-			}
+		isObject3D: true,
 
-			if ( style && style.length > 0 ) {
+		onBeforeRender: function () {},
+		onAfterRender: function () {},
 
-				return this.setColorName( style );
+		applyMatrix4: function ( matrix ) {
 
-			}
+			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
 
-			return this;
+			this.matrix.premultiply( matrix );
 
-		},
+			this.matrix.decompose( this.position, this.quaternion, this.scale );
 
-		setColorName: function ( style ) {
+		},
 
-			// color keywords
-			var hex = _colorKeywords[ style ];
+		applyQuaternion: function ( q ) {
 
-			if ( hex !== undefined ) {
+			this.quaternion.premultiply( q );
 
-				// red
-				this.setHex( hex );
+			return this;
 
-			} else {
+		},
 
-				// unknown color
-				console.warn( 'THREE.Color: Unknown color ' + style );
+		setRotationFromAxisAngle: function ( axis, angle ) {
 
-			}
+			// assumes axis is normalized
 
-			return this;
+			this.quaternion.setFromAxisAngle( axis, angle );
 
 		},
 
-		clone: function () {
+		setRotationFromEuler: function ( euler ) {
 
-			return new this.constructor( this.r, this.g, this.b );
+			this.quaternion.setFromEuler( euler, true );
 
 		},
 
-		copy: function ( color ) {
+		setRotationFromMatrix: function ( m ) {
 
-			this.r = color.r;
-			this.g = color.g;
-			this.b = color.b;
+			// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
 
-			return this;
+			this.quaternion.setFromRotationMatrix( m );
 
 		},
 
-		copyGammaToLinear: function ( color, gammaFactor ) {
-
-			if ( gammaFactor === undefined ) { gammaFactor = 2.0; }
+		setRotationFromQuaternion: function ( q ) {
 
-			this.r = Math.pow( color.r, gammaFactor );
-			this.g = Math.pow( color.g, gammaFactor );
-			this.b = Math.pow( color.b, gammaFactor );
+			// assumes q is normalized
 
-			return this;
+			this.quaternion.copy( q );
 
 		},
 
-		copyLinearToGamma: function ( color, gammaFactor ) {
+		rotateOnAxis: function ( axis, angle ) {
 
-			if ( gammaFactor === undefined ) { gammaFactor = 2.0; }
+			// rotate object on axis in object space
+			// axis is assumed to be normalized
 
-			var safeInverse = ( gammaFactor > 0 ) ? ( 1.0 / gammaFactor ) : 1.0;
+			_q1.setFromAxisAngle( axis, angle );
 
-			this.r = Math.pow( color.r, safeInverse );
-			this.g = Math.pow( color.g, safeInverse );
-			this.b = Math.pow( color.b, safeInverse );
+			this.quaternion.multiply( _q1 );
 
 			return this;
 
 		},
 
-		convertGammaToLinear: function ( gammaFactor ) {
-
-			this.copyGammaToLinear( this, gammaFactor );
-
-			return this;
+		rotateOnWorldAxis: function ( axis, angle ) {
 
-		},
+			// rotate object on axis in world space
+			// axis is assumed to be normalized
+			// method assumes no rotated parent
 
-		convertLinearToGamma: function ( gammaFactor ) {
+			_q1.setFromAxisAngle( axis, angle );
 
-			this.copyLinearToGamma( this, gammaFactor );
+			this.quaternion.premultiply( _q1 );
 
 			return this;
 
 		},
 
-		copySRGBToLinear: function ( color ) {
-
-			this.r = SRGBToLinear( color.r );
-			this.g = SRGBToLinear( color.g );
-			this.b = SRGBToLinear( color.b );
+		rotateX: function ( angle ) {
 
-			return this;
+			return this.rotateOnAxis( _xAxis, angle );
 
 		},
 
-		copyLinearToSRGB: function ( color ) {
-
-			this.r = LinearToSRGB( color.r );
-			this.g = LinearToSRGB( color.g );
-			this.b = LinearToSRGB( color.b );
+		rotateY: function ( angle ) {
 
-			return this;
+			return this.rotateOnAxis( _yAxis, angle );
 
 		},
 
-		convertSRGBToLinear: function () {
-
-			this.copySRGBToLinear( this );
+		rotateZ: function ( angle ) {
 
-			return this;
+			return this.rotateOnAxis( _zAxis, angle );
 
 		},
 
-		convertLinearToSRGB: function () {
+		translateOnAxis: function ( axis, distance ) {
 
-			this.copyLinearToSRGB( this );
+			// translate object by distance along axis in object space
+			// axis is assumed to be normalized
+
+			_v1$1.copy( axis ).applyQuaternion( this.quaternion );
+
+			this.position.add( _v1$1.multiplyScalar( distance ) );
 
 			return this;
 
 		},
 
-		getHex: function () {
+		translateX: function ( distance ) {
 
-			return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;
+			return this.translateOnAxis( _xAxis, distance );
 
 		},
 
-		getHexString: function () {
+		translateY: function ( distance ) {
 
-			return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
+			return this.translateOnAxis( _yAxis, distance );
 
 		},
 
-		getHSL: function ( target ) {
-
-			// h,s,l ranges are in 0.0 - 1.0
-
-			if ( target === undefined ) {
+		translateZ: function ( distance ) {
 
-				console.warn( 'THREE.Color: .getHSL() target is now required' );
-				target = { h: 0, s: 0, l: 0 };
+			return this.translateOnAxis( _zAxis, distance );
 
-			}
+		},
 
-			var r = this.r, g = this.g, b = this.b;
+		localToWorld: function ( vector ) {
 
-			var max = Math.max( r, g, b );
-			var min = Math.min( r, g, b );
+			return vector.applyMatrix4( this.matrixWorld );
 
-			var hue, saturation;
-			var lightness = ( min + max ) / 2.0;
+		},
 
-			if ( min === max ) {
+		worldToLocal: function ( vector ) {
 
-				hue = 0;
-				saturation = 0;
+			return vector.applyMatrix4( _m1$1.getInverse( this.matrixWorld ) );
 
-			} else {
+		},
 
-				var delta = max - min;
+		lookAt: function ( x, y, z ) {
 
-				saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
+			// This method does not support objects having non-uniformly-scaled parent(s)
 
-				switch ( max ) {
+			if ( x.isVector3 ) {
 
-					case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
-					case g: hue = ( b - r ) / delta + 2; break;
-					case b: hue = ( r - g ) / delta + 4; break;
+				_target.copy( x );
 
-				}
+			} else {
 
-				hue /= 6;
+				_target.set( x, y, z );
 
 			}
 
-			target.h = hue;
-			target.s = saturation;
-			target.l = lightness;
+			var parent = this.parent;
 
-			return target;
+			this.updateWorldMatrix( true, false );
 
-		},
+			_position.setFromMatrixPosition( this.matrixWorld );
 
-		getStyle: function () {
+			if ( this.isCamera || this.isLight ) {
 
-			return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')';
+				_m1$1.lookAt( _position, _target, this.up );
 
-		},
+			} else {
 
-		offsetHSL: function ( h, s, l ) {
+				_m1$1.lookAt( _target, _position, this.up );
 
-			this.getHSL( _hslA );
+			}
 
-			_hslA.h += h; _hslA.s += s; _hslA.l += l;
+			this.quaternion.setFromRotationMatrix( _m1$1 );
 
-			this.setHSL( _hslA.h, _hslA.s, _hslA.l );
+			if ( parent ) {
 
-			return this;
+				_m1$1.extractRotation( parent.matrixWorld );
+				_q1.setFromRotationMatrix( _m1$1 );
+				this.quaternion.premultiply( _q1.inverse() );
+
+			}
 
 		},
 
-		add: function ( color ) {
+		add: function ( object ) {
 
-			this.r += color.r;
-			this.g += color.g;
-			this.b += color.b;
+			if ( arguments.length > 1 ) {
 
-			return this;
+				for ( var i = 0; i < arguments.length; i ++ ) {
 
-		},
+					this.add( arguments[ i ] );
 
-		addColors: function ( color1, color2 ) {
+				}
 
-			this.r = color1.r + color2.r;
-			this.g = color1.g + color2.g;
-			this.b = color1.b + color2.b;
+				return this;
 
-			return this;
+			}
 
-		},
+			if ( object === this ) {
 
-		addScalar: function ( s ) {
+				console.error( "THREE.Object3D.add: object can't be added as a child of itself.", object );
+				return this;
 
-			this.r += s;
-			this.g += s;
-			this.b += s;
+			}
 
-			return this;
+			if ( ( object && object.isObject3D ) ) {
 
-		},
+				if ( object.parent !== null ) {
 
-		sub: function ( color ) {
+					object.parent.remove( object );
 
-			this.r = Math.max( 0, this.r - color.r );
-			this.g = Math.max( 0, this.g - color.g );
-			this.b = Math.max( 0, this.b - color.b );
+				}
 
-			return this;
+				object.parent = this;
+				this.children.push( object );
 
-		},
+				object.dispatchEvent( _addedEvent );
 
-		multiply: function ( color ) {
+			} else {
 
-			this.r *= color.r;
-			this.g *= color.g;
-			this.b *= color.b;
+				console.error( "THREE.Object3D.add: object not an instance of THREE.Object3D.", object );
+
+			}
 
 			return this;
 
 		},
 
-		multiplyScalar: function ( s ) {
+		remove: function ( object ) {
 
-			this.r *= s;
-			this.g *= s;
-			this.b *= s;
+			if ( arguments.length > 1 ) {
 
-			return this;
+				for ( var i = 0; i < arguments.length; i ++ ) {
 
-		},
+					this.remove( arguments[ i ] );
 
-		lerp: function ( color, alpha ) {
+				}
 
-			this.r += ( color.r - this.r ) * alpha;
-			this.g += ( color.g - this.g ) * alpha;
-			this.b += ( color.b - this.b ) * alpha;
+				return this;
 
-			return this;
+			}
 
-		},
+			var index = this.children.indexOf( object );
 
-		lerpHSL: function ( color, alpha ) {
+			if ( index !== - 1 ) {
 
-			this.getHSL( _hslA );
-			color.getHSL( _hslB );
+				object.parent = null;
+				this.children.splice( index, 1 );
 
-			var h = MathUtils.lerp( _hslA.h, _hslB.h, alpha );
-			var s = MathUtils.lerp( _hslA.s, _hslB.s, alpha );
-			var l = MathUtils.lerp( _hslA.l, _hslB.l, alpha );
+				object.dispatchEvent( _removedEvent );
 
-			this.setHSL( h, s, l );
+			}
 
 			return this;
 
 		},
 
-		equals: function ( c ) {
+		attach: function ( object ) {
 
-			return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
+			// adds object as a child of this, while maintaining the object's world transform
 
-		},
+			this.updateWorldMatrix( true, false );
 
-		fromArray: function ( array, offset ) {
+			_m1$1.getInverse( this.matrixWorld );
 
-			if ( offset === undefined ) { offset = 0; }
+			if ( object.parent !== null ) {
 
-			this.r = array[ offset ];
-			this.g = array[ offset + 1 ];
-			this.b = array[ offset + 2 ];
+				object.parent.updateWorldMatrix( true, false );
 
-			return this;
+				_m1$1.multiply( object.parent.matrixWorld );
 
-		},
+			}
 
-		toArray: function ( array, offset ) {
+			object.applyMatrix4( _m1$1 );
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+			object.updateWorldMatrix( false, false );
 
-			array[ offset ] = this.r;
-			array[ offset + 1 ] = this.g;
-			array[ offset + 2 ] = this.b;
+			this.add( object );
 
-			return array;
+			return this;
 
 		},
 
-		toJSON: function () {
+		getObjectById: function ( id ) {
 
-			return this.getHex();
+			return this.getObjectByProperty( 'id', id );
 
-		}
+		},
 
-	} );
+		getObjectByName: function ( name ) {
 
-	Color.NAMES = _colorKeywords;
+			return this.getObjectByProperty( 'name', name );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+		},
 
-	function Face3( a, b, c, normal, color, materialIndex ) {
+		getObjectByProperty: function ( name, value ) {
 
-		this.a = a;
-		this.b = b;
-		this.c = c;
+			if ( this[ name ] === value ) { return this; }
 
-		this.normal = ( normal && normal.isVector3 ) ? normal : new Vector3();
-		this.vertexNormals = Array.isArray( normal ) ? normal : [];
+			for ( var i = 0, l = this.children.length; i < l; i ++ ) {
 
-		this.color = ( color && color.isColor ) ? color : new Color();
-		this.vertexColors = Array.isArray( color ) ? color : [];
+				var child = this.children[ i ];
+				var object = child.getObjectByProperty( name, value );
 
-		this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
+				if ( object !== undefined ) {
 
-	}
+					return object;
 
-	Object.assign( Face3.prototype, {
+				}
 
-		clone: function () {
+			}
 
-			return new this.constructor().copy( this );
+			return undefined;
 
 		},
 
-		copy: function ( source ) {
+		getWorldPosition: function ( target ) {
 
-			this.a = source.a;
-			this.b = source.b;
-			this.c = source.c;
+			if ( target === undefined ) {
 
-			this.normal.copy( source.normal );
-			this.color.copy( source.color );
+				console.warn( 'THREE.Object3D: .getWorldPosition() target is now required' );
+				target = new Vector3();
 
-			this.materialIndex = source.materialIndex;
+			}
 
-			for ( var i = 0, il = source.vertexNormals.length; i < il; i ++ ) {
+			this.updateMatrixWorld( true );
 
-				this.vertexNormals[ i ] = source.vertexNormals[ i ].clone();
+			return target.setFromMatrixPosition( this.matrixWorld );
 
-			}
+		},
 
-			for ( var i = 0, il = source.vertexColors.length; i < il; i ++ ) {
+		getWorldQuaternion: function ( target ) {
 
-				this.vertexColors[ i ] = source.vertexColors[ i ].clone();
+			if ( target === undefined ) {
+
+				console.warn( 'THREE.Object3D: .getWorldQuaternion() target is now required' );
+				target = new Quaternion();
 
 			}
 
-			return this;
+			this.updateMatrixWorld( true );
 
-		}
+			this.matrixWorld.decompose( _position, target, _scale );
 
-	} );
+			return target;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+		},
 
-	var materialId = 0;
+		getWorldScale: function ( target ) {
 
-	function Material() {
+			if ( target === undefined ) {
 
-		Object.defineProperty( this, 'id', { value: materialId ++ } );
+				console.warn( 'THREE.Object3D: .getWorldScale() target is now required' );
+				target = new Vector3();
 
-		this.uuid = MathUtils.generateUUID();
+			}
 
-		this.name = '';
-		this.type = 'Material';
+			this.updateMatrixWorld( true );
 
-		this.fog = true;
+			this.matrixWorld.decompose( _position, _quaternion$2, target );
 
-		this.blending = NormalBlending;
-		this.side = FrontSide;
-		this.flatShading = false;
-		this.vertexColors = false;
+			return target;
 
-		this.opacity = 1;
-		this.transparent = false;
+		},
 
-		this.blendSrc = SrcAlphaFactor;
-		this.blendDst = OneMinusSrcAlphaFactor;
-		this.blendEquation = AddEquation;
-		this.blendSrcAlpha = null;
-		this.blendDstAlpha = null;
-		this.blendEquationAlpha = null;
+		getWorldDirection: function ( target ) {
 
-		this.depthFunc = LessEqualDepth;
-		this.depthTest = true;
-		this.depthWrite = true;
+			if ( target === undefined ) {
 
-		this.stencilWriteMask = 0xff;
-		this.stencilFunc = AlwaysStencilFunc;
-		this.stencilRef = 0;
-		this.stencilFuncMask = 0xff;
-		this.stencilFail = KeepStencilOp;
-		this.stencilZFail = KeepStencilOp;
-		this.stencilZPass = KeepStencilOp;
-		this.stencilWrite = false;
+				console.warn( 'THREE.Object3D: .getWorldDirection() target is now required' );
+				target = new Vector3();
 
-		this.clippingPlanes = null;
-		this.clipIntersection = false;
-		this.clipShadows = false;
+			}
 
-		this.shadowSide = null;
+			this.updateMatrixWorld( true );
 
-		this.colorWrite = true;
+			var e = this.matrixWorld.elements;
 
-		this.precision = null; // override the renderer's default precision for this material
+			return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
 
-		this.polygonOffset = false;
-		this.polygonOffsetFactor = 0;
-		this.polygonOffsetUnits = 0;
+		},
 
-		this.dithering = false;
+		raycast: function () {},
 
-		this.alphaTest = 0;
-		this.premultipliedAlpha = false;
+		traverse: function ( callback ) {
 
-		this.visible = true;
+			callback( this );
 
-		this.toneMapped = true;
+			var children = this.children;
 
-		this.userData = {};
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-		this.version = 0;
+				children[ i ].traverse( callback );
 
-	}
+			}
 
-	Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+		},
 
-		constructor: Material,
+		traverseVisible: function ( callback ) {
 
-		isMaterial: true,
+			if ( this.visible === false ) { return; }
 
-		onBeforeCompile: function () {},
+			callback( this );
 
-		setValues: function ( values ) {
+			var children = this.children;
 
-			if ( values === undefined ) { return; }
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-			for ( var key in values ) {
+				children[ i ].traverseVisible( callback );
 
-				var newValue = values[ key ];
+			}
 
-				if ( newValue === undefined ) {
+		},
 
-					console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
-					continue;
+		traverseAncestors: function ( callback ) {
 
-				}
+			var parent = this.parent;
 
-				// for backward compatability if shading is set in the constructor
-				if ( key === 'shading' ) {
+			if ( parent !== null ) {
 
-					console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
-					this.flatShading = ( newValue === FlatShading ) ? true : false;
-					continue;
+				callback( parent );
 
-				}
+				parent.traverseAncestors( callback );
 
-				var currentValue = this[ key ];
+			}
 
-				if ( currentValue === undefined ) {
+		},
 
-					console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
-					continue;
+		updateMatrix: function () {
 
-				}
+			this.matrix.compose( this.position, this.quaternion, this.scale );
 
-				if ( currentValue && currentValue.isColor ) {
+			this.matrixWorldNeedsUpdate = true;
 
-					currentValue.set( newValue );
+		},
 
-				} else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
+		updateMatrixWorld: function ( force ) {
 
-					currentValue.copy( newValue );
+			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
+
+			if ( this.matrixWorldNeedsUpdate || force ) {
+
+				if ( this.parent === null ) {
+
+					this.matrixWorld.copy( this.matrix );
 
 				} else {
 
-					this[ key ] = newValue;
+					this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
 
 				}
 
+				this.matrixWorldNeedsUpdate = false;
+
+				force = true;
+
+			}
+
+			// update children
+
+			var children = this.children;
+
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
+
+				children[ i ].updateMatrixWorld( force );
+
 			}
 
 		},
 
-		toJSON: function ( meta ) {
+		updateWorldMatrix: function ( updateParents, updateChildren ) {
 
-			var isRoot = ( meta === undefined || typeof meta === 'string' );
+			var parent = this.parent;
 
-			if ( isRoot ) {
+			if ( updateParents === true && parent !== null ) {
 
-				meta = {
-					textures: {},
-					images: {}
-				};
+				parent.updateWorldMatrix( true, false );
 
 			}
 
-			var data = {
-				metadata: {
-					version: 4.5,
-					type: 'Material',
-					generator: 'Material.toJSON'
-				}
-			};
+			if ( this.matrixAutoUpdate ) { this.updateMatrix(); }
 
-			// standard Material serialization
-			data.uuid = this.uuid;
-			data.type = this.type;
+			if ( this.parent === null ) {
 
-			if ( this.name !== '' ) { data.name = this.name; }
+				this.matrixWorld.copy( this.matrix );
 
-			if ( this.color && this.color.isColor ) { data.color = this.color.getHex(); }
+			} else {
 
-			if ( this.roughness !== undefined ) { data.roughness = this.roughness; }
-			if ( this.metalness !== undefined ) { data.metalness = this.metalness; }
+				this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
 
-			if ( this.sheen && this.sheen.isColor ) { data.sheen = this.sheen.getHex(); }
-			if ( this.emissive && this.emissive.isColor ) { data.emissive = this.emissive.getHex(); }
-			if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) { data.emissiveIntensity = this.emissiveIntensity; }
+			}
 
-			if ( this.specular && this.specular.isColor ) { data.specular = this.specular.getHex(); }
-			if ( this.shininess !== undefined ) { data.shininess = this.shininess; }
-			if ( this.clearcoat !== undefined ) { data.clearcoat = this.clearcoat; }
-			if ( this.clearcoatRoughness !== undefined ) { data.clearcoatRoughness = this.clearcoatRoughness; }
+			// update children
 
-			if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
+			if ( updateChildren === true ) {
 
-				data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
+				var children = this.children;
 
-			}
+				for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-			if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
+					children[ i ].updateWorldMatrix( false, true );
 
-				data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
+				}
 
 			}
 
-			if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
+		},
 
-				data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
-				data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
+		toJSON: function ( meta ) {
 
-			}
+			// meta is a string when called from JSON.stringify
+			var isRootObject = ( meta === undefined || typeof meta === 'string' );
 
-			if ( this.map && this.map.isTexture ) { data.map = this.map.toJSON( meta ).uuid; }
-			if ( this.matcap && this.matcap.isTexture ) { data.matcap = this.matcap.toJSON( meta ).uuid; }
-			if ( this.alphaMap && this.alphaMap.isTexture ) { data.alphaMap = this.alphaMap.toJSON( meta ).uuid; }
-			if ( this.lightMap && this.lightMap.isTexture ) { data.lightMap = this.lightMap.toJSON( meta ).uuid; }
+			var output = {};
 
-			if ( this.aoMap && this.aoMap.isTexture ) {
+			// meta is a hash used to collect geometries, materials.
+			// not providing it implies that this is the root object
+			// being serialized.
+			if ( isRootObject ) {
 
-				data.aoMap = this.aoMap.toJSON( meta ).uuid;
-				data.aoMapIntensity = this.aoMapIntensity;
+				// initialize meta obj
+				meta = {
+					geometries: {},
+					materials: {},
+					textures: {},
+					images: {},
+					shapes: {}
+				};
+
+				output.metadata = {
+					version: 4.5,
+					type: 'Object',
+					generator: 'Object3D.toJSON'
+				};
 
 			}
 
-			if ( this.bumpMap && this.bumpMap.isTexture ) {
+			// standard Object3D serialization
 
-				data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
-				data.bumpScale = this.bumpScale;
+			var object = {};
 
-			}
+			object.uuid = this.uuid;
+			object.type = this.type;
 
-			if ( this.normalMap && this.normalMap.isTexture ) {
+			if ( this.name !== '' ) { object.name = this.name; }
+			if ( this.castShadow === true ) { object.castShadow = true; }
+			if ( this.receiveShadow === true ) { object.receiveShadow = true; }
+			if ( this.visible === false ) { object.visible = false; }
+			if ( this.frustumCulled === false ) { object.frustumCulled = false; }
+			if ( this.renderOrder !== 0 ) { object.renderOrder = this.renderOrder; }
+			if ( JSON.stringify( this.userData ) !== '{}' ) { object.userData = this.userData; }
 
-				data.normalMap = this.normalMap.toJSON( meta ).uuid;
-				data.normalMapType = this.normalMapType;
-				data.normalScale = this.normalScale.toArray();
+			object.layers = this.layers.mask;
+			object.matrix = this.matrix.toArray();
 
-			}
+			if ( this.matrixAutoUpdate === false ) { object.matrixAutoUpdate = false; }
 
-			if ( this.displacementMap && this.displacementMap.isTexture ) {
+			// object specific properties
 
-				data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
-				data.displacementScale = this.displacementScale;
-				data.displacementBias = this.displacementBias;
+			if ( this.isInstancedMesh ) {
+
+				object.type = 'InstancedMesh';
+				object.count = this.count;
+				object.instanceMatrix = this.instanceMatrix.toJSON();
 
 			}
 
-			if ( this.roughnessMap && this.roughnessMap.isTexture ) { data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid; }
-			if ( this.metalnessMap && this.metalnessMap.isTexture ) { data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid; }
+			//
 
-			if ( this.emissiveMap && this.emissiveMap.isTexture ) { data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid; }
-			if ( this.specularMap && this.specularMap.isTexture ) { data.specularMap = this.specularMap.toJSON( meta ).uuid; }
+			function serialize( library, element ) {
 
-			if ( this.envMap && this.envMap.isTexture ) {
+				if ( library[ element.uuid ] === undefined ) {
 
-				data.envMap = this.envMap.toJSON( meta ).uuid;
-				data.reflectivity = this.reflectivity; // Scale behind envMap
-				data.refractionRatio = this.refractionRatio;
+					library[ element.uuid ] = element.toJSON( meta );
 
-				if ( this.combine !== undefined ) { data.combine = this.combine; }
-				if ( this.envMapIntensity !== undefined ) { data.envMapIntensity = this.envMapIntensity; }
+				}
+
+				return element.uuid;
 
 			}
 
-			if ( this.gradientMap && this.gradientMap.isTexture ) {
+			if ( this.isMesh || this.isLine || this.isPoints ) {
 
-				data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
+				object.geometry = serialize( meta.geometries, this.geometry );
 
-			}
+				var parameters = this.geometry.parameters;
 
-			if ( this.size !== undefined ) { data.size = this.size; }
-			if ( this.sizeAttenuation !== undefined ) { data.sizeAttenuation = this.sizeAttenuation; }
+				if ( parameters !== undefined && parameters.shapes !== undefined ) {
 
-			if ( this.blending !== NormalBlending ) { data.blending = this.blending; }
-			if ( this.flatShading === true ) { data.flatShading = this.flatShading; }
-			if ( this.side !== FrontSide ) { data.side = this.side; }
-			if ( this.vertexColors ) { data.vertexColors = true; }
+					var shapes = parameters.shapes;
 
-			if ( this.opacity < 1 ) { data.opacity = this.opacity; }
-			if ( this.transparent === true ) { data.transparent = this.transparent; }
+					if ( Array.isArray( shapes ) ) {
 
-			data.depthFunc = this.depthFunc;
-			data.depthTest = this.depthTest;
-			data.depthWrite = this.depthWrite;
+						for ( var i = 0, l = shapes.length; i < l; i ++ ) {
 
-			data.stencilWrite = this.stencilWrite;
-			data.stencilWriteMask = this.stencilWriteMask;
-			data.stencilFunc = this.stencilFunc;
-			data.stencilRef = this.stencilRef;
-			data.stencilFuncMask = this.stencilFuncMask;
-			data.stencilFail = this.stencilFail;
-			data.stencilZFail = this.stencilZFail;
-			data.stencilZPass = this.stencilZPass;
+							var shape = shapes[ i ];
 
-			// rotation (SpriteMaterial)
-			if ( this.rotation && this.rotation !== 0 ) { data.rotation = this.rotation; }
+							serialize( meta.shapes, shape );
 
-			if ( this.polygonOffset === true ) { data.polygonOffset = true; }
-			if ( this.polygonOffsetFactor !== 0 ) { data.polygonOffsetFactor = this.polygonOffsetFactor; }
-			if ( this.polygonOffsetUnits !== 0 ) { data.polygonOffsetUnits = this.polygonOffsetUnits; }
+						}
 
-			if ( this.linewidth && this.linewidth !== 1 ) { data.linewidth = this.linewidth; }
-			if ( this.dashSize !== undefined ) { data.dashSize = this.dashSize; }
-			if ( this.gapSize !== undefined ) { data.gapSize = this.gapSize; }
-			if ( this.scale !== undefined ) { data.scale = this.scale; }
+					} else {
 
-			if ( this.dithering === true ) { data.dithering = true; }
+						serialize( meta.shapes, shapes );
 
-			if ( this.alphaTest > 0 ) { data.alphaTest = this.alphaTest; }
-			if ( this.premultipliedAlpha === true ) { data.premultipliedAlpha = this.premultipliedAlpha; }
+					}
 
-			if ( this.wireframe === true ) { data.wireframe = this.wireframe; }
-			if ( this.wireframeLinewidth > 1 ) { data.wireframeLinewidth = this.wireframeLinewidth; }
-			if ( this.wireframeLinecap !== 'round' ) { data.wireframeLinecap = this.wireframeLinecap; }
-			if ( this.wireframeLinejoin !== 'round' ) { data.wireframeLinejoin = this.wireframeLinejoin; }
+				}
 
-			if ( this.morphTargets === true ) { data.morphTargets = true; }
-			if ( this.morphNormals === true ) { data.morphNormals = true; }
-			if ( this.skinning === true ) { data.skinning = true; }
+			}
 
-			if ( this.visible === false ) { data.visible = false; }
+			if ( this.material !== undefined ) {
 
-			if ( this.toneMapped === false ) { data.toneMapped = false; }
+				if ( Array.isArray( this.material ) ) {
 
-			if ( JSON.stringify( this.userData ) !== '{}' ) { data.userData = this.userData; }
+					var uuids = [];
 
-			// TODO: Copied from Object3D.toJSON
+					for ( var i = 0, l = this.material.length; i < l; i ++ ) {
 
-			function extractFromCache( cache ) {
+						uuids.push( serialize( meta.materials, this.material[ i ] ) );
 
-				var values = [];
+					}
 
-				for ( var key in cache ) {
+					object.material = uuids;
 
-					var data = cache[ key ];
-					delete data.metadata;
-					values.push( data );
+				} else {
 
-				}
+					object.material = serialize( meta.materials, this.material );
 
-				return values;
+				}
 
 			}
 
-			if ( isRoot ) {
-
-				var textures = extractFromCache( meta.textures );
-				var images = extractFromCache( meta.images );
+			//
 
-				if ( textures.length > 0 ) { data.textures = textures; }
-				if ( images.length > 0 ) { data.images = images; }
+			if ( this.children.length > 0 ) {
 
-			}
+				object.children = [];
 
-			return data;
+				for ( var i = 0; i < this.children.length; i ++ ) {
 
-		},
+					object.children.push( this.children[ i ].toJSON( meta ).object );
 
-		clone: function () {
+				}
 
-			return new this.constructor().copy( this );
+			}
 
-		},
+			if ( isRootObject ) {
 
-		copy: function ( source ) {
+				var geometries = extractFromCache( meta.geometries );
+				var materials = extractFromCache( meta.materials );
+				var textures = extractFromCache( meta.textures );
+				var images = extractFromCache( meta.images );
+				var shapes = extractFromCache( meta.shapes );
 
-			this.name = source.name;
+				if ( geometries.length > 0 ) { output.geometries = geometries; }
+				if ( materials.length > 0 ) { output.materials = materials; }
+				if ( textures.length > 0 ) { output.textures = textures; }
+				if ( images.length > 0 ) { output.images = images; }
+				if ( shapes.length > 0 ) { output.shapes = shapes; }
 
-			this.fog = source.fog;
+			}
 
-			this.blending = source.blending;
-			this.side = source.side;
-			this.flatShading = source.flatShading;
-			this.vertexColors = source.vertexColors;
+			output.object = object;
 
-			this.opacity = source.opacity;
-			this.transparent = source.transparent;
+			return output;
 
-			this.blendSrc = source.blendSrc;
-			this.blendDst = source.blendDst;
-			this.blendEquation = source.blendEquation;
-			this.blendSrcAlpha = source.blendSrcAlpha;
-			this.blendDstAlpha = source.blendDstAlpha;
-			this.blendEquationAlpha = source.blendEquationAlpha;
+			// extract data from the cache hash
+			// remove metadata on each item
+			// and return as array
+			function extractFromCache( cache ) {
 
-			this.depthFunc = source.depthFunc;
-			this.depthTest = source.depthTest;
-			this.depthWrite = source.depthWrite;
+				var values = [];
+				for ( var key in cache ) {
 
-			this.stencilWriteMask = source.stencilWriteMask;
-			this.stencilFunc = source.stencilFunc;
-			this.stencilRef = source.stencilRef;
-			this.stencilFuncMask = source.stencilFuncMask;
-			this.stencilFail = source.stencilFail;
-			this.stencilZFail = source.stencilZFail;
-			this.stencilZPass = source.stencilZPass;
-			this.stencilWrite = source.stencilWrite;
+					var data = cache[ key ];
+					delete data.metadata;
+					values.push( data );
 
-			var srcPlanes = source.clippingPlanes,
-				dstPlanes = null;
+				}
 
-			if ( srcPlanes !== null ) {
+				return values;
 
-				var n = srcPlanes.length;
-				dstPlanes = new Array( n );
+			}
 
-				for ( var i = 0; i !== n; ++ i )
-					{ dstPlanes[ i ] = srcPlanes[ i ].clone(); }
+		},
 
-			}
+		clone: function ( recursive ) {
 
-			this.clippingPlanes = dstPlanes;
-			this.clipIntersection = source.clipIntersection;
-			this.clipShadows = source.clipShadows;
+			return new this.constructor().copy( this, recursive );
 
-			this.shadowSide = source.shadowSide;
+		},
 
-			this.colorWrite = source.colorWrite;
+		copy: function ( source, recursive ) {
 
-			this.precision = source.precision;
+			if ( recursive === undefined ) { recursive = true; }
 
-			this.polygonOffset = source.polygonOffset;
-			this.polygonOffsetFactor = source.polygonOffsetFactor;
-			this.polygonOffsetUnits = source.polygonOffsetUnits;
+			this.name = source.name;
 
-			this.dithering = source.dithering;
+			this.up.copy( source.up );
 
-			this.alphaTest = source.alphaTest;
-			this.premultipliedAlpha = source.premultipliedAlpha;
+			this.position.copy( source.position );
+			this.quaternion.copy( source.quaternion );
+			this.scale.copy( source.scale );
 
-			this.visible = source.visible;
+			this.matrix.copy( source.matrix );
+			this.matrixWorld.copy( source.matrixWorld );
 
-			this.toneMapped = source.toneMapped;
+			this.matrixAutoUpdate = source.matrixAutoUpdate;
+			this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
 
-			this.userData = JSON.parse( JSON.stringify( source.userData ) );
+			this.layers.mask = source.layers.mask;
+			this.visible = source.visible;
 
-			return this;
+			this.castShadow = source.castShadow;
+			this.receiveShadow = source.receiveShadow;
 
-		},
+			this.frustumCulled = source.frustumCulled;
+			this.renderOrder = source.renderOrder;
 
-		dispose: function () {
+			this.userData = JSON.parse( JSON.stringify( source.userData ) );
 
-			this.dispatchEvent( { type: 'dispose' } );
+			if ( recursive === true ) {
 
-		}
+				for ( var i = 0; i < source.children.length; i ++ ) {
 
-	} );
+					var child = source.children[ i ];
+					this.add( child.clone() );
 
-	Object.defineProperty( Material.prototype, 'needsUpdate', {
+				}
 
-		set: function ( value ) {
+			}
 
-			if ( value === true ) { this.version ++; }
+			return this;
 
 		}
 
@@ -17558,223 +25143,204 @@ function simpleEnd(buf) {
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  lightMap: new THREE.Texture( <Image> ),
-	 *  lightMapIntensity: <float>
-	 *
-	 *  aoMap: new THREE.Texture( <Image> ),
-	 *  aoMapIntensity: <float>
-	 *
-	 *  specularMap: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
-	 *  combine: THREE.Multiply,
-	 *  reflectivity: <float>,
-	 *  refractionRatio: <float>,
-	 *
-	 *  depthTest: <bool>,
-	 *  depthWrite: <bool>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>
-	 * }
 	 */
 
-	function MeshBasicMaterial( parameters ) {
+	function Scene() {
 
-		Material.call( this );
+		Object3D.call( this );
 
-		this.type = 'MeshBasicMaterial';
+		this.type = 'Scene';
 
-		this.color = new Color( 0xffffff ); // emissive
+		this.background = null;
+		this.environment = null;
+		this.fog = null;
 
-		this.map = null;
+		this.overrideMaterial = null;
 
-		this.lightMap = null;
-		this.lightMapIntensity = 1.0;
+		this.autoUpdate = true; // checked by the renderer
 
-		this.aoMap = null;
-		this.aoMapIntensity = 1.0;
+		if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
 
-		this.specularMap = null;
+			__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef
 
-		this.alphaMap = null;
+		}
 
-		this.envMap = null;
-		this.combine = MultiplyOperation;
-		this.reflectivity = 1;
-		this.refractionRatio = 0.98;
+	}
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
-		this.wireframeLinecap = 'round';
-		this.wireframeLinejoin = 'round';
+	Scene.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-		this.skinning = false;
-		this.morphTargets = false;
+		constructor: Scene,
 
-		this.setValues( parameters );
+		isScene: true,
 
-	}
+		copy: function ( source, recursive ) {
 
-	MeshBasicMaterial.prototype = Object.create( Material.prototype );
-	MeshBasicMaterial.prototype.constructor = MeshBasicMaterial;
+			Object3D.prototype.copy.call( this, source, recursive );
 
-	MeshBasicMaterial.prototype.isMeshBasicMaterial = true;
+			if ( source.background !== null ) { this.background = source.background.clone(); }
+			if ( source.environment !== null ) { this.environment = source.environment.clone(); }
+			if ( source.fog !== null ) { this.fog = source.fog.clone(); }
 
-	MeshBasicMaterial.prototype.copy = function ( source ) {
+			if ( source.overrideMaterial !== null ) { this.overrideMaterial = source.overrideMaterial.clone(); }
 
-		Material.prototype.copy.call( this, source );
+			this.autoUpdate = source.autoUpdate;
+			this.matrixAutoUpdate = source.matrixAutoUpdate;
 
-		this.color.copy( source.color );
+			return this;
 
-		this.map = source.map;
+		},
 
-		this.lightMap = source.lightMap;
-		this.lightMapIntensity = source.lightMapIntensity;
+		toJSON: function ( meta ) {
 
-		this.aoMap = source.aoMap;
-		this.aoMapIntensity = source.aoMapIntensity;
+			var data = Object3D.prototype.toJSON.call( this, meta );
 
-		this.specularMap = source.specularMap;
+			if ( this.background !== null ) { data.object.background = this.background.toJSON( meta ); }
+			if ( this.environment !== null ) { data.object.environment = this.environment.toJSON( meta ); }
+			if ( this.fog !== null ) { data.object.fog = this.fog.toJSON(); }
 
-		this.alphaMap = source.alphaMap;
+			return data;
 
-		this.envMap = source.envMap;
-		this.combine = source.combine;
-		this.reflectivity = source.reflectivity;
-		this.refractionRatio = source.refractionRatio;
+		},
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
-		this.wireframeLinecap = source.wireframeLinecap;
-		this.wireframeLinejoin = source.wireframeLinejoin;
+		dispose: function () {
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
+			this.dispatchEvent( { type: 'dispose' } );
 
-		return this;
+		}
 
-	};
+	} );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+	var _points = [
+		new Vector3(),
+		new Vector3(),
+		new Vector3(),
+		new Vector3(),
+		new Vector3(),
+		new Vector3(),
+		new Vector3(),
+		new Vector3()
+	];
 
-	var _vector$3 = new Vector3();
+	var _vector$1 = new Vector3();
 
-	function BufferAttribute( array, itemSize, normalized ) {
+	var _box = new Box3();
 
-		if ( Array.isArray( array ) ) {
+	// triangle centered vertices
 
-			throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
+	var _v0 = new Vector3();
+	var _v1$2 = new Vector3();
+	var _v2 = new Vector3();
 
-		}
+	// triangle edge vectors
 
-		this.name = '';
+	var _f0 = new Vector3();
+	var _f1 = new Vector3();
+	var _f2 = new Vector3();
 
-		this.array = array;
-		this.itemSize = itemSize;
-		this.count = array !== undefined ? array.length / itemSize : 0;
-		this.normalized = normalized === true;
+	var _center = new Vector3();
+	var _extents = new Vector3();
+	var _triangleNormal = new Vector3();
+	var _testAxis = new Vector3();
 
-		this.usage = StaticDrawUsage;
-		this.updateRange = { offset: 0, count: - 1 };
+	/**
+	 * @author bhouston / http://clara.io
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-		this.version = 0;
+	function Box3( min, max ) {
+
+		this.min = ( min !== undefined ) ? min : new Vector3( + Infinity, + Infinity, + Infinity );
+		this.max = ( max !== undefined ) ? max : new Vector3( - Infinity, - Infinity, - Infinity );
 
 	}
 
-	Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', {
 
-		set: function ( value ) {
+	Object.assign( Box3.prototype, {
 
-			if ( value === true ) { this.version ++; }
+		isBox3: true,
 
-		}
+		set: function ( min, max ) {
 
-	} );
+			this.min.copy( min );
+			this.max.copy( max );
 
-	Object.assign( BufferAttribute.prototype, {
+			return this;
 
-		isBufferAttribute: true,
+		},
 
-		onUploadCallback: function () {},
+		setFromArray: function ( array ) {
 
-		setUsage: function ( value ) {
+			var minX = + Infinity;
+			var minY = + Infinity;
+			var minZ = + Infinity;
 
-			this.usage = value;
+			var maxX = - Infinity;
+			var maxY = - Infinity;
+			var maxZ = - Infinity;
 
-			return this;
+			for ( var i = 0, l = array.length; i < l; i += 3 ) {
 
-		},
+				var x = array[ i ];
+				var y = array[ i + 1 ];
+				var z = array[ i + 2 ];
 
-		copy: function ( source ) {
+				if ( x < minX ) { minX = x; }
+				if ( y < minY ) { minY = y; }
+				if ( z < minZ ) { minZ = z; }
 
-			this.name = source.name;
-			this.array = new source.array.constructor( source.array );
-			this.itemSize = source.itemSize;
-			this.count = source.count;
-			this.normalized = source.normalized;
+				if ( x > maxX ) { maxX = x; }
+				if ( y > maxY ) { maxY = y; }
+				if ( z > maxZ ) { maxZ = z; }
 
-			this.usage = source.usage;
+			}
+
+			this.min.set( minX, minY, minZ );
+			this.max.set( maxX, maxY, maxZ );
 
 			return this;
 
 		},
 
-		copyAt: function ( index1, attribute, index2 ) {
+		setFromBufferAttribute: function ( attribute ) {
 
-			index1 *= this.itemSize;
-			index2 *= attribute.itemSize;
+			var minX = + Infinity;
+			var minY = + Infinity;
+			var minZ = + Infinity;
 
-			for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
+			var maxX = - Infinity;
+			var maxY = - Infinity;
+			var maxZ = - Infinity;
 
-				this.array[ index1 + i ] = attribute.array[ index2 + i ];
+			for ( var i = 0, l = attribute.count; i < l; i ++ ) {
 
-			}
+				var x = attribute.getX( i );
+				var y = attribute.getY( i );
+				var z = attribute.getZ( i );
 
-			return this;
+				if ( x < minX ) { minX = x; }
+				if ( y < minY ) { minY = y; }
+				if ( z < minZ ) { minZ = z; }
 
-		},
+				if ( x > maxX ) { maxX = x; }
+				if ( y > maxY ) { maxY = y; }
+				if ( z > maxZ ) { maxZ = z; }
 
-		copyArray: function ( array ) {
+			}
 
-			this.array.set( array );
+			this.min.set( minX, minY, minZ );
+			this.max.set( maxX, maxY, maxZ );
 
 			return this;
 
 		},
 
-		copyColorsArray: function ( colors ) {
-
-			var array = this.array, offset = 0;
-
-			for ( var i = 0, l = colors.length; i < l; i ++ ) {
-
-				var color = colors[ i ];
-
-				if ( color === undefined ) {
+		setFromPoints: function ( points ) {
 
-					console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
-					color = new Color();
+			this.makeEmpty();
 
-				}
+			for ( var i = 0, il = points.length; i < il; i ++ ) {
 
-				array[ offset ++ ] = color.r;
-				array[ offset ++ ] = color.g;
-				array[ offset ++ ] = color.b;
+				this.expandByPoint( points[ i ] );
 
 			}
 
@@ -17782,9869 +25348,9797 @@ function simpleEnd(buf) {
 
 		},
 
-		copyVector2sArray: function ( vectors ) {
-
-			var array = this.array, offset = 0;
-
-			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
+		setFromCenterAndSize: function ( center, size ) {
 
-				var vector = vectors[ i ];
+			var halfSize = _vector$1.copy( size ).multiplyScalar( 0.5 );
 
-				if ( vector === undefined ) {
+			this.min.copy( center ).sub( halfSize );
+			this.max.copy( center ).add( halfSize );
 
-					console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
-					vector = new Vector2();
+			return this;
 
-				}
+		},
 
-				array[ offset ++ ] = vector.x;
-				array[ offset ++ ] = vector.y;
+		setFromObject: function ( object ) {
 
-			}
+			this.makeEmpty();
 
-			return this;
+			return this.expandByObject( object );
 
 		},
 
-		copyVector3sArray: function ( vectors ) {
+		clone: function () {
 
-			var array = this.array, offset = 0;
+			return new this.constructor().copy( this );
 
-			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
+		},
 
-				var vector = vectors[ i ];
+		copy: function ( box ) {
 
-				if ( vector === undefined ) {
+			this.min.copy( box.min );
+			this.max.copy( box.max );
 
-					console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
-					vector = new Vector3();
+			return this;
 
-				}
+		},
 
-				array[ offset ++ ] = vector.x;
-				array[ offset ++ ] = vector.y;
-				array[ offset ++ ] = vector.z;
+		makeEmpty: function () {
 
-			}
+			this.min.x = this.min.y = this.min.z = + Infinity;
+			this.max.x = this.max.y = this.max.z = - Infinity;
 
 			return this;
 
 		},
 
-		copyVector4sArray: function ( vectors ) {
-
-			var array = this.array, offset = 0;
+		isEmpty: function () {
 
-			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
+			// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
 
-				var vector = vectors[ i ];
+			return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
 
-				if ( vector === undefined ) {
+		},
 
-					console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
-					vector = new Vector4();
+		getCenter: function ( target ) {
 
-				}
+			if ( target === undefined ) {
 
-				array[ offset ++ ] = vector.x;
-				array[ offset ++ ] = vector.y;
-				array[ offset ++ ] = vector.z;
-				array[ offset ++ ] = vector.w;
+				console.warn( 'THREE.Box3: .getCenter() target is now required' );
+				target = new Vector3();
 
 			}
 
-			return this;
+			return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
 
 		},
 
-		applyMatrix3: function ( m ) {
-
-			for ( var i = 0, l = this.count; i < l; i ++ ) {
-
-				_vector$3.x = this.getX( i );
-				_vector$3.y = this.getY( i );
-				_vector$3.z = this.getZ( i );
+		getSize: function ( target ) {
 
-				_vector$3.applyMatrix3( m );
+			if ( target === undefined ) {
 
-				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
+				console.warn( 'THREE.Box3: .getSize() target is now required' );
+				target = new Vector3();
 
 			}
 
-			return this;
+			return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
 
 		},
 
-		applyMatrix4: function ( m ) {
+		expandByPoint: function ( point ) {
 
-			for ( var i = 0, l = this.count; i < l; i ++ ) {
+			this.min.min( point );
+			this.max.max( point );
 
-				_vector$3.x = this.getX( i );
-				_vector$3.y = this.getY( i );
-				_vector$3.z = this.getZ( i );
+			return this;
 
-				_vector$3.applyMatrix4( m );
+		},
 
-				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
+		expandByVector: function ( vector ) {
 
-			}
+			this.min.sub( vector );
+			this.max.add( vector );
 
 			return this;
 
 		},
 
-		applyNormalMatrix: function ( m ) {
+		expandByScalar: function ( scalar ) {
 
-			for ( var i = 0, l = this.count; i < l; i ++ ) {
+			this.min.addScalar( - scalar );
+			this.max.addScalar( scalar );
 
-				_vector$3.x = this.getX( i );
-				_vector$3.y = this.getY( i );
-				_vector$3.z = this.getZ( i );
+			return this;
 
-				_vector$3.applyNormalMatrix( m );
+		},
 
-				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
+		expandByObject: function ( object ) {
 
-			}
+			// Computes the world-axis-aligned bounding box of an object (including its children),
+			// accounting for both the object's, and children's, world transforms
 
-			return this;
+			object.updateWorldMatrix( false, false );
 
-		},
+			var geometry = object.geometry;
 
-		transformDirection: function ( m ) {
+			if ( geometry !== undefined ) {
 
-			for ( var i = 0, l = this.count; i < l; i ++ ) {
+				if ( geometry.boundingBox === null ) {
 
-				_vector$3.x = this.getX( i );
-				_vector$3.y = this.getY( i );
-				_vector$3.z = this.getZ( i );
+					geometry.computeBoundingBox();
 
-				_vector$3.transformDirection( m );
+				}
 
-				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
+				_box.copy( geometry.boundingBox );
+				_box.applyMatrix4( object.matrixWorld );
 
-			}
+				this.union( _box );
 
-			return this;
+			}
 
-		},
+			var children = object.children;
 
-		set: function ( value, offset ) {
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-			if ( offset === undefined ) { offset = 0; }
+				this.expandByObject( children[ i ] );
 
-			this.array.set( value, offset );
+			}
 
 			return this;
 
 		},
 
-		getX: function ( index ) {
+		containsPoint: function ( point ) {
 
-			return this.array[ index * this.itemSize ];
+			return point.x < this.min.x || point.x > this.max.x ||
+				point.y < this.min.y || point.y > this.max.y ||
+				point.z < this.min.z || point.z > this.max.z ? false : true;
 
 		},
 
-		setX: function ( index, x ) {
-
-			this.array[ index * this.itemSize ] = x;
+		containsBox: function ( box ) {
 
-			return this;
+			return this.min.x <= box.min.x && box.max.x <= this.max.x &&
+				this.min.y <= box.min.y && box.max.y <= this.max.y &&
+				this.min.z <= box.min.z && box.max.z <= this.max.z;
 
 		},
 
-		getY: function ( index ) {
+		getParameter: function ( point, target ) {
 
-			return this.array[ index * this.itemSize + 1 ];
+			// This can potentially have a divide by zero if the box
+			// has a size dimension of 0.
 
-		},
+			if ( target === undefined ) {
 
-		setY: function ( index, y ) {
+				console.warn( 'THREE.Box3: .getParameter() target is now required' );
+				target = new Vector3();
 
-			this.array[ index * this.itemSize + 1 ] = y;
+			}
 
-			return this;
+			return target.set(
+				( point.x - this.min.x ) / ( this.max.x - this.min.x ),
+				( point.y - this.min.y ) / ( this.max.y - this.min.y ),
+				( point.z - this.min.z ) / ( this.max.z - this.min.z )
+			);
 
 		},
 
-		getZ: function ( index ) {
+		intersectsBox: function ( box ) {
 
-			return this.array[ index * this.itemSize + 2 ];
+			// using 6 splitting planes to rule out intersections.
+			return box.max.x < this.min.x || box.min.x > this.max.x ||
+				box.max.y < this.min.y || box.min.y > this.max.y ||
+				box.max.z < this.min.z || box.min.z > this.max.z ? false : true;
 
 		},
 
-		setZ: function ( index, z ) {
+		intersectsSphere: function ( sphere ) {
 
-			this.array[ index * this.itemSize + 2 ] = z;
+			// Find the point on the AABB closest to the sphere center.
+			this.clampPoint( sphere.center, _vector$1 );
 
-			return this;
+			// If that point is inside the sphere, the AABB and sphere intersect.
+			return _vector$1.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
 
 		},
 
-		getW: function ( index ) {
-
-			return this.array[ index * this.itemSize + 3 ];
-
-		},
+		intersectsPlane: function ( plane ) {
 
-		setW: function ( index, w ) {
+			// We compute the minimum and maximum dot product values. If those values
+			// are on the same side (back or front) of the plane, then there is no intersection.
 
-			this.array[ index * this.itemSize + 3 ] = w;
+			var min, max;
 
-			return this;
+			if ( plane.normal.x > 0 ) {
 
-		},
+				min = plane.normal.x * this.min.x;
+				max = plane.normal.x * this.max.x;
 
-		setXY: function ( index, x, y ) {
+			} else {
 
-			index *= this.itemSize;
+				min = plane.normal.x * this.max.x;
+				max = plane.normal.x * this.min.x;
 
-			this.array[ index + 0 ] = x;
-			this.array[ index + 1 ] = y;
+			}
 
-			return this;
+			if ( plane.normal.y > 0 ) {
 
-		},
+				min += plane.normal.y * this.min.y;
+				max += plane.normal.y * this.max.y;
 
-		setXYZ: function ( index, x, y, z ) {
+			} else {
 
-			index *= this.itemSize;
+				min += plane.normal.y * this.max.y;
+				max += plane.normal.y * this.min.y;
 
-			this.array[ index + 0 ] = x;
-			this.array[ index + 1 ] = y;
-			this.array[ index + 2 ] = z;
+			}
 
-			return this;
+			if ( plane.normal.z > 0 ) {
 
-		},
+				min += plane.normal.z * this.min.z;
+				max += plane.normal.z * this.max.z;
 
-		setXYZW: function ( index, x, y, z, w ) {
+			} else {
 
-			index *= this.itemSize;
+				min += plane.normal.z * this.max.z;
+				max += plane.normal.z * this.min.z;
 
-			this.array[ index + 0 ] = x;
-			this.array[ index + 1 ] = y;
-			this.array[ index + 2 ] = z;
-			this.array[ index + 3 ] = w;
+			}
 
-			return this;
+			return ( min <= - plane.constant && max >= - plane.constant );
 
 		},
 
-		onUpload: function ( callback ) {
+		intersectsTriangle: function ( triangle ) {
 
-			this.onUploadCallback = callback;
+			if ( this.isEmpty() ) {
 
-			return this;
+				return false;
 
-		},
+			}
 
-		clone: function () {
+			// compute box center and extents
+			this.getCenter( _center );
+			_extents.subVectors( this.max, _center );
 
-			return new this.constructor( this.array, this.itemSize ).copy( this );
+			// translate triangle to aabb origin
+			_v0.subVectors( triangle.a, _center );
+			_v1$2.subVectors( triangle.b, _center );
+			_v2.subVectors( triangle.c, _center );
 
-		},
+			// compute edge vectors for triangle
+			_f0.subVectors( _v1$2, _v0 );
+			_f1.subVectors( _v2, _v1$2 );
+			_f2.subVectors( _v0, _v2 );
 
-		toJSON: function () {
+			// test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
+			// make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
+			// axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
+			var axes = [
+				0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
+				_f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
+				- _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
+			];
+			if ( ! satForAxes( axes, _v0, _v1$2, _v2, _extents ) ) {
 
-			return {
-				itemSize: this.itemSize,
-				type: this.array.constructor.name,
-				array: Array.prototype.slice.call( this.array ),
-				normalized: this.normalized
-			};
+				return false;
 
-		}
+			}
 
-	} );
+			// test 3 face normals from the aabb
+			axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
+			if ( ! satForAxes( axes, _v0, _v1$2, _v2, _extents ) ) {
 
-	//
+				return false;
 
-	function Int8BufferAttribute( array, itemSize, normalized ) {
+			}
 
-		BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
+			// finally testing the face normal of the triangle
+			// use already existing triangle edge vectors here
+			_triangleNormal.crossVectors( _f0, _f1 );
+			axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
 
-	}
+			return satForAxes( axes, _v0, _v1$2, _v2, _extents );
 
-	Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
+		},
 
+		clampPoint: function ( point, target ) {
 
-	function Uint8BufferAttribute( array, itemSize, normalized ) {
+			if ( target === undefined ) {
 
-		BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
+				console.warn( 'THREE.Box3: .clampPoint() target is now required' );
+				target = new Vector3();
 
-	}
+			}
 
-	Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
+			return target.copy( point ).clamp( this.min, this.max );
 
+		},
 
-	function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
+		distanceToPoint: function ( point ) {
 
-		BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
+			var clampedPoint = _vector$1.copy( point ).clamp( this.min, this.max );
 
-	}
+			return clampedPoint.sub( point ).length();
 
-	Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
+		},
 
+		getBoundingSphere: function ( target ) {
 
-	function Int16BufferAttribute( array, itemSize, normalized ) {
+			if ( target === undefined ) {
 
-		BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
+				console.error( 'THREE.Box3: .getBoundingSphere() target is now required' );
+				//target = new Sphere(); // removed to avoid cyclic dependency
 
-	}
+			}
 
-	Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
+			this.getCenter( target.center );
 
+			target.radius = this.getSize( _vector$1 ).length() * 0.5;
 
-	function Uint16BufferAttribute( array, itemSize, normalized ) {
+			return target;
 
-		BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
+		},
 
-	}
+		intersect: function ( box ) {
 
-	Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
+			this.min.max( box.min );
+			this.max.min( box.max );
 
+			// ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values.
+			if ( this.isEmpty() ) { this.makeEmpty(); }
 
-	function Int32BufferAttribute( array, itemSize, normalized ) {
+			return this;
 
-		BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
+		},
 
-	}
-
-	Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
+		union: function ( box ) {
 
+			this.min.min( box.min );
+			this.max.max( box.max );
 
-	function Uint32BufferAttribute( array, itemSize, normalized ) {
+			return this;
 
-		BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
+		},
 
-	}
+		applyMatrix4: function ( matrix ) {
 
-	Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
+			// transform of empty box is an empty box.
+			if ( this.isEmpty() ) { return this; }
 
+			// NOTE: I am using a binary pattern to specify all 2^3 combinations below
+			_points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
+			_points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
+			_points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
+			_points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
+			_points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
+			_points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
+			_points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
+			_points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
 
-	function Float32BufferAttribute( array, itemSize, normalized ) {
+			this.setFromPoints( _points );
 
-		BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
+			return this;
 
-	}
+		},
 
-	Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
+		translate: function ( offset ) {
 
+			this.min.add( offset );
+			this.max.add( offset );
 
-	function Float64BufferAttribute( array, itemSize, normalized ) {
+			return this;
 
-		BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
+		},
 
-	}
+		equals: function ( box ) {
 
-	Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
-	Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
+			return box.min.equals( this.min ) && box.max.equals( this.max );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		}
 
-	function DirectGeometry() {
+	} );
 
-		this.vertices = [];
-		this.normals = [];
-		this.colors = [];
-		this.uvs = [];
-		this.uvs2 = [];
+	function satForAxes( axes, v0, v1, v2, extents ) {
 
-		this.groups = [];
+		var i, j;
 
-		this.morphTargets = {};
+		for ( i = 0, j = axes.length - 3; i <= j; i += 3 ) {
 
-		this.skinWeights = [];
-		this.skinIndices = [];
+			_testAxis.fromArray( axes, i );
+			// project the aabb onto the seperating axis
+			var r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
+			// project all 3 vertices of the triangle onto the seperating axis
+			var p0 = v0.dot( _testAxis );
+			var p1 = v1.dot( _testAxis );
+			var p2 = v2.dot( _testAxis );
+			// actual test, basically see if either of the most extreme of the triangle points intersects r
+			if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
 
-		// this.lineDistances = [];
+				// points of the projected triangle are outside the projected half-length of the aabb
+				// the axis is seperating and we can exit
+				return false;
 
-		this.boundingBox = null;
-		this.boundingSphere = null;
+			}
 
-		// update flags
+		}
 
-		this.verticesNeedUpdate = false;
-		this.normalsNeedUpdate = false;
-		this.colorsNeedUpdate = false;
-		this.uvsNeedUpdate = false;
-		this.groupsNeedUpdate = false;
+		return true;
 
 	}
 
-	Object.assign( DirectGeometry.prototype, {
+	var _box$1 = new Box3();
 
-		computeGroups: function ( geometry ) {
+	/**
+	 * @author bhouston / http://clara.io
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			var group;
-			var groups = [];
-			var materialIndex = undefined;
+	function Sphere( center, radius ) {
 
-			var faces = geometry.faces;
+		this.center = ( center !== undefined ) ? center : new Vector3();
+		this.radius = ( radius !== undefined ) ? radius : - 1;
 
-			for ( var i = 0; i < faces.length; i ++ ) {
+	}
 
-				var face = faces[ i ];
+	Object.assign( Sphere.prototype, {
 
-				// materials
+		set: function ( center, radius ) {
 
-				if ( face.materialIndex !== materialIndex ) {
+			this.center.copy( center );
+			this.radius = radius;
 
-					materialIndex = face.materialIndex;
+			return this;
 
-					if ( group !== undefined ) {
+		},
 
-						group.count = ( i * 3 ) - group.start;
-						groups.push( group );
+		setFromPoints: function ( points, optionalCenter ) {
 
-					}
+			var center = this.center;
 
-					group = {
-						start: i * 3,
-						materialIndex: materialIndex
-					};
+			if ( optionalCenter !== undefined ) {
 
-				}
+				center.copy( optionalCenter );
+
+			} else {
+
+				_box$1.setFromPoints( points ).getCenter( center );
 
 			}
 
-			if ( group !== undefined ) {
+			var maxRadiusSq = 0;
 
-				group.count = ( i * 3 ) - group.start;
-				groups.push( group );
+			for ( var i = 0, il = points.length; i < il; i ++ ) {
+
+				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
 
 			}
 
-			this.groups = groups;
+			this.radius = Math.sqrt( maxRadiusSq );
 
-		},
+			return this;
 
-		fromGeometry: function ( geometry ) {
+		},
 
-			var faces = geometry.faces;
-			var vertices = geometry.vertices;
-			var faceVertexUvs = geometry.faceVertexUvs;
+		clone: function () {
 
-			var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
-			var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
+			return new this.constructor().copy( this );
 
-			// morphs
+		},
 
-			var morphTargets = geometry.morphTargets;
-			var morphTargetsLength = morphTargets.length;
+		copy: function ( sphere ) {
 
-			var morphTargetsPosition;
+			this.center.copy( sphere.center );
+			this.radius = sphere.radius;
 
-			if ( morphTargetsLength > 0 ) {
+			return this;
 
-				morphTargetsPosition = [];
+		},
 
-				for ( var i = 0; i < morphTargetsLength; i ++ ) {
+		isEmpty: function () {
 
-					morphTargetsPosition[ i ] = {
-						name: morphTargets[ i ].name,
-					 	data: []
-					};
+			return ( this.radius < 0 );
 
-				}
+		},
 
-				this.morphTargets.position = morphTargetsPosition;
+		makeEmpty: function () {
 
-			}
+			this.center.set( 0, 0, 0 );
+			this.radius = - 1;
 
-			var morphNormals = geometry.morphNormals;
-			var morphNormalsLength = morphNormals.length;
+			return this;
 
-			var morphTargetsNormal;
+		},
 
-			if ( morphNormalsLength > 0 ) {
+		containsPoint: function ( point ) {
 
-				morphTargetsNormal = [];
+			return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
 
-				for ( var i = 0; i < morphNormalsLength; i ++ ) {
+		},
 
-					morphTargetsNormal[ i ] = {
-						name: morphNormals[ i ].name,
-					 	data: []
-					};
+		distanceToPoint: function ( point ) {
 
-				}
+			return ( point.distanceTo( this.center ) - this.radius );
 
-				this.morphTargets.normal = morphTargetsNormal;
+		},
 
-			}
+		intersectsSphere: function ( sphere ) {
 
-			// skins
+			var radiusSum = this.radius + sphere.radius;
 
-			var skinIndices = geometry.skinIndices;
-			var skinWeights = geometry.skinWeights;
+			return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
 
-			var hasSkinIndices = skinIndices.length === vertices.length;
-			var hasSkinWeights = skinWeights.length === vertices.length;
+		},
 
-			//
+		intersectsBox: function ( box ) {
 
-			if ( vertices.length > 0 && faces.length === 0 ) {
+			return box.intersectsSphere( this );
 
-				console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
+		},
 
-			}
+		intersectsPlane: function ( plane ) {
 
-			for ( var i = 0; i < faces.length; i ++ ) {
+			return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
 
-				var face = faces[ i ];
+		},
 
-				this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
+		clampPoint: function ( point, target ) {
 
-				var vertexNormals = face.vertexNormals;
+			var deltaLengthSq = this.center.distanceToSquared( point );
 
-				if ( vertexNormals.length === 3 ) {
+			if ( target === undefined ) {
 
-					this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
+				console.warn( 'THREE.Sphere: .clampPoint() target is now required' );
+				target = new Vector3();
 
-				} else {
+			}
 
-					var normal = face.normal;
+			target.copy( point );
 
-					this.normals.push( normal, normal, normal );
+			if ( deltaLengthSq > ( this.radius * this.radius ) ) {
 
-				}
+				target.sub( this.center ).normalize();
+				target.multiplyScalar( this.radius ).add( this.center );
 
-				var vertexColors = face.vertexColors;
+			}
 
-				if ( vertexColors.length === 3 ) {
+			return target;
 
-					this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
+		},
 
-				} else {
+		getBoundingBox: function ( target ) {
 
-					var color = face.color;
+			if ( target === undefined ) {
 
-					this.colors.push( color, color, color );
+				console.warn( 'THREE.Sphere: .getBoundingBox() target is now required' );
+				target = new Box3();
 
-				}
+			}
 
-				if ( hasFaceVertexUv === true ) {
+			if ( this.isEmpty() ) {
 
-					var vertexUvs = faceVertexUvs[ 0 ][ i ];
+				// Empty sphere produces empty bounding box
+				target.makeEmpty();
+				return target;
 
-					if ( vertexUvs !== undefined ) {
+			}
 
-						this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
+			target.set( this.center, this.center );
+			target.expandByScalar( this.radius );
 
-					} else {
+			return target;
 
-						console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
+		},
 
-						this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
+		applyMatrix4: function ( matrix ) {
 
-					}
+			this.center.applyMatrix4( matrix );
+			this.radius = this.radius * matrix.getMaxScaleOnAxis();
 
-				}
+			return this;
 
-				if ( hasFaceVertexUv2 === true ) {
+		},
 
-					var vertexUvs = faceVertexUvs[ 1 ][ i ];
+		translate: function ( offset ) {
 
-					if ( vertexUvs !== undefined ) {
+			this.center.add( offset );
 
-						this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
+			return this;
 
-					} else {
+		},
 
-						console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
+		equals: function ( sphere ) {
 
-						this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
+			return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
 
-					}
+		}
 
-				}
+	} );
 
-				// morphs
+	var _vector$2 = new Vector3();
+	var _segCenter = new Vector3();
+	var _segDir = new Vector3();
+	var _diff = new Vector3();
 
-				for ( var j = 0; j < morphTargetsLength; j ++ ) {
+	var _edge1 = new Vector3();
+	var _edge2 = new Vector3();
+	var _normal = new Vector3();
 
-					var morphTarget = morphTargets[ j ].vertices;
+	/**
+	 * @author bhouston / http://clara.io
+	 */
 
-					morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
+	function Ray( origin, direction ) {
 
-				}
+		this.origin = ( origin !== undefined ) ? origin : new Vector3();
+		this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 );
 
-				for ( var j = 0; j < morphNormalsLength; j ++ ) {
+	}
 
-					var morphNormal = morphNormals[ j ].vertexNormals[ i ];
+	Object.assign( Ray.prototype, {
 
-					morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
+		set: function ( origin, direction ) {
 
-				}
+			this.origin.copy( origin );
+			this.direction.copy( direction );
 
-				// skins
+			return this;
 
-				if ( hasSkinIndices ) {
+		},
 
-					this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
+		clone: function () {
 
-				}
+			return new this.constructor().copy( this );
 
-				if ( hasSkinWeights ) {
+		},
 
-					this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
+		copy: function ( ray ) {
 
-				}
+			this.origin.copy( ray.origin );
+			this.direction.copy( ray.direction );
 
-			}
+			return this;
 
-			this.computeGroups( geometry );
+		},
 
-			this.verticesNeedUpdate = geometry.verticesNeedUpdate;
-			this.normalsNeedUpdate = geometry.normalsNeedUpdate;
-			this.colorsNeedUpdate = geometry.colorsNeedUpdate;
-			this.uvsNeedUpdate = geometry.uvsNeedUpdate;
-			this.groupsNeedUpdate = geometry.groupsNeedUpdate;
+		at: function ( t, target ) {
 
-			if ( geometry.boundingSphere !== null ) {
+			if ( target === undefined ) {
 
-				this.boundingSphere = geometry.boundingSphere.clone();
+				console.warn( 'THREE.Ray: .at() target is now required' );
+				target = new Vector3();
 
 			}
 
-			if ( geometry.boundingBox !== null ) {
+			return target.copy( this.direction ).multiplyScalar( t ).add( this.origin );
 
-				this.boundingBox = geometry.boundingBox.clone();
+		},
 
-			}
+		lookAt: function ( v ) {
+
+			this.direction.copy( v ).sub( this.origin ).normalize();
 
 			return this;
 
-		}
+		},
 
-	} );
+		recast: function ( t ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			this.origin.copy( this.at( t, _vector$2 ) );
 
-	function arrayMax( array ) {
+			return this;
 
-		if ( array.length === 0 ) { return - Infinity; }
+		},
 
-		var max = array[ 0 ];
+		closestPointToPoint: function ( point, target ) {
 
-		for ( var i = 1, l = array.length; i < l; ++ i ) {
+			if ( target === undefined ) {
 
-			if ( array[ i ] > max ) { max = array[ i ]; }
+				console.warn( 'THREE.Ray: .closestPointToPoint() target is now required' );
+				target = new Vector3();
 
-		}
+			}
 
-		return max;
+			target.subVectors( point, this.origin );
 
-	}
+			var directionDistance = target.dot( this.direction );
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			if ( directionDistance < 0 ) {
 
-	var _bufferGeometryId = 1; // BufferGeometry uses odd numbers as Id
+				return target.copy( this.origin );
 
-	var _m1$2 = new Matrix4();
-	var _obj = new Object3D();
-	var _offset = new Vector3();
-	var _box$2 = new Box3();
-	var _boxMorphTargets = new Box3();
-	var _vector$4 = new Vector3();
+			}
 
-	function BufferGeometry() {
+			return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
 
-		Object.defineProperty( this, 'id', { value: _bufferGeometryId += 2 } );
+		},
 
-		this.uuid = MathUtils.generateUUID();
+		distanceToPoint: function ( point ) {
 
-		this.name = '';
-		this.type = 'BufferGeometry';
+			return Math.sqrt( this.distanceSqToPoint( point ) );
 
-		this.index = null;
-		this.attributes = {};
+		},
 
-		this.morphAttributes = {};
-		this.morphTargetsRelative = false;
+		distanceSqToPoint: function ( point ) {
 
-		this.groups = [];
+			var directionDistance = _vector$2.subVectors( point, this.origin ).dot( this.direction );
 
-		this.boundingBox = null;
-		this.boundingSphere = null;
+			// point behind the ray
 
-		this.drawRange = { start: 0, count: Infinity };
+			if ( directionDistance < 0 ) {
 
-		this.userData = {};
+				return this.origin.distanceToSquared( point );
 
-	}
+			}
 
-	BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+			_vector$2.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
 
-		constructor: BufferGeometry,
+			return _vector$2.distanceToSquared( point );
 
-		isBufferGeometry: true,
+		},
 
-		getIndex: function () {
+		distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
 
-			return this.index;
+			// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
+			// It returns the min distance between the ray and the segment
+			// defined by v0 and v1
+			// It can also set two optional targets :
+			// - The closest point on the ray
+			// - The closest point on the segment
 
-		},
+			_segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
+			_segDir.copy( v1 ).sub( v0 ).normalize();
+			_diff.copy( this.origin ).sub( _segCenter );
 
-		setIndex: function ( index ) {
+			var segExtent = v0.distanceTo( v1 ) * 0.5;
+			var a01 = - this.direction.dot( _segDir );
+			var b0 = _diff.dot( this.direction );
+			var b1 = - _diff.dot( _segDir );
+			var c = _diff.lengthSq();
+			var det = Math.abs( 1 - a01 * a01 );
+			var s0, s1, sqrDist, extDet;
 
-			if ( Array.isArray( index ) ) {
+			if ( det > 0 ) {
 
-				this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
+				// The ray and segment are not parallel.
 
-			} else {
+				s0 = a01 * b1 - b0;
+				s1 = a01 * b0 - b1;
+				extDet = segExtent * det;
 
-				this.index = index;
+				if ( s0 >= 0 ) {
 
-			}
+					if ( s1 >= - extDet ) {
 
-		},
+						if ( s1 <= extDet ) {
 
-		getAttribute: function ( name ) {
+							// region 0
+							// Minimum at interior points of ray and segment.
 
-			return this.attributes[ name ];
+							var invDet = 1 / det;
+							s0 *= invDet;
+							s1 *= invDet;
+							sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
 
-		},
+						} else {
 
-		setAttribute: function ( name, attribute ) {
+							// region 1
 
-			this.attributes[ name ] = attribute;
+							s1 = segExtent;
+							s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
+							sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
 
-			return this;
+						}
 
-		},
+					} else {
 
-		deleteAttribute: function ( name ) {
+						// region 5
 
-			delete this.attributes[ name ];
+						s1 = - segExtent;
+						s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
+						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
 
-			return this;
+					}
 
-		},
+				} else {
 
-		addGroup: function ( start, count, materialIndex ) {
+					if ( s1 <= - extDet ) {
 
-			this.groups.push( {
+						// region 4
 
-				start: start,
-				count: count,
-				materialIndex: materialIndex !== undefined ? materialIndex : 0
+						s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
+						s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
+						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
 
-			} );
+					} else if ( s1 <= extDet ) {
 
-		},
+						// region 3
 
-		clearGroups: function () {
+						s0 = 0;
+						s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
+						sqrDist = s1 * ( s1 + 2 * b1 ) + c;
 
-			this.groups = [];
+					} else {
 
-		},
+						// region 2
 
-		setDrawRange: function ( start, count ) {
-
-			this.drawRange.start = start;
-			this.drawRange.count = count;
-
-		},
+						s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
+						s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
+						sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
 
-		applyMatrix4: function ( matrix ) {
+					}
 
-			var position = this.attributes.position;
+				}
 
-			if ( position !== undefined ) {
+			} else {
 
-				position.applyMatrix4( matrix );
+				// Ray and segment are parallel.
 
-				position.needsUpdate = true;
+				s1 = ( a01 > 0 ) ? - segExtent : segExtent;
+				s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
+				sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
 
 			}
 
-			var normal = this.attributes.normal;
+			if ( optionalPointOnRay ) {
 
-			if ( normal !== undefined ) {
+				optionalPointOnRay.copy( this.direction ).multiplyScalar( s0 ).add( this.origin );
 
-				var normalMatrix = new Matrix3().getNormalMatrix( matrix );
+			}
 
-				normal.applyNormalMatrix( normalMatrix );
+			if ( optionalPointOnSegment ) {
 
-				normal.needsUpdate = true;
+				optionalPointOnSegment.copy( _segDir ).multiplyScalar( s1 ).add( _segCenter );
 
 			}
 
-			var tangent = this.attributes.tangent;
+			return sqrDist;
 
-			if ( tangent !== undefined ) {
+		},
 
-				tangent.transformDirection( matrix );
+		intersectSphere: function ( sphere, target ) {
 
-				tangent.needsUpdate = true;
+			_vector$2.subVectors( sphere.center, this.origin );
+			var tca = _vector$2.dot( this.direction );
+			var d2 = _vector$2.dot( _vector$2 ) - tca * tca;
+			var radius2 = sphere.radius * sphere.radius;
 
-			}
+			if ( d2 > radius2 ) { return null; }
 
-			if ( this.boundingBox !== null ) {
+			var thc = Math.sqrt( radius2 - d2 );
 
-				this.computeBoundingBox();
+			// t0 = first intersect point - entrance on front of sphere
+			var t0 = tca - thc;
 
-			}
+			// t1 = second intersect point - exit point on back of sphere
+			var t1 = tca + thc;
 
-			if ( this.boundingSphere !== null ) {
+			// test to see if both t0 and t1 are behind the ray - if so, return null
+			if ( t0 < 0 && t1 < 0 ) { return null; }
 
-				this.computeBoundingSphere();
+			// test to see if t0 is behind the ray:
+			// if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
+			// in order to always return an intersect point that is in front of the ray.
+			if ( t0 < 0 ) { return this.at( t1, target ); }
 
-			}
+			// else t0 is in front of the ray, so return the first collision point scaled by t0
+			return this.at( t0, target );
 
-			return this;
+		},
+
+		intersectsSphere: function ( sphere ) {
+
+			return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
 
 		},
 
-		rotateX: function ( angle ) {
+		distanceToPlane: function ( plane ) {
 
-			// rotate geometry around world x-axis
+			var denominator = plane.normal.dot( this.direction );
 
-			_m1$2.makeRotationX( angle );
+			if ( denominator === 0 ) {
 
-			this.applyMatrix4( _m1$2 );
+				// line is coplanar, return origin
+				if ( plane.distanceToPoint( this.origin ) === 0 ) {
 
-			return this;
+					return 0;
 
-		},
+				}
 
-		rotateY: function ( angle ) {
+				// Null is preferable to undefined since undefined means.... it is undefined
 
-			// rotate geometry around world y-axis
+				return null;
 
-			_m1$2.makeRotationY( angle );
+			}
 
-			this.applyMatrix4( _m1$2 );
+			var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
 
-			return this;
+			// Return if the ray never intersects the plane
+
+			return t >= 0 ? t : null;
 
 		},
 
-		rotateZ: function ( angle ) {
+		intersectPlane: function ( plane, target ) {
 
-			// rotate geometry around world z-axis
+			var t = this.distanceToPlane( plane );
 
-			_m1$2.makeRotationZ( angle );
+			if ( t === null ) {
 
-			this.applyMatrix4( _m1$2 );
+				return null;
 
-			return this;
+			}
+
+			return this.at( t, target );
 
 		},
 
-		translate: function ( x, y, z ) {
+		intersectsPlane: function ( plane ) {
 
-			// translate geometry
+			// check if the ray lies on the plane first
 
-			_m1$2.makeTranslation( x, y, z );
+			var distToPoint = plane.distanceToPoint( this.origin );
 
-			this.applyMatrix4( _m1$2 );
+			if ( distToPoint === 0 ) {
 
-			return this;
+				return true;
 
-		},
+			}
 
-		scale: function ( x, y, z ) {
+			var denominator = plane.normal.dot( this.direction );
 
-			// scale geometry
+			if ( denominator * distToPoint < 0 ) {
 
-			_m1$2.makeScale( x, y, z );
+				return true;
 
-			this.applyMatrix4( _m1$2 );
+			}
 
-			return this;
+			// ray origin is behind the plane (and is pointing behind it)
+
+			return false;
 
 		},
 
-		lookAt: function ( vector ) {
+		intersectBox: function ( box, target ) {
 
-			_obj.lookAt( vector );
+			var tmin, tmax, tymin, tymax, tzmin, tzmax;
 
-			_obj.updateMatrix();
+			var invdirx = 1 / this.direction.x,
+				invdiry = 1 / this.direction.y,
+				invdirz = 1 / this.direction.z;
 
-			this.applyMatrix4( _obj.matrix );
+			var origin = this.origin;
 
-			return this;
+			if ( invdirx >= 0 ) {
 
-		},
+				tmin = ( box.min.x - origin.x ) * invdirx;
+				tmax = ( box.max.x - origin.x ) * invdirx;
 
-		center: function () {
+			} else {
 
-			this.computeBoundingBox();
+				tmin = ( box.max.x - origin.x ) * invdirx;
+				tmax = ( box.min.x - origin.x ) * invdirx;
 
-			this.boundingBox.getCenter( _offset ).negate();
+			}
 
-			this.translate( _offset.x, _offset.y, _offset.z );
+			if ( invdiry >= 0 ) {
 
-			return this;
+				tymin = ( box.min.y - origin.y ) * invdiry;
+				tymax = ( box.max.y - origin.y ) * invdiry;
 
-		},
+			} else {
 
-		setFromObject: function ( object ) {
+				tymin = ( box.max.y - origin.y ) * invdiry;
+				tymax = ( box.min.y - origin.y ) * invdiry;
 
-			// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
+			}
 
-			var geometry = object.geometry;
+			if ( ( tmin > tymax ) || ( tymin > tmax ) ) { return null; }
 
-			if ( object.isPoints || object.isLine ) {
+			// These lines also handle the case where tmin or tmax is NaN
+			// (result of 0 * Infinity). x !== x returns true if x is NaN
 
-				var positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
-				var colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
+			if ( tymin > tmin || tmin !== tmin ) { tmin = tymin; }
 
-				this.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
-				this.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
+			if ( tymax < tmax || tmax !== tmax ) { tmax = tymax; }
 
-				if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
+			if ( invdirz >= 0 ) {
 
-					var lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
+				tzmin = ( box.min.z - origin.z ) * invdirz;
+				tzmax = ( box.max.z - origin.z ) * invdirz;
 
-					this.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
+			} else {
 
-				}
+				tzmin = ( box.max.z - origin.z ) * invdirz;
+				tzmax = ( box.min.z - origin.z ) * invdirz;
 
-				if ( geometry.boundingSphere !== null ) {
+			}
 
-					this.boundingSphere = geometry.boundingSphere.clone();
+			if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) { return null; }
 
-				}
+			if ( tzmin > tmin || tmin !== tmin ) { tmin = tzmin; }
 
-				if ( geometry.boundingBox !== null ) {
+			if ( tzmax < tmax || tmax !== tmax ) { tmax = tzmax; }
 
-					this.boundingBox = geometry.boundingBox.clone();
+			//return point closest to the ray (positive side)
 
-				}
+			if ( tmax < 0 ) { return null; }
 
-			} else if ( object.isMesh ) {
+			return this.at( tmin >= 0 ? tmin : tmax, target );
 
-				if ( geometry && geometry.isGeometry ) {
+		},
 
-					this.fromGeometry( geometry );
+		intersectsBox: function ( box ) {
 
-				}
+			return this.intersectBox( box, _vector$2 ) !== null;
 
-			}
+		},
 
-			return this;
+		intersectTriangle: function ( a, b, c, backfaceCulling, target ) {
 
-		},
+			// Compute the offset origin, edges, and normal.
 
-		setFromPoints: function ( points ) {
+			// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
 
-			var position = [];
+			_edge1.subVectors( b, a );
+			_edge2.subVectors( c, a );
+			_normal.crossVectors( _edge1, _edge2 );
 
-			for ( var i = 0, l = points.length; i < l; i ++ ) {
+			// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
+			// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
+			//   |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
+			//   |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
+			//   |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
+			var DdN = this.direction.dot( _normal );
+			var sign;
 
-				var point = points[ i ];
-				position.push( point.x, point.y, point.z || 0 );
+			if ( DdN > 0 ) {
 
-			}
+				if ( backfaceCulling ) { return null; }
+				sign = 1;
 
-			this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
+			} else if ( DdN < 0 ) {
 
-			return this;
+				sign = - 1;
+				DdN = - DdN;
 
-		},
+			} else {
 
-		updateFromObject: function ( object ) {
+				return null;
 
-			var geometry = object.geometry;
+			}
 
-			if ( object.isMesh ) {
+			_diff.subVectors( this.origin, a );
+			var DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) );
 
-				var direct = geometry.__directGeometry;
+			// b1 < 0, no intersection
+			if ( DdQxE2 < 0 ) {
 
-				if ( geometry.elementsNeedUpdate === true ) {
+				return null;
 
-					direct = undefined;
-					geometry.elementsNeedUpdate = false;
+			}
 
-				}
+			var DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) );
 
-				if ( direct === undefined ) {
+			// b2 < 0, no intersection
+			if ( DdE1xQ < 0 ) {
 
-					return this.fromGeometry( geometry );
+				return null;
 
-				}
+			}
 
-				direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
-				direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
-				direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
-				direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
-				direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
+			// b1+b2 > 1, no intersection
+			if ( DdQxE2 + DdE1xQ > DdN ) {
 
-				geometry.verticesNeedUpdate = false;
-				geometry.normalsNeedUpdate = false;
-				geometry.colorsNeedUpdate = false;
-				geometry.uvsNeedUpdate = false;
-				geometry.groupsNeedUpdate = false;
+				return null;
 
-				geometry = direct;
+			}
+
+			// Line intersects triangle, check if ray does.
+			var QdN = - sign * _diff.dot( _normal );
+
+			// t < 0, no intersection
+			if ( QdN < 0 ) {
+
+				return null;
 
 			}
 
-			var attribute;
+			// Ray intersects triangle.
+			return this.at( QdN / DdN, target );
 
-			if ( geometry.verticesNeedUpdate === true ) {
+		},
 
-				attribute = this.attributes.position;
+		applyMatrix4: function ( matrix4 ) {
 
-				if ( attribute !== undefined ) {
+			this.origin.applyMatrix4( matrix4 );
+			this.direction.transformDirection( matrix4 );
 
-					attribute.copyVector3sArray( geometry.vertices );
-					attribute.needsUpdate = true;
+			return this;
 
-				}
+		},
 
-				geometry.verticesNeedUpdate = false;
+		equals: function ( ray ) {
 
-			}
+			return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
 
-			if ( geometry.normalsNeedUpdate === true ) {
+		}
 
-				attribute = this.attributes.normal;
+	} );
 
-				if ( attribute !== undefined ) {
+	/**
+	 * @author bhouston / http://clara.io
+	 */
 
-					attribute.copyVector3sArray( geometry.normals );
-					attribute.needsUpdate = true;
+	var _vector1 = new Vector3();
+	var _vector2 = new Vector3();
+	var _normalMatrix = new Matrix3();
 
-				}
+	function Plane( normal, constant ) {
 
-				geometry.normalsNeedUpdate = false;
+		// normal is assumed to be normalized
 
-			}
+		this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 );
+		this.constant = ( constant !== undefined ) ? constant : 0;
 
-			if ( geometry.colorsNeedUpdate === true ) {
+	}
 
-				attribute = this.attributes.color;
+	Object.assign( Plane.prototype, {
 
-				if ( attribute !== undefined ) {
+		isPlane: true,
 
-					attribute.copyColorsArray( geometry.colors );
-					attribute.needsUpdate = true;
+		set: function ( normal, constant ) {
 
-				}
+			this.normal.copy( normal );
+			this.constant = constant;
 
-				geometry.colorsNeedUpdate = false;
+			return this;
 
-			}
+		},
 
-			if ( geometry.uvsNeedUpdate ) {
+		setComponents: function ( x, y, z, w ) {
 
-				attribute = this.attributes.uv;
+			this.normal.set( x, y, z );
+			this.constant = w;
 
-				if ( attribute !== undefined ) {
+			return this;
 
-					attribute.copyVector2sArray( geometry.uvs );
-					attribute.needsUpdate = true;
+		},
 
-				}
+		setFromNormalAndCoplanarPoint: function ( normal, point ) {
 
-				geometry.uvsNeedUpdate = false;
+			this.normal.copy( normal );
+			this.constant = - point.dot( this.normal );
 
-			}
+			return this;
 
-			if ( geometry.lineDistancesNeedUpdate ) {
+		},
 
-				attribute = this.attributes.lineDistance;
+		setFromCoplanarPoints: function ( a, b, c ) {
 
-				if ( attribute !== undefined ) {
+			var normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
 
-					attribute.copyArray( geometry.lineDistances );
-					attribute.needsUpdate = true;
+			// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
 
-				}
+			this.setFromNormalAndCoplanarPoint( normal, a );
 
-				geometry.lineDistancesNeedUpdate = false;
+			return this;
 
-			}
+		},
 
-			if ( geometry.groupsNeedUpdate ) {
+		clone: function () {
 
-				geometry.computeGroups( object.geometry );
-				this.groups = geometry.groups;
+			return new this.constructor().copy( this );
 
-				geometry.groupsNeedUpdate = false;
+		},
 
-			}
+		copy: function ( plane ) {
+
+			this.normal.copy( plane.normal );
+			this.constant = plane.constant;
 
 			return this;
 
 		},
 
-		fromGeometry: function ( geometry ) {
+		normalize: function () {
 
-			geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
+			// Note: will lead to a divide by zero if the plane is invalid.
 
-			return this.fromDirectGeometry( geometry.__directGeometry );
+			var inverseNormalLength = 1.0 / this.normal.length();
+			this.normal.multiplyScalar( inverseNormalLength );
+			this.constant *= inverseNormalLength;
+
+			return this;
 
 		},
 
-		fromDirectGeometry: function ( geometry ) {
+		negate: function () {
 
-			var positions = new Float32Array( geometry.vertices.length * 3 );
-			this.setAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
+			this.constant *= - 1;
+			this.normal.negate();
 
-			if ( geometry.normals.length > 0 ) {
+			return this;
 
-				var normals = new Float32Array( geometry.normals.length * 3 );
-				this.setAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
+		},
 
-			}
+		distanceToPoint: function ( point ) {
 
-			if ( geometry.colors.length > 0 ) {
+			return this.normal.dot( point ) + this.constant;
 
-				var colors = new Float32Array( geometry.colors.length * 3 );
-				this.setAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
+		},
 
-			}
+		distanceToSphere: function ( sphere ) {
 
-			if ( geometry.uvs.length > 0 ) {
+			return this.distanceToPoint( sphere.center ) - sphere.radius;
 
-				var uvs = new Float32Array( geometry.uvs.length * 2 );
-				this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
+		},
 
-			}
+		projectPoint: function ( point, target ) {
 
-			if ( geometry.uvs2.length > 0 ) {
+			if ( target === undefined ) {
 
-				var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
-				this.setAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
+				console.warn( 'THREE.Plane: .projectPoint() target is now required' );
+				target = new Vector3();
 
 			}
 
-			// groups
+			return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point );
 
-			this.groups = geometry.groups;
+		},
 
-			// morphs
+		intersectLine: function ( line, target ) {
 
-			for ( var name in geometry.morphTargets ) {
+			if ( target === undefined ) {
 
-				var array = [];
-				var morphTargets = geometry.morphTargets[ name ];
+				console.warn( 'THREE.Plane: .intersectLine() target is now required' );
+				target = new Vector3();
 
-				for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
+			}
 
-					var morphTarget = morphTargets[ i ];
+			var direction = line.delta( _vector1 );
 
-					var attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 );
-					attribute.name = morphTarget.name;
+			var denominator = this.normal.dot( direction );
 
-					array.push( attribute.copyVector3sArray( morphTarget.data ) );
+			if ( denominator === 0 ) {
+
+				// line is coplanar, return origin
+				if ( this.distanceToPoint( line.start ) === 0 ) {
+
+					return target.copy( line.start );
 
 				}
 
-				this.morphAttributes[ name ] = array;
+				// Unsure if this is the correct method to handle this case.
+				return undefined;
 
 			}
 
-			// skinning
+			var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
 
-			if ( geometry.skinIndices.length > 0 ) {
+			if ( t < 0 || t > 1 ) {
 
-				var skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
-				this.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
+				return undefined;
 
 			}
 
-			if ( geometry.skinWeights.length > 0 ) {
+			return target.copy( direction ).multiplyScalar( t ).add( line.start );
 
-				var skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
-				this.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
+		},
 
-			}
+		intersectsLine: function ( line ) {
 
-			//
+			// Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
 
-			if ( geometry.boundingSphere !== null ) {
+			var startSign = this.distanceToPoint( line.start );
+			var endSign = this.distanceToPoint( line.end );
 
-				this.boundingSphere = geometry.boundingSphere.clone();
+			return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
 
-			}
+		},
 
-			if ( geometry.boundingBox !== null ) {
+		intersectsBox: function ( box ) {
 
-				this.boundingBox = geometry.boundingBox.clone();
+			return box.intersectsPlane( this );
 
-			}
+		},
 
-			return this;
+		intersectsSphere: function ( sphere ) {
+
+			return sphere.intersectsPlane( this );
 
 		},
 
-		computeBoundingBox: function () {
+		coplanarPoint: function ( target ) {
 
-			if ( this.boundingBox === null ) {
+			if ( target === undefined ) {
 
-				this.boundingBox = new Box3();
+				console.warn( 'THREE.Plane: .coplanarPoint() target is now required' );
+				target = new Vector3();
 
 			}
 
-			var position = this.attributes.position;
-			var morphAttributesPosition = this.morphAttributes.position;
+			return target.copy( this.normal ).multiplyScalar( - this.constant );
 
-			if ( position !== undefined ) {
+		},
 
-				this.boundingBox.setFromBufferAttribute( position );
+		applyMatrix4: function ( matrix, optionalNormalMatrix ) {
 
-				// process morph attributes if present
+			var normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
 
-				if ( morphAttributesPosition ) {
+			var referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
 
-					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
+			var normal = this.normal.applyMatrix3( normalMatrix ).normalize();
 
-						var morphAttribute = morphAttributesPosition[ i ];
-						_box$2.setFromBufferAttribute( morphAttribute );
+			this.constant = - referencePoint.dot( normal );
 
-						if ( this.morphTargetsRelative ) {
+			return this;
 
-							_vector$4.addVectors( this.boundingBox.min, _box$2.min );
-							this.boundingBox.expandByPoint( _vector$4 );
+		},
 
-							_vector$4.addVectors( this.boundingBox.max, _box$2.max );
-							this.boundingBox.expandByPoint( _vector$4 );
+		translate: function ( offset ) {
 
-						} else {
+			this.constant -= offset.dot( this.normal );
 
-							this.boundingBox.expandByPoint( _box$2.min );
-							this.boundingBox.expandByPoint( _box$2.max );
+			return this;
 
-						}
+		},
 
-					}
+		equals: function ( plane ) {
 
-				}
+			return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
 
-			} else {
+		}
 
-				this.boundingBox.makeEmpty();
+	} );
 
-			}
+	/**
+	 * @author bhouston / http://clara.io
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
+	var _v0$1 = new Vector3();
+	var _v1$3 = new Vector3();
+	var _v2$1 = new Vector3();
+	var _v3 = new Vector3();
 
-				console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
+	var _vab = new Vector3();
+	var _vac = new Vector3();
+	var _vbc = new Vector3();
+	var _vap = new Vector3();
+	var _vbp = new Vector3();
+	var _vcp = new Vector3();
 
-			}
+	function Triangle( a, b, c ) {
 
-		},
+		this.a = ( a !== undefined ) ? a : new Vector3();
+		this.b = ( b !== undefined ) ? b : new Vector3();
+		this.c = ( c !== undefined ) ? c : new Vector3();
 
-		computeBoundingSphere: function () {
+	}
 
-			if ( this.boundingSphere === null ) {
+	Object.assign( Triangle, {
 
-				this.boundingSphere = new Sphere();
+		getNormal: function ( a, b, c, target ) {
 
-			}
+			if ( target === undefined ) {
 
-			var position = this.attributes.position;
-			var morphAttributesPosition = this.morphAttributes.position;
+				console.warn( 'THREE.Triangle: .getNormal() target is now required' );
+				target = new Vector3();
 
-			if ( position ) {
+			}
 
-				// first, find the center of the bounding sphere
+			target.subVectors( c, b );
+			_v0$1.subVectors( a, b );
+			target.cross( _v0$1 );
 
-				var center = this.boundingSphere.center;
+			var targetLengthSq = target.lengthSq();
+			if ( targetLengthSq > 0 ) {
 
-				_box$2.setFromBufferAttribute( position );
+				return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
 
-				// process morph attributes if present
+			}
 
-				if ( morphAttributesPosition ) {
+			return target.set( 0, 0, 0 );
 
-					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
+		},
 
-						var morphAttribute = morphAttributesPosition[ i ];
-						_boxMorphTargets.setFromBufferAttribute( morphAttribute );
+		// static/instance method to calculate barycentric coordinates
+		// based on: http://www.blackpawn.com/texts/pointinpoly/default.html
+		getBarycoord: function ( point, a, b, c, target ) {
 
-						if ( this.morphTargetsRelative ) {
+			_v0$1.subVectors( c, a );
+			_v1$3.subVectors( b, a );
+			_v2$1.subVectors( point, a );
 
-							_vector$4.addVectors( _box$2.min, _boxMorphTargets.min );
-							_box$2.expandByPoint( _vector$4 );
+			var dot00 = _v0$1.dot( _v0$1 );
+			var dot01 = _v0$1.dot( _v1$3 );
+			var dot02 = _v0$1.dot( _v2$1 );
+			var dot11 = _v1$3.dot( _v1$3 );
+			var dot12 = _v1$3.dot( _v2$1 );
 
-							_vector$4.addVectors( _box$2.max, _boxMorphTargets.max );
-							_box$2.expandByPoint( _vector$4 );
+			var denom = ( dot00 * dot11 - dot01 * dot01 );
 
-						} else {
+			if ( target === undefined ) {
 
-							_box$2.expandByPoint( _boxMorphTargets.min );
-							_box$2.expandByPoint( _boxMorphTargets.max );
+				console.warn( 'THREE.Triangle: .getBarycoord() target is now required' );
+				target = new Vector3();
 
-						}
+			}
 
-					}
+			// collinear or singular triangle
+			if ( denom === 0 ) {
 
-				}
+				// arbitrary location outside of triangle?
+				// not sure if this is the best idea, maybe should be returning undefined
+				return target.set( - 2, - 1, - 1 );
 
-				_box$2.getCenter( center );
+			}
 
-				// second, try to find a boundingSphere with a radius smaller than the
-				// boundingSphere of the boundingBox: sqrt(3) smaller in the best case
+			var invDenom = 1 / denom;
+			var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
+			var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
 
-				var maxRadiusSq = 0;
+			// barycentric coordinates must always sum to 1
+			return target.set( 1 - u - v, v, u );
 
-				for ( var i = 0, il = position.count; i < il; i ++ ) {
+		},
 
-					_vector$4.fromBufferAttribute( position, i );
+		containsPoint: function ( point, a, b, c ) {
 
-					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) );
+			Triangle.getBarycoord( point, a, b, c, _v3 );
 
-				}
+			return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 );
 
-				// process morph attributes if present
+		},
 
-				if ( morphAttributesPosition ) {
+		getUV: function ( point, p1, p2, p3, uv1, uv2, uv3, target ) {
 
-					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
+			this.getBarycoord( point, p1, p2, p3, _v3 );
 
-						var morphAttribute = morphAttributesPosition[ i ];
-						var morphTargetsRelative = this.morphTargetsRelative;
+			target.set( 0, 0 );
+			target.addScaledVector( uv1, _v3.x );
+			target.addScaledVector( uv2, _v3.y );
+			target.addScaledVector( uv3, _v3.z );
 
-						for ( var j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
+			return target;
 
-							_vector$4.fromBufferAttribute( morphAttribute, j );
+		},
 
-							if ( morphTargetsRelative ) {
+		isFrontFacing: function ( a, b, c, direction ) {
 
-								_offset.fromBufferAttribute( position, j );
-								_vector$4.add( _offset );
+			_v0$1.subVectors( c, b );
+			_v1$3.subVectors( a, b );
 
-							}
+			// strictly front facing
+			return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false;
 
-							maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) );
+		}
 
-						}
+	} );
 
-					}
+	Object.assign( Triangle.prototype, {
 
-				}
+		set: function ( a, b, c ) {
 
-				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
+			this.a.copy( a );
+			this.b.copy( b );
+			this.c.copy( c );
 
-				if ( isNaN( this.boundingSphere.radius ) ) {
+			return this;
 
-					console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
+		},
 
-				}
+		setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
 
-			}
+			this.a.copy( points[ i0 ] );
+			this.b.copy( points[ i1 ] );
+			this.c.copy( points[ i2 ] );
+
+			return this;
 
 		},
 
-		computeFaceNormals: function () {
+		clone: function () {
 
-			// backwards compatibility
+			return new this.constructor().copy( this );
 
 		},
 
-		computeVertexNormals: function () {
-
-			var index = this.index;
-			var attributes = this.attributes;
-
-			if ( attributes.position ) {
+		copy: function ( triangle ) {
 
-				var positions = attributes.position.array;
+			this.a.copy( triangle.a );
+			this.b.copy( triangle.b );
+			this.c.copy( triangle.c );
 
-				if ( attributes.normal === undefined ) {
+			return this;
 
-					this.setAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
+		},
 
-				} else {
+		getArea: function () {
 
-					// reset existing normals to zero
+			_v0$1.subVectors( this.c, this.b );
+			_v1$3.subVectors( this.a, this.b );
 
-					var array = attributes.normal.array;
+			return _v0$1.cross( _v1$3 ).length() * 0.5;
 
-					for ( var i = 0, il = array.length; i < il; i ++ ) {
+		},
 
-						array[ i ] = 0;
+		getMidpoint: function ( target ) {
 
-					}
+			if ( target === undefined ) {
 
-				}
+				console.warn( 'THREE.Triangle: .getMidpoint() target is now required' );
+				target = new Vector3();
 
-				var normals = attributes.normal.array;
+			}
 
-				var vA, vB, vC;
-				var pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
-				var cb = new Vector3(), ab = new Vector3();
+			return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
 
-				// indexed elements
+		},
 
-				if ( index ) {
+		getNormal: function ( target ) {
 
-					var indices = index.array;
+			return Triangle.getNormal( this.a, this.b, this.c, target );
 
-					for ( var i = 0, il = index.count; i < il; i += 3 ) {
+		},
 
-						vA = indices[ i + 0 ] * 3;
-						vB = indices[ i + 1 ] * 3;
-						vC = indices[ i + 2 ] * 3;
+		getPlane: function ( target ) {
 
-						pA.fromArray( positions, vA );
-						pB.fromArray( positions, vB );
-						pC.fromArray( positions, vC );
+			if ( target === undefined ) {
 
-						cb.subVectors( pC, pB );
-						ab.subVectors( pA, pB );
-						cb.cross( ab );
+				console.warn( 'THREE.Triangle: .getPlane() target is now required' );
+				target = new Plane();
 
-						normals[ vA ] += cb.x;
-						normals[ vA + 1 ] += cb.y;
-						normals[ vA + 2 ] += cb.z;
+			}
 
-						normals[ vB ] += cb.x;
-						normals[ vB + 1 ] += cb.y;
-						normals[ vB + 2 ] += cb.z;
+			return target.setFromCoplanarPoints( this.a, this.b, this.c );
 
-						normals[ vC ] += cb.x;
-						normals[ vC + 1 ] += cb.y;
-						normals[ vC + 2 ] += cb.z;
+		},
 
-					}
+		getBarycoord: function ( point, target ) {
 
-				} else {
+			return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
 
-					// non-indexed elements (unconnected triangle soup)
+		},
 
-					for ( var i = 0, il = positions.length; i < il; i += 9 ) {
+		getUV: function ( point, uv1, uv2, uv3, target ) {
 
-						pA.fromArray( positions, i );
-						pB.fromArray( positions, i + 3 );
-						pC.fromArray( positions, i + 6 );
+			return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, target );
 
-						cb.subVectors( pC, pB );
-						ab.subVectors( pA, pB );
-						cb.cross( ab );
+		},
 
-						normals[ i ] = cb.x;
-						normals[ i + 1 ] = cb.y;
-						normals[ i + 2 ] = cb.z;
+		containsPoint: function ( point ) {
 
-						normals[ i + 3 ] = cb.x;
-						normals[ i + 4 ] = cb.y;
-						normals[ i + 5 ] = cb.z;
+			return Triangle.containsPoint( point, this.a, this.b, this.c );
 
-						normals[ i + 6 ] = cb.x;
-						normals[ i + 7 ] = cb.y;
-						normals[ i + 8 ] = cb.z;
+		},
 
-					}
+		isFrontFacing: function ( direction ) {
 
-				}
+			return Triangle.isFrontFacing( this.a, this.b, this.c, direction );
 
-				this.normalizeNormals();
+		},
 
-				attributes.normal.needsUpdate = true;
+		intersectsBox: function ( box ) {
 
-			}
+			return box.intersectsTriangle( this );
 
 		},
 
-		merge: function ( geometry, offset ) {
+		closestPointToPoint: function ( p, target ) {
 
-			if ( ! ( geometry && geometry.isBufferGeometry ) ) {
+			if ( target === undefined ) {
 
-				console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
-				return;
+				console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
+				target = new Vector3();
 
 			}
 
-			if ( offset === undefined ) {
+			var a = this.a, b = this.b, c = this.c;
+			var v, w;
 
-				offset = 0;
+			// algorithm thanks to Real-Time Collision Detection by Christer Ericson,
+			// published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
+			// under the accompanying license; see chapter 5.1.5 for detailed explanation.
+			// basically, we're distinguishing which of the voronoi regions of the triangle
+			// the point lies in with the minimum amount of redundant computation.
 
-				console.warn(
-					'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. '
-					+ 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.'
-				);
+			_vab.subVectors( b, a );
+			_vac.subVectors( c, a );
+			_vap.subVectors( p, a );
+			var d1 = _vab.dot( _vap );
+			var d2 = _vac.dot( _vap );
+			if ( d1 <= 0 && d2 <= 0 ) {
 
-			}
+				// vertex region of A; barycentric coords (1, 0, 0)
+				return target.copy( a );
 
-			var attributes = this.attributes;
+			}
 
-			for ( var key in attributes ) {
+			_vbp.subVectors( p, b );
+			var d3 = _vab.dot( _vbp );
+			var d4 = _vac.dot( _vbp );
+			if ( d3 >= 0 && d4 <= d3 ) {
 
-				if ( geometry.attributes[ key ] === undefined ) { continue; }
+				// vertex region of B; barycentric coords (0, 1, 0)
+				return target.copy( b );
 
-				var attribute1 = attributes[ key ];
-				var attributeArray1 = attribute1.array;
+			}
 
-				var attribute2 = geometry.attributes[ key ];
-				var attributeArray2 = attribute2.array;
+			var vc = d1 * d4 - d3 * d2;
+			if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
 
-				var attributeOffset = attribute2.itemSize * offset;
-				var length = Math.min( attributeArray2.length, attributeArray1.length - attributeOffset );
+				v = d1 / ( d1 - d3 );
+				// edge region of AB; barycentric coords (1-v, v, 0)
+				return target.copy( a ).addScaledVector( _vab, v );
 
-				for ( var i = 0, j = attributeOffset; i < length; i ++, j ++ ) {
+			}
 
-					attributeArray1[ j ] = attributeArray2[ i ];
+			_vcp.subVectors( p, c );
+			var d5 = _vab.dot( _vcp );
+			var d6 = _vac.dot( _vcp );
+			if ( d6 >= 0 && d5 <= d6 ) {
 
-				}
+				// vertex region of C; barycentric coords (0, 0, 1)
+				return target.copy( c );
 
 			}
 
-			return this;
-
-		},
+			var vb = d5 * d2 - d1 * d6;
+			if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
 
-		normalizeNormals: function () {
+				w = d2 / ( d2 - d6 );
+				// edge region of AC; barycentric coords (1-w, 0, w)
+				return target.copy( a ).addScaledVector( _vac, w );
 
-			var normals = this.attributes.normal;
+			}
 
-			for ( var i = 0, il = normals.count; i < il; i ++ ) {
+			var va = d3 * d6 - d5 * d4;
+			if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
 
-				_vector$4.x = normals.getX( i );
-				_vector$4.y = normals.getY( i );
-				_vector$4.z = normals.getZ( i );
+				_vbc.subVectors( c, b );
+				w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
+				// edge region of BC; barycentric coords (0, 1-w, w)
+				return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC
 
-				_vector$4.normalize();
+			}
 
-				normals.setXYZ( i, _vector$4.x, _vector$4.y, _vector$4.z );
+			// face region
+			var denom = 1 / ( va + vb + vc );
+			// u = va * denom
+			v = vb * denom;
+			w = vc * denom;
 
-			}
+			return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w );
 
 		},
 
-		toNonIndexed: function () {
+		equals: function ( triangle ) {
 
-			function convertBufferAttribute( attribute, indices ) {
+			return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
 
-				var array = attribute.array;
-				var itemSize = attribute.itemSize;
+		}
 
-				var array2 = new array.constructor( indices.length * itemSize );
+	} );
 
-				var index = 0, index2 = 0;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				for ( var i = 0, l = indices.length; i < l; i ++ ) {
+	var _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
+		'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
+		'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
+		'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
+		'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
+		'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
+		'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
+		'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
+		'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
+		'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
+		'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
+		'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
+		'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
+		'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
+		'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
+		'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
+		'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
+		'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
+		'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
+		'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
+		'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
+		'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
+		'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
+		'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
 
-					index = indices[ i ] * itemSize;
+	var _hslA = { h: 0, s: 0, l: 0 };
+	var _hslB = { h: 0, s: 0, l: 0 };
 
-					for ( var j = 0; j < itemSize; j ++ ) {
+	function Color( r, g, b ) {
 
-						array2[ index2 ++ ] = array[ index ++ ];
+		if ( g === undefined && b === undefined ) {
 
-					}
+			// r is THREE.Color, hex or string
+			return this.set( r );
 
-				}
+		}
 
-				return new BufferAttribute( array2, itemSize );
+		return this.setRGB( r, g, b );
 
-			}
+	}
 
-			//
+	function hue2rgb( p, q, t ) {
 
-			if ( this.index === null ) {
+		if ( t < 0 ) { t += 1; }
+		if ( t > 1 ) { t -= 1; }
+		if ( t < 1 / 6 ) { return p + ( q - p ) * 6 * t; }
+		if ( t < 1 / 2 ) { return q; }
+		if ( t < 2 / 3 ) { return p + ( q - p ) * 6 * ( 2 / 3 - t ); }
+		return p;
 
-				console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
-				return this;
+	}
 
-			}
+	function SRGBToLinear( c ) {
 
-			var geometry2 = new BufferGeometry();
+		return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
 
-			var indices = this.index.array;
-			var attributes = this.attributes;
+	}
 
-			// attributes
+	function LinearToSRGB( c ) {
 
-			for ( var name in attributes ) {
+		return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
 
-				var attribute = attributes[ name ];
+	}
 
-				var newAttribute = convertBufferAttribute( attribute, indices );
+	Object.assign( Color.prototype, {
 
-				geometry2.setAttribute( name, newAttribute );
+		isColor: true,
 
-			}
+		r: 1, g: 1, b: 1,
 
-			// morph attributes
+		set: function ( value ) {
 
-			var morphAttributes = this.morphAttributes;
+			if ( value && value.isColor ) {
 
-			for ( name in morphAttributes ) {
+				this.copy( value );
 
-				var morphArray = [];
-				var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
+			} else if ( typeof value === 'number' ) {
 
-				for ( var i = 0, il = morphAttribute.length; i < il; i ++ ) {
+				this.setHex( value );
 
-					var attribute = morphAttribute[ i ];
+			} else if ( typeof value === 'string' ) {
 
-					var newAttribute = convertBufferAttribute( attribute, indices );
+				this.setStyle( value );
 
-					morphArray.push( newAttribute );
+			}
 
-				}
+			return this;
 
-				geometry2.morphAttributes[ name ] = morphArray;
+		},
 
-			}
+		setScalar: function ( scalar ) {
 
-			geometry2.morphTargetsRelative = this.morphTargetsRelative;
+			this.r = scalar;
+			this.g = scalar;
+			this.b = scalar;
 
-			// groups
+			return this;
 
-			var groups = this.groups;
+		},
 
-			for ( var i = 0, l = groups.length; i < l; i ++ ) {
+		setHex: function ( hex ) {
 
-				var group = groups[ i ];
-				geometry2.addGroup( group.start, group.count, group.materialIndex );
+			hex = Math.floor( hex );
 
-			}
+			this.r = ( hex >> 16 & 255 ) / 255;
+			this.g = ( hex >> 8 & 255 ) / 255;
+			this.b = ( hex & 255 ) / 255;
 
-			return geometry2;
+			return this;
 
 		},
 
-		toJSON: function () {
+		setRGB: function ( r, g, b ) {
 
-			var data = {
-				metadata: {
-					version: 4.5,
-					type: 'BufferGeometry',
-					generator: 'BufferGeometry.toJSON'
-				}
-			};
-
-			// standard BufferGeometry serialization
-
-			data.uuid = this.uuid;
-			data.type = this.type;
-			if ( this.name !== '' ) { data.name = this.name; }
-			if ( Object.keys( this.userData ).length > 0 ) { data.userData = this.userData; }
+			this.r = r;
+			this.g = g;
+			this.b = b;
 
-			if ( this.parameters !== undefined ) {
+			return this;
 
-				var parameters = this.parameters;
+		},
 
-				for ( var key in parameters ) {
+		setHSL: function ( h, s, l ) {
 
-					if ( parameters[ key ] !== undefined ) { data[ key ] = parameters[ key ]; }
+			// h,s,l ranges are in 0.0 - 1.0
+			h = MathUtils.euclideanModulo( h, 1 );
+			s = MathUtils.clamp( s, 0, 1 );
+			l = MathUtils.clamp( l, 0, 1 );
 
-				}
+			if ( s === 0 ) {
 
-				return data;
+				this.r = this.g = this.b = l;
 
-			}
+			} else {
 
-			data.data = { attributes: {} };
+				var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
+				var q = ( 2 * l ) - p;
 
-			var index = this.index;
+				this.r = hue2rgb( q, p, h + 1 / 3 );
+				this.g = hue2rgb( q, p, h );
+				this.b = hue2rgb( q, p, h - 1 / 3 );
 
-			if ( index !== null ) {
+			}
 
-				data.data.index = {
-					type: index.array.constructor.name,
-					array: Array.prototype.slice.call( index.array )
-				};
+			return this;
 
-			}
+		},
 
-			var attributes = this.attributes;
+		setStyle: function ( style ) {
 
-			for ( var key in attributes ) {
+			function handleAlpha( string ) {
 
-				var attribute = attributes[ key ];
+				if ( string === undefined ) { return; }
 
-				var attributeData = attribute.toJSON();
+				if ( parseFloat( string ) < 1 ) {
 
-				if ( attribute.name !== '' ) { attributeData.name = attribute.name; }
+					console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' );
 
-				data.data.attributes[ key ] = attributeData;
+				}
 
 			}
 
-			var morphAttributes = {};
-			var hasMorphAttributes = false;
 
-			for ( var key in this.morphAttributes ) {
+			var m;
 
-				var attributeArray = this.morphAttributes[ key ];
+			if ( m = /^((?:rgb|hsl)a?)\(\s*([^\)]*)\)/.exec( style ) ) {
 
-				var array = [];
+				// rgb / hsl
 
-				for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
+				var color;
+				var name = m[ 1 ];
+				var components = m[ 2 ];
 
-					var attribute = attributeArray[ i ];
+				switch ( name ) {
 
-					var attributeData = attribute.toJSON();
+					case 'rgb':
+					case 'rgba':
 
-					if ( attribute.name !== '' ) { attributeData.name = attribute.name; }
+						if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
 
-					array.push( attributeData );
+							// rgb(255,0,0) rgba(255,0,0,0.5)
+							this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
+							this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
+							this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
 
-				}
+							handleAlpha( color[ 5 ] );
 
-				if ( array.length > 0 ) {
+							return this;
 
-					morphAttributes[ key ] = array;
+						}
 
-					hasMorphAttributes = true;
+						if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
 
-				}
+							// rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
+							this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
+							this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
+							this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
 
-			}
+							handleAlpha( color[ 5 ] );
 
-			if ( hasMorphAttributes ) {
+							return this;
 
-				data.data.morphAttributes = morphAttributes;
-				data.data.morphTargetsRelative = this.morphTargetsRelative;
+						}
 
-			}
+						break;
 
-			var groups = this.groups;
+					case 'hsl':
+					case 'hsla':
 
-			if ( groups.length > 0 ) {
+						if ( color = /^([0-9]*\.?[0-9]+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(,\s*([0-9]*\.?[0-9]+)\s*)?$/.exec( components ) ) {
 
-				data.data.groups = JSON.parse( JSON.stringify( groups ) );
+							// hsl(120,50%,50%) hsla(120,50%,50%,0.5)
+							var h = parseFloat( color[ 1 ] ) / 360;
+							var s = parseInt( color[ 2 ], 10 ) / 100;
+							var l = parseInt( color[ 3 ], 10 ) / 100;
 
-			}
+							handleAlpha( color[ 5 ] );
 
-			var boundingSphere = this.boundingSphere;
+							return this.setHSL( h, s, l );
 
-			if ( boundingSphere !== null ) {
+						}
 
-				data.data.boundingSphere = {
-					center: boundingSphere.center.toArray(),
-					radius: boundingSphere.radius
-				};
+						break;
 
-			}
+				}
 
-			return data;
+			} else if ( m = /^\#([A-Fa-f0-9]+)$/.exec( style ) ) {
 
-		},
+				// hex color
 
-		clone: function () {
+				var hex = m[ 1 ];
+				var size = hex.length;
 
-			/*
-			 // Handle primitives
+				if ( size === 3 ) {
 
-			 var parameters = this.parameters;
+					// #ff0
+					this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 0 ), 16 ) / 255;
+					this.g = parseInt( hex.charAt( 1 ) + hex.charAt( 1 ), 16 ) / 255;
+					this.b = parseInt( hex.charAt( 2 ) + hex.charAt( 2 ), 16 ) / 255;
 
-			 if ( parameters !== undefined ) {
+					return this;
 
-			 var values = [];
+				} else if ( size === 6 ) {
 
-			 for ( var key in parameters ) {
+					// #ff0000
+					this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 1 ), 16 ) / 255;
+					this.g = parseInt( hex.charAt( 2 ) + hex.charAt( 3 ), 16 ) / 255;
+					this.b = parseInt( hex.charAt( 4 ) + hex.charAt( 5 ), 16 ) / 255;
 
-			 values.push( parameters[ key ] );
+					return this;
 
-			 }
+				}
 
-			 var geometry = Object.create( this.constructor.prototype );
-			 this.constructor.apply( geometry, values );
-			 return geometry;
+			}
 
-			 }
+			if ( style && style.length > 0 ) {
 
-			 return new this.constructor().copy( this );
-			 */
+				return this.setColorName( style );
 
-			return new BufferGeometry().copy( this );
+			}
+
+			return this;
 
 		},
 
-		copy: function ( source ) {
+		setColorName: function ( style ) {
 
-			var name, i, l;
+			// color keywords
+			var hex = _colorKeywords[ style ];
 
-			// reset
+			if ( hex !== undefined ) {
 
-			this.index = null;
-			this.attributes = {};
-			this.morphAttributes = {};
-			this.groups = [];
-			this.boundingBox = null;
-			this.boundingSphere = null;
+				// red
+				this.setHex( hex );
 
-			// name
+			} else {
 
-			this.name = source.name;
+				// unknown color
+				console.warn( 'THREE.Color: Unknown color ' + style );
 
-			// index
+			}
 
-			var index = source.index;
+			return this;
 
-			if ( index !== null ) {
+		},
 
-				this.setIndex( index.clone() );
+		clone: function () {
 
-			}
+			return new this.constructor( this.r, this.g, this.b );
 
-			// attributes
+		},
 
-			var attributes = source.attributes;
+		copy: function ( color ) {
 
-			for ( name in attributes ) {
+			this.r = color.r;
+			this.g = color.g;
+			this.b = color.b;
 
-				var attribute = attributes[ name ];
-				this.setAttribute( name, attribute.clone() );
+			return this;
 
-			}
+		},
 
-			// morph attributes
+		copyGammaToLinear: function ( color, gammaFactor ) {
 
-			var morphAttributes = source.morphAttributes;
+			if ( gammaFactor === undefined ) { gammaFactor = 2.0; }
 
-			for ( name in morphAttributes ) {
+			this.r = Math.pow( color.r, gammaFactor );
+			this.g = Math.pow( color.g, gammaFactor );
+			this.b = Math.pow( color.b, gammaFactor );
 
-				var array = [];
-				var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
+			return this;
 
-				for ( i = 0, l = morphAttribute.length; i < l; i ++ ) {
+		},
 
-					array.push( morphAttribute[ i ].clone() );
+		copyLinearToGamma: function ( color, gammaFactor ) {
 
-				}
+			if ( gammaFactor === undefined ) { gammaFactor = 2.0; }
 
-				this.morphAttributes[ name ] = array;
+			var safeInverse = ( gammaFactor > 0 ) ? ( 1.0 / gammaFactor ) : 1.0;
 
-			}
+			this.r = Math.pow( color.r, safeInverse );
+			this.g = Math.pow( color.g, safeInverse );
+			this.b = Math.pow( color.b, safeInverse );
 
-			this.morphTargetsRelative = source.morphTargetsRelative;
+			return this;
 
-			// groups
+		},
 
-			var groups = source.groups;
+		convertGammaToLinear: function ( gammaFactor ) {
 
-			for ( i = 0, l = groups.length; i < l; i ++ ) {
+			this.copyGammaToLinear( this, gammaFactor );
 
-				var group = groups[ i ];
-				this.addGroup( group.start, group.count, group.materialIndex );
+			return this;
 
-			}
+		},
 
-			// bounding box
+		convertLinearToGamma: function ( gammaFactor ) {
 
-			var boundingBox = source.boundingBox;
+			this.copyLinearToGamma( this, gammaFactor );
 
-			if ( boundingBox !== null ) {
+			return this;
 
-				this.boundingBox = boundingBox.clone();
+		},
 
-			}
+		copySRGBToLinear: function ( color ) {
 
-			// bounding sphere
+			this.r = SRGBToLinear( color.r );
+			this.g = SRGBToLinear( color.g );
+			this.b = SRGBToLinear( color.b );
 
-			var boundingSphere = source.boundingSphere;
+			return this;
 
-			if ( boundingSphere !== null ) {
+		},
 
-				this.boundingSphere = boundingSphere.clone();
+		copyLinearToSRGB: function ( color ) {
 
-			}
+			this.r = LinearToSRGB( color.r );
+			this.g = LinearToSRGB( color.g );
+			this.b = LinearToSRGB( color.b );
 
-			// draw range
+			return this;
 
-			this.drawRange.start = source.drawRange.start;
-			this.drawRange.count = source.drawRange.count;
+		},
 
-			// user data
+		convertSRGBToLinear: function () {
 
-			this.userData = source.userData;
+			this.copySRGBToLinear( this );
 
 			return this;
 
 		},
 
-		dispose: function () {
+		convertLinearToSRGB: function () {
 
-			this.dispatchEvent( { type: 'dispose' } );
+			this.copyLinearToSRGB( this );
 
-		}
+			return this;
 
-	} );
+		},
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author jonobr1 / http://jonobr1.com/
-	 */
+		getHex: function () {
 
-	var _inverseMatrix = new Matrix4();
-	var _ray = new Ray();
-	var _sphere = new Sphere();
+			return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;
 
-	var _vA = new Vector3();
-	var _vB = new Vector3();
-	var _vC = new Vector3();
+		},
 
-	var _tempA = new Vector3();
-	var _tempB = new Vector3();
-	var _tempC = new Vector3();
+		getHexString: function () {
 
-	var _morphA = new Vector3();
-	var _morphB = new Vector3();
-	var _morphC = new Vector3();
+			return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
 
-	var _uvA = new Vector2();
-	var _uvB = new Vector2();
-	var _uvC = new Vector2();
+		},
 
-	var _intersectionPoint = new Vector3();
-	var _intersectionPointWorld = new Vector3();
+		getHSL: function ( target ) {
 
-	function Mesh( geometry, material ) {
+			// h,s,l ranges are in 0.0 - 1.0
 
-		Object3D.call( this );
+			if ( target === undefined ) {
 
-		this.type = 'Mesh';
+				console.warn( 'THREE.Color: .getHSL() target is now required' );
+				target = { h: 0, s: 0, l: 0 };
 
-		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
-		this.material = material !== undefined ? material : new MeshBasicMaterial();
+			}
 
-		this.updateMorphTargets();
+			var r = this.r, g = this.g, b = this.b;
 
-	}
+			var max = Math.max( r, g, b );
+			var min = Math.min( r, g, b );
 
-	Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			var hue, saturation;
+			var lightness = ( min + max ) / 2.0;
 
-		constructor: Mesh,
+			if ( min === max ) {
 
-		isMesh: true,
+				hue = 0;
+				saturation = 0;
 
-		copy: function ( source ) {
+			} else {
 
-			Object3D.prototype.copy.call( this, source );
+				var delta = max - min;
 
-			if ( source.morphTargetInfluences !== undefined ) {
+				saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
 
-				this.morphTargetInfluences = source.morphTargetInfluences.slice();
+				switch ( max ) {
 
-			}
+					case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
+					case g: hue = ( b - r ) / delta + 2; break;
+					case b: hue = ( r - g ) / delta + 4; break;
 
-			if ( source.morphTargetDictionary !== undefined ) {
+				}
 
-				this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
+				hue /= 6;
 
 			}
 
-			return this;
+			target.h = hue;
+			target.s = saturation;
+			target.l = lightness;
+
+			return target;
 
 		},
 
-		updateMorphTargets: function () {
+		getStyle: function () {
 
-			var geometry = this.geometry;
-			var m, ml, name;
+			return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')';
 
-			if ( geometry.isBufferGeometry ) {
+		},
 
-				var morphAttributes = geometry.morphAttributes;
-				var keys = Object.keys( morphAttributes );
+		offsetHSL: function ( h, s, l ) {
 
-				if ( keys.length > 0 ) {
+			this.getHSL( _hslA );
 
-					var morphAttribute = morphAttributes[ keys[ 0 ] ];
+			_hslA.h += h; _hslA.s += s; _hslA.l += l;
 
-					if ( morphAttribute !== undefined ) {
+			this.setHSL( _hslA.h, _hslA.s, _hslA.l );
 
-						this.morphTargetInfluences = [];
-						this.morphTargetDictionary = {};
+			return this;
 
-						for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
+		},
 
-							name = morphAttribute[ m ].name || String( m );
+		add: function ( color ) {
 
-							this.morphTargetInfluences.push( 0 );
-							this.morphTargetDictionary[ name ] = m;
+			this.r += color.r;
+			this.g += color.g;
+			this.b += color.b;
 
-						}
+			return this;
 
-					}
+		},
 
-				}
+		addColors: function ( color1, color2 ) {
 
-			} else {
+			this.r = color1.r + color2.r;
+			this.g = color1.g + color2.g;
+			this.b = color1.b + color2.b;
 
-				var morphTargets = geometry.morphTargets;
+			return this;
 
-				if ( morphTargets !== undefined && morphTargets.length > 0 ) {
+		},
 
-					console.error( 'THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
+		addScalar: function ( s ) {
 
-				}
+			this.r += s;
+			this.g += s;
+			this.b += s;
 
-			}
+			return this;
 
 		},
 
-		raycast: function ( raycaster, intersects ) {
+		sub: function ( color ) {
 
-			var geometry = this.geometry;
-			var material = this.material;
-			var matrixWorld = this.matrixWorld;
+			this.r = Math.max( 0, this.r - color.r );
+			this.g = Math.max( 0, this.g - color.g );
+			this.b = Math.max( 0, this.b - color.b );
 
-			if ( material === undefined ) { return; }
+			return this;
 
-			// Checking boundingSphere distance to ray
+		},
 
-			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
+		multiply: function ( color ) {
 
-			_sphere.copy( geometry.boundingSphere );
-			_sphere.applyMatrix4( matrixWorld );
+			this.r *= color.r;
+			this.g *= color.g;
+			this.b *= color.b;
 
-			if ( raycaster.ray.intersectsSphere( _sphere ) === false ) { return; }
+			return this;
 
-			//
+		},
 
-			_inverseMatrix.getInverse( matrixWorld );
-			_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
+		multiplyScalar: function ( s ) {
 
-			// Check boundingBox before continuing
+			this.r *= s;
+			this.g *= s;
+			this.b *= s;
 
-			if ( geometry.boundingBox !== null ) {
+			return this;
 
-				if ( _ray.intersectsBox( geometry.boundingBox ) === false ) { return; }
+		},
 
-			}
+		lerp: function ( color, alpha ) {
 
-			var intersection;
+			this.r += ( color.r - this.r ) * alpha;
+			this.g += ( color.g - this.g ) * alpha;
+			this.b += ( color.b - this.b ) * alpha;
 
-			if ( geometry.isBufferGeometry ) {
+			return this;
 
-				var a, b, c;
-				var index = geometry.index;
-				var position = geometry.attributes.position;
-				var morphPosition = geometry.morphAttributes.position;
-				var morphTargetsRelative = geometry.morphTargetsRelative;
-				var uv = geometry.attributes.uv;
-				var uv2 = geometry.attributes.uv2;
-				var groups = geometry.groups;
-				var drawRange = geometry.drawRange;
-				var i, j, il, jl;
-				var group, groupMaterial;
-				var start, end;
+		},
 
-				if ( index !== null ) {
+		lerpHSL: function ( color, alpha ) {
 
-					// indexed buffer geometry
+			this.getHSL( _hslA );
+			color.getHSL( _hslB );
 
-					if ( Array.isArray( material ) ) {
+			var h = MathUtils.lerp( _hslA.h, _hslB.h, alpha );
+			var s = MathUtils.lerp( _hslA.s, _hslB.s, alpha );
+			var l = MathUtils.lerp( _hslA.l, _hslB.l, alpha );
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+			this.setHSL( h, s, l );
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+			return this;
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+		},
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+		equals: function ( c ) {
 
-								a = index.getX( j );
-								b = index.getX( j + 1 );
-								c = index.getX( j + 2 );
+			return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
+		},
 
-								if ( intersection ) {
+		fromArray: function ( array, offset ) {
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
-									intersects.push( intersection );
+			if ( offset === undefined ) { offset = 0; }
 
-								}
+			this.r = array[ offset ];
+			this.g = array[ offset + 1 ];
+			this.b = array[ offset + 2 ];
 
-							}
+			return this;
 
-						}
+		},
 
-					} else {
+		toArray: function ( array, offset ) {
 
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
-
-						for ( i = start, il = end; i < il; i += 3 ) {
-
-							a = index.getX( i );
-							b = index.getX( i + 1 );
-							c = index.getX( i + 2 );
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
+			array[ offset ] = this.r;
+			array[ offset + 1 ] = this.g;
+			array[ offset + 2 ] = this.b;
 
-							if ( intersection ) {
+			return array;
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
-								intersects.push( intersection );
+		},
 
-							}
+		toJSON: function () {
 
-						}
+			return this.getHex();
 
-					}
+		}
 
-				} else if ( position !== undefined ) {
+	} );
 
-					// non-indexed buffer geometry
+	Color.NAMES = _colorKeywords;
 
-					if ( Array.isArray( material ) ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-						for ( i = 0, il = groups.length; i < il; i ++ ) {
+	function Face3( a, b, c, normal, color, materialIndex ) {
 
-							group = groups[ i ];
-							groupMaterial = material[ group.materialIndex ];
+		this.a = a;
+		this.b = b;
+		this.c = c;
 
-							start = Math.max( group.start, drawRange.start );
-							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
+		this.normal = ( normal && normal.isVector3 ) ? normal : new Vector3();
+		this.vertexNormals = Array.isArray( normal ) ? normal : [];
 
-							for ( j = start, jl = end; j < jl; j += 3 ) {
+		this.color = ( color && color.isColor ) ? color : new Color();
+		this.vertexColors = Array.isArray( color ) ? color : [];
 
-								a = j;
-								b = j + 1;
-								c = j + 2;
+		this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
 
-								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
+	}
 
-								if ( intersection ) {
+	Object.assign( Face3.prototype, {
 
-									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
-									intersection.face.materialIndex = group.materialIndex;
-									intersects.push( intersection );
+		clone: function () {
 
-								}
+			return new this.constructor().copy( this );
 
-							}
+		},
 
-						}
+		copy: function ( source ) {
 
-					} else {
+			this.a = source.a;
+			this.b = source.b;
+			this.c = source.c;
 
-						start = Math.max( 0, drawRange.start );
-						end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
+			this.normal.copy( source.normal );
+			this.color.copy( source.color );
 
-						for ( i = start, il = end; i < il; i += 3 ) {
+			this.materialIndex = source.materialIndex;
 
-							a = i;
-							b = i + 1;
-							c = i + 2;
+			for ( var i = 0, il = source.vertexNormals.length; i < il; i ++ ) {
 
-							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
+				this.vertexNormals[ i ] = source.vertexNormals[ i ].clone();
 
-							if ( intersection ) {
+			}
 
-								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
-								intersects.push( intersection );
+			for ( var i = 0, il = source.vertexColors.length; i < il; i ++ ) {
 
-							}
+				this.vertexColors[ i ] = source.vertexColors[ i ].clone();
 
-						}
+			}
 
-					}
+			return this;
 
-				}
+		}
 
-			} else if ( geometry.isGeometry ) {
+	} );
 
-				var fvA, fvB, fvC;
-				var isMultiMaterial = Array.isArray( material );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				var vertices = geometry.vertices;
-				var faces = geometry.faces;
-				var uvs;
+	var materialId = 0;
 
-				var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
-				if ( faceVertexUvs.length > 0 ) { uvs = faceVertexUvs; }
+	function Material() {
 
-				for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
+		Object.defineProperty( this, 'id', { value: materialId ++ } );
 
-					var face = faces[ f ];
-					var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
+		this.uuid = MathUtils.generateUUID();
 
-					if ( faceMaterial === undefined ) { continue; }
+		this.name = '';
+		this.type = 'Material';
 
-					fvA = vertices[ face.a ];
-					fvB = vertices[ face.b ];
-					fvC = vertices[ face.c ];
+		this.fog = true;
 
-					intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
+		this.blending = NormalBlending;
+		this.side = FrontSide;
+		this.flatShading = false;
+		this.vertexColors = false;
 
-					if ( intersection ) {
+		this.opacity = 1;
+		this.transparent = false;
 
-						if ( uvs && uvs[ f ] ) {
+		this.blendSrc = SrcAlphaFactor;
+		this.blendDst = OneMinusSrcAlphaFactor;
+		this.blendEquation = AddEquation;
+		this.blendSrcAlpha = null;
+		this.blendDstAlpha = null;
+		this.blendEquationAlpha = null;
 
-							var uvs_f = uvs[ f ];
-							_uvA.copy( uvs_f[ 0 ] );
-							_uvB.copy( uvs_f[ 1 ] );
-							_uvC.copy( uvs_f[ 2 ] );
+		this.depthFunc = LessEqualDepth;
+		this.depthTest = true;
+		this.depthWrite = true;
 
-							intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
+		this.stencilWriteMask = 0xff;
+		this.stencilFunc = AlwaysStencilFunc;
+		this.stencilRef = 0;
+		this.stencilFuncMask = 0xff;
+		this.stencilFail = KeepStencilOp;
+		this.stencilZFail = KeepStencilOp;
+		this.stencilZPass = KeepStencilOp;
+		this.stencilWrite = false;
 
-						}
+		this.clippingPlanes = null;
+		this.clipIntersection = false;
+		this.clipShadows = false;
 
-						intersection.face = face;
-						intersection.faceIndex = f;
-						intersects.push( intersection );
+		this.shadowSide = null;
 
-					}
+		this.colorWrite = true;
 
-				}
+		this.precision = null; // override the renderer's default precision for this material
 
-			}
+		this.polygonOffset = false;
+		this.polygonOffsetFactor = 0;
+		this.polygonOffsetUnits = 0;
 
-		},
+		this.dithering = false;
 
-		clone: function () {
+		this.alphaTest = 0;
+		this.premultipliedAlpha = false;
 
-			return new this.constructor( this.geometry, this.material ).copy( this );
+		this.visible = true;
 
-		}
+		this.toneMapped = true;
 
-	} );
+		this.userData = {};
 
-	function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
+		this.version = 0;
 
-		var intersect;
+	}
 
-		if ( material.side === BackSide ) {
+	Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-			intersect = ray.intersectTriangle( pC, pB, pA, true, point );
+		constructor: Material,
 
-		} else {
+		isMaterial: true,
 
-			intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
+		onBeforeCompile: function () {},
 
-		}
+		setValues: function ( values ) {
 
-		if ( intersect === null ) { return null; }
+			if ( values === undefined ) { return; }
 
-		_intersectionPointWorld.copy( point );
-		_intersectionPointWorld.applyMatrix4( object.matrixWorld );
+			for ( var key in values ) {
 
-		var distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
+				var newValue = values[ key ];
 
-		if ( distance < raycaster.near || distance > raycaster.far ) { return null; }
+				if ( newValue === undefined ) {
 
-		return {
-			distance: distance,
-			point: _intersectionPointWorld.clone(),
-			object: object
-		};
+					console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
+					continue;
 
-	}
+				}
 
-	function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ) {
+				// for backward compatability if shading is set in the constructor
+				if ( key === 'shading' ) {
 
-		_vA.fromBufferAttribute( position, a );
-		_vB.fromBufferAttribute( position, b );
-		_vC.fromBufferAttribute( position, c );
+					console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
+					this.flatShading = ( newValue === FlatShading ) ? true : false;
+					continue;
 
-		var morphInfluences = object.morphTargetInfluences;
+				}
 
-		if ( material.morphTargets && morphPosition && morphInfluences ) {
+				var currentValue = this[ key ];
 
-			_morphA.set( 0, 0, 0 );
-			_morphB.set( 0, 0, 0 );
-			_morphC.set( 0, 0, 0 );
+				if ( currentValue === undefined ) {
 
-			for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {
+					console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
+					continue;
 
-				var influence = morphInfluences[ i ];
-				var morphAttribute = morphPosition[ i ];
+				}
 
-				if ( influence === 0 ) { continue; }
+				if ( currentValue && currentValue.isColor ) {
 
-				_tempA.fromBufferAttribute( morphAttribute, a );
-				_tempB.fromBufferAttribute( morphAttribute, b );
-				_tempC.fromBufferAttribute( morphAttribute, c );
+					currentValue.set( newValue );
 
-				if ( morphTargetsRelative ) {
+				} else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
 
-					_morphA.addScaledVector( _tempA, influence );
-					_morphB.addScaledVector( _tempB, influence );
-					_morphC.addScaledVector( _tempC, influence );
+					currentValue.copy( newValue );
 
 				} else {
 
-					_morphA.addScaledVector( _tempA.sub( _vA ), influence );
-					_morphB.addScaledVector( _tempB.sub( _vB ), influence );
-					_morphC.addScaledVector( _tempC.sub( _vC ), influence );
+					this[ key ] = newValue;
 
 				}
 
 			}
 
-			_vA.add( _morphA );
-			_vB.add( _morphB );
-			_vC.add( _morphC );
-
-		}
-
-		if ( object.isSkinnedMesh ) {
-
-			object.boneTransform( a, _vA );
-			object.boneTransform( b, _vB );
-			object.boneTransform( c, _vC );
-
-		}
-
-		var intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
+		},
 
-		if ( intersection ) {
+		toJSON: function ( meta ) {
 
-			if ( uv ) {
+			var isRoot = ( meta === undefined || typeof meta === 'string' );
 
-				_uvA.fromBufferAttribute( uv, a );
-				_uvB.fromBufferAttribute( uv, b );
-				_uvC.fromBufferAttribute( uv, c );
+			if ( isRoot ) {
 
-				intersection.uv = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
+				meta = {
+					textures: {},
+					images: {}
+				};
 
 			}
 
-			if ( uv2 ) {
-
-				_uvA.fromBufferAttribute( uv2, a );
-				_uvB.fromBufferAttribute( uv2, b );
-				_uvC.fromBufferAttribute( uv2, c );
-
-				intersection.uv2 = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
+			var data = {
+				metadata: {
+					version: 4.5,
+					type: 'Material',
+					generator: 'Material.toJSON'
+				}
+			};
 
-			}
+			// standard Material serialization
+			data.uuid = this.uuid;
+			data.type = this.type;
 
-			var face = new Face3( a, b, c );
-			Triangle.getNormal( _vA, _vB, _vC, face.normal );
+			if ( this.name !== '' ) { data.name = this.name; }
 
-			intersection.face = face;
+			if ( this.color && this.color.isColor ) { data.color = this.color.getHex(); }
 
-		}
+			if ( this.roughness !== undefined ) { data.roughness = this.roughness; }
+			if ( this.metalness !== undefined ) { data.metalness = this.metalness; }
 
-		return intersection;
+			if ( this.sheen && this.sheen.isColor ) { data.sheen = this.sheen.getHex(); }
+			if ( this.emissive && this.emissive.isColor ) { data.emissive = this.emissive.getHex(); }
+			if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) { data.emissiveIntensity = this.emissiveIntensity; }
 
-	}
+			if ( this.specular && this.specular.isColor ) { data.specular = this.specular.getHex(); }
+			if ( this.shininess !== undefined ) { data.shininess = this.shininess; }
+			if ( this.clearcoat !== undefined ) { data.clearcoat = this.clearcoat; }
+			if ( this.clearcoatRoughness !== undefined ) { data.clearcoatRoughness = this.clearcoatRoughness; }
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author kile / http://kile.stravaganza.org/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * @author bhouston / http://clara.io
-	 */
+			if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
 
-	var _geometryId = 0; // Geometry uses even numbers as Id
-	var _m1$3 = new Matrix4();
-	var _obj$1 = new Object3D();
-	var _offset$1 = new Vector3();
+				data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
 
-	function Geometry() {
+			}
 
-		Object.defineProperty( this, 'id', { value: _geometryId += 2 } );
+			if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
 
-		this.uuid = MathUtils.generateUUID();
+				data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
 
-		this.name = '';
-		this.type = 'Geometry';
+			}
 
-		this.vertices = [];
-		this.colors = [];
-		this.faces = [];
-		this.faceVertexUvs = [[]];
+			if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
 
-		this.morphTargets = [];
-		this.morphNormals = [];
+				data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
+				data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
 
-		this.skinWeights = [];
-		this.skinIndices = [];
+			}
 
-		this.lineDistances = [];
+			if ( this.map && this.map.isTexture ) { data.map = this.map.toJSON( meta ).uuid; }
+			if ( this.matcap && this.matcap.isTexture ) { data.matcap = this.matcap.toJSON( meta ).uuid; }
+			if ( this.alphaMap && this.alphaMap.isTexture ) { data.alphaMap = this.alphaMap.toJSON( meta ).uuid; }
+			if ( this.lightMap && this.lightMap.isTexture ) { data.lightMap = this.lightMap.toJSON( meta ).uuid; }
 
-		this.boundingBox = null;
-		this.boundingSphere = null;
+			if ( this.aoMap && this.aoMap.isTexture ) {
 
-		// update flags
+				data.aoMap = this.aoMap.toJSON( meta ).uuid;
+				data.aoMapIntensity = this.aoMapIntensity;
 
-		this.elementsNeedUpdate = false;
-		this.verticesNeedUpdate = false;
-		this.uvsNeedUpdate = false;
-		this.normalsNeedUpdate = false;
-		this.colorsNeedUpdate = false;
-		this.lineDistancesNeedUpdate = false;
-		this.groupsNeedUpdate = false;
+			}
 
-	}
+			if ( this.bumpMap && this.bumpMap.isTexture ) {
 
-	Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+				data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
+				data.bumpScale = this.bumpScale;
 
-		constructor: Geometry,
+			}
 
-		isGeometry: true,
+			if ( this.normalMap && this.normalMap.isTexture ) {
 
-		applyMatrix4: function ( matrix ) {
+				data.normalMap = this.normalMap.toJSON( meta ).uuid;
+				data.normalMapType = this.normalMapType;
+				data.normalScale = this.normalScale.toArray();
 
-			var normalMatrix = new Matrix3().getNormalMatrix( matrix );
+			}
 
-			for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
+			if ( this.displacementMap && this.displacementMap.isTexture ) {
 
-				var vertex = this.vertices[ i ];
-				vertex.applyMatrix4( matrix );
+				data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
+				data.displacementScale = this.displacementScale;
+				data.displacementBias = this.displacementBias;
 
 			}
 
-			for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
+			if ( this.roughnessMap && this.roughnessMap.isTexture ) { data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid; }
+			if ( this.metalnessMap && this.metalnessMap.isTexture ) { data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid; }
 
-				var face = this.faces[ i ];
-				face.normal.applyMatrix3( normalMatrix ).normalize();
+			if ( this.emissiveMap && this.emissiveMap.isTexture ) { data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid; }
+			if ( this.specularMap && this.specularMap.isTexture ) { data.specularMap = this.specularMap.toJSON( meta ).uuid; }
 
-				for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
+			if ( this.envMap && this.envMap.isTexture ) {
 
-					face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
+				data.envMap = this.envMap.toJSON( meta ).uuid;
+				data.reflectivity = this.reflectivity; // Scale behind envMap
+				data.refractionRatio = this.refractionRatio;
 
-				}
+				if ( this.combine !== undefined ) { data.combine = this.combine; }
+				if ( this.envMapIntensity !== undefined ) { data.envMapIntensity = this.envMapIntensity; }
 
 			}
 
-			if ( this.boundingBox !== null ) {
+			if ( this.gradientMap && this.gradientMap.isTexture ) {
 
-				this.computeBoundingBox();
+				data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
 
 			}
 
-			if ( this.boundingSphere !== null ) {
-
-				this.computeBoundingSphere();
+			if ( this.size !== undefined ) { data.size = this.size; }
+			if ( this.sizeAttenuation !== undefined ) { data.sizeAttenuation = this.sizeAttenuation; }
 
-			}
+			if ( this.blending !== NormalBlending ) { data.blending = this.blending; }
+			if ( this.flatShading === true ) { data.flatShading = this.flatShading; }
+			if ( this.side !== FrontSide ) { data.side = this.side; }
+			if ( this.vertexColors ) { data.vertexColors = true; }
 
-			this.verticesNeedUpdate = true;
-			this.normalsNeedUpdate = true;
+			if ( this.opacity < 1 ) { data.opacity = this.opacity; }
+			if ( this.transparent === true ) { data.transparent = this.transparent; }
 
-			return this;
+			data.depthFunc = this.depthFunc;
+			data.depthTest = this.depthTest;
+			data.depthWrite = this.depthWrite;
 
-		},
+			data.stencilWrite = this.stencilWrite;
+			data.stencilWriteMask = this.stencilWriteMask;
+			data.stencilFunc = this.stencilFunc;
+			data.stencilRef = this.stencilRef;
+			data.stencilFuncMask = this.stencilFuncMask;
+			data.stencilFail = this.stencilFail;
+			data.stencilZFail = this.stencilZFail;
+			data.stencilZPass = this.stencilZPass;
 
-		rotateX: function ( angle ) {
+			// rotation (SpriteMaterial)
+			if ( this.rotation && this.rotation !== 0 ) { data.rotation = this.rotation; }
 
-			// rotate geometry around world x-axis
+			if ( this.polygonOffset === true ) { data.polygonOffset = true; }
+			if ( this.polygonOffsetFactor !== 0 ) { data.polygonOffsetFactor = this.polygonOffsetFactor; }
+			if ( this.polygonOffsetUnits !== 0 ) { data.polygonOffsetUnits = this.polygonOffsetUnits; }
 
-			_m1$3.makeRotationX( angle );
+			if ( this.linewidth && this.linewidth !== 1 ) { data.linewidth = this.linewidth; }
+			if ( this.dashSize !== undefined ) { data.dashSize = this.dashSize; }
+			if ( this.gapSize !== undefined ) { data.gapSize = this.gapSize; }
+			if ( this.scale !== undefined ) { data.scale = this.scale; }
 
-			this.applyMatrix4( _m1$3 );
+			if ( this.dithering === true ) { data.dithering = true; }
 
-			return this;
+			if ( this.alphaTest > 0 ) { data.alphaTest = this.alphaTest; }
+			if ( this.premultipliedAlpha === true ) { data.premultipliedAlpha = this.premultipliedAlpha; }
 
-		},
+			if ( this.wireframe === true ) { data.wireframe = this.wireframe; }
+			if ( this.wireframeLinewidth > 1 ) { data.wireframeLinewidth = this.wireframeLinewidth; }
+			if ( this.wireframeLinecap !== 'round' ) { data.wireframeLinecap = this.wireframeLinecap; }
+			if ( this.wireframeLinejoin !== 'round' ) { data.wireframeLinejoin = this.wireframeLinejoin; }
 
-		rotateY: function ( angle ) {
+			if ( this.morphTargets === true ) { data.morphTargets = true; }
+			if ( this.morphNormals === true ) { data.morphNormals = true; }
+			if ( this.skinning === true ) { data.skinning = true; }
 
-			// rotate geometry around world y-axis
+			if ( this.visible === false ) { data.visible = false; }
 
-			_m1$3.makeRotationY( angle );
+			if ( this.toneMapped === false ) { data.toneMapped = false; }
 
-			this.applyMatrix4( _m1$3 );
+			if ( JSON.stringify( this.userData ) !== '{}' ) { data.userData = this.userData; }
 
-			return this;
+			// TODO: Copied from Object3D.toJSON
 
-		},
+			function extractFromCache( cache ) {
 
-		rotateZ: function ( angle ) {
+				var values = [];
 
-			// rotate geometry around world z-axis
+				for ( var key in cache ) {
 
-			_m1$3.makeRotationZ( angle );
+					var data = cache[ key ];
+					delete data.metadata;
+					values.push( data );
 
-			this.applyMatrix4( _m1$3 );
+				}
 
-			return this;
+				return values;
 
-		},
+			}
 
-		translate: function ( x, y, z ) {
+			if ( isRoot ) {
 
-			// translate geometry
+				var textures = extractFromCache( meta.textures );
+				var images = extractFromCache( meta.images );
 
-			_m1$3.makeTranslation( x, y, z );
+				if ( textures.length > 0 ) { data.textures = textures; }
+				if ( images.length > 0 ) { data.images = images; }
 
-			this.applyMatrix4( _m1$3 );
+			}
 
-			return this;
+			return data;
 
 		},
 
-		scale: function ( x, y, z ) {
-
-			// scale geometry
-
-			_m1$3.makeScale( x, y, z );
-
-			this.applyMatrix4( _m1$3 );
+		clone: function () {
 
-			return this;
+			return new this.constructor().copy( this );
 
 		},
 
-		lookAt: function ( vector ) {
+		copy: function ( source ) {
 
-			_obj$1.lookAt( vector );
+			this.name = source.name;
 
-			_obj$1.updateMatrix();
+			this.fog = source.fog;
 
-			this.applyMatrix4( _obj$1.matrix );
+			this.blending = source.blending;
+			this.side = source.side;
+			this.flatShading = source.flatShading;
+			this.vertexColors = source.vertexColors;
 
-			return this;
+			this.opacity = source.opacity;
+			this.transparent = source.transparent;
 
-		},
+			this.blendSrc = source.blendSrc;
+			this.blendDst = source.blendDst;
+			this.blendEquation = source.blendEquation;
+			this.blendSrcAlpha = source.blendSrcAlpha;
+			this.blendDstAlpha = source.blendDstAlpha;
+			this.blendEquationAlpha = source.blendEquationAlpha;
 
-		fromBufferGeometry: function ( geometry ) {
+			this.depthFunc = source.depthFunc;
+			this.depthTest = source.depthTest;
+			this.depthWrite = source.depthWrite;
 
-			var scope = this;
+			this.stencilWriteMask = source.stencilWriteMask;
+			this.stencilFunc = source.stencilFunc;
+			this.stencilRef = source.stencilRef;
+			this.stencilFuncMask = source.stencilFuncMask;
+			this.stencilFail = source.stencilFail;
+			this.stencilZFail = source.stencilZFail;
+			this.stencilZPass = source.stencilZPass;
+			this.stencilWrite = source.stencilWrite;
 
-			var indices = geometry.index !== null ? geometry.index.array : undefined;
-			var attributes = geometry.attributes;
+			var srcPlanes = source.clippingPlanes,
+				dstPlanes = null;
 
-			if ( attributes.position === undefined ) {
+			if ( srcPlanes !== null ) {
 
-				console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' );
-				return this;
+				var n = srcPlanes.length;
+				dstPlanes = new Array( n );
+
+				for ( var i = 0; i !== n; ++ i )
+					{ dstPlanes[ i ] = srcPlanes[ i ].clone(); }
 
 			}
 
-			var positions = attributes.position.array;
-			var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
-			var colors = attributes.color !== undefined ? attributes.color.array : undefined;
-			var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
-			var uvs2 = attributes.uv2 !== undefined ? attributes.uv2.array : undefined;
+			this.clippingPlanes = dstPlanes;
+			this.clipIntersection = source.clipIntersection;
+			this.clipShadows = source.clipShadows;
 
-			if ( uvs2 !== undefined ) { this.faceVertexUvs[ 1 ] = []; }
+			this.shadowSide = source.shadowSide;
 
-			for ( var i = 0; i < positions.length; i += 3 ) {
+			this.colorWrite = source.colorWrite;
 
-				scope.vertices.push( new Vector3().fromArray( positions, i ) );
+			this.precision = source.precision;
 
-				if ( colors !== undefined ) {
+			this.polygonOffset = source.polygonOffset;
+			this.polygonOffsetFactor = source.polygonOffsetFactor;
+			this.polygonOffsetUnits = source.polygonOffsetUnits;
 
-					scope.colors.push( new Color().fromArray( colors, i ) );
+			this.dithering = source.dithering;
 
-				}
+			this.alphaTest = source.alphaTest;
+			this.premultipliedAlpha = source.premultipliedAlpha;
 
-			}
+			this.visible = source.visible;
 
-			function addFace( a, b, c, materialIndex ) {
+			this.toneMapped = source.toneMapped;
 
-				var vertexColors = ( colors === undefined ) ? [] : [
-					scope.colors[ a ].clone(),
-					scope.colors[ b ].clone(),
-					scope.colors[ c ].clone() ];
+			this.userData = JSON.parse( JSON.stringify( source.userData ) );
 
-				var vertexNormals = ( normals === undefined ) ? [] : [
-					new Vector3().fromArray( normals, a * 3 ),
-					new Vector3().fromArray( normals, b * 3 ),
-					new Vector3().fromArray( normals, c * 3 )
-				];
+			return this;
 
-				var face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
+		},
 
-				scope.faces.push( face );
+		dispose: function () {
 
-				if ( uvs !== undefined ) {
+			this.dispatchEvent( { type: 'dispose' } );
 
-					scope.faceVertexUvs[ 0 ].push( [
-						new Vector2().fromArray( uvs, a * 2 ),
-						new Vector2().fromArray( uvs, b * 2 ),
-						new Vector2().fromArray( uvs, c * 2 )
-					] );
+		}
 
-				}
+	} );
 
-				if ( uvs2 !== undefined ) {
+	Object.defineProperty( Material.prototype, 'needsUpdate', {
 
-					scope.faceVertexUvs[ 1 ].push( [
-						new Vector2().fromArray( uvs2, a * 2 ),
-						new Vector2().fromArray( uvs2, b * 2 ),
-						new Vector2().fromArray( uvs2, c * 2 )
-					] );
+		set: function ( value ) {
 
-				}
+			if ( value === true ) { this.version ++; }
 
-			}
+		}
 
-			var groups = geometry.groups;
+	} );
 
-			if ( groups.length > 0 ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  lightMap: new THREE.Texture( <Image> ),
+	 *  lightMapIntensity: <float>
+	 *
+	 *  aoMap: new THREE.Texture( <Image> ),
+	 *  aoMapIntensity: <float>
+	 *
+	 *  specularMap: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
+	 *  combine: THREE.Multiply,
+	 *  reflectivity: <float>,
+	 *  refractionRatio: <float>,
+	 *
+	 *  depthTest: <bool>,
+	 *  depthWrite: <bool>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>
+	 * }
+	 */
 
-				for ( var i = 0; i < groups.length; i ++ ) {
+	function MeshBasicMaterial( parameters ) {
 
-					var group = groups[ i ];
+		Material.call( this );
 
-					var start = group.start;
-					var count = group.count;
+		this.type = 'MeshBasicMaterial';
 
-					for ( var j = start, jl = start + count; j < jl; j += 3 ) {
+		this.color = new Color( 0xffffff ); // emissive
 
-						if ( indices !== undefined ) {
+		this.map = null;
 
-							addFace( indices[ j ], indices[ j + 1 ], indices[ j + 2 ], group.materialIndex );
+		this.lightMap = null;
+		this.lightMapIntensity = 1.0;
 
-						} else {
+		this.aoMap = null;
+		this.aoMapIntensity = 1.0;
 
-							addFace( j, j + 1, j + 2, group.materialIndex );
+		this.specularMap = null;
 
-						}
+		this.alphaMap = null;
 
-					}
+		this.envMap = null;
+		this.combine = MultiplyOperation;
+		this.reflectivity = 1;
+		this.refractionRatio = 0.98;
 
-				}
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
+		this.wireframeLinecap = 'round';
+		this.wireframeLinejoin = 'round';
 
-			} else {
+		this.skinning = false;
+		this.morphTargets = false;
 
-				if ( indices !== undefined ) {
+		this.setValues( parameters );
 
-					for ( var i = 0; i < indices.length; i += 3 ) {
+	}
 
-						addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
+	MeshBasicMaterial.prototype = Object.create( Material.prototype );
+	MeshBasicMaterial.prototype.constructor = MeshBasicMaterial;
 
-					}
+	MeshBasicMaterial.prototype.isMeshBasicMaterial = true;
 
-				} else {
+	MeshBasicMaterial.prototype.copy = function ( source ) {
 
-					for ( var i = 0; i < positions.length / 3; i += 3 ) {
+		Material.prototype.copy.call( this, source );
 
-						addFace( i, i + 1, i + 2 );
+		this.color.copy( source.color );
 
-					}
+		this.map = source.map;
 
-				}
+		this.lightMap = source.lightMap;
+		this.lightMapIntensity = source.lightMapIntensity;
 
-			}
+		this.aoMap = source.aoMap;
+		this.aoMapIntensity = source.aoMapIntensity;
 
-			this.computeFaceNormals();
+		this.specularMap = source.specularMap;
 
-			if ( geometry.boundingBox !== null ) {
+		this.alphaMap = source.alphaMap;
 
-				this.boundingBox = geometry.boundingBox.clone();
+		this.envMap = source.envMap;
+		this.combine = source.combine;
+		this.reflectivity = source.reflectivity;
+		this.refractionRatio = source.refractionRatio;
 
-			}
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
+		this.wireframeLinecap = source.wireframeLinecap;
+		this.wireframeLinejoin = source.wireframeLinejoin;
 
-			if ( geometry.boundingSphere !== null ) {
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
 
-				this.boundingSphere = geometry.boundingSphere.clone();
+		return this;
 
-			}
+	};
 
-			return this;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		},
+	var _vector$3 = new Vector3();
 
-		center: function () {
+	function BufferAttribute( array, itemSize, normalized ) {
 
-			this.computeBoundingBox();
+		if ( Array.isArray( array ) ) {
 
-			this.boundingBox.getCenter( _offset$1 ).negate();
+			throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
 
-			this.translate( _offset$1.x, _offset$1.y, _offset$1.z );
+		}
 
-			return this;
+		this.name = '';
 
-		},
+		this.array = array;
+		this.itemSize = itemSize;
+		this.count = array !== undefined ? array.length / itemSize : 0;
+		this.normalized = normalized === true;
 
-		normalize: function () {
+		this.usage = StaticDrawUsage;
+		this.updateRange = { offset: 0, count: - 1 };
 
-			this.computeBoundingSphere();
+		this.version = 0;
 
-			var center = this.boundingSphere.center;
-			var radius = this.boundingSphere.radius;
+	}
 
-			var s = radius === 0 ? 1 : 1.0 / radius;
+	Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', {
 
-			var matrix = new Matrix4();
-			matrix.set(
-				s, 0, 0, - s * center.x,
-				0, s, 0, - s * center.y,
-				0, 0, s, - s * center.z,
-				0, 0, 0, 1
-			);
+		set: function ( value ) {
 
-			this.applyMatrix4( matrix );
+			if ( value === true ) { this.version ++; }
+
+		}
+
+	} );
+
+	Object.assign( BufferAttribute.prototype, {
+
+		isBufferAttribute: true,
+
+		onUploadCallback: function () {},
+
+		setUsage: function ( value ) {
+
+			this.usage = value;
 
 			return this;
 
 		},
 
-		computeFaceNormals: function () {
+		copy: function ( source ) {
 
-			var cb = new Vector3(), ab = new Vector3();
+			this.name = source.name;
+			this.array = new source.array.constructor( source.array );
+			this.itemSize = source.itemSize;
+			this.count = source.count;
+			this.normalized = source.normalized;
 
-			for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
+			this.usage = source.usage;
 
-				var face = this.faces[ f ];
+			return this;
 
-				var vA = this.vertices[ face.a ];
-				var vB = this.vertices[ face.b ];
-				var vC = this.vertices[ face.c ];
+		},
 
-				cb.subVectors( vC, vB );
-				ab.subVectors( vA, vB );
-				cb.cross( ab );
+		copyAt: function ( index1, attribute, index2 ) {
 
-				cb.normalize();
+			index1 *= this.itemSize;
+			index2 *= attribute.itemSize;
 
-				face.normal.copy( cb );
+			for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
+
+				this.array[ index1 + i ] = attribute.array[ index2 + i ];
 
 			}
 
+			return this;
+
 		},
 
-		computeVertexNormals: function ( areaWeighted ) {
+		copyArray: function ( array ) {
 
-			if ( areaWeighted === undefined ) { areaWeighted = true; }
+			this.array.set( array );
 
-			var v, vl, f, fl, face, vertices;
+			return this;
 
-			vertices = new Array( this.vertices.length );
+		},
 
-			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
+		copyColorsArray: function ( colors ) {
 
-				vertices[ v ] = new Vector3();
+			var array = this.array, offset = 0;
 
-			}
+			for ( var i = 0, l = colors.length; i < l; i ++ ) {
 
-			if ( areaWeighted ) {
+				var color = colors[ i ];
 
-				// vertex normals weighted by triangle areas
-				// http://www.iquilezles.org/www/articles/normals/normals.htm
+				if ( color === undefined ) {
 
-				var vA, vB, vC;
-				var cb = new Vector3(), ab = new Vector3();
+					console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
+					color = new Color();
 
-				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+				}
 
-					face = this.faces[ f ];
+				array[ offset ++ ] = color.r;
+				array[ offset ++ ] = color.g;
+				array[ offset ++ ] = color.b;
 
-					vA = this.vertices[ face.a ];
-					vB = this.vertices[ face.b ];
-					vC = this.vertices[ face.c ];
+			}
 
-					cb.subVectors( vC, vB );
-					ab.subVectors( vA, vB );
-					cb.cross( ab );
+			return this;
 
-					vertices[ face.a ].add( cb );
-					vertices[ face.b ].add( cb );
-					vertices[ face.c ].add( cb );
+		},
 
-				}
+		copyVector2sArray: function ( vectors ) {
 
-			} else {
+			var array = this.array, offset = 0;
 
-				this.computeFaceNormals();
+			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
 
-				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+				var vector = vectors[ i ];
 
-					face = this.faces[ f ];
+				if ( vector === undefined ) {
 
-					vertices[ face.a ].add( face.normal );
-					vertices[ face.b ].add( face.normal );
-					vertices[ face.c ].add( face.normal );
+					console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
+					vector = new Vector2();
 
 				}
 
-			}
-
-			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
-
-				vertices[ v ].normalize();
+				array[ offset ++ ] = vector.x;
+				array[ offset ++ ] = vector.y;
 
 			}
 
-			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+			return this;
 
-				face = this.faces[ f ];
+		},
 
-				var vertexNormals = face.vertexNormals;
+		copyVector3sArray: function ( vectors ) {
 
-				if ( vertexNormals.length === 3 ) {
+			var array = this.array, offset = 0;
 
-					vertexNormals[ 0 ].copy( vertices[ face.a ] );
-					vertexNormals[ 1 ].copy( vertices[ face.b ] );
-					vertexNormals[ 2 ].copy( vertices[ face.c ] );
+			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
 
-				} else {
+				var vector = vectors[ i ];
 
-					vertexNormals[ 0 ] = vertices[ face.a ].clone();
-					vertexNormals[ 1 ] = vertices[ face.b ].clone();
-					vertexNormals[ 2 ] = vertices[ face.c ].clone();
+				if ( vector === undefined ) {
+
+					console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
+					vector = new Vector3();
 
 				}
 
+				array[ offset ++ ] = vector.x;
+				array[ offset ++ ] = vector.y;
+				array[ offset ++ ] = vector.z;
+
 			}
 
-			if ( this.faces.length > 0 ) {
+			return this;
 
-				this.normalsNeedUpdate = true;
+		},
 
-			}
+		copyVector4sArray: function ( vectors ) {
 
-		},
+			var array = this.array, offset = 0;
 
-		computeFlatVertexNormals: function () {
+			for ( var i = 0, l = vectors.length; i < l; i ++ ) {
 
-			var f, fl, face;
+				var vector = vectors[ i ];
 
-			this.computeFaceNormals();
+				if ( vector === undefined ) {
 
-			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+					console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
+					vector = new Vector4();
 
-				face = this.faces[ f ];
+				}
 
-				var vertexNormals = face.vertexNormals;
+				array[ offset ++ ] = vector.x;
+				array[ offset ++ ] = vector.y;
+				array[ offset ++ ] = vector.z;
+				array[ offset ++ ] = vector.w;
 
-				if ( vertexNormals.length === 3 ) {
+			}
 
-					vertexNormals[ 0 ].copy( face.normal );
-					vertexNormals[ 1 ].copy( face.normal );
-					vertexNormals[ 2 ].copy( face.normal );
+			return this;
 
-				} else {
+		},
 
-					vertexNormals[ 0 ] = face.normal.clone();
-					vertexNormals[ 1 ] = face.normal.clone();
-					vertexNormals[ 2 ] = face.normal.clone();
+		applyMatrix3: function ( m ) {
 
-				}
+			for ( var i = 0, l = this.count; i < l; i ++ ) {
 
-			}
+				_vector$3.x = this.getX( i );
+				_vector$3.y = this.getY( i );
+				_vector$3.z = this.getZ( i );
 
-			if ( this.faces.length > 0 ) {
+				_vector$3.applyMatrix3( m );
 
-				this.normalsNeedUpdate = true;
+				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
 
 			}
 
+			return this;
+
 		},
 
-		computeMorphNormals: function () {
+		applyMatrix4: function ( m ) {
 
-			var i, il, f, fl, face;
+			for ( var i = 0, l = this.count; i < l; i ++ ) {
 
-			// save original normals
-			// - create temp variables on first access
-			//   otherwise just copy (for faster repeated calls)
+				_vector$3.x = this.getX( i );
+				_vector$3.y = this.getY( i );
+				_vector$3.z = this.getZ( i );
 
-			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+				_vector$3.applyMatrix4( m );
 
-				face = this.faces[ f ];
+				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
 
-				if ( ! face.__originalFaceNormal ) {
+			}
 
-					face.__originalFaceNormal = face.normal.clone();
+			return this;
 
-				} else {
+		},
 
-					face.__originalFaceNormal.copy( face.normal );
+		applyNormalMatrix: function ( m ) {
 
-				}
+			for ( var i = 0, l = this.count; i < l; i ++ ) {
 
-				if ( ! face.__originalVertexNormals ) { face.__originalVertexNormals = []; }
+				_vector$3.x = this.getX( i );
+				_vector$3.y = this.getY( i );
+				_vector$3.z = this.getZ( i );
 
-				for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
+				_vector$3.applyNormalMatrix( m );
 
-					if ( ! face.__originalVertexNormals[ i ] ) {
+				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
 
-						face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
+			}
 
-					} else {
+			return this;
 
-						face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
+		},
 
-					}
+		transformDirection: function ( m ) {
 
-				}
+			for ( var i = 0, l = this.count; i < l; i ++ ) {
 
-			}
+				_vector$3.x = this.getX( i );
+				_vector$3.y = this.getY( i );
+				_vector$3.z = this.getZ( i );
 
-			// use temp geometry to compute face and vertex normals for each morph
+				_vector$3.transformDirection( m );
 
-			var tmpGeo = new Geometry();
-			tmpGeo.faces = this.faces;
+				this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z );
 
-			for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
+			}
 
-				// create on first access
+			return this;
 
-				if ( ! this.morphNormals[ i ] ) {
+		},
 
-					this.morphNormals[ i ] = {};
-					this.morphNormals[ i ].faceNormals = [];
-					this.morphNormals[ i ].vertexNormals = [];
+		set: function ( value, offset ) {
 
-					var dstNormalsFace = this.morphNormals[ i ].faceNormals;
-					var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
+			if ( offset === undefined ) { offset = 0; }
 
-					var faceNormal, vertexNormals;
+			this.array.set( value, offset );
 
-					for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+			return this;
 
-						faceNormal = new Vector3();
-						vertexNormals = { a: new Vector3(), b: new Vector3(), c: new Vector3() };
+		},
 
-						dstNormalsFace.push( faceNormal );
-						dstNormalsVertex.push( vertexNormals );
+		getX: function ( index ) {
 
-					}
+			return this.array[ index * this.itemSize ];
 
-				}
+		},
 
-				var morphNormals = this.morphNormals[ i ];
+		setX: function ( index, x ) {
 
-				// set vertices to morph target
+			this.array[ index * this.itemSize ] = x;
 
-				tmpGeo.vertices = this.morphTargets[ i ].vertices;
+			return this;
 
-				// compute morph normals
+		},
 
-				tmpGeo.computeFaceNormals();
-				tmpGeo.computeVertexNormals();
+		getY: function ( index ) {
 
-				// store morph normals
+			return this.array[ index * this.itemSize + 1 ];
 
-				var faceNormal, vertexNormals;
+		},
 
-				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+		setY: function ( index, y ) {
 
-					face = this.faces[ f ];
+			this.array[ index * this.itemSize + 1 ] = y;
 
-					faceNormal = morphNormals.faceNormals[ f ];
-					vertexNormals = morphNormals.vertexNormals[ f ];
+			return this;
 
-					faceNormal.copy( face.normal );
+		},
 
-					vertexNormals.a.copy( face.vertexNormals[ 0 ] );
-					vertexNormals.b.copy( face.vertexNormals[ 1 ] );
-					vertexNormals.c.copy( face.vertexNormals[ 2 ] );
+		getZ: function ( index ) {
 
-				}
+			return this.array[ index * this.itemSize + 2 ];
 
-			}
+		},
 
-			// restore original normals
+		setZ: function ( index, z ) {
 
-			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+			this.array[ index * this.itemSize + 2 ] = z;
 
-				face = this.faces[ f ];
+			return this;
 
-				face.normal = face.__originalFaceNormal;
-				face.vertexNormals = face.__originalVertexNormals;
+		},
 
-			}
+		getW: function ( index ) {
+
+			return this.array[ index * this.itemSize + 3 ];
 
 		},
 
-		computeBoundingBox: function () {
+		setW: function ( index, w ) {
 
-			if ( this.boundingBox === null ) {
+			this.array[ index * this.itemSize + 3 ] = w;
 
-				this.boundingBox = new Box3();
+			return this;
 
-			}
+		},
 
-			this.boundingBox.setFromPoints( this.vertices );
+		setXY: function ( index, x, y ) {
 
-		},
+			index *= this.itemSize;
 
-		computeBoundingSphere: function () {
+			this.array[ index + 0 ] = x;
+			this.array[ index + 1 ] = y;
 
-			if ( this.boundingSphere === null ) {
+			return this;
 
-				this.boundingSphere = new Sphere();
+		},
 
-			}
+		setXYZ: function ( index, x, y, z ) {
 
-			this.boundingSphere.setFromPoints( this.vertices );
+			index *= this.itemSize;
+
+			this.array[ index + 0 ] = x;
+			this.array[ index + 1 ] = y;
+			this.array[ index + 2 ] = z;
+
+			return this;
 
 		},
 
-		merge: function ( geometry, matrix, materialIndexOffset ) {
+		setXYZW: function ( index, x, y, z, w ) {
 
-			if ( ! ( geometry && geometry.isGeometry ) ) {
+			index *= this.itemSize;
 
-				console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
-				return;
+			this.array[ index + 0 ] = x;
+			this.array[ index + 1 ] = y;
+			this.array[ index + 2 ] = z;
+			this.array[ index + 3 ] = w;
 
-			}
+			return this;
 
-			var normalMatrix,
-				vertexOffset = this.vertices.length,
-				vertices1 = this.vertices,
-				vertices2 = geometry.vertices,
-				faces1 = this.faces,
-				faces2 = geometry.faces,
-				colors1 = this.colors,
-				colors2 = geometry.colors;
+		},
 
-			if ( materialIndexOffset === undefined ) { materialIndexOffset = 0; }
+		onUpload: function ( callback ) {
 
-			if ( matrix !== undefined ) {
+			this.onUploadCallback = callback;
 
-				normalMatrix = new Matrix3().getNormalMatrix( matrix );
+			return this;
 
-			}
+		},
 
-			// vertices
+		clone: function () {
 
-			for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
+			return new this.constructor( this.array, this.itemSize ).copy( this );
 
-				var vertex = vertices2[ i ];
+		},
 
-				var vertexCopy = vertex.clone();
+		toJSON: function () {
 
-				if ( matrix !== undefined ) { vertexCopy.applyMatrix4( matrix ); }
+			return {
+				itemSize: this.itemSize,
+				type: this.array.constructor.name,
+				array: Array.prototype.slice.call( this.array ),
+				normalized: this.normalized
+			};
 
-				vertices1.push( vertexCopy );
+		}
 
-			}
+	} );
 
-			// colors
+	//
 
-			for ( var i = 0, il = colors2.length; i < il; i ++ ) {
+	function Int8BufferAttribute( array, itemSize, normalized ) {
 
-				colors1.push( colors2[ i ].clone() );
+		BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized );
 
-			}
+	}
 
-			// faces
+	Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Int8BufferAttribute.prototype.constructor = Int8BufferAttribute;
 
-			for ( i = 0, il = faces2.length; i < il; i ++ ) {
 
-				var face = faces2[ i ], faceCopy, normal, color,
-					faceVertexNormals = face.vertexNormals,
-					faceVertexColors = face.vertexColors;
+	function Uint8BufferAttribute( array, itemSize, normalized ) {
 
-				faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
-				faceCopy.normal.copy( face.normal );
+		BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized );
 
-				if ( normalMatrix !== undefined ) {
+	}
 
-					faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
+	Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute;
 
-				}
 
-				for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
+	function Uint8ClampedBufferAttribute( array, itemSize, normalized ) {
 
-					normal = faceVertexNormals[ j ].clone();
+		BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized );
 
-					if ( normalMatrix !== undefined ) {
+	}
 
-						normal.applyMatrix3( normalMatrix ).normalize();
+	Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute;
 
-					}
 
-					faceCopy.vertexNormals.push( normal );
+	function Int16BufferAttribute( array, itemSize, normalized ) {
 
-				}
+		BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized );
 
-				faceCopy.color.copy( face.color );
+	}
 
-				for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
+	Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Int16BufferAttribute.prototype.constructor = Int16BufferAttribute;
 
-					color = faceVertexColors[ j ];
-					faceCopy.vertexColors.push( color.clone() );
 
-				}
+	function Uint16BufferAttribute( array, itemSize, normalized ) {
 
-				faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
+		BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized );
 
-				faces1.push( faceCopy );
+	}
 
-			}
+	Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute;
 
-			// uvs
 
-			for ( var i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
+	function Int32BufferAttribute( array, itemSize, normalized ) {
 
-				var faceVertexUvs2 = geometry.faceVertexUvs[ i ];
+		BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized );
 
-				if ( this.faceVertexUvs[ i ] === undefined ) { this.faceVertexUvs[ i ] = []; }
+	}
 
-				for ( var j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) {
+	Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Int32BufferAttribute.prototype.constructor = Int32BufferAttribute;
 
-					var uvs2 = faceVertexUvs2[ j ], uvsCopy = [];
 
-					for ( var k = 0, kl = uvs2.length; k < kl; k ++ ) {
+	function Uint32BufferAttribute( array, itemSize, normalized ) {
 
-						uvsCopy.push( uvs2[ k ].clone() );
+		BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized );
 
-					}
+	}
 
-					this.faceVertexUvs[ i ].push( uvsCopy );
+	Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute;
 
-				}
 
-			}
+	function Float32BufferAttribute( array, itemSize, normalized ) {
 
-		},
+		BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
 
-		mergeMesh: function ( mesh ) {
+	}
 
-			if ( ! ( mesh && mesh.isMesh ) ) {
+	Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
 
-				console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
-				return;
 
-			}
+	function Float64BufferAttribute( array, itemSize, normalized ) {
 
-			if ( mesh.matrixAutoUpdate ) { mesh.updateMatrix(); }
+		BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized );
 
-			this.merge( mesh.geometry, mesh.matrix );
+	}
 
-		},
+	Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype );
+	Float64BufferAttribute.prototype.constructor = Float64BufferAttribute;
 
-		/*
-		 * Checks for duplicate vertices with hashmap.
-		 * Duplicated vertices are removed
-		 * and faces' vertices are updated.
-		 */
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		mergeVertices: function () {
+	function DirectGeometry() {
 
-			var verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
-			var unique = [], changes = [];
+		this.vertices = [];
+		this.normals = [];
+		this.colors = [];
+		this.uvs = [];
+		this.uvs2 = [];
 
-			var v, key;
-			var precisionPoints = 4; // number of decimal points, e.g. 4 for epsilon of 0.0001
-			var precision = Math.pow( 10, precisionPoints );
-			var i, il, face;
-			var indices, j, jl;
+		this.groups = [];
 
-			for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
+		this.morphTargets = {};
 
-				v = this.vertices[ i ];
-				key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
+		this.skinWeights = [];
+		this.skinIndices = [];
 
-				if ( verticesMap[ key ] === undefined ) {
+		// this.lineDistances = [];
 
-					verticesMap[ key ] = i;
-					unique.push( this.vertices[ i ] );
-					changes[ i ] = unique.length - 1;
+		this.boundingBox = null;
+		this.boundingSphere = null;
 
-				} else {
+		// update flags
 
-					//console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
-					changes[ i ] = changes[ verticesMap[ key ] ];
+		this.verticesNeedUpdate = false;
+		this.normalsNeedUpdate = false;
+		this.colorsNeedUpdate = false;
+		this.uvsNeedUpdate = false;
+		this.groupsNeedUpdate = false;
 
-				}
+	}
 
-			}
+	Object.assign( DirectGeometry.prototype, {
 
+		computeGroups: function ( geometry ) {
 
-			// if faces are completely degenerate after merging vertices, we
-			// have to remove them from the geometry.
-			var faceIndicesToRemove = [];
+			var group;
+			var groups = [];
+			var materialIndex = undefined;
 
-			for ( i = 0, il = this.faces.length; i < il; i ++ ) {
+			var faces = geometry.faces;
 
-				face = this.faces[ i ];
+			for ( var i = 0; i < faces.length; i ++ ) {
 
-				face.a = changes[ face.a ];
-				face.b = changes[ face.b ];
-				face.c = changes[ face.c ];
+				var face = faces[ i ];
 
-				indices = [ face.a, face.b, face.c ];
+				// materials
 
-				// if any duplicate vertices are found in a Face3
-				// we have to remove the face as nothing can be saved
-				for ( var n = 0; n < 3; n ++ ) {
+				if ( face.materialIndex !== materialIndex ) {
 
-					if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
+					materialIndex = face.materialIndex;
 
-						faceIndicesToRemove.push( i );
-						break;
+					if ( group !== undefined ) {
+
+						group.count = ( i * 3 ) - group.start;
+						groups.push( group );
 
 					}
 
+					group = {
+						start: i * 3,
+						materialIndex: materialIndex
+					};
+
 				}
 
 			}
 
-			for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
+			if ( group !== undefined ) {
 
-				var idx = faceIndicesToRemove[ i ];
+				group.count = ( i * 3 ) - group.start;
+				groups.push( group );
 
-				this.faces.splice( idx, 1 );
+			}
 
-				for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
+			this.groups = groups;
 
-					this.faceVertexUvs[ j ].splice( idx, 1 );
+		},
 
-				}
+		fromGeometry: function ( geometry ) {
 
-			}
+			var faces = geometry.faces;
+			var vertices = geometry.vertices;
+			var faceVertexUvs = geometry.faceVertexUvs;
 
-			// Use unique set of vertices
+			var hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
+			var hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0;
 
-			var diff = this.vertices.length - unique.length;
-			this.vertices = unique;
-			return diff;
+			// morphs
 
-		},
+			var morphTargets = geometry.morphTargets;
+			var morphTargetsLength = morphTargets.length;
 
-		setFromPoints: function ( points ) {
+			var morphTargetsPosition;
 
-			this.vertices = [];
+			if ( morphTargetsLength > 0 ) {
 
-			for ( var i = 0, l = points.length; i < l; i ++ ) {
+				morphTargetsPosition = [];
 
-				var point = points[ i ];
-				this.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
+				for ( var i = 0; i < morphTargetsLength; i ++ ) {
+
+					morphTargetsPosition[ i ] = {
+						name: morphTargets[ i ].name,
+					 	data: []
+					};
+
+				}
+
+				this.morphTargets.position = morphTargetsPosition;
 
 			}
 
-			return this;
+			var morphNormals = geometry.morphNormals;
+			var morphNormalsLength = morphNormals.length;
 
-		},
+			var morphTargetsNormal;
 
-		sortFacesByMaterialIndex: function () {
+			if ( morphNormalsLength > 0 ) {
 
-			var faces = this.faces;
-			var length = faces.length;
+				morphTargetsNormal = [];
 
-			// tag faces
+				for ( var i = 0; i < morphNormalsLength; i ++ ) {
 
-			for ( var i = 0; i < length; i ++ ) {
+					morphTargetsNormal[ i ] = {
+						name: morphNormals[ i ].name,
+					 	data: []
+					};
 
-				faces[ i ]._id = i;
+				}
+
+				this.morphTargets.normal = morphTargetsNormal;
 
 			}
 
-			// sort faces
+			// skins
 
-			function materialIndexSort( a, b ) {
+			var skinIndices = geometry.skinIndices;
+			var skinWeights = geometry.skinWeights;
 
-				return a.materialIndex - b.materialIndex;
+			var hasSkinIndices = skinIndices.length === vertices.length;
+			var hasSkinWeights = skinWeights.length === vertices.length;
 
-			}
+			//
 
-			faces.sort( materialIndexSort );
+			if ( vertices.length > 0 && faces.length === 0 ) {
 
-			// sort uvs
+				console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
 
-			var uvs1 = this.faceVertexUvs[ 0 ];
-			var uvs2 = this.faceVertexUvs[ 1 ];
+			}
 
-			var newUvs1, newUvs2;
+			for ( var i = 0; i < faces.length; i ++ ) {
 
-			if ( uvs1 && uvs1.length === length ) { newUvs1 = []; }
-			if ( uvs2 && uvs2.length === length ) { newUvs2 = []; }
+				var face = faces[ i ];
 
-			for ( var i = 0; i < length; i ++ ) {
+				this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
 
-				var id = faces[ i ]._id;
+				var vertexNormals = face.vertexNormals;
 
-				if ( newUvs1 ) { newUvs1.push( uvs1[ id ] ); }
-				if ( newUvs2 ) { newUvs2.push( uvs2[ id ] ); }
+				if ( vertexNormals.length === 3 ) {
 
-			}
+					this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
 
-			if ( newUvs1 ) { this.faceVertexUvs[ 0 ] = newUvs1; }
-			if ( newUvs2 ) { this.faceVertexUvs[ 1 ] = newUvs2; }
+				} else {
 
-		},
+					var normal = face.normal;
 
-		toJSON: function () {
+					this.normals.push( normal, normal, normal );
 
-			var data = {
-				metadata: {
-					version: 4.5,
-					type: 'Geometry',
-					generator: 'Geometry.toJSON'
 				}
-			};
 
-			// standard Geometry serialization
+				var vertexColors = face.vertexColors;
 
-			data.uuid = this.uuid;
-			data.type = this.type;
-			if ( this.name !== '' ) { data.name = this.name; }
+				if ( vertexColors.length === 3 ) {
 
-			if ( this.parameters !== undefined ) {
+					this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
 
-				var parameters = this.parameters;
+				} else {
 
-				for ( var key in parameters ) {
+					var color = face.color;
 
-					if ( parameters[ key ] !== undefined ) { data[ key ] = parameters[ key ]; }
+					this.colors.push( color, color, color );
 
 				}
 
-				return data;
+				if ( hasFaceVertexUv === true ) {
 
-			}
+					var vertexUvs = faceVertexUvs[ 0 ][ i ];
 
-			var vertices = [];
+					if ( vertexUvs !== undefined ) {
 
-			for ( var i = 0; i < this.vertices.length; i ++ ) {
+						this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
 
-				var vertex = this.vertices[ i ];
-				vertices.push( vertex.x, vertex.y, vertex.z );
+					} else {
 
-			}
+						console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
 
-			var faces = [];
-			var normals = [];
-			var normalsHash = {};
-			var colors = [];
-			var colorsHash = {};
-			var uvs = [];
-			var uvsHash = {};
+						this.uvs.push( new Vector2(), new Vector2(), new Vector2() );
 
-			for ( var i = 0; i < this.faces.length; i ++ ) {
+					}
 
-				var face = this.faces[ i ];
+				}
 
-				var hasMaterial = true;
-				var hasFaceUv = false; // deprecated
-				var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
-				var hasFaceNormal = face.normal.length() > 0;
-				var hasFaceVertexNormal = face.vertexNormals.length > 0;
-				var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
-				var hasFaceVertexColor = face.vertexColors.length > 0;
+				if ( hasFaceVertexUv2 === true ) {
 
-				var faceType = 0;
+					var vertexUvs = faceVertexUvs[ 1 ][ i ];
 
-				faceType = setBit( faceType, 0, 0 ); // isQuad
-				faceType = setBit( faceType, 1, hasMaterial );
-				faceType = setBit( faceType, 2, hasFaceUv );
-				faceType = setBit( faceType, 3, hasFaceVertexUv );
-				faceType = setBit( faceType, 4, hasFaceNormal );
-				faceType = setBit( faceType, 5, hasFaceVertexNormal );
-				faceType = setBit( faceType, 6, hasFaceColor );
-				faceType = setBit( faceType, 7, hasFaceVertexColor );
+					if ( vertexUvs !== undefined ) {
 
-				faces.push( faceType );
-				faces.push( face.a, face.b, face.c );
-				faces.push( face.materialIndex );
+						this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
 
-				if ( hasFaceVertexUv ) {
+					} else {
 
-					var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
+						console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
 
-					faces.push(
-						getUvIndex( faceVertexUvs[ 0 ] ),
-						getUvIndex( faceVertexUvs[ 1 ] ),
-						getUvIndex( faceVertexUvs[ 2 ] )
-					);
+						this.uvs2.push( new Vector2(), new Vector2(), new Vector2() );
+
+					}
 
 				}
 
-				if ( hasFaceNormal ) {
+				// morphs
 
-					faces.push( getNormalIndex( face.normal ) );
+				for ( var j = 0; j < morphTargetsLength; j ++ ) {
+
+					var morphTarget = morphTargets[ j ].vertices;
+
+					morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
 
 				}
 
-				if ( hasFaceVertexNormal ) {
+				for ( var j = 0; j < morphNormalsLength; j ++ ) {
 
-					var vertexNormals = face.vertexNormals;
+					var morphNormal = morphNormals[ j ].vertexNormals[ i ];
 
-					faces.push(
-						getNormalIndex( vertexNormals[ 0 ] ),
-						getNormalIndex( vertexNormals[ 1 ] ),
-						getNormalIndex( vertexNormals[ 2 ] )
-					);
+					morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
 
 				}
 
-				if ( hasFaceColor ) {
+				// skins
 
-					faces.push( getColorIndex( face.color ) );
+				if ( hasSkinIndices ) {
 
-				}
+					this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
 
-				if ( hasFaceVertexColor ) {
+				}
 
-					var vertexColors = face.vertexColors;
+				if ( hasSkinWeights ) {
 
-					faces.push(
-						getColorIndex( vertexColors[ 0 ] ),
-						getColorIndex( vertexColors[ 1 ] ),
-						getColorIndex( vertexColors[ 2 ] )
-					);
+					this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
 
 				}
 
 			}
 
-			function setBit( value, position, enabled ) {
+			this.computeGroups( geometry );
 
-				return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
+			this.verticesNeedUpdate = geometry.verticesNeedUpdate;
+			this.normalsNeedUpdate = geometry.normalsNeedUpdate;
+			this.colorsNeedUpdate = geometry.colorsNeedUpdate;
+			this.uvsNeedUpdate = geometry.uvsNeedUpdate;
+			this.groupsNeedUpdate = geometry.groupsNeedUpdate;
+
+			if ( geometry.boundingSphere !== null ) {
+
+				this.boundingSphere = geometry.boundingSphere.clone();
 
 			}
 
-			function getNormalIndex( normal ) {
+			if ( geometry.boundingBox !== null ) {
 
-				var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
+				this.boundingBox = geometry.boundingBox.clone();
 
-				if ( normalsHash[ hash ] !== undefined ) {
+			}
 
-					return normalsHash[ hash ];
+			return this;
 
-				}
+		}
 
-				normalsHash[ hash ] = normals.length / 3;
-				normals.push( normal.x, normal.y, normal.z );
+	} );
 
-				return normalsHash[ hash ];
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			}
+	function arrayMax( array ) {
 
-			function getColorIndex( color ) {
+		if ( array.length === 0 ) { return - Infinity; }
 
-				var hash = color.r.toString() + color.g.toString() + color.b.toString();
+		var max = array[ 0 ];
 
-				if ( colorsHash[ hash ] !== undefined ) {
+		for ( var i = 1, l = array.length; i < l; ++ i ) {
 
-					return colorsHash[ hash ];
+			if ( array[ i ] > max ) { max = array[ i ]; }
 
-				}
+		}
 
-				colorsHash[ hash ] = colors.length;
-				colors.push( color.getHex() );
+		return max;
 
-				return colorsHash[ hash ];
+	}
 
-			}
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			function getUvIndex( uv ) {
+	var _bufferGeometryId = 1; // BufferGeometry uses odd numbers as Id
 
-				var hash = uv.x.toString() + uv.y.toString();
+	var _m1$2 = new Matrix4();
+	var _obj = new Object3D();
+	var _offset = new Vector3();
+	var _box$2 = new Box3();
+	var _boxMorphTargets = new Box3();
+	var _vector$4 = new Vector3();
 
-				if ( uvsHash[ hash ] !== undefined ) {
+	function BufferGeometry() {
 
-					return uvsHash[ hash ];
+		Object.defineProperty( this, 'id', { value: _bufferGeometryId += 2 } );
 
-				}
+		this.uuid = MathUtils.generateUUID();
 
-				uvsHash[ hash ] = uvs.length / 2;
-				uvs.push( uv.x, uv.y );
+		this.name = '';
+		this.type = 'BufferGeometry';
 
-				return uvsHash[ hash ];
+		this.index = null;
+		this.attributes = {};
 
-			}
+		this.morphAttributes = {};
+		this.morphTargetsRelative = false;
 
-			data.data = {};
+		this.groups = [];
 
-			data.data.vertices = vertices;
-			data.data.normals = normals;
-			if ( colors.length > 0 ) { data.data.colors = colors; }
-			if ( uvs.length > 0 ) { data.data.uvs = [ uvs ]; } // temporal backward compatibility
-			data.data.faces = faces;
+		this.boundingBox = null;
+		this.boundingSphere = null;
 
-			return data;
+		this.drawRange = { start: 0, count: Infinity };
 
-		},
+		this.userData = {};
 
-		clone: function () {
+	}
 
-			/*
-			 // Handle primitives
+	BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-			 var parameters = this.parameters;
+		constructor: BufferGeometry,
 
-			 if ( parameters !== undefined ) {
+		isBufferGeometry: true,
 
-			 var values = [];
+		getIndex: function () {
 
-			 for ( var key in parameters ) {
+			return this.index;
 
-			 values.push( parameters[ key ] );
+		},
 
-			 }
+		setIndex: function ( index ) {
 
-			 var geometry = Object.create( this.constructor.prototype );
-			 this.constructor.apply( geometry, values );
-			 return geometry;
+			if ( Array.isArray( index ) ) {
 
-			 }
+				this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
 
-			 return new this.constructor().copy( this );
-			 */
+			} else {
 
-			return new Geometry().copy( this );
+				this.index = index;
+
+			}
 
 		},
 
-		copy: function ( source ) {
+		getAttribute: function ( name ) {
 
-			var i, il, j, jl, k, kl;
+			return this.attributes[ name ];
 
-			// reset
+		},
 
-			this.vertices = [];
-			this.colors = [];
-			this.faces = [];
-			this.faceVertexUvs = [[]];
-			this.morphTargets = [];
-			this.morphNormals = [];
-			this.skinWeights = [];
-			this.skinIndices = [];
-			this.lineDistances = [];
-			this.boundingBox = null;
-			this.boundingSphere = null;
+		setAttribute: function ( name, attribute ) {
 
-			// name
+			this.attributes[ name ] = attribute;
 
-			this.name = source.name;
+			return this;
 
-			// vertices
+		},
 
-			var vertices = source.vertices;
+		deleteAttribute: function ( name ) {
 
-			for ( i = 0, il = vertices.length; i < il; i ++ ) {
+			delete this.attributes[ name ];
 
-				this.vertices.push( vertices[ i ].clone() );
+			return this;
 
-			}
+		},
 
-			// colors
+		addGroup: function ( start, count, materialIndex ) {
 
-			var colors = source.colors;
+			this.groups.push( {
 
-			for ( i = 0, il = colors.length; i < il; i ++ ) {
+				start: start,
+				count: count,
+				materialIndex: materialIndex !== undefined ? materialIndex : 0
 
-				this.colors.push( colors[ i ].clone() );
+			} );
 
-			}
+		},
 
-			// faces
+		clearGroups: function () {
 
-			var faces = source.faces;
+			this.groups = [];
 
-			for ( i = 0, il = faces.length; i < il; i ++ ) {
+		},
 
-				this.faces.push( faces[ i ].clone() );
+		setDrawRange: function ( start, count ) {
 
-			}
+			this.drawRange.start = start;
+			this.drawRange.count = count;
 
-			// face vertex uvs
+		},
 
-			for ( i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
+		applyMatrix4: function ( matrix ) {
 
-				var faceVertexUvs = source.faceVertexUvs[ i ];
+			var position = this.attributes.position;
 
-				if ( this.faceVertexUvs[ i ] === undefined ) {
+			if ( position !== undefined ) {
 
-					this.faceVertexUvs[ i ] = [];
+				position.applyMatrix4( matrix );
 
-				}
+				position.needsUpdate = true;
 
-				for ( j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
+			}
 
-					var uvs = faceVertexUvs[ j ], uvsCopy = [];
+			var normal = this.attributes.normal;
 
-					for ( k = 0, kl = uvs.length; k < kl; k ++ ) {
+			if ( normal !== undefined ) {
 
-						var uv = uvs[ k ];
+				var normalMatrix = new Matrix3().getNormalMatrix( matrix );
 
-						uvsCopy.push( uv.clone() );
+				normal.applyNormalMatrix( normalMatrix );
 
-					}
+				normal.needsUpdate = true;
 
-					this.faceVertexUvs[ i ].push( uvsCopy );
+			}
 
-				}
+			var tangent = this.attributes.tangent;
+
+			if ( tangent !== undefined ) {
+
+				tangent.transformDirection( matrix );
+
+				tangent.needsUpdate = true;
 
 			}
 
-			// morph targets
+			if ( this.boundingBox !== null ) {
 
-			var morphTargets = source.morphTargets;
+				this.computeBoundingBox();
 
-			for ( i = 0, il = morphTargets.length; i < il; i ++ ) {
+			}
 
-				var morphTarget = {};
-				morphTarget.name = morphTargets[ i ].name;
+			if ( this.boundingSphere !== null ) {
 
-				// vertices
+				this.computeBoundingSphere();
 
-				if ( morphTargets[ i ].vertices !== undefined ) {
+			}
 
-					morphTarget.vertices = [];
+			return this;
 
-					for ( j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) {
+		},
 
-						morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() );
+		rotateX: function ( angle ) {
 
-					}
+			// rotate geometry around world x-axis
 
-				}
+			_m1$2.makeRotationX( angle );
 
-				// normals
+			this.applyMatrix4( _m1$2 );
 
-				if ( morphTargets[ i ].normals !== undefined ) {
+			return this;
 
-					morphTarget.normals = [];
+		},
 
-					for ( j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) {
+		rotateY: function ( angle ) {
 
-						morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() );
+			// rotate geometry around world y-axis
 
-					}
+			_m1$2.makeRotationY( angle );
 
-				}
+			this.applyMatrix4( _m1$2 );
 
-				this.morphTargets.push( morphTarget );
+			return this;
 
-			}
+		},
 
-			// morph normals
+		rotateZ: function ( angle ) {
 
-			var morphNormals = source.morphNormals;
+			// rotate geometry around world z-axis
 
-			for ( i = 0, il = morphNormals.length; i < il; i ++ ) {
+			_m1$2.makeRotationZ( angle );
 
-				var morphNormal = {};
+			this.applyMatrix4( _m1$2 );
 
-				// vertex normals
+			return this;
 
-				if ( morphNormals[ i ].vertexNormals !== undefined ) {
+		},
 
-					morphNormal.vertexNormals = [];
+		translate: function ( x, y, z ) {
 
-					for ( j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) {
+			// translate geometry
 
-						var srcVertexNormal = morphNormals[ i ].vertexNormals[ j ];
-						var destVertexNormal = {};
+			_m1$2.makeTranslation( x, y, z );
 
-						destVertexNormal.a = srcVertexNormal.a.clone();
-						destVertexNormal.b = srcVertexNormal.b.clone();
-						destVertexNormal.c = srcVertexNormal.c.clone();
+			this.applyMatrix4( _m1$2 );
 
-						morphNormal.vertexNormals.push( destVertexNormal );
+			return this;
 
-					}
+		},
 
-				}
+		scale: function ( x, y, z ) {
 
-				// face normals
+			// scale geometry
 
-				if ( morphNormals[ i ].faceNormals !== undefined ) {
+			_m1$2.makeScale( x, y, z );
 
-					morphNormal.faceNormals = [];
+			this.applyMatrix4( _m1$2 );
 
-					for ( j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) {
+			return this;
 
-						morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() );
+		},
 
-					}
+		lookAt: function ( vector ) {
 
-				}
+			_obj.lookAt( vector );
 
-				this.morphNormals.push( morphNormal );
+			_obj.updateMatrix();
 
-			}
+			this.applyMatrix4( _obj.matrix );
 
-			// skin weights
+			return this;
 
-			var skinWeights = source.skinWeights;
+		},
 
-			for ( i = 0, il = skinWeights.length; i < il; i ++ ) {
+		center: function () {
 
-				this.skinWeights.push( skinWeights[ i ].clone() );
+			this.computeBoundingBox();
 
-			}
+			this.boundingBox.getCenter( _offset ).negate();
 
-			// skin indices
+			this.translate( _offset.x, _offset.y, _offset.z );
 
-			var skinIndices = source.skinIndices;
+			return this;
 
-			for ( i = 0, il = skinIndices.length; i < il; i ++ ) {
+		},
 
-				this.skinIndices.push( skinIndices[ i ].clone() );
+		setFromObject: function ( object ) {
 
-			}
+			// console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
 
-			// line distances
+			var geometry = object.geometry;
 
-			var lineDistances = source.lineDistances;
+			if ( object.isPoints || object.isLine ) {
 
-			for ( i = 0, il = lineDistances.length; i < il; i ++ ) {
+				var positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
+				var colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
 
-				this.lineDistances.push( lineDistances[ i ] );
+				this.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
+				this.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
 
-			}
+				if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
 
-			// bounding box
+					var lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
 
-			var boundingBox = source.boundingBox;
+					this.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
 
-			if ( boundingBox !== null ) {
+				}
 
-				this.boundingBox = boundingBox.clone();
+				if ( geometry.boundingSphere !== null ) {
 
-			}
+					this.boundingSphere = geometry.boundingSphere.clone();
 
-			// bounding sphere
+				}
 
-			var boundingSphere = source.boundingSphere;
+				if ( geometry.boundingBox !== null ) {
 
-			if ( boundingSphere !== null ) {
+					this.boundingBox = geometry.boundingBox.clone();
 
-				this.boundingSphere = boundingSphere.clone();
+				}
 
-			}
+			} else if ( object.isMesh ) {
 
-			// update flags
+				if ( geometry && geometry.isGeometry ) {
 
-			this.elementsNeedUpdate = source.elementsNeedUpdate;
-			this.verticesNeedUpdate = source.verticesNeedUpdate;
-			this.uvsNeedUpdate = source.uvsNeedUpdate;
-			this.normalsNeedUpdate = source.normalsNeedUpdate;
-			this.colorsNeedUpdate = source.colorsNeedUpdate;
-			this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate;
-			this.groupsNeedUpdate = source.groupsNeedUpdate;
+					this.fromGeometry( geometry );
+
+				}
+
+			}
 
 			return this;
 
 		},
 
-		dispose: function () {
+		setFromPoints: function ( points ) {
 
-			this.dispatchEvent( { type: 'dispose' } );
+			var position = [];
 
-		}
+			for ( var i = 0, l = points.length; i < l; i ++ ) {
 
-	} );
+				var point = points[ i ];
+				position.push( point.x, point.y, point.z || 0 );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+			}
 
-	// BoxGeometry
+			this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
 
-	var BoxGeometry = /*@__PURE__*/(function (Geometry) {
-		function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
+			return this;
 
-			Geometry.call(this);
+		},
 
-			this.type = 'BoxGeometry';
+		updateFromObject: function ( object ) {
 
-			this.parameters = {
-				width: width,
-				height: height,
-				depth: depth,
-				widthSegments: widthSegments,
-				heightSegments: heightSegments,
-				depthSegments: depthSegments
-			};
+			var geometry = object.geometry;
 
-			this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) );
-			this.mergeVertices();
+			if ( object.isMesh ) {
 
-		}
+				var direct = geometry.__directGeometry;
 
-		if ( Geometry ) BoxGeometry.__proto__ = Geometry;
-		BoxGeometry.prototype = Object.create( Geometry && Geometry.prototype );
-		BoxGeometry.prototype.constructor = BoxGeometry;
+				if ( geometry.elementsNeedUpdate === true ) {
 
-		return BoxGeometry;
-	}(Geometry));
+					direct = undefined;
+					geometry.elementsNeedUpdate = false;
 
-	// BoxBufferGeometry
+				}
 
-	var BoxBufferGeometry = /*@__PURE__*/(function (BufferGeometry) {
-		function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
+				if ( direct === undefined ) {
 
-			BufferGeometry.call(this);
+					return this.fromGeometry( geometry );
 
-			this.type = 'BoxBufferGeometry';
+				}
 
-			this.parameters = {
-				width: width,
-				height: height,
-				depth: depth,
-				widthSegments: widthSegments,
-				heightSegments: heightSegments,
-				depthSegments: depthSegments
-			};
+				direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
+				direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
+				direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
+				direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
+				direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
 
-			var scope = this;
+				geometry.verticesNeedUpdate = false;
+				geometry.normalsNeedUpdate = false;
+				geometry.colorsNeedUpdate = false;
+				geometry.uvsNeedUpdate = false;
+				geometry.groupsNeedUpdate = false;
 
-			width = width || 1;
-			height = height || 1;
-			depth = depth || 1;
+				geometry = direct;
 
-			// segments
+			}
 
-			widthSegments = Math.floor( widthSegments ) || 1;
-			heightSegments = Math.floor( heightSegments ) || 1;
-			depthSegments = Math.floor( depthSegments ) || 1;
+			var attribute;
 
-			// buffers
+			if ( geometry.verticesNeedUpdate === true ) {
 
-			var indices = [];
-			var vertices = [];
-			var normals = [];
-			var uvs = [];
+				attribute = this.attributes.position;
 
-			// helper variables
+				if ( attribute !== undefined ) {
 
-			var numberOfVertices = 0;
-			var groupStart = 0;
+					attribute.copyVector3sArray( geometry.vertices );
+					attribute.needsUpdate = true;
 
-			// build each side of the box geometry
+				}
 
-			buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
-			buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
-			buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
-			buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
-			buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
-			buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
+				geometry.verticesNeedUpdate = false;
 
-			// build geometry
+			}
 
-			this.setIndex( indices );
-			this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-			this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-			this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+			if ( geometry.normalsNeedUpdate === true ) {
 
-			function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
+				attribute = this.attributes.normal;
 
-				var segmentWidth = width / gridX;
-				var segmentHeight = height / gridY;
+				if ( attribute !== undefined ) {
 
-				var widthHalf = width / 2;
-				var heightHalf = height / 2;
-				var depthHalf = depth / 2;
+					attribute.copyVector3sArray( geometry.normals );
+					attribute.needsUpdate = true;
 
-				var gridX1 = gridX + 1;
-				var gridY1 = gridY + 1;
+				}
 
-				var vertexCounter = 0;
-				var groupCount = 0;
+				geometry.normalsNeedUpdate = false;
 
-				var ix, iy;
+			}
 
-				var vector = new Vector3();
+			if ( geometry.colorsNeedUpdate === true ) {
 
-				// generate vertices, normals and uvs
+				attribute = this.attributes.color;
 
-				for ( iy = 0; iy < gridY1; iy ++ ) {
-
-					var y = iy * segmentHeight - heightHalf;
+				if ( attribute !== undefined ) {
 
-					for ( ix = 0; ix < gridX1; ix ++ ) {
+					attribute.copyColorsArray( geometry.colors );
+					attribute.needsUpdate = true;
 
-						var x = ix * segmentWidth - widthHalf;
+				}
 
-						// set values to correct vector component
+				geometry.colorsNeedUpdate = false;
 
-						vector[ u ] = x * udir;
-						vector[ v ] = y * vdir;
-						vector[ w ] = depthHalf;
+			}
 
-						// now apply vector to vertex buffer
+			if ( geometry.uvsNeedUpdate ) {
 
-						vertices.push( vector.x, vector.y, vector.z );
+				attribute = this.attributes.uv;
 
-						// set values to correct vector component
+				if ( attribute !== undefined ) {
 
-						vector[ u ] = 0;
-						vector[ v ] = 0;
-						vector[ w ] = depth > 0 ? 1 : - 1;
+					attribute.copyVector2sArray( geometry.uvs );
+					attribute.needsUpdate = true;
 
-						// now apply vector to normal buffer
+				}
 
-						normals.push( vector.x, vector.y, vector.z );
+				geometry.uvsNeedUpdate = false;
 
-						// uvs
+			}
 
-						uvs.push( ix / gridX );
-						uvs.push( 1 - ( iy / gridY ) );
+			if ( geometry.lineDistancesNeedUpdate ) {
 
-						// counters
+				attribute = this.attributes.lineDistance;
 
-						vertexCounter += 1;
+				if ( attribute !== undefined ) {
 
-					}
+					attribute.copyArray( geometry.lineDistances );
+					attribute.needsUpdate = true;
 
 				}
 
-				// indices
-
-				// 1. you need three indices to draw a single face
-				// 2. a single segment consists of two faces
-				// 3. so we need to generate six (2*3) indices per segment
+				geometry.lineDistancesNeedUpdate = false;
 
-				for ( iy = 0; iy < gridY; iy ++ ) {
+			}
 
-					for ( ix = 0; ix < gridX; ix ++ ) {
+			if ( geometry.groupsNeedUpdate ) {
 
-						var a = numberOfVertices + ix + gridX1 * iy;
-						var b = numberOfVertices + ix + gridX1 * ( iy + 1 );
-						var c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
-						var d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
+				geometry.computeGroups( object.geometry );
+				this.groups = geometry.groups;
 
-						// faces
+				geometry.groupsNeedUpdate = false;
 
-						indices.push( a, b, d );
-						indices.push( b, c, d );
+			}
 
-						// increase counter
+			return this;
 
-						groupCount += 6;
+		},
 
-					}
+		fromGeometry: function ( geometry ) {
 
-				}
+			geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
 
-				// add a group to the geometry. this will ensure multi material support
+			return this.fromDirectGeometry( geometry.__directGeometry );
 
-				scope.addGroup( groupStart, groupCount, materialIndex );
+		},
 
-				// calculate new start value for groups
+		fromDirectGeometry: function ( geometry ) {
 
-				groupStart += groupCount;
+			var positions = new Float32Array( geometry.vertices.length * 3 );
+			this.setAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
 
-				// update total number of vertices
+			if ( geometry.normals.length > 0 ) {
 
-				numberOfVertices += vertexCounter;
+				var normals = new Float32Array( geometry.normals.length * 3 );
+				this.setAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
 
 			}
 
-		}
+			if ( geometry.colors.length > 0 ) {
 
-		if ( BufferGeometry ) BoxBufferGeometry.__proto__ = BufferGeometry;
-		BoxBufferGeometry.prototype = Object.create( BufferGeometry && BufferGeometry.prototype );
-		BoxBufferGeometry.prototype.constructor = BoxBufferGeometry;
+				var colors = new Float32Array( geometry.colors.length * 3 );
+				this.setAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
 
-		return BoxBufferGeometry;
-	}(BufferGeometry));
+			}
 
-	/**
-	 * Uniform Utilities
-	 */
+			if ( geometry.uvs.length > 0 ) {
 
-	function cloneUniforms( src ) {
+				var uvs = new Float32Array( geometry.uvs.length * 2 );
+				this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
 
-		var dst = {};
+			}
 
-		for ( var u in src ) {
+			if ( geometry.uvs2.length > 0 ) {
 
-			dst[ u ] = {};
+				var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
+				this.setAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
 
-			for ( var p in src[ u ] ) {
+			}
 
-				var property = src[ u ][ p ];
+			// groups
 
-				if ( property && ( property.isColor ||
-					property.isMatrix3 || property.isMatrix4 ||
-					property.isVector2 || property.isVector3 || property.isVector4 ||
-					property.isTexture ) ) {
+			this.groups = geometry.groups;
 
-					dst[ u ][ p ] = property.clone();
+			// morphs
 
-				} else if ( Array.isArray( property ) ) {
+			for ( var name in geometry.morphTargets ) {
 
-					dst[ u ][ p ] = property.slice();
+				var array = [];
+				var morphTargets = geometry.morphTargets[ name ];
 
-				} else {
+				for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
 
-					dst[ u ][ p ] = property;
+					var morphTarget = morphTargets[ i ];
 
-				}
+					var attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 );
+					attribute.name = morphTarget.name;
 
-			}
+					array.push( attribute.copyVector3sArray( morphTarget.data ) );
 
-		}
+				}
 
-		return dst;
+				this.morphAttributes[ name ] = array;
 
-	}
+			}
 
-	function mergeUniforms( uniforms ) {
+			// skinning
 
-		var merged = {};
+			if ( geometry.skinIndices.length > 0 ) {
 
-		for ( var u = 0; u < uniforms.length; u ++ ) {
+				var skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
+				this.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
 
-			var tmp = cloneUniforms( uniforms[ u ] );
+			}
 
-			for ( var p in tmp ) {
+			if ( geometry.skinWeights.length > 0 ) {
 
-				merged[ p ] = tmp[ p ];
+				var skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
+				this.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
 
 			}
 
-		}
-
-		return merged;
+			//
 
-	}
+			if ( geometry.boundingSphere !== null ) {
 
-	// Legacy
+				this.boundingSphere = geometry.boundingSphere.clone();
 
-	var UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms };
+			}
 
-	var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}";
+			if ( geometry.boundingBox !== null ) {
 
-	var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}";
+				this.boundingBox = geometry.boundingBox.clone();
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  defines: { "label" : "value" },
-	 *  uniforms: { "parameter1": { value: 1.0 }, "parameter2": { value2: 2 } },
-	 *
-	 *  fragmentShader: <string>,
-	 *  vertexShader: <string>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  lights: <bool>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+			}
 
-	function ShaderMaterial( parameters ) {
+			return this;
 
-		Material.call( this );
+		},
 
-		this.type = 'ShaderMaterial';
+		computeBoundingBox: function () {
 
-		this.defines = {};
-		this.uniforms = {};
+			if ( this.boundingBox === null ) {
 
-		this.vertexShader = default_vertex;
-		this.fragmentShader = default_fragment;
+				this.boundingBox = new Box3();
 
-		this.linewidth = 1;
+			}
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
+			var position = this.attributes.position;
+			var morphAttributesPosition = this.morphAttributes.position;
 
-		this.fog = false; // set to use scene fog
-		this.lights = false; // set to use scene lights
-		this.clipping = false; // set to use user-defined clipping planes
+			if ( position !== undefined ) {
 
-		this.skinning = false; // set to use skinning attribute streams
-		this.morphTargets = false; // set to use morph targets
-		this.morphNormals = false; // set to use morph normals
+				this.boundingBox.setFromBufferAttribute( position );
 
-		this.extensions = {
-			derivatives: false, // set to use derivatives
-			fragDepth: false, // set to use fragment depth values
-			drawBuffers: false, // set to use draw buffers
-			shaderTextureLOD: false // set to use shader texture LOD
-		};
+				// process morph attributes if present
 
-		// When rendered geometry doesn't include these attributes but the material does,
-		// use these default values in WebGL. This avoids errors when buffer data is missing.
-		this.defaultAttributeValues = {
-			'color': [ 1, 1, 1 ],
-			'uv': [ 0, 0 ],
-			'uv2': [ 0, 0 ]
-		};
+				if ( morphAttributesPosition ) {
 
-		this.index0AttributeName = undefined;
-		this.uniformsNeedUpdate = false;
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
 
-		if ( parameters !== undefined ) {
+						var morphAttribute = morphAttributesPosition[ i ];
+						_box$2.setFromBufferAttribute( morphAttribute );
 
-			if ( parameters.attributes !== undefined ) {
+						if ( this.morphTargetsRelative ) {
 
-				console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
+							_vector$4.addVectors( this.boundingBox.min, _box$2.min );
+							this.boundingBox.expandByPoint( _vector$4 );
 
-			}
+							_vector$4.addVectors( this.boundingBox.max, _box$2.max );
+							this.boundingBox.expandByPoint( _vector$4 );
 
-			this.setValues( parameters );
+						} else {
 
-		}
+							this.boundingBox.expandByPoint( _box$2.min );
+							this.boundingBox.expandByPoint( _box$2.max );
 
-	}
+						}
 
-	ShaderMaterial.prototype = Object.create( Material.prototype );
-	ShaderMaterial.prototype.constructor = ShaderMaterial;
+					}
 
-	ShaderMaterial.prototype.isShaderMaterial = true;
+				}
 
-	ShaderMaterial.prototype.copy = function ( source ) {
+			} else {
 
-		Material.prototype.copy.call( this, source );
+				this.boundingBox.makeEmpty();
 
-		this.fragmentShader = source.fragmentShader;
-		this.vertexShader = source.vertexShader;
+			}
 
-		this.uniforms = cloneUniforms( source.uniforms );
+			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
 
-		this.defines = Object.assign( {}, source.defines );
+				console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
+			}
 
-		this.lights = source.lights;
-		this.clipping = source.clipping;
+		},
 
-		this.skinning = source.skinning;
+		computeBoundingSphere: function () {
 
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+			if ( this.boundingSphere === null ) {
 
-		this.extensions = source.extensions;
+				this.boundingSphere = new Sphere();
 
-		return this;
+			}
 
-	};
+			var position = this.attributes.position;
+			var morphAttributesPosition = this.morphAttributes.position;
 
-	ShaderMaterial.prototype.toJSON = function ( meta ) {
+			if ( position ) {
 
-		var data = Material.prototype.toJSON.call( this, meta );
+				// first, find the center of the bounding sphere
 
-		data.uniforms = {};
+				var center = this.boundingSphere.center;
 
-		for ( var name in this.uniforms ) {
+				_box$2.setFromBufferAttribute( position );
 
-			var uniform = this.uniforms[ name ];
-			var value = uniform.value;
+				// process morph attributes if present
 
-			if ( value && value.isTexture ) {
+				if ( morphAttributesPosition ) {
 
-				data.uniforms[ name ] = {
-					type: 't',
-					value: value.toJSON( meta ).uuid
-				};
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
 
-			} else if ( value && value.isColor ) {
+						var morphAttribute = morphAttributesPosition[ i ];
+						_boxMorphTargets.setFromBufferAttribute( morphAttribute );
 
-				data.uniforms[ name ] = {
-					type: 'c',
-					value: value.getHex()
-				};
+						if ( this.morphTargetsRelative ) {
 
-			} else if ( value && value.isVector2 ) {
+							_vector$4.addVectors( _box$2.min, _boxMorphTargets.min );
+							_box$2.expandByPoint( _vector$4 );
 
-				data.uniforms[ name ] = {
-					type: 'v2',
-					value: value.toArray()
-				};
+							_vector$4.addVectors( _box$2.max, _boxMorphTargets.max );
+							_box$2.expandByPoint( _vector$4 );
 
-			} else if ( value && value.isVector3 ) {
+						} else {
 
-				data.uniforms[ name ] = {
-					type: 'v3',
-					value: value.toArray()
-				};
+							_box$2.expandByPoint( _boxMorphTargets.min );
+							_box$2.expandByPoint( _boxMorphTargets.max );
 
-			} else if ( value && value.isVector4 ) {
+						}
 
-				data.uniforms[ name ] = {
-					type: 'v4',
-					value: value.toArray()
-				};
+					}
 
-			} else if ( value && value.isMatrix3 ) {
+				}
 
-				data.uniforms[ name ] = {
-					type: 'm3',
-					value: value.toArray()
-				};
+				_box$2.getCenter( center );
 
-			} else if ( value && value.isMatrix4 ) {
+				// second, try to find a boundingSphere with a radius smaller than the
+				// boundingSphere of the boundingBox: sqrt(3) smaller in the best case
 
-				data.uniforms[ name ] = {
-					type: 'm4',
-					value: value.toArray()
-				};
+				var maxRadiusSq = 0;
 
-			} else {
+				for ( var i = 0, il = position.count; i < il; i ++ ) {
 
-				data.uniforms[ name ] = {
-					value: value
-				};
+					_vector$4.fromBufferAttribute( position, i );
 
-				// note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
+					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) );
 
-			}
+				}
 
-		}
+				// process morph attributes if present
 
-		if ( Object.keys( this.defines ).length > 0 ) { data.defines = this.defines; }
+				if ( morphAttributesPosition ) {
 
-		data.vertexShader = this.vertexShader;
-		data.fragmentShader = this.fragmentShader;
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
 
-		var extensions = {};
+						var morphAttribute = morphAttributesPosition[ i ];
+						var morphTargetsRelative = this.morphTargetsRelative;
 
-		for ( var key in this.extensions ) {
+						for ( var j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
 
-			if ( this.extensions[ key ] === true ) { extensions[ key ] = true; }
+							_vector$4.fromBufferAttribute( morphAttribute, j );
 
-		}
+							if ( morphTargetsRelative ) {
 
-		if ( Object.keys( extensions ).length > 0 ) { data.extensions = extensions; }
+								_offset.fromBufferAttribute( position, j );
+								_vector$4.add( _offset );
 
-		return data;
+							}
 
-	};
+							maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author WestLangley / http://github.com/WestLangley
-	*/
+						}
 
-	function Camera() {
+					}
 
-		Object3D.call( this );
+				}
 
-		this.type = 'Camera';
+				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
 
-		this.matrixWorldInverse = new Matrix4();
+				if ( isNaN( this.boundingSphere.radius ) ) {
 
-		this.projectionMatrix = new Matrix4();
-		this.projectionMatrixInverse = new Matrix4();
+					console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
 
-	}
+				}
 
-	Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			}
 
-		constructor: Camera,
+		},
 
-		isCamera: true,
+		computeFaceNormals: function () {
 
-		copy: function ( source, recursive ) {
+			// backwards compatibility
 
-			Object3D.prototype.copy.call( this, source, recursive );
+		},
 
-			this.matrixWorldInverse.copy( source.matrixWorldInverse );
+		computeVertexNormals: function () {
 
-			this.projectionMatrix.copy( source.projectionMatrix );
-			this.projectionMatrixInverse.copy( source.projectionMatrixInverse );
+			var index = this.index;
+			var attributes = this.attributes;
 
-			return this;
+			if ( attributes.position ) {
 
-		},
+				var positions = attributes.position.array;
 
-		getWorldDirection: function ( target ) {
+				if ( attributes.normal === undefined ) {
 
-			if ( target === undefined ) {
+					this.setAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
 
-				console.warn( 'THREE.Camera: .getWorldDirection() target is now required' );
-				target = new Vector3();
+				} else {
 
-			}
+					// reset existing normals to zero
 
-			this.updateMatrixWorld( true );
+					var array = attributes.normal.array;
 
-			var e = this.matrixWorld.elements;
+					for ( var i = 0, il = array.length; i < il; i ++ ) {
 
-			return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
+						array[ i ] = 0;
 
-		},
+					}
 
-		updateMatrixWorld: function ( force ) {
+				}
 
-			Object3D.prototype.updateMatrixWorld.call( this, force );
+				var normals = attributes.normal.array;
 
-			this.matrixWorldInverse.getInverse( this.matrixWorld );
+				var vA, vB, vC;
+				var pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
+				var cb = new Vector3(), ab = new Vector3();
 
-		},
+				// indexed elements
 
-		updateWorldMatrix: function ( updateParents, updateChildren ) {
+				if ( index ) {
 
-			Object3D.prototype.updateWorldMatrix.call( this, updateParents, updateChildren );
+					var indices = index.array;
 
-			this.matrixWorldInverse.getInverse( this.matrixWorld );
+					for ( var i = 0, il = index.count; i < il; i += 3 ) {
 
-		},
+						vA = indices[ i + 0 ] * 3;
+						vB = indices[ i + 1 ] * 3;
+						vC = indices[ i + 2 ] * 3;
 
-		clone: function () {
+						pA.fromArray( positions, vA );
+						pB.fromArray( positions, vB );
+						pC.fromArray( positions, vC );
 
-			return new this.constructor().copy( this );
+						cb.subVectors( pC, pB );
+						ab.subVectors( pA, pB );
+						cb.cross( ab );
 
-		}
+						normals[ vA ] += cb.x;
+						normals[ vA + 1 ] += cb.y;
+						normals[ vA + 2 ] += cb.z;
 
-	} );
+						normals[ vB ] += cb.x;
+						normals[ vB + 1 ] += cb.y;
+						normals[ vB + 2 ] += cb.z;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author greggman / http://games.greggman.com/
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * @author tschw
-	 */
+						normals[ vC ] += cb.x;
+						normals[ vC + 1 ] += cb.y;
+						normals[ vC + 2 ] += cb.z;
 
-	function PerspectiveCamera( fov, aspect, near, far ) {
+					}
 
-		Camera.call( this );
+				} else {
 
-		this.type = 'PerspectiveCamera';
+					// non-indexed elements (unconnected triangle soup)
 
-		this.fov = fov !== undefined ? fov : 50;
-		this.zoom = 1;
+					for ( var i = 0, il = positions.length; i < il; i += 9 ) {
 
-		this.near = near !== undefined ? near : 0.1;
-		this.far = far !== undefined ? far : 2000;
-		this.focus = 10;
+						pA.fromArray( positions, i );
+						pB.fromArray( positions, i + 3 );
+						pC.fromArray( positions, i + 6 );
 
-		this.aspect = aspect !== undefined ? aspect : 1;
-		this.view = null;
+						cb.subVectors( pC, pB );
+						ab.subVectors( pA, pB );
+						cb.cross( ab );
 
-		this.filmGauge = 35;	// width of the film (default in millimeters)
-		this.filmOffset = 0;	// horizontal film offset (same unit as gauge)
+						normals[ i ] = cb.x;
+						normals[ i + 1 ] = cb.y;
+						normals[ i + 2 ] = cb.z;
 
-		this.updateProjectionMatrix();
+						normals[ i + 3 ] = cb.x;
+						normals[ i + 4 ] = cb.y;
+						normals[ i + 5 ] = cb.z;
 
-	}
-
-	PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
-
-		constructor: PerspectiveCamera,
+						normals[ i + 6 ] = cb.x;
+						normals[ i + 7 ] = cb.y;
+						normals[ i + 8 ] = cb.z;
 
-		isPerspectiveCamera: true,
+					}
 
-		copy: function ( source, recursive ) {
+				}
 
-			Camera.prototype.copy.call( this, source, recursive );
+				this.normalizeNormals();
 
-			this.fov = source.fov;
-			this.zoom = source.zoom;
+				attributes.normal.needsUpdate = true;
 
-			this.near = source.near;
-			this.far = source.far;
-			this.focus = source.focus;
+			}
 
-			this.aspect = source.aspect;
-			this.view = source.view === null ? null : Object.assign( {}, source.view );
+		},
 
-			this.filmGauge = source.filmGauge;
-			this.filmOffset = source.filmOffset;
+		merge: function ( geometry, offset ) {
 
-			return this;
+			if ( ! ( geometry && geometry.isBufferGeometry ) ) {
 
-		},
+				console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
+				return;
 
-		/**
-		 * Sets the FOV by focal length in respect to the current .filmGauge.
-		 *
-		 * The default film gauge is 35, so that the focal length can be specified for
-		 * a 35mm (full frame) camera.
-		 *
-		 * Values for focal length and film gauge must have the same unit.
-		 */
-		setFocalLength: function ( focalLength ) {
+			}
 
-			// see http://www.bobatkins.com/photography/technical/field_of_view.html
-			var vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
+			if ( offset === undefined ) {
 
-			this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope );
-			this.updateProjectionMatrix();
+				offset = 0;
 
-		},
+				console.warn(
+					'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. '
+					+ 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.'
+				);
 
-		/**
-		 * Calculates the focal length from the current .fov and .filmGauge.
-		 */
-		getFocalLength: function () {
+			}
 
-			var vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
+			var attributes = this.attributes;
 
-			return 0.5 * this.getFilmHeight() / vExtentSlope;
+			for ( var key in attributes ) {
 
-		},
+				if ( geometry.attributes[ key ] === undefined ) { continue; }
 
-		getEffectiveFOV: function () {
+				var attribute1 = attributes[ key ];
+				var attributeArray1 = attribute1.array;
 
-			return MathUtils.RAD2DEG * 2 * Math.atan(
-				Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom );
+				var attribute2 = geometry.attributes[ key ];
+				var attributeArray2 = attribute2.array;
 
-		},
+				var attributeOffset = attribute2.itemSize * offset;
+				var length = Math.min( attributeArray2.length, attributeArray1.length - attributeOffset );
 
-		getFilmWidth: function () {
+				for ( var i = 0, j = attributeOffset; i < length; i ++, j ++ ) {
 
-			// film not completely covered in portrait format (aspect < 1)
-			return this.filmGauge * Math.min( this.aspect, 1 );
+					attributeArray1[ j ] = attributeArray2[ i ];
 
-		},
+				}
 
-		getFilmHeight: function () {
+			}
 
-			// film not completely covered in landscape format (aspect > 1)
-			return this.filmGauge / Math.max( this.aspect, 1 );
+			return this;
 
 		},
 
-		/**
-		 * Sets an offset in a larger frustum. This is useful for multi-window or
-		 * multi-monitor/multi-machine setups.
-		 *
-		 * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
-		 * the monitors are in grid like this
-		 *
-		 *   +---+---+---+
-		 *   | A | B | C |
-		 *   +---+---+---+
-		 *   | D | E | F |
-		 *   +---+---+---+
-		 *
-		 * then for each monitor you would call it like this
-		 *
-		 *   var w = 1920;
-		 *   var h = 1080;
-		 *   var fullWidth = w * 3;
-		 *   var fullHeight = h * 2;
-		 *
-		 *   --A--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
-		 *   --B--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
-		 *   --C--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
-		 *   --D--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
-		 *   --E--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
-		 *   --F--
-		 *   camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
-		 *
-		 *   Note there is no reason monitors have to be the same size or in a grid.
-		 */
-		setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
+		normalizeNormals: function () {
 
-			this.aspect = fullWidth / fullHeight;
+			var normals = this.attributes.normal;
 
-			if ( this.view === null ) {
+			for ( var i = 0, il = normals.count; i < il; i ++ ) {
 
-				this.view = {
-					enabled: true,
-					fullWidth: 1,
-					fullHeight: 1,
-					offsetX: 0,
-					offsetY: 0,
-					width: 1,
-					height: 1
-				};
+				_vector$4.x = normals.getX( i );
+				_vector$4.y = normals.getY( i );
+				_vector$4.z = normals.getZ( i );
 
-			}
+				_vector$4.normalize();
 
-			this.view.enabled = true;
-			this.view.fullWidth = fullWidth;
-			this.view.fullHeight = fullHeight;
-			this.view.offsetX = x;
-			this.view.offsetY = y;
-			this.view.width = width;
-			this.view.height = height;
+				normals.setXYZ( i, _vector$4.x, _vector$4.y, _vector$4.z );
 
-			this.updateProjectionMatrix();
+			}
 
 		},
 
-		clearViewOffset: function () {
+		toNonIndexed: function () {
 
-			if ( this.view !== null ) {
+			function convertBufferAttribute( attribute, indices ) {
 
-				this.view.enabled = false;
+				var array = attribute.array;
+				var itemSize = attribute.itemSize;
 
-			}
+				var array2 = new array.constructor( indices.length * itemSize );
 
-			this.updateProjectionMatrix();
+				var index = 0, index2 = 0;
 
-		},
+				for ( var i = 0, l = indices.length; i < l; i ++ ) {
 
-		updateProjectionMatrix: function () {
+					index = indices[ i ] * itemSize;
 
-			var near = this.near,
-				top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom,
-				height = 2 * top,
-				width = this.aspect * height,
-				left = - 0.5 * width,
-				view = this.view;
+					for ( var j = 0; j < itemSize; j ++ ) {
 
-			if ( this.view !== null && this.view.enabled ) {
+						array2[ index2 ++ ] = array[ index ++ ];
 
-				var fullWidth = view.fullWidth,
-					fullHeight = view.fullHeight;
+					}
 
-				left += view.offsetX * width / fullWidth;
-				top -= view.offsetY * height / fullHeight;
-				width *= view.width / fullWidth;
-				height *= view.height / fullHeight;
+				}
 
-			}
+				return new BufferAttribute( array2, itemSize );
 
-			var skew = this.filmOffset;
-			if ( skew !== 0 ) { left += near * skew / this.getFilmWidth(); }
+			}
 
-			this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );
+			//
 
-			this.projectionMatrixInverse.getInverse( this.projectionMatrix );
+			if ( this.index === null ) {
 
-		},
+				console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
+				return this;
 
-		toJSON: function ( meta ) {
+			}
 
-			var data = Object3D.prototype.toJSON.call( this, meta );
+			var geometry2 = new BufferGeometry();
 
-			data.object.fov = this.fov;
-			data.object.zoom = this.zoom;
+			var indices = this.index.array;
+			var attributes = this.attributes;
 
-			data.object.near = this.near;
-			data.object.far = this.far;
-			data.object.focus = this.focus;
+			// attributes
 
-			data.object.aspect = this.aspect;
+			for ( var name in attributes ) {
 
-			if ( this.view !== null ) { data.object.view = Object.assign( {}, this.view ); }
+				var attribute = attributes[ name ];
 
-			data.object.filmGauge = this.filmGauge;
-			data.object.filmOffset = this.filmOffset;
+				var newAttribute = convertBufferAttribute( attribute, indices );
 
-			return data;
+				geometry2.setAttribute( name, newAttribute );
 
-		}
+			}
 
-	} );
+			// morph attributes
 
-	/**
-	 * Camera for rendering cube maps
-	 *	- renders scene into axis-aligned cube
-	 *
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+			var morphAttributes = this.morphAttributes;
 
-	var fov = 90, aspect = 1;
+			for ( name in morphAttributes ) {
 
-	function CubeCamera( near, far, cubeResolution, options ) {
+				var morphArray = [];
+				var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
 
-		Object3D.call( this );
+				for ( var i = 0, il = morphAttribute.length; i < il; i ++ ) {
 
-		this.type = 'CubeCamera';
+					var attribute = morphAttribute[ i ];
 
-		var cameraPX = new PerspectiveCamera( fov, aspect, near, far );
-		cameraPX.up.set( 0, - 1, 0 );
-		cameraPX.lookAt( new Vector3( 1, 0, 0 ) );
-		this.add( cameraPX );
+					var newAttribute = convertBufferAttribute( attribute, indices );
 
-		var cameraNX = new PerspectiveCamera( fov, aspect, near, far );
-		cameraNX.up.set( 0, - 1, 0 );
-		cameraNX.lookAt( new Vector3( - 1, 0, 0 ) );
-		this.add( cameraNX );
+					morphArray.push( newAttribute );
 
-		var cameraPY = new PerspectiveCamera( fov, aspect, near, far );
-		cameraPY.up.set( 0, 0, 1 );
-		cameraPY.lookAt( new Vector3( 0, 1, 0 ) );
-		this.add( cameraPY );
+				}
 
-		var cameraNY = new PerspectiveCamera( fov, aspect, near, far );
-		cameraNY.up.set( 0, 0, - 1 );
-		cameraNY.lookAt( new Vector3( 0, - 1, 0 ) );
-		this.add( cameraNY );
+				geometry2.morphAttributes[ name ] = morphArray;
 
-		var cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
-		cameraPZ.up.set( 0, - 1, 0 );
-		cameraPZ.lookAt( new Vector3( 0, 0, 1 ) );
-		this.add( cameraPZ );
+			}
 
-		var cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
-		cameraNZ.up.set( 0, - 1, 0 );
-		cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) );
-		this.add( cameraNZ );
+			geometry2.morphTargetsRelative = this.morphTargetsRelative;
 
-		options = options || { format: RGBFormat, magFilter: LinearFilter, minFilter: LinearFilter };
+			// groups
 
-		this.renderTarget = new WebGLCubeRenderTarget( cubeResolution, options );
-		this.renderTarget.texture.name = "CubeCamera";
+			var groups = this.groups;
 
-		this.update = function ( renderer, scene ) {
+			for ( var i = 0, l = groups.length; i < l; i ++ ) {
 
-			if ( this.parent === null ) { this.updateMatrixWorld(); }
+				var group = groups[ i ];
+				geometry2.addGroup( group.start, group.count, group.materialIndex );
 
-			var currentRenderTarget = renderer.getRenderTarget();
+			}
 
-			var renderTarget = this.renderTarget;
-			var generateMipmaps = renderTarget.texture.generateMipmaps;
+			return geometry2;
 
-			renderTarget.texture.generateMipmaps = false;
+		},
 
-			renderer.setRenderTarget( renderTarget, 0 );
-			renderer.render( scene, cameraPX );
+		toJSON: function () {
 
-			renderer.setRenderTarget( renderTarget, 1 );
-			renderer.render( scene, cameraNX );
+			var data = {
+				metadata: {
+					version: 4.5,
+					type: 'BufferGeometry',
+					generator: 'BufferGeometry.toJSON'
+				}
+			};
 
-			renderer.setRenderTarget( renderTarget, 2 );
-			renderer.render( scene, cameraPY );
+			// standard BufferGeometry serialization
 
-			renderer.setRenderTarget( renderTarget, 3 );
-			renderer.render( scene, cameraNY );
+			data.uuid = this.uuid;
+			data.type = this.type;
+			if ( this.name !== '' ) { data.name = this.name; }
+			if ( Object.keys( this.userData ).length > 0 ) { data.userData = this.userData; }
 
-			renderer.setRenderTarget( renderTarget, 4 );
-			renderer.render( scene, cameraPZ );
+			if ( this.parameters !== undefined ) {
 
-			renderTarget.texture.generateMipmaps = generateMipmaps;
+				var parameters = this.parameters;
 
-			renderer.setRenderTarget( renderTarget, 5 );
-			renderer.render( scene, cameraNZ );
+				for ( var key in parameters ) {
 
-			renderer.setRenderTarget( currentRenderTarget );
+					if ( parameters[ key ] !== undefined ) { data[ key ] = parameters[ key ]; }
 
-		};
+				}
 
-		this.clear = function ( renderer, color, depth, stencil ) {
+				return data;
 
-			var currentRenderTarget = renderer.getRenderTarget();
+			}
 
-			var renderTarget = this.renderTarget;
+			data.data = { attributes: {} };
 
-			for ( var i = 0; i < 6; i ++ ) {
+			var index = this.index;
 
-				renderer.setRenderTarget( renderTarget, i );
+			if ( index !== null ) {
 
-				renderer.clear( color, depth, stencil );
+				data.data.index = {
+					type: index.array.constructor.name,
+					array: Array.prototype.slice.call( index.array )
+				};
 
 			}
 
-			renderer.setRenderTarget( currentRenderTarget );
+			var attributes = this.attributes;
 
-		};
+			for ( var key in attributes ) {
 
-	}
+				var attribute = attributes[ key ];
 
-	CubeCamera.prototype = Object.create( Object3D.prototype );
-	CubeCamera.prototype.constructor = CubeCamera;
+				var attributeData = attribute.toJSON();
 
-	/**
-	 * @author alteredq / http://alteredqualia.com
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+				if ( attribute.name !== '' ) { attributeData.name = attribute.name; }
 
-	function WebGLCubeRenderTarget( size, options, dummy ) {
+				data.data.attributes[ key ] = attributeData;
 
-		if ( Number.isInteger( options ) ) {
+			}
 
-			console.warn( 'THREE.WebGLCubeRenderTarget: constructor signature is now WebGLCubeRenderTarget( size, options )' );
+			var morphAttributes = {};
+			var hasMorphAttributes = false;
 
-			options = dummy;
+			for ( var key in this.morphAttributes ) {
 
-		}
+				var attributeArray = this.morphAttributes[ key ];
 
-		WebGLRenderTarget.call( this, size, size, options );
+				var array = [];
 
-	}
+				for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
 
-	WebGLCubeRenderTarget.prototype = Object.create( WebGLRenderTarget.prototype );
-	WebGLCubeRenderTarget.prototype.constructor = WebGLCubeRenderTarget;
+					var attribute = attributeArray[ i ];
 
-	WebGLCubeRenderTarget.prototype.isWebGLCubeRenderTarget = true;
+					var attributeData = attribute.toJSON();
 
-	WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer, texture ) {
+					if ( attribute.name !== '' ) { attributeData.name = attribute.name; }
 
-		this.texture.type = texture.type;
-		this.texture.format = texture.format;
-		this.texture.encoding = texture.encoding;
+					array.push( attributeData );
 
-		var scene = new Scene();
+				}
 
-		var shader = {
+				if ( array.length > 0 ) {
 
-			uniforms: {
-				tEquirect: { value: null },
-			},
+					morphAttributes[ key ] = array;
 
-			vertexShader: [
+					hasMorphAttributes = true;
 
-				"varying vec3 vWorldDirection;",
+				}
 
-				"vec3 transformDirection( in vec3 dir, in mat4 matrix ) {",
+			}
 
-				"	return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );",
+			if ( hasMorphAttributes ) {
 
-				"}",
+				data.data.morphAttributes = morphAttributes;
+				data.data.morphTargetsRelative = this.morphTargetsRelative;
 
-				"void main() {",
+			}
 
-				"	vWorldDirection = transformDirection( position, modelMatrix );",
+			var groups = this.groups;
 
-				"	#include <begin_vertex>",
-				"	#include <project_vertex>",
+			if ( groups.length > 0 ) {
 
-				"}"
+				data.data.groups = JSON.parse( JSON.stringify( groups ) );
 
-			].join( '\n' ),
+			}
 
-			fragmentShader: [
+			var boundingSphere = this.boundingSphere;
 
-				"uniform sampler2D tEquirect;",
+			if ( boundingSphere !== null ) {
 
-				"varying vec3 vWorldDirection;",
+				data.data.boundingSphere = {
+					center: boundingSphere.center.toArray(),
+					radius: boundingSphere.radius
+				};
 
-				"#define RECIPROCAL_PI 0.31830988618",
-				"#define RECIPROCAL_PI2 0.15915494",
+			}
 
-				"void main() {",
+			return data;
 
-				"	vec3 direction = normalize( vWorldDirection );",
+		},
 
-				"	vec2 sampleUV;",
+		clone: function () {
 
-				"	sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;",
+			/*
+			 // Handle primitives
 
-				"	sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
+			 var parameters = this.parameters;
 
-				"	gl_FragColor = texture2D( tEquirect, sampleUV );",
+			 if ( parameters !== undefined ) {
 
-				"}"
+			 var values = [];
 
-			].join( '\n' ),
-		};
+			 for ( var key in parameters ) {
 
-		var material = new ShaderMaterial( {
+			 values.push( parameters[ key ] );
 
-			type: 'CubemapFromEquirect',
+			 }
 
-			uniforms: cloneUniforms( shader.uniforms ),
-			vertexShader: shader.vertexShader,
-			fragmentShader: shader.fragmentShader,
-			side: BackSide,
-			blending: NoBlending
+			 var geometry = Object.create( this.constructor.prototype );
+			 this.constructor.apply( geometry, values );
+			 return geometry;
 
-		} );
+			 }
 
-		material.uniforms.tEquirect.value = texture;
+			 return new this.constructor().copy( this );
+			 */
 
-		var mesh = new Mesh( new BoxBufferGeometry( 5, 5, 5 ), material );
+			return new BufferGeometry().copy( this );
 
-		scene.add( mesh );
+		},
 
-		var camera = new CubeCamera( 1, 10, 1 );
+		copy: function ( source ) {
 
-		camera.renderTarget = this;
-		camera.renderTarget.texture.name = 'CubeCameraTexture';
+			var name, i, l;
 
-		camera.update( renderer, scene );
+			// reset
 
-		mesh.geometry.dispose();
-		mesh.material.dispose();
+			this.index = null;
+			this.attributes = {};
+			this.morphAttributes = {};
+			this.groups = [];
+			this.boundingBox = null;
+			this.boundingSphere = null;
 
-		return this;
+			// name
 
-	};
+			this.name = source.name;
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+			// index
 
-	function DataTexture( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) {
+			var index = source.index;
 
-		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
+			if ( index !== null ) {
 
-		this.image = { data: data || null, width: width || 1, height: height || 1 };
+				this.setIndex( index.clone() );
 
-		this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
-		this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
+			}
 
-		this.generateMipmaps = false;
-		this.flipY = false;
-		this.unpackAlignment = 1;
+			// attributes
 
-		this.needsUpdate = true;
+			var attributes = source.attributes;
 
-	}
+			for ( name in attributes ) {
 
-	DataTexture.prototype = Object.create( Texture.prototype );
-	DataTexture.prototype.constructor = DataTexture;
+				var attribute = attributes[ name ];
+				this.setAttribute( name, attribute.clone() );
 
-	DataTexture.prototype.isDataTexture = true;
+			}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author bhouston / http://clara.io
-	 */
+			// morph attributes
 
-	var _sphere$1 = new Sphere();
-	var _vector$5 = new Vector3();
+			var morphAttributes = source.morphAttributes;
 
-	function Frustum( p0, p1, p2, p3, p4, p5 ) {
+			for ( name in morphAttributes ) {
 
-		this.planes = [
+				var array = [];
+				var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
 
-			( p0 !== undefined ) ? p0 : new Plane(),
-			( p1 !== undefined ) ? p1 : new Plane(),
-			( p2 !== undefined ) ? p2 : new Plane(),
-			( p3 !== undefined ) ? p3 : new Plane(),
-			( p4 !== undefined ) ? p4 : new Plane(),
-			( p5 !== undefined ) ? p5 : new Plane()
-
-		];
+				for ( i = 0, l = morphAttribute.length; i < l; i ++ ) {
 
-	}
+					array.push( morphAttribute[ i ].clone() );
 
-	Object.assign( Frustum.prototype, {
+				}
 
-		set: function ( p0, p1, p2, p3, p4, p5 ) {
+				this.morphAttributes[ name ] = array;
 
-			var planes = this.planes;
+			}
 
-			planes[ 0 ].copy( p0 );
-			planes[ 1 ].copy( p1 );
-			planes[ 2 ].copy( p2 );
-			planes[ 3 ].copy( p3 );
-			planes[ 4 ].copy( p4 );
-			planes[ 5 ].copy( p5 );
+			this.morphTargetsRelative = source.morphTargetsRelative;
 
-			return this;
+			// groups
 
-		},
+			var groups = source.groups;
 
-		clone: function () {
+			for ( i = 0, l = groups.length; i < l; i ++ ) {
 
-			return new this.constructor().copy( this );
+				var group = groups[ i ];
+				this.addGroup( group.start, group.count, group.materialIndex );
 
-		},
+			}
 
-		copy: function ( frustum ) {
+			// bounding box
 
-			var planes = this.planes;
+			var boundingBox = source.boundingBox;
 
-			for ( var i = 0; i < 6; i ++ ) {
+			if ( boundingBox !== null ) {
 
-				planes[ i ].copy( frustum.planes[ i ] );
+				this.boundingBox = boundingBox.clone();
 
 			}
 
-			return this;
+			// bounding sphere
 
-		},
+			var boundingSphere = source.boundingSphere;
 
-		setFromProjectionMatrix: function ( m ) {
+			if ( boundingSphere !== null ) {
 
-			var planes = this.planes;
-			var me = m.elements;
-			var me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
-			var me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
-			var me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
-			var me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
+				this.boundingSphere = boundingSphere.clone();
 
-			planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
-			planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
-			planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
-			planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
-			planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
-			planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
+			}
 
-			return this;
+			// draw range
 
-		},
+			this.drawRange.start = source.drawRange.start;
+			this.drawRange.count = source.drawRange.count;
 
-		intersectsObject: function ( object ) {
+			// user data
 
-			var geometry = object.geometry;
+			this.userData = source.userData;
 
-			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
+			return this;
 
-			_sphere$1.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
+		},
 
-			return this.intersectsSphere( _sphere$1 );
+		dispose: function () {
 
-		},
+			this.dispatchEvent( { type: 'dispose' } );
 
-		intersectsSprite: function ( sprite ) {
+		}
 
-			_sphere$1.center.set( 0, 0, 0 );
-			_sphere$1.radius = 0.7071067811865476;
-			_sphere$1.applyMatrix4( sprite.matrixWorld );
+	} );
 
-			return this.intersectsSphere( _sphere$1 );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author jonobr1 / http://jonobr1.com/
+	 */
 
-		},
+	var _inverseMatrix = new Matrix4();
+	var _ray = new Ray();
+	var _sphere = new Sphere();
 
-		intersectsSphere: function ( sphere ) {
+	var _vA = new Vector3();
+	var _vB = new Vector3();
+	var _vC = new Vector3();
 
-			var planes = this.planes;
-			var center = sphere.center;
-			var negRadius = - sphere.radius;
+	var _tempA = new Vector3();
+	var _tempB = new Vector3();
+	var _tempC = new Vector3();
 
-			for ( var i = 0; i < 6; i ++ ) {
+	var _morphA = new Vector3();
+	var _morphB = new Vector3();
+	var _morphC = new Vector3();
 
-				var distance = planes[ i ].distanceToPoint( center );
+	var _uvA = new Vector2();
+	var _uvB = new Vector2();
+	var _uvC = new Vector2();
 
-				if ( distance < negRadius ) {
+	var _intersectionPoint = new Vector3();
+	var _intersectionPointWorld = new Vector3();
 
-					return false;
+	function Mesh( geometry, material ) {
 
-				}
+		Object3D.call( this );
 
-			}
+		this.type = 'Mesh';
 
-			return true;
+		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
+		this.material = material !== undefined ? material : new MeshBasicMaterial();
 
-		},
+		this.updateMorphTargets();
 
-		intersectsBox: function ( box ) {
+	}
 
-			var planes = this.planes;
+	Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-			for ( var i = 0; i < 6; i ++ ) {
+		constructor: Mesh,
 
-				var plane = planes[ i ];
+		isMesh: true,
 
-				// corner at max distance
+		copy: function ( source ) {
 
-				_vector$5.x = plane.normal.x > 0 ? box.max.x : box.min.x;
-				_vector$5.y = plane.normal.y > 0 ? box.max.y : box.min.y;
-				_vector$5.z = plane.normal.z > 0 ? box.max.z : box.min.z;
+			Object3D.prototype.copy.call( this, source );
 
-				if ( plane.distanceToPoint( _vector$5 ) < 0 ) {
+			if ( source.morphTargetInfluences !== undefined ) {
 
-					return false;
+				this.morphTargetInfluences = source.morphTargetInfluences.slice();
 
-				}
+			}
+
+			if ( source.morphTargetDictionary !== undefined ) {
+
+				this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
 
 			}
 
-			return true;
+			return this;
 
 		},
 
-		containsPoint: function ( point ) {
+		updateMorphTargets: function () {
 
-			var planes = this.planes;
+			var geometry = this.geometry;
+			var m, ml, name;
 
-			for ( var i = 0; i < 6; i ++ ) {
+			if ( geometry.isBufferGeometry ) {
 
-				if ( planes[ i ].distanceToPoint( point ) < 0 ) {
+				var morphAttributes = geometry.morphAttributes;
+				var keys = Object.keys( morphAttributes );
 
-					return false;
+				if ( keys.length > 0 ) {
 
-				}
+					var morphAttribute = morphAttributes[ keys[ 0 ] ];
 
-			}
+					if ( morphAttribute !== undefined ) {
 
-			return true;
+						this.morphTargetInfluences = [];
+						this.morphTargetDictionary = {};
 
-		}
+						for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
 
-	} );
+							name = morphAttribute[ m ].name || String( m );
 
-	/**
-	 * Uniforms library for shared webgl shaders
-	 */
+							this.morphTargetInfluences.push( 0 );
+							this.morphTargetDictionary[ name ] = m;
 
-	var UniformsLib = {
+						}
 
-		common: {
+					}
 
-			diffuse: { value: new Color( 0xeeeeee ) },
-			opacity: { value: 1.0 },
+				}
 
-			map: { value: null },
-			uvTransform: { value: new Matrix3() },
-			uv2Transform: { value: new Matrix3() },
+			} else {
 
-			alphaMap: { value: null },
+				var morphTargets = geometry.morphTargets;
 
-		},
+				if ( morphTargets !== undefined && morphTargets.length > 0 ) {
 
-		specularmap: {
+					console.error( 'THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
 
-			specularMap: { value: null },
+				}
+
+			}
 
 		},
 
-		envmap: {
+		raycast: function ( raycaster, intersects ) {
 
-			envMap: { value: null },
-			flipEnvMap: { value: - 1 },
-			reflectivity: { value: 1.0 },
-			refractionRatio: { value: 0.98 },
-			maxMipLevel: { value: 0 }
+			var geometry = this.geometry;
+			var material = this.material;
+			var matrixWorld = this.matrixWorld;
 
-		},
+			if ( material === undefined ) { return; }
 
-		aomap: {
+			// Checking boundingSphere distance to ray
 
-			aoMap: { value: null },
-			aoMapIntensity: { value: 1 }
+			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
 
-		},
+			_sphere.copy( geometry.boundingSphere );
+			_sphere.applyMatrix4( matrixWorld );
 
-		lightmap: {
+			if ( raycaster.ray.intersectsSphere( _sphere ) === false ) { return; }
 
-			lightMap: { value: null },
-			lightMapIntensity: { value: 1 }
+			//
 
-		},
+			_inverseMatrix.getInverse( matrixWorld );
+			_ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
 
-		emissivemap: {
+			// Check boundingBox before continuing
 
-			emissiveMap: { value: null }
+			if ( geometry.boundingBox !== null ) {
 
-		},
+				if ( _ray.intersectsBox( geometry.boundingBox ) === false ) { return; }
 
-		bumpmap: {
+			}
 
-			bumpMap: { value: null },
-			bumpScale: { value: 1 }
+			var intersection;
 
-		},
+			if ( geometry.isBufferGeometry ) {
 
-		normalmap: {
+				var a, b, c;
+				var index = geometry.index;
+				var position = geometry.attributes.position;
+				var morphPosition = geometry.morphAttributes.position;
+				var morphTargetsRelative = geometry.morphTargetsRelative;
+				var uv = geometry.attributes.uv;
+				var uv2 = geometry.attributes.uv2;
+				var groups = geometry.groups;
+				var drawRange = geometry.drawRange;
+				var i, j, il, jl;
+				var group, groupMaterial;
+				var start, end;
 
-			normalMap: { value: null },
-			normalScale: { value: new Vector2( 1, 1 ) }
+				if ( index !== null ) {
 
-		},
+					// indexed buffer geometry
 
-		displacementmap: {
+					if ( Array.isArray( material ) ) {
 
-			displacementMap: { value: null },
-			displacementScale: { value: 1 },
-			displacementBias: { value: 0 }
+						for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-		},
+							group = groups[ i ];
+							groupMaterial = material[ group.materialIndex ];
 
-		roughnessmap: {
+							start = Math.max( group.start, drawRange.start );
+							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-			roughnessMap: { value: null }
+							for ( j = start, jl = end; j < jl; j += 3 ) {
 
-		},
+								a = index.getX( j );
+								b = index.getX( j + 1 );
+								c = index.getX( j + 2 );
 
-		metalnessmap: {
+								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-			metalnessMap: { value: null }
+								if ( intersection ) {
 
-		},
+									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
+									intersection.face.materialIndex = group.materialIndex;
+									intersects.push( intersection );
 
-		gradientmap: {
+								}
 
-			gradientMap: { value: null }
+							}
 
-		},
+						}
 
-		fog: {
+					} else {
 
-			fogDensity: { value: 0.00025 },
-			fogNear: { value: 1 },
-			fogFar: { value: 2000 },
-			fogColor: { value: new Color( 0xffffff ) }
+						start = Math.max( 0, drawRange.start );
+						end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
 
-		},
+						for ( i = start, il = end; i < il; i += 3 ) {
 
-		lights: {
+							a = index.getX( i );
+							b = index.getX( i + 1 );
+							c = index.getX( i + 2 );
 
-			ambientLightColor: { value: [] },
+							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-			lightProbe: { value: [] },
+							if ( intersection ) {
 
-			directionalLights: { value: [], properties: {
-				direction: {},
-				color: {}
-			} },
+								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
+								intersects.push( intersection );
 
-			directionalLightShadows: { value: [], properties: {
-				shadowBias: {},
-				shadowRadius: {},
-				shadowMapSize: {}
-			} },
+							}
 
-			directionalShadowMap: { value: [] },
-			directionalShadowMatrix: { value: [] },
+						}
 
-			spotLights: { value: [], properties: {
-				color: {},
-				position: {},
-				direction: {},
-				distance: {},
-				coneCos: {},
-				penumbraCos: {},
-				decay: {}
-			} },
+					}
 
-			spotLightShadows: { value: [], properties: {
-				shadowBias: {},
-				shadowRadius: {},
-				shadowMapSize: {}
-			} },
+				} else if ( position !== undefined ) {
 
-			spotShadowMap: { value: [] },
-			spotShadowMatrix: { value: [] },
+					// non-indexed buffer geometry
 
-			pointLights: { value: [], properties: {
-				color: {},
-				position: {},
-				decay: {},
-				distance: {}
-			} },
+					if ( Array.isArray( material ) ) {
 
-			pointLightShadows: { value: [], properties: {
-				shadowBias: {},
-				shadowRadius: {},
-				shadowMapSize: {},
-				shadowCameraNear: {},
-				shadowCameraFar: {}
-			} },
+						for ( i = 0, il = groups.length; i < il; i ++ ) {
 
-			pointShadowMap: { value: [] },
-			pointShadowMatrix: { value: [] },
+							group = groups[ i ];
+							groupMaterial = material[ group.materialIndex ];
 
-			hemisphereLights: { value: [], properties: {
-				direction: {},
-				skyColor: {},
-				groundColor: {}
-			} },
+							start = Math.max( group.start, drawRange.start );
+							end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
 
-			// TODO (abelnation): RectAreaLight BRDF data needs to be moved from example to main src
-			rectAreaLights: { value: [], properties: {
-				color: {},
-				position: {},
-				width: {},
-				height: {}
-			} }
+							for ( j = start, jl = end; j < jl; j += 3 ) {
 
-		},
+								a = j;
+								b = j + 1;
+								c = j + 2;
 
-		points: {
+								intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-			diffuse: { value: new Color( 0xeeeeee ) },
-			opacity: { value: 1.0 },
-			size: { value: 1.0 },
-			scale: { value: 1.0 },
-			map: { value: null },
-			alphaMap: { value: null },
-			uvTransform: { value: new Matrix3() }
+								if ( intersection ) {
 
-		},
+									intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
+									intersection.face.materialIndex = group.materialIndex;
+									intersects.push( intersection );
 
-		sprite: {
+								}
 
-			diffuse: { value: new Color( 0xeeeeee ) },
-			opacity: { value: 1.0 },
-			center: { value: new Vector2( 0.5, 0.5 ) },
-			rotation: { value: 0.0 },
-			map: { value: null },
-			alphaMap: { value: null },
-			uvTransform: { value: new Matrix3() }
+							}
 
-		}
+						}
 
-	};
+					} else {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+						start = Math.max( 0, drawRange.start );
+						end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
 
-	function WebGLAnimation() {
+						for ( i = start, il = end; i < il; i += 3 ) {
 
-		var context = null;
-		var isAnimating = false;
-		var animationLoop = null;
+							a = i;
+							b = i + 1;
+							c = i + 2;
 
-		function onAnimationFrame( time, frame ) {
+							intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c );
 
-			if ( isAnimating === false ) { return; }
+							if ( intersection ) {
 
-			animationLoop( time, frame );
+								intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
+								intersects.push( intersection );
 
-			context.requestAnimationFrame( onAnimationFrame );
+							}
 
-		}
+						}
 
-		return {
+					}
 
-			start: function () {
+				}
 
-				if ( isAnimating === true ) { return; }
-				if ( animationLoop === null ) { return; }
+			} else if ( geometry.isGeometry ) {
 
-				context.requestAnimationFrame( onAnimationFrame );
+				var fvA, fvB, fvC;
+				var isMultiMaterial = Array.isArray( material );
 
-				isAnimating = true;
+				var vertices = geometry.vertices;
+				var faces = geometry.faces;
+				var uvs;
 
-			},
+				var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
+				if ( faceVertexUvs.length > 0 ) { uvs = faceVertexUvs; }
 
-			stop: function () {
+				for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
 
-				isAnimating = false;
+					var face = faces[ f ];
+					var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
 
-			},
+					if ( faceMaterial === undefined ) { continue; }
 
-			setAnimationLoop: function ( callback ) {
+					fvA = vertices[ face.a ];
+					fvB = vertices[ face.b ];
+					fvC = vertices[ face.c ];
 
-				animationLoop = callback;
+					intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
 
-			},
+					if ( intersection ) {
 
-			setContext: function ( value ) {
+						if ( uvs && uvs[ f ] ) {
 
-				context = value;
+							var uvs_f = uvs[ f ];
+							_uvA.copy( uvs_f[ 0 ] );
+							_uvB.copy( uvs_f[ 1 ] );
+							_uvC.copy( uvs_f[ 2 ] );
 
-			}
+							intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
 
-		};
+						}
 
-	}
+						intersection.face = face;
+						intersection.faceIndex = f;
+						intersects.push( intersection );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+					}
 
-	function WebGLAttributes( gl, capabilities ) {
+				}
 
-		var isWebGL2 = capabilities.isWebGL2;
+			}
 
-		var buffers = new WeakMap();
+		},
 
-		function createBuffer( attribute, bufferType ) {
+		clone: function () {
 
-			var array = attribute.array;
-			var usage = attribute.usage;
+			return new this.constructor( this.geometry, this.material ).copy( this );
 
-			var buffer = gl.createBuffer();
+		}
 
-			gl.bindBuffer( bufferType, buffer );
-			gl.bufferData( bufferType, array, usage );
+	} );
 
-			attribute.onUploadCallback();
+	function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
 
-			var type = 5126;
+		var intersect;
 
-			if ( array instanceof Float32Array ) {
+		if ( material.side === BackSide ) {
 
-				type = 5126;
+			intersect = ray.intersectTriangle( pC, pB, pA, true, point );
 
-			} else if ( array instanceof Float64Array ) {
-
-				console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' );
-
-			} else if ( array instanceof Uint16Array ) {
-
-				type = 5123;
-
-			} else if ( array instanceof Int16Array ) {
-
-				type = 5122;
-
-			} else if ( array instanceof Uint32Array ) {
+		} else {
 
-				type = 5125;
+			intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
 
-			} else if ( array instanceof Int32Array ) {
+		}
 
-				type = 5124;
+		if ( intersect === null ) { return null; }
 
-			} else if ( array instanceof Int8Array ) {
+		_intersectionPointWorld.copy( point );
+		_intersectionPointWorld.applyMatrix4( object.matrixWorld );
 
-				type = 5120;
+		var distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
 
-			} else if ( array instanceof Uint8Array ) {
+		if ( distance < raycaster.near || distance > raycaster.far ) { return null; }
 
-				type = 5121;
+		return {
+			distance: distance,
+			point: _intersectionPointWorld.clone(),
+			object: object
+		};
 
-			}
+	}
 
-			return {
-				buffer: buffer,
-				type: type,
-				bytesPerElement: array.BYTES_PER_ELEMENT,
-				version: attribute.version
-			};
+	function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ) {
 
-		}
+		_vA.fromBufferAttribute( position, a );
+		_vB.fromBufferAttribute( position, b );
+		_vC.fromBufferAttribute( position, c );
 
-		function updateBuffer( buffer, attribute, bufferType ) {
+		var morphInfluences = object.morphTargetInfluences;
 
-			var array = attribute.array;
-			var updateRange = attribute.updateRange;
+		if ( material.morphTargets && morphPosition && morphInfluences ) {
 
-			gl.bindBuffer( bufferType, buffer );
+			_morphA.set( 0, 0, 0 );
+			_morphB.set( 0, 0, 0 );
+			_morphC.set( 0, 0, 0 );
 
-			if ( updateRange.count === - 1 ) {
+			for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {
 
-				// Not using update ranges
+				var influence = morphInfluences[ i ];
+				var morphAttribute = morphPosition[ i ];
 
-				gl.bufferSubData( bufferType, 0, array );
+				if ( influence === 0 ) { continue; }
 
-			} else {
+				_tempA.fromBufferAttribute( morphAttribute, a );
+				_tempB.fromBufferAttribute( morphAttribute, b );
+				_tempC.fromBufferAttribute( morphAttribute, c );
 
-				if ( isWebGL2 ) {
+				if ( morphTargetsRelative ) {
 
-					gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
-						array, updateRange.offset, updateRange.count );
+					_morphA.addScaledVector( _tempA, influence );
+					_morphB.addScaledVector( _tempB, influence );
+					_morphC.addScaledVector( _tempC, influence );
 
 				} else {
 
-					gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
-						array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
+					_morphA.addScaledVector( _tempA.sub( _vA ), influence );
+					_morphB.addScaledVector( _tempB.sub( _vB ), influence );
+					_morphC.addScaledVector( _tempC.sub( _vC ), influence );
 
 				}
 
-				updateRange.count = - 1; // reset range
-
 			}
 
-		}
-
-		//
+			_vA.add( _morphA );
+			_vB.add( _morphB );
+			_vC.add( _morphC );
 
-		function get( attribute ) {
+		}
 
-			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
+		if ( object.isSkinnedMesh ) {
 
-			return buffers.get( attribute );
+			object.boneTransform( a, _vA );
+			object.boneTransform( b, _vB );
+			object.boneTransform( c, _vC );
 
 		}
 
-		function remove( attribute ) {
-
-			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
+		var intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
 
-			var data = buffers.get( attribute );
+		if ( intersection ) {
 
-			if ( data ) {
+			if ( uv ) {
 
-				gl.deleteBuffer( data.buffer );
+				_uvA.fromBufferAttribute( uv, a );
+				_uvB.fromBufferAttribute( uv, b );
+				_uvC.fromBufferAttribute( uv, c );
 
-				buffers.delete( attribute );
+				intersection.uv = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
 
 			}
 
-		}
-
-		function update( attribute, bufferType ) {
-
-			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
-
-			var data = buffers.get( attribute );
-
-			if ( data === undefined ) {
-
-				buffers.set( attribute, createBuffer( attribute, bufferType ) );
-
-			} else if ( data.version < attribute.version ) {
+			if ( uv2 ) {
 
-				updateBuffer( data.buffer, attribute, bufferType );
+				_uvA.fromBufferAttribute( uv2, a );
+				_uvB.fromBufferAttribute( uv2, b );
+				_uvC.fromBufferAttribute( uv2, c );
 
-				data.version = attribute.version;
+				intersection.uv2 = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
 
 			}
 
-		}
+			var face = new Face3( a, b, c );
+			Triangle.getNormal( _vA, _vB, _vC, face.normal );
 
-		return {
+			intersection.face = face;
 
-			get: get,
-			remove: remove,
-			update: update
+		}
 
-		};
+		return intersection;
 
 	}
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author kile / http://kile.stravaganza.org/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * @author bhouston / http://clara.io
 	 */
 
-	// PlaneGeometry
+	var _geometryId = 0; // Geometry uses even numbers as Id
+	var _m1$3 = new Matrix4();
+	var _obj$1 = new Object3D();
+	var _offset$1 = new Vector3();
 
-	function PlaneGeometry( width, height, widthSegments, heightSegments ) {
+	function Geometry() {
 
-		Geometry.call( this );
+		Object.defineProperty( this, 'id', { value: _geometryId += 2 } );
 
-		this.type = 'PlaneGeometry';
+		this.uuid = MathUtils.generateUUID();
 
-		this.parameters = {
-			width: width,
-			height: height,
-			widthSegments: widthSegments,
-			heightSegments: heightSegments
-		};
+		this.name = '';
+		this.type = 'Geometry';
 
-		this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
-		this.mergeVertices();
+		this.vertices = [];
+		this.colors = [];
+		this.faces = [];
+		this.faceVertexUvs = [[]];
 
-	}
+		this.morphTargets = [];
+		this.morphNormals = [];
 
-	PlaneGeometry.prototype = Object.create( Geometry.prototype );
-	PlaneGeometry.prototype.constructor = PlaneGeometry;
+		this.skinWeights = [];
+		this.skinIndices = [];
 
-	// PlaneBufferGeometry
+		this.lineDistances = [];
 
-	function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
+		this.boundingBox = null;
+		this.boundingSphere = null;
 
-		BufferGeometry.call( this );
+		// update flags
 
-		this.type = 'PlaneBufferGeometry';
+		this.elementsNeedUpdate = false;
+		this.verticesNeedUpdate = false;
+		this.uvsNeedUpdate = false;
+		this.normalsNeedUpdate = false;
+		this.colorsNeedUpdate = false;
+		this.lineDistancesNeedUpdate = false;
+		this.groupsNeedUpdate = false;
 
-		this.parameters = {
-			width: width,
-			height: height,
-			widthSegments: widthSegments,
-			heightSegments: heightSegments
-		};
+	}
 
-		width = width || 1;
-		height = height || 1;
+	Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-		var width_half = width / 2;
-		var height_half = height / 2;
+		constructor: Geometry,
 
-		var gridX = Math.floor( widthSegments ) || 1;
-		var gridY = Math.floor( heightSegments ) || 1;
+		isGeometry: true,
 
-		var gridX1 = gridX + 1;
-		var gridY1 = gridY + 1;
+		applyMatrix4: function ( matrix ) {
 
-		var segment_width = width / gridX;
-		var segment_height = height / gridY;
+			var normalMatrix = new Matrix3().getNormalMatrix( matrix );
 
-		var ix, iy;
+			for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
 
-		// buffers
+				var vertex = this.vertices[ i ];
+				vertex.applyMatrix4( matrix );
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+			}
 
-		// generate vertices, normals and uvs
+			for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
 
-		for ( iy = 0; iy < gridY1; iy ++ ) {
+				var face = this.faces[ i ];
+				face.normal.applyMatrix3( normalMatrix ).normalize();
 
-			var y = iy * segment_height - height_half;
+				for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
 
-			for ( ix = 0; ix < gridX1; ix ++ ) {
+					face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
 
-				var x = ix * segment_width - width_half;
+				}
 
-				vertices.push( x, - y, 0 );
+			}
 
-				normals.push( 0, 0, 1 );
+			if ( this.boundingBox !== null ) {
 
-				uvs.push( ix / gridX );
-				uvs.push( 1 - ( iy / gridY ) );
+				this.computeBoundingBox();
 
 			}
 
-		}
+			if ( this.boundingSphere !== null ) {
 
-		// indices
+				this.computeBoundingSphere();
 
-		for ( iy = 0; iy < gridY; iy ++ ) {
+			}
 
-			for ( ix = 0; ix < gridX; ix ++ ) {
+			this.verticesNeedUpdate = true;
+			this.normalsNeedUpdate = true;
 
-				var a = ix + gridX1 * iy;
-				var b = ix + gridX1 * ( iy + 1 );
-				var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
-				var d = ( ix + 1 ) + gridX1 * iy;
+			return this;
 
-				// faces
+		},
 
-				indices.push( a, b, d );
-				indices.push( b, c, d );
+		rotateX: function ( angle ) {
 
-			}
+			// rotate geometry around world x-axis
 
-		}
+			_m1$3.makeRotationX( angle );
 
-		// build geometry
+			this.applyMatrix4( _m1$3 );
 
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+			return this;
 
-	}
+		},
 
-	PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
+		rotateY: function ( angle ) {
 
-	var alphamap_fragment = "#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, vUv ).g;\n#endif";
+			// rotate geometry around world y-axis
 
-	var alphamap_pars_fragment = "#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif";
+			_m1$3.makeRotationY( angle );
 
-	var alphatest_fragment = "#ifdef ALPHATEST\n\tif ( diffuseColor.a < ALPHATEST ) discard;\n#endif";
+			this.applyMatrix4( _m1$3 );
 
-	var aomap_fragment = "#ifdef USE_AOMAP\n\tfloat ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;\n\treflectedLight.indirectDiffuse *= ambientOcclusion;\n\t#if defined( USE_ENVMAP ) && defined( STANDARD )\n\t\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );\n\t#endif\n#endif";
+			return this;
 
-	var aomap_pars_fragment = "#ifdef USE_AOMAP\n\tuniform sampler2D aoMap;\n\tuniform float aoMapIntensity;\n#endif";
+		},
 
-	var begin_vertex = "vec3 transformed = vec3( position );";
+		rotateZ: function ( angle ) {
 
-	var beginnormal_vertex = "vec3 objectNormal = vec3( normal );\n#ifdef USE_TANGENT\n\tvec3 objectTangent = vec3( tangent.xyz );\n#endif";
+			// rotate geometry around world z-axis
 
-	var bsdfs = "vec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\treturn vec2( -1.04, 1.04 ) * a004 + r.zw;\n}\nfloat punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n#if defined ( PHYSICALLY_CORRECT_LIGHTS )\n\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\tif( cutoffDistance > 0.0 ) {\n\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t}\n\treturn distanceFalloff;\n#else\n\tif( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\treturn pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t}\n\treturn 1.0;\n#endif\n}\nvec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n}\nvec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );\n\treturn ( 1.0 - specularColor ) * fresnel + specularColor;\n}\nvec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );\n\tvec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;\n\treturn Fr * fresnel + F0;\n}\nfloat G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\tfloat gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\treturn 1.0 / ( gl * gv );\n}\nfloat G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\nvec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat alpha = pow2( roughness );\n\tvec3 halfDir = normalize( incidentLight.direction + viewDir );\n\tfloat dotNL = saturate( dot( normal, incidentLight.direction ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\tfloat D = D_GGX( alpha, dotNH );\n\treturn F * ( G * D );\n}\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE  = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS  = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 );\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\treturn vec3( result );\n}\nvec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\treturn specularColor * brdf.x + brdf.y;\n}\nvoid BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tvec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\tvec3 FssEss = F * brdf.x + brdf.y;\n\tfloat Ess = brdf.x + brdf.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619;\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\nfloat G_BlinnPhong_Implicit( ) {\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );\n\tfloat dotNH = saturate( dot( geometry.normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_BlinnPhong_Implicit( );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n}\nfloat GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\treturn ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );\n}\nfloat BlinnExponentToGGXRoughness( const in float blinnExponent ) {\n\treturn sqrt( 2.0 / ( blinnExponent + 2.0 ) );\n}\n#if defined( USE_SHEEN )\nfloat D_Charlie(float roughness, float NoH) {\n\tfloat invAlpha  = 1.0 / roughness;\n\tfloat cos2h = NoH * NoH;\n\tfloat sin2h = max(1.0 - cos2h, 0.0078125);\treturn (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);\n}\nfloat V_Neubelt(float NoV, float NoL) {\n\treturn saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));\n}\nvec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {\n\tvec3 N = geometry.normal;\n\tvec3 V = geometry.viewDir;\n\tvec3 H = normalize( V + L );\n\tfloat dotNH = saturate( dot( N, H ) );\n\treturn specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );\n}\n#endif";
+			_m1$3.makeRotationZ( angle );
 
-	var bumpmap_pars_fragment = "#ifdef USE_BUMPMAP\n\tuniform sampler2D bumpMap;\n\tuniform float bumpScale;\n\tvec2 dHdxy_fwd() {\n\t\tvec2 dSTdx = dFdx( vUv );\n\t\tvec2 dSTdy = dFdy( vUv );\n\t\tfloat Hll = bumpScale * texture2D( bumpMap, vUv ).x;\n\t\tfloat dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;\n\t\tfloat dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;\n\t\treturn vec2( dBx, dBy );\n\t}\n\tvec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {\n\t\tvec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );\n\t\tvec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );\n\t\tvec3 vN = surf_norm;\n\t\tvec3 R1 = cross( vSigmaY, vN );\n\t\tvec3 R2 = cross( vN, vSigmaX );\n\t\tfloat fDet = dot( vSigmaX, R1 );\n\t\tfDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\tvec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );\n\t\treturn normalize( abs( fDet ) * surf_norm - vGrad );\n\t}\n#endif";
+			this.applyMatrix4( _m1$3 );
 
-	var clipping_planes_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvec4 plane;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) {\n\t\tplane = clippingPlanes[ i ];\n\t\tif ( dot( vClipPosition, plane.xyz ) > plane.w ) discard;\n\t}\n\t#pragma unroll_loop_end\n\t#if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES\n\t\tbool clipped = true;\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) {\n\t\t\tplane = clippingPlanes[ i ];\n\t\t\tclipped = ( dot( vClipPosition, plane.xyz ) > plane.w ) && clipped;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t\tif ( clipped ) discard;\n\t#endif\n#endif";
+			return this;
 
-	var clipping_planes_pars_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n\tuniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];\n#endif";
+		},
 
-	var clipping_planes_pars_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n#endif";
+		translate: function ( x, y, z ) {
 
-	var clipping_planes_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvClipPosition = - mvPosition.xyz;\n#endif";
+			// translate geometry
 
-	var color_fragment = "#ifdef USE_COLOR\n\tdiffuseColor.rgb *= vColor;\n#endif";
+			_m1$3.makeTranslation( x, y, z );
 
-	var color_pars_fragment = "#ifdef USE_COLOR\n\tvarying vec3 vColor;\n#endif";
+			this.applyMatrix4( _m1$3 );
 
-	var color_pars_vertex = "#ifdef USE_COLOR\n\tvarying vec3 vColor;\n#endif";
+			return this;
 
-	var color_vertex = "#ifdef USE_COLOR\n\tvColor.xyz = color.xyz;\n#endif";
+		},
 
-	var common = "#define PI 3.14159265359\n#define PI2 6.28318530718\n#define PI_HALF 1.5707963267949\n#define RECIPROCAL_PI 0.31830988618\n#define RECIPROCAL_PI2 0.15915494\n#define LOG2 1.442695\n#define EPSILON 1e-6\n#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\n#define whiteComplement(a) ( 1.0 - saturate( a ) )\nfloat pow2( const in float x ) { return x*x; }\nfloat pow3( const in float x ) { return x*x*x; }\nfloat pow4( const in float x ) { float x2 = x*x; return x2*x2; }\nfloat average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }\nhighp float rand( const in vec2 uv ) {\n\tconst highp float a = 12.9898, b = 78.233, c = 43758.5453;\n\thighp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n\treturn fract(sin(sn) * c);\n}\n#ifdef HIGH_PRECISION\n\tfloat precisionSafeLength( vec3 v ) { return length( v ); }\n#else\n\tfloat max3( vec3 v ) { return max( max( v.x, v.y ), v.z ); }\n\tfloat precisionSafeLength( vec3 v ) {\n\t\tfloat maxComponent = max3( abs( v ) );\n\t\treturn length( v / maxComponent ) * maxComponent;\n\t}\n#endif\nstruct IncidentLight {\n\tvec3 color;\n\tvec3 direction;\n\tbool visible;\n};\nstruct ReflectedLight {\n\tvec3 directDiffuse;\n\tvec3 directSpecular;\n\tvec3 indirectDiffuse;\n\tvec3 indirectSpecular;\n};\nstruct GeometricContext {\n\tvec3 position;\n\tvec3 normal;\n\tvec3 viewDir;\n#ifdef CLEARCOAT\n\tvec3 clearcoatNormal;\n#endif\n};\nvec3 transformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );\n}\nvec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );\n}\nvec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\tfloat distance = dot( planeNormal, point - pointOnPlane );\n\treturn - distance * planeNormal + point;\n}\nfloat sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn sign( dot( point - pointOnPlane, planeNormal ) );\n}\nvec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;\n}\nmat3 transposeMat3( const in mat3 m ) {\n\tmat3 tmp;\n\ttmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n\ttmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n\ttmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n\treturn tmp;\n}\nfloat linearToRelativeLuminance( const in vec3 color ) {\n\tvec3 weights = vec3( 0.2126, 0.7152, 0.0722 );\n\treturn dot( weights, color.rgb );\n}\nbool isPerspectiveMatrix( mat4 m ) {\n  return m[ 2 ][ 3 ] == - 1.0;\n}";
+		scale: function ( x, y, z ) {
 
-	var cube_uv_reflection_fragment = "#ifdef ENVMAP_TYPE_CUBE_UV\n#define cubeUV_maxMipLevel 8.0\n#define cubeUV_minMipLevel 4.0\n#define cubeUV_maxTileSize 256.0\n#define cubeUV_minTileSize 16.0\nfloat getFace(vec3 direction) {\n    vec3 absDirection = abs(direction);\n    float face = -1.0;\n    if (absDirection.x > absDirection.z) {\n      if (absDirection.x > absDirection.y)\n        face = direction.x > 0.0 ? 0.0 : 3.0;\n      else\n        face = direction.y > 0.0 ? 1.0 : 4.0;\n    } else {\n      if (absDirection.z > absDirection.y)\n        face = direction.z > 0.0 ? 2.0 : 5.0;\n      else\n        face = direction.y > 0.0 ? 1.0 : 4.0;\n    }\n    return face;\n}\nvec2 getUV(vec3 direction, float face) {\n    vec2 uv;\n    if (face == 0.0) {\n      uv = vec2(-direction.z, direction.y) / abs(direction.x);\n    } else if (face == 1.0) {\n      uv = vec2(direction.x, -direction.z) / abs(direction.y);\n    } else if (face == 2.0) {\n      uv = direction.xy / abs(direction.z);\n    } else if (face == 3.0) {\n      uv = vec2(direction.z, direction.y) / abs(direction.x);\n    } else if (face == 4.0) {\n      uv = direction.xz / abs(direction.y);\n    } else {\n      uv = vec2(-direction.x, direction.y) / abs(direction.z);\n    }\n    return 0.5 * (uv + 1.0);\n}\nvec3 bilinearCubeUV(sampler2D envMap, vec3 direction, float mipInt) {\n  float face = getFace(direction);\n  float filterInt = max(cubeUV_minMipLevel - mipInt, 0.0);\n  mipInt = max(mipInt, cubeUV_minMipLevel);\n  float faceSize = exp2(mipInt);\n  float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize);\n  vec2 uv = getUV(direction, face) * (faceSize - 1.0);\n  vec2 f = fract(uv);\n  uv += 0.5 - f;\n  if (face > 2.0) {\n    uv.y += faceSize;\n    face -= 3.0;\n  }\n  uv.x += face * faceSize;\n  if(mipInt < cubeUV_maxMipLevel){\n    uv.y += 2.0 * cubeUV_maxTileSize;\n  }\n  uv.y += filterInt * 2.0 * cubeUV_minTileSize;\n  uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize);\n  uv *= texelSize;\n  vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.x += texelSize;\n  vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.y += texelSize;\n  vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.x -= texelSize;\n  vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  vec3 tm = mix(tl, tr, f.x);\n  vec3 bm = mix(bl, br, f.x);\n  return mix(tm, bm, f.y);\n}\n#define r0 1.0\n#define v0 0.339\n#define m0 -2.0\n#define r1 0.8\n#define v1 0.276\n#define m1 -1.0\n#define r4 0.4\n#define v4 0.046\n#define m4 2.0\n#define r5 0.305\n#define v5 0.016\n#define m5 3.0\n#define r6 0.21\n#define v6 0.0038\n#define m6 4.0\nfloat roughnessToMip(float roughness) {\n  float mip = 0.0;\n  if (roughness >= r1) {\n    mip = (r0 - roughness) * (m1 - m0) / (r0 - r1) + m0;\n  } else if (roughness >= r4) {\n    mip = (r1 - roughness) * (m4 - m1) / (r1 - r4) + m1;\n  } else if (roughness >= r5) {\n    mip = (r4 - roughness) * (m5 - m4) / (r4 - r5) + m4;\n  } else if (roughness >= r6) {\n    mip = (r5 - roughness) * (m6 - m5) / (r5 - r6) + m5;\n  } else {\n    mip = -2.0 * log2(1.16 * roughness);  }\n  return mip;\n}\nvec4 textureCubeUV(sampler2D envMap, vec3 sampleDir, float roughness) {\n  float mip = clamp(roughnessToMip(roughness), m0, cubeUV_maxMipLevel);\n  float mipF = fract(mip);\n  float mipInt = floor(mip);\n  vec3 color0 = bilinearCubeUV(envMap, sampleDir, mipInt);\n  if (mipF == 0.0) {\n    return vec4(color0, 1.0);\n  } else {\n    vec3 color1 = bilinearCubeUV(envMap, sampleDir, mipInt + 1.0);\n    return vec4(mix(color0, color1, mipF), 1.0);\n  }\n}\n#endif";
+			// scale geometry
 
-	var defaultnormal_vertex = "vec3 transformedNormal = objectNormal;\n#ifdef USE_INSTANCING\n\tmat3 m = mat3( instanceMatrix );\n\ttransformedNormal /= vec3( dot( m[ 0 ], m[ 0 ] ), dot( m[ 1 ], m[ 1 ] ), dot( m[ 2 ], m[ 2 ] ) );\n\ttransformedNormal = m * transformedNormal;\n#endif\ntransformedNormal = normalMatrix * transformedNormal;\n#ifdef FLIP_SIDED\n\ttransformedNormal = - transformedNormal;\n#endif\n#ifdef USE_TANGENT\n\tvec3 transformedTangent = ( modelViewMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#ifdef FLIP_SIDED\n\t\ttransformedTangent = - transformedTangent;\n\t#endif\n#endif";
+			_m1$3.makeScale( x, y, z );
 
-	var displacementmap_pars_vertex = "#ifdef USE_DISPLACEMENTMAP\n\tuniform sampler2D displacementMap;\n\tuniform float displacementScale;\n\tuniform float displacementBias;\n#endif";
+			this.applyMatrix4( _m1$3 );
 
-	var displacementmap_vertex = "#ifdef USE_DISPLACEMENTMAP\n\ttransformed += normalize( objectNormal ) * ( texture2D( displacementMap, vUv ).x * displacementScale + displacementBias );\n#endif";
+			return this;
 
-	var emissivemap_fragment = "#ifdef USE_EMISSIVEMAP\n\tvec4 emissiveColor = texture2D( emissiveMap, vUv );\n\temissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;\n\ttotalEmissiveRadiance *= emissiveColor.rgb;\n#endif";
+		},
 
-	var emissivemap_pars_fragment = "#ifdef USE_EMISSIVEMAP\n\tuniform sampler2D emissiveMap;\n#endif";
+		lookAt: function ( vector ) {
 
-	var encodings_fragment = "gl_FragColor = linearToOutputTexel( gl_FragColor );";
+			_obj$1.lookAt( vector );
 
-	var encodings_pars_fragment = "\nvec4 LinearToLinear( in vec4 value ) {\n\treturn value;\n}\nvec4 GammaToLinear( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( gammaFactor ) ), value.a );\n}\nvec4 LinearToGamma( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( 1.0 / gammaFactor ) ), value.a );\n}\nvec4 sRGBToLinear( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.a );\n}\nvec4 LinearTosRGB( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );\n}\nvec4 RGBEToLinear( in vec4 value ) {\n\treturn vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );\n}\nvec4 LinearToRGBE( in vec4 value ) {\n\tfloat maxComponent = max( max( value.r, value.g ), value.b );\n\tfloat fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );\n\treturn vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );\n}\nvec4 RGBMToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * value.a * maxRange, 1.0 );\n}\nvec4 LinearToRGBM( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat M = clamp( maxRGB / maxRange, 0.0, 1.0 );\n\tM = ceil( M * 255.0 ) / 255.0;\n\treturn vec4( value.rgb / ( M * maxRange ), M );\n}\nvec4 RGBDToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );\n}\nvec4 LinearToRGBD( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat D = max( maxRange / maxRGB, 1.0 );\n\tD = clamp( floor( D ) / 255.0, 0.0, 1.0 );\n\treturn vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );\n}\nconst mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );\nvec4 LinearToLogLuv( in vec4 value )  {\n\tvec3 Xp_Y_XYZp = cLogLuvM * value.rgb;\n\tXp_Y_XYZp = max( Xp_Y_XYZp, vec3( 1e-6, 1e-6, 1e-6 ) );\n\tvec4 vResult;\n\tvResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;\n\tfloat Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;\n\tvResult.w = fract( Le );\n\tvResult.z = ( Le - ( floor( vResult.w * 255.0 ) ) / 255.0 ) / 255.0;\n\treturn vResult;\n}\nconst mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );\nvec4 LogLuvToLinear( in vec4 value ) {\n\tfloat Le = value.z * 255.0 + value.w;\n\tvec3 Xp_Y_XYZp;\n\tXp_Y_XYZp.y = exp2( ( Le - 127.0 ) / 2.0 );\n\tXp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;\n\tXp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;\n\tvec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb;\n\treturn vec4( max( vRGB, 0.0 ), 1.0 );\n}";
+			_obj$1.updateMatrix();
 
-	var envmap_fragment = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvec3 cameraToFrag;\n\t\t\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t}  else {\n\t\t\tcameraToFrag = normalize( vWorldPosition - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvec3 reflectVec = reflect( cameraToFrag, worldNormal );\n\t\t#else\n\t\t\tvec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );\n\t\t#endif\n\t#else\n\t\tvec3 reflectVec = vReflect;\n\t#endif\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tvec4 envColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );\n\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\tvec4 envColor = textureCubeUV( envMap, reflectVec, 0.0 );\n\t#elif defined( ENVMAP_TYPE_EQUIREC )\n\t\tvec2 sampleUV;\n\t\treflectVec = normalize( reflectVec );\n\t\tsampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\t\tsampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;\n\t\tvec4 envColor = texture2D( envMap, sampleUV );\n\t#elif defined( ENVMAP_TYPE_SPHERE )\n\t\treflectVec = normalize( reflectVec );\n\t\tvec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );\n\t\tvec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );\n\t#else\n\t\tvec4 envColor = vec4( 0.0 );\n\t#endif\n\t#ifndef ENVMAP_TYPE_CUBE_UV\n\t\tenvColor = envMapTexelToLinear( envColor );\n\t#endif\n\t#ifdef ENVMAP_BLENDING_MULTIPLY\n\t\toutgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_MIX )\n\t\toutgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_ADD )\n\t\toutgoingLight += envColor.xyz * specularStrength * reflectivity;\n\t#endif\n#endif";
+			this.applyMatrix4( _obj$1.matrix );
 
-	var envmap_common_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float envMapIntensity;\n\tuniform float flipEnvMap;\n\tuniform int maxMipLevel;\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tuniform samplerCube envMap;\n\t#else\n\t\tuniform sampler2D envMap;\n\t#endif\n\t\n#endif";
+			return this;
 
-	var envmap_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float reflectivity;\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\tvarying vec3 vWorldPosition;\n\t\tuniform float refractionRatio;\n\t#else\n\t\tvarying vec3 vReflect;\n\t#endif\n#endif";
+		},
 
-	var envmap_pars_vertex = "#ifdef USE_ENVMAP\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) ||defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\t\n\t\tvarying vec3 vWorldPosition;\n\t#else\n\t\tvarying vec3 vReflect;\n\t\tuniform float refractionRatio;\n\t#endif\n#endif";
+		fromBufferGeometry: function ( geometry ) {
 
-	var envmap_vertex = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvWorldPosition = worldPosition.xyz;\n\t#else\n\t\tvec3 cameraToVertex;\n\t\tif ( isOrthographic ) { \n\t\t\tcameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToVertex = normalize( worldPosition.xyz - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvReflect = reflect( cameraToVertex, worldNormal );\n\t\t#else\n\t\t\tvReflect = refract( cameraToVertex, worldNormal, refractionRatio );\n\t\t#endif\n\t#endif\n#endif";
+			var scope = this;
 
-	var fog_vertex = "#ifdef USE_FOG\n\tfogDepth = -mvPosition.z;\n#endif";
+			var indices = geometry.index !== null ? geometry.index.array : undefined;
+			var attributes = geometry.attributes;
 
-	var fog_pars_vertex = "#ifdef USE_FOG\n\tvarying float fogDepth;\n#endif";
+			if ( attributes.position === undefined ) {
 
-	var fog_fragment = "#ifdef USE_FOG\n\t#ifdef FOG_EXP2\n\t\tfloat fogFactor = 1.0 - exp( - fogDensity * fogDensity * fogDepth * fogDepth );\n\t#else\n\t\tfloat fogFactor = smoothstep( fogNear, fogFar, fogDepth );\n\t#endif\n\tgl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );\n#endif";
+				console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' );
+				return this;
 
-	var fog_pars_fragment = "#ifdef USE_FOG\n\tuniform vec3 fogColor;\n\tvarying float fogDepth;\n\t#ifdef FOG_EXP2\n\t\tuniform float fogDensity;\n\t#else\n\t\tuniform float fogNear;\n\t\tuniform float fogFar;\n\t#endif\n#endif";
+			}
 
-	var gradientmap_pars_fragment = "#ifdef USE_GRADIENTMAP\n\tuniform sampler2D gradientMap;\n#endif\nvec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {\n\tfloat dotNL = dot( normal, lightDirection );\n\tvec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );\n\t#ifdef USE_GRADIENTMAP\n\t\treturn texture2D( gradientMap, coord ).rgb;\n\t#else\n\t\treturn ( coord.x < 0.7 ) ? vec3( 0.7 ) : vec3( 1.0 );\n\t#endif\n}";
+			var positions = attributes.position.array;
+			var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
+			var colors = attributes.color !== undefined ? attributes.color.array : undefined;
+			var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
+			var uvs2 = attributes.uv2 !== undefined ? attributes.uv2.array : undefined;
 
-	var lightmap_fragment = "#ifdef USE_LIGHTMAP\n\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\treflectedLight.indirectDiffuse += PI * lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n#endif";
+			if ( uvs2 !== undefined ) { this.faceVertexUvs[ 1 ] = []; }
 
-	var lightmap_pars_fragment = "#ifdef USE_LIGHTMAP\n\tuniform sampler2D lightMap;\n\tuniform float lightMapIntensity;\n#endif";
+			for ( var i = 0; i < positions.length; i += 3 ) {
 
-	var lights_lambert_vertex = "vec3 diffuse = vec3( 1.0 );\nGeometricContext geometry;\ngeometry.position = mvPosition.xyz;\ngeometry.normal = normalize( transformedNormal );\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );\nGeometricContext backGeometry;\nbackGeometry.position = geometry.position;\nbackGeometry.normal = -geometry.normal;\nbackGeometry.viewDir = geometry.viewDir;\nvLightFront = vec3( 0.0 );\nvIndirectFront = vec3( 0.0 );\n#ifdef DOUBLE_SIDED\n\tvLightBack = vec3( 0.0 );\n\tvIndirectBack = vec3( 0.0 );\n#endif\nIncidentLight directLight;\nfloat dotNL;\nvec3 directLightColor_Diffuse;\nvIndirectFront += getAmbientLightIrradiance( ambientLightColor );\nvIndirectFront += getLightProbeIrradiance( lightProbe, geometry );\n#ifdef DOUBLE_SIDED\n\tvIndirectBack += getAmbientLightIrradiance( ambientLightColor );\n\tvIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry );\n#endif\n#if NUM_POINT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tgetPointDirectLightIrradiance( pointLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tgetSpotDirectLightIrradiance( spotLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_DIR_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tgetDirectionalDirectLightIrradiance( directionalLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\tvIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry );\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif";
+				scope.vertices.push( new Vector3().fromArray( positions, i ) );
 
-	var lights_pars_begin = "uniform bool receiveShadow;\nuniform vec3 ambientLightColor;\nuniform vec3 lightProbe[ 9 ];\nvec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {\n\tfloat x = normal.x, y = normal.y, z = normal.z;\n\tvec3 result = shCoefficients[ 0 ] * 0.886227;\n\tresult += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;\n\tresult += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;\n\tresult += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;\n\tresult += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;\n\tresult += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;\n\tresult += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );\n\tresult += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;\n\tresult += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );\n\treturn result;\n}\nvec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in GeometricContext geometry ) {\n\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\tvec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );\n\treturn irradiance;\n}\nvec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {\n\tvec3 irradiance = ambientLightColor;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treturn irradiance;\n}\n#if NUM_DIR_LIGHTS > 0\n\tstruct DirectionalLight {\n\t\tvec3 direction;\n\t\tvec3 color;\n\t};\n\tuniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tdirectLight.color = directionalLight.color;\n\t\tdirectLight.direction = directionalLight.direction;\n\t\tdirectLight.visible = true;\n\t}\n#endif\n#if NUM_POINT_LIGHTS > 0\n\tstruct PointLight {\n\t\tvec3 position;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t};\n\tuniform PointLight pointLights[ NUM_POINT_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tvec3 lVector = pointLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tdirectLight.color = pointLight.color;\n\t\tdirectLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );\n\t\tdirectLight.visible = ( directLight.color != vec3( 0.0 ) );\n\t}\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\tstruct SpotLight {\n\t\tvec3 position;\n\t\tvec3 direction;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t\tfloat coneCos;\n\t\tfloat penumbraCos;\n\t};\n\tuniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight  ) {\n\t\tvec3 lVector = spotLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tfloat angleCos = dot( directLight.direction, spotLight.direction );\n\t\tif ( angleCos > spotLight.coneCos ) {\n\t\t\tfloat spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n\t\t\tdirectLight.color = spotLight.color;\n\t\t\tdirectLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );\n\t\t\tdirectLight.visible = true;\n\t\t} else {\n\t\t\tdirectLight.color = vec3( 0.0 );\n\t\t\tdirectLight.visible = false;\n\t\t}\n\t}\n#endif\n#if NUM_RECT_AREA_LIGHTS > 0\n\tstruct RectAreaLight {\n\t\tvec3 color;\n\t\tvec3 position;\n\t\tvec3 halfWidth;\n\t\tvec3 halfHeight;\n\t};\n\tuniform sampler2D ltc_1;\tuniform sampler2D ltc_2;\n\tuniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ];\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\tstruct HemisphereLight {\n\t\tvec3 direction;\n\t\tvec3 skyColor;\n\t\tvec3 groundColor;\n\t};\n\tuniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];\n\tvec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {\n\t\tfloat dotNL = dot( geometry.normal, hemiLight.direction );\n\t\tfloat hemiDiffuseWeight = 0.5 * dotNL + 0.5;\n\t\tvec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tirradiance *= PI;\n\t\t#endif\n\t\treturn irradiance;\n\t}\n#endif";
+				if ( colors !== undefined ) {
 
-	var envmap_physical_pars_fragment = "#if defined( USE_ENVMAP )\n\t#ifdef ENVMAP_MODE_REFRACTION\n\t\tuniform float refractionRatio;\n\t#endif\n\tvec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {\n\t\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, worldNormal, 1.0 );\n\t\t#else\n\t\t\tvec4 envMapColor = vec4( 0.0 );\n\t\t#endif\n\t\treturn PI * envMapColor.rgb * envMapIntensity;\n\t}\n\tfloat getSpecularMIPLevel( const in float roughness, const in int maxMIPLevel ) {\n\t\tfloat maxMIPLevelScalar = float( maxMIPLevel );\n\t\tfloat sigma = PI * roughness * roughness / ( 1.0 + roughness );\n\t\tfloat desiredMIPLevel = maxMIPLevelScalar + log2( sigma );\n\t\treturn clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );\n\t}\n\tvec3 getLightProbeIndirectRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in int maxMIPLevel ) {\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t  vec3 reflectVec = reflect( -viewDir, normal );\n\t\t  reflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );\n\t\t#else\n\t\t  vec3 reflectVec = refract( -viewDir, normal, refractionRatio );\n\t\t#endif\n\t\treflectVec = inverseTransformDirection( reflectVec, viewMatrix );\n\t\tfloat specularMIPLevel = getSpecularMIPLevel( roughness, maxMIPLevel );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );\n\t\t#elif defined( ENVMAP_TYPE_EQUIREC )\n\t\t\tvec2 sampleUV;\n\t\t\tsampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\t\t\tsampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_SPHERE )\n\t\t\tvec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#endif\n\t\treturn envMapColor.rgb * envMapIntensity;\n\t}\n#endif";
+					scope.colors.push( new Color().fromArray( colors, i ) );
 
-	var lights_toon_fragment = "ToonMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;";
+				}
 
-	var lights_toon_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct ToonMaterial {\n\tvec3\tdiffuseColor;\n\tvec3\tspecularColor;\n\tfloat\tspecularShininess;\n\tfloat\tspecularStrength;\n};\nvoid RE_Direct_Toon( const in IncidentLight directLight, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\tvec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_Toon( const in vec3 irradiance, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_Toon\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Toon\n#define Material_LightProbeLOD( material )\t(0)";
+			}
 
-	var lights_phong_fragment = "BlinnPhongMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;";
+			function addFace( a, b, c, materialIndex ) {
 
-	var lights_phong_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct BlinnPhongMaterial {\n\tvec3\tdiffuseColor;\n\tvec3\tspecularColor;\n\tfloat\tspecularShininess;\n\tfloat\tspecularStrength;\n};\nvoid RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_BlinnPhong\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_BlinnPhong\n#define Material_LightProbeLOD( material )\t(0)";
+				var vertexColors = ( colors === undefined ) ? [] : [
+					scope.colors[ a ].clone(),
+					scope.colors[ b ].clone(),
+					scope.colors[ c ].clone() ];
 
-	var lights_physical_fragment = "PhysicalMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );\nvec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );\nfloat geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );\nmaterial.specularRoughness = max( roughnessFactor, 0.0525 );material.specularRoughness += geometryRoughness;\nmaterial.specularRoughness = min( material.specularRoughness, 1.0 );\n#ifdef REFLECTIVITY\n\tmaterial.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );\n#else\n\tmaterial.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );\n#endif\n#ifdef CLEARCOAT\n\tmaterial.clearcoat = clearcoat;\n\tmaterial.clearcoatRoughness = clearcoatRoughness;\n\t#ifdef USE_CLEARCOATMAP\n\t\tmaterial.clearcoat *= texture2D( clearcoatMap, vUv ).x;\n\t#endif\n\t#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\t\tmaterial.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vUv ).y;\n\t#endif\n\tmaterial.clearcoat = saturate( material.clearcoat );\tmaterial.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 );\n\tmaterial.clearcoatRoughness += geometryRoughness;\n\tmaterial.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );\n#endif\n#ifdef USE_SHEEN\n\tmaterial.sheenColor = sheen;\n#endif";
+				var vertexNormals = ( normals === undefined ) ? [] : [
+					new Vector3().fromArray( normals, a * 3 ),
+					new Vector3().fromArray( normals, b * 3 ),
+					new Vector3().fromArray( normals, c * 3 )
+				];
 
-	var lights_physical_pars_fragment = "struct PhysicalMaterial {\n\tvec3\tdiffuseColor;\n\tfloat\tspecularRoughness;\n\tvec3\tspecularColor;\n#ifdef CLEARCOAT\n\tfloat clearcoat;\n\tfloat clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tvec3 sheenColor;\n#endif\n};\n#define MAXIMUM_SPECULAR_COEFFICIENT 0.16\n#define DEFAULT_SPECULAR_COEFFICIENT 0.04\nfloat clearcoatDHRApprox( const in float roughness, const in float dotNL ) {\n\treturn DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );\n}\n#if NUM_RECT_AREA_LIGHTS > 0\n\tvoid RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\t\tvec3 normal = geometry.normal;\n\t\tvec3 viewDir = geometry.viewDir;\n\t\tvec3 position = geometry.position;\n\t\tvec3 lightPos = rectAreaLight.position;\n\t\tvec3 halfWidth = rectAreaLight.halfWidth;\n\t\tvec3 halfHeight = rectAreaLight.halfHeight;\n\t\tvec3 lightColor = rectAreaLight.color;\n\t\tfloat roughness = material.specularRoughness;\n\t\tvec3 rectCoords[ 4 ];\n\t\trectCoords[ 0 ] = lightPos + halfWidth - halfHeight;\t\trectCoords[ 1 ] = lightPos - halfWidth - halfHeight;\n\t\trectCoords[ 2 ] = lightPos - halfWidth + halfHeight;\n\t\trectCoords[ 3 ] = lightPos + halfWidth + halfHeight;\n\t\tvec2 uv = LTC_Uv( normal, viewDir, roughness );\n\t\tvec4 t1 = texture2D( ltc_1, uv );\n\t\tvec4 t2 = texture2D( ltc_2, uv );\n\t\tmat3 mInv = mat3(\n\t\t\tvec3( t1.x, 0, t1.y ),\n\t\t\tvec3(    0, 1,    0 ),\n\t\t\tvec3( t1.z, 0, t1.w )\n\t\t);\n\t\tvec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y );\n\t\treflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords );\n\t\treflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );\n\t}\n#endif\nvoid RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNL = saturate( dot( geometry.clearcoatNormal, directLight.direction ) );\n\t\tvec3 ccIrradiance = ccDotNL * directLight.color;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tccIrradiance *= PI;\n\t\t#endif\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t\treflectedLight.directSpecular += ccIrradiance * material.clearcoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\t#ifdef USE_SHEEN\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_Sheen(\n\t\t\tmaterial.specularRoughness,\n\t\t\tdirectLight.direction,\n\t\t\tgeometry,\n\t\t\tmaterial.sheenColor\n\t\t);\n\t#else\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness);\n\t#endif\n\treflectedLight.directDiffuse += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNV = saturate( dot( geometry.clearcoatNormal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t\tfloat ccDotNL = ccDotNV;\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\tfloat clearcoatInv = 1.0 - clearcoatDHR;\n\tvec3 singleScattering = vec3( 0.0 );\n\tvec3 multiScattering = vec3( 0.0 );\n\tvec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI;\n\tBRDF_Specular_Multiscattering_Environment( geometry, material.specularColor, material.specularRoughness, singleScattering, multiScattering );\n\tvec3 diffuse = material.diffuseColor * ( 1.0 - ( singleScattering + multiScattering ) );\n\treflectedLight.indirectSpecular += clearcoatInv * radiance * singleScattering;\n\treflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance;\n\treflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance;\n}\n#define RE_Direct\t\t\t\tRE_Direct_Physical\n#define RE_Direct_RectArea\t\tRE_Direct_RectArea_Physical\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Physical\n#define RE_IndirectSpecular\t\tRE_IndirectSpecular_Physical\nfloat computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {\n\treturn saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );\n}";
+				var face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
 
-	var lights_fragment_begin = "\nGeometricContext geometry;\ngeometry.position = - vViewPosition;\ngeometry.normal = normal;\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\n#ifdef CLEARCOAT\n\tgeometry.clearcoatNormal = clearcoatNormal;\n#endif\nIncidentLight directLight;\n#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )\n\tPointLight pointLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tpointLight = pointLights[ i ];\n\t\tgetPointDirectLightIrradiance( pointLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )\n\t\tpointLightShadow = pointLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )\n\tSpotLight spotLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tspotLight = spotLights[ i ];\n\t\tgetSpotDirectLightIrradiance( spotLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\tspotLightShadow = spotLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )\n\tDirectionalLight directionalLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tdirectionalLight = directionalLights[ i ];\n\t\tgetDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )\n\t\tdirectionalLightShadow = directionalLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )\n\tRectAreaLight rectAreaLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {\n\t\trectAreaLight = rectAreaLights[ i ];\n\t\tRE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if defined( RE_IndirectDiffuse )\n\tvec3 iblIrradiance = vec3( 0.0 );\n\tvec3 irradiance = getAmbientLightIrradiance( ambientLightColor );\n\tirradiance += getLightProbeIrradiance( lightProbe, geometry );\n\t#if ( NUM_HEMI_LIGHTS > 0 )\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\t\tirradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n#endif\n#if defined( RE_IndirectSpecular )\n\tvec3 radiance = vec3( 0.0 );\n\tvec3 clearcoatRadiance = vec3( 0.0 );\n#endif";
+				scope.faces.push( face );
 
-	var lights_fragment_maps = "#if defined( RE_IndirectDiffuse )\n\t#ifdef USE_LIGHTMAP\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\tvec3 lightMapIrradiance = lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tlightMapIrradiance *= PI;\n\t\t#endif\n\t\tirradiance += lightMapIrradiance;\n\t#endif\n\t#if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )\n\t\tiblIrradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel );\n\t#endif\n#endif\n#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )\n\tradiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.normal, material.specularRoughness, maxMipLevel );\n\t#ifdef CLEARCOAT\n\t\tclearcoatRadiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness, maxMipLevel );\n\t#endif\n#endif";
+				if ( uvs !== undefined ) {
 
-	var lights_fragment_end = "#if defined( RE_IndirectDiffuse )\n\tRE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );\n#endif\n#if defined( RE_IndirectSpecular )\n\tRE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight );\n#endif";
+					scope.faceVertexUvs[ 0 ].push( [
+						new Vector2().fromArray( uvs, a * 2 ),
+						new Vector2().fromArray( uvs, b * 2 ),
+						new Vector2().fromArray( uvs, c * 2 )
+					] );
 
-	var logdepthbuf_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tgl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;\n#endif";
+				}
 
-	var logdepthbuf_pars_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tuniform float logDepthBufFC;\n\tvarying float vFragDepth;\n\tvarying float vIsPerspective;\n#endif";
+				if ( uvs2 !== undefined ) {
 
-	var logdepthbuf_pars_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvarying float vFragDepth;\n\t\tvarying float vIsPerspective;\n\t#else\n\t\tuniform float logDepthBufFC;\n\t#endif\n#endif";
+					scope.faceVertexUvs[ 1 ].push( [
+						new Vector2().fromArray( uvs2, a * 2 ),
+						new Vector2().fromArray( uvs2, b * 2 ),
+						new Vector2().fromArray( uvs2, c * 2 )
+					] );
 
-	var logdepthbuf_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvFragDepth = 1.0 + gl_Position.w;\n\t\tvIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) );\n\t#else\n\t\tif ( isPerspectiveMatrix( projectionMatrix ) ) {\n\t\t\tgl_Position.z = log2( max( EPSILON, gl_Position.w + 1.0 ) ) * logDepthBufFC - 1.0;\n\t\t\tgl_Position.z *= gl_Position.w;\n\t\t}\n\t#endif\n#endif";
+				}
 
-	var map_fragment = "#ifdef USE_MAP\n\tvec4 texelColor = texture2D( map, vUv );\n\ttexelColor = mapTexelToLinear( texelColor );\n\tdiffuseColor *= texelColor;\n#endif";
+			}
 
-	var map_pars_fragment = "#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif";
+			var groups = geometry.groups;
 
-	var map_particle_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tvec2 uv = ( uvTransform * vec3( gl_PointCoord.x, 1.0 - gl_PointCoord.y, 1 ) ).xy;\n#endif\n#ifdef USE_MAP\n\tvec4 mapTexel = texture2D( map, uv );\n\tdiffuseColor *= mapTexelToLinear( mapTexel );\n#endif\n#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, uv ).g;\n#endif";
+			if ( groups.length > 0 ) {
 
-	var map_particle_pars_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tuniform mat3 uvTransform;\n#endif\n#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif\n#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif";
+				for ( var i = 0; i < groups.length; i ++ ) {
 
-	var metalnessmap_fragment = "float metalnessFactor = metalness;\n#ifdef USE_METALNESSMAP\n\tvec4 texelMetalness = texture2D( metalnessMap, vUv );\n\tmetalnessFactor *= texelMetalness.b;\n#endif";
+					var group = groups[ i ];
 
-	var metalnessmap_pars_fragment = "#ifdef USE_METALNESSMAP\n\tuniform sampler2D metalnessMap;\n#endif";
+					var start = group.start;
+					var count = group.count;
 
-	var morphnormal_vertex = "#ifdef USE_MORPHNORMALS\n\tobjectNormal *= morphTargetBaseInfluence;\n\tobjectNormal += morphNormal0 * morphTargetInfluences[ 0 ];\n\tobjectNormal += morphNormal1 * morphTargetInfluences[ 1 ];\n\tobjectNormal += morphNormal2 * morphTargetInfluences[ 2 ];\n\tobjectNormal += morphNormal3 * morphTargetInfluences[ 3 ];\n#endif";
+					for ( var j = start, jl = start + count; j < jl; j += 3 ) {
 
-	var morphtarget_pars_vertex = "#ifdef USE_MORPHTARGETS\n\tuniform float morphTargetBaseInfluence;\n\t#ifndef USE_MORPHNORMALS\n\tuniform float morphTargetInfluences[ 8 ];\n\t#else\n\tuniform float morphTargetInfluences[ 4 ];\n\t#endif\n#endif";
+						if ( indices !== undefined ) {
 
-	var morphtarget_vertex = "#ifdef USE_MORPHTARGETS\n\ttransformed *= morphTargetBaseInfluence;\n\ttransformed += morphTarget0 * morphTargetInfluences[ 0 ];\n\ttransformed += morphTarget1 * morphTargetInfluences[ 1 ];\n\ttransformed += morphTarget2 * morphTargetInfluences[ 2 ];\n\ttransformed += morphTarget3 * morphTargetInfluences[ 3 ];\n\t#ifndef USE_MORPHNORMALS\n\ttransformed += morphTarget4 * morphTargetInfluences[ 4 ];\n\ttransformed += morphTarget5 * morphTargetInfluences[ 5 ];\n\ttransformed += morphTarget6 * morphTargetInfluences[ 6 ];\n\ttransformed += morphTarget7 * morphTargetInfluences[ 7 ];\n\t#endif\n#endif";
+							addFace( indices[ j ], indices[ j + 1 ], indices[ j + 2 ], group.materialIndex );
 
-	var normal_fragment_begin = "#ifdef FLAT_SHADED\n\tvec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );\n\tvec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );\n\tvec3 normal = normalize( cross( fdx, fdy ) );\n#else\n\tvec3 normal = normalize( vNormal );\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\t#ifdef USE_TANGENT\n\t\tvec3 tangent = normalize( vTangent );\n\t\tvec3 bitangent = normalize( vBitangent );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\ttangent = tangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t\tbitangent = bitangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t#endif\n\t\t#if defined( TANGENTSPACE_NORMALMAP ) || defined( USE_CLEARCOAT_NORMALMAP )\n\t\t\tmat3 vTBN = mat3( tangent, bitangent, normal );\n\t\t#endif\n\t#endif\n#endif\nvec3 geometryNormal = normal;";
+						} else {
 
-	var normal_fragment_maps = "#ifdef OBJECTSPACE_NORMALMAP\n\tnormal = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\t#ifdef FLIP_SIDED\n\t\tnormal = - normal;\n\t#endif\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\tnormal = normalize( normalMatrix * normal );\n#elif defined( TANGENTSPACE_NORMALMAP )\n\tvec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\tmapN.xy *= normalScale;\n\t#ifdef USE_TANGENT\n\t\tnormal = normalize( vTBN * mapN );\n\t#else\n\t\tnormal = perturbNormal2Arb( -vViewPosition, normal, mapN );\n\t#endif\n#elif defined( USE_BUMPMAP )\n\tnormal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );\n#endif";
+							addFace( j, j + 1, j + 2, group.materialIndex );
 
-	var normalmap_pars_fragment = "#ifdef USE_NORMALMAP\n\tuniform sampler2D normalMap;\n\tuniform vec2 normalScale;\n#endif\n#ifdef OBJECTSPACE_NORMALMAP\n\tuniform mat3 normalMatrix;\n#endif\n#if ! defined ( USE_TANGENT ) && ( defined ( TANGENTSPACE_NORMALMAP ) || defined ( USE_CLEARCOAT_NORMALMAP ) )\n\tvec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN ) {\n\t\tvec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );\n\t\tvec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );\n\t\tvec2 st0 = dFdx( vUv.st );\n\t\tvec2 st1 = dFdy( vUv.st );\n\t\tfloat scale = sign( st1.t * st0.s - st0.t * st1.s );\n\t\tvec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );\n\t\tvec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );\n\t\tvec3 N = normalize( surf_norm );\n\t\tmat3 tsn = mat3( S, T, N );\n\t\tmapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\treturn normalize( tsn * mapN );\n\t}\n#endif";
+						}
 
-	var clearcoat_normal_fragment_begin = "#ifdef CLEARCOAT\n\tvec3 clearcoatNormal = geometryNormal;\n#endif";
+					}
 
-	var clearcoat_normal_fragment_maps = "#ifdef USE_CLEARCOAT_NORMALMAP\n\tvec3 clearcoatMapN = texture2D( clearcoatNormalMap, vUv ).xyz * 2.0 - 1.0;\n\tclearcoatMapN.xy *= clearcoatNormalScale;\n\t#ifdef USE_TANGENT\n\t\tclearcoatNormal = normalize( vTBN * clearcoatMapN );\n\t#else\n\t\tclearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN );\n\t#endif\n#endif";
+				}
 
-	var clearcoat_pars_fragment = "#ifdef USE_CLEARCOATMAP\n\tuniform sampler2D clearcoatMap;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tuniform sampler2D clearcoatRoughnessMap;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tuniform sampler2D clearcoatNormalMap;\n\tuniform vec2 clearcoatNormalScale;\n#endif";
+			} else {
 
-	var packing = "vec3 packNormalToRGB( const in vec3 normal ) {\n\treturn normalize( normal ) * 0.5 + 0.5;\n}\nvec3 unpackRGBToNormal( const in vec3 rgb ) {\n\treturn 2.0 * rgb.xyz - 1.0;\n}\nconst float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;\nconst vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256.,  256. );\nconst vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );\nconst float ShiftRight8 = 1. / 256.;\nvec4 packDepthToRGBA( const in float v ) {\n\tvec4 r = vec4( fract( v * PackFactors ), v );\n\tr.yzw -= r.xyz * ShiftRight8;\treturn r * PackUpscale;\n}\nfloat unpackRGBAToDepth( const in vec4 v ) {\n\treturn dot( v, UnpackFactors );\n}\nvec4 pack2HalfToRGBA( vec2 v ) {\n\tvec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ));\n\treturn vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w);\n}\nvec2 unpackRGBATo2Half( vec4 v ) {\n\treturn vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );\n}\nfloat viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn ( viewZ + near ) / ( near - far );\n}\nfloat orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {\n\treturn linearClipZ * ( near - far ) - near;\n}\nfloat viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn (( near + viewZ ) * far ) / (( far - near ) * viewZ );\n}\nfloat perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {\n\treturn ( near * far ) / ( ( far - near ) * invClipZ - far );\n}";
+				if ( indices !== undefined ) {
 
-	var premultiplied_alpha_fragment = "#ifdef PREMULTIPLIED_ALPHA\n\tgl_FragColor.rgb *= gl_FragColor.a;\n#endif";
+					for ( var i = 0; i < indices.length; i += 3 ) {
 
-	var project_vertex = "vec4 mvPosition = vec4( transformed, 1.0 );\n#ifdef USE_INSTANCING\n\tmvPosition = instanceMatrix * mvPosition;\n#endif\nmvPosition = modelViewMatrix * mvPosition;\ngl_Position = projectionMatrix * mvPosition;";
+						addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
 
-	var dithering_fragment = "#ifdef DITHERING\n\tgl_FragColor.rgb = dithering( gl_FragColor.rgb );\n#endif";
+					}
 
-	var dithering_pars_fragment = "#ifdef DITHERING\n\tvec3 dithering( vec3 color ) {\n\t\tfloat grid_position = rand( gl_FragCoord.xy );\n\t\tvec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );\n\t\tdither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );\n\t\treturn color + dither_shift_RGB;\n\t}\n#endif";
+				} else {
 
-	var roughnessmap_fragment = "float roughnessFactor = roughness;\n#ifdef USE_ROUGHNESSMAP\n\tvec4 texelRoughness = texture2D( roughnessMap, vUv );\n\troughnessFactor *= texelRoughness.g;\n#endif";
+					for ( var i = 0; i < positions.length / 3; i += 3 ) {
 
-	var roughnessmap_pars_fragment = "#ifdef USE_ROUGHNESSMAP\n\tuniform sampler2D roughnessMap;\n#endif";
+						addFace( i, i + 1, i + 2 );
 
-	var shadowmap_pars_fragment = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D directionalShadowMap[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D spotShadowMap[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D pointShadowMap[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tfloat texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n\t\treturn step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n\t}\n\tvec2 texture2DDistribution( sampler2D shadow, vec2 uv ) {\n\t\treturn unpackRGBATo2Half( texture2D( shadow, uv ) );\n\t}\n\tfloat VSMShadow (sampler2D shadow, vec2 uv, float compare ){\n\t\tfloat occlusion = 1.0;\n\t\tvec2 distribution = texture2DDistribution( shadow, uv );\n\t\tfloat hard_shadow = step( compare , distribution.x );\n\t\tif (hard_shadow != 1.0 ) {\n\t\t\tfloat distance = compare - distribution.x ;\n\t\t\tfloat variance = max( 0.00000, distribution.y * distribution.y );\n\t\t\tfloat softness_probability = variance / (variance + distance * distance );\t\t\tsoftness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 );\t\t\tocclusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 );\n\t\t}\n\t\treturn occlusion;\n\t}\n\tfloat getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {\n\t\tfloat shadow = 1.0;\n\t\tshadowCoord.xyz /= shadowCoord.w;\n\t\tshadowCoord.z += shadowBias;\n\t\tbvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );\n\t\tbool inFrustum = all( inFrustumVec );\n\t\tbvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );\n\t\tbool frustumTest = all( frustumTestVec );\n\t\tif ( frustumTest ) {\n\t\t#if defined( SHADOWMAP_TYPE_PCF )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx0 = - texelSize.x * shadowRadius;\n\t\t\tfloat dy0 = - texelSize.y * shadowRadius;\n\t\t\tfloat dx1 = + texelSize.x * shadowRadius;\n\t\t\tfloat dy1 = + texelSize.y * shadowRadius;\n\t\t\tfloat dx2 = dx0 / 2.0;\n\t\t\tfloat dy2 = dy0 / 2.0;\n\t\t\tfloat dx3 = dx1 / 2.0;\n\t\t\tfloat dy3 = dy1 / 2.0;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )\n\t\t\t) * ( 1.0 / 17.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_PCF_SOFT )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx = texelSize.x;\n\t\t\tfloat dy = texelSize.y;\n\t\t\tvec2 uv = shadowCoord.xy;\n\t\t\tvec2 f = fract( uv * shadowMapSize + 0.5 );\n\t\t\tuv -= f * texelSize;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, uv, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t\t  texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t\t  f.x ),\n\t\t\t\t\t mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ), \n\t\t\t\t\t\t  texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t\t  f.x ),\n\t\t\t\t\t f.y )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_VSM )\n\t\t\tshadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#else\n\t\t\tshadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#endif\n\t\t}\n\t\treturn shadow;\n\t}\n\tvec2 cubeToUV( vec3 v, float texelSizeY ) {\n\t\tvec3 absV = abs( v );\n\t\tfloat scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );\n\t\tabsV *= scaleToCube;\n\t\tv *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );\n\t\tvec2 planar = v.xy;\n\t\tfloat almostATexel = 1.5 * texelSizeY;\n\t\tfloat almostOne = 1.0 - almostATexel;\n\t\tif ( absV.z >= almostOne ) {\n\t\t\tif ( v.z > 0.0 )\n\t\t\t\tplanar.x = 4.0 - v.x;\n\t\t} else if ( absV.x >= almostOne ) {\n\t\t\tfloat signX = sign( v.x );\n\t\t\tplanar.x = v.z * signX + 2.0 * signX;\n\t\t} else if ( absV.y >= almostOne ) {\n\t\t\tfloat signY = sign( v.y );\n\t\t\tplanar.x = v.x + 2.0 * signY + 2.0;\n\t\t\tplanar.y = v.z * signY - 2.0;\n\t\t}\n\t\treturn vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );\n\t}\n\tfloat getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {\n\t\tvec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );\n\t\tvec3 lightToPosition = shadowCoord.xyz;\n\t\tfloat dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear );\t\tdp += shadowBias;\n\t\tvec3 bd3D = normalize( lightToPosition );\n\t\t#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM )\n\t\t\tvec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;\n\t\t\treturn (\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#else\n\t\t\treturn texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );\n\t\t#endif\n\t}\n#endif";
+					}
 
-	var shadowmap_pars_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 spotShadowMatrix[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 pointShadowMatrix[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n#endif";
+				}
 
-	var shadowmap_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tvDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tvSpotShadowCoord[ i ] = spotShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tvPointShadowCoord[ i ] = pointShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n#endif";
+			}
 
-	var shadowmask_pars_fragment = "float getShadowMask() {\n\tfloat shadow = 1.0;\n\t#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tdirectionalLight = directionalLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tspotLight = spotLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tpointLight = pointLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#endif\n\treturn shadow;\n}";
+			this.computeFaceNormals();
 
-	var skinbase_vertex = "#ifdef USE_SKINNING\n\tmat4 boneMatX = getBoneMatrix( skinIndex.x );\n\tmat4 boneMatY = getBoneMatrix( skinIndex.y );\n\tmat4 boneMatZ = getBoneMatrix( skinIndex.z );\n\tmat4 boneMatW = getBoneMatrix( skinIndex.w );\n#endif";
+			if ( geometry.boundingBox !== null ) {
 
-	var skinning_pars_vertex = "#ifdef USE_SKINNING\n\tuniform mat4 bindMatrix;\n\tuniform mat4 bindMatrixInverse;\n\t#ifdef BONE_TEXTURE\n\t\tuniform highp sampler2D boneTexture;\n\t\tuniform int boneTextureSize;\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tfloat j = i * 4.0;\n\t\t\tfloat x = mod( j, float( boneTextureSize ) );\n\t\t\tfloat y = floor( j / float( boneTextureSize ) );\n\t\t\tfloat dx = 1.0 / float( boneTextureSize );\n\t\t\tfloat dy = 1.0 / float( boneTextureSize );\n\t\t\ty = dy * ( y + 0.5 );\n\t\t\tvec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );\n\t\t\tvec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );\n\t\t\tvec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );\n\t\t\tvec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );\n\t\t\tmat4 bone = mat4( v1, v2, v3, v4 );\n\t\t\treturn bone;\n\t\t}\n\t#else\n\t\tuniform mat4 boneMatrices[ MAX_BONES ];\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tmat4 bone = boneMatrices[ int(i) ];\n\t\t\treturn bone;\n\t\t}\n\t#endif\n#endif";
+				this.boundingBox = geometry.boundingBox.clone();
 
-	var skinning_vertex = "#ifdef USE_SKINNING\n\tvec4 skinVertex = bindMatrix * vec4( transformed, 1.0 );\n\tvec4 skinned = vec4( 0.0 );\n\tskinned += boneMatX * skinVertex * skinWeight.x;\n\tskinned += boneMatY * skinVertex * skinWeight.y;\n\tskinned += boneMatZ * skinVertex * skinWeight.z;\n\tskinned += boneMatW * skinVertex * skinWeight.w;\n\ttransformed = ( bindMatrixInverse * skinned ).xyz;\n#endif";
+			}
 
-	var skinnormal_vertex = "#ifdef USE_SKINNING\n\tmat4 skinMatrix = mat4( 0.0 );\n\tskinMatrix += skinWeight.x * boneMatX;\n\tskinMatrix += skinWeight.y * boneMatY;\n\tskinMatrix += skinWeight.z * boneMatZ;\n\tskinMatrix += skinWeight.w * boneMatW;\n\tskinMatrix  = bindMatrixInverse * skinMatrix * bindMatrix;\n\tobjectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( skinMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif";
+			if ( geometry.boundingSphere !== null ) {
 
-	var specularmap_fragment = "float specularStrength;\n#ifdef USE_SPECULARMAP\n\tvec4 texelSpecular = texture2D( specularMap, vUv );\n\tspecularStrength = texelSpecular.r;\n#else\n\tspecularStrength = 1.0;\n#endif";
+				this.boundingSphere = geometry.boundingSphere.clone();
 
-	var specularmap_pars_fragment = "#ifdef USE_SPECULARMAP\n\tuniform sampler2D specularMap;\n#endif";
+			}
 
-	var tonemapping_fragment = "#if defined( TONE_MAPPING )\n\tgl_FragColor.rgb = toneMapping( gl_FragColor.rgb );\n#endif";
+			return this;
 
-	var tonemapping_pars_fragment = "#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\nuniform float toneMappingExposure;\nuniform float toneMappingWhitePoint;\nvec3 LinearToneMapping( vec3 color ) {\n\treturn toneMappingExposure * color;\n}\nvec3 ReinhardToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( color / ( vec3( 1.0 ) + color ) );\n}\n#define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) )\nvec3 Uncharted2ToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) );\n}\nvec3 OptimizedCineonToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\tcolor = max( vec3( 0.0 ), color - 0.004 );\n\treturn pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\nvec3 ACESFilmicToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( ( color * ( 2.51 * color + 0.03 ) ) / ( color * ( 2.43 * color + 0.59 ) + 0.14 ) );\n}";
+		},
 
-	var uv_pars_fragment = "#if ( defined( USE_UV ) && ! defined( UVS_VERTEX_ONLY ) )\n\tvarying vec2 vUv;\n#endif";
+		center: function () {
 
-	var uv_pars_vertex = "#ifdef USE_UV\n\t#ifdef UVS_VERTEX_ONLY\n\t\tvec2 vUv;\n\t#else\n\t\tvarying vec2 vUv;\n\t#endif\n\tuniform mat3 uvTransform;\n#endif";
+			this.computeBoundingBox();
 
-	var uv_vertex = "#ifdef USE_UV\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n#endif";
+			this.boundingBox.getCenter( _offset$1 ).negate();
 
-	var uv2_pars_fragment = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvarying vec2 vUv2;\n#endif";
+			this.translate( _offset$1.x, _offset$1.y, _offset$1.z );
 
-	var uv2_pars_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tattribute vec2 uv2;\n\tvarying vec2 vUv2;\n\tuniform mat3 uv2Transform;\n#endif";
+			return this;
 
-	var uv2_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvUv2 = ( uv2Transform * vec3( uv2, 1 ) ).xy;\n#endif";
+		},
 
-	var worldpos_vertex = "#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP )\n\tvec4 worldPosition = vec4( transformed, 1.0 );\n\t#ifdef USE_INSTANCING\n\t\tworldPosition = instanceMatrix * worldPosition;\n\t#endif\n\tworldPosition = modelMatrix * worldPosition;\n#endif";
+		normalize: function () {
 
-	var background_frag = "uniform sampler2D t2D;\nvarying vec2 vUv;\nvoid main() {\n\tvec4 texColor = texture2D( t2D, vUv );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
+			this.computeBoundingSphere();
 
-	var background_vert = "varying vec2 vUv;\nuniform mat3 uvTransform;\nvoid main() {\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n\tgl_Position = vec4( position.xy, 1.0, 1.0 );\n}";
+			var center = this.boundingSphere.center;
+			var radius = this.boundingSphere.radius;
 
-	var cube_frag = "#include <envmap_common_pars_fragment>\nuniform float opacity;\nvarying vec3 vWorldDirection;\n#include <cube_uv_reflection_fragment>\nvoid main() {\n\tvec3 vReflect = vWorldDirection;\n\t#include <envmap_fragment>\n\tgl_FragColor = envColor;\n\tgl_FragColor.a *= opacity;\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
+			var s = radius === 0 ? 1 : 1.0 / radius;
 
-	var cube_vert = "varying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\tgl_Position.z = gl_Position.w;\n}";
+			var matrix = new Matrix4();
+			matrix.set(
+				s, 0, 0, - s * center.x,
+				0, s, 0, - s * center.y,
+				0, 0, s, - s * center.z,
+				0, 0, 0, 1
+			);
 
-	var depth_frag = "#if DEPTH_PACKING == 3200\n\tuniform float opacity;\n#endif\n#include <common>\n#include <packing>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#if DEPTH_PACKING == 3200\n\t\tdiffuseColor.a = opacity;\n\t#endif\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <logdepthbuf_fragment>\n\tfloat fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;\n\t#if DEPTH_PACKING == 3200\n\t\tgl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );\n\t#elif DEPTH_PACKING == 3201\n\t\tgl_FragColor = packDepthToRGBA( fragCoordZ );\n\t#endif\n}";
+			this.applyMatrix4( matrix );
 
-	var depth_vert = "#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include <uv_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include <beginnormal_vertex>\n\t\t#include <morphnormal_vertex>\n\t\t#include <skinnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvHighPrecisionZW = gl_Position.zw;\n}";
+			return this;
 
-	var distanceRGBA_frag = "#define DISTANCE\nuniform vec3 referencePosition;\nuniform float nearDistance;\nuniform float farDistance;\nvarying vec3 vWorldPosition;\n#include <common>\n#include <packing>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main () {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\tfloat dist = length( vWorldPosition - referencePosition );\n\tdist = ( dist - nearDistance ) / ( farDistance - nearDistance );\n\tdist = saturate( dist );\n\tgl_FragColor = packDepthToRGBA( dist );\n}";
+		},
 
-	var distanceRGBA_vert = "#define DISTANCE\nvarying vec3 vWorldPosition;\n#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include <beginnormal_vertex>\n\t\t#include <morphnormal_vertex>\n\t\t#include <skinnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <worldpos_vertex>\n\t#include <clipping_planes_vertex>\n\tvWorldPosition = worldPosition.xyz;\n}";
+		computeFaceNormals: function () {
 
-	var equirect_frag = "uniform sampler2D tEquirect;\nvarying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvec3 direction = normalize( vWorldDirection );\n\tvec2 sampleUV;\n\tsampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\tsampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;\n\tvec4 texColor = texture2D( tEquirect, sampleUV );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
-
-	var equirect_vert = "varying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include <begin_vertex>\n\t#include <project_vertex>\n}";
-
-	var linedashed_frag = "uniform vec3 diffuse;\nuniform float opacity;\nuniform float dashSize;\nuniform float totalSize;\nvarying float vLineDistance;\n#include <common>\n#include <color_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tif ( mod( vLineDistance, totalSize ) > dashSize ) {\n\t\tdiscard;\n\t}\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <color_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n}";
-
-	var linedashed_vert = "uniform float scale;\nattribute float lineDistance;\nvarying float vLineDistance;\n#include <common>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\tvLineDistance = scale * lineDistance;\n\t#include <color_vertex>\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n}";
+			var cb = new Vector3(), ab = new Vector3();
 
-	var meshbasic_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <fog_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\t#ifdef USE_LIGHTMAP\n\t\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\treflectedLight.indirectDiffuse += lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t#else\n\t\treflectedLight.indirectDiffuse += vec3( 1.0 );\n\t#endif\n\t#include <aomap_fragment>\n\treflectedLight.indirectDiffuse *= diffuseColor.rgb;\n\tvec3 outgoingLight = reflectedLight.indirectDiffuse;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+			for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-	var meshbasic_vert = "#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <envmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_ENVMAP\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <worldpos_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <envmap_vertex>\n\t#include <fog_vertex>\n}";
+				var face = this.faces[ f ];
 
-	var meshlambert_frag = "uniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <fog_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <shadowmask_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <emissivemap_fragment>\n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.indirectDiffuse += ( gl_FrontFacing ) ? vIndirectFront : vIndirectBack;\n\t#else\n\t\treflectedLight.indirectDiffuse += vIndirectFront;\n\t#endif\n\t#include <lightmap_fragment>\n\treflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb );\n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;\n\t#else\n\t\treflectedLight.directDiffuse = vLightFront;\n\t#endif\n\treflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask();\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+				var vA = this.vertices[ face.a ];
+				var vB = this.vertices[ face.b ];
+				var vC = this.vertices[ face.c ];
 
-	var meshlambert_vert = "#define LAMBERT\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <envmap_pars_vertex>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <worldpos_vertex>\n\t#include <envmap_vertex>\n\t#include <lights_lambert_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
+				cb.subVectors( vC, vB );
+				ab.subVectors( vA, vB );
+				cb.cross( ab );
 
-	var meshmatcap_frag = "#define MATCAP\nuniform vec3 diffuse;\nuniform float opacity;\nuniform sampler2D matcap;\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <fog_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\tvec3 viewDir = normalize( vViewPosition );\n\tvec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );\n\tvec3 y = cross( viewDir, x );\n\tvec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5;\n\t#ifdef USE_MATCAP\n\t\tvec4 matcapColor = texture2D( matcap, uv );\n\t\tmatcapColor = matcapTexelToLinear( matcapColor );\n\t#else\n\t\tvec4 matcapColor = vec4( 1.0 );\n\t#endif\n\tvec3 outgoingLight = diffuseColor.rgb * matcapColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+				cb.normalize();
 
-	var meshmatcap_vert = "#define MATCAP\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <color_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#ifndef FLAT_SHADED\n\t\tvNormal = normalize( transformedNormal );\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n\tvViewPosition = - mvPosition.xyz;\n}";
+				face.normal.copy( cb );
 
-	var meshtoon_frag = "#define TOON\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <gradientmap_pars_fragment>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <lights_toon_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_toon_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+			}
 
-	var meshtoon_vert = "#define TOON\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
+		},
 
-	var meshphong_frag = "#define PHONG\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <lights_phong_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_phong_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+		computeVertexNormals: function ( areaWeighted ) {
 
-	var meshphong_vert = "#define PHONG\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <envmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <envmap_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
+			if ( areaWeighted === undefined ) { areaWeighted = true; }
 
-	var meshphysical_frag = "#define STANDARD\n#ifdef PHYSICAL\n\t#define REFLECTIVITY\n\t#define CLEARCOAT\n\t#define TRANSPARENCY\n#endif\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float roughness;\nuniform float metalness;\nuniform float opacity;\n#ifdef TRANSPARENCY\n\tuniform float transparency;\n#endif\n#ifdef REFLECTIVITY\n\tuniform float reflectivity;\n#endif\n#ifdef CLEARCOAT\n\tuniform float clearcoat;\n\tuniform float clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tuniform vec3 sheen;\n#endif\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <bsdfs>\n#include <cube_uv_reflection_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_physical_pars_fragment>\n#include <fog_pars_fragment>\n#include <lights_pars_begin>\n#include <lights_physical_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <clearcoat_pars_fragment>\n#include <roughnessmap_pars_fragment>\n#include <metalnessmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <roughnessmap_fragment>\n\t#include <metalnessmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <clearcoat_normal_fragment_begin>\n\t#include <clearcoat_normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_physical_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#ifdef TRANSPARENCY\n\t\tdiffuseColor.a *= saturate( 1. - transparency + linearToRelativeLuminance( reflectedLight.directSpecular + reflectedLight.indirectSpecular ) );\n\t#endif\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
+			var v, vl, f, fl, face, vertices;
 
-	var meshphysical_vert = "#define STANDARD\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
+			vertices = new Array( this.vertices.length );
 
-	var normal_frag = "#define NORMAL\nuniform float opacity;\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <packing>\n#include <uv_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\t#include <logdepthbuf_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\tgl_FragColor = vec4( packNormalToRGB( normal ), opacity );\n}";
+			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
 
-	var normal_vert = "#define NORMAL\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvViewPosition = - mvPosition.xyz;\n#endif\n}";
+				vertices[ v ] = new Vector3();
 
-	var points_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include <common>\n#include <color_pars_fragment>\n#include <map_particle_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_particle_fragment>\n\t#include <color_fragment>\n\t#include <alphatest_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n}";
+			}
 
-	var points_vert = "uniform float size;\nuniform float scale;\n#include <common>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <color_vertex>\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <project_vertex>\n\tgl_PointSize = size;\n\t#ifdef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) gl_PointSize *= ( scale / - mvPosition.z );\n\t#endif\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <worldpos_vertex>\n\t#include <fog_vertex>\n}";
+			if ( areaWeighted ) {
 
-	var shadow_frag = "uniform vec3 color;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <shadowmap_pars_fragment>\n#include <shadowmask_pars_fragment>\nvoid main() {\n\tgl_FragColor = vec4( color, opacity * ( 1.0 - getShadowMask() ) );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n}";
+				// vertex normals weighted by triangle areas
+				// http://www.iquilezles.org/www/articles/normals/normals.htm
 
-	var shadow_vert = "#include <fog_pars_vertex>\n#include <shadowmap_pars_vertex>\nvoid main() {\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
+				var vA, vB, vC;
+				var cb = new Vector3(), ab = new Vector3();
 
-	var sprite_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include <common>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n}";
+				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-	var sprite_vert = "uniform float rotation;\nuniform vec2 center;\n#include <common>\n#include <uv_pars_vertex>\n#include <fog_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\tvec4 mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\n\tvec2 scale;\n\tscale.x = length( vec3( modelMatrix[ 0 ].x, modelMatrix[ 0 ].y, modelMatrix[ 0 ].z ) );\n\tscale.y = length( vec3( modelMatrix[ 1 ].x, modelMatrix[ 1 ].y, modelMatrix[ 1 ].z ) );\n\t#ifndef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) scale *= - mvPosition.z;\n\t#endif\n\tvec2 alignedPosition = ( position.xy - ( center - vec2( 0.5 ) ) ) * scale;\n\tvec2 rotatedPosition;\n\trotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;\n\trotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;\n\tmvPosition.xy += rotatedPosition;\n\tgl_Position = projectionMatrix * mvPosition;\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n}";
+					face = this.faces[ f ];
 
-	var ShaderChunk = {
-		alphamap_fragment: alphamap_fragment,
-		alphamap_pars_fragment: alphamap_pars_fragment,
-		alphatest_fragment: alphatest_fragment,
-		aomap_fragment: aomap_fragment,
-		aomap_pars_fragment: aomap_pars_fragment,
-		begin_vertex: begin_vertex,
-		beginnormal_vertex: beginnormal_vertex,
-		bsdfs: bsdfs,
-		bumpmap_pars_fragment: bumpmap_pars_fragment,
-		clipping_planes_fragment: clipping_planes_fragment,
-		clipping_planes_pars_fragment: clipping_planes_pars_fragment,
-		clipping_planes_pars_vertex: clipping_planes_pars_vertex,
-		clipping_planes_vertex: clipping_planes_vertex,
-		color_fragment: color_fragment,
-		color_pars_fragment: color_pars_fragment,
-		color_pars_vertex: color_pars_vertex,
-		color_vertex: color_vertex,
-		common: common,
-		cube_uv_reflection_fragment: cube_uv_reflection_fragment,
-		defaultnormal_vertex: defaultnormal_vertex,
-		displacementmap_pars_vertex: displacementmap_pars_vertex,
-		displacementmap_vertex: displacementmap_vertex,
-		emissivemap_fragment: emissivemap_fragment,
-		emissivemap_pars_fragment: emissivemap_pars_fragment,
-		encodings_fragment: encodings_fragment,
-		encodings_pars_fragment: encodings_pars_fragment,
-		envmap_fragment: envmap_fragment,
-		envmap_common_pars_fragment: envmap_common_pars_fragment,
-		envmap_pars_fragment: envmap_pars_fragment,
-		envmap_pars_vertex: envmap_pars_vertex,
-		envmap_physical_pars_fragment: envmap_physical_pars_fragment,
-		envmap_vertex: envmap_vertex,
-		fog_vertex: fog_vertex,
-		fog_pars_vertex: fog_pars_vertex,
-		fog_fragment: fog_fragment,
-		fog_pars_fragment: fog_pars_fragment,
-		gradientmap_pars_fragment: gradientmap_pars_fragment,
-		lightmap_fragment: lightmap_fragment,
-		lightmap_pars_fragment: lightmap_pars_fragment,
-		lights_lambert_vertex: lights_lambert_vertex,
-		lights_pars_begin: lights_pars_begin,
-		lights_toon_fragment: lights_toon_fragment,
-		lights_toon_pars_fragment: lights_toon_pars_fragment,
-		lights_phong_fragment: lights_phong_fragment,
-		lights_phong_pars_fragment: lights_phong_pars_fragment,
-		lights_physical_fragment: lights_physical_fragment,
-		lights_physical_pars_fragment: lights_physical_pars_fragment,
-		lights_fragment_begin: lights_fragment_begin,
-		lights_fragment_maps: lights_fragment_maps,
-		lights_fragment_end: lights_fragment_end,
-		logdepthbuf_fragment: logdepthbuf_fragment,
-		logdepthbuf_pars_fragment: logdepthbuf_pars_fragment,
-		logdepthbuf_pars_vertex: logdepthbuf_pars_vertex,
-		logdepthbuf_vertex: logdepthbuf_vertex,
-		map_fragment: map_fragment,
-		map_pars_fragment: map_pars_fragment,
-		map_particle_fragment: map_particle_fragment,
-		map_particle_pars_fragment: map_particle_pars_fragment,
-		metalnessmap_fragment: metalnessmap_fragment,
-		metalnessmap_pars_fragment: metalnessmap_pars_fragment,
-		morphnormal_vertex: morphnormal_vertex,
-		morphtarget_pars_vertex: morphtarget_pars_vertex,
-		morphtarget_vertex: morphtarget_vertex,
-		normal_fragment_begin: normal_fragment_begin,
-		normal_fragment_maps: normal_fragment_maps,
-		normalmap_pars_fragment: normalmap_pars_fragment,
-		clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin,
-		clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps,
-		clearcoat_pars_fragment: clearcoat_pars_fragment,
-		packing: packing,
-		premultiplied_alpha_fragment: premultiplied_alpha_fragment,
-		project_vertex: project_vertex,
-		dithering_fragment: dithering_fragment,
-		dithering_pars_fragment: dithering_pars_fragment,
-		roughnessmap_fragment: roughnessmap_fragment,
-		roughnessmap_pars_fragment: roughnessmap_pars_fragment,
-		shadowmap_pars_fragment: shadowmap_pars_fragment,
-		shadowmap_pars_vertex: shadowmap_pars_vertex,
-		shadowmap_vertex: shadowmap_vertex,
-		shadowmask_pars_fragment: shadowmask_pars_fragment,
-		skinbase_vertex: skinbase_vertex,
-		skinning_pars_vertex: skinning_pars_vertex,
-		skinning_vertex: skinning_vertex,
-		skinnormal_vertex: skinnormal_vertex,
-		specularmap_fragment: specularmap_fragment,
-		specularmap_pars_fragment: specularmap_pars_fragment,
-		tonemapping_fragment: tonemapping_fragment,
-		tonemapping_pars_fragment: tonemapping_pars_fragment,
-		uv_pars_fragment: uv_pars_fragment,
-		uv_pars_vertex: uv_pars_vertex,
-		uv_vertex: uv_vertex,
-		uv2_pars_fragment: uv2_pars_fragment,
-		uv2_pars_vertex: uv2_pars_vertex,
-		uv2_vertex: uv2_vertex,
-		worldpos_vertex: worldpos_vertex,
+					vA = this.vertices[ face.a ];
+					vB = this.vertices[ face.b ];
+					vC = this.vertices[ face.c ];
 
-		background_frag: background_frag,
-		background_vert: background_vert,
-		cube_frag: cube_frag,
-		cube_vert: cube_vert,
-		depth_frag: depth_frag,
-		depth_vert: depth_vert,
-		distanceRGBA_frag: distanceRGBA_frag,
-		distanceRGBA_vert: distanceRGBA_vert,
-		equirect_frag: equirect_frag,
-		equirect_vert: equirect_vert,
-		linedashed_frag: linedashed_frag,
-		linedashed_vert: linedashed_vert,
-		meshbasic_frag: meshbasic_frag,
-		meshbasic_vert: meshbasic_vert,
-		meshlambert_frag: meshlambert_frag,
-		meshlambert_vert: meshlambert_vert,
-		meshmatcap_frag: meshmatcap_frag,
-		meshmatcap_vert: meshmatcap_vert,
-		meshtoon_frag: meshtoon_frag,
-		meshtoon_vert: meshtoon_vert,
-		meshphong_frag: meshphong_frag,
-		meshphong_vert: meshphong_vert,
-		meshphysical_frag: meshphysical_frag,
-		meshphysical_vert: meshphysical_vert,
-		normal_frag: normal_frag,
-		normal_vert: normal_vert,
-		points_frag: points_frag,
-		points_vert: points_vert,
-		shadow_frag: shadow_frag,
-		shadow_vert: shadow_vert,
-		sprite_frag: sprite_frag,
-		sprite_vert: sprite_vert
-	};
+					cb.subVectors( vC, vB );
+					ab.subVectors( vA, vB );
+					cb.cross( ab );
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author mikael emtinger / http://gomo.se/
-	 */
+					vertices[ face.a ].add( cb );
+					vertices[ face.b ].add( cb );
+					vertices[ face.c ].add( cb );
 
-	var ShaderLib = {
+				}
 
-		basic: {
+			} else {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.specularmap,
-				UniformsLib.envmap,
-				UniformsLib.aomap,
-				UniformsLib.lightmap,
-				UniformsLib.fog
-			] ),
+				this.computeFaceNormals();
 
-			vertexShader: ShaderChunk.meshbasic_vert,
-			fragmentShader: ShaderChunk.meshbasic_frag
+				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-		},
+					face = this.faces[ f ];
 
-		lambert: {
+					vertices[ face.a ].add( face.normal );
+					vertices[ face.b ].add( face.normal );
+					vertices[ face.c ].add( face.normal );
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.specularmap,
-				UniformsLib.envmap,
-				UniformsLib.aomap,
-				UniformsLib.lightmap,
-				UniformsLib.emissivemap,
-				UniformsLib.fog,
-				UniformsLib.lights,
-				{
-					emissive: { value: new Color( 0x000000 ) }
 				}
-			] ),
 
-			vertexShader: ShaderChunk.meshlambert_vert,
-			fragmentShader: ShaderChunk.meshlambert_frag
+			}
 
-		},
+			for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
 
-		phong: {
+				vertices[ v ].normalize();
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.specularmap,
-				UniformsLib.envmap,
-				UniformsLib.aomap,
-				UniformsLib.lightmap,
-				UniformsLib.emissivemap,
-				UniformsLib.bumpmap,
-				UniformsLib.normalmap,
-				UniformsLib.displacementmap,
-				UniformsLib.fog,
-				UniformsLib.lights,
-				{
-					emissive: { value: new Color( 0x000000 ) },
-					specular: { value: new Color( 0x111111 ) },
-					shininess: { value: 30 }
-				}
-			] ),
+			}
 
-			vertexShader: ShaderChunk.meshphong_vert,
-			fragmentShader: ShaderChunk.meshphong_frag
+			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-		},
+				face = this.faces[ f ];
 
-		standard: {
+				var vertexNormals = face.vertexNormals;
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.envmap,
-				UniformsLib.aomap,
-				UniformsLib.lightmap,
-				UniformsLib.emissivemap,
-				UniformsLib.bumpmap,
-				UniformsLib.normalmap,
-				UniformsLib.displacementmap,
-				UniformsLib.roughnessmap,
-				UniformsLib.metalnessmap,
-				UniformsLib.fog,
-				UniformsLib.lights,
-				{
-					emissive: { value: new Color( 0x000000 ) },
-					roughness: { value: 1.0 },
-					metalness: { value: 0.0 },
-					envMapIntensity: { value: 1 } // temporary
-				}
-			] ),
+				if ( vertexNormals.length === 3 ) {
 
-			vertexShader: ShaderChunk.meshphysical_vert,
-			fragmentShader: ShaderChunk.meshphysical_frag
+					vertexNormals[ 0 ].copy( vertices[ face.a ] );
+					vertexNormals[ 1 ].copy( vertices[ face.b ] );
+					vertexNormals[ 2 ].copy( vertices[ face.c ] );
 
-		},
+				} else {
 
-		toon: {
+					vertexNormals[ 0 ] = vertices[ face.a ].clone();
+					vertexNormals[ 1 ] = vertices[ face.b ].clone();
+					vertexNormals[ 2 ] = vertices[ face.c ].clone();
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.specularmap,
-				UniformsLib.aomap,
-				UniformsLib.lightmap,
-				UniformsLib.emissivemap,
-				UniformsLib.bumpmap,
-				UniformsLib.normalmap,
-				UniformsLib.displacementmap,
-				UniformsLib.gradientmap,
-				UniformsLib.fog,
-				UniformsLib.lights,
-				{
-					emissive: { value: new Color( 0x000000 ) },
-					specular: { value: new Color( 0x111111 ) },
-					shininess: { value: 30 }
 				}
-			] ),
-
-			vertexShader: ShaderChunk.meshtoon_vert,
-			fragmentShader: ShaderChunk.meshtoon_frag
 
-		},
+			}
 
-		matcap: {
+			if ( this.faces.length > 0 ) {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.bumpmap,
-				UniformsLib.normalmap,
-				UniformsLib.displacementmap,
-				UniformsLib.fog,
-				{
-					matcap: { value: null }
-				}
-			] ),
+				this.normalsNeedUpdate = true;
 
-			vertexShader: ShaderChunk.meshmatcap_vert,
-			fragmentShader: ShaderChunk.meshmatcap_frag
+			}
 
 		},
 
-		points: {
+		computeFlatVertexNormals: function () {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.points,
-				UniformsLib.fog
-			] ),
+			var f, fl, face;
 
-			vertexShader: ShaderChunk.points_vert,
-			fragmentShader: ShaderChunk.points_frag
+			this.computeFaceNormals();
 
-		},
+			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-		dashed: {
+				face = this.faces[ f ];
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.fog,
-				{
-					scale: { value: 1 },
-					dashSize: { value: 1 },
-					totalSize: { value: 2 }
-				}
-			] ),
+				var vertexNormals = face.vertexNormals;
 
-			vertexShader: ShaderChunk.linedashed_vert,
-			fragmentShader: ShaderChunk.linedashed_frag
+				if ( vertexNormals.length === 3 ) {
 
-		},
+					vertexNormals[ 0 ].copy( face.normal );
+					vertexNormals[ 1 ].copy( face.normal );
+					vertexNormals[ 2 ].copy( face.normal );
 
-		depth: {
+				} else {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.displacementmap
-			] ),
+					vertexNormals[ 0 ] = face.normal.clone();
+					vertexNormals[ 1 ] = face.normal.clone();
+					vertexNormals[ 2 ] = face.normal.clone();
 
-			vertexShader: ShaderChunk.depth_vert,
-			fragmentShader: ShaderChunk.depth_frag
+				}
 
-		},
+			}
 
-		normal: {
+			if ( this.faces.length > 0 ) {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.bumpmap,
-				UniformsLib.normalmap,
-				UniformsLib.displacementmap,
-				{
-					opacity: { value: 1.0 }
-				}
-			] ),
+				this.normalsNeedUpdate = true;
 
-			vertexShader: ShaderChunk.normal_vert,
-			fragmentShader: ShaderChunk.normal_frag
+			}
 
 		},
 
-		sprite: {
+		computeMorphNormals: function () {
 
-			uniforms: mergeUniforms( [
-				UniformsLib.sprite,
-				UniformsLib.fog
-			] ),
+			var i, il, f, fl, face;
 
-			vertexShader: ShaderChunk.sprite_vert,
-			fragmentShader: ShaderChunk.sprite_frag
+			// save original normals
+			// - create temp variables on first access
+			//   otherwise just copy (for faster repeated calls)
 
-		},
+			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-		background: {
+				face = this.faces[ f ];
 
-			uniforms: {
-				uvTransform: { value: new Matrix3() },
-				t2D: { value: null },
-			},
+				if ( ! face.__originalFaceNormal ) {
 
-			vertexShader: ShaderChunk.background_vert,
-			fragmentShader: ShaderChunk.background_frag
+					face.__originalFaceNormal = face.normal.clone();
 
-		},
-		/* -------------------------------------------------------------------------
-		//	Cube map shader
-		 ------------------------------------------------------------------------- */
+				} else {
 
-		cube: {
+					face.__originalFaceNormal.copy( face.normal );
 
-			uniforms: mergeUniforms( [
-				UniformsLib.envmap,
-				{
-					opacity: { value: 1.0 }
 				}
-			] ),
 
-			vertexShader: ShaderChunk.cube_vert,
-			fragmentShader: ShaderChunk.cube_frag
+				if ( ! face.__originalVertexNormals ) { face.__originalVertexNormals = []; }
 
-		},
+				for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
 
-		equirect: {
+					if ( ! face.__originalVertexNormals[ i ] ) {
 
-			uniforms: {
-				tEquirect: { value: null },
-			},
+						face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
 
-			vertexShader: ShaderChunk.equirect_vert,
-			fragmentShader: ShaderChunk.equirect_frag
+					} else {
 
-		},
+						face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
 
-		distanceRGBA: {
+					}
 
-			uniforms: mergeUniforms( [
-				UniformsLib.common,
-				UniformsLib.displacementmap,
-				{
-					referencePosition: { value: new Vector3() },
-					nearDistance: { value: 1 },
-					farDistance: { value: 1000 }
 				}
-			] ),
 
-			vertexShader: ShaderChunk.distanceRGBA_vert,
-			fragmentShader: ShaderChunk.distanceRGBA_frag
+			}
 
-		},
+			// use temp geometry to compute face and vertex normals for each morph
 
-		shadow: {
+			var tmpGeo = new Geometry();
+			tmpGeo.faces = this.faces;
 
-			uniforms: mergeUniforms( [
-				UniformsLib.lights,
-				UniformsLib.fog,
-				{
-					color: { value: new Color( 0x00000 ) },
-					opacity: { value: 1.0 }
-				} ] ),
+			for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
 
-			vertexShader: ShaderChunk.shadow_vert,
-			fragmentShader: ShaderChunk.shadow_frag
+				// create on first access
 
-		}
+				if ( ! this.morphNormals[ i ] ) {
 
-	};
+					this.morphNormals[ i ] = {};
+					this.morphNormals[ i ].faceNormals = [];
+					this.morphNormals[ i ].vertexNormals = [];
 
-	ShaderLib.physical = {
+					var dstNormalsFace = this.morphNormals[ i ].faceNormals;
+					var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
 
-		uniforms: mergeUniforms( [
-			ShaderLib.standard.uniforms,
-			{
-				clearcoat: { value: 0 },
-				clearcoatMap: { value: null },
-				clearcoatRoughness: { value: 0 },
-				clearcoatRoughnessMap: { value: null },
-				clearcoatNormalScale: { value: new Vector2( 1, 1 ) },
-				clearcoatNormalMap: { value: null },
-				sheen: { value: new Color( 0x000000 ) },
-				transparency: { value: 0 },
-			}
-		] ),
+					var faceNormal, vertexNormals;
 
-		vertexShader: ShaderChunk.meshphysical_vert,
-		fragmentShader: ShaderChunk.meshphysical_frag
+					for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-	};
+						faceNormal = new Vector3();
+						vertexNormals = { a: new Vector3(), b: new Vector3(), c: new Vector3() };
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+						dstNormalsFace.push( faceNormal );
+						dstNormalsVertex.push( vertexNormals );
 
-	function WebGLBackground( renderer, state, objects, premultipliedAlpha ) {
+					}
 
-		var clearColor = new Color( 0x000000 );
-		var clearAlpha = 0;
+				}
 
-		var planeMesh;
-		var boxMesh;
+				var morphNormals = this.morphNormals[ i ];
 
-		var currentBackground = null;
-		var currentBackgroundVersion = 0;
-		var currentTonemapping = null;
+				// set vertices to morph target
 
-		function render( renderList, scene, camera, forceClear ) {
+				tmpGeo.vertices = this.morphTargets[ i ].vertices;
 
-			var background = scene.background;
+				// compute morph normals
 
-			// Ignore background in AR
-			// TODO: Reconsider this.
+				tmpGeo.computeFaceNormals();
+				tmpGeo.computeVertexNormals();
 
-			var xr = renderer.xr;
-			var session = xr.getSession && xr.getSession();
+				// store morph normals
 
-			if ( session && session.environmentBlendMode === 'additive' ) {
+				var faceNormal, vertexNormals;
 
-				background = null;
+				for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-			}
+					face = this.faces[ f ];
 
-			if ( background === null ) {
+					faceNormal = morphNormals.faceNormals[ f ];
+					vertexNormals = morphNormals.vertexNormals[ f ];
 
-				setClear( clearColor, clearAlpha );
+					faceNormal.copy( face.normal );
 
-			} else if ( background && background.isColor ) {
+					vertexNormals.a.copy( face.vertexNormals[ 0 ] );
+					vertexNormals.b.copy( face.vertexNormals[ 1 ] );
+					vertexNormals.c.copy( face.vertexNormals[ 2 ] );
 
-				setClear( background, 1 );
-				forceClear = true;
+				}
 
 			}
 
-			if ( renderer.autoClear || forceClear ) {
+			// restore original normals
 
-				renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
+			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
-			}
+				face = this.faces[ f ];
 
-			if ( background && ( background.isCubeTexture || background.isWebGLCubeRenderTarget || background.mapping === CubeUVReflectionMapping ) ) {
+				face.normal = face.__originalFaceNormal;
+				face.vertexNormals = face.__originalVertexNormals;
 
-				if ( boxMesh === undefined ) {
+			}
 
-					boxMesh = new Mesh(
-						new BoxBufferGeometry( 1, 1, 1 ),
-						new ShaderMaterial( {
-							type: 'BackgroundCubeMaterial',
-							uniforms: cloneUniforms( ShaderLib.cube.uniforms ),
-							vertexShader: ShaderLib.cube.vertexShader,
-							fragmentShader: ShaderLib.cube.fragmentShader,
-							side: BackSide,
-							depthTest: false,
-							depthWrite: false,
-							fog: false
-						} )
-					);
+		},
 
-					boxMesh.geometry.deleteAttribute( 'normal' );
-					boxMesh.geometry.deleteAttribute( 'uv' );
+		computeBoundingBox: function () {
 
-					boxMesh.onBeforeRender = function ( renderer, scene, camera ) {
+			if ( this.boundingBox === null ) {
 
-						this.matrixWorld.copyPosition( camera.matrixWorld );
+				this.boundingBox = new Box3();
 
-					};
+			}
 
-					// enable code injection for non-built-in material
-					Object.defineProperty( boxMesh.material, 'envMap', {
+			this.boundingBox.setFromPoints( this.vertices );
 
-						get: function () {
+		},
 
-							return this.uniforms.envMap.value;
+		computeBoundingSphere: function () {
 
-						}
+			if ( this.boundingSphere === null ) {
 
-					} );
+				this.boundingSphere = new Sphere();
 
-					objects.update( boxMesh );
+			}
 
-				}
+			this.boundingSphere.setFromPoints( this.vertices );
 
-				var texture = background.isWebGLCubeRenderTarget ? background.texture : background;
+		},
 
-				boxMesh.material.uniforms.envMap.value = texture;
-				boxMesh.material.uniforms.flipEnvMap.value = texture.isCubeTexture ? - 1 : 1;
+		merge: function ( geometry, matrix, materialIndexOffset ) {
 
-				if ( currentBackground !== background ||
-					currentBackgroundVersion !== texture.version ||
-					currentTonemapping !== renderer.toneMapping ) {
+			if ( ! ( geometry && geometry.isGeometry ) ) {
 
-					boxMesh.material.needsUpdate = true;
+				console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
+				return;
 
-					currentBackground = background;
-					currentBackgroundVersion = texture.version;
-					currentTonemapping = renderer.toneMapping;
+			}
 
-				}
+			var normalMatrix,
+				vertexOffset = this.vertices.length,
+				vertices1 = this.vertices,
+				vertices2 = geometry.vertices,
+				faces1 = this.faces,
+				faces2 = geometry.faces,
+				colors1 = this.colors,
+				colors2 = geometry.colors;
 
-				// push to the pre-sorted opaque render list
-				renderList.unshift( boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null );
+			if ( materialIndexOffset === undefined ) { materialIndexOffset = 0; }
 
-			} else if ( background && background.isTexture ) {
+			if ( matrix !== undefined ) {
 
-				if ( planeMesh === undefined ) {
+				normalMatrix = new Matrix3().getNormalMatrix( matrix );
 
-					planeMesh = new Mesh(
-						new PlaneBufferGeometry( 2, 2 ),
-						new ShaderMaterial( {
-							type: 'BackgroundMaterial',
-							uniforms: cloneUniforms( ShaderLib.background.uniforms ),
-							vertexShader: ShaderLib.background.vertexShader,
-							fragmentShader: ShaderLib.background.fragmentShader,
-							side: FrontSide,
-							depthTest: false,
-							depthWrite: false,
-							fog: false
-						} )
-					);
+			}
 
-					planeMesh.geometry.deleteAttribute( 'normal' );
+			// vertices
 
-					// enable code injection for non-built-in material
-					Object.defineProperty( planeMesh.material, 'map', {
+			for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
 
-						get: function () {
+				var vertex = vertices2[ i ];
 
-							return this.uniforms.t2D.value;
+				var vertexCopy = vertex.clone();
 
-						}
+				if ( matrix !== undefined ) { vertexCopy.applyMatrix4( matrix ); }
 
-					} );
+				vertices1.push( vertexCopy );
 
-					objects.update( planeMesh );
+			}
 
-				}
+			// colors
 
-				planeMesh.material.uniforms.t2D.value = background;
+			for ( var i = 0, il = colors2.length; i < il; i ++ ) {
 
-				if ( background.matrixAutoUpdate === true ) {
+				colors1.push( colors2[ i ].clone() );
 
-					background.updateMatrix();
+			}
 
-				}
+			// faces
 
-				planeMesh.material.uniforms.uvTransform.value.copy( background.matrix );
+			for ( i = 0, il = faces2.length; i < il; i ++ ) {
 
-				if ( currentBackground !== background ||
-					currentBackgroundVersion !== background.version ||
-					currentTonemapping !== renderer.toneMapping ) {
+				var face = faces2[ i ], faceCopy, normal, color,
+					faceVertexNormals = face.vertexNormals,
+					faceVertexColors = face.vertexColors;
 
-					planeMesh.material.needsUpdate = true;
+				faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
+				faceCopy.normal.copy( face.normal );
 
-					currentBackground = background;
-					currentBackgroundVersion = background.version;
-					currentTonemapping = renderer.toneMapping;
+				if ( normalMatrix !== undefined ) {
+
+					faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
 
 				}
 
+				for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
 
-				// push to the pre-sorted opaque render list
-				renderList.unshift( planeMesh, planeMesh.geometry, planeMesh.material, 0, 0, null );
+					normal = faceVertexNormals[ j ].clone();
 
-			}
+					if ( normalMatrix !== undefined ) {
 
-		}
+						normal.applyMatrix3( normalMatrix ).normalize();
 
-		function setClear( color, alpha ) {
+					}
 
-			state.buffers.color.setClear( color.r, color.g, color.b, alpha, premultipliedAlpha );
+					faceCopy.vertexNormals.push( normal );
 
-		}
+				}
 
-		return {
+				faceCopy.color.copy( face.color );
 
-			getClearColor: function () {
+				for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
 
-				return clearColor;
+					color = faceVertexColors[ j ];
+					faceCopy.vertexColors.push( color.clone() );
 
-			},
-			setClearColor: function ( color, alpha ) {
+				}
 
-				clearColor.set( color );
-				clearAlpha = alpha !== undefined ? alpha : 1;
-				setClear( clearColor, clearAlpha );
+				faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
 
-			},
-			getClearAlpha: function () {
+				faces1.push( faceCopy );
 
-				return clearAlpha;
+			}
 
-			},
-			setClearAlpha: function ( alpha ) {
+			// uvs
 
-				clearAlpha = alpha;
-				setClear( clearColor, clearAlpha );
+			for ( var i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
 
-			},
-			render: render
+				var faceVertexUvs2 = geometry.faceVertexUvs[ i ];
 
-		};
+				if ( this.faceVertexUvs[ i ] === undefined ) { this.faceVertexUvs[ i ] = []; }
 
-	}
+				for ( var j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+					var uvs2 = faceVertexUvs2[ j ], uvsCopy = [];
 
-	function WebGLBufferRenderer( gl, extensions, info, capabilities ) {
+					for ( var k = 0, kl = uvs2.length; k < kl; k ++ ) {
 
-		var isWebGL2 = capabilities.isWebGL2;
+						uvsCopy.push( uvs2[ k ].clone() );
 
-		var mode;
+					}
 
-		function setMode( value ) {
+					this.faceVertexUvs[ i ].push( uvsCopy );
 
-			mode = value;
+				}
 
-		}
+			}
 
-		function render( start, count ) {
+		},
 
-			gl.drawArrays( mode, start, count );
+		mergeMesh: function ( mesh ) {
 
-			info.update( count, mode );
+			if ( ! ( mesh && mesh.isMesh ) ) {
 
-		}
+				console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
+				return;
 
-		function renderInstances( geometry, start, count, primcount ) {
+			}
 
-			if ( primcount === 0 ) { return; }
+			if ( mesh.matrixAutoUpdate ) { mesh.updateMatrix(); }
 
-			var extension, methodName;
+			this.merge( mesh.geometry, mesh.matrix );
 
-			if ( isWebGL2 ) {
+		},
 
-				extension = gl;
-				methodName = 'drawArraysInstanced';
+		/*
+		 * Checks for duplicate vertices with hashmap.
+		 * Duplicated vertices are removed
+		 * and faces' vertices are updated.
+		 */
 
-			} else {
+		mergeVertices: function () {
 
-				extension = extensions.get( 'ANGLE_instanced_arrays' );
-				methodName = 'drawArraysInstancedANGLE';
+			var verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
+			var unique = [], changes = [];
 
-				if ( extension === null ) {
+			var v, key;
+			var precisionPoints = 4; // number of decimal points, e.g. 4 for epsilon of 0.0001
+			var precision = Math.pow( 10, precisionPoints );
+			var i, il, face;
+			var indices, j, jl;
 
-					console.error( 'THREE.WebGLBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
-					return;
+			for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
 
-				}
+				v = this.vertices[ i ];
+				key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
 
-			}
+				if ( verticesMap[ key ] === undefined ) {
 
-			extension[ methodName ]( mode, start, count, primcount );
+					verticesMap[ key ] = i;
+					unique.push( this.vertices[ i ] );
+					changes[ i ] = unique.length - 1;
 
-			info.update( count, mode, primcount );
+				} else {
 
-		}
+					//console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
+					changes[ i ] = changes[ verticesMap[ key ] ];
 
-		//
+				}
 
-		this.setMode = setMode;
-		this.render = render;
-		this.renderInstances = renderInstances;
+			}
 
-	}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			// if faces are completely degenerate after merging vertices, we
+			// have to remove them from the geometry.
+			var faceIndicesToRemove = [];
 
-	function WebGLCapabilities( gl, extensions, parameters ) {
+			for ( i = 0, il = this.faces.length; i < il; i ++ ) {
 
-		var maxAnisotropy;
+				face = this.faces[ i ];
 
-		function getMaxAnisotropy() {
+				face.a = changes[ face.a ];
+				face.b = changes[ face.b ];
+				face.c = changes[ face.c ];
 
-			if ( maxAnisotropy !== undefined ) { return maxAnisotropy; }
+				indices = [ face.a, face.b, face.c ];
 
-			var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
+				// if any duplicate vertices are found in a Face3
+				// we have to remove the face as nothing can be saved
+				for ( var n = 0; n < 3; n ++ ) {
 
-			if ( extension !== null ) {
+					if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
 
-				maxAnisotropy = gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT );
+						faceIndicesToRemove.push( i );
+						break;
 
-			} else {
+					}
 
-				maxAnisotropy = 0;
+				}
 
 			}
 
-			return maxAnisotropy;
-
-		}
+			for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
 
-		function getMaxPrecision( precision ) {
+				var idx = faceIndicesToRemove[ i ];
 
-			if ( precision === 'highp' ) {
+				this.faces.splice( idx, 1 );
 
-				if ( gl.getShaderPrecisionFormat( 35633, 36338 ).precision > 0 &&
-					gl.getShaderPrecisionFormat( 35632, 36338 ).precision > 0 ) {
+				for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
 
-					return 'highp';
+					this.faceVertexUvs[ j ].splice( idx, 1 );
 
 				}
 
-				precision = 'mediump';
-
 			}
 
-			if ( precision === 'mediump' ) {
+			// Use unique set of vertices
 
-				if ( gl.getShaderPrecisionFormat( 35633, 36337 ).precision > 0 &&
-					gl.getShaderPrecisionFormat( 35632, 36337 ).precision > 0 ) {
+			var diff = this.vertices.length - unique.length;
+			this.vertices = unique;
+			return diff;
 
-					return 'mediump';
+		},
 
-				}
+		setFromPoints: function ( points ) {
 
-			}
+			this.vertices = [];
 
-			return 'lowp';
+			for ( var i = 0, l = points.length; i < l; i ++ ) {
 
-		}
+				var point = points[ i ];
+				this.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
 
-		/* eslint-disable no-undef */
-		var isWebGL2 = ( typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext ) ||
-			( typeof WebGL2ComputeRenderingContext !== 'undefined' && gl instanceof WebGL2ComputeRenderingContext );
-		/* eslint-enable no-undef */
+			}
 
-		var precision = parameters.precision !== undefined ? parameters.precision : 'highp';
-		var maxPrecision = getMaxPrecision( precision );
+			return this;
 
-		if ( maxPrecision !== precision ) {
+		},
 
-			console.warn( 'THREE.WebGLRenderer:', precision, 'not supported, using', maxPrecision, 'instead.' );
-			precision = maxPrecision;
+		sortFacesByMaterialIndex: function () {
 
-		}
+			var faces = this.faces;
+			var length = faces.length;
 
-		var logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true;
+			// tag faces
 
-		var maxTextures = gl.getParameter( 34930 );
-		var maxVertexTextures = gl.getParameter( 35660 );
-		var maxTextureSize = gl.getParameter( 3379 );
-		var maxCubemapSize = gl.getParameter( 34076 );
+			for ( var i = 0; i < length; i ++ ) {
 
-		var maxAttributes = gl.getParameter( 34921 );
-		var maxVertexUniforms = gl.getParameter( 36347 );
-		var maxVaryings = gl.getParameter( 36348 );
-		var maxFragmentUniforms = gl.getParameter( 36349 );
+				faces[ i ]._id = i;
 
-		var vertexTextures = maxVertexTextures > 0;
-		var floatFragmentTextures = isWebGL2 || !! extensions.get( 'OES_texture_float' );
-		var floatVertexTextures = vertexTextures && floatFragmentTextures;
+			}
 
-		var maxSamples = isWebGL2 ? gl.getParameter( 36183 ) : 0;
+			// sort faces
 
-		return {
+			function materialIndexSort( a, b ) {
 
-			isWebGL2: isWebGL2,
+				return a.materialIndex - b.materialIndex;
 
-			getMaxAnisotropy: getMaxAnisotropy,
-			getMaxPrecision: getMaxPrecision,
+			}
 
-			precision: precision,
-			logarithmicDepthBuffer: logarithmicDepthBuffer,
+			faces.sort( materialIndexSort );
 
-			maxTextures: maxTextures,
-			maxVertexTextures: maxVertexTextures,
-			maxTextureSize: maxTextureSize,
-			maxCubemapSize: maxCubemapSize,
+			// sort uvs
 
-			maxAttributes: maxAttributes,
-			maxVertexUniforms: maxVertexUniforms,
-			maxVaryings: maxVaryings,
-			maxFragmentUniforms: maxFragmentUniforms,
+			var uvs1 = this.faceVertexUvs[ 0 ];
+			var uvs2 = this.faceVertexUvs[ 1 ];
 
-			vertexTextures: vertexTextures,
-			floatFragmentTextures: floatFragmentTextures,
-			floatVertexTextures: floatVertexTextures,
+			var newUvs1, newUvs2;
 
-			maxSamples: maxSamples
+			if ( uvs1 && uvs1.length === length ) { newUvs1 = []; }
+			if ( uvs2 && uvs2.length === length ) { newUvs2 = []; }
 
-		};
+			for ( var i = 0; i < length; i ++ ) {
 
-	}
+				var id = faces[ i ]._id;
 
-	/**
-	 * @author tschw
-	 */
+				if ( newUvs1 ) { newUvs1.push( uvs1[ id ] ); }
+				if ( newUvs2 ) { newUvs2.push( uvs2[ id ] ); }
 
-	function WebGLClipping() {
+			}
 
-		var scope = this,
+			if ( newUvs1 ) { this.faceVertexUvs[ 0 ] = newUvs1; }
+			if ( newUvs2 ) { this.faceVertexUvs[ 1 ] = newUvs2; }
 
-			globalState = null,
-			numGlobalPlanes = 0,
-			localClippingEnabled = false,
-			renderingShadows = false,
+		},
 
-			plane = new Plane(),
-			viewNormalMatrix = new Matrix3(),
+		toJSON: function () {
 
-			uniform = { value: null, needsUpdate: false };
+			var data = {
+				metadata: {
+					version: 4.5,
+					type: 'Geometry',
+					generator: 'Geometry.toJSON'
+				}
+			};
 
-		this.uniform = uniform;
-		this.numPlanes = 0;
-		this.numIntersection = 0;
+			// standard Geometry serialization
 
-		this.init = function ( planes, enableLocalClipping, camera ) {
+			data.uuid = this.uuid;
+			data.type = this.type;
+			if ( this.name !== '' ) { data.name = this.name; }
 
-			var enabled =
-				planes.length !== 0 ||
-				enableLocalClipping ||
-				// enable state of previous frame - the clipping code has to
-				// run another frame in order to reset the state:
-				numGlobalPlanes !== 0 ||
-				localClippingEnabled;
+			if ( this.parameters !== undefined ) {
 
-			localClippingEnabled = enableLocalClipping;
+				var parameters = this.parameters;
 
-			globalState = projectPlanes( planes, camera, 0 );
-			numGlobalPlanes = planes.length;
+				for ( var key in parameters ) {
 
-			return enabled;
+					if ( parameters[ key ] !== undefined ) { data[ key ] = parameters[ key ]; }
 
-		};
+				}
 
-		this.beginShadows = function () {
+				return data;
 
-			renderingShadows = true;
-			projectPlanes( null );
+			}
 
-		};
+			var vertices = [];
 
-		this.endShadows = function () {
+			for ( var i = 0; i < this.vertices.length; i ++ ) {
 
-			renderingShadows = false;
-			resetGlobalState();
+				var vertex = this.vertices[ i ];
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-		};
+			}
 
-		this.setState = function ( planes, clipIntersection, clipShadows, camera, cache, fromCache ) {
+			var faces = [];
+			var normals = [];
+			var normalsHash = {};
+			var colors = [];
+			var colorsHash = {};
+			var uvs = [];
+			var uvsHash = {};
 
-			if ( ! localClippingEnabled || planes === null || planes.length === 0 || renderingShadows && ! clipShadows ) {
+			for ( var i = 0; i < this.faces.length; i ++ ) {
 
-				// there's no local clipping
+				var face = this.faces[ i ];
 
-				if ( renderingShadows ) {
+				var hasMaterial = true;
+				var hasFaceUv = false; // deprecated
+				var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
+				var hasFaceNormal = face.normal.length() > 0;
+				var hasFaceVertexNormal = face.vertexNormals.length > 0;
+				var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
+				var hasFaceVertexColor = face.vertexColors.length > 0;
 
-					// there's no global clipping
+				var faceType = 0;
 
-					projectPlanes( null );
+				faceType = setBit( faceType, 0, 0 ); // isQuad
+				faceType = setBit( faceType, 1, hasMaterial );
+				faceType = setBit( faceType, 2, hasFaceUv );
+				faceType = setBit( faceType, 3, hasFaceVertexUv );
+				faceType = setBit( faceType, 4, hasFaceNormal );
+				faceType = setBit( faceType, 5, hasFaceVertexNormal );
+				faceType = setBit( faceType, 6, hasFaceColor );
+				faceType = setBit( faceType, 7, hasFaceVertexColor );
 
-				} else {
+				faces.push( faceType );
+				faces.push( face.a, face.b, face.c );
+				faces.push( face.materialIndex );
 
-					resetGlobalState();
+				if ( hasFaceVertexUv ) {
 
-				}
+					var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
 
-			} else {
+					faces.push(
+						getUvIndex( faceVertexUvs[ 0 ] ),
+						getUvIndex( faceVertexUvs[ 1 ] ),
+						getUvIndex( faceVertexUvs[ 2 ] )
+					);
 
-				var nGlobal = renderingShadows ? 0 : numGlobalPlanes,
-					lGlobal = nGlobal * 4,
+				}
 
-					dstArray = cache.clippingState || null;
+				if ( hasFaceNormal ) {
 
-				uniform.value = dstArray; // ensure unique state
+					faces.push( getNormalIndex( face.normal ) );
 
-				dstArray = projectPlanes( planes, camera, lGlobal, fromCache );
+				}
 
-				for ( var i = 0; i !== lGlobal; ++ i ) {
+				if ( hasFaceVertexNormal ) {
 
-					dstArray[ i ] = globalState[ i ];
+					var vertexNormals = face.vertexNormals;
+
+					faces.push(
+						getNormalIndex( vertexNormals[ 0 ] ),
+						getNormalIndex( vertexNormals[ 1 ] ),
+						getNormalIndex( vertexNormals[ 2 ] )
+					);
 
 				}
 
-				cache.clippingState = dstArray;
-				this.numIntersection = clipIntersection ? this.numPlanes : 0;
-				this.numPlanes += nGlobal;
+				if ( hasFaceColor ) {
 
-			}
+					faces.push( getColorIndex( face.color ) );
 
+				}
 
-		};
+				if ( hasFaceVertexColor ) {
 
-		function resetGlobalState() {
+					var vertexColors = face.vertexColors;
 
-			if ( uniform.value !== globalState ) {
+					faces.push(
+						getColorIndex( vertexColors[ 0 ] ),
+						getColorIndex( vertexColors[ 1 ] ),
+						getColorIndex( vertexColors[ 2 ] )
+					);
 
-				uniform.value = globalState;
-				uniform.needsUpdate = numGlobalPlanes > 0;
+				}
 
 			}
 
-			scope.numPlanes = numGlobalPlanes;
-			scope.numIntersection = 0;
-
-		}
+			function setBit( value, position, enabled ) {
 
-		function projectPlanes( planes, camera, dstOffset, skipTransform ) {
+				return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
 
-			var nPlanes = planes !== null ? planes.length : 0,
-				dstArray = null;
+			}
 
-			if ( nPlanes !== 0 ) {
+			function getNormalIndex( normal ) {
 
-				dstArray = uniform.value;
+				var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
 
-				if ( skipTransform !== true || dstArray === null ) {
+				if ( normalsHash[ hash ] !== undefined ) {
 
-					var flatSize = dstOffset + nPlanes * 4,
-						viewMatrix = camera.matrixWorldInverse;
+					return normalsHash[ hash ];
 
-					viewNormalMatrix.getNormalMatrix( viewMatrix );
+				}
 
-					if ( dstArray === null || dstArray.length < flatSize ) {
+				normalsHash[ hash ] = normals.length / 3;
+				normals.push( normal.x, normal.y, normal.z );
 
-						dstArray = new Float32Array( flatSize );
+				return normalsHash[ hash ];
 
-					}
+			}
 
-					for ( var i = 0, i4 = dstOffset; i !== nPlanes; ++ i, i4 += 4 ) {
+			function getColorIndex( color ) {
 
-						plane.copy( planes[ i ] ).applyMatrix4( viewMatrix, viewNormalMatrix );
+				var hash = color.r.toString() + color.g.toString() + color.b.toString();
 
-						plane.normal.toArray( dstArray, i4 );
-						dstArray[ i4 + 3 ] = plane.constant;
+				if ( colorsHash[ hash ] !== undefined ) {
 
-					}
+					return colorsHash[ hash ];
 
 				}
 
-				uniform.value = dstArray;
-				uniform.needsUpdate = true;
+				colorsHash[ hash ] = colors.length;
+				colors.push( color.getHex() );
+
+				return colorsHash[ hash ];
 
 			}
 
-			scope.numPlanes = nPlanes;
-			scope.numIntersection = 0;
+			function getUvIndex( uv ) {
 
-			return dstArray;
+				var hash = uv.x.toString() + uv.y.toString();
 
-		}
+				if ( uvsHash[ hash ] !== undefined ) {
 
-	}
+					return uvsHash[ hash ];
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				}
 
-	function WebGLExtensions( gl ) {
+				uvsHash[ hash ] = uvs.length / 2;
+				uvs.push( uv.x, uv.y );
 
-		var extensions = {};
+				return uvsHash[ hash ];
 
-		return {
+			}
 
-			get: function ( name ) {
+			data.data = {};
 
-				if ( extensions[ name ] !== undefined ) {
+			data.data.vertices = vertices;
+			data.data.normals = normals;
+			if ( colors.length > 0 ) { data.data.colors = colors; }
+			if ( uvs.length > 0 ) { data.data.uvs = [ uvs ]; } // temporal backward compatibility
+			data.data.faces = faces;
 
-					return extensions[ name ];
+			return data;
 
-				}
+		},
 
-				var extension;
+		clone: function () {
 
-				switch ( name ) {
+			/*
+			 // Handle primitives
 
-					case 'WEBGL_depth_texture':
-						extension = gl.getExtension( 'WEBGL_depth_texture' ) || gl.getExtension( 'MOZ_WEBGL_depth_texture' ) || gl.getExtension( 'WEBKIT_WEBGL_depth_texture' );
-						break;
+			 var parameters = this.parameters;
 
-					case 'EXT_texture_filter_anisotropic':
-						extension = gl.getExtension( 'EXT_texture_filter_anisotropic' ) || gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
-						break;
+			 if ( parameters !== undefined ) {
 
-					case 'WEBGL_compressed_texture_s3tc':
-						extension = gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
-						break;
+			 var values = [];
 
-					case 'WEBGL_compressed_texture_pvrtc':
-						extension = gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
-						break;
+			 for ( var key in parameters ) {
 
-					default:
-						extension = gl.getExtension( name );
+			 values.push( parameters[ key ] );
 
-				}
+			 }
 
-				if ( extension === null ) {
+			 var geometry = Object.create( this.constructor.prototype );
+			 this.constructor.apply( geometry, values );
+			 return geometry;
 
-					console.warn( 'THREE.WebGLRenderer: ' + name + ' extension not supported.' );
+			 }
 
-				}
+			 return new this.constructor().copy( this );
+			 */
 
-				extensions[ name ] = extension;
+			return new Geometry().copy( this );
 
-				return extension;
+		},
 
-			}
+		copy: function ( source ) {
 
-		};
+			var i, il, j, jl, k, kl;
 
-	}
+			// reset
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			this.vertices = [];
+			this.colors = [];
+			this.faces = [];
+			this.faceVertexUvs = [[]];
+			this.morphTargets = [];
+			this.morphNormals = [];
+			this.skinWeights = [];
+			this.skinIndices = [];
+			this.lineDistances = [];
+			this.boundingBox = null;
+			this.boundingSphere = null;
 
-	function WebGLGeometries( gl, attributes, info ) {
+			// name
 
-		var geometries = new WeakMap();
-		var wireframeAttributes = new WeakMap();
+			this.name = source.name;
 
-		function onGeometryDispose( event ) {
+			// vertices
 
-			var geometry = event.target;
-			var buffergeometry = geometries.get( geometry );
+			var vertices = source.vertices;
 
-			if ( buffergeometry.index !== null ) {
+			for ( i = 0, il = vertices.length; i < il; i ++ ) {
 
-				attributes.remove( buffergeometry.index );
+				this.vertices.push( vertices[ i ].clone() );
 
 			}
 
-			for ( var name in buffergeometry.attributes ) {
+			// colors
 
-				attributes.remove( buffergeometry.attributes[ name ] );
+			var colors = source.colors;
 
-			}
+			for ( i = 0, il = colors.length; i < il; i ++ ) {
 
-			geometry.removeEventListener( 'dispose', onGeometryDispose );
+				this.colors.push( colors[ i ].clone() );
 
-			geometries.delete( geometry );
+			}
 
-			var attribute = wireframeAttributes.get( buffergeometry );
+			// faces
 
-			if ( attribute ) {
+			var faces = source.faces;
 
-				attributes.remove( attribute );
-				wireframeAttributes.delete( buffergeometry );
+			for ( i = 0, il = faces.length; i < il; i ++ ) {
+
+				this.faces.push( faces[ i ].clone() );
 
 			}
 
-			//
+			// face vertex uvs
 
-			info.memory.geometries --;
+			for ( i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
 
-		}
+				var faceVertexUvs = source.faceVertexUvs[ i ];
 
-		function get( object, geometry ) {
+				if ( this.faceVertexUvs[ i ] === undefined ) {
 
-			var buffergeometry = geometries.get( geometry );
+					this.faceVertexUvs[ i ] = [];
 
-			if ( buffergeometry ) { return buffergeometry; }
+				}
 
-			geometry.addEventListener( 'dispose', onGeometryDispose );
+				for ( j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
 
-			if ( geometry.isBufferGeometry ) {
+					var uvs = faceVertexUvs[ j ], uvsCopy = [];
 
-				buffergeometry = geometry;
+					for ( k = 0, kl = uvs.length; k < kl; k ++ ) {
 
-			} else if ( geometry.isGeometry ) {
+						var uv = uvs[ k ];
 
-				if ( geometry._bufferGeometry === undefined ) {
+						uvsCopy.push( uv.clone() );
 
-					geometry._bufferGeometry = new BufferGeometry().setFromObject( object );
+					}
 
-				}
+					this.faceVertexUvs[ i ].push( uvsCopy );
 
-				buffergeometry = geometry._bufferGeometry;
+				}
 
 			}
 
-			geometries.set( geometry, buffergeometry );
-
-			info.memory.geometries ++;
+			// morph targets
 
-			return buffergeometry;
+			var morphTargets = source.morphTargets;
 
-		}
+			for ( i = 0, il = morphTargets.length; i < il; i ++ ) {
 
-		function update( geometry ) {
+				var morphTarget = {};
+				morphTarget.name = morphTargets[ i ].name;
 
-			var index = geometry.index;
-			var geometryAttributes = geometry.attributes;
+				// vertices
 
-			if ( index !== null ) {
+				if ( morphTargets[ i ].vertices !== undefined ) {
 
-				attributes.update( index, 34963 );
+					morphTarget.vertices = [];
 
-			}
+					for ( j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) {
 
-			for ( var name in geometryAttributes ) {
+						morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() );
 
-				attributes.update( geometryAttributes[ name ], 34962 );
+					}
 
-			}
+				}
 
-			// morph targets
+				// normals
 
-			var morphAttributes = geometry.morphAttributes;
+				if ( morphTargets[ i ].normals !== undefined ) {
 
-			for ( var name in morphAttributes ) {
+					morphTarget.normals = [];
 
-				var array = morphAttributes[ name ];
+					for ( j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) {
 
-				for ( var i = 0, l = array.length; i < l; i ++ ) {
+						morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() );
 
-					attributes.update( array[ i ], 34962 );
+					}
 
 				}
 
+				this.morphTargets.push( morphTarget );
+
 			}
 
-		}
+			// morph normals
 
-		function updateWireframeAttribute( geometry ) {
+			var morphNormals = source.morphNormals;
 
-			var indices = [];
+			for ( i = 0, il = morphNormals.length; i < il; i ++ ) {
 
-			var geometryIndex = geometry.index;
-			var geometryPosition = geometry.attributes.position;
-			var version = 0;
+				var morphNormal = {};
 
-			if ( geometryIndex !== null ) {
+				// vertex normals
 
-				var array = geometryIndex.array;
-				version = geometryIndex.version;
+				if ( morphNormals[ i ].vertexNormals !== undefined ) {
 
-				for ( var i = 0, l = array.length; i < l; i += 3 ) {
+					morphNormal.vertexNormals = [];
 
-					var a = array[ i + 0 ];
-					var b = array[ i + 1 ];
-					var c = array[ i + 2 ];
+					for ( j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) {
 
-					indices.push( a, b, b, c, c, a );
+						var srcVertexNormal = morphNormals[ i ].vertexNormals[ j ];
+						var destVertexNormal = {};
+
+						destVertexNormal.a = srcVertexNormal.a.clone();
+						destVertexNormal.b = srcVertexNormal.b.clone();
+						destVertexNormal.c = srcVertexNormal.c.clone();
+
+						morphNormal.vertexNormals.push( destVertexNormal );
+
+					}
 
 				}
 
-			} else {
+				// face normals
 
-				var array = geometryPosition.array;
-				version = geometryPosition.version;
+				if ( morphNormals[ i ].faceNormals !== undefined ) {
 
-				for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
+					morphNormal.faceNormals = [];
 
-					var a = i + 0;
-					var b = i + 1;
-					var c = i + 2;
+					for ( j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) {
 
-					indices.push( a, b, b, c, c, a );
+						morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() );
+
+					}
 
 				}
 
+				this.morphNormals.push( morphNormal );
+
 			}
 
-			var attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
-			attribute.version = version;
+			// skin weights
 
-			attributes.update( attribute, 34963 );
+			var skinWeights = source.skinWeights;
 
-			//
+			for ( i = 0, il = skinWeights.length; i < il; i ++ ) {
 
-			var previousAttribute = wireframeAttributes.get( geometry );
+				this.skinWeights.push( skinWeights[ i ].clone() );
 
-			if ( previousAttribute ) { attributes.remove( previousAttribute ); }
+			}
 
-			//
+			// skin indices
 
-			wireframeAttributes.set( geometry, attribute );
+			var skinIndices = source.skinIndices;
 
-		}
+			for ( i = 0, il = skinIndices.length; i < il; i ++ ) {
 
-		function getWireframeAttribute( geometry ) {
+				this.skinIndices.push( skinIndices[ i ].clone() );
 
-			var currentAttribute = wireframeAttributes.get( geometry );
+			}
 
-			if ( currentAttribute ) {
+			// line distances
 
-				var geometryIndex = geometry.index;
+			var lineDistances = source.lineDistances;
 
-				if ( geometryIndex !== null ) {
+			for ( i = 0, il = lineDistances.length; i < il; i ++ ) {
 
-					// if the attribute is obsolete, create a new one
+				this.lineDistances.push( lineDistances[ i ] );
 
-					if ( currentAttribute.version < geometryIndex.version ) {
+			}
 
-						updateWireframeAttribute( geometry );
+			// bounding box
 
-					}
+			var boundingBox = source.boundingBox;
 
-				}
+			if ( boundingBox !== null ) {
 
-			} else {
+				this.boundingBox = boundingBox.clone();
 
-				updateWireframeAttribute( geometry );
+			}
+
+			// bounding sphere
+
+			var boundingSphere = source.boundingSphere;
+
+			if ( boundingSphere !== null ) {
+
+				this.boundingSphere = boundingSphere.clone();
 
 			}
 
-			return wireframeAttributes.get( geometry );
+			// update flags
 
-		}
+			this.elementsNeedUpdate = source.elementsNeedUpdate;
+			this.verticesNeedUpdate = source.verticesNeedUpdate;
+			this.uvsNeedUpdate = source.uvsNeedUpdate;
+			this.normalsNeedUpdate = source.normalsNeedUpdate;
+			this.colorsNeedUpdate = source.colorsNeedUpdate;
+			this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate;
+			this.groupsNeedUpdate = source.groupsNeedUpdate;
 
-		return {
+			return this;
 
-			get: get,
-			update: update,
+		},
 
-			getWireframeAttribute: getWireframeAttribute
+		dispose: function () {
 
-		};
+			this.dispatchEvent( { type: 'dispose' } );
 
-	}
+		}
+
+	} );
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / https://github.com/Mugen87
 	 */
 
-	function WebGLIndexedBufferRenderer( gl, extensions, info, capabilities ) {
+	// BoxGeometry
 
-		var isWebGL2 = capabilities.isWebGL2;
+	var BoxGeometry = /*@__PURE__*/(function (Geometry) {
+		function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
 
-		var mode;
+			Geometry.call(this);
 
-		function setMode( value ) {
+			this.type = 'BoxGeometry';
 
-			mode = value;
+			this.parameters = {
+				width: width,
+				height: height,
+				depth: depth,
+				widthSegments: widthSegments,
+				heightSegments: heightSegments,
+				depthSegments: depthSegments
+			};
+
+			this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) );
+			this.mergeVertices();
 
 		}
 
-		var type, bytesPerElement;
+		if ( Geometry ) BoxGeometry.__proto__ = Geometry;
+		BoxGeometry.prototype = Object.create( Geometry && Geometry.prototype );
+		BoxGeometry.prototype.constructor = BoxGeometry;
 
-		function setIndex( value ) {
+		return BoxGeometry;
+	}(Geometry));
 
-			type = value.type;
-			bytesPerElement = value.bytesPerElement;
+	// BoxBufferGeometry
 
-		}
+	var BoxBufferGeometry = /*@__PURE__*/(function (BufferGeometry) {
+		function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) {
 
-		function render( start, count ) {
+			BufferGeometry.call(this);
 
-			gl.drawElements( mode, count, type, start * bytesPerElement );
+			this.type = 'BoxBufferGeometry';
 
-			info.update( count, mode );
+			this.parameters = {
+				width: width,
+				height: height,
+				depth: depth,
+				widthSegments: widthSegments,
+				heightSegments: heightSegments,
+				depthSegments: depthSegments
+			};
 
-		}
+			var scope = this;
 
-		function renderInstances( geometry, start, count, primcount ) {
+			width = width || 1;
+			height = height || 1;
+			depth = depth || 1;
 
-			if ( primcount === 0 ) { return; }
+			// segments
 
-			var extension, methodName;
+			widthSegments = Math.floor( widthSegments ) || 1;
+			heightSegments = Math.floor( heightSegments ) || 1;
+			depthSegments = Math.floor( depthSegments ) || 1;
 
-			if ( isWebGL2 ) {
+			// buffers
 
-				extension = gl;
-				methodName = 'drawElementsInstanced';
+			var indices = [];
+			var vertices = [];
+			var normals = [];
+			var uvs = [];
 
-			} else {
+			// helper variables
 
-				extension = extensions.get( 'ANGLE_instanced_arrays' );
-				methodName = 'drawElementsInstancedANGLE';
+			var numberOfVertices = 0;
+			var groupStart = 0;
 
-				if ( extension === null ) {
+			// build each side of the box geometry
 
-					console.error( 'THREE.WebGLIndexedBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
-					return;
+			buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
+			buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
+			buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
+			buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
+			buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
+			buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
 
-				}
+			// build geometry
 
-			}
+			this.setIndex( indices );
+			this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+			this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+			this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-			extension[ methodName ]( mode, count, type, start * bytesPerElement, primcount );
+			function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
 
-			info.update( count, mode, primcount );
-
-		}
-
-		//
+				var segmentWidth = width / gridX;
+				var segmentHeight = height / gridY;
 
-		this.setMode = setMode;
-		this.setIndex = setIndex;
-		this.render = render;
-		this.renderInstances = renderInstances;
+				var widthHalf = width / 2;
+				var heightHalf = height / 2;
+				var depthHalf = depth / 2;
 
-	}
+				var gridX1 = gridX + 1;
+				var gridY1 = gridY + 1;
 
-	/**
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+				var vertexCounter = 0;
+				var groupCount = 0;
 
-	function WebGLInfo( gl ) {
+				var ix, iy;
 
-		var memory = {
-			geometries: 0,
-			textures: 0
-		};
+				var vector = new Vector3();
 
-		var render = {
-			frame: 0,
-			calls: 0,
-			triangles: 0,
-			points: 0,
-			lines: 0
-		};
+				// generate vertices, normals and uvs
 
-		function update( count, mode, instanceCount ) {
+				for ( iy = 0; iy < gridY1; iy ++ ) {
 
-			instanceCount = instanceCount || 1;
+					var y = iy * segmentHeight - heightHalf;
 
-			render.calls ++;
+					for ( ix = 0; ix < gridX1; ix ++ ) {
 
-			switch ( mode ) {
+						var x = ix * segmentWidth - widthHalf;
 
-				case 4:
-					render.triangles += instanceCount * ( count / 3 );
-					break;
+						// set values to correct vector component
 
-				case 1:
-					render.lines += instanceCount * ( count / 2 );
-					break;
+						vector[ u ] = x * udir;
+						vector[ v ] = y * vdir;
+						vector[ w ] = depthHalf;
 
-				case 3:
-					render.lines += instanceCount * ( count - 1 );
-					break;
+						// now apply vector to vertex buffer
 
-				case 2:
-					render.lines += instanceCount * count;
-					break;
+						vertices.push( vector.x, vector.y, vector.z );
 
-				case 0:
-					render.points += instanceCount * count;
-					break;
+						// set values to correct vector component
 
-				default:
-					console.error( 'THREE.WebGLInfo: Unknown draw mode:', mode );
-					break;
+						vector[ u ] = 0;
+						vector[ v ] = 0;
+						vector[ w ] = depth > 0 ? 1 : - 1;
 
-			}
+						// now apply vector to normal buffer
 
-		}
+						normals.push( vector.x, vector.y, vector.z );
 
-		function reset() {
+						// uvs
 
-			render.frame ++;
-			render.calls = 0;
-			render.triangles = 0;
-			render.points = 0;
-			render.lines = 0;
+						uvs.push( ix / gridX );
+						uvs.push( 1 - ( iy / gridY ) );
 
-		}
+						// counters
 
-		return {
-			memory: memory,
-			render: render,
-			programs: null,
-			autoReset: true,
-			reset: reset,
-			update: update
-		};
+						vertexCounter += 1;
 
-	}
+					}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				}
 
-	function absNumericalSort( a, b ) {
+				// indices
 
-		return Math.abs( b[ 1 ] ) - Math.abs( a[ 1 ] );
+				// 1. you need three indices to draw a single face
+				// 2. a single segment consists of two faces
+				// 3. so we need to generate six (2*3) indices per segment
 
-	}
+				for ( iy = 0; iy < gridY; iy ++ ) {
 
-	function WebGLMorphtargets( gl ) {
+					for ( ix = 0; ix < gridX; ix ++ ) {
 
-		var influencesList = {};
-		var morphInfluences = new Float32Array( 8 );
+						var a = numberOfVertices + ix + gridX1 * iy;
+						var b = numberOfVertices + ix + gridX1 * ( iy + 1 );
+						var c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
+						var d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
 
-		function update( object, geometry, material, program ) {
+						// faces
 
-			var objectInfluences = object.morphTargetInfluences;
+						indices.push( a, b, d );
+						indices.push( b, c, d );
 
-			// When object doesn't have morph target influences defined, we treat it as a 0-length array
-			// This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
+						// increase counter
 
-			var length = objectInfluences === undefined ? 0 : objectInfluences.length;
+						groupCount += 6;
 
-			var influences = influencesList[ geometry.id ];
+					}
 
-			if ( influences === undefined ) {
+				}
 
-				// initialise list
+				// add a group to the geometry. this will ensure multi material support
 
-				influences = [];
+				scope.addGroup( groupStart, groupCount, materialIndex );
 
-				for ( var i = 0; i < length; i ++ ) {
+				// calculate new start value for groups
 
-					influences[ i ] = [ i, 0 ];
+				groupStart += groupCount;
 
-				}
+				// update total number of vertices
 
-				influencesList[ geometry.id ] = influences;
+				numberOfVertices += vertexCounter;
 
 			}
 
-			var morphTargets = material.morphTargets && geometry.morphAttributes.position;
-			var morphNormals = material.morphNormals && geometry.morphAttributes.normal;
+		}
 
-			// Remove current morphAttributes
+		if ( BufferGeometry ) BoxBufferGeometry.__proto__ = BufferGeometry;
+		BoxBufferGeometry.prototype = Object.create( BufferGeometry && BufferGeometry.prototype );
+		BoxBufferGeometry.prototype.constructor = BoxBufferGeometry;
 
-			for ( var i = 0; i < length; i ++ ) {
+		return BoxBufferGeometry;
+	}(BufferGeometry));
 
-				var influence = influences[ i ];
+	/**
+	 * Uniform Utilities
+	 */
 
-				if ( influence[ 1 ] !== 0 ) {
+	function cloneUniforms( src ) {
 
-					if ( morphTargets ) { geometry.deleteAttribute( 'morphTarget' + i ); }
-					if ( morphNormals ) { geometry.deleteAttribute( 'morphNormal' + i ); }
+		var dst = {};
 
-				}
+		for ( var u in src ) {
 
-			}
+			dst[ u ] = {};
 
-			// Collect influences
+			for ( var p in src[ u ] ) {
 
-			for ( var i = 0; i < length; i ++ ) {
+				var property = src[ u ][ p ];
 
-				var influence = influences[ i ];
+				if ( property && ( property.isColor ||
+					property.isMatrix3 || property.isMatrix4 ||
+					property.isVector2 || property.isVector3 || property.isVector4 ||
+					property.isTexture ) ) {
 
-				influence[ 0 ] = i;
-				influence[ 1 ] = objectInfluences[ i ];
+					dst[ u ][ p ] = property.clone();
 
-			}
+				} else if ( Array.isArray( property ) ) {
 
-			influences.sort( absNumericalSort );
+					dst[ u ][ p ] = property.slice();
 
-			// Add morphAttributes
+				} else {
 
-			var morphInfluencesSum = 0;
+					dst[ u ][ p ] = property;
 
-			for ( var i = 0; i < 8; i ++ ) {
+				}
 
-				var influence = influences[ i ];
+			}
 
-				if ( influence ) {
+		}
 
-					var index = influence[ 0 ];
-					var value = influence[ 1 ];
+		return dst;
 
-					if ( value ) {
+	}
 
-						if ( morphTargets ) { geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] ); }
-						if ( morphNormals ) { geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] ); }
+	function mergeUniforms( uniforms ) {
 
-						morphInfluences[ i ] = value;
-						morphInfluencesSum += value;
-						continue;
+		var merged = {};
 
-					}
+		for ( var u = 0; u < uniforms.length; u ++ ) {
 
-				}
+			var tmp = cloneUniforms( uniforms[ u ] );
 
-				morphInfluences[ i ] = 0;
+			for ( var p in tmp ) {
+
+				merged[ p ] = tmp[ p ];
 
 			}
 
-			// GLSL shader uses formula baseinfluence * base + sum(target * influence)
-			// This allows us to switch between absolute morphs and relative morphs without changing shader code
-			// When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
-			var morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
+		}
 
-			program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
-			program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
+		return merged;
 
-		}
+	}
 
-		return {
+	// Legacy
 
-			update: update
+	var UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms };
 
-		};
+	var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}";
 
-	}
+	var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}";
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  defines: { "label" : "value" },
+	 *  uniforms: { "parameter1": { value: 1.0 }, "parameter2": { value2: 2 } },
+	 *
+	 *  fragmentShader: <string>,
+	 *  vertexShader: <string>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  lights: <bool>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
 	 */
 
-	function WebGLObjects( gl, geometries, attributes, info ) {
+	function ShaderMaterial( parameters ) {
 
-		var updateMap = new WeakMap();
+		Material.call( this );
 
-		function update( object ) {
+		this.type = 'ShaderMaterial';
 
-			var frame = info.render.frame;
+		this.defines = {};
+		this.uniforms = {};
 
-			var geometry = object.geometry;
-			var buffergeometry = geometries.get( object, geometry );
+		this.vertexShader = default_vertex;
+		this.fragmentShader = default_fragment;
 
-			// Update once per frame
+		this.linewidth = 1;
 
-			if ( updateMap.get( buffergeometry ) !== frame ) {
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
 
-				if ( geometry.isGeometry ) {
+		this.fog = false; // set to use scene fog
+		this.lights = false; // set to use scene lights
+		this.clipping = false; // set to use user-defined clipping planes
 
-					buffergeometry.updateFromObject( object );
+		this.skinning = false; // set to use skinning attribute streams
+		this.morphTargets = false; // set to use morph targets
+		this.morphNormals = false; // set to use morph normals
 
-				}
+		this.extensions = {
+			derivatives: false, // set to use derivatives
+			fragDepth: false, // set to use fragment depth values
+			drawBuffers: false, // set to use draw buffers
+			shaderTextureLOD: false // set to use shader texture LOD
+		};
 
-				geometries.update( buffergeometry );
+		// When rendered geometry doesn't include these attributes but the material does,
+		// use these default values in WebGL. This avoids errors when buffer data is missing.
+		this.defaultAttributeValues = {
+			'color': [ 1, 1, 1 ],
+			'uv': [ 0, 0 ],
+			'uv2': [ 0, 0 ]
+		};
 
-				updateMap.set( buffergeometry, frame );
+		this.index0AttributeName = undefined;
+		this.uniformsNeedUpdate = false;
 
-			}
+		if ( parameters !== undefined ) {
 
-			if ( object.isInstancedMesh ) {
+			if ( parameters.attributes !== undefined ) {
 
-				attributes.update( object.instanceMatrix, 34962 );
+				console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
 
 			}
 
-			return buffergeometry;
+			this.setValues( parameters );
 
 		}
 
-		function dispose() {
+	}
 
-			updateMap = new WeakMap();
+	ShaderMaterial.prototype = Object.create( Material.prototype );
+	ShaderMaterial.prototype.constructor = ShaderMaterial;
 
-		}
+	ShaderMaterial.prototype.isShaderMaterial = true;
 
-		return {
+	ShaderMaterial.prototype.copy = function ( source ) {
 
-			update: update,
-			dispose: dispose
+		Material.prototype.copy.call( this, source );
 
-		};
+		this.fragmentShader = source.fragmentShader;
+		this.vertexShader = source.vertexShader;
 
-	}
+		this.uniforms = cloneUniforms( source.uniforms );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		this.defines = Object.assign( {}, source.defines );
 
-	function CubeTexture( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
 
-		images = images !== undefined ? images : [];
-		mapping = mapping !== undefined ? mapping : CubeReflectionMapping;
-		format = format !== undefined ? format : RGBFormat;
+		this.lights = source.lights;
+		this.clipping = source.clipping;
 
-		Texture.call( this, images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
+		this.skinning = source.skinning;
 
-		this.flipY = false;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-	}
+		this.extensions = source.extensions;
 
-	CubeTexture.prototype = Object.create( Texture.prototype );
-	CubeTexture.prototype.constructor = CubeTexture;
+		return this;
 
-	CubeTexture.prototype.isCubeTexture = true;
+	};
 
-	Object.defineProperty( CubeTexture.prototype, 'images', {
+	ShaderMaterial.prototype.toJSON = function ( meta ) {
 
-		get: function () {
+		var data = Material.prototype.toJSON.call( this, meta );
 
-			return this.image;
+		data.uniforms = {};
 
-		},
+		for ( var name in this.uniforms ) {
 
-		set: function ( value ) {
+			var uniform = this.uniforms[ name ];
+			var value = uniform.value;
 
-			this.image = value;
+			if ( value && value.isTexture ) {
 
-		}
+				data.uniforms[ name ] = {
+					type: 't',
+					value: value.toJSON( meta ).uuid
+				};
 
-	} );
+			} else if ( value && value.isColor ) {
 
-	/**
-	 * @author Takahiro https://github.com/takahirox
-	 */
+				data.uniforms[ name ] = {
+					type: 'c',
+					value: value.getHex()
+				};
 
-	function DataTexture2DArray( data, width, height, depth ) {
+			} else if ( value && value.isVector2 ) {
 
-		Texture.call( this, null );
+				data.uniforms[ name ] = {
+					type: 'v2',
+					value: value.toArray()
+				};
 
-		this.image = { data: data || null, width: width || 1, height: height || 1, depth: depth || 1 };
+			} else if ( value && value.isVector3 ) {
 
-		this.magFilter = NearestFilter;
-		this.minFilter = NearestFilter;
+				data.uniforms[ name ] = {
+					type: 'v3',
+					value: value.toArray()
+				};
 
-		this.wrapR = ClampToEdgeWrapping;
+			} else if ( value && value.isVector4 ) {
 
-		this.generateMipmaps = false;
-		this.flipY = false;
+				data.uniforms[ name ] = {
+					type: 'v4',
+					value: value.toArray()
+				};
 
-		this.needsUpdate = true;
+			} else if ( value && value.isMatrix3 ) {
 
-	}
+				data.uniforms[ name ] = {
+					type: 'm3',
+					value: value.toArray()
+				};
 
-	DataTexture2DArray.prototype = Object.create( Texture.prototype );
-	DataTexture2DArray.prototype.constructor = DataTexture2DArray;
-	DataTexture2DArray.prototype.isDataTexture2DArray = true;
+			} else if ( value && value.isMatrix4 ) {
 
-	/**
-	 * @author Artur Trzesiok
-	 */
+				data.uniforms[ name ] = {
+					type: 'm4',
+					value: value.toArray()
+				};
 
-	function DataTexture3D( data, width, height, depth ) {
+			} else {
 
-		// We're going to add .setXXX() methods for setting properties later.
-		// Users can still set in DataTexture3D directly.
-		//
-		//	var texture = new THREE.DataTexture3D( data, width, height, depth );
-		// 	texture.anisotropy = 16;
-		//
-		// See #14839
+				data.uniforms[ name ] = {
+					value: value
+				};
 
-		Texture.call( this, null );
+				// note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
 
-		this.image = { data: data || null, width: width || 1, height: height || 1, depth: depth || 1 };
+			}
 
-		this.magFilter = NearestFilter;
-		this.minFilter = NearestFilter;
+		}
 
-		this.wrapR = ClampToEdgeWrapping;
+		if ( Object.keys( this.defines ).length > 0 ) { data.defines = this.defines; }
 
-		this.generateMipmaps = false;
-		this.flipY = false;
+		data.vertexShader = this.vertexShader;
+		data.fragmentShader = this.fragmentShader;
 
-		this.needsUpdate = true;
+		var extensions = {};
+
+		for ( var key in this.extensions ) {
 
+			if ( this.extensions[ key ] === true ) { extensions[ key ] = true; }
 
-	}
+		}
 
-	DataTexture3D.prototype = Object.create( Texture.prototype );
-	DataTexture3D.prototype.constructor = DataTexture3D;
-	DataTexture3D.prototype.isDataTexture3D = true;
+		if ( Object.keys( extensions ).length > 0 ) { data.extensions = extensions; }
+
+		return data;
+
+	};
 
 	/**
-	 * @author tschw
-	 * @author Mugen87 / https://github.com/Mugen87
 	 * @author mrdoob / http://mrdoob.com/
-	 *
-	 * Uniforms of a program.
-	 * Those form a tree structure with a special top-level container for the root,
-	 * which you get by calling 'new WebGLUniforms( gl, program )'.
-	 *
-	 *
-	 * Properties of inner nodes including the top-level container:
-	 *
-	 * .seq - array of nested uniforms
-	 * .map - nested uniforms by name
-	 *
-	 *
-	 * Methods of all nodes except the top-level container:
-	 *
-	 * .setValue( gl, value, [textures] )
-	 *
-	 * 		uploads a uniform value(s)
-	 *  	the 'textures' parameter is needed for sampler uniforms
-	 *
-	 *
-	 * Static methods of the top-level container (textures factorizations):
-	 *
-	 * .upload( gl, seq, values, textures )
-	 *
-	 * 		sets uniforms in 'seq' to 'values[id].value'
-	 *
-	 * .seqWithValue( seq, values ) : filteredSeq
-	 *
-	 * 		filters 'seq' entries with corresponding entry in values
-	 *
-	 *
-	 * Methods of the top-level container (textures factorizations):
-	 *
-	 * .setValue( gl, name, value, textures )
-	 *
-	 * 		sets uniform with  name 'name' to 'value'
-	 *
-	 * .setOptional( gl, obj, prop )
-	 *
-	 * 		like .set for an optional property of the object
-	 *
-	 */
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author WestLangley / http://github.com/WestLangley
+	*/
 
-	var emptyTexture = new Texture();
-	var emptyTexture2dArray = new DataTexture2DArray();
-	var emptyTexture3d = new DataTexture3D();
-	var emptyCubeTexture = new CubeTexture();
+	function Camera() {
 
-	// --- Utilities ---
+		Object3D.call( this );
 
-	// Array Caches (provide typed arrays for temporary by size)
+		this.type = 'Camera';
 
-	var arrayCacheF32 = [];
-	var arrayCacheI32 = [];
+		this.matrixWorldInverse = new Matrix4();
 
-	// Float32Array caches used for uploading Matrix uniforms
+		this.projectionMatrix = new Matrix4();
+		this.projectionMatrixInverse = new Matrix4();
 
-	var mat4array = new Float32Array( 16 );
-	var mat3array = new Float32Array( 9 );
-	var mat2array = new Float32Array( 4 );
+	}
 
-	// Flattening for arrays of vectors and matrices
+	Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-	function flatten( array, nBlocks, blockSize ) {
+		constructor: Camera,
 
-		var firstElem = array[ 0 ];
+		isCamera: true,
 
-		if ( firstElem <= 0 || firstElem > 0 ) { return array; }
-		// unoptimized: ! isNaN( firstElem )
-		// see http://jacksondunstan.com/articles/983
+		copy: function ( source, recursive ) {
 
-		var n = nBlocks * blockSize,
-			r = arrayCacheF32[ n ];
+			Object3D.prototype.copy.call( this, source, recursive );
 
-		if ( r === undefined ) {
+			this.matrixWorldInverse.copy( source.matrixWorldInverse );
 
-			r = new Float32Array( n );
-			arrayCacheF32[ n ] = r;
+			this.projectionMatrix.copy( source.projectionMatrix );
+			this.projectionMatrixInverse.copy( source.projectionMatrixInverse );
 
-		}
+			return this;
 
-		if ( nBlocks !== 0 ) {
+		},
 
-			firstElem.toArray( r, 0 );
+		getWorldDirection: function ( target ) {
 
-			for ( var i = 1, offset = 0; i !== nBlocks; ++ i ) {
+			if ( target === undefined ) {
 
-				offset += blockSize;
-				array[ i ].toArray( r, offset );
+				console.warn( 'THREE.Camera: .getWorldDirection() target is now required' );
+				target = new Vector3();
 
 			}
 
-		}
+			this.updateMatrixWorld( true );
 
-		return r;
+			var e = this.matrixWorld.elements;
 
-	}
+			return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
 
-	function arraysEqual( a, b ) {
+		},
 
-		if ( a.length !== b.length ) { return false; }
+		updateMatrixWorld: function ( force ) {
 
-		for ( var i = 0, l = a.length; i < l; i ++ ) {
+			Object3D.prototype.updateMatrixWorld.call( this, force );
 
-			if ( a[ i ] !== b[ i ] ) { return false; }
+			this.matrixWorldInverse.getInverse( this.matrixWorld );
 
-		}
+		},
 
-		return true;
+		updateWorldMatrix: function ( updateParents, updateChildren ) {
 
-	}
+			Object3D.prototype.updateWorldMatrix.call( this, updateParents, updateChildren );
 
-	function copyArray( a, b ) {
+			this.matrixWorldInverse.getInverse( this.matrixWorld );
 
-		for ( var i = 0, l = b.length; i < l; i ++ ) {
+		},
 
-			a[ i ] = b[ i ];
+		clone: function () {
+
+			return new this.constructor().copy( this );
 
 		}
 
-	}
+	} );
 
-	// Texture unit allocation
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author greggman / http://games.greggman.com/
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * @author tschw
+	 */
 
-	function allocTexUnits( textures, n ) {
+	function PerspectiveCamera( fov, aspect, near, far ) {
 
-		var r = arrayCacheI32[ n ];
+		Camera.call( this );
 
-		if ( r === undefined ) {
+		this.type = 'PerspectiveCamera';
 
-			r = new Int32Array( n );
-			arrayCacheI32[ n ] = r;
+		this.fov = fov !== undefined ? fov : 50;
+		this.zoom = 1;
 
-		}
+		this.near = near !== undefined ? near : 0.1;
+		this.far = far !== undefined ? far : 2000;
+		this.focus = 10;
 
-		for ( var i = 0; i !== n; ++ i )
-			{ r[ i ] = textures.allocateTextureUnit(); }
+		this.aspect = aspect !== undefined ? aspect : 1;
+		this.view = null;
 
-		return r;
+		this.filmGauge = 35;	// width of the film (default in millimeters)
+		this.filmOffset = 0;	// horizontal film offset (same unit as gauge)
+
+		this.updateProjectionMatrix();
 
 	}
 
-	// --- Setters ---
+	PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
 
-	// Note: Defining these methods externally, because they come in a bunch
-	// and this way their names minify.
+		constructor: PerspectiveCamera,
 
-	// Single scalar
+		isPerspectiveCamera: true,
 
-	function setValueV1f( gl, v ) {
+		copy: function ( source, recursive ) {
 
-		var cache = this.cache;
+			Camera.prototype.copy.call( this, source, recursive );
 
-		if ( cache[ 0 ] === v ) { return; }
+			this.fov = source.fov;
+			this.zoom = source.zoom;
 
-		gl.uniform1f( this.addr, v );
+			this.near = source.near;
+			this.far = source.far;
+			this.focus = source.focus;
 
-		cache[ 0 ] = v;
+			this.aspect = source.aspect;
+			this.view = source.view === null ? null : Object.assign( {}, source.view );
 
-	}
+			this.filmGauge = source.filmGauge;
+			this.filmOffset = source.filmOffset;
 
-	// Single float vector (from flat array or THREE.VectorN)
+			return this;
 
-	function setValueV2f( gl, v ) {
+		},
 
-		var cache = this.cache;
+		/**
+		 * Sets the FOV by focal length in respect to the current .filmGauge.
+		 *
+		 * The default film gauge is 35, so that the focal length can be specified for
+		 * a 35mm (full frame) camera.
+		 *
+		 * Values for focal length and film gauge must have the same unit.
+		 */
+		setFocalLength: function ( focalLength ) {
 
-		if ( v.x !== undefined ) {
+			// see http://www.bobatkins.com/photography/technical/field_of_view.html
+			var vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
 
-			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) {
+			this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope );
+			this.updateProjectionMatrix();
 
-				gl.uniform2f( this.addr, v.x, v.y );
+		},
 
-				cache[ 0 ] = v.x;
-				cache[ 1 ] = v.y;
+		/**
+		 * Calculates the focal length from the current .fov and .filmGauge.
+		 */
+		getFocalLength: function () {
 
-			}
+			var vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
 
-		} else {
+			return 0.5 * this.getFilmHeight() / vExtentSlope;
 
-			if ( arraysEqual( cache, v ) ) { return; }
+		},
 
-			gl.uniform2fv( this.addr, v );
+		getEffectiveFOV: function () {
 
-			copyArray( cache, v );
+			return MathUtils.RAD2DEG * 2 * Math.atan(
+				Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom );
 
-		}
+		},
 
-	}
+		getFilmWidth: function () {
 
-	function setValueV3f( gl, v ) {
+			// film not completely covered in portrait format (aspect < 1)
+			return this.filmGauge * Math.min( this.aspect, 1 );
 
-		var cache = this.cache;
+		},
 
-		if ( v.x !== undefined ) {
+		getFilmHeight: function () {
 
-			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) {
+			// film not completely covered in landscape format (aspect > 1)
+			return this.filmGauge / Math.max( this.aspect, 1 );
 
-				gl.uniform3f( this.addr, v.x, v.y, v.z );
+		},
 
-				cache[ 0 ] = v.x;
-				cache[ 1 ] = v.y;
-				cache[ 2 ] = v.z;
+		/**
+		 * Sets an offset in a larger frustum. This is useful for multi-window or
+		 * multi-monitor/multi-machine setups.
+		 *
+		 * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
+		 * the monitors are in grid like this
+		 *
+		 *   +---+---+---+
+		 *   | A | B | C |
+		 *   +---+---+---+
+		 *   | D | E | F |
+		 *   +---+---+---+
+		 *
+		 * then for each monitor you would call it like this
+		 *
+		 *   var w = 1920;
+		 *   var h = 1080;
+		 *   var fullWidth = w * 3;
+		 *   var fullHeight = h * 2;
+		 *
+		 *   --A--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
+		 *   --B--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
+		 *   --C--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
+		 *   --D--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
+		 *   --E--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
+		 *   --F--
+		 *   camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
+		 *
+		 *   Note there is no reason monitors have to be the same size or in a grid.
+		 */
+		setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
 
-			}
+			this.aspect = fullWidth / fullHeight;
 
-		} else if ( v.r !== undefined ) {
+			if ( this.view === null ) {
 
-			if ( cache[ 0 ] !== v.r || cache[ 1 ] !== v.g || cache[ 2 ] !== v.b ) {
+				this.view = {
+					enabled: true,
+					fullWidth: 1,
+					fullHeight: 1,
+					offsetX: 0,
+					offsetY: 0,
+					width: 1,
+					height: 1
+				};
 
-				gl.uniform3f( this.addr, v.r, v.g, v.b );
+			}
 
-				cache[ 0 ] = v.r;
-				cache[ 1 ] = v.g;
-				cache[ 2 ] = v.b;
+			this.view.enabled = true;
+			this.view.fullWidth = fullWidth;
+			this.view.fullHeight = fullHeight;
+			this.view.offsetX = x;
+			this.view.offsetY = y;
+			this.view.width = width;
+			this.view.height = height;
 
-			}
+			this.updateProjectionMatrix();
 
-		} else {
+		},
 
-			if ( arraysEqual( cache, v ) ) { return; }
+		clearViewOffset: function () {
 
-			gl.uniform3fv( this.addr, v );
+			if ( this.view !== null ) {
 
-			copyArray( cache, v );
+				this.view.enabled = false;
 
-		}
+			}
 
-	}
+			this.updateProjectionMatrix();
 
-	function setValueV4f( gl, v ) {
+		},
 
-		var cache = this.cache;
+		updateProjectionMatrix: function () {
 
-		if ( v.x !== undefined ) {
+			var near = this.near,
+				top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom,
+				height = 2 * top,
+				width = this.aspect * height,
+				left = - 0.5 * width,
+				view = this.view;
 
-			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) {
+			if ( this.view !== null && this.view.enabled ) {
 
-				gl.uniform4f( this.addr, v.x, v.y, v.z, v.w );
+				var fullWidth = view.fullWidth,
+					fullHeight = view.fullHeight;
 
-				cache[ 0 ] = v.x;
-				cache[ 1 ] = v.y;
-				cache[ 2 ] = v.z;
-				cache[ 3 ] = v.w;
+				left += view.offsetX * width / fullWidth;
+				top -= view.offsetY * height / fullHeight;
+				width *= view.width / fullWidth;
+				height *= view.height / fullHeight;
 
 			}
 
-		} else {
-
-			if ( arraysEqual( cache, v ) ) { return; }
+			var skew = this.filmOffset;
+			if ( skew !== 0 ) { left += near * skew / this.getFilmWidth(); }
 
-			gl.uniform4fv( this.addr, v );
+			this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );
 
-			copyArray( cache, v );
+			this.projectionMatrixInverse.getInverse( this.projectionMatrix );
 
-		}
+		},
 
-	}
+		toJSON: function ( meta ) {
 
-	// Single matrix (from flat array or MatrixN)
+			var data = Object3D.prototype.toJSON.call( this, meta );
 
-	function setValueM2( gl, v ) {
+			data.object.fov = this.fov;
+			data.object.zoom = this.zoom;
 
-		var cache = this.cache;
-		var elements = v.elements;
+			data.object.near = this.near;
+			data.object.far = this.far;
+			data.object.focus = this.focus;
 
-		if ( elements === undefined ) {
+			data.object.aspect = this.aspect;
 
-			if ( arraysEqual( cache, v ) ) { return; }
+			if ( this.view !== null ) { data.object.view = Object.assign( {}, this.view ); }
 
-			gl.uniformMatrix2fv( this.addr, false, v );
+			data.object.filmGauge = this.filmGauge;
+			data.object.filmOffset = this.filmOffset;
 
-			copyArray( cache, v );
+			return data;
 
-		} else {
+		}
 
-			if ( arraysEqual( cache, elements ) ) { return; }
+	} );
 
-			mat2array.set( elements );
+	/**
+	 * Camera for rendering cube maps
+	 *	- renders scene into axis-aligned cube
+	 *
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-			gl.uniformMatrix2fv( this.addr, false, mat2array );
+	var fov = 90, aspect = 1;
 
-			copyArray( cache, elements );
+	function CubeCamera( near, far, cubeResolution, options ) {
 
-		}
+		Object3D.call( this );
 
-	}
+		this.type = 'CubeCamera';
 
-	function setValueM3( gl, v ) {
+		var cameraPX = new PerspectiveCamera( fov, aspect, near, far );
+		cameraPX.up.set( 0, - 1, 0 );
+		cameraPX.lookAt( new Vector3( 1, 0, 0 ) );
+		this.add( cameraPX );
 
-		var cache = this.cache;
-		var elements = v.elements;
+		var cameraNX = new PerspectiveCamera( fov, aspect, near, far );
+		cameraNX.up.set( 0, - 1, 0 );
+		cameraNX.lookAt( new Vector3( - 1, 0, 0 ) );
+		this.add( cameraNX );
 
-		if ( elements === undefined ) {
+		var cameraPY = new PerspectiveCamera( fov, aspect, near, far );
+		cameraPY.up.set( 0, 0, 1 );
+		cameraPY.lookAt( new Vector3( 0, 1, 0 ) );
+		this.add( cameraPY );
 
-			if ( arraysEqual( cache, v ) ) { return; }
+		var cameraNY = new PerspectiveCamera( fov, aspect, near, far );
+		cameraNY.up.set( 0, 0, - 1 );
+		cameraNY.lookAt( new Vector3( 0, - 1, 0 ) );
+		this.add( cameraNY );
 
-			gl.uniformMatrix3fv( this.addr, false, v );
+		var cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
+		cameraPZ.up.set( 0, - 1, 0 );
+		cameraPZ.lookAt( new Vector3( 0, 0, 1 ) );
+		this.add( cameraPZ );
 
-			copyArray( cache, v );
+		var cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
+		cameraNZ.up.set( 0, - 1, 0 );
+		cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) );
+		this.add( cameraNZ );
 
-		} else {
+		options = options || { format: RGBFormat, magFilter: LinearFilter, minFilter: LinearFilter };
 
-			if ( arraysEqual( cache, elements ) ) { return; }
+		this.renderTarget = new WebGLCubeRenderTarget( cubeResolution, options );
+		this.renderTarget.texture.name = "CubeCamera";
 
-			mat3array.set( elements );
+		this.update = function ( renderer, scene ) {
 
-			gl.uniformMatrix3fv( this.addr, false, mat3array );
+			if ( this.parent === null ) { this.updateMatrixWorld(); }
 
-			copyArray( cache, elements );
+			var currentRenderTarget = renderer.getRenderTarget();
 
-		}
+			var renderTarget = this.renderTarget;
+			var generateMipmaps = renderTarget.texture.generateMipmaps;
 
-	}
+			renderTarget.texture.generateMipmaps = false;
 
-	function setValueM4( gl, v ) {
+			renderer.setRenderTarget( renderTarget, 0 );
+			renderer.render( scene, cameraPX );
 
-		var cache = this.cache;
-		var elements = v.elements;
+			renderer.setRenderTarget( renderTarget, 1 );
+			renderer.render( scene, cameraNX );
 
-		if ( elements === undefined ) {
+			renderer.setRenderTarget( renderTarget, 2 );
+			renderer.render( scene, cameraPY );
 
-			if ( arraysEqual( cache, v ) ) { return; }
+			renderer.setRenderTarget( renderTarget, 3 );
+			renderer.render( scene, cameraNY );
 
-			gl.uniformMatrix4fv( this.addr, false, v );
+			renderer.setRenderTarget( renderTarget, 4 );
+			renderer.render( scene, cameraPZ );
 
-			copyArray( cache, v );
+			renderTarget.texture.generateMipmaps = generateMipmaps;
 
-		} else {
+			renderer.setRenderTarget( renderTarget, 5 );
+			renderer.render( scene, cameraNZ );
 
-			if ( arraysEqual( cache, elements ) ) { return; }
+			renderer.setRenderTarget( currentRenderTarget );
 
-			mat4array.set( elements );
+		};
 
-			gl.uniformMatrix4fv( this.addr, false, mat4array );
+		this.clear = function ( renderer, color, depth, stencil ) {
 
-			copyArray( cache, elements );
+			var currentRenderTarget = renderer.getRenderTarget();
 
-		}
+			var renderTarget = this.renderTarget;
 
-	}
+			for ( var i = 0; i < 6; i ++ ) {
 
-	// Single texture (2D / Cube)
+				renderer.setRenderTarget( renderTarget, i );
 
-	function setValueT1( gl, v, textures ) {
+				renderer.clear( color, depth, stencil );
 
-		var cache = this.cache;
-		var unit = textures.allocateTextureUnit();
+			}
 
-		if ( cache[ 0 ] !== unit ) {
+			renderer.setRenderTarget( currentRenderTarget );
 
-			gl.uniform1i( this.addr, unit );
-			cache[ 0 ] = unit;
+		};
 
-		}
+	}
 
-		textures.safeSetTexture2D( v || emptyTexture, unit );
+	CubeCamera.prototype = Object.create( Object3D.prototype );
+	CubeCamera.prototype.constructor = CubeCamera;
 
-	}
+	/**
+	 * @author alteredq / http://alteredqualia.com
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-	function setValueT2DArray1( gl, v, textures ) {
+	function WebGLCubeRenderTarget( size, options, dummy ) {
 
-		var cache = this.cache;
-		var unit = textures.allocateTextureUnit();
+		if ( Number.isInteger( options ) ) {
 
-		if ( cache[ 0 ] !== unit ) {
+			console.warn( 'THREE.WebGLCubeRenderTarget: constructor signature is now WebGLCubeRenderTarget( size, options )' );
 
-			gl.uniform1i( this.addr, unit );
-			cache[ 0 ] = unit;
+			options = dummy;
 
 		}
 
-		textures.setTexture2DArray( v || emptyTexture2dArray, unit );
+		WebGLRenderTarget.call( this, size, size, options );
 
 	}
 
-	function setValueT3D1( gl, v, textures ) {
+	WebGLCubeRenderTarget.prototype = Object.create( WebGLRenderTarget.prototype );
+	WebGLCubeRenderTarget.prototype.constructor = WebGLCubeRenderTarget;
 
-		var cache = this.cache;
-		var unit = textures.allocateTextureUnit();
+	WebGLCubeRenderTarget.prototype.isWebGLCubeRenderTarget = true;
 
-		if ( cache[ 0 ] !== unit ) {
+	WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer, texture ) {
 
-			gl.uniform1i( this.addr, unit );
-			cache[ 0 ] = unit;
+		this.texture.type = texture.type;
+		this.texture.format = texture.format;
+		this.texture.encoding = texture.encoding;
 
-		}
+		var scene = new Scene();
 
-		textures.setTexture3D( v || emptyTexture3d, unit );
+		var shader = {
 
-	}
+			uniforms: {
+				tEquirect: { value: null },
+			},
 
-	function setValueT6( gl, v, textures ) {
+			vertexShader: [
 
-		var cache = this.cache;
-		var unit = textures.allocateTextureUnit();
+				"varying vec3 vWorldDirection;",
 
-		if ( cache[ 0 ] !== unit ) {
+				"vec3 transformDirection( in vec3 dir, in mat4 matrix ) {",
 
-			gl.uniform1i( this.addr, unit );
-			cache[ 0 ] = unit;
+				"	return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );",
 
-		}
+				"}",
 
-		textures.safeSetTextureCube( v || emptyCubeTexture, unit );
+				"void main() {",
 
-	}
+				"	vWorldDirection = transformDirection( position, modelMatrix );",
 
-	// Integer / Boolean vectors or arrays thereof (always flat arrays)
+				"	#include <begin_vertex>",
+				"	#include <project_vertex>",
 
-	function setValueV1i( gl, v ) {
+				"}"
 
-		var cache = this.cache;
+			].join( '\n' ),
 
-		if ( cache[ 0 ] === v ) { return; }
+			fragmentShader: [
 
-		gl.uniform1i( this.addr, v );
+				"uniform sampler2D tEquirect;",
 
-		cache[ 0 ] = v;
+				"varying vec3 vWorldDirection;",
 
-	}
+				"#define RECIPROCAL_PI 0.31830988618",
+				"#define RECIPROCAL_PI2 0.15915494",
 
-	function setValueV2i( gl, v ) {
+				"void main() {",
 
-		var cache = this.cache;
+				"	vec3 direction = normalize( vWorldDirection );",
 
-		if ( arraysEqual( cache, v ) ) { return; }
+				"	vec2 sampleUV;",
 
-		gl.uniform2iv( this.addr, v );
+				"	sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;",
 
-		copyArray( cache, v );
+				"	sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
 
-	}
+				"	gl_FragColor = texture2D( tEquirect, sampleUV );",
 
-	function setValueV3i( gl, v ) {
+				"}"
 
-		var cache = this.cache;
+			].join( '\n' ),
+		};
 
-		if ( arraysEqual( cache, v ) ) { return; }
+		var material = new ShaderMaterial( {
 
-		gl.uniform3iv( this.addr, v );
+			type: 'CubemapFromEquirect',
 
-		copyArray( cache, v );
+			uniforms: cloneUniforms( shader.uniforms ),
+			vertexShader: shader.vertexShader,
+			fragmentShader: shader.fragmentShader,
+			side: BackSide,
+			blending: NoBlending
 
-	}
+		} );
 
-	function setValueV4i( gl, v ) {
+		material.uniforms.tEquirect.value = texture;
 
-		var cache = this.cache;
+		var mesh = new Mesh( new BoxBufferGeometry( 5, 5, 5 ), material );
 
-		if ( arraysEqual( cache, v ) ) { return; }
+		scene.add( mesh );
 
-		gl.uniform4iv( this.addr, v );
+		var camera = new CubeCamera( 1, 10, 1 );
 
-		copyArray( cache, v );
+		camera.renderTarget = this;
+		camera.renderTarget.texture.name = 'CubeCameraTexture';
 
-	}
+		camera.update( renderer, scene );
 
-	// uint
+		mesh.geometry.dispose();
+		mesh.material.dispose();
 
-	function setValueV1ui( gl, v ) {
+		return this;
 
-		var cache = this.cache;
+	};
 
-		if ( cache[ 0 ] === v ) { return; }
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-		gl.uniform1ui( this.addr, v );
+	function DataTexture( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) {
 
-		cache[ 0 ] = v;
+		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
 
-	}
+		this.image = { data: data || null, width: width || 1, height: height || 1 };
 
-	// Helper to pick the right setter for the singular case
+		this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
+		this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
 
-	function getSingularSetter( type ) {
+		this.generateMipmaps = false;
+		this.flipY = false;
+		this.unpackAlignment = 1;
 
-		switch ( type ) {
+		this.needsUpdate = true;
 
-			case 0x1406: return setValueV1f; // FLOAT
-			case 0x8b50: return setValueV2f; // _VEC2
-			case 0x8b51: return setValueV3f; // _VEC3
-			case 0x8b52: return setValueV4f; // _VEC4
+	}
 
-			case 0x8b5a: return setValueM2; // _MAT2
-			case 0x8b5b: return setValueM3; // _MAT3
-			case 0x8b5c: return setValueM4; // _MAT4
+	DataTexture.prototype = Object.create( Texture.prototype );
+	DataTexture.prototype.constructor = DataTexture;
 
-			case 0x1404: case 0x8b56: return setValueV1i; // INT, BOOL
-			case 0x8b53: case 0x8b57: return setValueV2i; // _VEC2
-			case 0x8b54: case 0x8b58: return setValueV3i; // _VEC3
-			case 0x8b55: case 0x8b59: return setValueV4i; // _VEC4
+	DataTexture.prototype.isDataTexture = true;
 
-			case 0x1405: return setValueV1ui; // UINT
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author bhouston / http://clara.io
+	 */
 
-			case 0x8b5e: // SAMPLER_2D
-			case 0x8d66: // SAMPLER_EXTERNAL_OES
-			case 0x8dca: // INT_SAMPLER_2D
-			case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
-			case 0x8b62: // SAMPLER_2D_SHADOW
-				return setValueT1;
+	var _sphere$1 = new Sphere();
+	var _vector$5 = new Vector3();
 
-			case 0x8b5f: // SAMPLER_3D
-			case 0x8dcb: // INT_SAMPLER_3D
-			case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D
-				return setValueT3D1;
+	function Frustum( p0, p1, p2, p3, p4, p5 ) {
 
-			case 0x8b60: // SAMPLER_CUBE
-			case 0x8dcc: // INT_SAMPLER_CUBE
-			case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
-			case 0x8dc5: // SAMPLER_CUBE_SHADOW
-				return setValueT6;
+		this.planes = [
 
-			case 0x8dc1: // SAMPLER_2D_ARRAY
-			case 0x8dcf: // INT_SAMPLER_2D_ARRAY
-			case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY
-			case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW
-				return setValueT2DArray1;
+			( p0 !== undefined ) ? p0 : new Plane(),
+			( p1 !== undefined ) ? p1 : new Plane(),
+			( p2 !== undefined ) ? p2 : new Plane(),
+			( p3 !== undefined ) ? p3 : new Plane(),
+			( p4 !== undefined ) ? p4 : new Plane(),
+			( p5 !== undefined ) ? p5 : new Plane()
 
-		}
+		];
 
 	}
 
-	// Array of scalars
-	function setValueV1fArray( gl, v ) {
+	Object.assign( Frustum.prototype, {
 
-		gl.uniform1fv( this.addr, v );
+		set: function ( p0, p1, p2, p3, p4, p5 ) {
 
-	}
+			var planes = this.planes;
 
-	// Integer / Boolean vectors or arrays thereof (always flat arrays)
-	function setValueV1iArray( gl, v ) {
+			planes[ 0 ].copy( p0 );
+			planes[ 1 ].copy( p1 );
+			planes[ 2 ].copy( p2 );
+			planes[ 3 ].copy( p3 );
+			planes[ 4 ].copy( p4 );
+			planes[ 5 ].copy( p5 );
 
-		gl.uniform1iv( this.addr, v );
+			return this;
 
-	}
+		},
 
-	function setValueV2iArray( gl, v ) {
+		clone: function () {
 
-		gl.uniform2iv( this.addr, v );
+			return new this.constructor().copy( this );
 
-	}
+		},
 
-	function setValueV3iArray( gl, v ) {
+		copy: function ( frustum ) {
 
-		gl.uniform3iv( this.addr, v );
+			var planes = this.planes;
 
-	}
+			for ( var i = 0; i < 6; i ++ ) {
 
-	function setValueV4iArray( gl, v ) {
+				planes[ i ].copy( frustum.planes[ i ] );
 
-		gl.uniform4iv( this.addr, v );
+			}
 
-	}
+			return this;
 
+		},
 
-	// Array of vectors (flat or from THREE classes)
+		setFromProjectionMatrix: function ( m ) {
 
-	function setValueV2fArray( gl, v ) {
+			var planes = this.planes;
+			var me = m.elements;
+			var me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
+			var me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
+			var me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
+			var me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
 
-		var data = flatten( v, this.size, 2 );
+			planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
+			planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
+			planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
+			planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
+			planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
+			planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
 
-		gl.uniform2fv( this.addr, data );
+			return this;
 
-	}
+		},
 
-	function setValueV3fArray( gl, v ) {
+		intersectsObject: function ( object ) {
 
-		var data = flatten( v, this.size, 3 );
+			var geometry = object.geometry;
 
-		gl.uniform3fv( this.addr, data );
+			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
 
-	}
+			_sphere$1.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
 
-	function setValueV4fArray( gl, v ) {
+			return this.intersectsSphere( _sphere$1 );
 
-		var data = flatten( v, this.size, 4 );
+		},
 
-		gl.uniform4fv( this.addr, data );
+		intersectsSprite: function ( sprite ) {
 
-	}
+			_sphere$1.center.set( 0, 0, 0 );
+			_sphere$1.radius = 0.7071067811865476;
+			_sphere$1.applyMatrix4( sprite.matrixWorld );
 
-	// Array of matrices (flat or from THREE clases)
+			return this.intersectsSphere( _sphere$1 );
 
-	function setValueM2Array( gl, v ) {
+		},
 
-		var data = flatten( v, this.size, 4 );
+		intersectsSphere: function ( sphere ) {
 
-		gl.uniformMatrix2fv( this.addr, false, data );
+			var planes = this.planes;
+			var center = sphere.center;
+			var negRadius = - sphere.radius;
 
-	}
+			for ( var i = 0; i < 6; i ++ ) {
 
-	function setValueM3Array( gl, v ) {
+				var distance = planes[ i ].distanceToPoint( center );
 
-		var data = flatten( v, this.size, 9 );
+				if ( distance < negRadius ) {
 
-		gl.uniformMatrix3fv( this.addr, false, data );
+					return false;
 
-	}
+				}
 
-	function setValueM4Array( gl, v ) {
+			}
 
-		var data = flatten( v, this.size, 16 );
+			return true;
 
-		gl.uniformMatrix4fv( this.addr, false, data );
+		},
 
-	}
+		intersectsBox: function ( box ) {
 
-	// Array of textures (2D / Cube)
+			var planes = this.planes;
 
-	function setValueT1Array( gl, v, textures ) {
+			for ( var i = 0; i < 6; i ++ ) {
 
-		var n = v.length;
+				var plane = planes[ i ];
 
-		var units = allocTexUnits( textures, n );
+				// corner at max distance
 
-		gl.uniform1iv( this.addr, units );
+				_vector$5.x = plane.normal.x > 0 ? box.max.x : box.min.x;
+				_vector$5.y = plane.normal.y > 0 ? box.max.y : box.min.y;
+				_vector$5.z = plane.normal.z > 0 ? box.max.z : box.min.z;
 
-		for ( var i = 0; i !== n; ++ i ) {
+				if ( plane.distanceToPoint( _vector$5 ) < 0 ) {
 
-			textures.safeSetTexture2D( v[ i ] || emptyTexture, units[ i ] );
+					return false;
 
-		}
+				}
 
-	}
+			}
 
-	function setValueT6Array( gl, v, textures ) {
+			return true;
 
-		var n = v.length;
+		},
 
-		var units = allocTexUnits( textures, n );
+		containsPoint: function ( point ) {
 
-		gl.uniform1iv( this.addr, units );
+			var planes = this.planes;
 
-		for ( var i = 0; i !== n; ++ i ) {
+			for ( var i = 0; i < 6; i ++ ) {
 
-			textures.safeSetTextureCube( v[ i ] || emptyCubeTexture, units[ i ] );
+				if ( planes[ i ].distanceToPoint( point ) < 0 ) {
 
-		}
+					return false;
 
-	}
+				}
 
-	// Helper to pick the right setter for a pure (bottom-level) array
+			}
 
-	function getPureArraySetter( type ) {
+			return true;
 
-		switch ( type ) {
+		}
 
-			case 0x1406: return setValueV1fArray; // FLOAT
-			case 0x8b50: return setValueV2fArray; // _VEC2
-			case 0x8b51: return setValueV3fArray; // _VEC3
-			case 0x8b52: return setValueV4fArray; // _VEC4
+	} );
 
-			case 0x8b5a: return setValueM2Array; // _MAT2
-			case 0x8b5b: return setValueM3Array; // _MAT3
-			case 0x8b5c: return setValueM4Array; // _MAT4
+	/**
+	 * Uniforms library for shared webgl shaders
+	 */
 
-			case 0x1404: case 0x8b56: return setValueV1iArray; // INT, BOOL
-			case 0x8b53: case 0x8b57: return setValueV2iArray; // _VEC2
-			case 0x8b54: case 0x8b58: return setValueV3iArray; // _VEC3
-			case 0x8b55: case 0x8b59: return setValueV4iArray; // _VEC4
+	var UniformsLib = {
 
-			case 0x8b5e: // SAMPLER_2D
-			case 0x8d66: // SAMPLER_EXTERNAL_OES
-			case 0x8dca: // INT_SAMPLER_2D
-			case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
-			case 0x8b62: // SAMPLER_2D_SHADOW
-				return setValueT1Array;
+		common: {
 
-			case 0x8b60: // SAMPLER_CUBE
-			case 0x8dcc: // INT_SAMPLER_CUBE
-			case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
-			case 0x8dc5: // SAMPLER_CUBE_SHADOW
-				return setValueT6Array;
+			diffuse: { value: new Color( 0xeeeeee ) },
+			opacity: { value: 1.0 },
 
-		}
+			map: { value: null },
+			uvTransform: { value: new Matrix3() },
+			uv2Transform: { value: new Matrix3() },
 
-	}
+			alphaMap: { value: null },
 
-	// --- Uniform Classes ---
+		},
 
-	function SingleUniform( id, activeInfo, addr ) {
+		specularmap: {
 
-		this.id = id;
-		this.addr = addr;
-		this.cache = [];
-		this.setValue = getSingularSetter( activeInfo.type );
+			specularMap: { value: null },
 
-		// this.path = activeInfo.name; // DEBUG
+		},
 
-	}
+		envmap: {
 
-	function PureArrayUniform( id, activeInfo, addr ) {
+			envMap: { value: null },
+			flipEnvMap: { value: - 1 },
+			reflectivity: { value: 1.0 },
+			refractionRatio: { value: 0.98 },
+			maxMipLevel: { value: 0 }
 
-		this.id = id;
-		this.addr = addr;
-		this.cache = [];
-		this.size = activeInfo.size;
-		this.setValue = getPureArraySetter( activeInfo.type );
+		},
 
-		// this.path = activeInfo.name; // DEBUG
+		aomap: {
 
-	}
+			aoMap: { value: null },
+			aoMapIntensity: { value: 1 }
 
-	PureArrayUniform.prototype.updateCache = function ( data ) {
+		},
 
-		var cache = this.cache;
+		lightmap: {
 
-		if ( data instanceof Float32Array && cache.length !== data.length ) {
+			lightMap: { value: null },
+			lightMapIntensity: { value: 1 }
 
-			this.cache = new Float32Array( data.length );
+		},
 
-		}
+		emissivemap: {
 
-		copyArray( cache, data );
+			emissiveMap: { value: null }
 
-	};
+		},
 
-	function StructuredUniform( id ) {
+		bumpmap: {
 
-		this.id = id;
+			bumpMap: { value: null },
+			bumpScale: { value: 1 }
 
-		this.seq = [];
-		this.map = {};
+		},
 
-	}
+		normalmap: {
 
-	StructuredUniform.prototype.setValue = function ( gl, value, textures ) {
+			normalMap: { value: null },
+			normalScale: { value: new Vector2( 1, 1 ) }
 
-		var seq = this.seq;
+		},
 
-		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
+		displacementmap: {
 
-			var u = seq[ i ];
-			u.setValue( gl, value[ u.id ], textures );
+			displacementMap: { value: null },
+			displacementScale: { value: 1 },
+			displacementBias: { value: 0 }
 
-		}
+		},
 
-	};
+		roughnessmap: {
 
-	// --- Top-level ---
+			roughnessMap: { value: null }
 
-	// Parser - builds up the property tree from the path strings
+		},
 
-	var RePathPart = /([\w\d_]+)(\])?(\[|\.)?/g;
+		metalnessmap: {
 
-	// extracts
-	// 	- the identifier (member name or array index)
-	//  - followed by an optional right bracket (found when array index)
-	//  - followed by an optional left bracket or dot (type of subscript)
-	//
-	// Note: These portions can be read in a non-overlapping fashion and
-	// allow straightforward parsing of the hierarchy that WebGL encodes
-	// in the uniform names.
+			metalnessMap: { value: null }
 
-	function addUniform( container, uniformObject ) {
+		},
 
-		container.seq.push( uniformObject );
-		container.map[ uniformObject.id ] = uniformObject;
+		gradientmap: {
 
-	}
+			gradientMap: { value: null }
 
-	function parseUniform( activeInfo, addr, container ) {
+		},
 
-		var path = activeInfo.name,
-			pathLength = path.length;
+		fog: {
 
-		// reset RegExp object, because of the early exit of a previous run
-		RePathPart.lastIndex = 0;
+			fogDensity: { value: 0.00025 },
+			fogNear: { value: 1 },
+			fogFar: { value: 2000 },
+			fogColor: { value: new Color( 0xffffff ) }
 
-		while ( true ) {
+		},
 
-			var match = RePathPart.exec( path ),
-				matchEnd = RePathPart.lastIndex,
+		lights: {
 
-				id = match[ 1 ],
-				idIsIndex = match[ 2 ] === ']',
-				subscript = match[ 3 ];
+			ambientLightColor: { value: [] },
 
-			if ( idIsIndex ) { id = id | 0; } // convert to integer
+			lightProbe: { value: [] },
 
-			if ( subscript === undefined || subscript === '[' && matchEnd + 2 === pathLength ) {
+			directionalLights: { value: [], properties: {
+				direction: {},
+				color: {}
+			} },
 
-				// bare name or "pure" bottom-level array "[0]" suffix
+			directionalLightShadows: { value: [], properties: {
+				shadowBias: {},
+				shadowRadius: {},
+				shadowMapSize: {}
+			} },
 
-				addUniform( container, subscript === undefined ?
-					new SingleUniform( id, activeInfo, addr ) :
-					new PureArrayUniform( id, activeInfo, addr ) );
+			directionalShadowMap: { value: [] },
+			directionalShadowMatrix: { value: [] },
 
-				break;
+			spotLights: { value: [], properties: {
+				color: {},
+				position: {},
+				direction: {},
+				distance: {},
+				coneCos: {},
+				penumbraCos: {},
+				decay: {}
+			} },
 
-			} else {
+			spotLightShadows: { value: [], properties: {
+				shadowBias: {},
+				shadowRadius: {},
+				shadowMapSize: {}
+			} },
 
-				// step into inner node / create it in case it doesn't exist
+			spotShadowMap: { value: [] },
+			spotShadowMatrix: { value: [] },
 
-				var map = container.map, next = map[ id ];
+			pointLights: { value: [], properties: {
+				color: {},
+				position: {},
+				decay: {},
+				distance: {}
+			} },
 
-				if ( next === undefined ) {
+			pointLightShadows: { value: [], properties: {
+				shadowBias: {},
+				shadowRadius: {},
+				shadowMapSize: {},
+				shadowCameraNear: {},
+				shadowCameraFar: {}
+			} },
 
-					next = new StructuredUniform( id );
-					addUniform( container, next );
+			pointShadowMap: { value: [] },
+			pointShadowMatrix: { value: [] },
 
-				}
+			hemisphereLights: { value: [], properties: {
+				direction: {},
+				skyColor: {},
+				groundColor: {}
+			} },
 
-				container = next;
+			// TODO (abelnation): RectAreaLight BRDF data needs to be moved from example to main src
+			rectAreaLights: { value: [], properties: {
+				color: {},
+				position: {},
+				width: {},
+				height: {}
+			} }
 
-			}
+		},
 
-		}
+		points: {
 
-	}
+			diffuse: { value: new Color( 0xeeeeee ) },
+			opacity: { value: 1.0 },
+			size: { value: 1.0 },
+			scale: { value: 1.0 },
+			map: { value: null },
+			alphaMap: { value: null },
+			uvTransform: { value: new Matrix3() }
 
-	// Root Container
+		},
 
-	function WebGLUniforms( gl, program ) {
+		sprite: {
 
-		this.seq = [];
-		this.map = {};
+			diffuse: { value: new Color( 0xeeeeee ) },
+			opacity: { value: 1.0 },
+			center: { value: new Vector2( 0.5, 0.5 ) },
+			rotation: { value: 0.0 },
+			map: { value: null },
+			alphaMap: { value: null },
+			uvTransform: { value: new Matrix3() }
 
-		var n = gl.getProgramParameter( program, 35718 );
+		}
 
-		for ( var i = 0; i < n; ++ i ) {
+	};
 
-			var info = gl.getActiveUniform( program, i ),
-				addr = gl.getUniformLocation( program, info.name );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			parseUniform( info, addr, this );
+	function WebGLAnimation() {
 
-		}
+		var context = null;
+		var isAnimating = false;
+		var animationLoop = null;
 
-	}
-
-	WebGLUniforms.prototype.setValue = function ( gl, name, value, textures ) {
-
-		var u = this.map[ name ];
-
-		if ( u !== undefined ) { u.setValue( gl, value, textures ); }
-
-	};
+		function onAnimationFrame( time, frame ) {
 
-	WebGLUniforms.prototype.setOptional = function ( gl, object, name ) {
+			if ( isAnimating === false ) { return; }
 
-		var v = object[ name ];
+			animationLoop( time, frame );
 
-		if ( v !== undefined ) { this.setValue( gl, name, v ); }
+			context.requestAnimationFrame( onAnimationFrame );
 
-	};
+		}
 
+		return {
 
-	// Static interface
+			start: function () {
 
-	WebGLUniforms.upload = function ( gl, seq, values, textures ) {
+				if ( isAnimating === true ) { return; }
+				if ( animationLoop === null ) { return; }
 
-		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
+				context.requestAnimationFrame( onAnimationFrame );
 
-			var u = seq[ i ],
-				v = values[ u.id ];
+				isAnimating = true;
 
-			if ( v.needsUpdate !== false ) {
+			},
 
-				// note: always updating when .needsUpdate is undefined
-				u.setValue( gl, v.value, textures );
+			stop: function () {
 
-			}
+				isAnimating = false;
 
-		}
+			},
 
-	};
+			setAnimationLoop: function ( callback ) {
 
-	WebGLUniforms.seqWithValue = function ( seq, values ) {
+				animationLoop = callback;
 
-		var r = [];
+			},
 
-		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
+			setContext: function ( value ) {
 
-			var u = seq[ i ];
-			if ( u.id in values ) { r.push( u ); }
+				context = value;
 
-		}
+			}
 
-		return r;
+		};
 
-	};
+	}
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function WebGLShader( gl, type, string ) {
+	function WebGLAttributes( gl, capabilities ) {
 
-		var shader = gl.createShader( type );
+		var isWebGL2 = capabilities.isWebGL2;
 
-		gl.shaderSource( shader, string );
-		gl.compileShader( shader );
+		var buffers = new WeakMap();
 
-		return shader;
+		function createBuffer( attribute, bufferType ) {
 
-	}
+			var array = attribute.array;
+			var usage = attribute.usage;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			var buffer = gl.createBuffer();
 
-	var programIdCount = 0;
+			gl.bindBuffer( bufferType, buffer );
+			gl.bufferData( bufferType, array, usage );
 
-	function addLineNumbers( string ) {
+			attribute.onUploadCallback();
 
-		var lines = string.split( '\n' );
+			var type = 5126;
 
-		for ( var i = 0; i < lines.length; i ++ ) {
+			if ( array instanceof Float32Array ) {
 
-			lines[ i ] = ( i + 1 ) + ': ' + lines[ i ];
+				type = 5126;
 
-		}
+			} else if ( array instanceof Float64Array ) {
 
-		return lines.join( '\n' );
+				console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' );
 
-	}
+			} else if ( array instanceof Uint16Array ) {
 
-	function getEncodingComponents( encoding ) {
+				type = 5123;
 
-		switch ( encoding ) {
+			} else if ( array instanceof Int16Array ) {
 
-			case LinearEncoding:
-				return [ 'Linear', '( value )' ];
-			case sRGBEncoding:
-				return [ 'sRGB', '( value )' ];
-			case RGBEEncoding:
-				return [ 'RGBE', '( value )' ];
-			case RGBM7Encoding:
-				return [ 'RGBM', '( value, 7.0 )' ];
-			case RGBM16Encoding:
-				return [ 'RGBM', '( value, 16.0 )' ];
-			case RGBDEncoding:
-				return [ 'RGBD', '( value, 256.0 )' ];
-			case GammaEncoding:
-				return [ 'Gamma', '( value, float( GAMMA_FACTOR ) )' ];
-			case LogLuvEncoding:
-				return [ 'LogLuv', '( value )' ];
-			default:
-				throw new Error( 'unsupported encoding: ' + encoding );
+				type = 5122;
 
-		}
+			} else if ( array instanceof Uint32Array ) {
 
-	}
+				type = 5125;
 
-	function getShaderErrors( gl, shader, type ) {
+			} else if ( array instanceof Int32Array ) {
 
-		var status = gl.getShaderParameter( shader, 35713 );
-		var log = gl.getShaderInfoLog( shader ).trim();
+				type = 5124;
 
-		if ( status && log === '' ) { return ''; }
+			} else if ( array instanceof Int8Array ) {
 
-		// --enable-privileged-webgl-extension
-		// console.log( '**' + type + '**', gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) );
+				type = 5120;
 
-		var source = gl.getShaderSource( shader );
+			} else if ( array instanceof Uint8Array ) {
 
-		return 'THREE.WebGLShader: gl.getShaderInfoLog() ' + type + '\n' + log + addLineNumbers( source );
+				type = 5121;
 
-	}
+			}
 
-	function getTexelDecodingFunction( functionName, encoding ) {
+			return {
+				buffer: buffer,
+				type: type,
+				bytesPerElement: array.BYTES_PER_ELEMENT,
+				version: attribute.version
+			};
 
-		var components = getEncodingComponents( encoding );
-		return 'vec4 ' + functionName + '( vec4 value ) { return ' + components[ 0 ] + 'ToLinear' + components[ 1 ] + '; }';
+		}
 
-	}
+		function updateBuffer( buffer, attribute, bufferType ) {
 
-	function getTexelEncodingFunction( functionName, encoding ) {
+			var array = attribute.array;
+			var updateRange = attribute.updateRange;
 
-		var components = getEncodingComponents( encoding );
-		return 'vec4 ' + functionName + '( vec4 value ) { return LinearTo' + components[ 0 ] + components[ 1 ] + '; }';
+			gl.bindBuffer( bufferType, buffer );
 
-	}
+			if ( updateRange.count === - 1 ) {
 
-	function getToneMappingFunction( functionName, toneMapping ) {
+				// Not using update ranges
 
-		var toneMappingName;
+				gl.bufferSubData( bufferType, 0, array );
 
-		switch ( toneMapping ) {
+			} else {
 
-			case LinearToneMapping:
-				toneMappingName = 'Linear';
-				break;
+				if ( isWebGL2 ) {
 
-			case ReinhardToneMapping:
-				toneMappingName = 'Reinhard';
-				break;
+					gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
+						array, updateRange.offset, updateRange.count );
 
-			case Uncharted2ToneMapping:
-				toneMappingName = 'Uncharted2';
-				break;
+				} else {
 
-			case CineonToneMapping:
-				toneMappingName = 'OptimizedCineon';
-				break;
+					gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT,
+						array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) );
 
-			case ACESFilmicToneMapping:
-				toneMappingName = 'ACESFilmic';
-				break;
+				}
 
-			default:
-				throw new Error( 'unsupported toneMapping: ' + toneMapping );
+				updateRange.count = - 1; // reset range
+
+			}
 
 		}
 
-		return 'vec3 ' + functionName + '( vec3 color ) { return ' + toneMappingName + 'ToneMapping( color ); }';
+		//
 
-	}
+		function get( attribute ) {
 
-	function generateExtensions( parameters ) {
+			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
 
-		var chunks = [
-			( parameters.extensionDerivatives || parameters.envMapCubeUV || parameters.bumpMap || parameters.tangentSpaceNormalMap || parameters.clearcoatNormalMap || parameters.flatShading || parameters.shaderID === 'physical' ) ? '#extension GL_OES_standard_derivatives : enable' : '',
-			( parameters.extensionFragDepth || parameters.logarithmicDepthBuffer ) && parameters.rendererExtensionFragDepth ? '#extension GL_EXT_frag_depth : enable' : '',
-			( parameters.extensionDrawBuffers && parameters.rendererExtensionDrawBuffers ) ? '#extension GL_EXT_draw_buffers : require' : '',
-			( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ? '#extension GL_EXT_shader_texture_lod : enable' : ''
-		];
+			return buffers.get( attribute );
 
-		return chunks.filter( filterEmptyLine ).join( '\n' );
+		}
 
-	}
+		function remove( attribute ) {
 
-	function generateDefines( defines ) {
+			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
 
-		var chunks = [];
+			var data = buffers.get( attribute );
 
-		for ( var name in defines ) {
+			if ( data ) {
 
-			var value = defines[ name ];
+				gl.deleteBuffer( data.buffer );
 
-			if ( value === false ) { continue; }
+				buffers.delete( attribute );
 
-			chunks.push( '#define ' + name + ' ' + value );
+			}
 
 		}
 
-		return chunks.join( '\n' );
+		function update( attribute, bufferType ) {
 
-	}
+			if ( attribute.isInterleavedBufferAttribute ) { attribute = attribute.data; }
 
-	function fetchAttributeLocations( gl, program ) {
+			var data = buffers.get( attribute );
 
-		var attributes = {};
+			if ( data === undefined ) {
 
-		var n = gl.getProgramParameter( program, 35721 );
+				buffers.set( attribute, createBuffer( attribute, bufferType ) );
 
-		for ( var i = 0; i < n; i ++ ) {
+			} else if ( data.version < attribute.version ) {
 
-			var info = gl.getActiveAttrib( program, i );
-			var name = info.name;
+				updateBuffer( data.buffer, attribute, bufferType );
 
-			// console.log( 'THREE.WebGLProgram: ACTIVE VERTEX ATTRIBUTE:', name, i );
+				data.version = attribute.version;
 
-			attributes[ name ] = gl.getAttribLocation( program, name );
+			}
 
 		}
 
-		return attributes;
-
-	}
-
-	function filterEmptyLine( string ) {
-
-		return string !== '';
-
-	}
+		return {
 
-	function replaceLightNums( string, parameters ) {
+			get: get,
+			remove: remove,
+			update: update
 
-		return string
-			.replace( /NUM_DIR_LIGHTS/g, parameters.numDirLights )
-			.replace( /NUM_SPOT_LIGHTS/g, parameters.numSpotLights )
-			.replace( /NUM_RECT_AREA_LIGHTS/g, parameters.numRectAreaLights )
-			.replace( /NUM_POINT_LIGHTS/g, parameters.numPointLights )
-			.replace( /NUM_HEMI_LIGHTS/g, parameters.numHemiLights )
-			.replace( /NUM_DIR_LIGHT_SHADOWS/g, parameters.numDirLightShadows )
-			.replace( /NUM_SPOT_LIGHT_SHADOWS/g, parameters.numSpotLightShadows )
-			.replace( /NUM_POINT_LIGHT_SHADOWS/g, parameters.numPointLightShadows );
+		};
 
 	}
 
-	function replaceClippingPlaneNums( string, parameters ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-		return string
-			.replace( /NUM_CLIPPING_PLANES/g, parameters.numClippingPlanes )
-			.replace( /UNION_CLIPPING_PLANES/g, ( parameters.numClippingPlanes - parameters.numClipIntersection ) );
+	// PlaneGeometry
 
-	}
+	function PlaneGeometry( width, height, widthSegments, heightSegments ) {
 
-	// Resolve Includes
+		Geometry.call( this );
 
-	var includePattern = /^[ \t]*#include +<([\w\d./]+)>/gm;
+		this.type = 'PlaneGeometry';
 
-	function resolveIncludes( string ) {
+		this.parameters = {
+			width: width,
+			height: height,
+			widthSegments: widthSegments,
+			heightSegments: heightSegments
+		};
 
-		return string.replace( includePattern, includeReplacer );
+		this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
+		this.mergeVertices();
 
 	}
 
-	function includeReplacer( match, include ) {
+	PlaneGeometry.prototype = Object.create( Geometry.prototype );
+	PlaneGeometry.prototype.constructor = PlaneGeometry;
 
-		var string = ShaderChunk[ include ];
+	// PlaneBufferGeometry
 
-		if ( string === undefined ) {
+	function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
 
-			throw new Error( 'Can not resolve #include <' + include + '>' );
+		BufferGeometry.call( this );
 
-		}
+		this.type = 'PlaneBufferGeometry';
 
-		return resolveIncludes( string );
+		this.parameters = {
+			width: width,
+			height: height,
+			widthSegments: widthSegments,
+			heightSegments: heightSegments
+		};
 
-	}
+		width = width || 1;
+		height = height || 1;
 
-	// Unroll Loops
+		var width_half = width / 2;
+		var height_half = height / 2;
 
-	var deprecatedUnrollLoopPattern = /#pragma unroll_loop[\s]+?for \( int i \= (\d+)\; i < (\d+)\; i \+\+ \) \{([\s\S]+?)(?=\})\}/g;
-	var unrollLoopPattern = /#pragma unroll_loop_start[\s]+?for \( int i \= (\d+)\; i < (\d+)\; i \+\+ \) \{([\s\S]+?)(?=\})\}[\s]+?#pragma unroll_loop_end/g;
+		var gridX = Math.floor( widthSegments ) || 1;
+		var gridY = Math.floor( heightSegments ) || 1;
 
-	function unrollLoops( string ) {
+		var gridX1 = gridX + 1;
+		var gridY1 = gridY + 1;
 
-		return string
-			.replace( unrollLoopPattern, loopReplacer )
-			.replace( deprecatedUnrollLoopPattern, deprecatedLoopReplacer );
+		var segment_width = width / gridX;
+		var segment_height = height / gridY;
 
-	}
+		var ix, iy;
 
-	function deprecatedLoopReplacer( match, start, end, snippet ) {
+		// buffers
 
-		console.warn( 'WebGLProgram: #pragma unroll_loop shader syntax is deprecated. Please use #pragma unroll_loop_start syntax instead.' );
-		return loopReplacer( match, start, end, snippet );
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-	}
+		// generate vertices, normals and uvs
 
-	function loopReplacer( match, start, end, snippet ) {
+		for ( iy = 0; iy < gridY1; iy ++ ) {
 
-		var string = '';
+			var y = iy * segment_height - height_half;
 
-		for ( var i = parseInt( start ); i < parseInt( end ); i ++ ) {
+			for ( ix = 0; ix < gridX1; ix ++ ) {
 
-			string += snippet
-				.replace( /\[ i \]/g, '[ ' + i + ' ]' )
-				.replace( /UNROLLED_LOOP_INDEX/g, i );
+				var x = ix * segment_width - width_half;
 
-		}
+				vertices.push( x, - y, 0 );
 
-		return string;
+				normals.push( 0, 0, 1 );
 
-	}
+				uvs.push( ix / gridX );
+				uvs.push( 1 - ( iy / gridY ) );
 
-	//
+			}
 
-	function generatePrecision( parameters ) {
+		}
 
-		var precisionstring = "precision " + parameters.precision + " float;\nprecision " + parameters.precision + " int;";
+		// indices
 
-		if ( parameters.precision === "highp" ) {
+		for ( iy = 0; iy < gridY; iy ++ ) {
 
-			precisionstring += "\n#define HIGH_PRECISION";
+			for ( ix = 0; ix < gridX; ix ++ ) {
 
-		} else if ( parameters.precision === "mediump" ) {
+				var a = ix + gridX1 * iy;
+				var b = ix + gridX1 * ( iy + 1 );
+				var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
+				var d = ( ix + 1 ) + gridX1 * iy;
 
-			precisionstring += "\n#define MEDIUM_PRECISION";
+				// faces
 
-		} else if ( parameters.precision === "lowp" ) {
+				indices.push( a, b, d );
+				indices.push( b, c, d );
 
-			precisionstring += "\n#define LOW_PRECISION";
+			}
 
 		}
 
-		return precisionstring;
+		// build geometry
+
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
 	}
 
-	function generateShadowMapTypeDefine( parameters ) {
+	PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
 
-		var shadowMapTypeDefine = 'SHADOWMAP_TYPE_BASIC';
+	var alphamap_fragment = "#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, vUv ).g;\n#endif";
 
-		if ( parameters.shadowMapType === PCFShadowMap ) {
+	var alphamap_pars_fragment = "#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif";
 
-			shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF';
+	var alphatest_fragment = "#ifdef ALPHATEST\n\tif ( diffuseColor.a < ALPHATEST ) discard;\n#endif";
 
-		} else if ( parameters.shadowMapType === PCFSoftShadowMap ) {
+	var aomap_fragment = "#ifdef USE_AOMAP\n\tfloat ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;\n\treflectedLight.indirectDiffuse *= ambientOcclusion;\n\t#if defined( USE_ENVMAP ) && defined( STANDARD )\n\t\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );\n\t#endif\n#endif";
 
-			shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF_SOFT';
+	var aomap_pars_fragment = "#ifdef USE_AOMAP\n\tuniform sampler2D aoMap;\n\tuniform float aoMapIntensity;\n#endif";
 
-		} else if ( parameters.shadowMapType === VSMShadowMap ) {
+	var begin_vertex = "vec3 transformed = vec3( position );";
 
-			shadowMapTypeDefine = 'SHADOWMAP_TYPE_VSM';
+	var beginnormal_vertex = "vec3 objectNormal = vec3( normal );\n#ifdef USE_TANGENT\n\tvec3 objectTangent = vec3( tangent.xyz );\n#endif";
 
-		}
+	var bsdfs = "vec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\treturn vec2( -1.04, 1.04 ) * a004 + r.zw;\n}\nfloat punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n#if defined ( PHYSICALLY_CORRECT_LIGHTS )\n\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\tif( cutoffDistance > 0.0 ) {\n\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t}\n\treturn distanceFalloff;\n#else\n\tif( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\treturn pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t}\n\treturn 1.0;\n#endif\n}\nvec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n}\nvec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );\n\treturn ( 1.0 - specularColor ) * fresnel + specularColor;\n}\nvec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );\n\tvec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;\n\treturn Fr * fresnel + F0;\n}\nfloat G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\tfloat gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\treturn 1.0 / ( gl * gv );\n}\nfloat G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\nvec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat alpha = pow2( roughness );\n\tvec3 halfDir = normalize( incidentLight.direction + viewDir );\n\tfloat dotNL = saturate( dot( normal, incidentLight.direction ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\tfloat D = D_GGX( alpha, dotNH );\n\treturn F * ( G * D );\n}\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE  = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS  = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 );\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\treturn vec3( result );\n}\nvec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\treturn specularColor * brdf.x + brdf.y;\n}\nvoid BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tvec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\tvec3 FssEss = F * brdf.x + brdf.y;\n\tfloat Ess = brdf.x + brdf.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619;\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\nfloat G_BlinnPhong_Implicit( ) {\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );\n\tfloat dotNH = saturate( dot( geometry.normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_BlinnPhong_Implicit( );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n}\nfloat GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\treturn ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );\n}\nfloat BlinnExponentToGGXRoughness( const in float blinnExponent ) {\n\treturn sqrt( 2.0 / ( blinnExponent + 2.0 ) );\n}\n#if defined( USE_SHEEN )\nfloat D_Charlie(float roughness, float NoH) {\n\tfloat invAlpha  = 1.0 / roughness;\n\tfloat cos2h = NoH * NoH;\n\tfloat sin2h = max(1.0 - cos2h, 0.0078125);\treturn (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);\n}\nfloat V_Neubelt(float NoV, float NoL) {\n\treturn saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));\n}\nvec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {\n\tvec3 N = geometry.normal;\n\tvec3 V = geometry.viewDir;\n\tvec3 H = normalize( V + L );\n\tfloat dotNH = saturate( dot( N, H ) );\n\treturn specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );\n}\n#endif";
 
-		return shadowMapTypeDefine;
+	var bumpmap_pars_fragment = "#ifdef USE_BUMPMAP\n\tuniform sampler2D bumpMap;\n\tuniform float bumpScale;\n\tvec2 dHdxy_fwd() {\n\t\tvec2 dSTdx = dFdx( vUv );\n\t\tvec2 dSTdy = dFdy( vUv );\n\t\tfloat Hll = bumpScale * texture2D( bumpMap, vUv ).x;\n\t\tfloat dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;\n\t\tfloat dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;\n\t\treturn vec2( dBx, dBy );\n\t}\n\tvec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {\n\t\tvec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );\n\t\tvec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );\n\t\tvec3 vN = surf_norm;\n\t\tvec3 R1 = cross( vSigmaY, vN );\n\t\tvec3 R2 = cross( vN, vSigmaX );\n\t\tfloat fDet = dot( vSigmaX, R1 );\n\t\tfDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\tvec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );\n\t\treturn normalize( abs( fDet ) * surf_norm - vGrad );\n\t}\n#endif";
 
-	}
+	var clipping_planes_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvec4 plane;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) {\n\t\tplane = clippingPlanes[ i ];\n\t\tif ( dot( vClipPosition, plane.xyz ) > plane.w ) discard;\n\t}\n\t#pragma unroll_loop_end\n\t#if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES\n\t\tbool clipped = true;\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) {\n\t\t\tplane = clippingPlanes[ i ];\n\t\t\tclipped = ( dot( vClipPosition, plane.xyz ) > plane.w ) && clipped;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t\tif ( clipped ) discard;\n\t#endif\n#endif";
 
-	function generateEnvMapTypeDefine( parameters ) {
+	var clipping_planes_pars_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n\tuniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];\n#endif";
 
-		var envMapTypeDefine = 'ENVMAP_TYPE_CUBE';
+	var clipping_planes_pars_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n#endif";
 
-		if ( parameters.envMap ) {
+	var clipping_planes_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvClipPosition = - mvPosition.xyz;\n#endif";
 
-			switch ( parameters.envMapMode ) {
+	var color_fragment = "#ifdef USE_COLOR\n\tdiffuseColor.rgb *= vColor;\n#endif";
 
-				case CubeReflectionMapping:
-				case CubeRefractionMapping:
-					envMapTypeDefine = 'ENVMAP_TYPE_CUBE';
-					break;
+	var color_pars_fragment = "#ifdef USE_COLOR\n\tvarying vec3 vColor;\n#endif";
 
-				case CubeUVReflectionMapping:
-				case CubeUVRefractionMapping:
-					envMapTypeDefine = 'ENVMAP_TYPE_CUBE_UV';
-					break;
+	var color_pars_vertex = "#ifdef USE_COLOR\n\tvarying vec3 vColor;\n#endif";
 
-				case EquirectangularReflectionMapping:
-				case EquirectangularRefractionMapping:
-					envMapTypeDefine = 'ENVMAP_TYPE_EQUIREC';
-					break;
+	var color_vertex = "#ifdef USE_COLOR\n\tvColor.xyz = color.xyz;\n#endif";
 
-				case SphericalReflectionMapping:
-					envMapTypeDefine = 'ENVMAP_TYPE_SPHERE';
-					break;
+	var common = "#define PI 3.14159265359\n#define PI2 6.28318530718\n#define PI_HALF 1.5707963267949\n#define RECIPROCAL_PI 0.31830988618\n#define RECIPROCAL_PI2 0.15915494\n#define LOG2 1.442695\n#define EPSILON 1e-6\n#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\n#define whiteComplement(a) ( 1.0 - saturate( a ) )\nfloat pow2( const in float x ) { return x*x; }\nfloat pow3( const in float x ) { return x*x*x; }\nfloat pow4( const in float x ) { float x2 = x*x; return x2*x2; }\nfloat average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }\nhighp float rand( const in vec2 uv ) {\n\tconst highp float a = 12.9898, b = 78.233, c = 43758.5453;\n\thighp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n\treturn fract(sin(sn) * c);\n}\n#ifdef HIGH_PRECISION\n\tfloat precisionSafeLength( vec3 v ) { return length( v ); }\n#else\n\tfloat max3( vec3 v ) { return max( max( v.x, v.y ), v.z ); }\n\tfloat precisionSafeLength( vec3 v ) {\n\t\tfloat maxComponent = max3( abs( v ) );\n\t\treturn length( v / maxComponent ) * maxComponent;\n\t}\n#endif\nstruct IncidentLight {\n\tvec3 color;\n\tvec3 direction;\n\tbool visible;\n};\nstruct ReflectedLight {\n\tvec3 directDiffuse;\n\tvec3 directSpecular;\n\tvec3 indirectDiffuse;\n\tvec3 indirectSpecular;\n};\nstruct GeometricContext {\n\tvec3 position;\n\tvec3 normal;\n\tvec3 viewDir;\n#ifdef CLEARCOAT\n\tvec3 clearcoatNormal;\n#endif\n};\nvec3 transformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );\n}\nvec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );\n}\nvec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\tfloat distance = dot( planeNormal, point - pointOnPlane );\n\treturn - distance * planeNormal + point;\n}\nfloat sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn sign( dot( point - pointOnPlane, planeNormal ) );\n}\nvec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;\n}\nmat3 transposeMat3( const in mat3 m ) {\n\tmat3 tmp;\n\ttmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n\ttmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n\ttmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n\treturn tmp;\n}\nfloat linearToRelativeLuminance( const in vec3 color ) {\n\tvec3 weights = vec3( 0.2126, 0.7152, 0.0722 );\n\treturn dot( weights, color.rgb );\n}\nbool isPerspectiveMatrix( mat4 m ) {\n  return m[ 2 ][ 3 ] == - 1.0;\n}";
 
-			}
+	var cube_uv_reflection_fragment = "#ifdef ENVMAP_TYPE_CUBE_UV\n#define cubeUV_maxMipLevel 8.0\n#define cubeUV_minMipLevel 4.0\n#define cubeUV_maxTileSize 256.0\n#define cubeUV_minTileSize 16.0\nfloat getFace(vec3 direction) {\n    vec3 absDirection = abs(direction);\n    float face = -1.0;\n    if (absDirection.x > absDirection.z) {\n      if (absDirection.x > absDirection.y)\n        face = direction.x > 0.0 ? 0.0 : 3.0;\n      else\n        face = direction.y > 0.0 ? 1.0 : 4.0;\n    } else {\n      if (absDirection.z > absDirection.y)\n        face = direction.z > 0.0 ? 2.0 : 5.0;\n      else\n        face = direction.y > 0.0 ? 1.0 : 4.0;\n    }\n    return face;\n}\nvec2 getUV(vec3 direction, float face) {\n    vec2 uv;\n    if (face == 0.0) {\n      uv = vec2(-direction.z, direction.y) / abs(direction.x);\n    } else if (face == 1.0) {\n      uv = vec2(direction.x, -direction.z) / abs(direction.y);\n    } else if (face == 2.0) {\n      uv = direction.xy / abs(direction.z);\n    } else if (face == 3.0) {\n      uv = vec2(direction.z, direction.y) / abs(direction.x);\n    } else if (face == 4.0) {\n      uv = direction.xz / abs(direction.y);\n    } else {\n      uv = vec2(-direction.x, direction.y) / abs(direction.z);\n    }\n    return 0.5 * (uv + 1.0);\n}\nvec3 bilinearCubeUV(sampler2D envMap, vec3 direction, float mipInt) {\n  float face = getFace(direction);\n  float filterInt = max(cubeUV_minMipLevel - mipInt, 0.0);\n  mipInt = max(mipInt, cubeUV_minMipLevel);\n  float faceSize = exp2(mipInt);\n  float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize);\n  vec2 uv = getUV(direction, face) * (faceSize - 1.0);\n  vec2 f = fract(uv);\n  uv += 0.5 - f;\n  if (face > 2.0) {\n    uv.y += faceSize;\n    face -= 3.0;\n  }\n  uv.x += face * faceSize;\n  if(mipInt < cubeUV_maxMipLevel){\n    uv.y += 2.0 * cubeUV_maxTileSize;\n  }\n  uv.y += filterInt * 2.0 * cubeUV_minTileSize;\n  uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize);\n  uv *= texelSize;\n  vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.x += texelSize;\n  vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.y += texelSize;\n  vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  uv.x -= texelSize;\n  vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n  vec3 tm = mix(tl, tr, f.x);\n  vec3 bm = mix(bl, br, f.x);\n  return mix(tm, bm, f.y);\n}\n#define r0 1.0\n#define v0 0.339\n#define m0 -2.0\n#define r1 0.8\n#define v1 0.276\n#define m1 -1.0\n#define r4 0.4\n#define v4 0.046\n#define m4 2.0\n#define r5 0.305\n#define v5 0.016\n#define m5 3.0\n#define r6 0.21\n#define v6 0.0038\n#define m6 4.0\nfloat roughnessToMip(float roughness) {\n  float mip = 0.0;\n  if (roughness >= r1) {\n    mip = (r0 - roughness) * (m1 - m0) / (r0 - r1) + m0;\n  } else if (roughness >= r4) {\n    mip = (r1 - roughness) * (m4 - m1) / (r1 - r4) + m1;\n  } else if (roughness >= r5) {\n    mip = (r4 - roughness) * (m5 - m4) / (r4 - r5) + m4;\n  } else if (roughness >= r6) {\n    mip = (r5 - roughness) * (m6 - m5) / (r5 - r6) + m5;\n  } else {\n    mip = -2.0 * log2(1.16 * roughness);  }\n  return mip;\n}\nvec4 textureCubeUV(sampler2D envMap, vec3 sampleDir, float roughness) {\n  float mip = clamp(roughnessToMip(roughness), m0, cubeUV_maxMipLevel);\n  float mipF = fract(mip);\n  float mipInt = floor(mip);\n  vec3 color0 = bilinearCubeUV(envMap, sampleDir, mipInt);\n  if (mipF == 0.0) {\n    return vec4(color0, 1.0);\n  } else {\n    vec3 color1 = bilinearCubeUV(envMap, sampleDir, mipInt + 1.0);\n    return vec4(mix(color0, color1, mipF), 1.0);\n  }\n}\n#endif";
 
-		}
+	var defaultnormal_vertex = "vec3 transformedNormal = objectNormal;\n#ifdef USE_INSTANCING\n\tmat3 m = mat3( instanceMatrix );\n\ttransformedNormal /= vec3( dot( m[ 0 ], m[ 0 ] ), dot( m[ 1 ], m[ 1 ] ), dot( m[ 2 ], m[ 2 ] ) );\n\ttransformedNormal = m * transformedNormal;\n#endif\ntransformedNormal = normalMatrix * transformedNormal;\n#ifdef FLIP_SIDED\n\ttransformedNormal = - transformedNormal;\n#endif\n#ifdef USE_TANGENT\n\tvec3 transformedTangent = ( modelViewMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#ifdef FLIP_SIDED\n\t\ttransformedTangent = - transformedTangent;\n\t#endif\n#endif";
 
-		return envMapTypeDefine;
+	var displacementmap_pars_vertex = "#ifdef USE_DISPLACEMENTMAP\n\tuniform sampler2D displacementMap;\n\tuniform float displacementScale;\n\tuniform float displacementBias;\n#endif";
 
-	}
+	var displacementmap_vertex = "#ifdef USE_DISPLACEMENTMAP\n\ttransformed += normalize( objectNormal ) * ( texture2D( displacementMap, vUv ).x * displacementScale + displacementBias );\n#endif";
 
-	function generateEnvMapModeDefine( parameters ) {
+	var emissivemap_fragment = "#ifdef USE_EMISSIVEMAP\n\tvec4 emissiveColor = texture2D( emissiveMap, vUv );\n\temissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;\n\ttotalEmissiveRadiance *= emissiveColor.rgb;\n#endif";
 
-		var envMapModeDefine = 'ENVMAP_MODE_REFLECTION';
+	var emissivemap_pars_fragment = "#ifdef USE_EMISSIVEMAP\n\tuniform sampler2D emissiveMap;\n#endif";
 
-		if ( parameters.envMap ) {
+	var encodings_fragment = "gl_FragColor = linearToOutputTexel( gl_FragColor );";
 
-			switch ( parameters.envMapMode ) {
+	var encodings_pars_fragment = "\nvec4 LinearToLinear( in vec4 value ) {\n\treturn value;\n}\nvec4 GammaToLinear( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( gammaFactor ) ), value.a );\n}\nvec4 LinearToGamma( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( 1.0 / gammaFactor ) ), value.a );\n}\nvec4 sRGBToLinear( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.a );\n}\nvec4 LinearTosRGB( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );\n}\nvec4 RGBEToLinear( in vec4 value ) {\n\treturn vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );\n}\nvec4 LinearToRGBE( in vec4 value ) {\n\tfloat maxComponent = max( max( value.r, value.g ), value.b );\n\tfloat fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );\n\treturn vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );\n}\nvec4 RGBMToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * value.a * maxRange, 1.0 );\n}\nvec4 LinearToRGBM( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat M = clamp( maxRGB / maxRange, 0.0, 1.0 );\n\tM = ceil( M * 255.0 ) / 255.0;\n\treturn vec4( value.rgb / ( M * maxRange ), M );\n}\nvec4 RGBDToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );\n}\nvec4 LinearToRGBD( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat D = max( maxRange / maxRGB, 1.0 );\n\tD = clamp( floor( D ) / 255.0, 0.0, 1.0 );\n\treturn vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );\n}\nconst mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );\nvec4 LinearToLogLuv( in vec4 value )  {\n\tvec3 Xp_Y_XYZp = cLogLuvM * value.rgb;\n\tXp_Y_XYZp = max( Xp_Y_XYZp, vec3( 1e-6, 1e-6, 1e-6 ) );\n\tvec4 vResult;\n\tvResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;\n\tfloat Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;\n\tvResult.w = fract( Le );\n\tvResult.z = ( Le - ( floor( vResult.w * 255.0 ) ) / 255.0 ) / 255.0;\n\treturn vResult;\n}\nconst mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );\nvec4 LogLuvToLinear( in vec4 value ) {\n\tfloat Le = value.z * 255.0 + value.w;\n\tvec3 Xp_Y_XYZp;\n\tXp_Y_XYZp.y = exp2( ( Le - 127.0 ) / 2.0 );\n\tXp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;\n\tXp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;\n\tvec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb;\n\treturn vec4( max( vRGB, 0.0 ), 1.0 );\n}";
 
-				case CubeRefractionMapping:
-				case EquirectangularRefractionMapping:
-					envMapModeDefine = 'ENVMAP_MODE_REFRACTION';
-					break;
+	var envmap_fragment = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvec3 cameraToFrag;\n\t\t\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t}  else {\n\t\t\tcameraToFrag = normalize( vWorldPosition - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvec3 reflectVec = reflect( cameraToFrag, worldNormal );\n\t\t#else\n\t\t\tvec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );\n\t\t#endif\n\t#else\n\t\tvec3 reflectVec = vReflect;\n\t#endif\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tvec4 envColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );\n\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\tvec4 envColor = textureCubeUV( envMap, reflectVec, 0.0 );\n\t#elif defined( ENVMAP_TYPE_EQUIREC )\n\t\tvec2 sampleUV;\n\t\treflectVec = normalize( reflectVec );\n\t\tsampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\t\tsampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;\n\t\tvec4 envColor = texture2D( envMap, sampleUV );\n\t#elif defined( ENVMAP_TYPE_SPHERE )\n\t\treflectVec = normalize( reflectVec );\n\t\tvec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );\n\t\tvec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );\n\t#else\n\t\tvec4 envColor = vec4( 0.0 );\n\t#endif\n\t#ifndef ENVMAP_TYPE_CUBE_UV\n\t\tenvColor = envMapTexelToLinear( envColor );\n\t#endif\n\t#ifdef ENVMAP_BLENDING_MULTIPLY\n\t\toutgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_MIX )\n\t\toutgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_ADD )\n\t\toutgoingLight += envColor.xyz * specularStrength * reflectivity;\n\t#endif\n#endif";
 
-			}
+	var envmap_common_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float envMapIntensity;\n\tuniform float flipEnvMap;\n\tuniform int maxMipLevel;\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tuniform samplerCube envMap;\n\t#else\n\t\tuniform sampler2D envMap;\n\t#endif\n\t\n#endif";
 
-		}
+	var envmap_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float reflectivity;\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\tvarying vec3 vWorldPosition;\n\t\tuniform float refractionRatio;\n\t#else\n\t\tvarying vec3 vReflect;\n\t#endif\n#endif";
 
-		return envMapModeDefine;
+	var envmap_pars_vertex = "#ifdef USE_ENVMAP\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) ||defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\t\n\t\tvarying vec3 vWorldPosition;\n\t#else\n\t\tvarying vec3 vReflect;\n\t\tuniform float refractionRatio;\n\t#endif\n#endif";
 
-	}
+	var envmap_vertex = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvWorldPosition = worldPosition.xyz;\n\t#else\n\t\tvec3 cameraToVertex;\n\t\tif ( isOrthographic ) { \n\t\t\tcameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToVertex = normalize( worldPosition.xyz - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvReflect = reflect( cameraToVertex, worldNormal );\n\t\t#else\n\t\t\tvReflect = refract( cameraToVertex, worldNormal, refractionRatio );\n\t\t#endif\n\t#endif\n#endif";
 
-	function generateEnvMapBlendingDefine( parameters ) {
+	var fog_vertex = "#ifdef USE_FOG\n\tfogDepth = -mvPosition.z;\n#endif";
 
-		var envMapBlendingDefine = 'ENVMAP_BLENDING_NONE';
+	var fog_pars_vertex = "#ifdef USE_FOG\n\tvarying float fogDepth;\n#endif";
 
-		if ( parameters.envMap ) {
+	var fog_fragment = "#ifdef USE_FOG\n\t#ifdef FOG_EXP2\n\t\tfloat fogFactor = 1.0 - exp( - fogDensity * fogDensity * fogDepth * fogDepth );\n\t#else\n\t\tfloat fogFactor = smoothstep( fogNear, fogFar, fogDepth );\n\t#endif\n\tgl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );\n#endif";
 
-			switch ( parameters.combine ) {
+	var fog_pars_fragment = "#ifdef USE_FOG\n\tuniform vec3 fogColor;\n\tvarying float fogDepth;\n\t#ifdef FOG_EXP2\n\t\tuniform float fogDensity;\n\t#else\n\t\tuniform float fogNear;\n\t\tuniform float fogFar;\n\t#endif\n#endif";
 
-				case MultiplyOperation:
-					envMapBlendingDefine = 'ENVMAP_BLENDING_MULTIPLY';
-					break;
+	var gradientmap_pars_fragment = "#ifdef USE_GRADIENTMAP\n\tuniform sampler2D gradientMap;\n#endif\nvec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {\n\tfloat dotNL = dot( normal, lightDirection );\n\tvec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );\n\t#ifdef USE_GRADIENTMAP\n\t\treturn texture2D( gradientMap, coord ).rgb;\n\t#else\n\t\treturn ( coord.x < 0.7 ) ? vec3( 0.7 ) : vec3( 1.0 );\n\t#endif\n}";
 
-				case MixOperation:
-					envMapBlendingDefine = 'ENVMAP_BLENDING_MIX';
-					break;
+	var lightmap_fragment = "#ifdef USE_LIGHTMAP\n\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\treflectedLight.indirectDiffuse += PI * lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n#endif";
 
-				case AddOperation:
-					envMapBlendingDefine = 'ENVMAP_BLENDING_ADD';
-					break;
+	var lightmap_pars_fragment = "#ifdef USE_LIGHTMAP\n\tuniform sampler2D lightMap;\n\tuniform float lightMapIntensity;\n#endif";
 
-			}
+	var lights_lambert_vertex = "vec3 diffuse = vec3( 1.0 );\nGeometricContext geometry;\ngeometry.position = mvPosition.xyz;\ngeometry.normal = normalize( transformedNormal );\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );\nGeometricContext backGeometry;\nbackGeometry.position = geometry.position;\nbackGeometry.normal = -geometry.normal;\nbackGeometry.viewDir = geometry.viewDir;\nvLightFront = vec3( 0.0 );\nvIndirectFront = vec3( 0.0 );\n#ifdef DOUBLE_SIDED\n\tvLightBack = vec3( 0.0 );\n\tvIndirectBack = vec3( 0.0 );\n#endif\nIncidentLight directLight;\nfloat dotNL;\nvec3 directLightColor_Diffuse;\nvIndirectFront += getAmbientLightIrradiance( ambientLightColor );\nvIndirectFront += getLightProbeIrradiance( lightProbe, geometry );\n#ifdef DOUBLE_SIDED\n\tvIndirectBack += getAmbientLightIrradiance( ambientLightColor );\n\tvIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry );\n#endif\n#if NUM_POINT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tgetPointDirectLightIrradiance( pointLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tgetSpotDirectLightIrradiance( spotLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_DIR_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tgetDirectionalDirectLightIrradiance( directionalLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\tvIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry );\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif";
 
-		}
+	var lights_pars_begin = "uniform bool receiveShadow;\nuniform vec3 ambientLightColor;\nuniform vec3 lightProbe[ 9 ];\nvec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {\n\tfloat x = normal.x, y = normal.y, z = normal.z;\n\tvec3 result = shCoefficients[ 0 ] * 0.886227;\n\tresult += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;\n\tresult += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;\n\tresult += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;\n\tresult += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;\n\tresult += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;\n\tresult += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );\n\tresult += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;\n\tresult += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );\n\treturn result;\n}\nvec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in GeometricContext geometry ) {\n\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\tvec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );\n\treturn irradiance;\n}\nvec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {\n\tvec3 irradiance = ambientLightColor;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treturn irradiance;\n}\n#if NUM_DIR_LIGHTS > 0\n\tstruct DirectionalLight {\n\t\tvec3 direction;\n\t\tvec3 color;\n\t};\n\tuniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tdirectLight.color = directionalLight.color;\n\t\tdirectLight.direction = directionalLight.direction;\n\t\tdirectLight.visible = true;\n\t}\n#endif\n#if NUM_POINT_LIGHTS > 0\n\tstruct PointLight {\n\t\tvec3 position;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t};\n\tuniform PointLight pointLights[ NUM_POINT_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tvec3 lVector = pointLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tdirectLight.color = pointLight.color;\n\t\tdirectLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );\n\t\tdirectLight.visible = ( directLight.color != vec3( 0.0 ) );\n\t}\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\tstruct SpotLight {\n\t\tvec3 position;\n\t\tvec3 direction;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t\tfloat coneCos;\n\t\tfloat penumbraCos;\n\t};\n\tuniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\tvoid getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight  ) {\n\t\tvec3 lVector = spotLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tfloat angleCos = dot( directLight.direction, spotLight.direction );\n\t\tif ( angleCos > spotLight.coneCos ) {\n\t\t\tfloat spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n\t\t\tdirectLight.color = spotLight.color;\n\t\t\tdirectLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );\n\t\t\tdirectLight.visible = true;\n\t\t} else {\n\t\t\tdirectLight.color = vec3( 0.0 );\n\t\t\tdirectLight.visible = false;\n\t\t}\n\t}\n#endif\n#if NUM_RECT_AREA_LIGHTS > 0\n\tstruct RectAreaLight {\n\t\tvec3 color;\n\t\tvec3 position;\n\t\tvec3 halfWidth;\n\t\tvec3 halfHeight;\n\t};\n\tuniform sampler2D ltc_1;\tuniform sampler2D ltc_2;\n\tuniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ];\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\tstruct HemisphereLight {\n\t\tvec3 direction;\n\t\tvec3 skyColor;\n\t\tvec3 groundColor;\n\t};\n\tuniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];\n\tvec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {\n\t\tfloat dotNL = dot( geometry.normal, hemiLight.direction );\n\t\tfloat hemiDiffuseWeight = 0.5 * dotNL + 0.5;\n\t\tvec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tirradiance *= PI;\n\t\t#endif\n\t\treturn irradiance;\n\t}\n#endif";
 
-		return envMapBlendingDefine;
+	var envmap_physical_pars_fragment = "#if defined( USE_ENVMAP )\n\t#ifdef ENVMAP_MODE_REFRACTION\n\t\tuniform float refractionRatio;\n\t#endif\n\tvec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {\n\t\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, worldNormal, 1.0 );\n\t\t#else\n\t\t\tvec4 envMapColor = vec4( 0.0 );\n\t\t#endif\n\t\treturn PI * envMapColor.rgb * envMapIntensity;\n\t}\n\tfloat getSpecularMIPLevel( const in float roughness, const in int maxMIPLevel ) {\n\t\tfloat maxMIPLevelScalar = float( maxMIPLevel );\n\t\tfloat sigma = PI * roughness * roughness / ( 1.0 + roughness );\n\t\tfloat desiredMIPLevel = maxMIPLevelScalar + log2( sigma );\n\t\treturn clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );\n\t}\n\tvec3 getLightProbeIndirectRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in int maxMIPLevel ) {\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t  vec3 reflectVec = reflect( -viewDir, normal );\n\t\t  reflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );\n\t\t#else\n\t\t  vec3 reflectVec = refract( -viewDir, normal, refractionRatio );\n\t\t#endif\n\t\treflectVec = inverseTransformDirection( reflectVec, viewMatrix );\n\t\tfloat specularMIPLevel = getSpecularMIPLevel( roughness, maxMIPLevel );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );\n\t\t#elif defined( ENVMAP_TYPE_EQUIREC )\n\t\t\tvec2 sampleUV;\n\t\t\tsampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\t\t\tsampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_SPHERE )\n\t\t\tvec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#endif\n\t\treturn envMapColor.rgb * envMapIntensity;\n\t}\n#endif";
 
-	}
+	var lights_toon_fragment = "ToonMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;";
 
-	function WebGLProgram( renderer, cacheKey, parameters ) {
+	var lights_toon_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct ToonMaterial {\n\tvec3\tdiffuseColor;\n\tvec3\tspecularColor;\n\tfloat\tspecularShininess;\n\tfloat\tspecularStrength;\n};\nvoid RE_Direct_Toon( const in IncidentLight directLight, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\tvec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_Toon( const in vec3 irradiance, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_Toon\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Toon\n#define Material_LightProbeLOD( material )\t(0)";
 
-		var gl = renderer.getContext();
+	var lights_phong_fragment = "BlinnPhongMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;";
 
-		var defines = parameters.defines;
+	var lights_phong_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct BlinnPhongMaterial {\n\tvec3\tdiffuseColor;\n\tvec3\tspecularColor;\n\tfloat\tspecularShininess;\n\tfloat\tspecularStrength;\n};\nvoid RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_BlinnPhong\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_BlinnPhong\n#define Material_LightProbeLOD( material )\t(0)";
 
-		var vertexShader = parameters.vertexShader;
-		var fragmentShader = parameters.fragmentShader;
-		var shadowMapTypeDefine = generateShadowMapTypeDefine( parameters );
-		var envMapTypeDefine = generateEnvMapTypeDefine( parameters );
-		var envMapModeDefine = generateEnvMapModeDefine( parameters );
-		var envMapBlendingDefine = generateEnvMapBlendingDefine( parameters );
+	var lights_physical_fragment = "PhysicalMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );\nvec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );\nfloat geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );\nmaterial.specularRoughness = max( roughnessFactor, 0.0525 );material.specularRoughness += geometryRoughness;\nmaterial.specularRoughness = min( material.specularRoughness, 1.0 );\n#ifdef REFLECTIVITY\n\tmaterial.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );\n#else\n\tmaterial.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );\n#endif\n#ifdef CLEARCOAT\n\tmaterial.clearcoat = clearcoat;\n\tmaterial.clearcoatRoughness = clearcoatRoughness;\n\t#ifdef USE_CLEARCOATMAP\n\t\tmaterial.clearcoat *= texture2D( clearcoatMap, vUv ).x;\n\t#endif\n\t#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\t\tmaterial.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vUv ).y;\n\t#endif\n\tmaterial.clearcoat = saturate( material.clearcoat );\tmaterial.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 );\n\tmaterial.clearcoatRoughness += geometryRoughness;\n\tmaterial.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );\n#endif\n#ifdef USE_SHEEN\n\tmaterial.sheenColor = sheen;\n#endif";
 
+	var lights_physical_pars_fragment = "struct PhysicalMaterial {\n\tvec3\tdiffuseColor;\n\tfloat\tspecularRoughness;\n\tvec3\tspecularColor;\n#ifdef CLEARCOAT\n\tfloat clearcoat;\n\tfloat clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tvec3 sheenColor;\n#endif\n};\n#define MAXIMUM_SPECULAR_COEFFICIENT 0.16\n#define DEFAULT_SPECULAR_COEFFICIENT 0.04\nfloat clearcoatDHRApprox( const in float roughness, const in float dotNL ) {\n\treturn DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );\n}\n#if NUM_RECT_AREA_LIGHTS > 0\n\tvoid RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\t\tvec3 normal = geometry.normal;\n\t\tvec3 viewDir = geometry.viewDir;\n\t\tvec3 position = geometry.position;\n\t\tvec3 lightPos = rectAreaLight.position;\n\t\tvec3 halfWidth = rectAreaLight.halfWidth;\n\t\tvec3 halfHeight = rectAreaLight.halfHeight;\n\t\tvec3 lightColor = rectAreaLight.color;\n\t\tfloat roughness = material.specularRoughness;\n\t\tvec3 rectCoords[ 4 ];\n\t\trectCoords[ 0 ] = lightPos + halfWidth - halfHeight;\t\trectCoords[ 1 ] = lightPos - halfWidth - halfHeight;\n\t\trectCoords[ 2 ] = lightPos - halfWidth + halfHeight;\n\t\trectCoords[ 3 ] = lightPos + halfWidth + halfHeight;\n\t\tvec2 uv = LTC_Uv( normal, viewDir, roughness );\n\t\tvec4 t1 = texture2D( ltc_1, uv );\n\t\tvec4 t2 = texture2D( ltc_2, uv );\n\t\tmat3 mInv = mat3(\n\t\t\tvec3( t1.x, 0, t1.y ),\n\t\t\tvec3(    0, 1,    0 ),\n\t\t\tvec3( t1.z, 0, t1.w )\n\t\t);\n\t\tvec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y );\n\t\treflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords );\n\t\treflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );\n\t}\n#endif\nvoid RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNL = saturate( dot( geometry.clearcoatNormal, directLight.direction ) );\n\t\tvec3 ccIrradiance = ccDotNL * directLight.color;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tccIrradiance *= PI;\n\t\t#endif\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t\treflectedLight.directSpecular += ccIrradiance * material.clearcoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\t#ifdef USE_SHEEN\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_Sheen(\n\t\t\tmaterial.specularRoughness,\n\t\t\tdirectLight.direction,\n\t\t\tgeometry,\n\t\t\tmaterial.sheenColor\n\t\t);\n\t#else\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness);\n\t#endif\n\treflectedLight.directDiffuse += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNV = saturate( dot( geometry.clearcoatNormal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t\tfloat ccDotNL = ccDotNV;\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\tfloat clearcoatInv = 1.0 - clearcoatDHR;\n\tvec3 singleScattering = vec3( 0.0 );\n\tvec3 multiScattering = vec3( 0.0 );\n\tvec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI;\n\tBRDF_Specular_Multiscattering_Environment( geometry, material.specularColor, material.specularRoughness, singleScattering, multiScattering );\n\tvec3 diffuse = material.diffuseColor * ( 1.0 - ( singleScattering + multiScattering ) );\n\treflectedLight.indirectSpecular += clearcoatInv * radiance * singleScattering;\n\treflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance;\n\treflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance;\n}\n#define RE_Direct\t\t\t\tRE_Direct_Physical\n#define RE_Direct_RectArea\t\tRE_Direct_RectArea_Physical\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Physical\n#define RE_IndirectSpecular\t\tRE_IndirectSpecular_Physical\nfloat computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {\n\treturn saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );\n}";
 
-		var gammaFactorDefine = ( renderer.gammaFactor > 0 ) ? renderer.gammaFactor : 1.0;
+	var lights_fragment_begin = "\nGeometricContext geometry;\ngeometry.position = - vViewPosition;\ngeometry.normal = normal;\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\n#ifdef CLEARCOAT\n\tgeometry.clearcoatNormal = clearcoatNormal;\n#endif\nIncidentLight directLight;\n#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )\n\tPointLight pointLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tpointLight = pointLights[ i ];\n\t\tgetPointDirectLightIrradiance( pointLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )\n\t\tpointLightShadow = pointLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )\n\tSpotLight spotLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tspotLight = spotLights[ i ];\n\t\tgetSpotDirectLightIrradiance( spotLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\tspotLightShadow = spotLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )\n\tDirectionalLight directionalLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tdirectionalLight = directionalLights[ i ];\n\t\tgetDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )\n\t\tdirectionalLightShadow = directionalLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )\n\tRectAreaLight rectAreaLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {\n\t\trectAreaLight = rectAreaLights[ i ];\n\t\tRE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if defined( RE_IndirectDiffuse )\n\tvec3 iblIrradiance = vec3( 0.0 );\n\tvec3 irradiance = getAmbientLightIrradiance( ambientLightColor );\n\tirradiance += getLightProbeIrradiance( lightProbe, geometry );\n\t#if ( NUM_HEMI_LIGHTS > 0 )\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\t\tirradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n#endif\n#if defined( RE_IndirectSpecular )\n\tvec3 radiance = vec3( 0.0 );\n\tvec3 clearcoatRadiance = vec3( 0.0 );\n#endif";
 
-		var customExtensions = parameters.isWebGL2 ? '' : generateExtensions( parameters );
+	var lights_fragment_maps = "#if defined( RE_IndirectDiffuse )\n\t#ifdef USE_LIGHTMAP\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\tvec3 lightMapIrradiance = lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tlightMapIrradiance *= PI;\n\t\t#endif\n\t\tirradiance += lightMapIrradiance;\n\t#endif\n\t#if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )\n\t\tiblIrradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel );\n\t#endif\n#endif\n#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )\n\tradiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.normal, material.specularRoughness, maxMipLevel );\n\t#ifdef CLEARCOAT\n\t\tclearcoatRadiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness, maxMipLevel );\n\t#endif\n#endif";
 
-		var customDefines = generateDefines( defines );
+	var lights_fragment_end = "#if defined( RE_IndirectDiffuse )\n\tRE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );\n#endif\n#if defined( RE_IndirectSpecular )\n\tRE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight );\n#endif";
 
-		var program = gl.createProgram();
+	var logdepthbuf_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tgl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;\n#endif";
 
-		var prefixVertex, prefixFragment;
+	var logdepthbuf_pars_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tuniform float logDepthBufFC;\n\tvarying float vFragDepth;\n\tvarying float vIsPerspective;\n#endif";
 
-		if ( parameters.isRawShaderMaterial ) {
+	var logdepthbuf_pars_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvarying float vFragDepth;\n\t\tvarying float vIsPerspective;\n\t#else\n\t\tuniform float logDepthBufFC;\n\t#endif\n#endif";
 
-			prefixVertex = [
+	var logdepthbuf_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvFragDepth = 1.0 + gl_Position.w;\n\t\tvIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) );\n\t#else\n\t\tif ( isPerspectiveMatrix( projectionMatrix ) ) {\n\t\t\tgl_Position.z = log2( max( EPSILON, gl_Position.w + 1.0 ) ) * logDepthBufFC - 1.0;\n\t\t\tgl_Position.z *= gl_Position.w;\n\t\t}\n\t#endif\n#endif";
 
-				customDefines
+	var map_fragment = "#ifdef USE_MAP\n\tvec4 texelColor = texture2D( map, vUv );\n\ttexelColor = mapTexelToLinear( texelColor );\n\tdiffuseColor *= texelColor;\n#endif";
 
-			].filter( filterEmptyLine ).join( '\n' );
+	var map_pars_fragment = "#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif";
 
-			if ( prefixVertex.length > 0 ) {
+	var map_particle_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tvec2 uv = ( uvTransform * vec3( gl_PointCoord.x, 1.0 - gl_PointCoord.y, 1 ) ).xy;\n#endif\n#ifdef USE_MAP\n\tvec4 mapTexel = texture2D( map, uv );\n\tdiffuseColor *= mapTexelToLinear( mapTexel );\n#endif\n#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, uv ).g;\n#endif";
 
-				prefixVertex += '\n';
+	var map_particle_pars_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tuniform mat3 uvTransform;\n#endif\n#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif\n#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif";
 
-			}
+	var metalnessmap_fragment = "float metalnessFactor = metalness;\n#ifdef USE_METALNESSMAP\n\tvec4 texelMetalness = texture2D( metalnessMap, vUv );\n\tmetalnessFactor *= texelMetalness.b;\n#endif";
 
-			prefixFragment = [
+	var metalnessmap_pars_fragment = "#ifdef USE_METALNESSMAP\n\tuniform sampler2D metalnessMap;\n#endif";
 
-				customExtensions,
-				customDefines
+	var morphnormal_vertex = "#ifdef USE_MORPHNORMALS\n\tobjectNormal *= morphTargetBaseInfluence;\n\tobjectNormal += morphNormal0 * morphTargetInfluences[ 0 ];\n\tobjectNormal += morphNormal1 * morphTargetInfluences[ 1 ];\n\tobjectNormal += morphNormal2 * morphTargetInfluences[ 2 ];\n\tobjectNormal += morphNormal3 * morphTargetInfluences[ 3 ];\n#endif";
 
-			].filter( filterEmptyLine ).join( '\n' );
+	var morphtarget_pars_vertex = "#ifdef USE_MORPHTARGETS\n\tuniform float morphTargetBaseInfluence;\n\t#ifndef USE_MORPHNORMALS\n\tuniform float morphTargetInfluences[ 8 ];\n\t#else\n\tuniform float morphTargetInfluences[ 4 ];\n\t#endif\n#endif";
 
-			if ( prefixFragment.length > 0 ) {
+	var morphtarget_vertex = "#ifdef USE_MORPHTARGETS\n\ttransformed *= morphTargetBaseInfluence;\n\ttransformed += morphTarget0 * morphTargetInfluences[ 0 ];\n\ttransformed += morphTarget1 * morphTargetInfluences[ 1 ];\n\ttransformed += morphTarget2 * morphTargetInfluences[ 2 ];\n\ttransformed += morphTarget3 * morphTargetInfluences[ 3 ];\n\t#ifndef USE_MORPHNORMALS\n\ttransformed += morphTarget4 * morphTargetInfluences[ 4 ];\n\ttransformed += morphTarget5 * morphTargetInfluences[ 5 ];\n\ttransformed += morphTarget6 * morphTargetInfluences[ 6 ];\n\ttransformed += morphTarget7 * morphTargetInfluences[ 7 ];\n\t#endif\n#endif";
 
-				prefixFragment += '\n';
+	var normal_fragment_begin = "#ifdef FLAT_SHADED\n\tvec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );\n\tvec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );\n\tvec3 normal = normalize( cross( fdx, fdy ) );\n#else\n\tvec3 normal = normalize( vNormal );\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\t#ifdef USE_TANGENT\n\t\tvec3 tangent = normalize( vTangent );\n\t\tvec3 bitangent = normalize( vBitangent );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\ttangent = tangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t\tbitangent = bitangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t#endif\n\t\t#if defined( TANGENTSPACE_NORMALMAP ) || defined( USE_CLEARCOAT_NORMALMAP )\n\t\t\tmat3 vTBN = mat3( tangent, bitangent, normal );\n\t\t#endif\n\t#endif\n#endif\nvec3 geometryNormal = normal;";
 
-			}
+	var normal_fragment_maps = "#ifdef OBJECTSPACE_NORMALMAP\n\tnormal = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\t#ifdef FLIP_SIDED\n\t\tnormal = - normal;\n\t#endif\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\tnormal = normalize( normalMatrix * normal );\n#elif defined( TANGENTSPACE_NORMALMAP )\n\tvec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\tmapN.xy *= normalScale;\n\t#ifdef USE_TANGENT\n\t\tnormal = normalize( vTBN * mapN );\n\t#else\n\t\tnormal = perturbNormal2Arb( -vViewPosition, normal, mapN );\n\t#endif\n#elif defined( USE_BUMPMAP )\n\tnormal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );\n#endif";
 
-		} else {
+	var normalmap_pars_fragment = "#ifdef USE_NORMALMAP\n\tuniform sampler2D normalMap;\n\tuniform vec2 normalScale;\n#endif\n#ifdef OBJECTSPACE_NORMALMAP\n\tuniform mat3 normalMatrix;\n#endif\n#if ! defined ( USE_TANGENT ) && ( defined ( TANGENTSPACE_NORMALMAP ) || defined ( USE_CLEARCOAT_NORMALMAP ) )\n\tvec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN ) {\n\t\tvec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );\n\t\tvec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );\n\t\tvec2 st0 = dFdx( vUv.st );\n\t\tvec2 st1 = dFdy( vUv.st );\n\t\tfloat scale = sign( st1.t * st0.s - st0.t * st1.s );\n\t\tvec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );\n\t\tvec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );\n\t\tvec3 N = normalize( surf_norm );\n\t\tmat3 tsn = mat3( S, T, N );\n\t\tmapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\treturn normalize( tsn * mapN );\n\t}\n#endif";
 
-			prefixVertex = [
+	var clearcoat_normal_fragment_begin = "#ifdef CLEARCOAT\n\tvec3 clearcoatNormal = geometryNormal;\n#endif";
 
-				generatePrecision( parameters ),
+	var clearcoat_normal_fragment_maps = "#ifdef USE_CLEARCOAT_NORMALMAP\n\tvec3 clearcoatMapN = texture2D( clearcoatNormalMap, vUv ).xyz * 2.0 - 1.0;\n\tclearcoatMapN.xy *= clearcoatNormalScale;\n\t#ifdef USE_TANGENT\n\t\tclearcoatNormal = normalize( vTBN * clearcoatMapN );\n\t#else\n\t\tclearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN );\n\t#endif\n#endif";
 
-				'#define SHADER_NAME ' + parameters.shaderName,
+	var clearcoat_pars_fragment = "#ifdef USE_CLEARCOATMAP\n\tuniform sampler2D clearcoatMap;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tuniform sampler2D clearcoatRoughnessMap;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tuniform sampler2D clearcoatNormalMap;\n\tuniform vec2 clearcoatNormalScale;\n#endif";
 
-				customDefines,
+	var packing = "vec3 packNormalToRGB( const in vec3 normal ) {\n\treturn normalize( normal ) * 0.5 + 0.5;\n}\nvec3 unpackRGBToNormal( const in vec3 rgb ) {\n\treturn 2.0 * rgb.xyz - 1.0;\n}\nconst float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;\nconst vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256.,  256. );\nconst vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );\nconst float ShiftRight8 = 1. / 256.;\nvec4 packDepthToRGBA( const in float v ) {\n\tvec4 r = vec4( fract( v * PackFactors ), v );\n\tr.yzw -= r.xyz * ShiftRight8;\treturn r * PackUpscale;\n}\nfloat unpackRGBAToDepth( const in vec4 v ) {\n\treturn dot( v, UnpackFactors );\n}\nvec4 pack2HalfToRGBA( vec2 v ) {\n\tvec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ));\n\treturn vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w);\n}\nvec2 unpackRGBATo2Half( vec4 v ) {\n\treturn vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );\n}\nfloat viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn ( viewZ + near ) / ( near - far );\n}\nfloat orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {\n\treturn linearClipZ * ( near - far ) - near;\n}\nfloat viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn (( near + viewZ ) * far ) / (( far - near ) * viewZ );\n}\nfloat perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {\n\treturn ( near * far ) / ( ( far - near ) * invClipZ - far );\n}";
 
-				parameters.instancing ? '#define USE_INSTANCING' : '',
-				parameters.supportsVertexTextures ? '#define VERTEX_TEXTURES' : '',
+	var premultiplied_alpha_fragment = "#ifdef PREMULTIPLIED_ALPHA\n\tgl_FragColor.rgb *= gl_FragColor.a;\n#endif";
 
-				'#define GAMMA_FACTOR ' + gammaFactorDefine,
+	var project_vertex = "vec4 mvPosition = vec4( transformed, 1.0 );\n#ifdef USE_INSTANCING\n\tmvPosition = instanceMatrix * mvPosition;\n#endif\nmvPosition = modelViewMatrix * mvPosition;\ngl_Position = projectionMatrix * mvPosition;";
 
-				'#define MAX_BONES ' + parameters.maxBones,
-				( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '',
-				( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '',
+	var dithering_fragment = "#ifdef DITHERING\n\tgl_FragColor.rgb = dithering( gl_FragColor.rgb );\n#endif";
 
-				parameters.map ? '#define USE_MAP' : '',
-				parameters.envMap ? '#define USE_ENVMAP' : '',
-				parameters.envMap ? '#define ' + envMapModeDefine : '',
-				parameters.lightMap ? '#define USE_LIGHTMAP' : '',
-				parameters.aoMap ? '#define USE_AOMAP' : '',
-				parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '',
-				parameters.bumpMap ? '#define USE_BUMPMAP' : '',
-				parameters.normalMap ? '#define USE_NORMALMAP' : '',
-				( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '',
-				( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '',
+	var dithering_pars_fragment = "#ifdef DITHERING\n\tvec3 dithering( vec3 color ) {\n\t\tfloat grid_position = rand( gl_FragCoord.xy );\n\t\tvec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );\n\t\tdither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );\n\t\treturn color + dither_shift_RGB;\n\t}\n#endif";
 
-				parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
-				parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
-				parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',
-				parameters.displacementMap && parameters.supportsVertexTextures ? '#define USE_DISPLACEMENTMAP' : '',
-				parameters.specularMap ? '#define USE_SPECULARMAP' : '',
-				parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
-				parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
-				parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
+	var roughnessmap_fragment = "float roughnessFactor = roughness;\n#ifdef USE_ROUGHNESSMAP\n\tvec4 texelRoughness = texture2D( roughnessMap, vUv );\n\troughnessFactor *= texelRoughness.g;\n#endif";
 
-				parameters.vertexTangents ? '#define USE_TANGENT' : '',
-				parameters.vertexColors ? '#define USE_COLOR' : '',
-				parameters.vertexUvs ? '#define USE_UV' : '',
-				parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '',
+	var roughnessmap_pars_fragment = "#ifdef USE_ROUGHNESSMAP\n\tuniform sampler2D roughnessMap;\n#endif";
 
-				parameters.flatShading ? '#define FLAT_SHADED' : '',
+	var shadowmap_pars_fragment = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D directionalShadowMap[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D spotShadowMap[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D pointShadowMap[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tfloat texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n\t\treturn step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n\t}\n\tvec2 texture2DDistribution( sampler2D shadow, vec2 uv ) {\n\t\treturn unpackRGBATo2Half( texture2D( shadow, uv ) );\n\t}\n\tfloat VSMShadow (sampler2D shadow, vec2 uv, float compare ){\n\t\tfloat occlusion = 1.0;\n\t\tvec2 distribution = texture2DDistribution( shadow, uv );\n\t\tfloat hard_shadow = step( compare , distribution.x );\n\t\tif (hard_shadow != 1.0 ) {\n\t\t\tfloat distance = compare - distribution.x ;\n\t\t\tfloat variance = max( 0.00000, distribution.y * distribution.y );\n\t\t\tfloat softness_probability = variance / (variance + distance * distance );\t\t\tsoftness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 );\t\t\tocclusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 );\n\t\t}\n\t\treturn occlusion;\n\t}\n\tfloat getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {\n\t\tfloat shadow = 1.0;\n\t\tshadowCoord.xyz /= shadowCoord.w;\n\t\tshadowCoord.z += shadowBias;\n\t\tbvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );\n\t\tbool inFrustum = all( inFrustumVec );\n\t\tbvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );\n\t\tbool frustumTest = all( frustumTestVec );\n\t\tif ( frustumTest ) {\n\t\t#if defined( SHADOWMAP_TYPE_PCF )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx0 = - texelSize.x * shadowRadius;\n\t\t\tfloat dy0 = - texelSize.y * shadowRadius;\n\t\t\tfloat dx1 = + texelSize.x * shadowRadius;\n\t\t\tfloat dy1 = + texelSize.y * shadowRadius;\n\t\t\tfloat dx2 = dx0 / 2.0;\n\t\t\tfloat dy2 = dy0 / 2.0;\n\t\t\tfloat dx3 = dx1 / 2.0;\n\t\t\tfloat dy3 = dy1 / 2.0;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )\n\t\t\t) * ( 1.0 / 17.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_PCF_SOFT )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx = texelSize.x;\n\t\t\tfloat dy = texelSize.y;\n\t\t\tvec2 uv = shadowCoord.xy;\n\t\t\tvec2 f = fract( uv * shadowMapSize + 0.5 );\n\t\t\tuv -= f * texelSize;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, uv, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t\t  texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t\t  f.x ),\n\t\t\t\t\t mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ), \n\t\t\t\t\t\t  texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t\t  f.x ),\n\t\t\t\t\t f.y )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_VSM )\n\t\t\tshadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#else\n\t\t\tshadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#endif\n\t\t}\n\t\treturn shadow;\n\t}\n\tvec2 cubeToUV( vec3 v, float texelSizeY ) {\n\t\tvec3 absV = abs( v );\n\t\tfloat scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );\n\t\tabsV *= scaleToCube;\n\t\tv *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );\n\t\tvec2 planar = v.xy;\n\t\tfloat almostATexel = 1.5 * texelSizeY;\n\t\tfloat almostOne = 1.0 - almostATexel;\n\t\tif ( absV.z >= almostOne ) {\n\t\t\tif ( v.z > 0.0 )\n\t\t\t\tplanar.x = 4.0 - v.x;\n\t\t} else if ( absV.x >= almostOne ) {\n\t\t\tfloat signX = sign( v.x );\n\t\t\tplanar.x = v.z * signX + 2.0 * signX;\n\t\t} else if ( absV.y >= almostOne ) {\n\t\t\tfloat signY = sign( v.y );\n\t\t\tplanar.x = v.x + 2.0 * signY + 2.0;\n\t\t\tplanar.y = v.z * signY - 2.0;\n\t\t}\n\t\treturn vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );\n\t}\n\tfloat getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {\n\t\tvec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );\n\t\tvec3 lightToPosition = shadowCoord.xyz;\n\t\tfloat dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear );\t\tdp += shadowBias;\n\t\tvec3 bd3D = normalize( lightToPosition );\n\t\t#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM )\n\t\t\tvec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;\n\t\t\treturn (\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#else\n\t\t\treturn texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );\n\t\t#endif\n\t}\n#endif";
 
-				parameters.skinning ? '#define USE_SKINNING' : '',
-				parameters.useVertexTexture ? '#define BONE_TEXTURE' : '',
+	var shadowmap_pars_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 spotShadowMatrix[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 pointShadowMatrix[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n#endif";
 
-				parameters.morphTargets ? '#define USE_MORPHTARGETS' : '',
-				parameters.morphNormals && parameters.flatShading === false ? '#define USE_MORPHNORMALS' : '',
-				parameters.doubleSided ? '#define DOUBLE_SIDED' : '',
-				parameters.flipSided ? '#define FLIP_SIDED' : '',
+	var shadowmap_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tvDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tvSpotShadowCoord[ i ] = spotShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tvPointShadowCoord[ i ] = pointShadowMatrix[ i ] * worldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n#endif";
 
-				parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '',
-				parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '',
+	var shadowmask_pars_fragment = "float getShadowMask() {\n\tfloat shadow = 1.0;\n\t#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tdirectionalLight = directionalLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tspotLight = spotLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tpointLight = pointLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#endif\n\treturn shadow;\n}";
 
-				parameters.sizeAttenuation ? '#define USE_SIZEATTENUATION' : '',
+	var skinbase_vertex = "#ifdef USE_SKINNING\n\tmat4 boneMatX = getBoneMatrix( skinIndex.x );\n\tmat4 boneMatY = getBoneMatrix( skinIndex.y );\n\tmat4 boneMatZ = getBoneMatrix( skinIndex.z );\n\tmat4 boneMatW = getBoneMatrix( skinIndex.w );\n#endif";
 
-				parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
-				( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '',
+	var skinning_pars_vertex = "#ifdef USE_SKINNING\n\tuniform mat4 bindMatrix;\n\tuniform mat4 bindMatrixInverse;\n\t#ifdef BONE_TEXTURE\n\t\tuniform highp sampler2D boneTexture;\n\t\tuniform int boneTextureSize;\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tfloat j = i * 4.0;\n\t\t\tfloat x = mod( j, float( boneTextureSize ) );\n\t\t\tfloat y = floor( j / float( boneTextureSize ) );\n\t\t\tfloat dx = 1.0 / float( boneTextureSize );\n\t\t\tfloat dy = 1.0 / float( boneTextureSize );\n\t\t\ty = dy * ( y + 0.5 );\n\t\t\tvec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );\n\t\t\tvec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );\n\t\t\tvec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );\n\t\t\tvec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );\n\t\t\tmat4 bone = mat4( v1, v2, v3, v4 );\n\t\t\treturn bone;\n\t\t}\n\t#else\n\t\tuniform mat4 boneMatrices[ MAX_BONES ];\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tmat4 bone = boneMatrices[ int(i) ];\n\t\t\treturn bone;\n\t\t}\n\t#endif\n#endif";
 
-				'uniform mat4 modelMatrix;',
-				'uniform mat4 modelViewMatrix;',
-				'uniform mat4 projectionMatrix;',
-				'uniform mat4 viewMatrix;',
-				'uniform mat3 normalMatrix;',
-				'uniform vec3 cameraPosition;',
-				'uniform bool isOrthographic;',
+	var skinning_vertex = "#ifdef USE_SKINNING\n\tvec4 skinVertex = bindMatrix * vec4( transformed, 1.0 );\n\tvec4 skinned = vec4( 0.0 );\n\tskinned += boneMatX * skinVertex * skinWeight.x;\n\tskinned += boneMatY * skinVertex * skinWeight.y;\n\tskinned += boneMatZ * skinVertex * skinWeight.z;\n\tskinned += boneMatW * skinVertex * skinWeight.w;\n\ttransformed = ( bindMatrixInverse * skinned ).xyz;\n#endif";
 
-				'#ifdef USE_INSTANCING',
+	var skinnormal_vertex = "#ifdef USE_SKINNING\n\tmat4 skinMatrix = mat4( 0.0 );\n\tskinMatrix += skinWeight.x * boneMatX;\n\tskinMatrix += skinWeight.y * boneMatY;\n\tskinMatrix += skinWeight.z * boneMatZ;\n\tskinMatrix += skinWeight.w * boneMatW;\n\tskinMatrix  = bindMatrixInverse * skinMatrix * bindMatrix;\n\tobjectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( skinMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif";
 
-				' attribute mat4 instanceMatrix;',
+	var specularmap_fragment = "float specularStrength;\n#ifdef USE_SPECULARMAP\n\tvec4 texelSpecular = texture2D( specularMap, vUv );\n\tspecularStrength = texelSpecular.r;\n#else\n\tspecularStrength = 1.0;\n#endif";
 
-				'#endif',
+	var specularmap_pars_fragment = "#ifdef USE_SPECULARMAP\n\tuniform sampler2D specularMap;\n#endif";
 
-				'attribute vec3 position;',
-				'attribute vec3 normal;',
-				'attribute vec2 uv;',
+	var tonemapping_fragment = "#if defined( TONE_MAPPING )\n\tgl_FragColor.rgb = toneMapping( gl_FragColor.rgb );\n#endif";
 
-				'#ifdef USE_TANGENT',
+	var tonemapping_pars_fragment = "#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\nuniform float toneMappingExposure;\nuniform float toneMappingWhitePoint;\nvec3 LinearToneMapping( vec3 color ) {\n\treturn toneMappingExposure * color;\n}\nvec3 ReinhardToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( color / ( vec3( 1.0 ) + color ) );\n}\n#define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) )\nvec3 Uncharted2ToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) );\n}\nvec3 OptimizedCineonToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\tcolor = max( vec3( 0.0 ), color - 0.004 );\n\treturn pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\nvec3 ACESFilmicToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( ( color * ( 2.51 * color + 0.03 ) ) / ( color * ( 2.43 * color + 0.59 ) + 0.14 ) );\n}";
 
-				'	attribute vec4 tangent;',
+	var uv_pars_fragment = "#if ( defined( USE_UV ) && ! defined( UVS_VERTEX_ONLY ) )\n\tvarying vec2 vUv;\n#endif";
 
-				'#endif',
+	var uv_pars_vertex = "#ifdef USE_UV\n\t#ifdef UVS_VERTEX_ONLY\n\t\tvec2 vUv;\n\t#else\n\t\tvarying vec2 vUv;\n\t#endif\n\tuniform mat3 uvTransform;\n#endif";
 
-				'#ifdef USE_COLOR',
+	var uv_vertex = "#ifdef USE_UV\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n#endif";
 
-				'	attribute vec3 color;',
+	var uv2_pars_fragment = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvarying vec2 vUv2;\n#endif";
 
-				'#endif',
+	var uv2_pars_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tattribute vec2 uv2;\n\tvarying vec2 vUv2;\n\tuniform mat3 uv2Transform;\n#endif";
 
-				'#ifdef USE_MORPHTARGETS',
+	var uv2_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvUv2 = ( uv2Transform * vec3( uv2, 1 ) ).xy;\n#endif";
 
-				'	attribute vec3 morphTarget0;',
-				'	attribute vec3 morphTarget1;',
-				'	attribute vec3 morphTarget2;',
-				'	attribute vec3 morphTarget3;',
+	var worldpos_vertex = "#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP )\n\tvec4 worldPosition = vec4( transformed, 1.0 );\n\t#ifdef USE_INSTANCING\n\t\tworldPosition = instanceMatrix * worldPosition;\n\t#endif\n\tworldPosition = modelMatrix * worldPosition;\n#endif";
 
-				'	#ifdef USE_MORPHNORMALS',
+	var background_frag = "uniform sampler2D t2D;\nvarying vec2 vUv;\nvoid main() {\n\tvec4 texColor = texture2D( t2D, vUv );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
 
-				'		attribute vec3 morphNormal0;',
-				'		attribute vec3 morphNormal1;',
-				'		attribute vec3 morphNormal2;',
-				'		attribute vec3 morphNormal3;',
+	var background_vert = "varying vec2 vUv;\nuniform mat3 uvTransform;\nvoid main() {\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n\tgl_Position = vec4( position.xy, 1.0, 1.0 );\n}";
 
-				'	#else',
+	var cube_frag = "#include <envmap_common_pars_fragment>\nuniform float opacity;\nvarying vec3 vWorldDirection;\n#include <cube_uv_reflection_fragment>\nvoid main() {\n\tvec3 vReflect = vWorldDirection;\n\t#include <envmap_fragment>\n\tgl_FragColor = envColor;\n\tgl_FragColor.a *= opacity;\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
 
-				'		attribute vec3 morphTarget4;',
-				'		attribute vec3 morphTarget5;',
-				'		attribute vec3 morphTarget6;',
-				'		attribute vec3 morphTarget7;',
+	var cube_vert = "varying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\tgl_Position.z = gl_Position.w;\n}";
 
-				'	#endif',
+	var depth_frag = "#if DEPTH_PACKING == 3200\n\tuniform float opacity;\n#endif\n#include <common>\n#include <packing>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#if DEPTH_PACKING == 3200\n\t\tdiffuseColor.a = opacity;\n\t#endif\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <logdepthbuf_fragment>\n\tfloat fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;\n\t#if DEPTH_PACKING == 3200\n\t\tgl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );\n\t#elif DEPTH_PACKING == 3201\n\t\tgl_FragColor = packDepthToRGBA( fragCoordZ );\n\t#endif\n}";
 
-				'#endif',
+	var depth_vert = "#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include <uv_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include <beginnormal_vertex>\n\t\t#include <morphnormal_vertex>\n\t\t#include <skinnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvHighPrecisionZW = gl_Position.zw;\n}";
 
-				'#ifdef USE_SKINNING',
+	var distanceRGBA_frag = "#define DISTANCE\nuniform vec3 referencePosition;\nuniform float nearDistance;\nuniform float farDistance;\nvarying vec3 vWorldPosition;\n#include <common>\n#include <packing>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main () {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( 1.0 );\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\tfloat dist = length( vWorldPosition - referencePosition );\n\tdist = ( dist - nearDistance ) / ( farDistance - nearDistance );\n\tdist = saturate( dist );\n\tgl_FragColor = packDepthToRGBA( dist );\n}";
 
-				'	attribute vec4 skinIndex;',
-				'	attribute vec4 skinWeight;',
+	var distanceRGBA_vert = "#define DISTANCE\nvarying vec3 vWorldPosition;\n#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include <beginnormal_vertex>\n\t\t#include <morphnormal_vertex>\n\t\t#include <skinnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <worldpos_vertex>\n\t#include <clipping_planes_vertex>\n\tvWorldPosition = worldPosition.xyz;\n}";
 
-				'#endif',
+	var equirect_frag = "uniform sampler2D tEquirect;\nvarying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvec3 direction = normalize( vWorldDirection );\n\tvec2 sampleUV;\n\tsampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\tsampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;\n\tvec4 texColor = texture2D( tEquirect, sampleUV );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n}";
 
-				'\n'
+	var equirect_vert = "varying vec3 vWorldDirection;\n#include <common>\nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include <begin_vertex>\n\t#include <project_vertex>\n}";
 
-			].filter( filterEmptyLine ).join( '\n' );
+	var linedashed_frag = "uniform vec3 diffuse;\nuniform float opacity;\nuniform float dashSize;\nuniform float totalSize;\nvarying float vLineDistance;\n#include <common>\n#include <color_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tif ( mod( vLineDistance, totalSize ) > dashSize ) {\n\t\tdiscard;\n\t}\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <color_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n}";
 
-			prefixFragment = [
+	var linedashed_vert = "uniform float scale;\nattribute float lineDistance;\nvarying float vLineDistance;\n#include <common>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\tvLineDistance = scale * lineDistance;\n\t#include <color_vertex>\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n}";
 
-				customExtensions,
+	var meshbasic_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <fog_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\t#ifdef USE_LIGHTMAP\n\t\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\treflectedLight.indirectDiffuse += lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t#else\n\t\treflectedLight.indirectDiffuse += vec3( 1.0 );\n\t#endif\n\t#include <aomap_fragment>\n\treflectedLight.indirectDiffuse *= diffuseColor.rgb;\n\tvec3 outgoingLight = reflectedLight.indirectDiffuse;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				generatePrecision( parameters ),
+	var meshbasic_vert = "#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <envmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <skinbase_vertex>\n\t#ifdef USE_ENVMAP\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <worldpos_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <envmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				'#define SHADER_NAME ' + parameters.shaderName,
+	var meshlambert_frag = "uniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <fog_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <shadowmask_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <emissivemap_fragment>\n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.indirectDiffuse += ( gl_FrontFacing ) ? vIndirectFront : vIndirectBack;\n\t#else\n\t\treflectedLight.indirectDiffuse += vIndirectFront;\n\t#endif\n\t#include <lightmap_fragment>\n\treflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb );\n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;\n\t#else\n\t\treflectedLight.directDiffuse = vLightFront;\n\t#endif\n\treflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask();\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				customDefines,
+	var meshlambert_vert = "#define LAMBERT\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <envmap_pars_vertex>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <worldpos_vertex>\n\t#include <envmap_vertex>\n\t#include <lights_lambert_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.alphaTest ? '#define ALPHATEST ' + parameters.alphaTest + ( parameters.alphaTest % 1 ? '' : '.0' ) : '', // add '.0' if integer
+	var meshmatcap_frag = "#define MATCAP\nuniform vec3 diffuse;\nuniform float opacity;\nuniform sampler2D matcap;\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <fog_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\tvec3 viewDir = normalize( vViewPosition );\n\tvec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );\n\tvec3 y = cross( viewDir, x );\n\tvec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5;\n\t#ifdef USE_MATCAP\n\t\tvec4 matcapColor = texture2D( matcap, uv );\n\t\tmatcapColor = matcapTexelToLinear( matcapColor );\n\t#else\n\t\tvec4 matcapColor = vec4( 1.0 );\n\t#endif\n\tvec3 outgoingLight = diffuseColor.rgb * matcapColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				'#define GAMMA_FACTOR ' + gammaFactorDefine,
+	var meshmatcap_vert = "#define MATCAP\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <color_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n\t#ifndef FLAT_SHADED\n\t\tvNormal = normalize( transformedNormal );\n\t#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n\tvViewPosition = - mvPosition.xyz;\n}";
 
-				( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '',
-				( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '',
+	var meshtoon_frag = "#define TOON\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <gradientmap_pars_fragment>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <lights_toon_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_toon_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				parameters.map ? '#define USE_MAP' : '',
-				parameters.matcap ? '#define USE_MATCAP' : '',
-				parameters.envMap ? '#define USE_ENVMAP' : '',
-				parameters.envMap ? '#define ' + envMapTypeDefine : '',
-				parameters.envMap ? '#define ' + envMapModeDefine : '',
-				parameters.envMap ? '#define ' + envMapBlendingDefine : '',
-				parameters.lightMap ? '#define USE_LIGHTMAP' : '',
-				parameters.aoMap ? '#define USE_AOMAP' : '',
-				parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '',
-				parameters.bumpMap ? '#define USE_BUMPMAP' : '',
-				parameters.normalMap ? '#define USE_NORMALMAP' : '',
-				( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '',
-				( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '',
-				parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
-				parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
-				parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',
-				parameters.specularMap ? '#define USE_SPECULARMAP' : '',
-				parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
-				parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
-				parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
+	var meshtoon_vert = "#define TOON\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.sheen ? '#define USE_SHEEN' : '',
+	var meshphong_frag = "#define PHONG\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_pars_fragment>\n#include <cube_uv_reflection_fragment>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <lights_phong_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <specularmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <specularmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_phong_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#include <envmap_fragment>\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				parameters.vertexTangents ? '#define USE_TANGENT' : '',
-				parameters.vertexColors ? '#define USE_COLOR' : '',
-				parameters.vertexUvs ? '#define USE_UV' : '',
-				parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '',
+	var meshphong_vert = "#define PHONG\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <envmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <envmap_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.gradientMap ? '#define USE_GRADIENTMAP' : '',
+	var meshphysical_frag = "#define STANDARD\n#ifdef PHYSICAL\n\t#define REFLECTIVITY\n\t#define CLEARCOAT\n\t#define TRANSPARENCY\n#endif\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float roughness;\nuniform float metalness;\nuniform float opacity;\n#ifdef TRANSPARENCY\n\tuniform float transparency;\n#endif\n#ifdef REFLECTIVITY\n\tuniform float reflectivity;\n#endif\n#ifdef CLEARCOAT\n\tuniform float clearcoat;\n\tuniform float clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tuniform vec3 sheen;\n#endif\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <packing>\n#include <dithering_pars_fragment>\n#include <color_pars_fragment>\n#include <uv_pars_fragment>\n#include <uv2_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <aomap_pars_fragment>\n#include <lightmap_pars_fragment>\n#include <emissivemap_pars_fragment>\n#include <bsdfs>\n#include <cube_uv_reflection_fragment>\n#include <envmap_common_pars_fragment>\n#include <envmap_physical_pars_fragment>\n#include <fog_pars_fragment>\n#include <lights_pars_begin>\n#include <lights_physical_pars_fragment>\n#include <shadowmap_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <clearcoat_pars_fragment>\n#include <roughnessmap_pars_fragment>\n#include <metalnessmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <color_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\t#include <roughnessmap_fragment>\n\t#include <metalnessmap_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\t#include <clearcoat_normal_fragment_begin>\n\t#include <clearcoat_normal_fragment_maps>\n\t#include <emissivemap_fragment>\n\t#include <lights_physical_fragment>\n\t#include <lights_fragment_begin>\n\t#include <lights_fragment_maps>\n\t#include <lights_fragment_end>\n\t#include <aomap_fragment>\n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#ifdef TRANSPARENCY\n\t\tdiffuseColor.a *= saturate( 1. - transparency + linearToRelativeLuminance( reflectedLight.directSpecular + reflectedLight.indirectSpecular ) );\n\t#endif\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n\t#include <dithering_fragment>\n}";
 
-				parameters.flatShading ? '#define FLAT_SHADED' : '',
+	var meshphysical_vert = "#define STANDARD\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <uv2_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <shadowmap_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <uv2_vertex>\n\t#include <color_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\tvViewPosition = - mvPosition.xyz;\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.doubleSided ? '#define DOUBLE_SIDED' : '',
-				parameters.flipSided ? '#define FLIP_SIDED' : '',
+	var normal_frag = "#define NORMAL\nuniform float opacity;\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <packing>\n#include <uv_pars_fragment>\n#include <bumpmap_pars_fragment>\n#include <normalmap_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\t#include <logdepthbuf_fragment>\n\t#include <normal_fragment_begin>\n\t#include <normal_fragment_maps>\n\tgl_FragColor = vec4( packNormalToRGB( normal ), opacity );\n}";
 
-				parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '',
-				parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '',
+	var normal_vert = "#define NORMAL\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include <common>\n#include <uv_pars_vertex>\n#include <displacementmap_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <skinning_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\t#include <beginnormal_vertex>\n\t#include <morphnormal_vertex>\n\t#include <skinbase_vertex>\n\t#include <skinnormal_vertex>\n\t#include <defaultnormal_vertex>\n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <skinning_vertex>\n\t#include <displacementmap_vertex>\n\t#include <project_vertex>\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvViewPosition = - mvPosition.xyz;\n#endif\n}";
 
-				parameters.premultipliedAlpha ? '#define PREMULTIPLIED_ALPHA' : '',
+	var points_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include <common>\n#include <color_pars_fragment>\n#include <map_particle_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_particle_fragment>\n\t#include <color_fragment>\n\t#include <alphatest_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n\t#include <premultiplied_alpha_fragment>\n}";
 
-				parameters.physicallyCorrectLights ? '#define PHYSICALLY_CORRECT_LIGHTS' : '',
+	var points_vert = "uniform float size;\nuniform float scale;\n#include <common>\n#include <color_pars_vertex>\n#include <fog_pars_vertex>\n#include <morphtarget_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <color_vertex>\n\t#include <begin_vertex>\n\t#include <morphtarget_vertex>\n\t#include <project_vertex>\n\tgl_PointSize = size;\n\t#ifdef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) gl_PointSize *= ( scale / - mvPosition.z );\n\t#endif\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <worldpos_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
-				( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '',
+	var shadow_frag = "uniform vec3 color;\nuniform float opacity;\n#include <common>\n#include <packing>\n#include <fog_pars_fragment>\n#include <bsdfs>\n#include <lights_pars_begin>\n#include <shadowmap_pars_fragment>\n#include <shadowmask_pars_fragment>\nvoid main() {\n\tgl_FragColor = vec4( color, opacity * ( 1.0 - getShadowMask() ) );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n}";
 
-				( ( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ) ? '#define TEXTURE_LOD_EXT' : '',
+	var shadow_vert = "#include <fog_pars_vertex>\n#include <shadowmap_pars_vertex>\nvoid main() {\n\t#include <begin_vertex>\n\t#include <project_vertex>\n\t#include <worldpos_vertex>\n\t#include <shadowmap_vertex>\n\t#include <fog_vertex>\n}";
 
-				'uniform mat4 viewMatrix;',
-				'uniform vec3 cameraPosition;',
-				'uniform bool isOrthographic;',
+	var sprite_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include <common>\n#include <uv_pars_fragment>\n#include <map_pars_fragment>\n#include <alphamap_pars_fragment>\n#include <fog_pars_fragment>\n#include <logdepthbuf_pars_fragment>\n#include <clipping_planes_pars_fragment>\nvoid main() {\n\t#include <clipping_planes_fragment>\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include <logdepthbuf_fragment>\n\t#include <map_fragment>\n\t#include <alphamap_fragment>\n\t#include <alphatest_fragment>\n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include <tonemapping_fragment>\n\t#include <encodings_fragment>\n\t#include <fog_fragment>\n}";
 
-				( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '',
-				( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below
-				( parameters.toneMapping !== NoToneMapping ) ? getToneMappingFunction( 'toneMapping', parameters.toneMapping ) : '',
+	var sprite_vert = "uniform float rotation;\nuniform vec2 center;\n#include <common>\n#include <uv_pars_vertex>\n#include <fog_pars_vertex>\n#include <logdepthbuf_pars_vertex>\n#include <clipping_planes_pars_vertex>\nvoid main() {\n\t#include <uv_vertex>\n\tvec4 mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\n\tvec2 scale;\n\tscale.x = length( vec3( modelMatrix[ 0 ].x, modelMatrix[ 0 ].y, modelMatrix[ 0 ].z ) );\n\tscale.y = length( vec3( modelMatrix[ 1 ].x, modelMatrix[ 1 ].y, modelMatrix[ 1 ].z ) );\n\t#ifndef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) scale *= - mvPosition.z;\n\t#endif\n\tvec2 alignedPosition = ( position.xy - ( center - vec2( 0.5 ) ) ) * scale;\n\tvec2 rotatedPosition;\n\trotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;\n\trotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;\n\tmvPosition.xy += rotatedPosition;\n\tgl_Position = projectionMatrix * mvPosition;\n\t#include <logdepthbuf_vertex>\n\t#include <clipping_planes_vertex>\n\t#include <fog_vertex>\n}";
 
-				parameters.dithering ? '#define DITHERING' : '',
+	var ShaderChunk = {
+		alphamap_fragment: alphamap_fragment,
+		alphamap_pars_fragment: alphamap_pars_fragment,
+		alphatest_fragment: alphatest_fragment,
+		aomap_fragment: aomap_fragment,
+		aomap_pars_fragment: aomap_pars_fragment,
+		begin_vertex: begin_vertex,
+		beginnormal_vertex: beginnormal_vertex,
+		bsdfs: bsdfs,
+		bumpmap_pars_fragment: bumpmap_pars_fragment,
+		clipping_planes_fragment: clipping_planes_fragment,
+		clipping_planes_pars_fragment: clipping_planes_pars_fragment,
+		clipping_planes_pars_vertex: clipping_planes_pars_vertex,
+		clipping_planes_vertex: clipping_planes_vertex,
+		color_fragment: color_fragment,
+		color_pars_fragment: color_pars_fragment,
+		color_pars_vertex: color_pars_vertex,
+		color_vertex: color_vertex,
+		common: common,
+		cube_uv_reflection_fragment: cube_uv_reflection_fragment,
+		defaultnormal_vertex: defaultnormal_vertex,
+		displacementmap_pars_vertex: displacementmap_pars_vertex,
+		displacementmap_vertex: displacementmap_vertex,
+		emissivemap_fragment: emissivemap_fragment,
+		emissivemap_pars_fragment: emissivemap_pars_fragment,
+		encodings_fragment: encodings_fragment,
+		encodings_pars_fragment: encodings_pars_fragment,
+		envmap_fragment: envmap_fragment,
+		envmap_common_pars_fragment: envmap_common_pars_fragment,
+		envmap_pars_fragment: envmap_pars_fragment,
+		envmap_pars_vertex: envmap_pars_vertex,
+		envmap_physical_pars_fragment: envmap_physical_pars_fragment,
+		envmap_vertex: envmap_vertex,
+		fog_vertex: fog_vertex,
+		fog_pars_vertex: fog_pars_vertex,
+		fog_fragment: fog_fragment,
+		fog_pars_fragment: fog_pars_fragment,
+		gradientmap_pars_fragment: gradientmap_pars_fragment,
+		lightmap_fragment: lightmap_fragment,
+		lightmap_pars_fragment: lightmap_pars_fragment,
+		lights_lambert_vertex: lights_lambert_vertex,
+		lights_pars_begin: lights_pars_begin,
+		lights_toon_fragment: lights_toon_fragment,
+		lights_toon_pars_fragment: lights_toon_pars_fragment,
+		lights_phong_fragment: lights_phong_fragment,
+		lights_phong_pars_fragment: lights_phong_pars_fragment,
+		lights_physical_fragment: lights_physical_fragment,
+		lights_physical_pars_fragment: lights_physical_pars_fragment,
+		lights_fragment_begin: lights_fragment_begin,
+		lights_fragment_maps: lights_fragment_maps,
+		lights_fragment_end: lights_fragment_end,
+		logdepthbuf_fragment: logdepthbuf_fragment,
+		logdepthbuf_pars_fragment: logdepthbuf_pars_fragment,
+		logdepthbuf_pars_vertex: logdepthbuf_pars_vertex,
+		logdepthbuf_vertex: logdepthbuf_vertex,
+		map_fragment: map_fragment,
+		map_pars_fragment: map_pars_fragment,
+		map_particle_fragment: map_particle_fragment,
+		map_particle_pars_fragment: map_particle_pars_fragment,
+		metalnessmap_fragment: metalnessmap_fragment,
+		metalnessmap_pars_fragment: metalnessmap_pars_fragment,
+		morphnormal_vertex: morphnormal_vertex,
+		morphtarget_pars_vertex: morphtarget_pars_vertex,
+		morphtarget_vertex: morphtarget_vertex,
+		normal_fragment_begin: normal_fragment_begin,
+		normal_fragment_maps: normal_fragment_maps,
+		normalmap_pars_fragment: normalmap_pars_fragment,
+		clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin,
+		clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps,
+		clearcoat_pars_fragment: clearcoat_pars_fragment,
+		packing: packing,
+		premultiplied_alpha_fragment: premultiplied_alpha_fragment,
+		project_vertex: project_vertex,
+		dithering_fragment: dithering_fragment,
+		dithering_pars_fragment: dithering_pars_fragment,
+		roughnessmap_fragment: roughnessmap_fragment,
+		roughnessmap_pars_fragment: roughnessmap_pars_fragment,
+		shadowmap_pars_fragment: shadowmap_pars_fragment,
+		shadowmap_pars_vertex: shadowmap_pars_vertex,
+		shadowmap_vertex: shadowmap_vertex,
+		shadowmask_pars_fragment: shadowmask_pars_fragment,
+		skinbase_vertex: skinbase_vertex,
+		skinning_pars_vertex: skinning_pars_vertex,
+		skinning_vertex: skinning_vertex,
+		skinnormal_vertex: skinnormal_vertex,
+		specularmap_fragment: specularmap_fragment,
+		specularmap_pars_fragment: specularmap_pars_fragment,
+		tonemapping_fragment: tonemapping_fragment,
+		tonemapping_pars_fragment: tonemapping_pars_fragment,
+		uv_pars_fragment: uv_pars_fragment,
+		uv_pars_vertex: uv_pars_vertex,
+		uv_vertex: uv_vertex,
+		uv2_pars_fragment: uv2_pars_fragment,
+		uv2_pars_vertex: uv2_pars_vertex,
+		uv2_vertex: uv2_vertex,
+		worldpos_vertex: worldpos_vertex,
 
-				( parameters.outputEncoding || parameters.mapEncoding || parameters.matcapEncoding || parameters.envMapEncoding || parameters.emissiveMapEncoding || parameters.lightMapEncoding ) ?
-					ShaderChunk[ 'encodings_pars_fragment' ] : '', // this code is required here because it is used by the various encoding/decoding function defined below
-				parameters.mapEncoding ? getTexelDecodingFunction( 'mapTexelToLinear', parameters.mapEncoding ) : '',
-				parameters.matcapEncoding ? getTexelDecodingFunction( 'matcapTexelToLinear', parameters.matcapEncoding ) : '',
-				parameters.envMapEncoding ? getTexelDecodingFunction( 'envMapTexelToLinear', parameters.envMapEncoding ) : '',
-				parameters.emissiveMapEncoding ? getTexelDecodingFunction( 'emissiveMapTexelToLinear', parameters.emissiveMapEncoding ) : '',
-				parameters.lightMapEncoding ? getTexelDecodingFunction( 'lightMapTexelToLinear', parameters.lightMapEncoding ) : '',
-				parameters.outputEncoding ? getTexelEncodingFunction( 'linearToOutputTexel', parameters.outputEncoding ) : '',
+		background_frag: background_frag,
+		background_vert: background_vert,
+		cube_frag: cube_frag,
+		cube_vert: cube_vert,
+		depth_frag: depth_frag,
+		depth_vert: depth_vert,
+		distanceRGBA_frag: distanceRGBA_frag,
+		distanceRGBA_vert: distanceRGBA_vert,
+		equirect_frag: equirect_frag,
+		equirect_vert: equirect_vert,
+		linedashed_frag: linedashed_frag,
+		linedashed_vert: linedashed_vert,
+		meshbasic_frag: meshbasic_frag,
+		meshbasic_vert: meshbasic_vert,
+		meshlambert_frag: meshlambert_frag,
+		meshlambert_vert: meshlambert_vert,
+		meshmatcap_frag: meshmatcap_frag,
+		meshmatcap_vert: meshmatcap_vert,
+		meshtoon_frag: meshtoon_frag,
+		meshtoon_vert: meshtoon_vert,
+		meshphong_frag: meshphong_frag,
+		meshphong_vert: meshphong_vert,
+		meshphysical_frag: meshphysical_frag,
+		meshphysical_vert: meshphysical_vert,
+		normal_frag: normal_frag,
+		normal_vert: normal_vert,
+		points_frag: points_frag,
+		points_vert: points_vert,
+		shadow_frag: shadow_frag,
+		shadow_vert: shadow_vert,
+		sprite_frag: sprite_frag,
+		sprite_vert: sprite_vert
+	};
 
-				parameters.depthPacking ? '#define DEPTH_PACKING ' + parameters.depthPacking : '',
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author mikael emtinger / http://gomo.se/
+	 */
 
-				'\n'
+	var ShaderLib = {
 
-			].filter( filterEmptyLine ).join( '\n' );
+		basic: {
 
-		}
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.specularmap,
+				UniformsLib.envmap,
+				UniformsLib.aomap,
+				UniformsLib.lightmap,
+				UniformsLib.fog
+			] ),
 
-		vertexShader = resolveIncludes( vertexShader );
-		vertexShader = replaceLightNums( vertexShader, parameters );
-		vertexShader = replaceClippingPlaneNums( vertexShader, parameters );
+			vertexShader: ShaderChunk.meshbasic_vert,
+			fragmentShader: ShaderChunk.meshbasic_frag
 
-		fragmentShader = resolveIncludes( fragmentShader );
-		fragmentShader = replaceLightNums( fragmentShader, parameters );
-		fragmentShader = replaceClippingPlaneNums( fragmentShader, parameters );
+		},
 
-		vertexShader = unrollLoops( vertexShader );
-		fragmentShader = unrollLoops( fragmentShader );
+		lambert: {
 
-		if ( parameters.isWebGL2 && ! parameters.isRawShaderMaterial ) {
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.specularmap,
+				UniformsLib.envmap,
+				UniformsLib.aomap,
+				UniformsLib.lightmap,
+				UniformsLib.emissivemap,
+				UniformsLib.fog,
+				UniformsLib.lights,
+				{
+					emissive: { value: new Color( 0x000000 ) }
+				}
+			] ),
 
-			var isGLSL3ShaderMaterial = false;
+			vertexShader: ShaderChunk.meshlambert_vert,
+			fragmentShader: ShaderChunk.meshlambert_frag
 
-			var versionRegex = /^\s*#version\s+300\s+es\s*\n/;
+		},
 
-			if ( parameters.isShaderMaterial &&
-				vertexShader.match( versionRegex ) !== null &&
-				fragmentShader.match( versionRegex ) !== null ) {
+		phong: {
 
-				isGLSL3ShaderMaterial = true;
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.specularmap,
+				UniformsLib.envmap,
+				UniformsLib.aomap,
+				UniformsLib.lightmap,
+				UniformsLib.emissivemap,
+				UniformsLib.bumpmap,
+				UniformsLib.normalmap,
+				UniformsLib.displacementmap,
+				UniformsLib.fog,
+				UniformsLib.lights,
+				{
+					emissive: { value: new Color( 0x000000 ) },
+					specular: { value: new Color( 0x111111 ) },
+					shininess: { value: 30 }
+				}
+			] ),
 
-				vertexShader = vertexShader.replace( versionRegex, '' );
-				fragmentShader = fragmentShader.replace( versionRegex, '' );
+			vertexShader: ShaderChunk.meshphong_vert,
+			fragmentShader: ShaderChunk.meshphong_frag
 
-			}
+		},
 
-			// GLSL 3.0 conversion
+		standard: {
 
-			prefixVertex = [
-				'#version 300 es\n',
-				'#define attribute in',
-				'#define varying out',
-				'#define texture2D texture'
-			].join( '\n' ) + '\n' + prefixVertex;
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.envmap,
+				UniformsLib.aomap,
+				UniformsLib.lightmap,
+				UniformsLib.emissivemap,
+				UniformsLib.bumpmap,
+				UniformsLib.normalmap,
+				UniformsLib.displacementmap,
+				UniformsLib.roughnessmap,
+				UniformsLib.metalnessmap,
+				UniformsLib.fog,
+				UniformsLib.lights,
+				{
+					emissive: { value: new Color( 0x000000 ) },
+					roughness: { value: 1.0 },
+					metalness: { value: 0.0 },
+					envMapIntensity: { value: 1 } // temporary
+				}
+			] ),
 
-			prefixFragment = [
-				'#version 300 es\n',
-				'#define varying in',
-				isGLSL3ShaderMaterial ? '' : 'out highp vec4 pc_fragColor;',
-				isGLSL3ShaderMaterial ? '' : '#define gl_FragColor pc_fragColor',
-				'#define gl_FragDepthEXT gl_FragDepth',
-				'#define texture2D texture',
-				'#define textureCube texture',
-				'#define texture2DProj textureProj',
-				'#define texture2DLodEXT textureLod',
-				'#define texture2DProjLodEXT textureProjLod',
-				'#define textureCubeLodEXT textureLod',
-				'#define texture2DGradEXT textureGrad',
-				'#define texture2DProjGradEXT textureProjGrad',
-				'#define textureCubeGradEXT textureGrad'
-			].join( '\n' ) + '\n' + prefixFragment;
+			vertexShader: ShaderChunk.meshphysical_vert,
+			fragmentShader: ShaderChunk.meshphysical_frag
 
-		}
+		},
 
-		var vertexGlsl = prefixVertex + vertexShader;
-		var fragmentGlsl = prefixFragment + fragmentShader;
+		toon: {
 
-		// console.log( '*VERTEX*', vertexGlsl );
-		// console.log( '*FRAGMENT*', fragmentGlsl );
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.specularmap,
+				UniformsLib.aomap,
+				UniformsLib.lightmap,
+				UniformsLib.emissivemap,
+				UniformsLib.bumpmap,
+				UniformsLib.normalmap,
+				UniformsLib.displacementmap,
+				UniformsLib.gradientmap,
+				UniformsLib.fog,
+				UniformsLib.lights,
+				{
+					emissive: { value: new Color( 0x000000 ) },
+					specular: { value: new Color( 0x111111 ) },
+					shininess: { value: 30 }
+				}
+			] ),
 
-		var glVertexShader = WebGLShader( gl, 35633, vertexGlsl );
-		var glFragmentShader = WebGLShader( gl, 35632, fragmentGlsl );
+			vertexShader: ShaderChunk.meshtoon_vert,
+			fragmentShader: ShaderChunk.meshtoon_frag
 
-		gl.attachShader( program, glVertexShader );
-		gl.attachShader( program, glFragmentShader );
-
-		// Force a particular attribute to index 0.
-
-		if ( parameters.index0AttributeName !== undefined ) {
+		},
 
-			gl.bindAttribLocation( program, 0, parameters.index0AttributeName );
+		matcap: {
 
-		} else if ( parameters.morphTargets === true ) {
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.bumpmap,
+				UniformsLib.normalmap,
+				UniformsLib.displacementmap,
+				UniformsLib.fog,
+				{
+					matcap: { value: null }
+				}
+			] ),
 
-			// programs with morphTargets displace position out of attribute 0
-			gl.bindAttribLocation( program, 0, 'position' );
+			vertexShader: ShaderChunk.meshmatcap_vert,
+			fragmentShader: ShaderChunk.meshmatcap_frag
 
-		}
+		},
 
-		gl.linkProgram( program );
+		points: {
 
-		// check for link errors
-		if ( renderer.debug.checkShaderErrors ) {
+			uniforms: mergeUniforms( [
+				UniformsLib.points,
+				UniformsLib.fog
+			] ),
 
-			var programLog = gl.getProgramInfoLog( program ).trim();
-			var vertexLog = gl.getShaderInfoLog( glVertexShader ).trim();
-			var fragmentLog = gl.getShaderInfoLog( glFragmentShader ).trim();
+			vertexShader: ShaderChunk.points_vert,
+			fragmentShader: ShaderChunk.points_frag
 
-			var runnable = true;
-			var haveDiagnostics = true;
+		},
 
-			if ( gl.getProgramParameter( program, 35714 ) === false ) {
+		dashed: {
 
-				runnable = false;
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.fog,
+				{
+					scale: { value: 1 },
+					dashSize: { value: 1 },
+					totalSize: { value: 2 }
+				}
+			] ),
 
-				var vertexErrors = getShaderErrors( gl, glVertexShader, 'vertex' );
-				var fragmentErrors = getShaderErrors( gl, glFragmentShader, 'fragment' );
+			vertexShader: ShaderChunk.linedashed_vert,
+			fragmentShader: ShaderChunk.linedashed_frag
 
-				console.error( 'THREE.WebGLProgram: shader error: ', gl.getError(), '35715', gl.getProgramParameter( program, 35715 ), 'gl.getProgramInfoLog', programLog, vertexErrors, fragmentErrors );
+		},
 
-			} else if ( programLog !== '' ) {
+		depth: {
 
-				console.warn( 'THREE.WebGLProgram: gl.getProgramInfoLog()', programLog );
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.displacementmap
+			] ),
 
-			} else if ( vertexLog === '' || fragmentLog === '' ) {
+			vertexShader: ShaderChunk.depth_vert,
+			fragmentShader: ShaderChunk.depth_frag
 
-				haveDiagnostics = false;
+		},
 
-			}
+		normal: {
 
-			if ( haveDiagnostics ) {
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.bumpmap,
+				UniformsLib.normalmap,
+				UniformsLib.displacementmap,
+				{
+					opacity: { value: 1.0 }
+				}
+			] ),
 
-				this.diagnostics = {
+			vertexShader: ShaderChunk.normal_vert,
+			fragmentShader: ShaderChunk.normal_frag
 
-					runnable: runnable,
+		},
 
-					programLog: programLog,
+		sprite: {
 
-					vertexShader: {
+			uniforms: mergeUniforms( [
+				UniformsLib.sprite,
+				UniformsLib.fog
+			] ),
 
-						log: vertexLog,
-						prefix: prefixVertex
+			vertexShader: ShaderChunk.sprite_vert,
+			fragmentShader: ShaderChunk.sprite_frag
 
-					},
+		},
 
-					fragmentShader: {
+		background: {
 
-						log: fragmentLog,
-						prefix: prefixFragment
+			uniforms: {
+				uvTransform: { value: new Matrix3() },
+				t2D: { value: null },
+			},
 
-					}
+			vertexShader: ShaderChunk.background_vert,
+			fragmentShader: ShaderChunk.background_frag
 
-				};
+		},
+		/* -------------------------------------------------------------------------
+		//	Cube map shader
+		 ------------------------------------------------------------------------- */
 
-			}
+		cube: {
 
-		}
+			uniforms: mergeUniforms( [
+				UniformsLib.envmap,
+				{
+					opacity: { value: 1.0 }
+				}
+			] ),
 
-		// Clean up
+			vertexShader: ShaderChunk.cube_vert,
+			fragmentShader: ShaderChunk.cube_frag
 
-		// Crashes in iOS9 and iOS10. #18402
-		// gl.detachShader( program, glVertexShader );
-		// gl.detachShader( program, glFragmentShader );
+		},
 
-		gl.deleteShader( glVertexShader );
-		gl.deleteShader( glFragmentShader );
+		equirect: {
 
-		// set up caching for uniform locations
+			uniforms: {
+				tEquirect: { value: null },
+			},
 
-		var cachedUniforms;
+			vertexShader: ShaderChunk.equirect_vert,
+			fragmentShader: ShaderChunk.equirect_frag
 
-		this.getUniforms = function () {
+		},
 
-			if ( cachedUniforms === undefined ) {
+		distanceRGBA: {
 
-				cachedUniforms = new WebGLUniforms( gl, program );
+			uniforms: mergeUniforms( [
+				UniformsLib.common,
+				UniformsLib.displacementmap,
+				{
+					referencePosition: { value: new Vector3() },
+					nearDistance: { value: 1 },
+					farDistance: { value: 1000 }
+				}
+			] ),
 
-			}
+			vertexShader: ShaderChunk.distanceRGBA_vert,
+			fragmentShader: ShaderChunk.distanceRGBA_frag
 
-			return cachedUniforms;
+		},
 
-		};
+		shadow: {
 
-		// set up caching for attribute locations
+			uniforms: mergeUniforms( [
+				UniformsLib.lights,
+				UniformsLib.fog,
+				{
+					color: { value: new Color( 0x00000 ) },
+					opacity: { value: 1.0 }
+				} ] ),
 
-		var cachedAttributes;
+			vertexShader: ShaderChunk.shadow_vert,
+			fragmentShader: ShaderChunk.shadow_frag
 
-		this.getAttributes = function () {
+		}
 
-			if ( cachedAttributes === undefined ) {
+	};
 
-				cachedAttributes = fetchAttributeLocations( gl, program );
+	ShaderLib.physical = {
 
+		uniforms: mergeUniforms( [
+			ShaderLib.standard.uniforms,
+			{
+				clearcoat: { value: 0 },
+				clearcoatMap: { value: null },
+				clearcoatRoughness: { value: 0 },
+				clearcoatRoughnessMap: { value: null },
+				clearcoatNormalScale: { value: new Vector2( 1, 1 ) },
+				clearcoatNormalMap: { value: null },
+				sheen: { value: new Color( 0x000000 ) },
+				transparency: { value: 0 },
 			}
+		] ),
 
-			return cachedAttributes;
-
-		};
-
-		// free resource
+		vertexShader: ShaderChunk.meshphysical_vert,
+		fragmentShader: ShaderChunk.meshphysical_frag
 
-		this.destroy = function () {
+	};
 
-			gl.deleteProgram( program );
-			this.program = undefined;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		};
+	function WebGLBackground( renderer, state, objects, premultipliedAlpha ) {
 
-		//
+		var clearColor = new Color( 0x000000 );
+		var clearAlpha = 0;
 
-		this.name = parameters.shaderName;
-		this.id = programIdCount ++;
-		this.cacheKey = cacheKey;
-		this.usedTimes = 1;
-		this.program = program;
-		this.vertexShader = glVertexShader;
-		this.fragmentShader = glFragmentShader;
+		var planeMesh;
+		var boxMesh;
 
-		return this;
+		var currentBackground = null;
+		var currentBackgroundVersion = 0;
+		var currentTonemapping = null;
 
-	}
+		function render( renderList, scene, camera, forceClear ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			var background = scene.background;
 
-	function WebGLPrograms( renderer, extensions, capabilities ) {
+			// Ignore background in AR
+			// TODO: Reconsider this.
 
-		var programs = [];
+			var xr = renderer.xr;
+			var session = xr.getSession && xr.getSession();
 
-		var isWebGL2 = capabilities.isWebGL2;
-		var logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
-		var floatVertexTextures = capabilities.floatVertexTextures;
-		var precision = capabilities.precision;
-		var maxVertexUniforms = capabilities.maxVertexUniforms;
-		var vertexTextures = capabilities.vertexTextures;
+			if ( session && session.environmentBlendMode === 'additive' ) {
 
-		var shaderIDs = {
-			MeshDepthMaterial: 'depth',
-			MeshDistanceMaterial: 'distanceRGBA',
-			MeshNormalMaterial: 'normal',
-			MeshBasicMaterial: 'basic',
-			MeshLambertMaterial: 'lambert',
-			MeshPhongMaterial: 'phong',
-			MeshToonMaterial: 'toon',
-			MeshStandardMaterial: 'physical',
-			MeshPhysicalMaterial: 'physical',
-			MeshMatcapMaterial: 'matcap',
-			LineBasicMaterial: 'basic',
-			LineDashedMaterial: 'dashed',
-			PointsMaterial: 'points',
-			ShadowMaterial: 'shadow',
-			SpriteMaterial: 'sprite'
-		};
+				background = null;
 
-		var parameterNames = [
-			"precision", "isWebGL2", "supportsVertexTextures", "outputEncoding", "instancing",
-			"map", "mapEncoding", "matcap", "matcapEncoding", "envMap", "envMapMode", "envMapEncoding", "envMapCubeUV",
-			"lightMap", "lightMapEncoding", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "objectSpaceNormalMap", "tangentSpaceNormalMap", "clearcoatMap", "clearcoatRoughnessMap", "clearcoatNormalMap", "displacementMap", "specularMap",
-			"roughnessMap", "metalnessMap", "gradientMap",
-			"alphaMap", "combine", "vertexColors", "vertexTangents", "vertexUvs", "uvsVertexOnly", "fog", "useFog", "fogExp2",
-			"flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
-			"maxBones", "useVertexTexture", "morphTargets", "morphNormals",
-			"maxMorphTargets", "maxMorphNormals", "premultipliedAlpha",
-			"numDirLights", "numPointLights", "numSpotLights", "numHemiLights", "numRectAreaLights",
-			"numDirLightShadows", "numPointLightShadows", "numSpotLightShadows",
-			"shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights',
-			"alphaTest", "doubleSided", "flipSided", "numClippingPlanes", "numClipIntersection", "depthPacking", "dithering",
-			"sheen"
-		];
+			}
 
-		function getShaderObject( material, shaderID ) {
+			if ( background === null ) {
 
-			var shaderobject;
+				setClear( clearColor, clearAlpha );
 
-			if ( shaderID ) {
+			} else if ( background && background.isColor ) {
 
-				var shader = ShaderLib[ shaderID ];
+				setClear( background, 1 );
+				forceClear = true;
 
-				shaderobject = {
-					name: material.type,
-					uniforms: UniformsUtils.clone( shader.uniforms ),
-					vertexShader: shader.vertexShader,
-					fragmentShader: shader.fragmentShader
-				};
+			}
 
-			} else {
+			if ( renderer.autoClear || forceClear ) {
 
-				shaderobject = {
-					name: material.type,
-					uniforms: material.uniforms,
-					vertexShader: material.vertexShader,
-					fragmentShader: material.fragmentShader
-				};
+				renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
 
 			}
 
-			return shaderobject;
+			if ( background && ( background.isCubeTexture || background.isWebGLCubeRenderTarget || background.mapping === CubeUVReflectionMapping ) ) {
 
-		}
+				if ( boxMesh === undefined ) {
 
-		function allocateBones( object ) {
+					boxMesh = new Mesh(
+						new BoxBufferGeometry( 1, 1, 1 ),
+						new ShaderMaterial( {
+							type: 'BackgroundCubeMaterial',
+							uniforms: cloneUniforms( ShaderLib.cube.uniforms ),
+							vertexShader: ShaderLib.cube.vertexShader,
+							fragmentShader: ShaderLib.cube.fragmentShader,
+							side: BackSide,
+							depthTest: false,
+							depthWrite: false,
+							fog: false
+						} )
+					);
 
-			var skeleton = object.skeleton;
-			var bones = skeleton.bones;
+					boxMesh.geometry.deleteAttribute( 'normal' );
+					boxMesh.geometry.deleteAttribute( 'uv' );
 
-			if ( floatVertexTextures ) {
+					boxMesh.onBeforeRender = function ( renderer, scene, camera ) {
 
-				return 1024;
+						this.matrixWorld.copyPosition( camera.matrixWorld );
 
-			} else {
+					};
 
-				// default for when object is not specified
-				// ( for example when prebuilding shader to be used with multiple objects )
-				//
-				//  - leave some extra space for other uniforms
-				//  - limit here is ANGLE's 254 max uniform vectors
-				//    (up to 54 should be safe)
+					// enable code injection for non-built-in material
+					Object.defineProperty( boxMesh.material, 'envMap', {
 
-				var nVertexUniforms = maxVertexUniforms;
-				var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
+						get: function () {
 
-				var maxBones = Math.min( nVertexMatrices, bones.length );
+							return this.uniforms.envMap.value;
 
-				if ( maxBones < bones.length ) {
+						}
 
-					console.warn( 'THREE.WebGLRenderer: Skeleton has ' + bones.length + ' bones. This GPU supports ' + maxBones + '.' );
-					return 0;
+					} );
+
+					objects.update( boxMesh );
 
 				}
 
-				return maxBones;
+				var texture = background.isWebGLCubeRenderTarget ? background.texture : background;
 
-			}
+				boxMesh.material.uniforms.envMap.value = texture;
+				boxMesh.material.uniforms.flipEnvMap.value = texture.isCubeTexture ? - 1 : 1;
 
-		}
+				if ( currentBackground !== background ||
+					currentBackgroundVersion !== texture.version ||
+					currentTonemapping !== renderer.toneMapping ) {
 
-		function getTextureEncodingFromMap( map ) {
+					boxMesh.material.needsUpdate = true;
 
-			var encoding;
+					currentBackground = background;
+					currentBackgroundVersion = texture.version;
+					currentTonemapping = renderer.toneMapping;
 
-			if ( ! map ) {
+				}
 
-				encoding = LinearEncoding;
+				// push to the pre-sorted opaque render list
+				renderList.unshift( boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null );
 
-			} else if ( map.isTexture ) {
+			} else if ( background && background.isTexture ) {
 
-				encoding = map.encoding;
+				if ( planeMesh === undefined ) {
 
-			} else if ( map.isWebGLRenderTarget ) {
+					planeMesh = new Mesh(
+						new PlaneBufferGeometry( 2, 2 ),
+						new ShaderMaterial( {
+							type: 'BackgroundMaterial',
+							uniforms: cloneUniforms( ShaderLib.background.uniforms ),
+							vertexShader: ShaderLib.background.vertexShader,
+							fragmentShader: ShaderLib.background.fragmentShader,
+							side: FrontSide,
+							depthTest: false,
+							depthWrite: false,
+							fog: false
+						} )
+					);
 
-				console.warn( "THREE.WebGLPrograms.getTextureEncodingFromMap: don't use render targets as textures. Use their .texture property instead." );
-				encoding = map.texture.encoding;
+					planeMesh.geometry.deleteAttribute( 'normal' );
 
-			}
+					// enable code injection for non-built-in material
+					Object.defineProperty( planeMesh.material, 'map', {
 
-			return encoding;
+						get: function () {
 
-		}
+							return this.uniforms.t2D.value;
 
-		this.getParameters = function ( material, lights, shadows, scene, nClipPlanes, nClipIntersection, object ) {
+						}
 
-			var fog = scene.fog;
-			var environment = material.isMeshStandardMaterial ? scene.environment : null;
+					} );
 
-			var envMap = material.envMap || environment;
+					objects.update( planeMesh );
 
-			var shaderID = shaderIDs[ material.type ];
+				}
 
-			// heuristics to create shader parameters according to lights in the scene
-			// (not to blow over maxLights budget)
+				planeMesh.material.uniforms.t2D.value = background;
 
-			var maxBones = object.isSkinnedMesh ? allocateBones( object ) : 0;
+				if ( background.matrixAutoUpdate === true ) {
 
-			if ( material.precision !== null ) {
+					background.updateMatrix();
 
-				precision = capabilities.getMaxPrecision( material.precision );
+				}
 
-				if ( precision !== material.precision ) {
+				planeMesh.material.uniforms.uvTransform.value.copy( background.matrix );
 
-					console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
+				if ( currentBackground !== background ||
+					currentBackgroundVersion !== background.version ||
+					currentTonemapping !== renderer.toneMapping ) {
+
+					planeMesh.material.needsUpdate = true;
+
+					currentBackground = background;
+					currentBackgroundVersion = background.version;
+					currentTonemapping = renderer.toneMapping;
 
 				}
 
-			}
 
-			var shaderobject = getShaderObject( material, shaderID );
-			material.onBeforeCompile( shaderobject, renderer );
+				// push to the pre-sorted opaque render list
+				renderList.unshift( planeMesh, planeMesh.geometry, planeMesh.material, 0, 0, null );
 
-			var currentRenderTarget = renderer.getRenderTarget();
+			}
 
-			var parameters = {
+		}
 
-				isWebGL2: isWebGL2,
+		function setClear( color, alpha ) {
 
-				shaderID: shaderID,
-				shaderName: shaderobject.name,
+			state.buffers.color.setClear( color.r, color.g, color.b, alpha, premultipliedAlpha );
 
-				uniforms: shaderobject.uniforms,
-				vertexShader: shaderobject.vertexShader,
-				fragmentShader: shaderobject.fragmentShader,
-				defines: material.defines,
+		}
 
-				isRawShaderMaterial: material.isRawShaderMaterial,
-				isShaderMaterial: material.isShaderMaterial,
+		return {
 
-				precision: precision,
+			getClearColor: function () {
 
-				instancing: object.isInstancedMesh === true,
+				return clearColor;
 
-				supportsVertexTextures: vertexTextures,
-				outputEncoding: ( currentRenderTarget !== null ) ? getTextureEncodingFromMap( currentRenderTarget.texture ) : renderer.outputEncoding,
-				map: !! material.map,
-				mapEncoding: getTextureEncodingFromMap( material.map ),
-				matcap: !! material.matcap,
-				matcapEncoding: getTextureEncodingFromMap( material.matcap ),
-				envMap: !! envMap,
-				envMapMode: envMap && envMap.mapping,
-				envMapEncoding: getTextureEncodingFromMap( envMap ),
-				envMapCubeUV: ( !! envMap ) && ( ( envMap.mapping === CubeUVReflectionMapping ) || ( envMap.mapping === CubeUVRefractionMapping ) ),
-				lightMap: !! material.lightMap,
-				lightMapEncoding: getTextureEncodingFromMap( material.lightMap ),
-				aoMap: !! material.aoMap,
-				emissiveMap: !! material.emissiveMap,
-				emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap ),
-				bumpMap: !! material.bumpMap,
-				normalMap: !! material.normalMap,
-				objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap,
-				tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap,
-				clearcoatMap: !! material.clearcoatMap,
-				clearcoatRoughnessMap: !! material.clearcoatRoughnessMap,
-				clearcoatNormalMap: !! material.clearcoatNormalMap,
-				displacementMap: !! material.displacementMap,
-				roughnessMap: !! material.roughnessMap,
-				metalnessMap: !! material.metalnessMap,
-				specularMap: !! material.specularMap,
-				alphaMap: !! material.alphaMap,
+			},
+			setClearColor: function ( color, alpha ) {
 
-				gradientMap: !! material.gradientMap,
+				clearColor.set( color );
+				clearAlpha = alpha !== undefined ? alpha : 1;
+				setClear( clearColor, clearAlpha );
 
-				sheen: !! material.sheen,
+			},
+			getClearAlpha: function () {
 
-				combine: material.combine,
+				return clearAlpha;
 
-				vertexTangents: ( material.normalMap && material.vertexTangents ),
-				vertexColors: material.vertexColors,
-				vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatMap || !! material.clearcoatRoughnessMap || !! material.clearcoatNormalMap || !! material.displacementMap,
-				uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap ) && !! material.displacementMap,
+			},
+			setClearAlpha: function ( alpha ) {
 
-				fog: !! fog,
-				useFog: material.fog,
-				fogExp2: ( fog && fog.isFogExp2 ),
+				clearAlpha = alpha;
+				setClear( clearColor, clearAlpha );
 
-				flatShading: material.flatShading,
+			},
+			render: render
 
-				sizeAttenuation: material.sizeAttenuation,
-				logarithmicDepthBuffer: logarithmicDepthBuffer,
+		};
 
-				skinning: material.skinning && maxBones > 0,
-				maxBones: maxBones,
-				useVertexTexture: floatVertexTextures,
-
-				morphTargets: material.morphTargets,
-				morphNormals: material.morphNormals,
-				maxMorphTargets: renderer.maxMorphTargets,
-				maxMorphNormals: renderer.maxMorphNormals,
-
-				numDirLights: lights.directional.length,
-				numPointLights: lights.point.length,
-				numSpotLights: lights.spot.length,
-				numRectAreaLights: lights.rectArea.length,
-				numHemiLights: lights.hemi.length,
+	}
 
-				numDirLightShadows: lights.directionalShadowMap.length,
-				numPointLightShadows: lights.pointShadowMap.length,
-				numSpotLightShadows: lights.spotShadowMap.length,
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				numClippingPlanes: nClipPlanes,
-				numClipIntersection: nClipIntersection,
+	function WebGLBufferRenderer( gl, extensions, info, capabilities ) {
 
-				dithering: material.dithering,
+		var isWebGL2 = capabilities.isWebGL2;
 
-				shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0,
-				shadowMapType: renderer.shadowMap.type,
+		var mode;
 
-				toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping,
-				physicallyCorrectLights: renderer.physicallyCorrectLights,
+		function setMode( value ) {
 
-				premultipliedAlpha: material.premultipliedAlpha,
+			mode = value;
 
-				alphaTest: material.alphaTest,
-				doubleSided: material.side === DoubleSide,
-				flipSided: material.side === BackSide,
+		}
 
-				depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false,
+		function render( start, count ) {
 
-				index0AttributeName: material.index0AttributeName,
+			gl.drawArrays( mode, start, count );
 
-				extensionDerivatives: material.extensions && material.extensions.derivatives,
-				extensionFragDepth: material.extensions && material.extensions.fragDepth,
-				extensionDrawBuffers: material.extensions && material.extensions.drawBuffers,
-				extensionShaderTextureLOD: material.extensions && material.extensions.shaderTextureLOD,
+			info.update( count, mode );
 
-				rendererExtensionFragDepth: isWebGL2 || extensions.get( 'EXT_frag_depth' ) !== null,
-				rendererExtensionDrawBuffers: isWebGL2 || extensions.get( 'WEBGL_draw_buffers' ) !== null,
-				rendererExtensionShaderTextureLod: isWebGL2 || extensions.get( 'EXT_shader_texture_lod' ) !== null,
+		}
 
-				onBeforeCompile: material.onBeforeCompile
+		function renderInstances( geometry, start, count, primcount ) {
 
-			};
+			if ( primcount === 0 ) { return; }
 
-			return parameters;
+			var extension, methodName;
 
-		};
+			if ( isWebGL2 ) {
 
-		this.getProgramCacheKey = function ( parameters ) {
+				extension = gl;
+				methodName = 'drawArraysInstanced';
 
-			var array = [];
+			} else {
 
-			if ( parameters.shaderID ) {
+				extension = extensions.get( 'ANGLE_instanced_arrays' );
+				methodName = 'drawArraysInstancedANGLE';
 
-				array.push( parameters.shaderID );
+				if ( extension === null ) {
 
-			} else {
+					console.error( 'THREE.WebGLBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
+					return;
 
-				array.push( parameters.fragmentShader );
-				array.push( parameters.vertexShader );
+				}
 
 			}
 
-			if ( parameters.defines !== undefined ) {
+			extension[ methodName ]( mode, start, count, primcount );
 
-				for ( var name in parameters.defines ) {
+			info.update( count, mode, primcount );
 
-					array.push( name );
-					array.push( parameters.defines[ name ] );
+		}
 
-				}
+		//
 
-			}
+		this.setMode = setMode;
+		this.render = render;
+		this.renderInstances = renderInstances;
 
-			if ( parameters.isRawShaderMaterial === undefined ) {
+	}
 
-				for ( var i = 0; i < parameterNames.length; i ++ ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-					array.push( parameters[ parameterNames[ i ] ] );
+	function WebGLCapabilities( gl, extensions, parameters ) {
 
-				}
+		var maxAnisotropy;
 
-				array.push( renderer.outputEncoding );
-				array.push( renderer.gammaFactor );
+		function getMaxAnisotropy() {
 
-			}
+			if ( maxAnisotropy !== undefined ) { return maxAnisotropy; }
 
-			array.push( parameters.onBeforeCompile.toString() );
+			var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
 
-			return array.join();
+			if ( extension !== null ) {
 
-		};
+				maxAnisotropy = gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT );
 
-		this.acquireProgram = function ( parameters, cacheKey ) {
+			} else {
 
-			var program;
+				maxAnisotropy = 0;
 
-			// Check if code has been already compiled
-			for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
+			}
 
-				var preexistingProgram = programs[ p ];
+			return maxAnisotropy;
 
-				if ( preexistingProgram.cacheKey === cacheKey ) {
+		}
 
-					program = preexistingProgram;
-					++ program.usedTimes;
+		function getMaxPrecision( precision ) {
 
-					break;
+			if ( precision === 'highp' ) {
 
-				}
+				if ( gl.getShaderPrecisionFormat( 35633, 36338 ).precision > 0 &&
+					gl.getShaderPrecisionFormat( 35632, 36338 ).precision > 0 ) {
 
-			}
+					return 'highp';
 
-			if ( program === undefined ) {
+				}
 
-				program = new WebGLProgram( renderer, cacheKey, parameters );
-				programs.push( program );
+				precision = 'mediump';
 
 			}
 
-			return program;
-
-		};
-
-		this.releaseProgram = function ( program ) {
+			if ( precision === 'mediump' ) {
 
-			if ( -- program.usedTimes === 0 ) {
+				if ( gl.getShaderPrecisionFormat( 35633, 36337 ).precision > 0 &&
+					gl.getShaderPrecisionFormat( 35632, 36337 ).precision > 0 ) {
 
-				// Remove from unordered set
-				var i = programs.indexOf( program );
-				programs[ i ] = programs[ programs.length - 1 ];
-				programs.pop();
+					return 'mediump';
 
-				// Free WebGL resources
-				program.destroy();
+				}
 
 			}
 
-		};
-
-		// Exposed for resource monitoring & error feedback via renderer.info:
-		this.programs = programs;
-
-	}
+			return 'lowp';
 
-	/**
-	 * @author fordacious / fordacious.github.io
-	 */
+		}
 
-	function WebGLProperties() {
+		/* eslint-disable no-undef */
+		var isWebGL2 = ( typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext ) ||
+			( typeof WebGL2ComputeRenderingContext !== 'undefined' && gl instanceof WebGL2ComputeRenderingContext );
+		/* eslint-enable no-undef */
 
-		var properties = new WeakMap();
+		var precision = parameters.precision !== undefined ? parameters.precision : 'highp';
+		var maxPrecision = getMaxPrecision( precision );
 
-		function get( object ) {
+		if ( maxPrecision !== precision ) {
 
-			var map = properties.get( object );
+			console.warn( 'THREE.WebGLRenderer:', precision, 'not supported, using', maxPrecision, 'instead.' );
+			precision = maxPrecision;
 
-			if ( map === undefined ) {
+		}
 
-				map = {};
-				properties.set( object, map );
+		var logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true;
 
-			}
+		var maxTextures = gl.getParameter( 34930 );
+		var maxVertexTextures = gl.getParameter( 35660 );
+		var maxTextureSize = gl.getParameter( 3379 );
+		var maxCubemapSize = gl.getParameter( 34076 );
 
-			return map;
+		var maxAttributes = gl.getParameter( 34921 );
+		var maxVertexUniforms = gl.getParameter( 36347 );
+		var maxVaryings = gl.getParameter( 36348 );
+		var maxFragmentUniforms = gl.getParameter( 36349 );
 
-		}
+		var vertexTextures = maxVertexTextures > 0;
+		var floatFragmentTextures = isWebGL2 || !! extensions.get( 'OES_texture_float' );
+		var floatVertexTextures = vertexTextures && floatFragmentTextures;
 
-		function remove( object ) {
+		var maxSamples = isWebGL2 ? gl.getParameter( 36183 ) : 0;
 
-			properties.delete( object );
+		return {
 
-		}
+			isWebGL2: isWebGL2,
 
-		function update( object, key, value ) {
+			getMaxAnisotropy: getMaxAnisotropy,
+			getMaxPrecision: getMaxPrecision,
 
-			properties.get( object )[ key ] = value;
+			precision: precision,
+			logarithmicDepthBuffer: logarithmicDepthBuffer,
 
-		}
+			maxTextures: maxTextures,
+			maxVertexTextures: maxVertexTextures,
+			maxTextureSize: maxTextureSize,
+			maxCubemapSize: maxCubemapSize,
 
-		function dispose() {
+			maxAttributes: maxAttributes,
+			maxVertexUniforms: maxVertexUniforms,
+			maxVaryings: maxVaryings,
+			maxFragmentUniforms: maxFragmentUniforms,
 
-			properties = new WeakMap();
+			vertexTextures: vertexTextures,
+			floatFragmentTextures: floatFragmentTextures,
+			floatVertexTextures: floatVertexTextures,
 
-		}
+			maxSamples: maxSamples
 
-		return {
-			get: get,
-			remove: remove,
-			update: update,
-			dispose: dispose
 		};
 
 	}
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author tschw
 	 */
 
-	function painterSortStable( a, b ) {
+	function WebGLClipping() {
 
-		if ( a.groupOrder !== b.groupOrder ) {
+		var scope = this,
 
-			return a.groupOrder - b.groupOrder;
+			globalState = null,
+			numGlobalPlanes = 0,
+			localClippingEnabled = false,
+			renderingShadows = false,
 
-		} else if ( a.renderOrder !== b.renderOrder ) {
+			plane = new Plane(),
+			viewNormalMatrix = new Matrix3(),
 
-			return a.renderOrder - b.renderOrder;
+			uniform = { value: null, needsUpdate: false };
 
-		} else if ( a.program !== b.program ) {
+		this.uniform = uniform;
+		this.numPlanes = 0;
+		this.numIntersection = 0;
 
-			return a.program.id - b.program.id;
+		this.init = function ( planes, enableLocalClipping, camera ) {
 
-		} else if ( a.material.id !== b.material.id ) {
+			var enabled =
+				planes.length !== 0 ||
+				enableLocalClipping ||
+				// enable state of previous frame - the clipping code has to
+				// run another frame in order to reset the state:
+				numGlobalPlanes !== 0 ||
+				localClippingEnabled;
 
-			return a.material.id - b.material.id;
+			localClippingEnabled = enableLocalClipping;
 
-		} else if ( a.z !== b.z ) {
+			globalState = projectPlanes( planes, camera, 0 );
+			numGlobalPlanes = planes.length;
 
-			return a.z - b.z;
+			return enabled;
 
-		} else {
+		};
 
-			return a.id - b.id;
+		this.beginShadows = function () {
 
-		}
+			renderingShadows = true;
+			projectPlanes( null );
 
-	}
+		};
 
-	function reversePainterSortStable( a, b ) {
+		this.endShadows = function () {
 
-		if ( a.groupOrder !== b.groupOrder ) {
+			renderingShadows = false;
+			resetGlobalState();
 
-			return a.groupOrder - b.groupOrder;
+		};
 
-		} else if ( a.renderOrder !== b.renderOrder ) {
+		this.setState = function ( planes, clipIntersection, clipShadows, camera, cache, fromCache ) {
 
-			return a.renderOrder - b.renderOrder;
+			if ( ! localClippingEnabled || planes === null || planes.length === 0 || renderingShadows && ! clipShadows ) {
 
-		} else if ( a.z !== b.z ) {
+				// there's no local clipping
 
-			return b.z - a.z;
+				if ( renderingShadows ) {
 
-		} else {
+					// there's no global clipping
 
-			return a.id - b.id;
+					projectPlanes( null );
 
-		}
+				} else {
 
-	}
+					resetGlobalState();
 
+				}
 
-	function WebGLRenderList() {
+			} else {
 
-		var renderItems = [];
-		var renderItemsIndex = 0;
+				var nGlobal = renderingShadows ? 0 : numGlobalPlanes,
+					lGlobal = nGlobal * 4,
 
-		var opaque = [];
-		var transparent = [];
+					dstArray = cache.clippingState || null;
 
-		var defaultProgram = { id: - 1 };
+				uniform.value = dstArray; // ensure unique state
 
-		function init() {
+				dstArray = projectPlanes( planes, camera, lGlobal, fromCache );
 
-			renderItemsIndex = 0;
+				for ( var i = 0; i !== lGlobal; ++ i ) {
 
-			opaque.length = 0;
-			transparent.length = 0;
+					dstArray[ i ] = globalState[ i ];
 
-		}
+				}
 
-		function getNextRenderItem( object, geometry, material, groupOrder, z, group ) {
+				cache.clippingState = dstArray;
+				this.numIntersection = clipIntersection ? this.numPlanes : 0;
+				this.numPlanes += nGlobal;
 
-			var renderItem = renderItems[ renderItemsIndex ];
+			}
 
-			if ( renderItem === undefined ) {
 
-				renderItem = {
-					id: object.id,
-					object: object,
-					geometry: geometry,
-					material: material,
-					program: material.program || defaultProgram,
-					groupOrder: groupOrder,
-					renderOrder: object.renderOrder,
-					z: z,
-					group: group
-				};
+		};
 
-				renderItems[ renderItemsIndex ] = renderItem;
+		function resetGlobalState() {
 
-			} else {
+			if ( uniform.value !== globalState ) {
 
-				renderItem.id = object.id;
-				renderItem.object = object;
-				renderItem.geometry = geometry;
-				renderItem.material = material;
-				renderItem.program = material.program || defaultProgram;
-				renderItem.groupOrder = groupOrder;
-				renderItem.renderOrder = object.renderOrder;
-				renderItem.z = z;
-				renderItem.group = group;
+				uniform.value = globalState;
+				uniform.needsUpdate = numGlobalPlanes > 0;
 
 			}
 
-			renderItemsIndex ++;
-
-			return renderItem;
+			scope.numPlanes = numGlobalPlanes;
+			scope.numIntersection = 0;
 
 		}
 
-		function push( object, geometry, material, groupOrder, z, group ) {
-
-			var renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group );
+		function projectPlanes( planes, camera, dstOffset, skipTransform ) {
 
-			( material.transparent === true ? transparent : opaque ).push( renderItem );
+			var nPlanes = planes !== null ? planes.length : 0,
+				dstArray = null;
 
-		}
+			if ( nPlanes !== 0 ) {
 
-		function unshift( object, geometry, material, groupOrder, z, group ) {
+				dstArray = uniform.value;
 
-			var renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group );
+				if ( skipTransform !== true || dstArray === null ) {
 
-			( material.transparent === true ? transparent : opaque ).unshift( renderItem );
+					var flatSize = dstOffset + nPlanes * 4,
+						viewMatrix = camera.matrixWorldInverse;
 
-		}
+					viewNormalMatrix.getNormalMatrix( viewMatrix );
 
-		function sort( customOpaqueSort, customTransparentSort ) {
+					if ( dstArray === null || dstArray.length < flatSize ) {
 
-			if ( opaque.length > 1 ) { opaque.sort( customOpaqueSort || painterSortStable ); }
-			if ( transparent.length > 1 ) { transparent.sort( customTransparentSort || reversePainterSortStable ); }
+						dstArray = new Float32Array( flatSize );
 
-		}
+					}
 
-		function finish() {
+					for ( var i = 0, i4 = dstOffset; i !== nPlanes; ++ i, i4 += 4 ) {
 
-			// Clear references from inactive renderItems in the list
+						plane.copy( planes[ i ] ).applyMatrix4( viewMatrix, viewNormalMatrix );
 
-			for ( var i = renderItemsIndex, il = renderItems.length; i < il; i ++ ) {
+						plane.normal.toArray( dstArray, i4 );
+						dstArray[ i4 + 3 ] = plane.constant;
 
-				var renderItem = renderItems[ i ];
+					}
 
-				if ( renderItem.id === null ) { break; }
+				}
 
-				renderItem.id = null;
-				renderItem.object = null;
-				renderItem.geometry = null;
-				renderItem.material = null;
-				renderItem.program = null;
-				renderItem.group = null;
+				uniform.value = dstArray;
+				uniform.needsUpdate = true;
 
 			}
 
-		}
-
-		return {
-			opaque: opaque,
-			transparent: transparent,
+			scope.numPlanes = nPlanes;
+			scope.numIntersection = 0;
 
-			init: init,
-			push: push,
-			unshift: unshift,
-			finish: finish,
+			return dstArray;
 
-			sort: sort
-		};
+		}
 
 	}
 
-	function WebGLRenderLists() {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		var lists = new WeakMap();
+	function WebGLExtensions( gl ) {
 
-		function onSceneDispose( event ) {
+		var extensions = {};
 
-			var scene = event.target;
+		return {
 
-			scene.removeEventListener( 'dispose', onSceneDispose );
+			get: function ( name ) {
 
-			lists.delete( scene );
+				if ( extensions[ name ] !== undefined ) {
 
-		}
+					return extensions[ name ];
 
-		function get( scene, camera ) {
+				}
 
-			var cameras = lists.get( scene );
-			var list;
-			if ( cameras === undefined ) {
+				var extension;
 
-				list = new WebGLRenderList();
-				lists.set( scene, new WeakMap() );
-				lists.get( scene ).set( camera, list );
+				switch ( name ) {
 
-				scene.addEventListener( 'dispose', onSceneDispose );
+					case 'WEBGL_depth_texture':
+						extension = gl.getExtension( 'WEBGL_depth_texture' ) || gl.getExtension( 'MOZ_WEBGL_depth_texture' ) || gl.getExtension( 'WEBKIT_WEBGL_depth_texture' );
+						break;
 
-			} else {
+					case 'EXT_texture_filter_anisotropic':
+						extension = gl.getExtension( 'EXT_texture_filter_anisotropic' ) || gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
+						break;
 
-				list = cameras.get( camera );
-				if ( list === undefined ) {
+					case 'WEBGL_compressed_texture_s3tc':
+						extension = gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
+						break;
 
-					list = new WebGLRenderList();
-					cameras.set( camera, list );
+					case 'WEBGL_compressed_texture_pvrtc':
+						extension = gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
+						break;
+
+					default:
+						extension = gl.getExtension( name );
 
 				}
 
-			}
+				if ( extension === null ) {
 
-			return list;
+					console.warn( 'THREE.WebGLRenderer: ' + name + ' extension not supported.' );
 
-		}
+				}
 
-		function dispose() {
+				extensions[ name ] = extension;
 
-			lists = new WeakMap();
+				return extension;
 
-		}
+			}
 
-		return {
-			get: get,
-			dispose: dispose
 		};
 
 	}
@@ -27653,12424 +35147,12255 @@ function simpleEnd(buf) {
 	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function UniformsCache() {
+	function WebGLGeometries( gl, attributes, info ) {
 
-		var lights = {};
+		var geometries = new WeakMap();
+		var wireframeAttributes = new WeakMap();
 
-		return {
+		function onGeometryDispose( event ) {
 
-			get: function ( light ) {
+			var geometry = event.target;
+			var buffergeometry = geometries.get( geometry );
 
-				if ( lights[ light.id ] !== undefined ) {
+			if ( buffergeometry.index !== null ) {
 
-					return lights[ light.id ];
+				attributes.remove( buffergeometry.index );
 
-				}
+			}
 
-				var uniforms;
+			for ( var name in buffergeometry.attributes ) {
 
-				switch ( light.type ) {
+				attributes.remove( buffergeometry.attributes[ name ] );
 
-					case 'DirectionalLight':
-						uniforms = {
-							direction: new Vector3(),
-							color: new Color()
-						};
-						break;
+			}
 
-					case 'SpotLight':
-						uniforms = {
-							position: new Vector3(),
-							direction: new Vector3(),
-							color: new Color(),
-							distance: 0,
-							coneCos: 0,
-							penumbraCos: 0,
-							decay: 0
-						};
-						break;
+			geometry.removeEventListener( 'dispose', onGeometryDispose );
 
-					case 'PointLight':
-						uniforms = {
-							position: new Vector3(),
-							color: new Color(),
-							distance: 0,
-							decay: 0
-						};
-						break;
+			geometries.delete( geometry );
 
-					case 'HemisphereLight':
-						uniforms = {
-							direction: new Vector3(),
-							skyColor: new Color(),
-							groundColor: new Color()
-						};
-						break;
+			var attribute = wireframeAttributes.get( buffergeometry );
 
-					case 'RectAreaLight':
-						uniforms = {
-							color: new Color(),
-							position: new Vector3(),
-							halfWidth: new Vector3(),
-							halfHeight: new Vector3()
-						};
-						break;
+			if ( attribute ) {
 
-				}
+				attributes.remove( attribute );
+				wireframeAttributes.delete( buffergeometry );
 
-				lights[ light.id ] = uniforms;
+			}
 
-				return uniforms;
+			//
 
-			}
+			info.memory.geometries --;
 
-		};
+		}
 
-	}
+		function get( object, geometry ) {
 
-	function ShadowUniformsCache() {
+			var buffergeometry = geometries.get( geometry );
 
-		var lights = {};
+			if ( buffergeometry ) { return buffergeometry; }
 
-		return {
+			geometry.addEventListener( 'dispose', onGeometryDispose );
 
-			get: function ( light ) {
+			if ( geometry.isBufferGeometry ) {
 
-				if ( lights[ light.id ] !== undefined ) {
+				buffergeometry = geometry;
 
-					return lights[ light.id ];
+			} else if ( geometry.isGeometry ) {
+
+				if ( geometry._bufferGeometry === undefined ) {
+
+					geometry._bufferGeometry = new BufferGeometry().setFromObject( object );
 
 				}
 
-				var uniforms;
+				buffergeometry = geometry._bufferGeometry;
 
-				switch ( light.type ) {
+			}
 
-					case 'DirectionalLight':
-						uniforms = {
-							shadowBias: 0,
-							shadowRadius: 1,
-							shadowMapSize: new Vector2()
-						};
-						break;
+			geometries.set( geometry, buffergeometry );
 
-					case 'SpotLight':
-						uniforms = {
-							shadowBias: 0,
-							shadowRadius: 1,
-							shadowMapSize: new Vector2()
-						};
-						break;
+			info.memory.geometries ++;
 
-					case 'PointLight':
-						uniforms = {
-							shadowBias: 0,
-							shadowRadius: 1,
-							shadowMapSize: new Vector2(),
-							shadowCameraNear: 1,
-							shadowCameraFar: 1000
-						};
-						break;
+			return buffergeometry;
 
-					// TODO (abelnation): set RectAreaLight shadow uniforms
+		}
 
-				}
+		function update( geometry ) {
 
-				lights[ light.id ] = uniforms;
+			var index = geometry.index;
+			var geometryAttributes = geometry.attributes;
 
-				return uniforms;
+			if ( index !== null ) {
 
-			}
+				attributes.update( index, 34963 );
 
-		};
+			}
 
-	}
+			for ( var name in geometryAttributes ) {
 
+				attributes.update( geometryAttributes[ name ], 34962 );
 
+			}
 
-	var nextVersion = 0;
+			// morph targets
 
-	function shadowCastingLightsFirst( lightA, lightB ) {
+			var morphAttributes = geometry.morphAttributes;
 
-		return ( lightB.castShadow ? 1 : 0 ) - ( lightA.castShadow ? 1 : 0 );
+			for ( var name in morphAttributes ) {
 
-	}
+				var array = morphAttributes[ name ];
 
-	function WebGLLights() {
+				for ( var i = 0, l = array.length; i < l; i ++ ) {
 
-		var cache = new UniformsCache();
+					attributes.update( array[ i ], 34962 );
 
-		var shadowCache = ShadowUniformsCache();
+				}
 
-		var state = {
+			}
 
-			version: 0,
+		}
 
-			hash: {
-				directionalLength: - 1,
-				pointLength: - 1,
-				spotLength: - 1,
-				rectAreaLength: - 1,
-				hemiLength: - 1,
+		function updateWireframeAttribute( geometry ) {
 
-				numDirectionalShadows: - 1,
-				numPointShadows: - 1,
-				numSpotShadows: - 1
-			},
+			var indices = [];
 
-			ambient: [ 0, 0, 0 ],
-			probe: [],
-			directional: [],
-			directionalShadow: [],
-			directionalShadowMap: [],
-			directionalShadowMatrix: [],
-			spot: [],
-			spotShadow: [],
-			spotShadowMap: [],
-			spotShadowMatrix: [],
-			rectArea: [],
-			point: [],
-			pointShadow: [],
-			pointShadowMap: [],
-			pointShadowMatrix: [],
-			hemi: []
+			var geometryIndex = geometry.index;
+			var geometryPosition = geometry.attributes.position;
+			var version = 0;
 
-		};
+			if ( geometryIndex !== null ) {
 
-		for ( var i = 0; i < 9; i ++ ) { state.probe.push( new Vector3() ); }
+				var array = geometryIndex.array;
+				version = geometryIndex.version;
 
-		var vector3 = new Vector3();
-		var matrix4 = new Matrix4();
-		var matrix42 = new Matrix4();
+				for ( var i = 0, l = array.length; i < l; i += 3 ) {
 
-		function setup( lights, shadows, camera ) {
+					var a = array[ i + 0 ];
+					var b = array[ i + 1 ];
+					var c = array[ i + 2 ];
 
-			var r = 0, g = 0, b = 0;
+					indices.push( a, b, b, c, c, a );
 
-			for ( var i = 0; i < 9; i ++ ) { state.probe[ i ].set( 0, 0, 0 ); }
+				}
 
-			var directionalLength = 0;
-			var pointLength = 0;
-			var spotLength = 0;
-			var rectAreaLength = 0;
-			var hemiLength = 0;
+			} else {
 
-			var numDirectionalShadows = 0;
-			var numPointShadows = 0;
-			var numSpotShadows = 0;
+				var array = geometryPosition.array;
+				version = geometryPosition.version;
 
-			var viewMatrix = camera.matrixWorldInverse;
+				for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
 
-			lights.sort( shadowCastingLightsFirst );
+					var a = i + 0;
+					var b = i + 1;
+					var c = i + 2;
 
-			for ( var i = 0, l = lights.length; i < l; i ++ ) {
+					indices.push( a, b, b, c, c, a );
 
-				var light = lights[ i ];
+				}
 
-				var color = light.color;
-				var intensity = light.intensity;
-				var distance = light.distance;
+			}
 
-				var shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
+			var attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
+			attribute.version = version;
 
-				if ( light.isAmbientLight ) {
+			attributes.update( attribute, 34963 );
 
-					r += color.r * intensity;
-					g += color.g * intensity;
-					b += color.b * intensity;
+			//
 
-				} else if ( light.isLightProbe ) {
+			var previousAttribute = wireframeAttributes.get( geometry );
 
-					for ( var j = 0; j < 9; j ++ ) {
+			if ( previousAttribute ) { attributes.remove( previousAttribute ); }
 
-						state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
+			//
 
-					}
+			wireframeAttributes.set( geometry, attribute );
 
-				} else if ( light.isDirectionalLight ) {
+		}
 
-					var uniforms = cache.get( light );
+		function getWireframeAttribute( geometry ) {
 
-					uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
-					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
-					vector3.setFromMatrixPosition( light.target.matrixWorld );
-					uniforms.direction.sub( vector3 );
-					uniforms.direction.transformDirection( viewMatrix );
+			var currentAttribute = wireframeAttributes.get( geometry );
 
-					if ( light.castShadow ) {
+			if ( currentAttribute ) {
 
-						var shadow = light.shadow;
+				var geometryIndex = geometry.index;
 
-						var shadowUniforms = shadowCache.get( light );
+				if ( geometryIndex !== null ) {
 
-						shadowUniforms.shadowBias = shadow.bias;
-						shadowUniforms.shadowRadius = shadow.radius;
-						shadowUniforms.shadowMapSize = shadow.mapSize;
+					// if the attribute is obsolete, create a new one
 
-						state.directionalShadow[ directionalLength ] = shadowUniforms;
-						state.directionalShadowMap[ directionalLength ] = shadowMap;
-						state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
+					if ( currentAttribute.version < geometryIndex.version ) {
 
-						numDirectionalShadows ++;
+						updateWireframeAttribute( geometry );
 
 					}
 
-					state.directional[ directionalLength ] = uniforms;
+				}
 
-					directionalLength ++;
+			} else {
 
-				} else if ( light.isSpotLight ) {
+				updateWireframeAttribute( geometry );
 
-					var uniforms = cache.get( light );
+			}
 
-					uniforms.position.setFromMatrixPosition( light.matrixWorld );
-					uniforms.position.applyMatrix4( viewMatrix );
+			return wireframeAttributes.get( geometry );
 
-					uniforms.color.copy( color ).multiplyScalar( intensity );
-					uniforms.distance = distance;
+		}
 
-					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
-					vector3.setFromMatrixPosition( light.target.matrixWorld );
-					uniforms.direction.sub( vector3 );
-					uniforms.direction.transformDirection( viewMatrix );
+		return {
 
-					uniforms.coneCos = Math.cos( light.angle );
-					uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
-					uniforms.decay = light.decay;
+			get: get,
+			update: update,
 
-					if ( light.castShadow ) {
+			getWireframeAttribute: getWireframeAttribute
 
-						var shadow = light.shadow;
+		};
 
-						var shadowUniforms = shadowCache.get( light );
+	}
 
-						shadowUniforms.shadowBias = shadow.bias;
-						shadowUniforms.shadowRadius = shadow.radius;
-						shadowUniforms.shadowMapSize = shadow.mapSize;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-						state.spotShadow[ spotLength ] = shadowUniforms;
-						state.spotShadowMap[ spotLength ] = shadowMap;
-						state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
+	function WebGLIndexedBufferRenderer( gl, extensions, info, capabilities ) {
 
-						numSpotShadows ++;
+		var isWebGL2 = capabilities.isWebGL2;
 
-					}
+		var mode;
 
-					state.spot[ spotLength ] = uniforms;
+		function setMode( value ) {
 
-					spotLength ++;
+			mode = value;
 
-				} else if ( light.isRectAreaLight ) {
+		}
 
-					var uniforms = cache.get( light );
+		var type, bytesPerElement;
 
-					// (a) intensity is the total visible light emitted
-					//uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) );
+		function setIndex( value ) {
 
-					// (b) intensity is the brightness of the light
-					uniforms.color.copy( color ).multiplyScalar( intensity );
+			type = value.type;
+			bytesPerElement = value.bytesPerElement;
 
-					uniforms.position.setFromMatrixPosition( light.matrixWorld );
-					uniforms.position.applyMatrix4( viewMatrix );
+		}
 
-					// extract local rotation of light to derive width/height half vectors
-					matrix42.identity();
-					matrix4.copy( light.matrixWorld );
-					matrix4.premultiply( viewMatrix );
-					matrix42.extractRotation( matrix4 );
+		function render( start, count ) {
 
-					uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
-					uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
+			gl.drawElements( mode, count, type, start * bytesPerElement );
 
-					uniforms.halfWidth.applyMatrix4( matrix42 );
-					uniforms.halfHeight.applyMatrix4( matrix42 );
+			info.update( count, mode );
 
-					// TODO (abelnation): RectAreaLight distance?
-					// uniforms.distance = distance;
+		}
 
-					state.rectArea[ rectAreaLength ] = uniforms;
+		function renderInstances( geometry, start, count, primcount ) {
 
-					rectAreaLength ++;
+			if ( primcount === 0 ) { return; }
 
-				} else if ( light.isPointLight ) {
+			var extension, methodName;
 
-					var uniforms = cache.get( light );
+			if ( isWebGL2 ) {
 
-					uniforms.position.setFromMatrixPosition( light.matrixWorld );
-					uniforms.position.applyMatrix4( viewMatrix );
+				extension = gl;
+				methodName = 'drawElementsInstanced';
 
-					uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
-					uniforms.distance = light.distance;
-					uniforms.decay = light.decay;
+			} else {
 
-					if ( light.castShadow ) {
+				extension = extensions.get( 'ANGLE_instanced_arrays' );
+				methodName = 'drawElementsInstancedANGLE';
 
-						var shadow = light.shadow;
+				if ( extension === null ) {
 
-						var shadowUniforms = shadowCache.get( light );
+					console.error( 'THREE.WebGLIndexedBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
+					return;
 
-						shadowUniforms.shadowBias = shadow.bias;
-						shadowUniforms.shadowRadius = shadow.radius;
-						shadowUniforms.shadowMapSize = shadow.mapSize;
-						shadowUniforms.shadowCameraNear = shadow.camera.near;
-						shadowUniforms.shadowCameraFar = shadow.camera.far;
+				}
 
-						state.pointShadow[ pointLength ] = shadowUniforms;
-						state.pointShadowMap[ pointLength ] = shadowMap;
-						state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
+			}
 
-						numPointShadows ++;
+			extension[ methodName ]( mode, count, type, start * bytesPerElement, primcount );
 
-					}
+			info.update( count, mode, primcount );
 
-					state.point[ pointLength ] = uniforms;
+		}
 
-					pointLength ++;
+		//
 
-				} else if ( light.isHemisphereLight ) {
+		this.setMode = setMode;
+		this.setIndex = setIndex;
+		this.render = render;
+		this.renderInstances = renderInstances;
 
-					var uniforms = cache.get( light );
+	}
 
-					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
-					uniforms.direction.transformDirection( viewMatrix );
-					uniforms.direction.normalize();
+	/**
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-					uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
-					uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
+	function WebGLInfo( gl ) {
 
-					state.hemi[ hemiLength ] = uniforms;
+		var memory = {
+			geometries: 0,
+			textures: 0
+		};
 
-					hemiLength ++;
+		var render = {
+			frame: 0,
+			calls: 0,
+			triangles: 0,
+			points: 0,
+			lines: 0
+		};
 
-				}
+		function update( count, mode, instanceCount ) {
 
-			}
+			instanceCount = instanceCount || 1;
 
-			state.ambient[ 0 ] = r;
-			state.ambient[ 1 ] = g;
-			state.ambient[ 2 ] = b;
+			render.calls ++;
 
-			var hash = state.hash;
+			switch ( mode ) {
 
-			if ( hash.directionalLength !== directionalLength ||
-				hash.pointLength !== pointLength ||
-				hash.spotLength !== spotLength ||
-				hash.rectAreaLength !== rectAreaLength ||
-				hash.hemiLength !== hemiLength ||
-				hash.numDirectionalShadows !== numDirectionalShadows ||
-				hash.numPointShadows !== numPointShadows ||
-				hash.numSpotShadows !== numSpotShadows ) {
+				case 4:
+					render.triangles += instanceCount * ( count / 3 );
+					break;
 
-				state.directional.length = directionalLength;
-				state.spot.length = spotLength;
-				state.rectArea.length = rectAreaLength;
-				state.point.length = pointLength;
-				state.hemi.length = hemiLength;
+				case 1:
+					render.lines += instanceCount * ( count / 2 );
+					break;
 
-				state.directionalShadow.length = numDirectionalShadows;
-				state.directionalShadowMap.length = numDirectionalShadows;
-				state.pointShadow.length = numPointShadows;
-				state.pointShadowMap.length = numPointShadows;
-				state.spotShadow.length = numSpotShadows;
-				state.spotShadowMap.length = numSpotShadows;
-				state.directionalShadowMatrix.length = numDirectionalShadows;
-				state.pointShadowMatrix.length = numPointShadows;
-				state.spotShadowMatrix.length = numSpotShadows;
+				case 3:
+					render.lines += instanceCount * ( count - 1 );
+					break;
 
-				hash.directionalLength = directionalLength;
-				hash.pointLength = pointLength;
-				hash.spotLength = spotLength;
-				hash.rectAreaLength = rectAreaLength;
-				hash.hemiLength = hemiLength;
+				case 2:
+					render.lines += instanceCount * count;
+					break;
 
-				hash.numDirectionalShadows = numDirectionalShadows;
-				hash.numPointShadows = numPointShadows;
-				hash.numSpotShadows = numSpotShadows;
+				case 0:
+					render.points += instanceCount * count;
+					break;
 
-				state.version = nextVersion ++;
+				default:
+					console.error( 'THREE.WebGLInfo: Unknown draw mode:', mode );
+					break;
 
 			}
 
 		}
 
+		function reset() {
+
+			render.frame ++;
+			render.calls = 0;
+			render.triangles = 0;
+			render.points = 0;
+			render.lines = 0;
+
+		}
+
 		return {
-			setup: setup,
-			state: state
+			memory: memory,
+			render: render,
+			programs: null,
+			autoReset: true,
+			reset: reset,
+			update: update
 		};
 
 	}
 
 	/**
-	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function WebGLRenderState() {
+	function absNumericalSort( a, b ) {
 
-		var lights = new WebGLLights();
+		return Math.abs( b[ 1 ] ) - Math.abs( a[ 1 ] );
 
-		var lightsArray = [];
-		var shadowsArray = [];
+	}
 
-		function init() {
+	function WebGLMorphtargets( gl ) {
 
-			lightsArray.length = 0;
-			shadowsArray.length = 0;
+		var influencesList = {};
+		var morphInfluences = new Float32Array( 8 );
 
-		}
+		function update( object, geometry, material, program ) {
 
-		function pushLight( light ) {
+			var objectInfluences = object.morphTargetInfluences;
 
-			lightsArray.push( light );
+			// When object doesn't have morph target influences defined, we treat it as a 0-length array
+			// This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
 
-		}
+			var length = objectInfluences === undefined ? 0 : objectInfluences.length;
 
-		function pushShadow( shadowLight ) {
+			var influences = influencesList[ geometry.id ];
 
-			shadowsArray.push( shadowLight );
+			if ( influences === undefined ) {
 
-		}
+				// initialise list
 
-		function setupLights( camera ) {
+				influences = [];
 
-			lights.setup( lightsArray, shadowsArray, camera );
+				for ( var i = 0; i < length; i ++ ) {
 
-		}
+					influences[ i ] = [ i, 0 ];
 
-		var state = {
-			lightsArray: lightsArray,
-			shadowsArray: shadowsArray,
+				}
 
-			lights: lights
-		};
+				influencesList[ geometry.id ] = influences;
 
-		return {
-			init: init,
-			state: state,
-			setupLights: setupLights,
+			}
 
-			pushLight: pushLight,
-			pushShadow: pushShadow
-		};
+			var morphTargets = material.morphTargets && geometry.morphAttributes.position;
+			var morphNormals = material.morphNormals && geometry.morphAttributes.normal;
 
-	}
+			// Remove current morphAttributes
 
-	function WebGLRenderStates() {
+			for ( var i = 0; i < length; i ++ ) {
 
-		var renderStates = new WeakMap();
+				var influence = influences[ i ];
 
-		function onSceneDispose( event ) {
+				if ( influence[ 1 ] !== 0 ) {
 
-			var scene = event.target;
+					if ( morphTargets ) { geometry.deleteAttribute( 'morphTarget' + i ); }
+					if ( morphNormals ) { geometry.deleteAttribute( 'morphNormal' + i ); }
 
-			scene.removeEventListener( 'dispose', onSceneDispose );
+				}
 
-			renderStates.delete( scene );
+			}
 
-		}
+			// Collect influences
 
-		function get( scene, camera ) {
+			for ( var i = 0; i < length; i ++ ) {
 
-			var renderState;
+				var influence = influences[ i ];
 
-			if ( renderStates.has( scene ) === false ) {
+				influence[ 0 ] = i;
+				influence[ 1 ] = objectInfluences[ i ];
 
-				renderState = new WebGLRenderState();
-				renderStates.set( scene, new WeakMap() );
-				renderStates.get( scene ).set( camera, renderState );
+			}
 
-				scene.addEventListener( 'dispose', onSceneDispose );
+			influences.sort( absNumericalSort );
 
-			} else {
+			// Add morphAttributes
 
-				if ( renderStates.get( scene ).has( camera ) === false ) {
+			var morphInfluencesSum = 0;
 
-					renderState = new WebGLRenderState();
-					renderStates.get( scene ).set( camera, renderState );
+			for ( var i = 0; i < 8; i ++ ) {
 
-				} else {
+				var influence = influences[ i ];
 
-					renderState = renderStates.get( scene ).get( camera );
+				if ( influence ) {
 
-				}
+					var index = influence[ 0 ];
+					var value = influence[ 1 ];
 
-			}
+					if ( value ) {
 
-			return renderState;
+						if ( morphTargets ) { geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] ); }
+						if ( morphNormals ) { geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] ); }
 
-		}
+						morphInfluences[ i ] = value;
+						morphInfluencesSum += value;
+						continue;
 
-		function dispose() {
+					}
 
-			renderStates = new WeakMap();
+				}
+
+				morphInfluences[ i ] = 0;
+
+			}
+
+			// GLSL shader uses formula baseinfluence * base + sum(target * influence)
+			// This allows us to switch between absolute morphs and relative morphs without changing shader code
+			// When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
+			var morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
+
+			program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
+			program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
 
 		}
 
 		return {
-			get: get,
-			dispose: dispose
+
+			update: update
+
 		};
 
 	}
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author bhouston / https://clara.io
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *
-	 *  opacity: <float>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>
-	 * }
 	 */
 
-	function MeshDepthMaterial( parameters ) {
+	function WebGLObjects( gl, geometries, attributes, info ) {
 
-		Material.call( this );
+		var updateMap = new WeakMap();
 
-		this.type = 'MeshDepthMaterial';
+		function update( object ) {
 
-		this.depthPacking = BasicDepthPacking;
+			var frame = info.render.frame;
 
-		this.skinning = false;
-		this.morphTargets = false;
+			var geometry = object.geometry;
+			var buffergeometry = geometries.get( object, geometry );
 
-		this.map = null;
+			// Update once per frame
 
-		this.alphaMap = null;
+			if ( updateMap.get( buffergeometry ) !== frame ) {
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+				if ( geometry.isGeometry ) {
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
+					buffergeometry.updateFromObject( object );
 
-		this.fog = false;
+				}
 
-		this.setValues( parameters );
+				geometries.update( buffergeometry );
 
-	}
+				updateMap.set( buffergeometry, frame );
 
-	MeshDepthMaterial.prototype = Object.create( Material.prototype );
-	MeshDepthMaterial.prototype.constructor = MeshDepthMaterial;
+			}
 
-	MeshDepthMaterial.prototype.isMeshDepthMaterial = true;
+			if ( object.isInstancedMesh ) {
 
-	MeshDepthMaterial.prototype.copy = function ( source ) {
+				attributes.update( object.instanceMatrix, 34962 );
 
-		Material.prototype.copy.call( this, source );
+			}
 
-		this.depthPacking = source.depthPacking;
+			return buffergeometry;
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
+		}
 
-		this.map = source.map;
+		function dispose() {
 
-		this.alphaMap = source.alphaMap;
+			updateMap = new WeakMap();
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+		}
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
+		return {
 
-		return this;
+			update: update,
+			dispose: dispose
 
-	};
+		};
+
+	}
 
 	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *
-	 *  referencePosition: <float>,
-	 *  nearDistance: <float>,
-	 *  farDistance: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>
-	 *
-	 * }
+	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function MeshDistanceMaterial( parameters ) {
+	function CubeTexture( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
 
-		Material.call( this );
+		images = images !== undefined ? images : [];
+		mapping = mapping !== undefined ? mapping : CubeReflectionMapping;
+		format = format !== undefined ? format : RGBFormat;
 
-		this.type = 'MeshDistanceMaterial';
+		Texture.call( this, images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
 
-		this.referencePosition = new Vector3();
-		this.nearDistance = 1;
-		this.farDistance = 1000;
+		this.flipY = false;
 
-		this.skinning = false;
-		this.morphTargets = false;
+	}
 
-		this.map = null;
+	CubeTexture.prototype = Object.create( Texture.prototype );
+	CubeTexture.prototype.constructor = CubeTexture;
 
-		this.alphaMap = null;
+	CubeTexture.prototype.isCubeTexture = true;
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+	Object.defineProperty( CubeTexture.prototype, 'images', {
 
-		this.fog = false;
+		get: function () {
 
-		this.setValues( parameters );
+			return this.image;
 
-	}
+		},
 
-	MeshDistanceMaterial.prototype = Object.create( Material.prototype );
-	MeshDistanceMaterial.prototype.constructor = MeshDistanceMaterial;
+		set: function ( value ) {
 
-	MeshDistanceMaterial.prototype.isMeshDistanceMaterial = true;
+			this.image = value;
 
-	MeshDistanceMaterial.prototype.copy = function ( source ) {
+		}
 
-		Material.prototype.copy.call( this, source );
+	} );
 
-		this.referencePosition.copy( source.referencePosition );
-		this.nearDistance = source.nearDistance;
-		this.farDistance = source.farDistance;
+	/**
+	 * @author Takahiro https://github.com/takahirox
+	 */
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
+	function DataTexture2DArray( data, width, height, depth ) {
 
-		this.map = source.map;
+		Texture.call( this, null );
 
-		this.alphaMap = source.alphaMap;
+		this.image = { data: data || null, width: width || 1, height: height || 1, depth: depth || 1 };
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+		this.magFilter = NearestFilter;
+		this.minFilter = NearestFilter;
 
-		return this;
+		this.wrapR = ClampToEdgeWrapping;
 
-	};
+		this.generateMipmaps = false;
+		this.flipY = false;
 
-	var vsm_frag = "uniform sampler2D shadow_pass;\nuniform vec2 resolution;\nuniform float radius;\n#include <packing>\nvoid main() {\n  float mean = 0.0;\n  float squared_mean = 0.0;\n\tfloat depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy  ) / resolution ) );\n  for ( float i = -1.0; i < 1.0 ; i += SAMPLE_RATE) {\n    #ifdef HORIZONAL_PASS\n      vec2 distribution = unpackRGBATo2Half( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( i, 0.0 ) * radius ) / resolution ) );\n      mean += distribution.x;\n      squared_mean += distribution.y * distribution.y + distribution.x * distribution.x;\n    #else\n      float depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0,  i )  * radius ) / resolution ) );\n      mean += depth;\n      squared_mean += depth * depth;\n    #endif\n  }\n  mean = mean * HALF_SAMPLE_RATE;\n  squared_mean = squared_mean * HALF_SAMPLE_RATE;\n  float std_dev = sqrt( squared_mean - mean * mean );\n  gl_FragColor = pack2HalfToRGBA( vec2( mean, std_dev ) );\n}";
+		this.needsUpdate = true;
 
-	var vsm_vert = "void main() {\n\tgl_Position = vec4( position, 1.0 );\n}";
+	}
+
+	DataTexture2DArray.prototype = Object.create( Texture.prototype );
+	DataTexture2DArray.prototype.constructor = DataTexture2DArray;
+	DataTexture2DArray.prototype.isDataTexture2DArray = true;
 
 	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author Artur Trzesiok
 	 */
 
-	function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
+	function DataTexture3D( data, width, height, depth ) {
 
-		var _frustum = new Frustum(),
+		// We're going to add .setXXX() methods for setting properties later.
+		// Users can still set in DataTexture3D directly.
+		//
+		//	var texture = new THREE.DataTexture3D( data, width, height, depth );
+		// 	texture.anisotropy = 16;
+		//
+		// See #14839
 
-			_shadowMapSize = new Vector2(),
-			_viewportSize = new Vector2(),
+		Texture.call( this, null );
 
-			_viewport = new Vector4(),
+		this.image = { data: data || null, width: width || 1, height: height || 1, depth: depth || 1 };
 
-			_depthMaterials = [],
-			_distanceMaterials = [],
+		this.magFilter = NearestFilter;
+		this.minFilter = NearestFilter;
 
-			_materialCache = {};
+		this.wrapR = ClampToEdgeWrapping;
 
-		var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
+		this.generateMipmaps = false;
+		this.flipY = false;
 
-		var shadowMaterialVertical = new ShaderMaterial( {
+		this.needsUpdate = true;
 
-			defines: {
-				SAMPLE_RATE: 2.0 / 8.0,
-				HALF_SAMPLE_RATE: 1.0 / 8.0
-			},
 
-			uniforms: {
-				shadow_pass: { value: null },
-				resolution: { value: new Vector2() },
-				radius: { value: 4.0 }
-			},
+	}
 
-			vertexShader: vsm_vert,
+	DataTexture3D.prototype = Object.create( Texture.prototype );
+	DataTexture3D.prototype.constructor = DataTexture3D;
+	DataTexture3D.prototype.isDataTexture3D = true;
 
-			fragmentShader: vsm_frag
+	/**
+	 * @author tschw
+	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author mrdoob / http://mrdoob.com/
+	 *
+	 * Uniforms of a program.
+	 * Those form a tree structure with a special top-level container for the root,
+	 * which you get by calling 'new WebGLUniforms( gl, program )'.
+	 *
+	 *
+	 * Properties of inner nodes including the top-level container:
+	 *
+	 * .seq - array of nested uniforms
+	 * .map - nested uniforms by name
+	 *
+	 *
+	 * Methods of all nodes except the top-level container:
+	 *
+	 * .setValue( gl, value, [textures] )
+	 *
+	 * 		uploads a uniform value(s)
+	 *  	the 'textures' parameter is needed for sampler uniforms
+	 *
+	 *
+	 * Static methods of the top-level container (textures factorizations):
+	 *
+	 * .upload( gl, seq, values, textures )
+	 *
+	 * 		sets uniforms in 'seq' to 'values[id].value'
+	 *
+	 * .seqWithValue( seq, values ) : filteredSeq
+	 *
+	 * 		filters 'seq' entries with corresponding entry in values
+	 *
+	 *
+	 * Methods of the top-level container (textures factorizations):
+	 *
+	 * .setValue( gl, name, value, textures )
+	 *
+	 * 		sets uniform with  name 'name' to 'value'
+	 *
+	 * .setOptional( gl, obj, prop )
+	 *
+	 * 		like .set for an optional property of the object
+	 *
+	 */
 
-		} );
+	var emptyTexture = new Texture();
+	var emptyTexture2dArray = new DataTexture2DArray();
+	var emptyTexture3d = new DataTexture3D();
+	var emptyCubeTexture = new CubeTexture();
 
-		var shadowMaterialHorizonal = shadowMaterialVertical.clone();
-		shadowMaterialHorizonal.defines.HORIZONAL_PASS = 1;
+	// --- Utilities ---
 
-		var fullScreenTri = new BufferGeometry();
-		fullScreenTri.setAttribute(
-			"position",
-			new BufferAttribute(
-				new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ),
-				3
-			)
-		);
+	// Array Caches (provide typed arrays for temporary by size)
 
-		var fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical );
+	var arrayCacheF32 = [];
+	var arrayCacheI32 = [];
 
-		var scope = this;
+	// Float32Array caches used for uploading Matrix uniforms
 
-		this.enabled = false;
+	var mat4array = new Float32Array( 16 );
+	var mat3array = new Float32Array( 9 );
+	var mat2array = new Float32Array( 4 );
 
-		this.autoUpdate = true;
-		this.needsUpdate = false;
+	// Flattening for arrays of vectors and matrices
 
-		this.type = PCFShadowMap;
+	function flatten( array, nBlocks, blockSize ) {
 
-		this.render = function ( lights, scene, camera ) {
+		var firstElem = array[ 0 ];
 
-			if ( scope.enabled === false ) { return; }
-			if ( scope.autoUpdate === false && scope.needsUpdate === false ) { return; }
+		if ( firstElem <= 0 || firstElem > 0 ) { return array; }
+		// unoptimized: ! isNaN( firstElem )
+		// see http://jacksondunstan.com/articles/983
 
-			if ( lights.length === 0 ) { return; }
+		var n = nBlocks * blockSize,
+			r = arrayCacheF32[ n ];
 
-			var currentRenderTarget = _renderer.getRenderTarget();
-			var activeCubeFace = _renderer.getActiveCubeFace();
-			var activeMipmapLevel = _renderer.getActiveMipmapLevel();
+		if ( r === undefined ) {
 
-			var _state = _renderer.state;
+			r = new Float32Array( n );
+			arrayCacheF32[ n ] = r;
 
-			// Set GL state for depth map.
-			_state.setBlending( NoBlending );
-			_state.buffers.color.setClear( 1, 1, 1, 1 );
-			_state.buffers.depth.setTest( true );
-			_state.setScissorTest( false );
+		}
 
-			// render depth map
+		if ( nBlocks !== 0 ) {
 
-			for ( var i = 0, il = lights.length; i < il; i ++ ) {
+			firstElem.toArray( r, 0 );
 
-				var light = lights[ i ];
-				var shadow = light.shadow;
+			for ( var i = 1, offset = 0; i !== nBlocks; ++ i ) {
 
-				if ( shadow === undefined ) {
+				offset += blockSize;
+				array[ i ].toArray( r, offset );
 
-					console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' );
-					continue;
+			}
 
-				}
+		}
 
-				_shadowMapSize.copy( shadow.mapSize );
+		return r;
 
-				var shadowFrameExtents = shadow.getFrameExtents();
+	}
 
-				_shadowMapSize.multiply( shadowFrameExtents );
+	function arraysEqual( a, b ) {
 
-				_viewportSize.copy( shadow.mapSize );
+		if ( a.length !== b.length ) { return false; }
 
-				if ( _shadowMapSize.x > maxTextureSize || _shadowMapSize.y > maxTextureSize ) {
+		for ( var i = 0, l = a.length; i < l; i ++ ) {
 
-					if ( _shadowMapSize.x > maxTextureSize ) {
+			if ( a[ i ] !== b[ i ] ) { return false; }
 
-						_viewportSize.x = Math.floor( maxTextureSize / shadowFrameExtents.x );
-						_shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x;
-						shadow.mapSize.x = _viewportSize.x;
+		}
 
-					}
+		return true;
 
-					if ( _shadowMapSize.y > maxTextureSize ) {
+	}
 
-						_viewportSize.y = Math.floor( maxTextureSize / shadowFrameExtents.y );
-						_shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y;
-						shadow.mapSize.y = _viewportSize.y;
+	function copyArray( a, b ) {
 
-					}
+		for ( var i = 0, l = b.length; i < l; i ++ ) {
 
-				}
+			a[ i ] = b[ i ];
 
-				if ( shadow.map === null && ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
+		}
 
-					var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
+	}
 
-					shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
-					shadow.map.texture.name = light.name + ".shadowMap";
+	// Texture unit allocation
 
-					shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
+	function allocTexUnits( textures, n ) {
 
-					shadow.camera.updateProjectionMatrix();
+		var r = arrayCacheI32[ n ];
 
-				}
+		if ( r === undefined ) {
 
-				if ( shadow.map === null ) {
+			r = new Int32Array( n );
+			arrayCacheI32[ n ] = r;
 
-					var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
+		}
 
-					shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
-					shadow.map.texture.name = light.name + ".shadowMap";
+		for ( var i = 0; i !== n; ++ i )
+			{ r[ i ] = textures.allocateTextureUnit(); }
 
-					shadow.camera.updateProjectionMatrix();
+		return r;
 
-				}
+	}
 
-				_renderer.setRenderTarget( shadow.map );
-				_renderer.clear();
+	// --- Setters ---
 
-				var viewportCount = shadow.getViewportCount();
+	// Note: Defining these methods externally, because they come in a bunch
+	// and this way their names minify.
 
-				for ( var vp = 0; vp < viewportCount; vp ++ ) {
+	// Single scalar
 
-					var viewport = shadow.getViewport( vp );
+	function setValueV1f( gl, v ) {
 
-					_viewport.set(
-						_viewportSize.x * viewport.x,
-						_viewportSize.y * viewport.y,
-						_viewportSize.x * viewport.z,
-						_viewportSize.y * viewport.w
-					);
+		var cache = this.cache;
 
-					_state.viewport( _viewport );
+		if ( cache[ 0 ] === v ) { return; }
 
-					shadow.updateMatrices( light, vp );
+		gl.uniform1f( this.addr, v );
 
-					_frustum = shadow.getFrustum();
+		cache[ 0 ] = v;
 
-					renderObject( scene, camera, shadow.camera, light, this.type );
+	}
 
-				}
+	// Single float vector (from flat array or THREE.VectorN)
 
-				// do blur pass for VSM
+	function setValueV2f( gl, v ) {
 
-				if ( ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
+		var cache = this.cache;
 
-					VSMPass( shadow, camera );
+		if ( v.x !== undefined ) {
 
-				}
+			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) {
 
-			}
+				gl.uniform2f( this.addr, v.x, v.y );
 
-			scope.needsUpdate = false;
+				cache[ 0 ] = v.x;
+				cache[ 1 ] = v.y;
 
-			_renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel );
+			}
 
-		};
+		} else {
 
-		function VSMPass( shadow, camera ) {
+			if ( arraysEqual( cache, v ) ) { return; }
 
-			var geometry = _objects.update( fullScreenMesh );
+			gl.uniform2fv( this.addr, v );
 
-			// vertical pass
+			copyArray( cache, v );
 
-			shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
-			shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
-			shadowMaterialVertical.uniforms.radius.value = shadow.radius;
-			_renderer.setRenderTarget( shadow.mapPass );
-			_renderer.clear();
-			_renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null );
+		}
 
-			// horizonal pass
+	}
 
-			shadowMaterialHorizonal.uniforms.shadow_pass.value = shadow.mapPass.texture;
-			shadowMaterialHorizonal.uniforms.resolution.value = shadow.mapSize;
-			shadowMaterialHorizonal.uniforms.radius.value = shadow.radius;
-			_renderer.setRenderTarget( shadow.map );
-			_renderer.clear();
-			_renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizonal, fullScreenMesh, null );
+	function setValueV3f( gl, v ) {
 
-		}
+		var cache = this.cache;
 
-		function getDepthMaterialVariant( useMorphing, useSkinning, useInstancing ) {
+		if ( v.x !== undefined ) {
 
-			var index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2;
+			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) {
 
-			var material = _depthMaterials[ index ];
+				gl.uniform3f( this.addr, v.x, v.y, v.z );
 
-			if ( material === undefined ) {
+				cache[ 0 ] = v.x;
+				cache[ 1 ] = v.y;
+				cache[ 2 ] = v.z;
 
-				material = new MeshDepthMaterial( {
+			}
 
-					depthPacking: RGBADepthPacking,
+		} else if ( v.r !== undefined ) {
 
-					morphTargets: useMorphing,
-					skinning: useSkinning
+			if ( cache[ 0 ] !== v.r || cache[ 1 ] !== v.g || cache[ 2 ] !== v.b ) {
 
-				} );
+				gl.uniform3f( this.addr, v.r, v.g, v.b );
 
-				_depthMaterials[ index ] = material;
+				cache[ 0 ] = v.r;
+				cache[ 1 ] = v.g;
+				cache[ 2 ] = v.b;
 
 			}
 
-			return material;
+		} else {
 
-		}
+			if ( arraysEqual( cache, v ) ) { return; }
 
-		function getDistanceMaterialVariant( useMorphing, useSkinning, useInstancing ) {
+			gl.uniform3fv( this.addr, v );
 
-			var index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2;
+			copyArray( cache, v );
 
-			var material = _distanceMaterials[ index ];
+		}
 
-			if ( material === undefined ) {
+	}
 
-				material = new MeshDistanceMaterial( {
+	function setValueV4f( gl, v ) {
 
-					morphTargets: useMorphing,
-					skinning: useSkinning
+		var cache = this.cache;
 
-				} );
+		if ( v.x !== undefined ) {
 
-				_distanceMaterials[ index ] = material;
+			if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) {
+
+				gl.uniform4f( this.addr, v.x, v.y, v.z, v.w );
+
+				cache[ 0 ] = v.x;
+				cache[ 1 ] = v.y;
+				cache[ 2 ] = v.z;
+				cache[ 3 ] = v.w;
 
 			}
 
-			return material;
+		} else {
 
-		}
+			if ( arraysEqual( cache, v ) ) { return; }
 
-		function getDepthMaterial( object, geometry, material, light, shadowCameraNear, shadowCameraFar, type ) {
+			gl.uniform4fv( this.addr, v );
 
-			var result = null;
+			copyArray( cache, v );
 
-			var getMaterialVariant = getDepthMaterialVariant;
-			var customMaterial = object.customDepthMaterial;
+		}
 
-			if ( light.isPointLight === true ) {
+	}
 
-				getMaterialVariant = getDistanceMaterialVariant;
-				customMaterial = object.customDistanceMaterial;
+	// Single matrix (from flat array or MatrixN)
 
-			}
+	function setValueM2( gl, v ) {
 
-			if ( customMaterial === undefined ) {
+		var cache = this.cache;
+		var elements = v.elements;
 
-				var useMorphing = false;
+		if ( elements === undefined ) {
 
-				if ( material.morphTargets === true ) {
+			if ( arraysEqual( cache, v ) ) { return; }
 
-					useMorphing = geometry.morphAttributes && geometry.morphAttributes.position && geometry.morphAttributes.position.length > 0;
+			gl.uniformMatrix2fv( this.addr, false, v );
 
-				}
+			copyArray( cache, v );
 
-				var useSkinning = false;
+		} else {
 
-				if ( object.isSkinnedMesh === true ) {
+			if ( arraysEqual( cache, elements ) ) { return; }
 
-					if ( material.skinning === true ) {
+			mat2array.set( elements );
 
-						useSkinning = true;
+			gl.uniformMatrix2fv( this.addr, false, mat2array );
 
-					} else {
+			copyArray( cache, elements );
 
-						console.warn( 'THREE.WebGLShadowMap: THREE.SkinnedMesh with material.skinning set to false:', object );
+		}
 
-					}
+	}
 
-				}
+	function setValueM3( gl, v ) {
 
-				var useInstancing = object.isInstancedMesh === true;
+		var cache = this.cache;
+		var elements = v.elements;
 
-				result = getMaterialVariant( useMorphing, useSkinning, useInstancing );
+		if ( elements === undefined ) {
 
-			} else {
+			if ( arraysEqual( cache, v ) ) { return; }
 
-				result = customMaterial;
+			gl.uniformMatrix3fv( this.addr, false, v );
 
-			}
+			copyArray( cache, v );
 
-			if ( _renderer.localClippingEnabled &&
-					material.clipShadows === true &&
-					material.clippingPlanes.length !== 0 ) {
+		} else {
 
-				// in this case we need a unique material instance reflecting the
-				// appropriate state
+			if ( arraysEqual( cache, elements ) ) { return; }
 
-				var keyA = result.uuid, keyB = material.uuid;
+			mat3array.set( elements );
 
-				var materialsForVariant = _materialCache[ keyA ];
+			gl.uniformMatrix3fv( this.addr, false, mat3array );
 
-				if ( materialsForVariant === undefined ) {
+			copyArray( cache, elements );
 
-					materialsForVariant = {};
-					_materialCache[ keyA ] = materialsForVariant;
+		}
 
-				}
+	}
 
-				var cachedMaterial = materialsForVariant[ keyB ];
+	function setValueM4( gl, v ) {
 
-				if ( cachedMaterial === undefined ) {
+		var cache = this.cache;
+		var elements = v.elements;
 
-					cachedMaterial = result.clone();
-					materialsForVariant[ keyB ] = cachedMaterial;
+		if ( elements === undefined ) {
 
-				}
+			if ( arraysEqual( cache, v ) ) { return; }
 
-				result = cachedMaterial;
+			gl.uniformMatrix4fv( this.addr, false, v );
 
-			}
+			copyArray( cache, v );
 
-			result.visible = material.visible;
-			result.wireframe = material.wireframe;
+		} else {
 
-			if ( type === VSMShadowMap ) {
+			if ( arraysEqual( cache, elements ) ) { return; }
 
-				result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side;
+			mat4array.set( elements );
 
-			} else {
+			gl.uniformMatrix4fv( this.addr, false, mat4array );
 
-				result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ];
+			copyArray( cache, elements );
 
-			}
+		}
 
-			result.clipShadows = material.clipShadows;
-			result.clippingPlanes = material.clippingPlanes;
-			result.clipIntersection = material.clipIntersection;
+	}
 
-			result.wireframeLinewidth = material.wireframeLinewidth;
-			result.linewidth = material.linewidth;
+	// Single texture (2D / Cube)
 
-			if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) {
+	function setValueT1( gl, v, textures ) {
 
-				result.referencePosition.setFromMatrixPosition( light.matrixWorld );
-				result.nearDistance = shadowCameraNear;
-				result.farDistance = shadowCameraFar;
+		var cache = this.cache;
+		var unit = textures.allocateTextureUnit();
 
-			}
+		if ( cache[ 0 ] !== unit ) {
 
-			return result;
+			gl.uniform1i( this.addr, unit );
+			cache[ 0 ] = unit;
 
 		}
 
-		function renderObject( object, camera, shadowCamera, light, type ) {
+		textures.safeSetTexture2D( v || emptyTexture, unit );
 
-			if ( object.visible === false ) { return; }
+	}
 
-			var visible = object.layers.test( camera.layers );
+	function setValueT2DArray1( gl, v, textures ) {
 
-			if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) {
+		var cache = this.cache;
+		var unit = textures.allocateTextureUnit();
 
-				if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) {
+		if ( cache[ 0 ] !== unit ) {
 
-					object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
+			gl.uniform1i( this.addr, unit );
+			cache[ 0 ] = unit;
 
-					var geometry = _objects.update( object );
-					var material = object.material;
+		}
 
-					if ( Array.isArray( material ) ) {
+		textures.setTexture2DArray( v || emptyTexture2dArray, unit );
 
-						var groups = geometry.groups;
+	}
 
-						for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
+	function setValueT3D1( gl, v, textures ) {
 
-							var group = groups[ k ];
-							var groupMaterial = material[ group.materialIndex ];
+		var cache = this.cache;
+		var unit = textures.allocateTextureUnit();
 
-							if ( groupMaterial && groupMaterial.visible ) {
+		if ( cache[ 0 ] !== unit ) {
 
-								var depthMaterial = getDepthMaterial( object, geometry, groupMaterial, light, shadowCamera.near, shadowCamera.far, type );
+			gl.uniform1i( this.addr, unit );
+			cache[ 0 ] = unit;
 
-								_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
+		}
 
-							}
+		textures.setTexture3D( v || emptyTexture3d, unit );
 
-						}
+	}
 
-					} else if ( material.visible ) {
+	function setValueT6( gl, v, textures ) {
 
-						var depthMaterial = getDepthMaterial( object, geometry, material, light, shadowCamera.near, shadowCamera.far, type );
+		var cache = this.cache;
+		var unit = textures.allocateTextureUnit();
 
-						_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
+		if ( cache[ 0 ] !== unit ) {
 
-					}
+			gl.uniform1i( this.addr, unit );
+			cache[ 0 ] = unit;
 
-				}
+		}
 
-			}
+		textures.safeSetTextureCube( v || emptyCubeTexture, unit );
 
-			var children = object.children;
+	}
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+	// Integer / Boolean vectors or arrays thereof (always flat arrays)
 
-				renderObject( children[ i ], camera, shadowCamera, light, type );
+	function setValueV1i( gl, v ) {
 
-			}
+		var cache = this.cache;
 
-		}
+		if ( cache[ 0 ] === v ) { return; }
 
-	}
+		gl.uniform1i( this.addr, v );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		cache[ 0 ] = v;
 
-	function WebGLState( gl, extensions, capabilities ) {
+	}
 
-		var isWebGL2 = capabilities.isWebGL2;
+	function setValueV2i( gl, v ) {
 
-		function ColorBuffer() {
+		var cache = this.cache;
 
-			var locked = false;
+		if ( arraysEqual( cache, v ) ) { return; }
 
-			var color = new Vector4();
-			var currentColorMask = null;
-			var currentColorClear = new Vector4( 0, 0, 0, 0 );
+		gl.uniform2iv( this.addr, v );
 
-			return {
+		copyArray( cache, v );
 
-				setMask: function ( colorMask ) {
+	}
 
-					if ( currentColorMask !== colorMask && ! locked ) {
+	function setValueV3i( gl, v ) {
 
-						gl.colorMask( colorMask, colorMask, colorMask, colorMask );
-						currentColorMask = colorMask;
+		var cache = this.cache;
 
-					}
+		if ( arraysEqual( cache, v ) ) { return; }
 
-				},
+		gl.uniform3iv( this.addr, v );
 
-				setLocked: function ( lock ) {
+		copyArray( cache, v );
 
-					locked = lock;
+	}
 
-				},
+	function setValueV4i( gl, v ) {
 
-				setClear: function ( r, g, b, a, premultipliedAlpha ) {
+		var cache = this.cache;
 
-					if ( premultipliedAlpha === true ) {
+		if ( arraysEqual( cache, v ) ) { return; }
 
-						r *= a; g *= a; b *= a;
+		gl.uniform4iv( this.addr, v );
 
-					}
+		copyArray( cache, v );
 
-					color.set( r, g, b, a );
+	}
 
-					if ( currentColorClear.equals( color ) === false ) {
+	// uint
 
-						gl.clearColor( r, g, b, a );
-						currentColorClear.copy( color );
+	function setValueV1ui( gl, v ) {
 
-					}
+		var cache = this.cache;
 
-				},
+		if ( cache[ 0 ] === v ) { return; }
 
-				reset: function () {
+		gl.uniform1ui( this.addr, v );
 
-					locked = false;
+		cache[ 0 ] = v;
 
-					currentColorMask = null;
-					currentColorClear.set( - 1, 0, 0, 0 ); // set to invalid state
+	}
 
-				}
+	// Helper to pick the right setter for the singular case
 
-			};
+	function getSingularSetter( type ) {
 
-		}
+		switch ( type ) {
 
-		function DepthBuffer() {
+			case 0x1406: return setValueV1f; // FLOAT
+			case 0x8b50: return setValueV2f; // _VEC2
+			case 0x8b51: return setValueV3f; // _VEC3
+			case 0x8b52: return setValueV4f; // _VEC4
 
-			var locked = false;
+			case 0x8b5a: return setValueM2; // _MAT2
+			case 0x8b5b: return setValueM3; // _MAT3
+			case 0x8b5c: return setValueM4; // _MAT4
 
-			var currentDepthMask = null;
-			var currentDepthFunc = null;
-			var currentDepthClear = null;
+			case 0x1404: case 0x8b56: return setValueV1i; // INT, BOOL
+			case 0x8b53: case 0x8b57: return setValueV2i; // _VEC2
+			case 0x8b54: case 0x8b58: return setValueV3i; // _VEC3
+			case 0x8b55: case 0x8b59: return setValueV4i; // _VEC4
 
-			return {
+			case 0x1405: return setValueV1ui; // UINT
 
-				setTest: function ( depthTest ) {
+			case 0x8b5e: // SAMPLER_2D
+			case 0x8d66: // SAMPLER_EXTERNAL_OES
+			case 0x8dca: // INT_SAMPLER_2D
+			case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
+			case 0x8b62: // SAMPLER_2D_SHADOW
+				return setValueT1;
 
-					if ( depthTest ) {
+			case 0x8b5f: // SAMPLER_3D
+			case 0x8dcb: // INT_SAMPLER_3D
+			case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D
+				return setValueT3D1;
 
-						enable( 2929 );
+			case 0x8b60: // SAMPLER_CUBE
+			case 0x8dcc: // INT_SAMPLER_CUBE
+			case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
+			case 0x8dc5: // SAMPLER_CUBE_SHADOW
+				return setValueT6;
 
-					} else {
+			case 0x8dc1: // SAMPLER_2D_ARRAY
+			case 0x8dcf: // INT_SAMPLER_2D_ARRAY
+			case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY
+			case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW
+				return setValueT2DArray1;
 
-						disable( 2929 );
+		}
 
-					}
+	}
 
-				},
+	// Array of scalars
+	function setValueV1fArray( gl, v ) {
 
-				setMask: function ( depthMask ) {
+		gl.uniform1fv( this.addr, v );
 
-					if ( currentDepthMask !== depthMask && ! locked ) {
+	}
 
-						gl.depthMask( depthMask );
-						currentDepthMask = depthMask;
+	// Integer / Boolean vectors or arrays thereof (always flat arrays)
+	function setValueV1iArray( gl, v ) {
 
-					}
+		gl.uniform1iv( this.addr, v );
 
-				},
+	}
 
-				setFunc: function ( depthFunc ) {
+	function setValueV2iArray( gl, v ) {
 
-					if ( currentDepthFunc !== depthFunc ) {
+		gl.uniform2iv( this.addr, v );
 
-						if ( depthFunc ) {
+	}
 
-							switch ( depthFunc ) {
+	function setValueV3iArray( gl, v ) {
 
-								case NeverDepth:
+		gl.uniform3iv( this.addr, v );
 
-									gl.depthFunc( 512 );
-									break;
+	}
 
-								case AlwaysDepth:
+	function setValueV4iArray( gl, v ) {
 
-									gl.depthFunc( 519 );
-									break;
+		gl.uniform4iv( this.addr, v );
 
-								case LessDepth:
+	}
 
-									gl.depthFunc( 513 );
-									break;
 
-								case LessEqualDepth:
+	// Array of vectors (flat or from THREE classes)
 
-									gl.depthFunc( 515 );
-									break;
+	function setValueV2fArray( gl, v ) {
 
-								case EqualDepth:
+		var data = flatten( v, this.size, 2 );
 
-									gl.depthFunc( 514 );
-									break;
+		gl.uniform2fv( this.addr, data );
 
-								case GreaterEqualDepth:
+	}
 
-									gl.depthFunc( 518 );
-									break;
+	function setValueV3fArray( gl, v ) {
 
-								case GreaterDepth:
+		var data = flatten( v, this.size, 3 );
 
-									gl.depthFunc( 516 );
-									break;
+		gl.uniform3fv( this.addr, data );
 
-								case NotEqualDepth:
+	}
 
-									gl.depthFunc( 517 );
-									break;
+	function setValueV4fArray( gl, v ) {
 
-								default:
+		var data = flatten( v, this.size, 4 );
 
-									gl.depthFunc( 515 );
+		gl.uniform4fv( this.addr, data );
 
-							}
+	}
 
-						} else {
+	// Array of matrices (flat or from THREE clases)
 
-							gl.depthFunc( 515 );
+	function setValueM2Array( gl, v ) {
 
-						}
+		var data = flatten( v, this.size, 4 );
 
-						currentDepthFunc = depthFunc;
+		gl.uniformMatrix2fv( this.addr, false, data );
 
-					}
+	}
 
-				},
+	function setValueM3Array( gl, v ) {
 
-				setLocked: function ( lock ) {
+		var data = flatten( v, this.size, 9 );
 
-					locked = lock;
+		gl.uniformMatrix3fv( this.addr, false, data );
 
-				},
+	}
 
-				setClear: function ( depth ) {
+	function setValueM4Array( gl, v ) {
 
-					if ( currentDepthClear !== depth ) {
+		var data = flatten( v, this.size, 16 );
 
-						gl.clearDepth( depth );
-						currentDepthClear = depth;
+		gl.uniformMatrix4fv( this.addr, false, data );
 
-					}
+	}
 
-				},
+	// Array of textures (2D / Cube)
 
-				reset: function () {
+	function setValueT1Array( gl, v, textures ) {
 
-					locked = false;
+		var n = v.length;
 
-					currentDepthMask = null;
-					currentDepthFunc = null;
-					currentDepthClear = null;
+		var units = allocTexUnits( textures, n );
 
-				}
+		gl.uniform1iv( this.addr, units );
 
-			};
+		for ( var i = 0; i !== n; ++ i ) {
 
-		}
+			textures.safeSetTexture2D( v[ i ] || emptyTexture, units[ i ] );
 
-		function StencilBuffer() {
+		}
 
-			var locked = false;
+	}
 
-			var currentStencilMask = null;
-			var currentStencilFunc = null;
-			var currentStencilRef = null;
-			var currentStencilFuncMask = null;
-			var currentStencilFail = null;
-			var currentStencilZFail = null;
-			var currentStencilZPass = null;
-			var currentStencilClear = null;
+	function setValueT6Array( gl, v, textures ) {
 
-			return {
+		var n = v.length;
 
-				setTest: function ( stencilTest ) {
+		var units = allocTexUnits( textures, n );
 
-					if ( ! locked ) {
+		gl.uniform1iv( this.addr, units );
 
-						if ( stencilTest ) {
+		for ( var i = 0; i !== n; ++ i ) {
 
-							enable( 2960 );
+			textures.safeSetTextureCube( v[ i ] || emptyCubeTexture, units[ i ] );
 
-						} else {
+		}
 
-							disable( 2960 );
+	}
 
-						}
+	// Helper to pick the right setter for a pure (bottom-level) array
 
-					}
+	function getPureArraySetter( type ) {
 
-				},
+		switch ( type ) {
 
-				setMask: function ( stencilMask ) {
+			case 0x1406: return setValueV1fArray; // FLOAT
+			case 0x8b50: return setValueV2fArray; // _VEC2
+			case 0x8b51: return setValueV3fArray; // _VEC3
+			case 0x8b52: return setValueV4fArray; // _VEC4
 
-					if ( currentStencilMask !== stencilMask && ! locked ) {
+			case 0x8b5a: return setValueM2Array; // _MAT2
+			case 0x8b5b: return setValueM3Array; // _MAT3
+			case 0x8b5c: return setValueM4Array; // _MAT4
 
-						gl.stencilMask( stencilMask );
-						currentStencilMask = stencilMask;
+			case 0x1404: case 0x8b56: return setValueV1iArray; // INT, BOOL
+			case 0x8b53: case 0x8b57: return setValueV2iArray; // _VEC2
+			case 0x8b54: case 0x8b58: return setValueV3iArray; // _VEC3
+			case 0x8b55: case 0x8b59: return setValueV4iArray; // _VEC4
 
-					}
+			case 0x8b5e: // SAMPLER_2D
+			case 0x8d66: // SAMPLER_EXTERNAL_OES
+			case 0x8dca: // INT_SAMPLER_2D
+			case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
+			case 0x8b62: // SAMPLER_2D_SHADOW
+				return setValueT1Array;
 
-				},
+			case 0x8b60: // SAMPLER_CUBE
+			case 0x8dcc: // INT_SAMPLER_CUBE
+			case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
+			case 0x8dc5: // SAMPLER_CUBE_SHADOW
+				return setValueT6Array;
 
-				setFunc: function ( stencilFunc, stencilRef, stencilMask ) {
+		}
 
-					if ( currentStencilFunc !== stencilFunc ||
-					     currentStencilRef 	!== stencilRef 	||
-					     currentStencilFuncMask !== stencilMask ) {
+	}
 
-						gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
+	// --- Uniform Classes ---
 
-						currentStencilFunc = stencilFunc;
-						currentStencilRef = stencilRef;
-						currentStencilFuncMask = stencilMask;
+	function SingleUniform( id, activeInfo, addr ) {
 
-					}
+		this.id = id;
+		this.addr = addr;
+		this.cache = [];
+		this.setValue = getSingularSetter( activeInfo.type );
 
-				},
+		// this.path = activeInfo.name; // DEBUG
 
-				setOp: function ( stencilFail, stencilZFail, stencilZPass ) {
+	}
 
-					if ( currentStencilFail	 !== stencilFail 	||
-					     currentStencilZFail !== stencilZFail ||
-					     currentStencilZPass !== stencilZPass ) {
+	function PureArrayUniform( id, activeInfo, addr ) {
 
-						gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
+		this.id = id;
+		this.addr = addr;
+		this.cache = [];
+		this.size = activeInfo.size;
+		this.setValue = getPureArraySetter( activeInfo.type );
 
-						currentStencilFail = stencilFail;
-						currentStencilZFail = stencilZFail;
-						currentStencilZPass = stencilZPass;
+		// this.path = activeInfo.name; // DEBUG
 
-					}
+	}
 
-				},
+	PureArrayUniform.prototype.updateCache = function ( data ) {
 
-				setLocked: function ( lock ) {
+		var cache = this.cache;
 
-					locked = lock;
+		if ( data instanceof Float32Array && cache.length !== data.length ) {
 
-				},
+			this.cache = new Float32Array( data.length );
 
-				setClear: function ( stencil ) {
+		}
 
-					if ( currentStencilClear !== stencil ) {
+		copyArray( cache, data );
 
-						gl.clearStencil( stencil );
-						currentStencilClear = stencil;
+	};
 
-					}
+	function StructuredUniform( id ) {
 
-				},
+		this.id = id;
 
-				reset: function () {
+		this.seq = [];
+		this.map = {};
 
-					locked = false;
+	}
 
-					currentStencilMask = null;
-					currentStencilFunc = null;
-					currentStencilRef = null;
-					currentStencilFuncMask = null;
-					currentStencilFail = null;
-					currentStencilZFail = null;
-					currentStencilZPass = null;
-					currentStencilClear = null;
+	StructuredUniform.prototype.setValue = function ( gl, value, textures ) {
 
-				}
+		var seq = this.seq;
 
-			};
+		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
 
-		}
+			var u = seq[ i ];
+			u.setValue( gl, value[ u.id ], textures );
 
-		//
+		}
 
-		var colorBuffer = new ColorBuffer();
-		var depthBuffer = new DepthBuffer();
-		var stencilBuffer = new StencilBuffer();
+	};
 
-		var maxVertexAttributes = gl.getParameter( 34921 );
-		var newAttributes = new Uint8Array( maxVertexAttributes );
-		var enabledAttributes = new Uint8Array( maxVertexAttributes );
-		var attributeDivisors = new Uint8Array( maxVertexAttributes );
+	// --- Top-level ---
 
-		var enabledCapabilities = {};
+	// Parser - builds up the property tree from the path strings
 
-		var currentProgram = null;
+	var RePathPart = /([\w\d_]+)(\])?(\[|\.)?/g;
 
-		var currentBlendingEnabled = null;
-		var currentBlending = null;
-		var currentBlendEquation = null;
-		var currentBlendSrc = null;
-		var currentBlendDst = null;
-		var currentBlendEquationAlpha = null;
-		var currentBlendSrcAlpha = null;
-		var currentBlendDstAlpha = null;
-		var currentPremultipledAlpha = false;
+	// extracts
+	// 	- the identifier (member name or array index)
+	//  - followed by an optional right bracket (found when array index)
+	//  - followed by an optional left bracket or dot (type of subscript)
+	//
+	// Note: These portions can be read in a non-overlapping fashion and
+	// allow straightforward parsing of the hierarchy that WebGL encodes
+	// in the uniform names.
 
-		var currentFlipSided = null;
-		var currentCullFace = null;
+	function addUniform( container, uniformObject ) {
 
-		var currentLineWidth = null;
+		container.seq.push( uniformObject );
+		container.map[ uniformObject.id ] = uniformObject;
 
-		var currentPolygonOffsetFactor = null;
-		var currentPolygonOffsetUnits = null;
+	}
 
-		var maxTextures = gl.getParameter( 35661 );
+	function parseUniform( activeInfo, addr, container ) {
 
-		var lineWidthAvailable = false;
-		var version = 0;
-		var glVersion = gl.getParameter( 7938 );
+		var path = activeInfo.name,
+			pathLength = path.length;
 
-		if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) {
+		// reset RegExp object, because of the early exit of a previous run
+		RePathPart.lastIndex = 0;
 
-			version = parseFloat( /^WebGL\ ([0-9])/.exec( glVersion )[ 1 ] );
-			lineWidthAvailable = ( version >= 1.0 );
+		while ( true ) {
 
-		} else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) {
+			var match = RePathPart.exec( path ),
+				matchEnd = RePathPart.lastIndex,
 
-			version = parseFloat( /^OpenGL\ ES\ ([0-9])/.exec( glVersion )[ 1 ] );
-			lineWidthAvailable = ( version >= 2.0 );
+				id = match[ 1 ],
+				idIsIndex = match[ 2 ] === ']',
+				subscript = match[ 3 ];
 
-		}
+			if ( idIsIndex ) { id = id | 0; } // convert to integer
 
-		var currentTextureSlot = null;
-		var currentBoundTextures = {};
+			if ( subscript === undefined || subscript === '[' && matchEnd + 2 === pathLength ) {
 
-		var currentScissor = new Vector4();
-		var currentViewport = new Vector4();
+				// bare name or "pure" bottom-level array "[0]" suffix
 
-		function createTexture( type, target, count ) {
+				addUniform( container, subscript === undefined ?
+					new SingleUniform( id, activeInfo, addr ) :
+					new PureArrayUniform( id, activeInfo, addr ) );
 
-			var data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
-			var texture = gl.createTexture();
+				break;
 
-			gl.bindTexture( type, texture );
-			gl.texParameteri( type, 10241, 9728 );
-			gl.texParameteri( type, 10240, 9728 );
+			} else {
 
-			for ( var i = 0; i < count; i ++ ) {
+				// step into inner node / create it in case it doesn't exist
 
-				gl.texImage2D( target + i, 0, 6408, 1, 1, 0, 6408, 5121, data );
+				var map = container.map, next = map[ id ];
 
-			}
+				if ( next === undefined ) {
 
-			return texture;
+					next = new StructuredUniform( id );
+					addUniform( container, next );
 
-		}
+				}
 
-		var emptyTextures = {};
-		emptyTextures[ 3553 ] = createTexture( 3553, 3553, 1 );
-		emptyTextures[ 34067 ] = createTexture( 34067, 34069, 6 );
+				container = next;
 
-		// init
+			}
 
-		colorBuffer.setClear( 0, 0, 0, 1 );
-		depthBuffer.setClear( 1 );
-		stencilBuffer.setClear( 0 );
+		}
 
-		enable( 2929 );
-		depthBuffer.setFunc( LessEqualDepth );
+	}
 
-		setFlipSided( false );
-		setCullFace( CullFaceBack );
-		enable( 2884 );
+	// Root Container
 
-		setBlending( NoBlending );
+	function WebGLUniforms( gl, program ) {
 
-		//
+		this.seq = [];
+		this.map = {};
 
-		function initAttributes() {
+		var n = gl.getProgramParameter( program, 35718 );
 
-			for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
+		for ( var i = 0; i < n; ++ i ) {
 
-				newAttributes[ i ] = 0;
+			var info = gl.getActiveUniform( program, i ),
+				addr = gl.getUniformLocation( program, info.name );
 
-			}
+			parseUniform( info, addr, this );
 
 		}
 
-		function enableAttribute( attribute ) {
-
-			enableAttributeAndDivisor( attribute, 0 );
-
-		}
+	}
 
-		function enableAttributeAndDivisor( attribute, meshPerAttribute ) {
+	WebGLUniforms.prototype.setValue = function ( gl, name, value, textures ) {
 
-			newAttributes[ attribute ] = 1;
+		var u = this.map[ name ];
 
-			if ( enabledAttributes[ attribute ] === 0 ) {
+		if ( u !== undefined ) { u.setValue( gl, value, textures ); }
 
-				gl.enableVertexAttribArray( attribute );
-				enabledAttributes[ attribute ] = 1;
+	};
 
-			}
+	WebGLUniforms.prototype.setOptional = function ( gl, object, name ) {
 
-			if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {
+		var v = object[ name ];
 
-				var extension = isWebGL2 ? gl : extensions.get( 'ANGLE_instanced_arrays' );
+		if ( v !== undefined ) { this.setValue( gl, name, v ); }
 
-				extension[ isWebGL2 ? 'vertexAttribDivisor' : 'vertexAttribDivisorANGLE' ]( attribute, meshPerAttribute );
-				attributeDivisors[ attribute ] = meshPerAttribute;
+	};
 
-			}
 
-		}
+	// Static interface
 
-		function disableUnusedAttributes() {
+	WebGLUniforms.upload = function ( gl, seq, values, textures ) {
 
-			for ( var i = 0, l = enabledAttributes.length; i !== l; ++ i ) {
+		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
 
-				if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
+			var u = seq[ i ],
+				v = values[ u.id ];
 
-					gl.disableVertexAttribArray( i );
-					enabledAttributes[ i ] = 0;
+			if ( v.needsUpdate !== false ) {
 
-				}
+				// note: always updating when .needsUpdate is undefined
+				u.setValue( gl, v.value, textures );
 
 			}
 
 		}
 
-		function vertexAttribPointer( index, size, type, normalized, stride, offset ) {
-
-			if ( isWebGL2 === true && ( type === 5124 || type === 5125 ) ) {
+	};
 
-				gl.vertexAttribIPointer( index, size, type, normalized, stride, offset );
+	WebGLUniforms.seqWithValue = function ( seq, values ) {
 
-			} else {
+		var r = [];
 
-				gl.vertexAttribPointer( index, size, type, normalized, stride, offset );
+		for ( var i = 0, n = seq.length; i !== n; ++ i ) {
 
-			}
+			var u = seq[ i ];
+			if ( u.id in values ) { r.push( u ); }
 
 		}
 
-		function enable( id ) {
+		return r;
 
-			if ( enabledCapabilities[ id ] !== true ) {
+	};
 
-				gl.enable( id );
-				enabledCapabilities[ id ] = true;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			}
+	function WebGLShader( gl, type, string ) {
 
-		}
+		var shader = gl.createShader( type );
 
-		function disable( id ) {
+		gl.shaderSource( shader, string );
+		gl.compileShader( shader );
 
-			if ( enabledCapabilities[ id ] !== false ) {
+		return shader;
 
-				gl.disable( id );
-				enabledCapabilities[ id ] = false;
+	}
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		}
+	var programIdCount = 0;
 
-		function useProgram( program ) {
+	function addLineNumbers( string ) {
 
-			if ( currentProgram !== program ) {
+		var lines = string.split( '\n' );
 
-				gl.useProgram( program );
+		for ( var i = 0; i < lines.length; i ++ ) {
 
-				currentProgram = program;
+			lines[ i ] = ( i + 1 ) + ': ' + lines[ i ];
 
-				return true;
+		}
 
-			}
+		return lines.join( '\n' );
 
-			return false;
+	}
 
-		}
+	function getEncodingComponents( encoding ) {
 
-		var equationToGL = {};
-		equationToGL[ AddEquation ] = 32774;
-		equationToGL[ SubtractEquation ] = 32778;
-		equationToGL[ ReverseSubtractEquation ] = 32779;
+		switch ( encoding ) {
 
-		if ( isWebGL2 ) {
+			case LinearEncoding:
+				return [ 'Linear', '( value )' ];
+			case sRGBEncoding:
+				return [ 'sRGB', '( value )' ];
+			case RGBEEncoding:
+				return [ 'RGBE', '( value )' ];
+			case RGBM7Encoding:
+				return [ 'RGBM', '( value, 7.0 )' ];
+			case RGBM16Encoding:
+				return [ 'RGBM', '( value, 16.0 )' ];
+			case RGBDEncoding:
+				return [ 'RGBD', '( value, 256.0 )' ];
+			case GammaEncoding:
+				return [ 'Gamma', '( value, float( GAMMA_FACTOR ) )' ];
+			case LogLuvEncoding:
+				return [ 'LogLuv', '( value )' ];
+			default:
+				throw new Error( 'unsupported encoding: ' + encoding );
 
-			equationToGL[ MinEquation ] = 32775;
-			equationToGL[ MaxEquation ] = 32776;
+		}
 
-		} else {
+	}
 
-			var extension = extensions.get( 'EXT_blend_minmax' );
+	function getShaderErrors( gl, shader, type ) {
 
-			if ( extension !== null ) {
+		var status = gl.getShaderParameter( shader, 35713 );
+		var log = gl.getShaderInfoLog( shader ).trim();
 
-				equationToGL[ MinEquation ] = extension.MIN_EXT;
-				equationToGL[ MaxEquation ] = extension.MAX_EXT;
+		if ( status && log === '' ) { return ''; }
 
-			}
+		// --enable-privileged-webgl-extension
+		// console.log( '**' + type + '**', gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) );
 
-		}
+		var source = gl.getShaderSource( shader );
 
-		var factorToGL = {};
-		factorToGL[ ZeroFactor ] = 0;
-		factorToGL[ OneFactor ] = 1;
-		factorToGL[ SrcColorFactor ] = 768;
-		factorToGL[ SrcAlphaFactor ] = 770;
-		factorToGL[ SrcAlphaSaturateFactor ] = 776;
-		factorToGL[ DstColorFactor ] = 774;
-		factorToGL[ DstAlphaFactor ] = 772;
-		factorToGL[ OneMinusSrcColorFactor ] = 769;
-		factorToGL[ OneMinusSrcAlphaFactor ] = 771;
-		factorToGL[ OneMinusDstColorFactor ] = 775;
-		factorToGL[ OneMinusDstAlphaFactor ] = 773;
+		return 'THREE.WebGLShader: gl.getShaderInfoLog() ' + type + '\n' + log + addLineNumbers( source );
 
-		function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
+	}
 
-			if ( blending === NoBlending ) {
+	function getTexelDecodingFunction( functionName, encoding ) {
 
-				if ( currentBlendingEnabled ) {
+		var components = getEncodingComponents( encoding );
+		return 'vec4 ' + functionName + '( vec4 value ) { return ' + components[ 0 ] + 'ToLinear' + components[ 1 ] + '; }';
 
-					disable( 3042 );
-					currentBlendingEnabled = false;
+	}
 
-				}
+	function getTexelEncodingFunction( functionName, encoding ) {
 
-				return;
+		var components = getEncodingComponents( encoding );
+		return 'vec4 ' + functionName + '( vec4 value ) { return LinearTo' + components[ 0 ] + components[ 1 ] + '; }';
 
-			}
+	}
 
-			if ( ! currentBlendingEnabled ) {
+	function getToneMappingFunction( functionName, toneMapping ) {
 
-				enable( 3042 );
-				currentBlendingEnabled = true;
+		var toneMappingName;
 
-			}
+		switch ( toneMapping ) {
 
-			if ( blending !== CustomBlending ) {
+			case LinearToneMapping:
+				toneMappingName = 'Linear';
+				break;
 
-				if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
+			case ReinhardToneMapping:
+				toneMappingName = 'Reinhard';
+				break;
 
-					if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {
+			case Uncharted2ToneMapping:
+				toneMappingName = 'Uncharted2';
+				break;
 
-						gl.blendEquation( 32774 );
+			case CineonToneMapping:
+				toneMappingName = 'OptimizedCineon';
+				break;
 
-						currentBlendEquation = AddEquation;
-						currentBlendEquationAlpha = AddEquation;
+			case ACESFilmicToneMapping:
+				toneMappingName = 'ACESFilmic';
+				break;
 
-					}
+			default:
+				throw new Error( 'unsupported toneMapping: ' + toneMapping );
 
-					if ( premultipliedAlpha ) {
+		}
 
-						switch ( blending ) {
+		return 'vec3 ' + functionName + '( vec3 color ) { return ' + toneMappingName + 'ToneMapping( color ); }';
 
-							case NormalBlending:
-								gl.blendFuncSeparate( 1, 771, 1, 771 );
-								break;
+	}
 
-							case AdditiveBlending:
-								gl.blendFunc( 1, 1 );
-								break;
+	function generateExtensions( parameters ) {
 
-							case SubtractiveBlending:
-								gl.blendFuncSeparate( 0, 0, 769, 771 );
-								break;
+		var chunks = [
+			( parameters.extensionDerivatives || parameters.envMapCubeUV || parameters.bumpMap || parameters.tangentSpaceNormalMap || parameters.clearcoatNormalMap || parameters.flatShading || parameters.shaderID === 'physical' ) ? '#extension GL_OES_standard_derivatives : enable' : '',
+			( parameters.extensionFragDepth || parameters.logarithmicDepthBuffer ) && parameters.rendererExtensionFragDepth ? '#extension GL_EXT_frag_depth : enable' : '',
+			( parameters.extensionDrawBuffers && parameters.rendererExtensionDrawBuffers ) ? '#extension GL_EXT_draw_buffers : require' : '',
+			( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ? '#extension GL_EXT_shader_texture_lod : enable' : ''
+		];
 
-							case MultiplyBlending:
-								gl.blendFuncSeparate( 0, 768, 0, 770 );
-								break;
+		return chunks.filter( filterEmptyLine ).join( '\n' );
 
-							default:
-								console.error( 'THREE.WebGLState: Invalid blending: ', blending );
-								break;
+	}
 
-						}
+	function generateDefines( defines ) {
 
-					} else {
+		var chunks = [];
 
-						switch ( blending ) {
+		for ( var name in defines ) {
 
-							case NormalBlending:
-								gl.blendFuncSeparate( 770, 771, 1, 771 );
-								break;
+			var value = defines[ name ];
 
-							case AdditiveBlending:
-								gl.blendFunc( 770, 1 );
-								break;
+			if ( value === false ) { continue; }
 
-							case SubtractiveBlending:
-								gl.blendFunc( 0, 769 );
-								break;
+			chunks.push( '#define ' + name + ' ' + value );
 
-							case MultiplyBlending:
-								gl.blendFunc( 0, 768 );
-								break;
+		}
 
-							default:
-								console.error( 'THREE.WebGLState: Invalid blending: ', blending );
-								break;
+		return chunks.join( '\n' );
 
-						}
+	}
 
-					}
+	function fetchAttributeLocations( gl, program ) {
 
-					currentBlendSrc = null;
-					currentBlendDst = null;
-					currentBlendSrcAlpha = null;
-					currentBlendDstAlpha = null;
+		var attributes = {};
 
-					currentBlending = blending;
-					currentPremultipledAlpha = premultipliedAlpha;
+		var n = gl.getProgramParameter( program, 35721 );
 
-				}
+		for ( var i = 0; i < n; i ++ ) {
 
-				return;
+			var info = gl.getActiveAttrib( program, i );
+			var name = info.name;
 
-			}
+			// console.log( 'THREE.WebGLProgram: ACTIVE VERTEX ATTRIBUTE:', name, i );
 
-			// custom blending
+			attributes[ name ] = gl.getAttribLocation( program, name );
 
-			blendEquationAlpha = blendEquationAlpha || blendEquation;
-			blendSrcAlpha = blendSrcAlpha || blendSrc;
-			blendDstAlpha = blendDstAlpha || blendDst;
+		}
 
-			if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
+		return attributes;
 
-				gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] );
+	}
 
-				currentBlendEquation = blendEquation;
-				currentBlendEquationAlpha = blendEquationAlpha;
+	function filterEmptyLine( string ) {
 
-			}
+		return string !== '';
 
-			if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
+	}
 
-				gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] );
+	function replaceLightNums( string, parameters ) {
 
-				currentBlendSrc = blendSrc;
-				currentBlendDst = blendDst;
-				currentBlendSrcAlpha = blendSrcAlpha;
-				currentBlendDstAlpha = blendDstAlpha;
+		return string
+			.replace( /NUM_DIR_LIGHTS/g, parameters.numDirLights )
+			.replace( /NUM_SPOT_LIGHTS/g, parameters.numSpotLights )
+			.replace( /NUM_RECT_AREA_LIGHTS/g, parameters.numRectAreaLights )
+			.replace( /NUM_POINT_LIGHTS/g, parameters.numPointLights )
+			.replace( /NUM_HEMI_LIGHTS/g, parameters.numHemiLights )
+			.replace( /NUM_DIR_LIGHT_SHADOWS/g, parameters.numDirLightShadows )
+			.replace( /NUM_SPOT_LIGHT_SHADOWS/g, parameters.numSpotLightShadows )
+			.replace( /NUM_POINT_LIGHT_SHADOWS/g, parameters.numPointLightShadows );
 
-			}
+	}
 
-			currentBlending = blending;
-			currentPremultipledAlpha = null;
+	function replaceClippingPlaneNums( string, parameters ) {
 
-		}
+		return string
+			.replace( /NUM_CLIPPING_PLANES/g, parameters.numClippingPlanes )
+			.replace( /UNION_CLIPPING_PLANES/g, ( parameters.numClippingPlanes - parameters.numClipIntersection ) );
 
-		function setMaterial( material, frontFaceCW ) {
+	}
 
-			material.side === DoubleSide
-				? disable( 2884 )
-				: enable( 2884 );
+	// Resolve Includes
 
-			var flipSided = ( material.side === BackSide );
-			if ( frontFaceCW ) { flipSided = ! flipSided; }
+	var includePattern = /^[ \t]*#include +<([\w\d./]+)>/gm;
 
-			setFlipSided( flipSided );
+	function resolveIncludes( string ) {
 
-			( material.blending === NormalBlending && material.transparent === false )
-				? setBlending( NoBlending )
-				: setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha );
+		return string.replace( includePattern, includeReplacer );
 
-			depthBuffer.setFunc( material.depthFunc );
-			depthBuffer.setTest( material.depthTest );
-			depthBuffer.setMask( material.depthWrite );
-			colorBuffer.setMask( material.colorWrite );
+	}
 
-			var stencilWrite = material.stencilWrite;
-			stencilBuffer.setTest( stencilWrite );
-			if ( stencilWrite ) {
+	function includeReplacer( match, include ) {
 
-				stencilBuffer.setMask( material.stencilWriteMask );
-				stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask );
-				stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass );
+		var string = ShaderChunk[ include ];
 
-			}
+		if ( string === undefined ) {
 
-			setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
+			throw new Error( 'Can not resolve #include <' + include + '>' );
 
 		}
 
-		//
-
-		function setFlipSided( flipSided ) {
+		return resolveIncludes( string );
 
-			if ( currentFlipSided !== flipSided ) {
+	}
 
-				if ( flipSided ) {
+	// Unroll Loops
 
-					gl.frontFace( 2304 );
+	var deprecatedUnrollLoopPattern = /#pragma unroll_loop[\s]+?for \( int i \= (\d+)\; i < (\d+)\; i \+\+ \) \{([\s\S]+?)(?=\})\}/g;
+	var unrollLoopPattern = /#pragma unroll_loop_start[\s]+?for \( int i \= (\d+)\; i < (\d+)\; i \+\+ \) \{([\s\S]+?)(?=\})\}[\s]+?#pragma unroll_loop_end/g;
 
-				} else {
+	function unrollLoops( string ) {
 
-					gl.frontFace( 2305 );
+		return string
+			.replace( unrollLoopPattern, loopReplacer )
+			.replace( deprecatedUnrollLoopPattern, deprecatedLoopReplacer );
 
-				}
+	}
 
-				currentFlipSided = flipSided;
+	function deprecatedLoopReplacer( match, start, end, snippet ) {
 
-			}
+		console.warn( 'WebGLProgram: #pragma unroll_loop shader syntax is deprecated. Please use #pragma unroll_loop_start syntax instead.' );
+		return loopReplacer( match, start, end, snippet );
 
-		}
+	}
 
-		function setCullFace( cullFace ) {
+	function loopReplacer( match, start, end, snippet ) {
 
-			if ( cullFace !== CullFaceNone ) {
+		var string = '';
 
-				enable( 2884 );
+		for ( var i = parseInt( start ); i < parseInt( end ); i ++ ) {
 
-				if ( cullFace !== currentCullFace ) {
+			string += snippet
+				.replace( /\[ i \]/g, '[ ' + i + ' ]' )
+				.replace( /UNROLLED_LOOP_INDEX/g, i );
 
-					if ( cullFace === CullFaceBack ) {
+		}
 
-						gl.cullFace( 1029 );
+		return string;
 
-					} else if ( cullFace === CullFaceFront ) {
+	}
 
-						gl.cullFace( 1028 );
+	//
 
-					} else {
+	function generatePrecision( parameters ) {
 
-						gl.cullFace( 1032 );
+		var precisionstring = "precision " + parameters.precision + " float;\nprecision " + parameters.precision + " int;";
 
-					}
+		if ( parameters.precision === "highp" ) {
 
-				}
+			precisionstring += "\n#define HIGH_PRECISION";
 
-			} else {
+		} else if ( parameters.precision === "mediump" ) {
 
-				disable( 2884 );
+			precisionstring += "\n#define MEDIUM_PRECISION";
 
-			}
+		} else if ( parameters.precision === "lowp" ) {
 
-			currentCullFace = cullFace;
+			precisionstring += "\n#define LOW_PRECISION";
 
 		}
 
-		function setLineWidth( width ) {
-
-			if ( width !== currentLineWidth ) {
+		return precisionstring;
 
-				if ( lineWidthAvailable ) { gl.lineWidth( width ); }
+	}
 
-				currentLineWidth = width;
+	function generateShadowMapTypeDefine( parameters ) {
 
-			}
+		var shadowMapTypeDefine = 'SHADOWMAP_TYPE_BASIC';
 
-		}
+		if ( parameters.shadowMapType === PCFShadowMap ) {
 
-		function setPolygonOffset( polygonOffset, factor, units ) {
+			shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF';
 
-			if ( polygonOffset ) {
+		} else if ( parameters.shadowMapType === PCFSoftShadowMap ) {
 
-				enable( 32823 );
+			shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF_SOFT';
 
-				if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
+		} else if ( parameters.shadowMapType === VSMShadowMap ) {
 
-					gl.polygonOffset( factor, units );
+			shadowMapTypeDefine = 'SHADOWMAP_TYPE_VSM';
 
-					currentPolygonOffsetFactor = factor;
-					currentPolygonOffsetUnits = units;
+		}
 
-				}
+		return shadowMapTypeDefine;
 
-			} else {
+	}
 
-				disable( 32823 );
+	function generateEnvMapTypeDefine( parameters ) {
 
-			}
+		var envMapTypeDefine = 'ENVMAP_TYPE_CUBE';
 
-		}
+		if ( parameters.envMap ) {
 
-		function setScissorTest( scissorTest ) {
+			switch ( parameters.envMapMode ) {
 
-			if ( scissorTest ) {
+				case CubeReflectionMapping:
+				case CubeRefractionMapping:
+					envMapTypeDefine = 'ENVMAP_TYPE_CUBE';
+					break;
 
-				enable( 3089 );
+				case CubeUVReflectionMapping:
+				case CubeUVRefractionMapping:
+					envMapTypeDefine = 'ENVMAP_TYPE_CUBE_UV';
+					break;
 
-			} else {
+				case EquirectangularReflectionMapping:
+				case EquirectangularRefractionMapping:
+					envMapTypeDefine = 'ENVMAP_TYPE_EQUIREC';
+					break;
 
-				disable( 3089 );
+				case SphericalReflectionMapping:
+					envMapTypeDefine = 'ENVMAP_TYPE_SPHERE';
+					break;
 
 			}
 
 		}
 
-		// texture
+		return envMapTypeDefine;
 
-		function activeTexture( webglSlot ) {
+	}
 
-			if ( webglSlot === undefined ) { webglSlot = 33984 + maxTextures - 1; }
+	function generateEnvMapModeDefine( parameters ) {
 
-			if ( currentTextureSlot !== webglSlot ) {
+		var envMapModeDefine = 'ENVMAP_MODE_REFLECTION';
 
-				gl.activeTexture( webglSlot );
-				currentTextureSlot = webglSlot;
+		if ( parameters.envMap ) {
 
-			}
+			switch ( parameters.envMapMode ) {
 
-		}
+				case CubeRefractionMapping:
+				case EquirectangularRefractionMapping:
+					envMapModeDefine = 'ENVMAP_MODE_REFRACTION';
+					break;
 
-		function bindTexture( webglType, webglTexture ) {
+			}
 
-			if ( currentTextureSlot === null ) {
+		}
 
-				activeTexture();
+		return envMapModeDefine;
 
-			}
+	}
 
-			var boundTexture = currentBoundTextures[ currentTextureSlot ];
+	function generateEnvMapBlendingDefine( parameters ) {
 
-			if ( boundTexture === undefined ) {
+		var envMapBlendingDefine = 'ENVMAP_BLENDING_NONE';
 
-				boundTexture = { type: undefined, texture: undefined };
-				currentBoundTextures[ currentTextureSlot ] = boundTexture;
+		if ( parameters.envMap ) {
 
-			}
+			switch ( parameters.combine ) {
 
-			if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
+				case MultiplyOperation:
+					envMapBlendingDefine = 'ENVMAP_BLENDING_MULTIPLY';
+					break;
 
-				gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] );
+				case MixOperation:
+					envMapBlendingDefine = 'ENVMAP_BLENDING_MIX';
+					break;
 
-				boundTexture.type = webglType;
-				boundTexture.texture = webglTexture;
+				case AddOperation:
+					envMapBlendingDefine = 'ENVMAP_BLENDING_ADD';
+					break;
 
 			}
 
 		}
 
-		function unbindTexture() {
-
-			var boundTexture = currentBoundTextures[ currentTextureSlot ];
+		return envMapBlendingDefine;
 
-			if ( boundTexture !== undefined && boundTexture.type !== undefined ) {
+	}
 
-				gl.bindTexture( boundTexture.type, null );
+	function WebGLProgram( renderer, cacheKey, parameters ) {
 
-				boundTexture.type = undefined;
-				boundTexture.texture = undefined;
+		var gl = renderer.getContext();
 
-			}
+		var defines = parameters.defines;
 
-		}
+		var vertexShader = parameters.vertexShader;
+		var fragmentShader = parameters.fragmentShader;
+		var shadowMapTypeDefine = generateShadowMapTypeDefine( parameters );
+		var envMapTypeDefine = generateEnvMapTypeDefine( parameters );
+		var envMapModeDefine = generateEnvMapModeDefine( parameters );
+		var envMapBlendingDefine = generateEnvMapBlendingDefine( parameters );
 
-		function compressedTexImage2D() {
 
-			try {
+		var gammaFactorDefine = ( renderer.gammaFactor > 0 ) ? renderer.gammaFactor : 1.0;
 
-				gl.compressedTexImage2D.apply( gl, arguments );
+		var customExtensions = parameters.isWebGL2 ? '' : generateExtensions( parameters );
 
-			} catch ( error ) {
+		var customDefines = generateDefines( defines );
 
-				console.error( 'THREE.WebGLState:', error );
+		var program = gl.createProgram();
 
-			}
+		var prefixVertex, prefixFragment;
 
-		}
+		if ( parameters.isRawShaderMaterial ) {
 
-		function texImage2D() {
+			prefixVertex = [
 
-			try {
+				customDefines
 
-				gl.texImage2D.apply( gl, arguments );
+			].filter( filterEmptyLine ).join( '\n' );
 
-			} catch ( error ) {
+			if ( prefixVertex.length > 0 ) {
 
-				console.error( 'THREE.WebGLState:', error );
+				prefixVertex += '\n';
 
 			}
 
-		}
-
-		function texImage3D() {
+			prefixFragment = [
 
-			try {
+				customExtensions,
+				customDefines
 
-				gl.texImage3D.apply( gl, arguments );
+			].filter( filterEmptyLine ).join( '\n' );
 
-			} catch ( error ) {
+			if ( prefixFragment.length > 0 ) {
 
-				console.error( 'THREE.WebGLState:', error );
+				prefixFragment += '\n';
 
 			}
 
-		}
+		} else {
 
-		//
+			prefixVertex = [
 
-		function scissor( scissor ) {
+				generatePrecision( parameters ),
 
-			if ( currentScissor.equals( scissor ) === false ) {
+				'#define SHADER_NAME ' + parameters.shaderName,
 
-				gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
-				currentScissor.copy( scissor );
+				customDefines,
 
-			}
+				parameters.instancing ? '#define USE_INSTANCING' : '',
+				parameters.supportsVertexTextures ? '#define VERTEX_TEXTURES' : '',
 
-		}
+				'#define GAMMA_FACTOR ' + gammaFactorDefine,
 
-		function viewport( viewport ) {
+				'#define MAX_BONES ' + parameters.maxBones,
+				( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '',
+				( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '',
 
-			if ( currentViewport.equals( viewport ) === false ) {
+				parameters.map ? '#define USE_MAP' : '',
+				parameters.envMap ? '#define USE_ENVMAP' : '',
+				parameters.envMap ? '#define ' + envMapModeDefine : '',
+				parameters.lightMap ? '#define USE_LIGHTMAP' : '',
+				parameters.aoMap ? '#define USE_AOMAP' : '',
+				parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '',
+				parameters.bumpMap ? '#define USE_BUMPMAP' : '',
+				parameters.normalMap ? '#define USE_NORMALMAP' : '',
+				( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '',
+				( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '',
 
-				gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
-				currentViewport.copy( viewport );
+				parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
+				parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
+				parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',
+				parameters.displacementMap && parameters.supportsVertexTextures ? '#define USE_DISPLACEMENTMAP' : '',
+				parameters.specularMap ? '#define USE_SPECULARMAP' : '',
+				parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
+				parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
+				parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
 
-			}
+				parameters.vertexTangents ? '#define USE_TANGENT' : '',
+				parameters.vertexColors ? '#define USE_COLOR' : '',
+				parameters.vertexUvs ? '#define USE_UV' : '',
+				parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '',
 
-		}
+				parameters.flatShading ? '#define FLAT_SHADED' : '',
 
-		//
+				parameters.skinning ? '#define USE_SKINNING' : '',
+				parameters.useVertexTexture ? '#define BONE_TEXTURE' : '',
 
-		function reset() {
+				parameters.morphTargets ? '#define USE_MORPHTARGETS' : '',
+				parameters.morphNormals && parameters.flatShading === false ? '#define USE_MORPHNORMALS' : '',
+				parameters.doubleSided ? '#define DOUBLE_SIDED' : '',
+				parameters.flipSided ? '#define FLIP_SIDED' : '',
 
-			for ( var i = 0; i < enabledAttributes.length; i ++ ) {
+				parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '',
+				parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '',
 
-				if ( enabledAttributes[ i ] === 1 ) {
+				parameters.sizeAttenuation ? '#define USE_SIZEATTENUATION' : '',
 
-					gl.disableVertexAttribArray( i );
-					enabledAttributes[ i ] = 0;
+				parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
+				( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '',
 
-				}
+				'uniform mat4 modelMatrix;',
+				'uniform mat4 modelViewMatrix;',
+				'uniform mat4 projectionMatrix;',
+				'uniform mat4 viewMatrix;',
+				'uniform mat3 normalMatrix;',
+				'uniform vec3 cameraPosition;',
+				'uniform bool isOrthographic;',
 
-			}
+				'#ifdef USE_INSTANCING',
 
-			enabledCapabilities = {};
+				' attribute mat4 instanceMatrix;',
 
-			currentTextureSlot = null;
-			currentBoundTextures = {};
+				'#endif',
 
-			currentProgram = null;
+				'attribute vec3 position;',
+				'attribute vec3 normal;',
+				'attribute vec2 uv;',
 
-			currentBlending = null;
+				'#ifdef USE_TANGENT',
 
-			currentFlipSided = null;
-			currentCullFace = null;
+				'	attribute vec4 tangent;',
 
-			colorBuffer.reset();
-			depthBuffer.reset();
-			stencilBuffer.reset();
+				'#endif',
 
-		}
+				'#ifdef USE_COLOR',
 
-		return {
+				'	attribute vec3 color;',
 
-			buffers: {
-				color: colorBuffer,
-				depth: depthBuffer,
-				stencil: stencilBuffer
-			},
+				'#endif',
 
-			initAttributes: initAttributes,
-			enableAttribute: enableAttribute,
-			enableAttributeAndDivisor: enableAttributeAndDivisor,
-			disableUnusedAttributes: disableUnusedAttributes,
-			vertexAttribPointer: vertexAttribPointer,
-			enable: enable,
-			disable: disable,
+				'#ifdef USE_MORPHTARGETS',
 
-			useProgram: useProgram,
+				'	attribute vec3 morphTarget0;',
+				'	attribute vec3 morphTarget1;',
+				'	attribute vec3 morphTarget2;',
+				'	attribute vec3 morphTarget3;',
 
-			setBlending: setBlending,
-			setMaterial: setMaterial,
+				'	#ifdef USE_MORPHNORMALS',
 
-			setFlipSided: setFlipSided,
-			setCullFace: setCullFace,
+				'		attribute vec3 morphNormal0;',
+				'		attribute vec3 morphNormal1;',
+				'		attribute vec3 morphNormal2;',
+				'		attribute vec3 morphNormal3;',
 
-			setLineWidth: setLineWidth,
-			setPolygonOffset: setPolygonOffset,
+				'	#else',
 
-			setScissorTest: setScissorTest,
+				'		attribute vec3 morphTarget4;',
+				'		attribute vec3 morphTarget5;',
+				'		attribute vec3 morphTarget6;',
+				'		attribute vec3 morphTarget7;',
 
-			activeTexture: activeTexture,
-			bindTexture: bindTexture,
-			unbindTexture: unbindTexture,
-			compressedTexImage2D: compressedTexImage2D,
-			texImage2D: texImage2D,
-			texImage3D: texImage3D,
+				'	#endif',
 
-			scissor: scissor,
-			viewport: viewport,
+				'#endif',
 
-			reset: reset
+				'#ifdef USE_SKINNING',
 
-		};
+				'	attribute vec4 skinIndex;',
+				'	attribute vec4 skinWeight;',
 
-	}
+				'#endif',
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				'\n'
 
-	function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) {
+			].filter( filterEmptyLine ).join( '\n' );
 
-		var isWebGL2 = capabilities.isWebGL2;
-		var maxTextures = capabilities.maxTextures;
-		var maxCubemapSize = capabilities.maxCubemapSize;
-		var maxTextureSize = capabilities.maxTextureSize;
-		var maxSamples = capabilities.maxSamples;
+			prefixFragment = [
 
-		var _videoTextures = new WeakMap();
-		var _canvas;
+				customExtensions,
 
-		// cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
-		// also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
-		// Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d).
+				generatePrecision( parameters ),
 
-		var useOffscreenCanvas = false;
+				'#define SHADER_NAME ' + parameters.shaderName,
 
-		try {
+				customDefines,
 
-			useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
-				&& ( new OffscreenCanvas( 1, 1 ).getContext( "2d" ) ) !== null;
+				parameters.alphaTest ? '#define ALPHATEST ' + parameters.alphaTest + ( parameters.alphaTest % 1 ? '' : '.0' ) : '', // add '.0' if integer
 
-		} catch ( err ) {
+				'#define GAMMA_FACTOR ' + gammaFactorDefine,
 
-			// Ignore any errors
+				( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '',
+				( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '',
 
-		}
+				parameters.map ? '#define USE_MAP' : '',
+				parameters.matcap ? '#define USE_MATCAP' : '',
+				parameters.envMap ? '#define USE_ENVMAP' : '',
+				parameters.envMap ? '#define ' + envMapTypeDefine : '',
+				parameters.envMap ? '#define ' + envMapModeDefine : '',
+				parameters.envMap ? '#define ' + envMapBlendingDefine : '',
+				parameters.lightMap ? '#define USE_LIGHTMAP' : '',
+				parameters.aoMap ? '#define USE_AOMAP' : '',
+				parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '',
+				parameters.bumpMap ? '#define USE_BUMPMAP' : '',
+				parameters.normalMap ? '#define USE_NORMALMAP' : '',
+				( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '',
+				( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '',
+				parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '',
+				parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '',
+				parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '',
+				parameters.specularMap ? '#define USE_SPECULARMAP' : '',
+				parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '',
+				parameters.metalnessMap ? '#define USE_METALNESSMAP' : '',
+				parameters.alphaMap ? '#define USE_ALPHAMAP' : '',
 
-		function createCanvas( width, height ) {
+				parameters.sheen ? '#define USE_SHEEN' : '',
 
-			// Use OffscreenCanvas when available. Specially needed in web workers
+				parameters.vertexTangents ? '#define USE_TANGENT' : '',
+				parameters.vertexColors ? '#define USE_COLOR' : '',
+				parameters.vertexUvs ? '#define USE_UV' : '',
+				parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '',
 
-			return useOffscreenCanvas ?
-				new OffscreenCanvas( width, height ) :
-				document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
+				parameters.gradientMap ? '#define USE_GRADIENTMAP' : '',
 
-		}
+				parameters.flatShading ? '#define FLAT_SHADED' : '',
 
-		function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) {
+				parameters.doubleSided ? '#define DOUBLE_SIDED' : '',
+				parameters.flipSided ? '#define FLIP_SIDED' : '',
 
-			var scale = 1;
+				parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '',
+				parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '',
 
-			// handle case if texture exceeds max size
+				parameters.premultipliedAlpha ? '#define PREMULTIPLIED_ALPHA' : '',
 
-			if ( image.width > maxSize || image.height > maxSize ) {
+				parameters.physicallyCorrectLights ? '#define PHYSICALLY_CORRECT_LIGHTS' : '',
 
-				scale = maxSize / Math.max( image.width, image.height );
+				parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
+				( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '',
 
-			}
+				( ( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ) ? '#define TEXTURE_LOD_EXT' : '',
 
-			// only perform resize if necessary
-
-			if ( scale < 1 || needsPowerOfTwo === true ) {
-
-				// only perform resize for certain image types
+				'uniform mat4 viewMatrix;',
+				'uniform vec3 cameraPosition;',
+				'uniform bool isOrthographic;',
 
-				if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
-					( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
-					( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
+				( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '',
+				( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below
+				( parameters.toneMapping !== NoToneMapping ) ? getToneMappingFunction( 'toneMapping', parameters.toneMapping ) : '',
 
-					var floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor;
+				parameters.dithering ? '#define DITHERING' : '',
 
-					var width = floor( scale * image.width );
-					var height = floor( scale * image.height );
+				( parameters.outputEncoding || parameters.mapEncoding || parameters.matcapEncoding || parameters.envMapEncoding || parameters.emissiveMapEncoding || parameters.lightMapEncoding ) ?
+					ShaderChunk[ 'encodings_pars_fragment' ] : '', // this code is required here because it is used by the various encoding/decoding function defined below
+				parameters.mapEncoding ? getTexelDecodingFunction( 'mapTexelToLinear', parameters.mapEncoding ) : '',
+				parameters.matcapEncoding ? getTexelDecodingFunction( 'matcapTexelToLinear', parameters.matcapEncoding ) : '',
+				parameters.envMapEncoding ? getTexelDecodingFunction( 'envMapTexelToLinear', parameters.envMapEncoding ) : '',
+				parameters.emissiveMapEncoding ? getTexelDecodingFunction( 'emissiveMapTexelToLinear', parameters.emissiveMapEncoding ) : '',
+				parameters.lightMapEncoding ? getTexelDecodingFunction( 'lightMapTexelToLinear', parameters.lightMapEncoding ) : '',
+				parameters.outputEncoding ? getTexelEncodingFunction( 'linearToOutputTexel', parameters.outputEncoding ) : '',
 
-					if ( _canvas === undefined ) { _canvas = createCanvas( width, height ); }
+				parameters.depthPacking ? '#define DEPTH_PACKING ' + parameters.depthPacking : '',
 
-					// cube textures can't reuse the same canvas
+				'\n'
 
-					var canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas;
+			].filter( filterEmptyLine ).join( '\n' );
 
-					canvas.width = width;
-					canvas.height = height;
+		}
 
-					var context = canvas.getContext( '2d' );
-					context.drawImage( image, 0, 0, width, height );
+		vertexShader = resolveIncludes( vertexShader );
+		vertexShader = replaceLightNums( vertexShader, parameters );
+		vertexShader = replaceClippingPlaneNums( vertexShader, parameters );
 
-					console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
+		fragmentShader = resolveIncludes( fragmentShader );
+		fragmentShader = replaceLightNums( fragmentShader, parameters );
+		fragmentShader = replaceClippingPlaneNums( fragmentShader, parameters );
 
-					return canvas;
+		vertexShader = unrollLoops( vertexShader );
+		fragmentShader = unrollLoops( fragmentShader );
 
-				} else {
+		if ( parameters.isWebGL2 && ! parameters.isRawShaderMaterial ) {
 
-					if ( 'data' in image ) {
+			var isGLSL3ShaderMaterial = false;
 
-						console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' );
+			var versionRegex = /^\s*#version\s+300\s+es\s*\n/;
 
-					}
+			if ( parameters.isShaderMaterial &&
+				vertexShader.match( versionRegex ) !== null &&
+				fragmentShader.match( versionRegex ) !== null ) {
 
-					return image;
+				isGLSL3ShaderMaterial = true;
 
-				}
+				vertexShader = vertexShader.replace( versionRegex, '' );
+				fragmentShader = fragmentShader.replace( versionRegex, '' );
 
 			}
 
-			return image;
-
-		}
+			// GLSL 3.0 conversion
 
-		function isPowerOfTwo( image ) {
+			prefixVertex = [
+				'#version 300 es\n',
+				'#define attribute in',
+				'#define varying out',
+				'#define texture2D texture'
+			].join( '\n' ) + '\n' + prefixVertex;
 
-			return MathUtils.isPowerOfTwo( image.width ) && MathUtils.isPowerOfTwo( image.height );
+			prefixFragment = [
+				'#version 300 es\n',
+				'#define varying in',
+				isGLSL3ShaderMaterial ? '' : 'out highp vec4 pc_fragColor;',
+				isGLSL3ShaderMaterial ? '' : '#define gl_FragColor pc_fragColor',
+				'#define gl_FragDepthEXT gl_FragDepth',
+				'#define texture2D texture',
+				'#define textureCube texture',
+				'#define texture2DProj textureProj',
+				'#define texture2DLodEXT textureLod',
+				'#define texture2DProjLodEXT textureProjLod',
+				'#define textureCubeLodEXT textureLod',
+				'#define texture2DGradEXT textureGrad',
+				'#define texture2DProjGradEXT textureProjGrad',
+				'#define textureCubeGradEXT textureGrad'
+			].join( '\n' ) + '\n' + prefixFragment;
 
 		}
 
-		function textureNeedsPowerOfTwo( texture ) {
-
-			if ( isWebGL2 ) { return false; }
-
-			return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) ||
-				( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter );
+		var vertexGlsl = prefixVertex + vertexShader;
+		var fragmentGlsl = prefixFragment + fragmentShader;
 
-		}
+		// console.log( '*VERTEX*', vertexGlsl );
+		// console.log( '*FRAGMENT*', fragmentGlsl );
 
-		function textureNeedsGenerateMipmaps( texture, supportsMips ) {
+		var glVertexShader = WebGLShader( gl, 35633, vertexGlsl );
+		var glFragmentShader = WebGLShader( gl, 35632, fragmentGlsl );
 
-			return texture.generateMipmaps && supportsMips &&
-				texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
+		gl.attachShader( program, glVertexShader );
+		gl.attachShader( program, glFragmentShader );
 
-		}
+		// Force a particular attribute to index 0.
 
-		function generateMipmap( target, texture, width, height ) {
+		if ( parameters.index0AttributeName !== undefined ) {
 
-			_gl.generateMipmap( target );
+			gl.bindAttribLocation( program, 0, parameters.index0AttributeName );
 
-			var textureProperties = properties.get( texture );
+		} else if ( parameters.morphTargets === true ) {
 
-			// Note: Math.log( x ) * Math.LOG2E used instead of Math.log2( x ) which is not supported by IE11
-			textureProperties.__maxMipLevel = Math.log( Math.max( width, height ) ) * Math.LOG2E;
+			// programs with morphTargets displace position out of attribute 0
+			gl.bindAttribLocation( program, 0, 'position' );
 
 		}
 
-		function getInternalFormat( internalFormatName, glFormat, glType ) {
+		gl.linkProgram( program );
 
-			if ( isWebGL2 === false ) { return glFormat; }
+		// check for link errors
+		if ( renderer.debug.checkShaderErrors ) {
 
-			if ( internalFormatName !== null ) {
+			var programLog = gl.getProgramInfoLog( program ).trim();
+			var vertexLog = gl.getShaderInfoLog( glVertexShader ).trim();
+			var fragmentLog = gl.getShaderInfoLog( glFragmentShader ).trim();
 
-				if ( _gl[ internalFormatName ] !== undefined ) { return _gl[ internalFormatName ]; }
+			var runnable = true;
+			var haveDiagnostics = true;
 
-				console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
+			if ( gl.getProgramParameter( program, 35714 ) === false ) {
 
-			}
+				runnable = false;
 
-			var internalFormat = glFormat;
+				var vertexErrors = getShaderErrors( gl, glVertexShader, 'vertex' );
+				var fragmentErrors = getShaderErrors( gl, glFragmentShader, 'fragment' );
 
-			if ( glFormat === 6403 ) {
+				console.error( 'THREE.WebGLProgram: shader error: ', gl.getError(), '35715', gl.getProgramParameter( program, 35715 ), 'gl.getProgramInfoLog', programLog, vertexErrors, fragmentErrors );
 
-				if ( glType === 5126 ) { internalFormat = 33326; }
-				if ( glType === 5131 ) { internalFormat = 33325; }
-				if ( glType === 5121 ) { internalFormat = 33321; }
+			} else if ( programLog !== '' ) {
 
-			}
+				console.warn( 'THREE.WebGLProgram: gl.getProgramInfoLog()', programLog );
 
-			if ( glFormat === 6407 ) {
+			} else if ( vertexLog === '' || fragmentLog === '' ) {
 
-				if ( glType === 5126 ) { internalFormat = 34837; }
-				if ( glType === 5131 ) { internalFormat = 34843; }
-				if ( glType === 5121 ) { internalFormat = 32849; }
+				haveDiagnostics = false;
 
 			}
 
-			if ( glFormat === 6408 ) {
-
-				if ( glType === 5126 ) { internalFormat = 34836; }
-				if ( glType === 5131 ) { internalFormat = 34842; }
-				if ( glType === 5121 ) { internalFormat = 32856; }
+			if ( haveDiagnostics ) {
 
-			}
+				this.diagnostics = {
 
-			if ( internalFormat === 33325 || internalFormat === 33326 ||
-				internalFormat === 34842 || internalFormat === 34836 ) {
+					runnable: runnable,
 
-				extensions.get( 'EXT_color_buffer_float' );
+					programLog: programLog,
 
-			}
+					vertexShader: {
 
-			return internalFormat;
+						log: vertexLog,
+						prefix: prefixVertex
 
-		}
+					},
 
-		// Fallback filters for non-power-of-2 textures
+					fragmentShader: {
 
-		function filterFallback( f ) {
+						log: fragmentLog,
+						prefix: prefixFragment
 
-			if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
+					}
 
-				return 9728;
+				};
 
 			}
 
-			return 9729;
-
 		}
 
-		//
+		// Clean up
 
-		function onTextureDispose( event ) {
+		// Crashes in iOS9 and iOS10. #18402
+		// gl.detachShader( program, glVertexShader );
+		// gl.detachShader( program, glFragmentShader );
 
-			var texture = event.target;
+		gl.deleteShader( glVertexShader );
+		gl.deleteShader( glFragmentShader );
 
-			texture.removeEventListener( 'dispose', onTextureDispose );
+		// set up caching for uniform locations
 
-			deallocateTexture( texture );
+		var cachedUniforms;
 
-			if ( texture.isVideoTexture ) {
+		this.getUniforms = function () {
 
-				_videoTextures.delete( texture );
+			if ( cachedUniforms === undefined ) {
+
+				cachedUniforms = new WebGLUniforms( gl, program );
 
 			}
 
-			info.memory.textures --;
+			return cachedUniforms;
 
-		}
+		};
 
-		function onRenderTargetDispose( event ) {
+		// set up caching for attribute locations
 
-			var renderTarget = event.target;
+		var cachedAttributes;
 
-			renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
+		this.getAttributes = function () {
 
-			deallocateRenderTarget( renderTarget );
+			if ( cachedAttributes === undefined ) {
 
-			info.memory.textures --;
+				cachedAttributes = fetchAttributeLocations( gl, program );
 
-		}
+			}
 
-		//
+			return cachedAttributes;
 
-		function deallocateTexture( texture ) {
+		};
 
-			var textureProperties = properties.get( texture );
+		// free resource
 
-			if ( textureProperties.__webglInit === undefined ) { return; }
+		this.destroy = function () {
 
-			_gl.deleteTexture( textureProperties.__webglTexture );
+			gl.deleteProgram( program );
+			this.program = undefined;
 
-			properties.remove( texture );
+		};
 
-		}
+		//
 
-		function deallocateRenderTarget( renderTarget ) {
+		this.name = parameters.shaderName;
+		this.id = programIdCount ++;
+		this.cacheKey = cacheKey;
+		this.usedTimes = 1;
+		this.program = program;
+		this.vertexShader = glVertexShader;
+		this.fragmentShader = glFragmentShader;
 
-			var renderTargetProperties = properties.get( renderTarget );
-			var textureProperties = properties.get( renderTarget.texture );
+		return this;
 
-			if ( ! renderTarget ) { return; }
+	}
 
-			if ( textureProperties.__webglTexture !== undefined ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				_gl.deleteTexture( textureProperties.__webglTexture );
+	function WebGLPrograms( renderer, extensions, capabilities ) {
 
-			}
+		var programs = [];
 
-			if ( renderTarget.depthTexture ) {
+		var isWebGL2 = capabilities.isWebGL2;
+		var logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
+		var floatVertexTextures = capabilities.floatVertexTextures;
+		var precision = capabilities.precision;
+		var maxVertexUniforms = capabilities.maxVertexUniforms;
+		var vertexTextures = capabilities.vertexTextures;
 
-				renderTarget.depthTexture.dispose();
+		var shaderIDs = {
+			MeshDepthMaterial: 'depth',
+			MeshDistanceMaterial: 'distanceRGBA',
+			MeshNormalMaterial: 'normal',
+			MeshBasicMaterial: 'basic',
+			MeshLambertMaterial: 'lambert',
+			MeshPhongMaterial: 'phong',
+			MeshToonMaterial: 'toon',
+			MeshStandardMaterial: 'physical',
+			MeshPhysicalMaterial: 'physical',
+			MeshMatcapMaterial: 'matcap',
+			LineBasicMaterial: 'basic',
+			LineDashedMaterial: 'dashed',
+			PointsMaterial: 'points',
+			ShadowMaterial: 'shadow',
+			SpriteMaterial: 'sprite'
+		};
 
-			}
+		var parameterNames = [
+			"precision", "isWebGL2", "supportsVertexTextures", "outputEncoding", "instancing",
+			"map", "mapEncoding", "matcap", "matcapEncoding", "envMap", "envMapMode", "envMapEncoding", "envMapCubeUV",
+			"lightMap", "lightMapEncoding", "aoMap", "emissiveMap", "emissiveMapEncoding", "bumpMap", "normalMap", "objectSpaceNormalMap", "tangentSpaceNormalMap", "clearcoatMap", "clearcoatRoughnessMap", "clearcoatNormalMap", "displacementMap", "specularMap",
+			"roughnessMap", "metalnessMap", "gradientMap",
+			"alphaMap", "combine", "vertexColors", "vertexTangents", "vertexUvs", "uvsVertexOnly", "fog", "useFog", "fogExp2",
+			"flatShading", "sizeAttenuation", "logarithmicDepthBuffer", "skinning",
+			"maxBones", "useVertexTexture", "morphTargets", "morphNormals",
+			"maxMorphTargets", "maxMorphNormals", "premultipliedAlpha",
+			"numDirLights", "numPointLights", "numSpotLights", "numHemiLights", "numRectAreaLights",
+			"numDirLightShadows", "numPointLightShadows", "numSpotLightShadows",
+			"shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights',
+			"alphaTest", "doubleSided", "flipSided", "numClippingPlanes", "numClipIntersection", "depthPacking", "dithering",
+			"sheen"
+		];
 
-			if ( renderTarget.isWebGLCubeRenderTarget ) {
+		function getShaderObject( material, shaderID ) {
 
-				for ( var i = 0; i < 6; i ++ ) {
+			var shaderobject;
 
-					_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
-					if ( renderTargetProperties.__webglDepthbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] ); }
+			if ( shaderID ) {
 
-				}
+				var shader = ShaderLib[ shaderID ];
+
+				shaderobject = {
+					name: material.type,
+					uniforms: UniformsUtils.clone( shader.uniforms ),
+					vertexShader: shader.vertexShader,
+					fragmentShader: shader.fragmentShader
+				};
 
 			} else {
 
-				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
-				if ( renderTargetProperties.__webglDepthbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer ); }
-				if ( renderTargetProperties.__webglMultisampledFramebuffer ) { _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer ); }
-				if ( renderTargetProperties.__webglColorRenderbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer ); }
-				if ( renderTargetProperties.__webglDepthRenderbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer ); }
+				shaderobject = {
+					name: material.type,
+					uniforms: material.uniforms,
+					vertexShader: material.vertexShader,
+					fragmentShader: material.fragmentShader
+				};
 
 			}
 
-			properties.remove( renderTarget.texture );
-			properties.remove( renderTarget );
+			return shaderobject;
 
 		}
 
-		//
-
-		var textureUnits = 0;
-
-		function resetTextureUnits() {
-
-			textureUnits = 0;
-
-		}
+		function allocateBones( object ) {
 
-		function allocateTextureUnit() {
+			var skeleton = object.skeleton;
+			var bones = skeleton.bones;
 
-			var textureUnit = textureUnits;
+			if ( floatVertexTextures ) {
 
-			if ( textureUnit >= maxTextures ) {
+				return 1024;
 
-				console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures );
+			} else {
 
-			}
+				// default for when object is not specified
+				// ( for example when prebuilding shader to be used with multiple objects )
+				//
+				//  - leave some extra space for other uniforms
+				//  - limit here is ANGLE's 254 max uniform vectors
+				//    (up to 54 should be safe)
 
-			textureUnits += 1;
+				var nVertexUniforms = maxVertexUniforms;
+				var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
 
-			return textureUnit;
+				var maxBones = Math.min( nVertexMatrices, bones.length );
 
-		}
+				if ( maxBones < bones.length ) {
 
-		//
+					console.warn( 'THREE.WebGLRenderer: Skeleton has ' + bones.length + ' bones. This GPU supports ' + maxBones + '.' );
+					return 0;
 
-		function setTexture2D( texture, slot ) {
+				}
 
-			var textureProperties = properties.get( texture );
+				return maxBones;
 
-			if ( texture.isVideoTexture ) { updateVideoTexture( texture ); }
+			}
 
-			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
+		}
 
-				var image = texture.image;
+		function getTextureEncodingFromMap( map ) {
 
-				if ( image === undefined ) {
+			var encoding;
 
-					console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined' );
+			if ( ! map ) {
 
-				} else if ( image.complete === false ) {
+				encoding = LinearEncoding;
 
-					console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' );
+			} else if ( map.isTexture ) {
 
-				} else {
+				encoding = map.encoding;
 
-					uploadTexture( textureProperties, texture, slot );
-					return;
+			} else if ( map.isWebGLRenderTarget ) {
 
-				}
+				console.warn( "THREE.WebGLPrograms.getTextureEncodingFromMap: don't use render targets as textures. Use their .texture property instead." );
+				encoding = map.texture.encoding;
 
 			}
 
-			state.activeTexture( 33984 + slot );
-			state.bindTexture( 3553, textureProperties.__webglTexture );
+			return encoding;
 
 		}
 
-		function setTexture2DArray( texture, slot ) {
+		this.getParameters = function ( material, lights, shadows, scene, nClipPlanes, nClipIntersection, object ) {
 
-			var textureProperties = properties.get( texture );
+			var fog = scene.fog;
+			var environment = material.isMeshStandardMaterial ? scene.environment : null;
 
-			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
+			var envMap = material.envMap || environment;
 
-				uploadTexture( textureProperties, texture, slot );
-				return;
+			var shaderID = shaderIDs[ material.type ];
 
-			}
+			// heuristics to create shader parameters according to lights in the scene
+			// (not to blow over maxLights budget)
 
-			state.activeTexture( 33984 + slot );
-			state.bindTexture( 35866, textureProperties.__webglTexture );
+			var maxBones = object.isSkinnedMesh ? allocateBones( object ) : 0;
 
-		}
+			if ( material.precision !== null ) {
 
-		function setTexture3D( texture, slot ) {
+				precision = capabilities.getMaxPrecision( material.precision );
 
-			var textureProperties = properties.get( texture );
+				if ( precision !== material.precision ) {
 
-			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
+					console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' );
 
-				uploadTexture( textureProperties, texture, slot );
-				return;
+				}
 
 			}
 
-			state.activeTexture( 33984 + slot );
-			state.bindTexture( 32879, textureProperties.__webglTexture );
+			var shaderobject = getShaderObject( material, shaderID );
+			material.onBeforeCompile( shaderobject, renderer );
 
-		}
+			var currentRenderTarget = renderer.getRenderTarget();
 
-		function setTextureCube( texture, slot ) {
+			var parameters = {
 
-			if ( texture.image.length !== 6 ) { return; }
+				isWebGL2: isWebGL2,
 
-			var textureProperties = properties.get( texture );
+				shaderID: shaderID,
+				shaderName: shaderobject.name,
 
-			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
+				uniforms: shaderobject.uniforms,
+				vertexShader: shaderobject.vertexShader,
+				fragmentShader: shaderobject.fragmentShader,
+				defines: material.defines,
 
-				initTexture( textureProperties, texture );
+				isRawShaderMaterial: material.isRawShaderMaterial,
+				isShaderMaterial: material.isShaderMaterial,
 
-				state.activeTexture( 33984 + slot );
-				state.bindTexture( 34067, textureProperties.__webglTexture );
+				precision: precision,
 
-				_gl.pixelStorei( 37440, texture.flipY );
+				instancing: object.isInstancedMesh === true,
 
-				var isCompressed = ( texture && ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture ) );
-				var isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture );
+				supportsVertexTextures: vertexTextures,
+				outputEncoding: ( currentRenderTarget !== null ) ? getTextureEncodingFromMap( currentRenderTarget.texture ) : renderer.outputEncoding,
+				map: !! material.map,
+				mapEncoding: getTextureEncodingFromMap( material.map ),
+				matcap: !! material.matcap,
+				matcapEncoding: getTextureEncodingFromMap( material.matcap ),
+				envMap: !! envMap,
+				envMapMode: envMap && envMap.mapping,
+				envMapEncoding: getTextureEncodingFromMap( envMap ),
+				envMapCubeUV: ( !! envMap ) && ( ( envMap.mapping === CubeUVReflectionMapping ) || ( envMap.mapping === CubeUVRefractionMapping ) ),
+				lightMap: !! material.lightMap,
+				lightMapEncoding: getTextureEncodingFromMap( material.lightMap ),
+				aoMap: !! material.aoMap,
+				emissiveMap: !! material.emissiveMap,
+				emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap ),
+				bumpMap: !! material.bumpMap,
+				normalMap: !! material.normalMap,
+				objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap,
+				tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap,
+				clearcoatMap: !! material.clearcoatMap,
+				clearcoatRoughnessMap: !! material.clearcoatRoughnessMap,
+				clearcoatNormalMap: !! material.clearcoatNormalMap,
+				displacementMap: !! material.displacementMap,
+				roughnessMap: !! material.roughnessMap,
+				metalnessMap: !! material.metalnessMap,
+				specularMap: !! material.specularMap,
+				alphaMap: !! material.alphaMap,
 
-				var cubeImage = [];
+				gradientMap: !! material.gradientMap,
 
-				for ( var i = 0; i < 6; i ++ ) {
+				sheen: !! material.sheen,
 
-					if ( ! isCompressed && ! isDataTexture ) {
+				combine: material.combine,
 
-						cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, maxCubemapSize );
+				vertexTangents: ( material.normalMap && material.vertexTangents ),
+				vertexColors: material.vertexColors,
+				vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatMap || !! material.clearcoatRoughnessMap || !! material.clearcoatNormalMap || !! material.displacementMap,
+				uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap ) && !! material.displacementMap,
 
-					} else {
+				fog: !! fog,
+				useFog: material.fog,
+				fogExp2: ( fog && fog.isFogExp2 ),
 
-						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
+				flatShading: material.flatShading,
 
-					}
+				sizeAttenuation: material.sizeAttenuation,
+				logarithmicDepthBuffer: logarithmicDepthBuffer,
 
-				}
+				skinning: material.skinning && maxBones > 0,
+				maxBones: maxBones,
+				useVertexTexture: floatVertexTextures,
 
-				var image = cubeImage[ 0 ],
-					supportsMips = isPowerOfTwo( image ) || isWebGL2,
-					glFormat = utils.convert( texture.format ),
-					glType = utils.convert( texture.type ),
-					glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
+				morphTargets: material.morphTargets,
+				morphNormals: material.morphNormals,
+				maxMorphTargets: renderer.maxMorphTargets,
+				maxMorphNormals: renderer.maxMorphNormals,
 
-				setTextureParameters( 34067, texture, supportsMips );
+				numDirLights: lights.directional.length,
+				numPointLights: lights.point.length,
+				numSpotLights: lights.spot.length,
+				numRectAreaLights: lights.rectArea.length,
+				numHemiLights: lights.hemi.length,
 
-				var mipmaps;
+				numDirLightShadows: lights.directionalShadowMap.length,
+				numPointLightShadows: lights.pointShadowMap.length,
+				numSpotLightShadows: lights.spotShadowMap.length,
 
-				if ( isCompressed ) {
+				numClippingPlanes: nClipPlanes,
+				numClipIntersection: nClipIntersection,
 
-					for ( var i = 0; i < 6; i ++ ) {
+				dithering: material.dithering,
 
-						mipmaps = cubeImage[ i ].mipmaps;
+				shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0,
+				shadowMapType: renderer.shadowMap.type,
 
-						for ( var j = 0; j < mipmaps.length; j ++ ) {
+				toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping,
+				physicallyCorrectLights: renderer.physicallyCorrectLights,
 
-							var mipmap = mipmaps[ j ];
+				premultipliedAlpha: material.premultipliedAlpha,
 
-							if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
+				alphaTest: material.alphaTest,
+				doubleSided: material.side === DoubleSide,
+				flipSided: material.side === BackSide,
 
-								if ( glFormat !== null ) {
+				depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false,
 
-									state.compressedTexImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
+				index0AttributeName: material.index0AttributeName,
 
-								} else {
+				extensionDerivatives: material.extensions && material.extensions.derivatives,
+				extensionFragDepth: material.extensions && material.extensions.fragDepth,
+				extensionDrawBuffers: material.extensions && material.extensions.drawBuffers,
+				extensionShaderTextureLOD: material.extensions && material.extensions.shaderTextureLOD,
 
-									console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' );
+				rendererExtensionFragDepth: isWebGL2 || extensions.get( 'EXT_frag_depth' ) !== null,
+				rendererExtensionDrawBuffers: isWebGL2 || extensions.get( 'WEBGL_draw_buffers' ) !== null,
+				rendererExtensionShaderTextureLod: isWebGL2 || extensions.get( 'EXT_shader_texture_lod' ) !== null,
 
-								}
+				onBeforeCompile: material.onBeforeCompile
 
-							} else {
+			};
 
-								state.texImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
+			return parameters;
 
-							}
+		};
 
-						}
+		this.getProgramCacheKey = function ( parameters ) {
 
-					}
+			var array = [];
 
-					textureProperties.__maxMipLevel = mipmaps.length - 1;
+			if ( parameters.shaderID ) {
 
-				} else {
+				array.push( parameters.shaderID );
 
-					mipmaps = texture.mipmaps;
+			} else {
 
-					for ( var i = 0; i < 6; i ++ ) {
+				array.push( parameters.fragmentShader );
+				array.push( parameters.vertexShader );
 
-						if ( isDataTexture ) {
+			}
 
-							state.texImage2D( 34069 + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
+			if ( parameters.defines !== undefined ) {
 
-							for ( var j = 0; j < mipmaps.length; j ++ ) {
+				for ( var name in parameters.defines ) {
 
-								var mipmap = mipmaps[ j ];
-								var mipmapImage = mipmap.image[ i ].image;
+					array.push( name );
+					array.push( parameters.defines[ name ] );
 
-								state.texImage2D( 34069 + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data );
+				}
 
-							}
+			}
 
-						} else {
+			if ( parameters.isRawShaderMaterial === undefined ) {
 
-							state.texImage2D( 34069 + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] );
+				for ( var i = 0; i < parameterNames.length; i ++ ) {
 
-							for ( var j = 0; j < mipmaps.length; j ++ ) {
+					array.push( parameters[ parameterNames[ i ] ] );
 
-								var mipmap = mipmaps[ j ];
+				}
 
-								state.texImage2D( 34069 + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] );
+				array.push( renderer.outputEncoding );
+				array.push( renderer.gammaFactor );
 
-							}
+			}
 
-						}
+			array.push( parameters.onBeforeCompile.toString() );
 
-					}
+			return array.join();
 
-					textureProperties.__maxMipLevel = mipmaps.length;
+		};
 
-				}
+		this.acquireProgram = function ( parameters, cacheKey ) {
 
-				if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
+			var program;
 
-					// We assume images for cube map have the same size.
-					generateMipmap( 34067, texture, image.width, image.height );
+			// Check if code has been already compiled
+			for ( var p = 0, pl = programs.length; p < pl; p ++ ) {
 
-				}
+				var preexistingProgram = programs[ p ];
 
-				textureProperties.__version = texture.version;
+				if ( preexistingProgram.cacheKey === cacheKey ) {
 
-				if ( texture.onUpdate ) { texture.onUpdate( texture ); }
+					program = preexistingProgram;
+					++ program.usedTimes;
 
-			} else {
+					break;
 
-				state.activeTexture( 33984 + slot );
-				state.bindTexture( 34067, textureProperties.__webglTexture );
+				}
 
 			}
 
-		}
-
-		function setTextureCubeDynamic( texture, slot ) {
-
-			state.activeTexture( 33984 + slot );
-			state.bindTexture( 34067, properties.get( texture ).__webglTexture );
-
-		}
-
-		var wrappingToGL = {};
-		wrappingToGL[ RepeatWrapping ] = 10497;
-		wrappingToGL[ ClampToEdgeWrapping ] = 33071;
-		wrappingToGL[ MirroredRepeatWrapping ] = 33648;
-
-		var filterToGL = {};
-		filterToGL[ NearestFilter ] = 9728;
-		filterToGL[ NearestMipmapNearestFilter ] = 9984;
-		filterToGL[ NearestMipmapLinearFilter ] = 9986;
-		filterToGL[ LinearFilter ] = 9729;
-		filterToGL[ LinearMipmapNearestFilter ] = 9985;
-		filterToGL[ LinearMipmapLinearFilter ] = 9987;
+			if ( program === undefined ) {
 
-		function setTextureParameters( textureType, texture, supportsMips ) {
+				program = new WebGLProgram( renderer, cacheKey, parameters );
+				programs.push( program );
 
-			if ( supportsMips ) {
+			}
 
-				_gl.texParameteri( textureType, 10242, wrappingToGL[ texture.wrapS ] );
-				_gl.texParameteri( textureType, 10243, wrappingToGL[ texture.wrapT ] );
+			return program;
 
-				if ( textureType === 32879 || textureType === 35866 ) {
+		};
 
-					_gl.texParameteri( textureType, 32882, wrappingToGL[ texture.wrapR ] );
+		this.releaseProgram = function ( program ) {
 
-				}
+			if ( -- program.usedTimes === 0 ) {
 
-				_gl.texParameteri( textureType, 10240, filterToGL[ texture.magFilter ] );
-				_gl.texParameteri( textureType, 10241, filterToGL[ texture.minFilter ] );
+				// Remove from unordered set
+				var i = programs.indexOf( program );
+				programs[ i ] = programs[ programs.length - 1 ];
+				programs.pop();
 
-			} else {
+				// Free WebGL resources
+				program.destroy();
 
-				_gl.texParameteri( textureType, 10242, 33071 );
-				_gl.texParameteri( textureType, 10243, 33071 );
+			}
 
-				if ( textureType === 32879 || textureType === 35866 ) {
+		};
 
-					_gl.texParameteri( textureType, 32882, 33071 );
+		// Exposed for resource monitoring & error feedback via renderer.info:
+		this.programs = programs;
 
-				}
+	}
 
-				if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) {
+	/**
+	 * @author fordacious / fordacious.github.io
+	 */
 
-					console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' );
+	function WebGLProperties() {
 
-				}
+		var properties = new WeakMap();
 
-				_gl.texParameteri( textureType, 10240, filterFallback( texture.magFilter ) );
-				_gl.texParameteri( textureType, 10241, filterFallback( texture.minFilter ) );
+		function get( object ) {
 
-				if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) {
+			var map = properties.get( object );
 
-					console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' );
+			if ( map === undefined ) {
 
-				}
+				map = {};
+				properties.set( object, map );
 
 			}
 
-			var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
+			return map;
 
-			if ( extension ) {
+		}
 
-				if ( texture.type === FloatType && extensions.get( 'OES_texture_float_linear' ) === null ) { return; }
-				if ( texture.type === HalfFloatType && ( isWebGL2 || extensions.get( 'OES_texture_half_float_linear' ) ) === null ) { return; }
+		function remove( object ) {
 
-				if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
+			properties.delete( object );
 
-					_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
-					properties.get( texture ).__currentAnisotropy = texture.anisotropy;
+		}
 
-				}
+		function update( object, key, value ) {
 
-			}
+			properties.get( object )[ key ] = value;
 
 		}
 
-		function initTexture( textureProperties, texture ) {
-
-			if ( textureProperties.__webglInit === undefined ) {
+		function dispose() {
 
-				textureProperties.__webglInit = true;
+			properties = new WeakMap();
 
-				texture.addEventListener( 'dispose', onTextureDispose );
+		}
 
-				textureProperties.__webglTexture = _gl.createTexture();
+		return {
+			get: get,
+			remove: remove,
+			update: update,
+			dispose: dispose
+		};
 
-				info.memory.textures ++;
+	}
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		}
+	function painterSortStable( a, b ) {
 
-		function uploadTexture( textureProperties, texture, slot ) {
+		if ( a.groupOrder !== b.groupOrder ) {
 
-			var textureType = 3553;
+			return a.groupOrder - b.groupOrder;
 
-			if ( texture.isDataTexture2DArray ) { textureType = 35866; }
-			if ( texture.isDataTexture3D ) { textureType = 32879; }
+		} else if ( a.renderOrder !== b.renderOrder ) {
 
-			initTexture( textureProperties, texture );
+			return a.renderOrder - b.renderOrder;
 
-			state.activeTexture( 33984 + slot );
-			state.bindTexture( textureType, textureProperties.__webglTexture );
+		} else if ( a.program !== b.program ) {
 
-			_gl.pixelStorei( 37440, texture.flipY );
-			_gl.pixelStorei( 37441, texture.premultiplyAlpha );
-			_gl.pixelStorei( 3317, texture.unpackAlignment );
+			return a.program.id - b.program.id;
 
-			var needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false;
-			var image = resizeImage( texture.image, needsPowerOfTwo, false, maxTextureSize );
+		} else if ( a.material.id !== b.material.id ) {
 
-			var supportsMips = isPowerOfTwo( image ) || isWebGL2,
-				glFormat = utils.convert( texture.format ),
-				glType = utils.convert( texture.type ),
-				glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
+			return a.material.id - b.material.id;
 
-			setTextureParameters( textureType, texture, supportsMips );
+		} else if ( a.z !== b.z ) {
 
-			var mipmap, mipmaps = texture.mipmaps;
+			return a.z - b.z;
 
-			if ( texture.isDepthTexture ) {
+		} else {
 
-				// populate depth texture with dummy data
+			return a.id - b.id;
 
-				glInternalFormat = 6402;
+		}
 
-				if ( isWebGL2 ) {
+	}
 
-					if ( texture.type === FloatType ) {
+	function reversePainterSortStable( a, b ) {
 
-						glInternalFormat = 36012;
+		if ( a.groupOrder !== b.groupOrder ) {
 
-					} else if ( texture.type === UnsignedIntType ) {
+			return a.groupOrder - b.groupOrder;
 
-						glInternalFormat = 33190;
+		} else if ( a.renderOrder !== b.renderOrder ) {
 
-					} else if ( texture.type === UnsignedInt248Type ) {
+			return a.renderOrder - b.renderOrder;
 
-						glInternalFormat = 35056;
+		} else if ( a.z !== b.z ) {
 
-					} else {
+			return b.z - a.z;
 
-						glInternalFormat = 33189; // WebGL2 requires sized internalformat for glTexImage2D
+		} else {
 
-					}
+			return a.id - b.id;
 
-				} else {
+		}
 
-					if ( texture.type === FloatType ) {
+	}
 
-						console.error( 'WebGLRenderer: Floating point depth texture requires WebGL2.' );
 
-					}
+	function WebGLRenderList() {
 
-				}
+		var renderItems = [];
+		var renderItemsIndex = 0;
 
-				// validation checks for WebGL 1
+		var opaque = [];
+		var transparent = [];
 
-				if ( texture.format === DepthFormat && glInternalFormat === 6402 ) {
+		var defaultProgram = { id: - 1 };
 
-					// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
-					// DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
-					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
-					if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) {
+		function init() {
 
-						console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' );
+			renderItemsIndex = 0;
 
-						texture.type = UnsignedShortType;
-						glType = utils.convert( texture.type );
+			opaque.length = 0;
+			transparent.length = 0;
 
-					}
+		}
 
-				}
+		function getNextRenderItem( object, geometry, material, groupOrder, z, group ) {
 
-				if ( texture.format === DepthStencilFormat && glInternalFormat === 6402 ) {
+			var renderItem = renderItems[ renderItemsIndex ];
 
-					// Depth stencil textures need the DEPTH_STENCIL internal format
-					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
-					glInternalFormat = 34041;
+			if ( renderItem === undefined ) {
 
-					// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
-					// DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
-					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
-					if ( texture.type !== UnsignedInt248Type ) {
+				renderItem = {
+					id: object.id,
+					object: object,
+					geometry: geometry,
+					material: material,
+					program: material.program || defaultProgram,
+					groupOrder: groupOrder,
+					renderOrder: object.renderOrder,
+					z: z,
+					group: group
+				};
 
-						console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' );
+				renderItems[ renderItemsIndex ] = renderItem;
 
-						texture.type = UnsignedInt248Type;
-						glType = utils.convert( texture.type );
+			} else {
 
-					}
+				renderItem.id = object.id;
+				renderItem.object = object;
+				renderItem.geometry = geometry;
+				renderItem.material = material;
+				renderItem.program = material.program || defaultProgram;
+				renderItem.groupOrder = groupOrder;
+				renderItem.renderOrder = object.renderOrder;
+				renderItem.z = z;
+				renderItem.group = group;
 
-				}
+			}
 
-				//
+			renderItemsIndex ++;
 
-				state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null );
+			return renderItem;
 
-			} else if ( texture.isDataTexture ) {
+		}
 
-				// use manually created mipmaps if available
-				// if there are no manual mipmaps
-				// set 0 level mipmap and then use GL to generate other mipmap levels
+		function push( object, geometry, material, groupOrder, z, group ) {
 
-				if ( mipmaps.length > 0 && supportsMips ) {
+			var renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group );
 
-					for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
+			( material.transparent === true ? transparent : opaque ).push( renderItem );
 
-						mipmap = mipmaps[ i ];
-						state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
+		}
 
-					}
+		function unshift( object, geometry, material, groupOrder, z, group ) {
 
-					texture.generateMipmaps = false;
-					textureProperties.__maxMipLevel = mipmaps.length - 1;
+			var renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group );
 
-				} else {
+			( material.transparent === true ? transparent : opaque ).unshift( renderItem );
 
-					state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data );
-					textureProperties.__maxMipLevel = 0;
+		}
 
-				}
+		function sort( customOpaqueSort, customTransparentSort ) {
 
-			} else if ( texture.isCompressedTexture ) {
+			if ( opaque.length > 1 ) { opaque.sort( customOpaqueSort || painterSortStable ); }
+			if ( transparent.length > 1 ) { transparent.sort( customTransparentSort || reversePainterSortStable ); }
 
-				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
+		}
 
-					mipmap = mipmaps[ i ];
+		function finish() {
 
-					if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
+			// Clear references from inactive renderItems in the list
 
-						if ( glFormat !== null ) {
+			for ( var i = renderItemsIndex, il = renderItems.length; i < il; i ++ ) {
 
-							state.compressedTexImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
+				var renderItem = renderItems[ i ];
 
-						} else {
+				if ( renderItem.id === null ) { break; }
 
-							console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
+				renderItem.id = null;
+				renderItem.object = null;
+				renderItem.geometry = null;
+				renderItem.material = null;
+				renderItem.program = null;
+				renderItem.group = null;
 
-						}
+			}
 
-					} else {
+		}
 
-						state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
+		return {
+			opaque: opaque,
+			transparent: transparent,
 
-					}
+			init: init,
+			push: push,
+			unshift: unshift,
+			finish: finish,
 
-				}
+			sort: sort
+		};
 
-				textureProperties.__maxMipLevel = mipmaps.length - 1;
+	}
 
-			} else if ( texture.isDataTexture2DArray ) {
+	function WebGLRenderLists() {
 
-				state.texImage3D( 35866, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
-				textureProperties.__maxMipLevel = 0;
+		var lists = new WeakMap();
 
-			} else if ( texture.isDataTexture3D ) {
+		function onSceneDispose( event ) {
 
-				state.texImage3D( 32879, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
-				textureProperties.__maxMipLevel = 0;
+			var scene = event.target;
 
-			} else {
+			scene.removeEventListener( 'dispose', onSceneDispose );
 
-				// regular Texture (image, video, canvas)
+			lists.delete( scene );
 
-				// use manually created mipmaps if available
-				// if there are no manual mipmaps
-				// set 0 level mipmap and then use GL to generate other mipmap levels
+		}
 
-				if ( mipmaps.length > 0 && supportsMips ) {
+		function get( scene, camera ) {
 
-					for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
+			var cameras = lists.get( scene );
+			var list;
+			if ( cameras === undefined ) {
 
-						mipmap = mipmaps[ i ];
-						state.texImage2D( 3553, i, glInternalFormat, glFormat, glType, mipmap );
+				list = new WebGLRenderList();
+				lists.set( scene, new WeakMap() );
+				lists.get( scene ).set( camera, list );
 
-					}
+				scene.addEventListener( 'dispose', onSceneDispose );
 
-					texture.generateMipmaps = false;
-					textureProperties.__maxMipLevel = mipmaps.length - 1;
+			} else {
 
-				} else {
+				list = cameras.get( camera );
+				if ( list === undefined ) {
 
-					state.texImage2D( 3553, 0, glInternalFormat, glFormat, glType, image );
-					textureProperties.__maxMipLevel = 0;
+					list = new WebGLRenderList();
+					cameras.set( camera, list );
 
 				}
 
 			}
 
-			if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
-
-				generateMipmap( textureType, texture, image.width, image.height );
-
-			}
-
-			textureProperties.__version = texture.version;
-
-			if ( texture.onUpdate ) { texture.onUpdate( texture ); }
+			return list;
 
 		}
 
-		// Render targets
-
-		// Setup storage for target texture and bind it to correct framebuffer
-		function setupFrameBufferTexture( framebuffer, renderTarget, attachment, textureTarget ) {
+		function dispose() {
 
-			var glFormat = utils.convert( renderTarget.texture.format );
-			var glType = utils.convert( renderTarget.texture.type );
-			var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
-			state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
-			_gl.bindFramebuffer( 36160, framebuffer );
-			_gl.framebufferTexture2D( 36160, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
-			_gl.bindFramebuffer( 36160, null );
+			lists = new WeakMap();
 
 		}
 
-		// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
-		function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) {
+		return {
+			get: get,
+			dispose: dispose
+		};
 
-			_gl.bindRenderbuffer( 36161, renderbuffer );
+	}
 
-			if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				var glInternalFormat = 33189;
+	function UniformsCache() {
 
-				if ( isMultisample ) {
+		var lights = {};
 
-					var depthTexture = renderTarget.depthTexture;
+		return {
 
-					if ( depthTexture && depthTexture.isDepthTexture ) {
+			get: function ( light ) {
 
-						if ( depthTexture.type === FloatType ) {
+				if ( lights[ light.id ] !== undefined ) {
 
-							glInternalFormat = 36012;
+					return lights[ light.id ];
 
-						} else if ( depthTexture.type === UnsignedIntType ) {
+				}
 
-							glInternalFormat = 33190;
+				var uniforms;
 
-						}
+				switch ( light.type ) {
 
-					}
+					case 'DirectionalLight':
+						uniforms = {
+							direction: new Vector3(),
+							color: new Color()
+						};
+						break;
 
-					var samples = getRenderTargetSamples( renderTarget );
+					case 'SpotLight':
+						uniforms = {
+							position: new Vector3(),
+							direction: new Vector3(),
+							color: new Color(),
+							distance: 0,
+							coneCos: 0,
+							penumbraCos: 0,
+							decay: 0
+						};
+						break;
 
-					_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
+					case 'PointLight':
+						uniforms = {
+							position: new Vector3(),
+							color: new Color(),
+							distance: 0,
+							decay: 0
+						};
+						break;
 
-				} else {
+					case 'HemisphereLight':
+						uniforms = {
+							direction: new Vector3(),
+							skyColor: new Color(),
+							groundColor: new Color()
+						};
+						break;
 
-					_gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height );
+					case 'RectAreaLight':
+						uniforms = {
+							color: new Color(),
+							position: new Vector3(),
+							halfWidth: new Vector3(),
+							halfHeight: new Vector3()
+						};
+						break;
 
 				}
 
-				_gl.framebufferRenderbuffer( 36160, 36096, 36161, renderbuffer );
+				lights[ light.id ] = uniforms;
 
-			} else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
+				return uniforms;
 
-				if ( isMultisample ) {
+			}
 
-					var samples = getRenderTargetSamples( renderTarget );
+		};
 
-					_gl.renderbufferStorageMultisample( 36161, samples, 35056, renderTarget.width, renderTarget.height );
+	}
 
-				} else {
+	function ShadowUniformsCache() {
 
-					_gl.renderbufferStorage( 36161, 34041, renderTarget.width, renderTarget.height );
+		var lights = {};
 
-				}
+		return {
 
+			get: function ( light ) {
 
-				_gl.framebufferRenderbuffer( 36160, 33306, 36161, renderbuffer );
+				if ( lights[ light.id ] !== undefined ) {
 
-			} else {
+					return lights[ light.id ];
 
-				var glFormat = utils.convert( renderTarget.texture.format );
-				var glType = utils.convert( renderTarget.texture.type );
-				var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
+				}
 
-				if ( isMultisample ) {
+				var uniforms;
 
-					var samples = getRenderTargetSamples( renderTarget );
+				switch ( light.type ) {
 
-					_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
+					case 'DirectionalLight':
+						uniforms = {
+							shadowBias: 0,
+							shadowRadius: 1,
+							shadowMapSize: new Vector2()
+						};
+						break;
 
-				} else {
+					case 'SpotLight':
+						uniforms = {
+							shadowBias: 0,
+							shadowRadius: 1,
+							shadowMapSize: new Vector2()
+						};
+						break;
 
-					_gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height );
+					case 'PointLight':
+						uniforms = {
+							shadowBias: 0,
+							shadowRadius: 1,
+							shadowMapSize: new Vector2(),
+							shadowCameraNear: 1,
+							shadowCameraFar: 1000
+						};
+						break;
+
+					// TODO (abelnation): set RectAreaLight shadow uniforms
 
 				}
 
+				lights[ light.id ] = uniforms;
+
+				return uniforms;
+
 			}
 
-			_gl.bindRenderbuffer( 36161, null );
+		};
 
-		}
+	}
 
-		// Setup resources for a Depth Texture for a FBO (needs an extension)
-		function setupDepthTexture( framebuffer, renderTarget ) {
 
-			var isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget );
-			if ( isCube ) { throw new Error( 'Depth Texture with cube render targets is not supported' ); }
 
-			_gl.bindFramebuffer( 36160, framebuffer );
+	var nextVersion = 0;
 
-			if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) {
+	function shadowCastingLightsFirst( lightA, lightB ) {
 
-				throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' );
+		return ( lightB.castShadow ? 1 : 0 ) - ( lightA.castShadow ? 1 : 0 );
 
-			}
+	}
 
-			// upload an empty depth texture with framebuffer size
-			if ( ! properties.get( renderTarget.depthTexture ).__webglTexture ||
-					renderTarget.depthTexture.image.width !== renderTarget.width ||
-					renderTarget.depthTexture.image.height !== renderTarget.height ) {
+	function WebGLLights() {
 
-				renderTarget.depthTexture.image.width = renderTarget.width;
-				renderTarget.depthTexture.image.height = renderTarget.height;
-				renderTarget.depthTexture.needsUpdate = true;
+		var cache = new UniformsCache();
 
-			}
+		var shadowCache = ShadowUniformsCache();
 
-			setTexture2D( renderTarget.depthTexture, 0 );
+		var state = {
 
-			var webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
+			version: 0,
 
-			if ( renderTarget.depthTexture.format === DepthFormat ) {
+			hash: {
+				directionalLength: - 1,
+				pointLength: - 1,
+				spotLength: - 1,
+				rectAreaLength: - 1,
+				hemiLength: - 1,
 
-				_gl.framebufferTexture2D( 36160, 36096, 3553, webglDepthTexture, 0 );
+				numDirectionalShadows: - 1,
+				numPointShadows: - 1,
+				numSpotShadows: - 1
+			},
 
-			} else if ( renderTarget.depthTexture.format === DepthStencilFormat ) {
+			ambient: [ 0, 0, 0 ],
+			probe: [],
+			directional: [],
+			directionalShadow: [],
+			directionalShadowMap: [],
+			directionalShadowMatrix: [],
+			spot: [],
+			spotShadow: [],
+			spotShadowMap: [],
+			spotShadowMatrix: [],
+			rectArea: [],
+			point: [],
+			pointShadow: [],
+			pointShadowMap: [],
+			pointShadowMatrix: [],
+			hemi: []
 
-				_gl.framebufferTexture2D( 36160, 33306, 3553, webglDepthTexture, 0 );
+		};
 
-			} else {
+		for ( var i = 0; i < 9; i ++ ) { state.probe.push( new Vector3() ); }
 
-				throw new Error( 'Unknown depthTexture format' );
+		var vector3 = new Vector3();
+		var matrix4 = new Matrix4();
+		var matrix42 = new Matrix4();
 
-			}
+		function setup( lights, shadows, camera ) {
 
-		}
+			var r = 0, g = 0, b = 0;
 
-		// Setup GL resources for a non-texture depth buffer
-		function setupDepthRenderbuffer( renderTarget ) {
+			for ( var i = 0; i < 9; i ++ ) { state.probe[ i ].set( 0, 0, 0 ); }
 
-			var renderTargetProperties = properties.get( renderTarget );
+			var directionalLength = 0;
+			var pointLength = 0;
+			var spotLength = 0;
+			var rectAreaLength = 0;
+			var hemiLength = 0;
 
-			var isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
+			var numDirectionalShadows = 0;
+			var numPointShadows = 0;
+			var numSpotShadows = 0;
 
-			if ( renderTarget.depthTexture ) {
+			var viewMatrix = camera.matrixWorldInverse;
 
-				if ( isCube ) { throw new Error( 'target.depthTexture not supported in Cube render targets' ); }
+			lights.sort( shadowCastingLightsFirst );
 
-				setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );
+			for ( var i = 0, l = lights.length; i < l; i ++ ) {
 
-			} else {
+				var light = lights[ i ];
 
-				if ( isCube ) {
+				var color = light.color;
+				var intensity = light.intensity;
+				var distance = light.distance;
 
-					renderTargetProperties.__webglDepthbuffer = [];
+				var shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
 
-					for ( var i = 0; i < 6; i ++ ) {
+				if ( light.isAmbientLight ) {
 
-						_gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer[ i ] );
-						renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
-						setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false );
+					r += color.r * intensity;
+					g += color.g * intensity;
+					b += color.b * intensity;
 
-					}
+				} else if ( light.isLightProbe ) {
 
-				} else {
+					for ( var j = 0; j < 9; j ++ ) {
 
-					_gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer );
-					renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
-					setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false );
+						state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity );
 
-				}
+					}
 
-			}
+				} else if ( light.isDirectionalLight ) {
 
-			_gl.bindFramebuffer( 36160, null );
+					var uniforms = cache.get( light );
 
-		}
+					uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
+					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
+					vector3.setFromMatrixPosition( light.target.matrixWorld );
+					uniforms.direction.sub( vector3 );
+					uniforms.direction.transformDirection( viewMatrix );
 
-		// Set up GL resources for the render target
-		function setupRenderTarget( renderTarget ) {
+					if ( light.castShadow ) {
 
-			var renderTargetProperties = properties.get( renderTarget );
-			var textureProperties = properties.get( renderTarget.texture );
+						var shadow = light.shadow;
 
-			renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
+						var shadowUniforms = shadowCache.get( light );
 
-			textureProperties.__webglTexture = _gl.createTexture();
+						shadowUniforms.shadowBias = shadow.bias;
+						shadowUniforms.shadowRadius = shadow.radius;
+						shadowUniforms.shadowMapSize = shadow.mapSize;
 
-			info.memory.textures ++;
+						state.directionalShadow[ directionalLength ] = shadowUniforms;
+						state.directionalShadowMap[ directionalLength ] = shadowMap;
+						state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix;
 
-			var isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
-			var isMultisample = ( renderTarget.isWebGLMultisampleRenderTarget === true );
-			var supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
+						numDirectionalShadows ++;
 
-			// Handles WebGL2 RGBFormat fallback - #18858
+					}
 
-			if ( isWebGL2 && renderTarget.texture.format === RGBFormat && ( renderTarget.texture.type === FloatType || renderTarget.texture.type === HalfFloatType ) ) {
+					state.directional[ directionalLength ] = uniforms;
 
-				renderTarget.texture.format = RGBAFormat;
+					directionalLength ++;
 
-				console.warn( 'THREE.WebGLRenderer: Rendering to textures with RGB format is not supported. Using RGBA format instead.' );
+				} else if ( light.isSpotLight ) {
 
-			}
+					var uniforms = cache.get( light );
 
-			// Setup framebuffer
+					uniforms.position.setFromMatrixPosition( light.matrixWorld );
+					uniforms.position.applyMatrix4( viewMatrix );
 
-			if ( isCube ) {
+					uniforms.color.copy( color ).multiplyScalar( intensity );
+					uniforms.distance = distance;
 
-				renderTargetProperties.__webglFramebuffer = [];
+					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
+					vector3.setFromMatrixPosition( light.target.matrixWorld );
+					uniforms.direction.sub( vector3 );
+					uniforms.direction.transformDirection( viewMatrix );
 
-				for ( var i = 0; i < 6; i ++ ) {
+					uniforms.coneCos = Math.cos( light.angle );
+					uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) );
+					uniforms.decay = light.decay;
 
-					renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
+					if ( light.castShadow ) {
 
-				}
+						var shadow = light.shadow;
 
-			} else {
+						var shadowUniforms = shadowCache.get( light );
 
-				renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
+						shadowUniforms.shadowBias = shadow.bias;
+						shadowUniforms.shadowRadius = shadow.radius;
+						shadowUniforms.shadowMapSize = shadow.mapSize;
 
-				if ( isMultisample ) {
+						state.spotShadow[ spotLength ] = shadowUniforms;
+						state.spotShadowMap[ spotLength ] = shadowMap;
+						state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
 
-					if ( isWebGL2 ) {
+						numSpotShadows ++;
 
-						renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
-						renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer();
+					}
 
-						_gl.bindRenderbuffer( 36161, renderTargetProperties.__webglColorRenderbuffer );
+					state.spot[ spotLength ] = uniforms;
 
-						var glFormat = utils.convert( renderTarget.texture.format );
-						var glType = utils.convert( renderTarget.texture.type );
-						var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
-						var samples = getRenderTargetSamples( renderTarget );
-						_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
+					spotLength ++;
 
-						_gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer );
-						_gl.framebufferRenderbuffer( 36160, 36064, 36161, renderTargetProperties.__webglColorRenderbuffer );
-						_gl.bindRenderbuffer( 36161, null );
+				} else if ( light.isRectAreaLight ) {
 
-						if ( renderTarget.depthBuffer ) {
+					var uniforms = cache.get( light );
 
-							renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
-							setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true );
+					// (a) intensity is the total visible light emitted
+					//uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) );
 
-						}
+					// (b) intensity is the brightness of the light
+					uniforms.color.copy( color ).multiplyScalar( intensity );
 
-						_gl.bindFramebuffer( 36160, null );
+					uniforms.position.setFromMatrixPosition( light.matrixWorld );
+					uniforms.position.applyMatrix4( viewMatrix );
 
+					// extract local rotation of light to derive width/height half vectors
+					matrix42.identity();
+					matrix4.copy( light.matrixWorld );
+					matrix4.premultiply( viewMatrix );
+					matrix42.extractRotation( matrix4 );
 
-					} else {
+					uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 );
+					uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 );
 
-						console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
+					uniforms.halfWidth.applyMatrix4( matrix42 );
+					uniforms.halfHeight.applyMatrix4( matrix42 );
 
-					}
+					// TODO (abelnation): RectAreaLight distance?
+					// uniforms.distance = distance;
 
-				}
+					state.rectArea[ rectAreaLength ] = uniforms;
 
-			}
+					rectAreaLength ++;
 
-			// Setup color buffer
+				} else if ( light.isPointLight ) {
 
-			if ( isCube ) {
+					var uniforms = cache.get( light );
 
-				state.bindTexture( 34067, textureProperties.__webglTexture );
-				setTextureParameters( 34067, renderTarget.texture, supportsMips );
+					uniforms.position.setFromMatrixPosition( light.matrixWorld );
+					uniforms.position.applyMatrix4( viewMatrix );
 
-				for ( var i = 0; i < 6; i ++ ) {
+					uniforms.color.copy( light.color ).multiplyScalar( light.intensity );
+					uniforms.distance = light.distance;
+					uniforms.decay = light.decay;
 
-					setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, 36064, 34069 + i );
+					if ( light.castShadow ) {
 
-				}
+						var shadow = light.shadow;
 
-				if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
+						var shadowUniforms = shadowCache.get( light );
 
-					generateMipmap( 34067, renderTarget.texture, renderTarget.width, renderTarget.height );
+						shadowUniforms.shadowBias = shadow.bias;
+						shadowUniforms.shadowRadius = shadow.radius;
+						shadowUniforms.shadowMapSize = shadow.mapSize;
+						shadowUniforms.shadowCameraNear = shadow.camera.near;
+						shadowUniforms.shadowCameraFar = shadow.camera.far;
 
-				}
+						state.pointShadow[ pointLength ] = shadowUniforms;
+						state.pointShadowMap[ pointLength ] = shadowMap;
+						state.pointShadowMatrix[ pointLength ] = light.shadow.matrix;
 
-				state.bindTexture( 34067, null );
+						numPointShadows ++;
 
-			} else {
+					}
 
-				state.bindTexture( 3553, textureProperties.__webglTexture );
-				setTextureParameters( 3553, renderTarget.texture, supportsMips );
-				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, 36064, 3553 );
+					state.point[ pointLength ] = uniforms;
 
-				if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
+					pointLength ++;
 
-					generateMipmap( 3553, renderTarget.texture, renderTarget.width, renderTarget.height );
+				} else if ( light.isHemisphereLight ) {
 
-				}
+					var uniforms = cache.get( light );
 
-				state.bindTexture( 3553, null );
+					uniforms.direction.setFromMatrixPosition( light.matrixWorld );
+					uniforms.direction.transformDirection( viewMatrix );
+					uniforms.direction.normalize();
 
-			}
+					uniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
+					uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
 
-			// Setup depth and stencil buffers
+					state.hemi[ hemiLength ] = uniforms;
 
-			if ( renderTarget.depthBuffer ) {
+					hemiLength ++;
 
-				setupDepthRenderbuffer( renderTarget );
+				}
 
 			}
 
-		}
+			state.ambient[ 0 ] = r;
+			state.ambient[ 1 ] = g;
+			state.ambient[ 2 ] = b;
 
-		function updateRenderTargetMipmap( renderTarget ) {
+			var hash = state.hash;
 
-			var texture = renderTarget.texture;
-			var supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
+			if ( hash.directionalLength !== directionalLength ||
+				hash.pointLength !== pointLength ||
+				hash.spotLength !== spotLength ||
+				hash.rectAreaLength !== rectAreaLength ||
+				hash.hemiLength !== hemiLength ||
+				hash.numDirectionalShadows !== numDirectionalShadows ||
+				hash.numPointShadows !== numPointShadows ||
+				hash.numSpotShadows !== numSpotShadows ) {
 
-			if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
+				state.directional.length = directionalLength;
+				state.spot.length = spotLength;
+				state.rectArea.length = rectAreaLength;
+				state.point.length = pointLength;
+				state.hemi.length = hemiLength;
 
-				var target = renderTarget.isWebGLCubeRenderTarget ? 34067 : 3553;
-				var webglTexture = properties.get( texture ).__webglTexture;
+				state.directionalShadow.length = numDirectionalShadows;
+				state.directionalShadowMap.length = numDirectionalShadows;
+				state.pointShadow.length = numPointShadows;
+				state.pointShadowMap.length = numPointShadows;
+				state.spotShadow.length = numSpotShadows;
+				state.spotShadowMap.length = numSpotShadows;
+				state.directionalShadowMatrix.length = numDirectionalShadows;
+				state.pointShadowMatrix.length = numPointShadows;
+				state.spotShadowMatrix.length = numSpotShadows;
 
-				state.bindTexture( target, webglTexture );
-				generateMipmap( target, texture, renderTarget.width, renderTarget.height );
-				state.bindTexture( target, null );
+				hash.directionalLength = directionalLength;
+				hash.pointLength = pointLength;
+				hash.spotLength = spotLength;
+				hash.rectAreaLength = rectAreaLength;
+				hash.hemiLength = hemiLength;
+
+				hash.numDirectionalShadows = numDirectionalShadows;
+				hash.numPointShadows = numPointShadows;
+				hash.numSpotShadows = numSpotShadows;
+
+				state.version = nextVersion ++;
 
 			}
 
 		}
 
-		function updateMultisampleRenderTarget( renderTarget ) {
+		return {
+			setup: setup,
+			state: state
+		};
 
-			if ( renderTarget.isWebGLMultisampleRenderTarget ) {
+	}
 
-				if ( isWebGL2 ) {
+	/**
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-					var renderTargetProperties = properties.get( renderTarget );
+	function WebGLRenderState() {
 
-					_gl.bindFramebuffer( 36008, renderTargetProperties.__webglMultisampledFramebuffer );
-					_gl.bindFramebuffer( 36009, renderTargetProperties.__webglFramebuffer );
+		var lights = new WebGLLights();
 
-					var width = renderTarget.width;
-					var height = renderTarget.height;
-					var mask = 16384;
+		var lightsArray = [];
+		var shadowsArray = [];
 
-					if ( renderTarget.depthBuffer ) { mask |= 256; }
-					if ( renderTarget.stencilBuffer ) { mask |= 1024; }
+		function init() {
 
-					_gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, 9728 );
+			lightsArray.length = 0;
+			shadowsArray.length = 0;
 
-					_gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer ); // see #18905
+		}
 
-				} else {
+		function pushLight( light ) {
 
-					console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
+			lightsArray.push( light );
 
-				}
+		}
 
-			}
+		function pushShadow( shadowLight ) {
+
+			shadowsArray.push( shadowLight );
 
 		}
 
-		function getRenderTargetSamples( renderTarget ) {
+		function setupLights( camera ) {
 
-			return ( isWebGL2 && renderTarget.isWebGLMultisampleRenderTarget ) ?
-				Math.min( maxSamples, renderTarget.samples ) : 0;
+			lights.setup( lightsArray, shadowsArray, camera );
 
 		}
 
-		function updateVideoTexture( texture ) {
+		var state = {
+			lightsArray: lightsArray,
+			shadowsArray: shadowsArray,
 
-			var frame = info.render.frame;
+			lights: lights
+		};
 
-			// Check the last frame we updated the VideoTexture
+		return {
+			init: init,
+			state: state,
+			setupLights: setupLights,
 
-			if ( _videoTextures.get( texture ) !== frame ) {
+			pushLight: pushLight,
+			pushShadow: pushShadow
+		};
 
-				_videoTextures.set( texture, frame );
-				texture.update();
+	}
 
-			}
+	function WebGLRenderStates() {
 
-		}
+		var renderStates = new WeakMap();
 
-		// backwards compatibility
+		function onSceneDispose( event ) {
 
-		var warnedTexture2D = false;
-		var warnedTextureCube = false;
+			var scene = event.target;
 
-		function safeSetTexture2D( texture, slot ) {
+			scene.removeEventListener( 'dispose', onSceneDispose );
 
-			if ( texture && texture.isWebGLRenderTarget ) {
+			renderStates.delete( scene );
 
-				if ( warnedTexture2D === false ) {
+		}
 
-					console.warn( "THREE.WebGLTextures.safeSetTexture2D: don't use render targets as textures. Use their .texture property instead." );
-					warnedTexture2D = true;
+		function get( scene, camera ) {
 
-				}
+			var renderState;
 
-				texture = texture.texture;
+			if ( renderStates.has( scene ) === false ) {
 
-			}
+				renderState = new WebGLRenderState();
+				renderStates.set( scene, new WeakMap() );
+				renderStates.get( scene ).set( camera, renderState );
 
-			setTexture2D( texture, slot );
+				scene.addEventListener( 'dispose', onSceneDispose );
 
-		}
+			} else {
 
-		function safeSetTextureCube( texture, slot ) {
+				if ( renderStates.get( scene ).has( camera ) === false ) {
 
-			if ( texture && texture.isWebGLCubeRenderTarget ) {
+					renderState = new WebGLRenderState();
+					renderStates.get( scene ).set( camera, renderState );
 
-				if ( warnedTextureCube === false ) {
+				} else {
 
-					console.warn( "THREE.WebGLTextures.safeSetTextureCube: don't use cube render targets as textures. Use their .texture property instead." );
-					warnedTextureCube = true;
+					renderState = renderStates.get( scene ).get( camera );
 
 				}
 
-				texture = texture.texture;
-
 			}
 
-			// currently relying on the fact that WebGLCubeRenderTarget.texture is a Texture and NOT a CubeTexture
-			// TODO: unify these code paths
-			if ( ( texture && texture.isCubeTexture ) ||
-				( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
-
-				// CompressedTexture can have Array in image :/
-
-				// this function alone should take care of cube textures
-				setTextureCube( texture, slot );
-
-			} else {
-
-				// assumed: texture property of THREE.WebGLCubeRenderTarget
-				setTextureCubeDynamic( texture, slot );
-
-			}
+			return renderState;
 
 		}
 
-		//
+		function dispose() {
 
-		this.allocateTextureUnit = allocateTextureUnit;
-		this.resetTextureUnits = resetTextureUnits;
+			renderStates = new WeakMap();
 
-		this.setTexture2D = setTexture2D;
-		this.setTexture2DArray = setTexture2DArray;
-		this.setTexture3D = setTexture3D;
-		this.setTextureCube = setTextureCube;
-		this.setTextureCubeDynamic = setTextureCubeDynamic;
-		this.setupRenderTarget = setupRenderTarget;
-		this.updateRenderTargetMipmap = updateRenderTargetMipmap;
-		this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
+		}
 
-		this.safeSetTexture2D = safeSetTexture2D;
-		this.safeSetTextureCube = safeSetTextureCube;
+		return {
+			get: get,
+			dispose: dispose
+		};
 
 	}
 
 	/**
-	 * @author thespite / http://www.twitter.com/thespite
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author bhouston / https://clara.io
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * parameters = {
+	 *
+	 *  opacity: <float>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>
+	 * }
 	 */
 
-	function WebGLUtils( gl, extensions, capabilities ) {
-
-		var isWebGL2 = capabilities.isWebGL2;
+	function MeshDepthMaterial( parameters ) {
 
-		function convert( p ) {
+		Material.call( this );
 
-			var extension;
+		this.type = 'MeshDepthMaterial';
 
-			if ( p === UnsignedByteType ) { return 5121; }
-			if ( p === UnsignedShort4444Type ) { return 32819; }
-			if ( p === UnsignedShort5551Type ) { return 32820; }
-			if ( p === UnsignedShort565Type ) { return 33635; }
+		this.depthPacking = BasicDepthPacking;
 
-			if ( p === ByteType ) { return 5120; }
-			if ( p === ShortType ) { return 5122; }
-			if ( p === UnsignedShortType ) { return 5123; }
-			if ( p === IntType ) { return 5124; }
-			if ( p === UnsignedIntType ) { return 5125; }
-			if ( p === FloatType ) { return 5126; }
+		this.skinning = false;
+		this.morphTargets = false;
 
-			if ( p === HalfFloatType ) {
+		this.map = null;
 
-				if ( isWebGL2 ) { return 5131; }
+		this.alphaMap = null;
 
-				extension = extensions.get( 'OES_texture_half_float' );
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-				if ( extension !== null ) {
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
 
-					return extension.HALF_FLOAT_OES;
+		this.fog = false;
 
-				} else {
+		this.setValues( parameters );
 
-					return null;
+	}
 
-				}
+	MeshDepthMaterial.prototype = Object.create( Material.prototype );
+	MeshDepthMaterial.prototype.constructor = MeshDepthMaterial;
 
-			}
+	MeshDepthMaterial.prototype.isMeshDepthMaterial = true;
 
-			if ( p === AlphaFormat ) { return 6406; }
-			if ( p === RGBFormat ) { return 6407; }
-			if ( p === RGBAFormat ) { return 6408; }
-			if ( p === LuminanceFormat ) { return 6409; }
-			if ( p === LuminanceAlphaFormat ) { return 6410; }
-			if ( p === DepthFormat ) { return 6402; }
-			if ( p === DepthStencilFormat ) { return 34041; }
-			if ( p === RedFormat ) { return 6403; }
+	MeshDepthMaterial.prototype.copy = function ( source ) {
 
-			// WebGL2 formats.
+		Material.prototype.copy.call( this, source );
 
-			if ( p === RedIntegerFormat ) { return 36244; }
-			if ( p === RGFormat ) { return 33319; }
-			if ( p === RGIntegerFormat ) { return 33320; }
-			if ( p === RGBIntegerFormat ) { return 36248; }
-			if ( p === RGBAIntegerFormat ) { return 36249; }
+		this.depthPacking = source.depthPacking;
 
-			if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format ||
-				p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) {
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
 
-				extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
+		this.map = source.map;
 
-				if ( extension !== null ) {
+		this.alphaMap = source.alphaMap;
 
-					if ( p === RGB_S3TC_DXT1_Format ) { return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; }
-					if ( p === RGBA_S3TC_DXT1_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; }
-					if ( p === RGBA_S3TC_DXT3_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; }
-					if ( p === RGBA_S3TC_DXT5_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; }
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-				} else {
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
 
-					return null;
+		return this;
 
-				}
+	};
 
-			}
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * parameters = {
+	 *
+	 *  referencePosition: <float>,
+	 *  nearDistance: <float>,
+	 *  farDistance: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>
+	 *
+	 * }
+	 */
 
-			if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format ||
-				p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) {
+	function MeshDistanceMaterial( parameters ) {
 
-				extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
+		Material.call( this );
 
-				if ( extension !== null ) {
+		this.type = 'MeshDistanceMaterial';
 
-					if ( p === RGB_PVRTC_4BPPV1_Format ) { return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; }
-					if ( p === RGB_PVRTC_2BPPV1_Format ) { return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; }
-					if ( p === RGBA_PVRTC_4BPPV1_Format ) { return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; }
-					if ( p === RGBA_PVRTC_2BPPV1_Format ) { return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; }
+		this.referencePosition = new Vector3();
+		this.nearDistance = 1;
+		this.farDistance = 1000;
 
-				} else {
+		this.skinning = false;
+		this.morphTargets = false;
 
-					return null;
+		this.map = null;
 
-				}
+		this.alphaMap = null;
 
-			}
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-			if ( p === RGB_ETC1_Format ) {
+		this.fog = false;
 
-				extension = extensions.get( 'WEBGL_compressed_texture_etc1' );
+		this.setValues( parameters );
 
-				if ( extension !== null ) {
+	}
 
-					return extension.COMPRESSED_RGB_ETC1_WEBGL;
+	MeshDistanceMaterial.prototype = Object.create( Material.prototype );
+	MeshDistanceMaterial.prototype.constructor = MeshDistanceMaterial;
 
-				} else {
+	MeshDistanceMaterial.prototype.isMeshDistanceMaterial = true;
 
-					return null;
+	MeshDistanceMaterial.prototype.copy = function ( source ) {
 
-				}
+		Material.prototype.copy.call( this, source );
 
-			}
+		this.referencePosition.copy( source.referencePosition );
+		this.nearDistance = source.nearDistance;
+		this.farDistance = source.farDistance;
 
-			if ( p === RGB_ETC2_Format || p === RGBA_ETC2_EAC_Format ) {
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
 
-				extension = extensions.get( 'WEBGL_compressed_texture_etc' );
+		this.map = source.map;
 
-				if ( extension !== null ) {
+		this.alphaMap = source.alphaMap;
 
-					if ( p === RGB_ETC2_Format ) { return extension.COMPRESSED_RGB8_ETC2; }
-					if ( p === RGBA_ETC2_EAC_Format ) { return extension.COMPRESSED_RGBA8_ETC2_EAC; }
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-				}
+		return this;
 
-			}
+	};
 
-			if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format ||
-				p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format ||
-				p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format ||
-				p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format ||
-				p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format ||
-				p === SRGB8_ALPHA8_ASTC_4x4_Format || p === SRGB8_ALPHA8_ASTC_5x4_Format || p === SRGB8_ALPHA8_ASTC_5x5_Format ||
-				p === SRGB8_ALPHA8_ASTC_6x5_Format || p === SRGB8_ALPHA8_ASTC_6x6_Format || p === SRGB8_ALPHA8_ASTC_8x5_Format ||
-				p === SRGB8_ALPHA8_ASTC_8x6_Format || p === SRGB8_ALPHA8_ASTC_8x8_Format || p === SRGB8_ALPHA8_ASTC_10x5_Format ||
-				p === SRGB8_ALPHA8_ASTC_10x6_Format || p === SRGB8_ALPHA8_ASTC_10x8_Format || p === SRGB8_ALPHA8_ASTC_10x10_Format ||
-				p === SRGB8_ALPHA8_ASTC_12x10_Format || p === SRGB8_ALPHA8_ASTC_12x12_Format ) {
+	var vsm_frag = "uniform sampler2D shadow_pass;\nuniform vec2 resolution;\nuniform float radius;\n#include <packing>\nvoid main() {\n  float mean = 0.0;\n  float squared_mean = 0.0;\n\tfloat depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy  ) / resolution ) );\n  for ( float i = -1.0; i < 1.0 ; i += SAMPLE_RATE) {\n    #ifdef HORIZONAL_PASS\n      vec2 distribution = unpackRGBATo2Half( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( i, 0.0 ) * radius ) / resolution ) );\n      mean += distribution.x;\n      squared_mean += distribution.y * distribution.y + distribution.x * distribution.x;\n    #else\n      float depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0,  i )  * radius ) / resolution ) );\n      mean += depth;\n      squared_mean += depth * depth;\n    #endif\n  }\n  mean = mean * HALF_SAMPLE_RATE;\n  squared_mean = squared_mean * HALF_SAMPLE_RATE;\n  float std_dev = sqrt( squared_mean - mean * mean );\n  gl_FragColor = pack2HalfToRGBA( vec2( mean, std_dev ) );\n}";
 
-				extension = extensions.get( 'WEBGL_compressed_texture_astc' );
+	var vsm_vert = "void main() {\n\tgl_Position = vec4( position, 1.0 );\n}";
 
-				if ( extension !== null ) {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-					// TODO Complete?
+	function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
 
-					return p;
+		var _frustum = new Frustum(),
 
-				} else {
+			_shadowMapSize = new Vector2(),
+			_viewportSize = new Vector2(),
 
-					return null;
+			_viewport = new Vector4(),
 
-				}
+			_depthMaterials = [],
+			_distanceMaterials = [],
 
-			}
+			_materialCache = {};
 
-			if ( p === RGBA_BPTC_Format ) {
+		var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
 
-				extension = extensions.get( 'EXT_texture_compression_bptc' );
+		var shadowMaterialVertical = new ShaderMaterial( {
 
-				if ( extension !== null ) {
+			defines: {
+				SAMPLE_RATE: 2.0 / 8.0,
+				HALF_SAMPLE_RATE: 1.0 / 8.0
+			},
 
-					// TODO Complete?
+			uniforms: {
+				shadow_pass: { value: null },
+				resolution: { value: new Vector2() },
+				radius: { value: 4.0 }
+			},
 
-					return p;
+			vertexShader: vsm_vert,
 
-				} else {
+			fragmentShader: vsm_frag
 
-					return null;
+		} );
 
-				}
+		var shadowMaterialHorizonal = shadowMaterialVertical.clone();
+		shadowMaterialHorizonal.defines.HORIZONAL_PASS = 1;
 
-			}
+		var fullScreenTri = new BufferGeometry();
+		fullScreenTri.setAttribute(
+			"position",
+			new BufferAttribute(
+				new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ),
+				3
+			)
+		);
 
-			if ( p === UnsignedInt248Type ) {
+		var fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical );
 
-				if ( isWebGL2 ) { return 34042; }
+		var scope = this;
 
-				extension = extensions.get( 'WEBGL_depth_texture' );
+		this.enabled = false;
 
-				if ( extension !== null ) {
+		this.autoUpdate = true;
+		this.needsUpdate = false;
 
-					return extension.UNSIGNED_INT_24_8_WEBGL;
+		this.type = PCFShadowMap;
 
-				} else {
+		this.render = function ( lights, scene, camera ) {
 
-					return null;
+			if ( scope.enabled === false ) { return; }
+			if ( scope.autoUpdate === false && scope.needsUpdate === false ) { return; }
 
-				}
+			if ( lights.length === 0 ) { return; }
 
-			}
+			var currentRenderTarget = _renderer.getRenderTarget();
+			var activeCubeFace = _renderer.getActiveCubeFace();
+			var activeMipmapLevel = _renderer.getActiveMipmapLevel();
 
-		}
+			var _state = _renderer.state;
 
-		return { convert: convert };
+			// Set GL state for depth map.
+			_state.setBlending( NoBlending );
+			_state.buffers.color.setClear( 1, 1, 1, 1 );
+			_state.buffers.depth.setTest( true );
+			_state.setScissorTest( false );
 
-	}
+			// render depth map
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			for ( var i = 0, il = lights.length; i < il; i ++ ) {
 
-	function ArrayCamera( array ) {
+				var light = lights[ i ];
+				var shadow = light.shadow;
 
-		PerspectiveCamera.call( this );
+				if ( shadow === undefined ) {
 
-		this.cameras = array || [];
+					console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' );
+					continue;
 
-	}
+				}
 
-	ArrayCamera.prototype = Object.assign( Object.create( PerspectiveCamera.prototype ), {
+				_shadowMapSize.copy( shadow.mapSize );
 
-		constructor: ArrayCamera,
+				var shadowFrameExtents = shadow.getFrameExtents();
 
-		isArrayCamera: true
+				_shadowMapSize.multiply( shadowFrameExtents );
 
-	} );
+				_viewportSize.copy( shadow.mapSize );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				if ( _shadowMapSize.x > maxTextureSize || _shadowMapSize.y > maxTextureSize ) {
 
-	function Group() {
+					if ( _shadowMapSize.x > maxTextureSize ) {
 
-		Object3D.call( this );
+						_viewportSize.x = Math.floor( maxTextureSize / shadowFrameExtents.x );
+						_shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x;
+						shadow.mapSize.x = _viewportSize.x;
 
-		this.type = 'Group';
+					}
 
-	}
+					if ( _shadowMapSize.y > maxTextureSize ) {
 
-	Group.prototype = Object.assign( Object.create( Object3D.prototype ), {
+						_viewportSize.y = Math.floor( maxTextureSize / shadowFrameExtents.y );
+						_shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y;
+						shadow.mapSize.y = _viewportSize.y;
 
-		constructor: Group,
+					}
 
-		isGroup: true
+				}
 
-	} );
+				if ( shadow.map === null && ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
 
-	/**
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+					var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat };
 
-	function WebXRController() {
+					shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
+					shadow.map.texture.name = light.name + ".shadowMap";
 
-		this._targetRay = null;
-		this._grip = null;
+					shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
 
-	}
+					shadow.camera.updateProjectionMatrix();
 
-	Object.assign( WebXRController.prototype, {
+				}
 
-		constructor: WebXRController,
+				if ( shadow.map === null ) {
 
-		getTargetRaySpace: function () {
+					var pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat };
 
-			if ( this._targetRay === null ) {
+					shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
+					shadow.map.texture.name = light.name + ".shadowMap";
 
-				this._targetRay = new Group();
-				this._targetRay.matrixAutoUpdate = false;
-				this._targetRay.visible = false;
+					shadow.camera.updateProjectionMatrix();
 
-			}
+				}
 
-			return this._targetRay;
+				_renderer.setRenderTarget( shadow.map );
+				_renderer.clear();
 
-		},
+				var viewportCount = shadow.getViewportCount();
 
-		getGripSpace: function () {
+				for ( var vp = 0; vp < viewportCount; vp ++ ) {
 
-			if ( this._grip === null ) {
+					var viewport = shadow.getViewport( vp );
 
-				this._grip = new Group();
-				this._grip.matrixAutoUpdate = false;
-				this._grip.visible = false;
+					_viewport.set(
+						_viewportSize.x * viewport.x,
+						_viewportSize.y * viewport.y,
+						_viewportSize.x * viewport.z,
+						_viewportSize.y * viewport.w
+					);
 
-			}
+					_state.viewport( _viewport );
 
-			return this._grip;
+					shadow.updateMatrices( light, vp );
 
-		},
+					_frustum = shadow.getFrustum();
 
-		dispatchEvent: function ( event ) {
+					renderObject( scene, camera, shadow.camera, light, this.type );
 
-			if ( this._targetRay !== null ) {
+				}
 
-				this._targetRay.dispatchEvent( event );
+				// do blur pass for VSM
 
-			}
+				if ( ! shadow.isPointLightShadow && this.type === VSMShadowMap ) {
 
-			if ( this._grip !== null ) {
+					VSMPass( shadow, camera );
 
-				this._grip.dispatchEvent( event );
+				}
 
 			}
 
-			return this;
+			scope.needsUpdate = false;
 
-		},
+			_renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel );
 
-		disconnect: function ( inputSource ) {
+		};
 
-			this.dispatchEvent( { type: 'disconnected', data: inputSource } );
+		function VSMPass( shadow, camera ) {
 
-			if ( this._targetRay !== null ) {
+			var geometry = _objects.update( fullScreenMesh );
 
-				this._targetRay.visible = false;
+			// vertical pass
 
-			}
+			shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
+			shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
+			shadowMaterialVertical.uniforms.radius.value = shadow.radius;
+			_renderer.setRenderTarget( shadow.mapPass );
+			_renderer.clear();
+			_renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null );
 
-			if ( this._grip !== null ) {
+			// horizonal pass
 
-				this._grip.visible = false;
+			shadowMaterialHorizonal.uniforms.shadow_pass.value = shadow.mapPass.texture;
+			shadowMaterialHorizonal.uniforms.resolution.value = shadow.mapSize;
+			shadowMaterialHorizonal.uniforms.radius.value = shadow.radius;
+			_renderer.setRenderTarget( shadow.map );
+			_renderer.clear();
+			_renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizonal, fullScreenMesh, null );
 
-			}
+		}
 
-			return this;
+		function getDepthMaterialVariant( useMorphing, useSkinning, useInstancing ) {
 
-		},
+			var index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2;
 
-		update: function ( inputSource, frame, referenceSpace ) {
+			var material = _depthMaterials[ index ];
 
-			var inputPose = null;
-			var gripPose = null;
+			if ( material === undefined ) {
 
-			var targetRay = this._targetRay;
-			var grip = this._grip;
+				material = new MeshDepthMaterial( {
 
-			if ( inputSource ) {
+					depthPacking: RGBADepthPacking,
 
-				if ( targetRay !== null ) {
+					morphTargets: useMorphing,
+					skinning: useSkinning
 
-					inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
+				} );
 
-					if ( inputPose !== null ) {
+				_depthMaterials[ index ] = material;
 
-						targetRay.matrix.fromArray( inputPose.transform.matrix );
-						targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
+			}
 
-					}
+			return material;
 
-				}
-
-				if ( grip !== null && inputSource.gripSpace ) {
-
-					gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
-
-					if ( gripPose !== null ) {
-
-						grip.matrix.fromArray( gripPose.transform.matrix );
-						grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
+		}
 
-					}
+		function getDistanceMaterialVariant( useMorphing, useSkinning, useInstancing ) {
 
-				}
+			var index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2;
 
-			}
+			var material = _distanceMaterials[ index ];
 
-			if ( targetRay !== null ) {
+			if ( material === undefined ) {
 
-				targetRay.visible = ( inputPose !== null );
+				material = new MeshDistanceMaterial( {
 
-			}
+					morphTargets: useMorphing,
+					skinning: useSkinning
 
-			if ( grip !== null ) {
+				} );
 
-				grip.visible = ( gripPose !== null );
+				_distanceMaterials[ index ] = material;
 
 			}
 
-			return this;
+			return material;
 
 		}
 
-	} );
+		function getDepthMaterial( object, geometry, material, light, shadowCameraNear, shadowCameraFar, type ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			var result = null;
 
-	function WebXRManager( renderer, gl ) {
+			var getMaterialVariant = getDepthMaterialVariant;
+			var customMaterial = object.customDepthMaterial;
 
-		var scope = this;
+			if ( light.isPointLight === true ) {
 
-		var session = null;
+				getMaterialVariant = getDistanceMaterialVariant;
+				customMaterial = object.customDistanceMaterial;
 
-		var framebufferScaleFactor = 1.0;
+			}
 
-		var referenceSpace = null;
-		var referenceSpaceType = 'local-floor';
+			if ( customMaterial === undefined ) {
 
-		var pose = null;
+				var useMorphing = false;
 
-		var controllers = [];
-		var inputSourcesMap = new Map();
+				if ( material.morphTargets === true ) {
 
-		//
+					useMorphing = geometry.morphAttributes && geometry.morphAttributes.position && geometry.morphAttributes.position.length > 0;
 
-		var cameraL = new PerspectiveCamera();
-		cameraL.layers.enable( 1 );
-		cameraL.viewport = new Vector4();
+				}
 
-		var cameraR = new PerspectiveCamera();
-		cameraR.layers.enable( 2 );
-		cameraR.viewport = new Vector4();
+				var useSkinning = false;
 
-		var cameras = [ cameraL, cameraR ];
+				if ( object.isSkinnedMesh === true ) {
 
-		var cameraVR = new ArrayCamera();
-		cameraVR.layers.enable( 1 );
-		cameraVR.layers.enable( 2 );
+					if ( material.skinning === true ) {
 
-		var _currentDepthNear = null;
-		var _currentDepthFar = null;
+						useSkinning = true;
 
-		//
+					} else {
 
-		this.enabled = false;
+						console.warn( 'THREE.WebGLShadowMap: THREE.SkinnedMesh with material.skinning set to false:', object );
 
-		this.isPresenting = false;
+					}
 
-		this.getController = function ( index ) {
+				}
 
-			var controller = controllers[ index ];
+				var useInstancing = object.isInstancedMesh === true;
 
-			if ( controller === undefined ) {
+				result = getMaterialVariant( useMorphing, useSkinning, useInstancing );
 
-				controller = new WebXRController();
-				controllers[ index ] = controller;
+			} else {
+
+				result = customMaterial;
 
 			}
 
-			return controller.getTargetRaySpace();
+			if ( _renderer.localClippingEnabled &&
+					material.clipShadows === true &&
+					material.clippingPlanes.length !== 0 ) {
 
-		};
+				// in this case we need a unique material instance reflecting the
+				// appropriate state
 
-		this.getControllerGrip = function ( index ) {
+				var keyA = result.uuid, keyB = material.uuid;
 
-			var controller = controllers[ index ];
+				var materialsForVariant = _materialCache[ keyA ];
 
-			if ( controller === undefined ) {
+				if ( materialsForVariant === undefined ) {
 
-				controller = new WebXRController();
-				controllers[ index ] = controller;
+					materialsForVariant = {};
+					_materialCache[ keyA ] = materialsForVariant;
 
-			}
+				}
 
-			return controller.getGripSpace();
+				var cachedMaterial = materialsForVariant[ keyB ];
 
-		};
+				if ( cachedMaterial === undefined ) {
 
-		//
+					cachedMaterial = result.clone();
+					materialsForVariant[ keyB ] = cachedMaterial;
 
-		function onSessionEvent( event ) {
+				}
 
-			var controller = inputSourcesMap.get( event.inputSource );
+				result = cachedMaterial;
 
-			if ( controller ) {
+			}
 
-				controller.dispatchEvent( { type: event.type } );
+			result.visible = material.visible;
+			result.wireframe = material.wireframe;
 
-			}
+			if ( type === VSMShadowMap ) {
 
-		}
+				result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side;
 
-		function onSessionEnd() {
+			} else {
 
-			inputSourcesMap.forEach( function ( controller, inputSource ) {
+				result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ];
 
-				controller.disconnect( inputSource );
+			}
 
-			} );
+			result.clipShadows = material.clipShadows;
+			result.clippingPlanes = material.clippingPlanes;
+			result.clipIntersection = material.clipIntersection;
 
-			inputSourcesMap.clear();
+			result.wireframeLinewidth = material.wireframeLinewidth;
+			result.linewidth = material.linewidth;
 
-			//
+			if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) {
 
-			renderer.setFramebuffer( null );
-			renderer.setRenderTarget( renderer.getRenderTarget() ); // Hack #15830
-			animation.stop();
+				result.referencePosition.setFromMatrixPosition( light.matrixWorld );
+				result.nearDistance = shadowCameraNear;
+				result.farDistance = shadowCameraFar;
 
-			scope.isPresenting = false;
+			}
 
-			scope.dispatchEvent( { type: 'sessionend' } );
+			return result;
 
 		}
 
-		function onRequestReferenceSpace( value ) {
+		function renderObject( object, camera, shadowCamera, light, type ) {
 
-			referenceSpace = value;
+			if ( object.visible === false ) { return; }
 
-			animation.setContext( session );
-			animation.start();
+			var visible = object.layers.test( camera.layers );
 
-			scope.isPresenting = true;
+			if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) {
 
-			scope.dispatchEvent( { type: 'sessionstart' } );
+				if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) {
 
-		}
+					object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
 
-		this.setFramebufferScaleFactor = function ( value ) {
+					var geometry = _objects.update( object );
+					var material = object.material;
 
-			framebufferScaleFactor = value;
+					if ( Array.isArray( material ) ) {
 
-			if ( scope.isPresenting === true ) {
+						var groups = geometry.groups;
 
-				console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting.' );
+						for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
 
-			}
+							var group = groups[ k ];
+							var groupMaterial = material[ group.materialIndex ];
 
-		};
+							if ( groupMaterial && groupMaterial.visible ) {
 
-		this.setReferenceSpaceType = function ( value ) {
+								var depthMaterial = getDepthMaterial( object, geometry, groupMaterial, light, shadowCamera.near, shadowCamera.far, type );
 
-			referenceSpaceType = value;
+								_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group );
 
-			if ( scope.isPresenting === true ) {
+							}
 
-				console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting.' );
+						}
 
-			}
+					} else if ( material.visible ) {
 
-		};
+						var depthMaterial = getDepthMaterial( object, geometry, material, light, shadowCamera.near, shadowCamera.far, type );
 
-		this.getReferenceSpace = function () {
+						_renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null );
 
-			return referenceSpace;
+					}
 
-		};
+				}
 
-		this.getSession = function () {
+			}
 
-			return session;
+			var children = object.children;
 
-		};
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-		this.setSession = function ( value ) {
+				renderObject( children[ i ], camera, shadowCamera, light, type );
 
-			session = value;
+			}
 
-			if ( session !== null ) {
+		}
 
-				session.addEventListener( 'select', onSessionEvent );
-				session.addEventListener( 'selectstart', onSessionEvent );
-				session.addEventListener( 'selectend', onSessionEvent );
-				session.addEventListener( 'squeeze', onSessionEvent );
-				session.addEventListener( 'squeezestart', onSessionEvent );
-				session.addEventListener( 'squeezeend', onSessionEvent );
-				session.addEventListener( 'end', onSessionEnd );
+	}
 
-				var attributes = gl.getContextAttributes();
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				var layerInit = {
-					antialias: attributes.antialias,
-					alpha: attributes.alpha,
-					depth: attributes.depth,
-					stencil: attributes.stencil,
-					framebufferScaleFactor: framebufferScaleFactor
-				};
+	function WebGLState( gl, extensions, capabilities ) {
 
-				// eslint-disable-next-line no-undef
-				var baseLayer = new XRWebGLLayer( session, gl, layerInit );
+		var isWebGL2 = capabilities.isWebGL2;
 
-				session.updateRenderState( { baseLayer: baseLayer } );
+		function ColorBuffer() {
 
-				session.requestReferenceSpace( referenceSpaceType ).then( onRequestReferenceSpace );
+			var locked = false;
 
-				//
+			var color = new Vector4();
+			var currentColorMask = null;
+			var currentColorClear = new Vector4( 0, 0, 0, 0 );
 
-				session.addEventListener( 'inputsourceschange', updateInputSources );
+			return {
 
-			}
+				setMask: function ( colorMask ) {
 
-		};
+					if ( currentColorMask !== colorMask && ! locked ) {
 
-		function updateInputSources( event ) {
+						gl.colorMask( colorMask, colorMask, colorMask, colorMask );
+						currentColorMask = colorMask;
 
-			var inputSources = session.inputSources;
+					}
 
-			// Assign inputSources to available controllers
+				},
 
-			for ( var i = 0; i < controllers.length; i ++ ) {
+				setLocked: function ( lock ) {
 
-				inputSourcesMap.set( inputSources[ i ], controllers[ i ] );
+					locked = lock;
 
-			}
+				},
 
-			// Notify disconnected
+				setClear: function ( r, g, b, a, premultipliedAlpha ) {
 
-			for ( var i = 0; i < event.removed.length; i ++ ) {
+					if ( premultipliedAlpha === true ) {
 
-				var inputSource = event.removed[ i ];
-				var controller = inputSourcesMap.get( inputSource );
+						r *= a; g *= a; b *= a;
 
-				if ( controller ) {
+					}
 
-					controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
-					inputSourcesMap.delete( inputSource );
+					color.set( r, g, b, a );
 
-				}
+					if ( currentColorClear.equals( color ) === false ) {
 
-			}
+						gl.clearColor( r, g, b, a );
+						currentColorClear.copy( color );
 
-			// Notify connected
+					}
 
-			for ( var i = 0; i < event.added.length; i ++ ) {
+				},
 
-				var inputSource = event.added[ i ];
-				var controller = inputSourcesMap.get( inputSource );
+				reset: function () {
 
-				if ( controller ) {
+					locked = false;
 
-					controller.dispatchEvent( { type: 'connected', data: inputSource } );
+					currentColorMask = null;
+					currentColorClear.set( - 1, 0, 0, 0 ); // set to invalid state
 
 				}
 
-			}
+			};
 
 		}
 
-		//
+		function DepthBuffer() {
 
-		var cameraLPos = new Vector3();
-		var cameraRPos = new Vector3();
+			var locked = false;
 
-		/**
-		 * @author jsantell / https://www.jsantell.com/
-		 *
-		 * Assumes 2 cameras that are parallel and share an X-axis, and that
-		 * the cameras' projection and world matrices have already been set.
-		 * And that near and far planes are identical for both cameras.
-		 * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
-		 */
-		function setProjectionFromUnion( camera, cameraL, cameraR ) {
+			var currentDepthMask = null;
+			var currentDepthFunc = null;
+			var currentDepthClear = null;
 
-			cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
-			cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
+			return {
 
-			var ipd = cameraLPos.distanceTo( cameraRPos );
+				setTest: function ( depthTest ) {
 
-			var projL = cameraL.projectionMatrix.elements;
-			var projR = cameraR.projectionMatrix.elements;
+					if ( depthTest ) {
 
-			// VR systems will have identical far and near planes, and
-			// most likely identical top and bottom frustum extents.
-			// Use the left camera for these values.
-			var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
-			var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
-			var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
-			var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
+						enable( 2929 );
 
-			var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
-			var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
-			var left = near * leftFov;
-			var right = near * rightFov;
+					} else {
 
-			// Calculate the new camera's position offset from the
-			// left camera. xOffset should be roughly half `ipd`.
-			var zOffset = ipd / ( - leftFov + rightFov );
-			var xOffset = zOffset * - leftFov;
+						disable( 2929 );
 
-			// TODO: Better way to apply this offset?
-			cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
-			camera.translateX( xOffset );
-			camera.translateZ( zOffset );
-			camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
-			camera.matrixWorldInverse.getInverse( camera.matrixWorld );
+					}
 
-			// Find the union of the frustum values of the cameras and scale
-			// the values so that the near plane's position does not change in world space,
-			// although must now be relative to the new union camera.
-			var near2 = near + zOffset;
-			var far2 = far + zOffset;
-			var left2 = left - xOffset;
-			var right2 = right + ( ipd - xOffset );
-			var top2 = topFov * far / far2 * near2;
-			var bottom2 = bottomFov * far / far2 * near2;
+				},
 
-			camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
+				setMask: function ( depthMask ) {
 
-		}
+					if ( currentDepthMask !== depthMask && ! locked ) {
 
-		function updateCamera( camera, parent ) {
+						gl.depthMask( depthMask );
+						currentDepthMask = depthMask;
 
-			if ( parent === null ) {
+					}
 
-				camera.matrixWorld.copy( camera.matrix );
+				},
 
-			} else {
+				setFunc: function ( depthFunc ) {
 
-				camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
+					if ( currentDepthFunc !== depthFunc ) {
 
-			}
+						if ( depthFunc ) {
 
-			camera.matrixWorldInverse.getInverse( camera.matrixWorld );
+							switch ( depthFunc ) {
 
-		}
+								case NeverDepth:
 
-		this.getCamera = function ( camera ) {
+									gl.depthFunc( 512 );
+									break;
 
-			cameraVR.near = cameraR.near = cameraL.near = camera.near;
-			cameraVR.far = cameraR.far = cameraL.far = camera.far;
+								case AlwaysDepth:
 
-			if ( _currentDepthNear !== cameraVR.near || _currentDepthFar !== cameraVR.far ) {
+									gl.depthFunc( 519 );
+									break;
 
-				// Note that the new renderState won't apply until the next frame. See #18320
+								case LessDepth:
 
-				session.updateRenderState( {
-					depthNear: cameraVR.near,
-					depthFar: cameraVR.far
-				} );
+									gl.depthFunc( 513 );
+									break;
 
-				_currentDepthNear = cameraVR.near;
-				_currentDepthFar = cameraVR.far;
+								case LessEqualDepth:
 
-			}
+									gl.depthFunc( 515 );
+									break;
 
-			var parent = camera.parent;
-			var cameras = cameraVR.cameras;
+								case EqualDepth:
 
-			updateCamera( cameraVR, parent );
+									gl.depthFunc( 514 );
+									break;
 
-			for ( var i = 0; i < cameras.length; i ++ ) {
+								case GreaterEqualDepth:
 
-				updateCamera( cameras[ i ], parent );
+									gl.depthFunc( 518 );
+									break;
 
-			}
+								case GreaterDepth:
 
-			// update camera and its children
+									gl.depthFunc( 516 );
+									break;
 
-			camera.matrixWorld.copy( cameraVR.matrixWorld );
+								case NotEqualDepth:
 
-			var children = camera.children;
+									gl.depthFunc( 517 );
+									break;
 
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+								default:
 
-				children[ i ].updateMatrixWorld( true );
+									gl.depthFunc( 515 );
 
-			}
+							}
 
-			// update projection matrix for proper view frustum culling
+						} else {
 
-			if ( cameras.length === 2 ) {
+							gl.depthFunc( 515 );
 
-				setProjectionFromUnion( cameraVR, cameraL, cameraR );
+						}
 
-			} else {
+						currentDepthFunc = depthFunc;
 
-				// assume single camera setup (AR)
+					}
 
-				cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
+				},
 
-			}
+				setLocked: function ( lock ) {
 
-			return cameraVR;
+					locked = lock;
 
-		};
+				},
 
-		// Animation Loop
+				setClear: function ( depth ) {
 
-		var onAnimationFrameCallback = null;
+					if ( currentDepthClear !== depth ) {
 
-		function onAnimationFrame( time, frame ) {
+						gl.clearDepth( depth );
+						currentDepthClear = depth;
 
-			pose = frame.getViewerPose( referenceSpace );
+					}
 
-			if ( pose !== null ) {
+				},
 
-				var views = pose.views;
-				var baseLayer = session.renderState.baseLayer;
+				reset: function () {
 
-				renderer.setFramebuffer( baseLayer.framebuffer );
+					locked = false;
 
-				var cameraVRNeedsUpdate = false;
+					currentDepthMask = null;
+					currentDepthFunc = null;
+					currentDepthClear = null;
 
-				// check if it's necessary to rebuild cameraVR's camera list
+				}
 
-				if ( views.length !== cameraVR.cameras.length ) {
+			};
 
-					cameraVR.cameras.length = 0;
-					cameraVRNeedsUpdate = true;
+		}
 
-				}
+		function StencilBuffer() {
 
-				for ( var i = 0; i < views.length; i ++ ) {
+			var locked = false;
 
-					var view = views[ i ];
-					var viewport = baseLayer.getViewport( view );
+			var currentStencilMask = null;
+			var currentStencilFunc = null;
+			var currentStencilRef = null;
+			var currentStencilFuncMask = null;
+			var currentStencilFail = null;
+			var currentStencilZFail = null;
+			var currentStencilZPass = null;
+			var currentStencilClear = null;
 
-					var camera = cameras[ i ];
-					camera.matrix.fromArray( view.transform.matrix );
-					camera.projectionMatrix.fromArray( view.projectionMatrix );
-					camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
+			return {
 
-					if ( i === 0 ) {
+				setTest: function ( stencilTest ) {
 
-						cameraVR.matrix.copy( camera.matrix );
+					if ( ! locked ) {
 
-					}
+						if ( stencilTest ) {
 
-					if ( cameraVRNeedsUpdate === true ) {
+							enable( 2960 );
 
-						cameraVR.cameras.push( camera );
+						} else {
+
+							disable( 2960 );
+
+						}
 
 					}
 
-				}
+				},
 
-			}
+				setMask: function ( stencilMask ) {
 
-			//
+					if ( currentStencilMask !== stencilMask && ! locked ) {
 
-			var inputSources = session.inputSources;
+						gl.stencilMask( stencilMask );
+						currentStencilMask = stencilMask;
 
-			for ( var i = 0; i < controllers.length; i ++ ) {
+					}
 
-				var controller = controllers[ i ];
-				var inputSource = inputSources[ i ];
-
-				controller.update( inputSource, frame, referenceSpace );
+				},
 
-			}
+				setFunc: function ( stencilFunc, stencilRef, stencilMask ) {
 
-			if ( onAnimationFrameCallback ) { onAnimationFrameCallback( time, frame ); }
+					if ( currentStencilFunc !== stencilFunc ||
+					     currentStencilRef 	!== stencilRef 	||
+					     currentStencilFuncMask !== stencilMask ) {
 
-		}
+						gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
 
-		var animation = new WebGLAnimation();
-		animation.setAnimationLoop( onAnimationFrame );
+						currentStencilFunc = stencilFunc;
+						currentStencilRef = stencilRef;
+						currentStencilFuncMask = stencilMask;
 
-		this.setAnimationLoop = function ( callback ) {
+					}
 
-			onAnimationFrameCallback = callback;
+				},
 
-		};
+				setOp: function ( stencilFail, stencilZFail, stencilZPass ) {
 
-		this.dispose = function () {};
+					if ( currentStencilFail	 !== stencilFail 	||
+					     currentStencilZFail !== stencilZFail ||
+					     currentStencilZPass !== stencilZPass ) {
 
-	}
+						gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
 
-	Object.assign( WebXRManager.prototype, EventDispatcher.prototype );
+						currentStencilFail = stencilFail;
+						currentStencilZFail = stencilZFail;
+						currentStencilZPass = stencilZPass;
 
-	/**
-	 * @author supereggbert / http://www.paulbrunt.co.uk/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author szimek / https://github.com/szimek/
-	 * @author tschw
-	 */
+					}
 
-	function WebGLRenderer( parameters ) {
+				},
 
-		parameters = parameters || {};
+				setLocked: function ( lock ) {
 
-		var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ),
-			_context = parameters.context !== undefined ? parameters.context : null,
+					locked = lock;
 
-			_alpha = parameters.alpha !== undefined ? parameters.alpha : false,
-			_depth = parameters.depth !== undefined ? parameters.depth : true,
-			_stencil = parameters.stencil !== undefined ? parameters.stencil : true,
-			_antialias = parameters.antialias !== undefined ? parameters.antialias : false,
-			_premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
-			_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
-			_powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default',
-			_failIfMajorPerformanceCaveat = parameters.failIfMajorPerformanceCaveat !== undefined ? parameters.failIfMajorPerformanceCaveat : false;
+				},
 
-		var currentRenderList = null;
-		var currentRenderState = null;
+				setClear: function ( stencil ) {
 
-		// public properties
+					if ( currentStencilClear !== stencil ) {
 
-		this.domElement = _canvas;
+						gl.clearStencil( stencil );
+						currentStencilClear = stencil;
 
-		// Debug configuration container
-		this.debug = {
+					}
 
-			/**
-			 * Enables error checking and reporting when shader programs are being compiled
-			 * @type {boolean}
-			 */
-			checkShaderErrors: true
-		};
+				},
 
-		// clearing
+				reset: function () {
 
-		this.autoClear = true;
-		this.autoClearColor = true;
-		this.autoClearDepth = true;
-		this.autoClearStencil = true;
+					locked = false;
 
-		// scene graph
+					currentStencilMask = null;
+					currentStencilFunc = null;
+					currentStencilRef = null;
+					currentStencilFuncMask = null;
+					currentStencilFail = null;
+					currentStencilZFail = null;
+					currentStencilZPass = null;
+					currentStencilClear = null;
 
-		this.sortObjects = true;
+				}
 
-		// user-defined clipping
+			};
 
-		this.clippingPlanes = [];
-		this.localClippingEnabled = false;
+		}
 
-		// physically based shading
+		//
 
-		this.gammaFactor = 2.0;	// for backwards compatibility
-		this.outputEncoding = LinearEncoding;
+		var colorBuffer = new ColorBuffer();
+		var depthBuffer = new DepthBuffer();
+		var stencilBuffer = new StencilBuffer();
 
-		// physical lights
+		var maxVertexAttributes = gl.getParameter( 34921 );
+		var newAttributes = new Uint8Array( maxVertexAttributes );
+		var enabledAttributes = new Uint8Array( maxVertexAttributes );
+		var attributeDivisors = new Uint8Array( maxVertexAttributes );
 
-		this.physicallyCorrectLights = false;
+		var enabledCapabilities = {};
 
-		// tone mapping
+		var currentProgram = null;
 
-		this.toneMapping = NoToneMapping;
-		this.toneMappingExposure = 1.0;
-		this.toneMappingWhitePoint = 1.0;
+		var currentBlendingEnabled = null;
+		var currentBlending = null;
+		var currentBlendEquation = null;
+		var currentBlendSrc = null;
+		var currentBlendDst = null;
+		var currentBlendEquationAlpha = null;
+		var currentBlendSrcAlpha = null;
+		var currentBlendDstAlpha = null;
+		var currentPremultipledAlpha = false;
 
-		// morphs
+		var currentFlipSided = null;
+		var currentCullFace = null;
 
-		this.maxMorphTargets = 8;
-		this.maxMorphNormals = 4;
+		var currentLineWidth = null;
 
-		// internal properties
+		var currentPolygonOffsetFactor = null;
+		var currentPolygonOffsetUnits = null;
 
-		var _this = this,
+		var maxTextures = gl.getParameter( 35661 );
 
-			_isContextLost = false,
+		var lineWidthAvailable = false;
+		var version = 0;
+		var glVersion = gl.getParameter( 7938 );
 
-			// internal state cache
+		if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) {
 
-			_framebuffer = null,
+			version = parseFloat( /^WebGL\ ([0-9])/.exec( glVersion )[ 1 ] );
+			lineWidthAvailable = ( version >= 1.0 );
 
-			_currentActiveCubeFace = 0,
-			_currentActiveMipmapLevel = 0,
-			_currentRenderTarget = null,
-			_currentFramebuffer = null,
-			_currentMaterialId = - 1,
+		} else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) {
 
-			// geometry and program caching
+			version = parseFloat( /^OpenGL\ ES\ ([0-9])/.exec( glVersion )[ 1 ] );
+			lineWidthAvailable = ( version >= 2.0 );
 
-			_currentGeometryProgram = {
-				geometry: null,
-				program: null,
-				wireframe: false
-			},
+		}
 
-			_currentCamera = null,
-			_currentArrayCamera = null,
+		var currentTextureSlot = null;
+		var currentBoundTextures = {};
 
-			_currentViewport = new Vector4(),
-			_currentScissor = new Vector4(),
-			_currentScissorTest = null,
+		var currentScissor = new Vector4();
+		var currentViewport = new Vector4();
 
-			//
+		function createTexture( type, target, count ) {
 
-			_width = _canvas.width,
-			_height = _canvas.height,
+			var data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
+			var texture = gl.createTexture();
 
-			_pixelRatio = 1,
-			_opaqueSort = null,
-			_transparentSort = null,
+			gl.bindTexture( type, texture );
+			gl.texParameteri( type, 10241, 9728 );
+			gl.texParameteri( type, 10240, 9728 );
 
-			_viewport = new Vector4( 0, 0, _width, _height ),
-			_scissor = new Vector4( 0, 0, _width, _height ),
-			_scissorTest = false,
+			for ( var i = 0; i < count; i ++ ) {
 
-			// frustum
+				gl.texImage2D( target + i, 0, 6408, 1, 1, 0, 6408, 5121, data );
 
-			_frustum = new Frustum(),
+			}
 
-			// clipping
+			return texture;
 
-			_clipping = new WebGLClipping(),
-			_clippingEnabled = false,
-			_localClippingEnabled = false,
+		}
 
-			// camera matrices cache
+		var emptyTextures = {};
+		emptyTextures[ 3553 ] = createTexture( 3553, 3553, 1 );
+		emptyTextures[ 34067 ] = createTexture( 34067, 34069, 6 );
 
-			_projScreenMatrix = new Matrix4(),
+		// init
 
-			_vector3 = new Vector3();
+		colorBuffer.setClear( 0, 0, 0, 1 );
+		depthBuffer.setClear( 1 );
+		stencilBuffer.setClear( 0 );
 
-		function getTargetPixelRatio() {
+		enable( 2929 );
+		depthBuffer.setFunc( LessEqualDepth );
 
-			return _currentRenderTarget === null ? _pixelRatio : 1;
+		setFlipSided( false );
+		setCullFace( CullFaceBack );
+		enable( 2884 );
 
-		}
+		setBlending( NoBlending );
 
-		// initialize
+		//
 
-		var _gl;
+		function initAttributes() {
 
-		try {
+			for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
 
-			var contextAttributes = {
-				alpha: _alpha,
-				depth: _depth,
-				stencil: _stencil,
-				antialias: _antialias,
-				premultipliedAlpha: _premultipliedAlpha,
-				preserveDrawingBuffer: _preserveDrawingBuffer,
-				powerPreference: _powerPreference,
-				failIfMajorPerformanceCaveat: _failIfMajorPerformanceCaveat,
-				xrCompatible: true
-			};
+				newAttributes[ i ] = 0;
 
-			// event listeners must be registered before WebGL context is created, see #12753
+			}
 
-			_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
-			_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
+		}
 
-			_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
+		function enableAttribute( attribute ) {
 
-			if ( _gl === null ) {
+			enableAttributeAndDivisor( attribute, 0 );
 
-				if ( _canvas.getContext( 'webgl' ) !== null ) {
+		}
 
-					throw new Error( 'Error creating WebGL context with your selected attributes.' );
+		function enableAttributeAndDivisor( attribute, meshPerAttribute ) {
 
-				} else {
+			newAttributes[ attribute ] = 1;
 
-					throw new Error( 'Error creating WebGL context.' );
+			if ( enabledAttributes[ attribute ] === 0 ) {
 
-				}
+				gl.enableVertexAttribArray( attribute );
+				enabledAttributes[ attribute ] = 1;
 
 			}
 
-			// Some experimental-webgl implementations do not have getShaderPrecisionFormat
-
-			if ( _gl.getShaderPrecisionFormat === undefined ) {
-
-				_gl.getShaderPrecisionFormat = function () {
+			if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {
 
-					return { 'rangeMin': 1, 'rangeMax': 1, 'precision': 1 };
+				var extension = isWebGL2 ? gl : extensions.get( 'ANGLE_instanced_arrays' );
 
-				};
+				extension[ isWebGL2 ? 'vertexAttribDivisor' : 'vertexAttribDivisorANGLE' ]( attribute, meshPerAttribute );
+				attributeDivisors[ attribute ] = meshPerAttribute;
 
 			}
 
-		} catch ( error ) {
-
-			console.error( 'THREE.WebGLRenderer: ' + error.message );
-			throw error;
-
 		}
 
-		var extensions, capabilities, state, info;
-		var properties, textures, attributes, geometries, objects;
-		var programCache, renderLists, renderStates;
-
-		var background, morphtargets, bufferRenderer, indexedBufferRenderer;
-
-		var utils;
-
-		function initGLContext() {
+		function disableUnusedAttributes() {
 
-			extensions = new WebGLExtensions( _gl );
+			for ( var i = 0, l = enabledAttributes.length; i !== l; ++ i ) {
 
-			capabilities = new WebGLCapabilities( _gl, extensions, parameters );
+				if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
 
-			if ( capabilities.isWebGL2 === false ) {
+					gl.disableVertexAttribArray( i );
+					enabledAttributes[ i ] = 0;
 
-				extensions.get( 'WEBGL_depth_texture' );
-				extensions.get( 'OES_texture_float' );
-				extensions.get( 'OES_texture_half_float' );
-				extensions.get( 'OES_texture_half_float_linear' );
-				extensions.get( 'OES_standard_derivatives' );
-				extensions.get( 'OES_element_index_uint' );
-				extensions.get( 'ANGLE_instanced_arrays' );
+				}
 
 			}
 
-			extensions.get( 'OES_texture_float_linear' );
-
-			utils = new WebGLUtils( _gl, extensions, capabilities );
+		}
 
-			state = new WebGLState( _gl, extensions, capabilities );
-			state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
-			state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
+		function vertexAttribPointer( index, size, type, normalized, stride, offset ) {
 
-			info = new WebGLInfo( _gl );
-			properties = new WebGLProperties();
-			textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
-			attributes = new WebGLAttributes( _gl, capabilities );
-			geometries = new WebGLGeometries( _gl, attributes, info );
-			objects = new WebGLObjects( _gl, geometries, attributes, info );
-			morphtargets = new WebGLMorphtargets( _gl );
-			programCache = new WebGLPrograms( _this, extensions, capabilities );
-			renderLists = new WebGLRenderLists();
-			renderStates = new WebGLRenderStates();
+			if ( isWebGL2 === true && ( type === 5124 || type === 5125 ) ) {
 
-			background = new WebGLBackground( _this, state, objects, _premultipliedAlpha );
+				gl.vertexAttribIPointer( index, size, type, normalized, stride, offset );
 
-			bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );
-			indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info, capabilities );
+			} else {
 
-			info.programs = programCache.programs;
+				gl.vertexAttribPointer( index, size, type, normalized, stride, offset );
 
-			_this.capabilities = capabilities;
-			_this.extensions = extensions;
-			_this.properties = properties;
-			_this.renderLists = renderLists;
-			_this.state = state;
-			_this.info = info;
+			}
 
 		}
 
-		initGLContext();
+		function enable( id ) {
 
-		// xr
+			if ( enabledCapabilities[ id ] !== true ) {
 
-		var xr = new WebXRManager( _this, _gl );
+				gl.enable( id );
+				enabledCapabilities[ id ] = true;
 
-		this.xr = xr;
+			}
 
-		// shadow map
+		}
 
-		var shadowMap = new WebGLShadowMap( _this, objects, capabilities.maxTextureSize );
+		function disable( id ) {
 
-		this.shadowMap = shadowMap;
+			if ( enabledCapabilities[ id ] !== false ) {
 
-		// API
+				gl.disable( id );
+				enabledCapabilities[ id ] = false;
 
-		this.getContext = function () {
+			}
 
-			return _gl;
+		}
 
-		};
+		function useProgram( program ) {
 
-		this.getContextAttributes = function () {
+			if ( currentProgram !== program ) {
 
-			return _gl.getContextAttributes();
+				gl.useProgram( program );
 
-		};
+				currentProgram = program;
 
-		this.forceContextLoss = function () {
+				return true;
 
-			var extension = extensions.get( 'WEBGL_lose_context' );
-			if ( extension ) { extension.loseContext(); }
+			}
 
-		};
+			return false;
 
-		this.forceContextRestore = function () {
+		}
 
-			var extension = extensions.get( 'WEBGL_lose_context' );
-			if ( extension ) { extension.restoreContext(); }
+		var equationToGL = {};
+		equationToGL[ AddEquation ] = 32774;
+		equationToGL[ SubtractEquation ] = 32778;
+		equationToGL[ ReverseSubtractEquation ] = 32779;
 
-		};
+		if ( isWebGL2 ) {
 
-		this.getPixelRatio = function () {
+			equationToGL[ MinEquation ] = 32775;
+			equationToGL[ MaxEquation ] = 32776;
 
-			return _pixelRatio;
+		} else {
 
-		};
+			var extension = extensions.get( 'EXT_blend_minmax' );
 
-		this.setPixelRatio = function ( value ) {
+			if ( extension !== null ) {
 
-			if ( value === undefined ) { return; }
+				equationToGL[ MinEquation ] = extension.MIN_EXT;
+				equationToGL[ MaxEquation ] = extension.MAX_EXT;
 
-			_pixelRatio = value;
+			}
 
-			this.setSize( _width, _height, false );
+		}
 
-		};
+		var factorToGL = {};
+		factorToGL[ ZeroFactor ] = 0;
+		factorToGL[ OneFactor ] = 1;
+		factorToGL[ SrcColorFactor ] = 768;
+		factorToGL[ SrcAlphaFactor ] = 770;
+		factorToGL[ SrcAlphaSaturateFactor ] = 776;
+		factorToGL[ DstColorFactor ] = 774;
+		factorToGL[ DstAlphaFactor ] = 772;
+		factorToGL[ OneMinusSrcColorFactor ] = 769;
+		factorToGL[ OneMinusSrcAlphaFactor ] = 771;
+		factorToGL[ OneMinusDstColorFactor ] = 775;
+		factorToGL[ OneMinusDstAlphaFactor ] = 773;
 
-		this.getSize = function ( target ) {
+		function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
 
-			if ( target === undefined ) {
+			if ( blending === NoBlending ) {
 
-				console.warn( 'WebGLRenderer: .getsize() now requires a Vector2 as an argument' );
+				if ( currentBlendingEnabled ) {
 
-				target = new Vector2();
+					disable( 3042 );
+					currentBlendingEnabled = false;
+
+				}
+
+				return;
 
 			}
 
-			return target.set( _width, _height );
+			if ( ! currentBlendingEnabled ) {
 
-		};
+				enable( 3042 );
+				currentBlendingEnabled = true;
 
-		this.setSize = function ( width, height, updateStyle ) {
+			}
 
-			if ( xr.isPresenting ) {
+			if ( blending !== CustomBlending ) {
 
-				console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' );
-				return;
+				if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
 
-			}
+					if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {
 
-			_width = width;
-			_height = height;
+						gl.blendEquation( 32774 );
 
-			_canvas.width = Math.floor( width * _pixelRatio );
-			_canvas.height = Math.floor( height * _pixelRatio );
+						currentBlendEquation = AddEquation;
+						currentBlendEquationAlpha = AddEquation;
 
-			if ( updateStyle !== false ) {
+					}
 
-				_canvas.style.width = width + 'px';
-				_canvas.style.height = height + 'px';
+					if ( premultipliedAlpha ) {
 
-			}
+						switch ( blending ) {
 
-			this.setViewport( 0, 0, width, height );
+							case NormalBlending:
+								gl.blendFuncSeparate( 1, 771, 1, 771 );
+								break;
 
-		};
+							case AdditiveBlending:
+								gl.blendFunc( 1, 1 );
+								break;
 
-		this.getDrawingBufferSize = function ( target ) {
+							case SubtractiveBlending:
+								gl.blendFuncSeparate( 0, 0, 769, 771 );
+								break;
 
-			if ( target === undefined ) {
+							case MultiplyBlending:
+								gl.blendFuncSeparate( 0, 768, 0, 770 );
+								break;
 
-				console.warn( 'WebGLRenderer: .getdrawingBufferSize() now requires a Vector2 as an argument' );
+							default:
+								console.error( 'THREE.WebGLState: Invalid blending: ', blending );
+								break;
 
-				target = new Vector2();
+						}
 
-			}
+					} else {
 
-			return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor();
+						switch ( blending ) {
 
-		};
+							case NormalBlending:
+								gl.blendFuncSeparate( 770, 771, 1, 771 );
+								break;
 
-		this.setDrawingBufferSize = function ( width, height, pixelRatio ) {
+							case AdditiveBlending:
+								gl.blendFunc( 770, 1 );
+								break;
 
-			_width = width;
-			_height = height;
+							case SubtractiveBlending:
+								gl.blendFunc( 0, 769 );
+								break;
 
-			_pixelRatio = pixelRatio;
+							case MultiplyBlending:
+								gl.blendFunc( 0, 768 );
+								break;
 
-			_canvas.width = Math.floor( width * pixelRatio );
-			_canvas.height = Math.floor( height * pixelRatio );
+							default:
+								console.error( 'THREE.WebGLState: Invalid blending: ', blending );
+								break;
 
-			this.setViewport( 0, 0, width, height );
+						}
 
-		};
+					}
 
-		this.getCurrentViewport = function ( target ) {
+					currentBlendSrc = null;
+					currentBlendDst = null;
+					currentBlendSrcAlpha = null;
+					currentBlendDstAlpha = null;
 
-			if ( target === undefined ) {
+					currentBlending = blending;
+					currentPremultipledAlpha = premultipliedAlpha;
 
-				console.warn( 'WebGLRenderer: .getCurrentViewport() now requires a Vector4 as an argument' );
+				}
 
-				target = new Vector4();
+				return;
 
 			}
 
-			return target.copy( _currentViewport );
+			// custom blending
 
-		};
-
-		this.getViewport = function ( target ) {
+			blendEquationAlpha = blendEquationAlpha || blendEquation;
+			blendSrcAlpha = blendSrcAlpha || blendSrc;
+			blendDstAlpha = blendDstAlpha || blendDst;
 
-			return target.copy( _viewport );
+			if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
 
-		};
+				gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] );
 
-		this.setViewport = function ( x, y, width, height ) {
+				currentBlendEquation = blendEquation;
+				currentBlendEquationAlpha = blendEquationAlpha;
 
-			if ( x.isVector4 ) {
+			}
 
-				_viewport.set( x.x, x.y, x.z, x.w );
+			if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
 
-			} else {
+				gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] );
 
-				_viewport.set( x, y, width, height );
+				currentBlendSrc = blendSrc;
+				currentBlendDst = blendDst;
+				currentBlendSrcAlpha = blendSrcAlpha;
+				currentBlendDstAlpha = blendDstAlpha;
 
 			}
 
-			state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
+			currentBlending = blending;
+			currentPremultipledAlpha = null;
 
-		};
+		}
 
-		this.getScissor = function ( target ) {
+		function setMaterial( material, frontFaceCW ) {
 
-			return target.copy( _scissor );
+			material.side === DoubleSide
+				? disable( 2884 )
+				: enable( 2884 );
 
-		};
+			var flipSided = ( material.side === BackSide );
+			if ( frontFaceCW ) { flipSided = ! flipSided; }
 
-		this.setScissor = function ( x, y, width, height ) {
+			setFlipSided( flipSided );
 
-			if ( x.isVector4 ) {
+			( material.blending === NormalBlending && material.transparent === false )
+				? setBlending( NoBlending )
+				: setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha );
 
-				_scissor.set( x.x, x.y, x.z, x.w );
+			depthBuffer.setFunc( material.depthFunc );
+			depthBuffer.setTest( material.depthTest );
+			depthBuffer.setMask( material.depthWrite );
+			colorBuffer.setMask( material.colorWrite );
 
-			} else {
+			var stencilWrite = material.stencilWrite;
+			stencilBuffer.setTest( stencilWrite );
+			if ( stencilWrite ) {
 
-				_scissor.set( x, y, width, height );
+				stencilBuffer.setMask( material.stencilWriteMask );
+				stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask );
+				stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass );
 
 			}
 
-			state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
+			setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
 
-		};
+		}
 
-		this.getScissorTest = function () {
+		//
 
-			return _scissorTest;
+		function setFlipSided( flipSided ) {
 
-		};
+			if ( currentFlipSided !== flipSided ) {
 
-		this.setScissorTest = function ( boolean ) {
+				if ( flipSided ) {
 
-			state.setScissorTest( _scissorTest = boolean );
+					gl.frontFace( 2304 );
 
-		};
+				} else {
 
-		this.setOpaqueSort = function ( method ) {
+					gl.frontFace( 2305 );
 
-			_opaqueSort = method;
+				}
 
-		};
+				currentFlipSided = flipSided;
 
-		this.setTransparentSort = function ( method ) {
+			}
 
-			_transparentSort = method;
+		}
 
-		};
+		function setCullFace( cullFace ) {
 
-		// Clearing
+			if ( cullFace !== CullFaceNone ) {
 
-		this.getClearColor = function () {
+				enable( 2884 );
 
-			return background.getClearColor();
+				if ( cullFace !== currentCullFace ) {
 
-		};
+					if ( cullFace === CullFaceBack ) {
 
-		this.setClearColor = function () {
+						gl.cullFace( 1029 );
 
-			background.setClearColor.apply( background, arguments );
+					} else if ( cullFace === CullFaceFront ) {
 
-		};
+						gl.cullFace( 1028 );
 
-		this.getClearAlpha = function () {
+					} else {
 
-			return background.getClearAlpha();
+						gl.cullFace( 1032 );
 
-		};
+					}
 
-		this.setClearAlpha = function () {
+				}
 
-			background.setClearAlpha.apply( background, arguments );
+			} else {
 
-		};
+				disable( 2884 );
 
-		this.clear = function ( color, depth, stencil ) {
+			}
 
-			var bits = 0;
+			currentCullFace = cullFace;
 
-			if ( color === undefined || color ) { bits |= 16384; }
-			if ( depth === undefined || depth ) { bits |= 256; }
-			if ( stencil === undefined || stencil ) { bits |= 1024; }
+		}
 
-			_gl.clear( bits );
+		function setLineWidth( width ) {
 
-		};
+			if ( width !== currentLineWidth ) {
 
-		this.clearColor = function () {
+				if ( lineWidthAvailable ) { gl.lineWidth( width ); }
 
-			this.clear( true, false, false );
+				currentLineWidth = width;
 
-		};
+			}
 
-		this.clearDepth = function () {
+		}
 
-			this.clear( false, true, false );
+		function setPolygonOffset( polygonOffset, factor, units ) {
 
-		};
+			if ( polygonOffset ) {
 
-		this.clearStencil = function () {
+				enable( 32823 );
 
-			this.clear( false, false, true );
+				if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
 
-		};
+					gl.polygonOffset( factor, units );
 
-		//
+					currentPolygonOffsetFactor = factor;
+					currentPolygonOffsetUnits = units;
 
-		this.dispose = function () {
+				}
 
-			_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );
-			_canvas.removeEventListener( 'webglcontextrestored', onContextRestore, false );
+			} else {
 
-			renderLists.dispose();
-			renderStates.dispose();
-			properties.dispose();
-			objects.dispose();
+				disable( 32823 );
 
-			xr.dispose();
+			}
 
-			animation.stop();
+		}
 
-		};
+		function setScissorTest( scissorTest ) {
 
-		// Events
+			if ( scissorTest ) {
 
-		function onContextLost( event ) {
+				enable( 3089 );
 
-			event.preventDefault();
+			} else {
 
-			console.log( 'THREE.WebGLRenderer: Context Lost.' );
+				disable( 3089 );
 
-			_isContextLost = true;
+			}
 
 		}
 
-		function onContextRestore( /* event */ ) {
-
-			console.log( 'THREE.WebGLRenderer: Context Restored.' );
-
-			_isContextLost = false;
-
-			initGLContext();
+		// texture
 
-		}
+		function activeTexture( webglSlot ) {
 
-		function onMaterialDispose( event ) {
+			if ( webglSlot === undefined ) { webglSlot = 33984 + maxTextures - 1; }
 
-			var material = event.target;
+			if ( currentTextureSlot !== webglSlot ) {
 
-			material.removeEventListener( 'dispose', onMaterialDispose );
+				gl.activeTexture( webglSlot );
+				currentTextureSlot = webglSlot;
 
-			deallocateMaterial( material );
+			}
 
 		}
 
-		// Buffer deallocation
+		function bindTexture( webglType, webglTexture ) {
 
-		function deallocateMaterial( material ) {
+			if ( currentTextureSlot === null ) {
 
-			releaseMaterialProgramReference( material );
+				activeTexture();
 
-			properties.remove( material );
+			}
 
-		}
+			var boundTexture = currentBoundTextures[ currentTextureSlot ];
 
+			if ( boundTexture === undefined ) {
 
-		function releaseMaterialProgramReference( material ) {
+				boundTexture = { type: undefined, texture: undefined };
+				currentBoundTextures[ currentTextureSlot ] = boundTexture;
 
-			var programInfo = properties.get( material ).program;
+			}
 
-			material.program = undefined;
+			if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
 
-			if ( programInfo !== undefined ) {
+				gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] );
 
-				programCache.releaseProgram( programInfo );
+				boundTexture.type = webglType;
+				boundTexture.texture = webglTexture;
 
 			}
 
 		}
 
-		// Buffer rendering
-
-		function renderObjectImmediate( object, program ) {
-
-			object.render( function ( object ) {
+		function unbindTexture() {
 
-				_this.renderBufferImmediate( object, program );
+			var boundTexture = currentBoundTextures[ currentTextureSlot ];
 
-			} );
+			if ( boundTexture !== undefined && boundTexture.type !== undefined ) {
 
-		}
+				gl.bindTexture( boundTexture.type, null );
 
-		this.renderBufferImmediate = function ( object, program ) {
+				boundTexture.type = undefined;
+				boundTexture.texture = undefined;
 
-			state.initAttributes();
+			}
 
-			var buffers = properties.get( object );
+		}
 
-			if ( object.hasPositions && ! buffers.position ) { buffers.position = _gl.createBuffer(); }
-			if ( object.hasNormals && ! buffers.normal ) { buffers.normal = _gl.createBuffer(); }
-			if ( object.hasUvs && ! buffers.uv ) { buffers.uv = _gl.createBuffer(); }
-			if ( object.hasColors && ! buffers.color ) { buffers.color = _gl.createBuffer(); }
+		function compressedTexImage2D() {
 
-			var programAttributes = program.getAttributes();
+			try {
 
-			if ( object.hasPositions ) {
+				gl.compressedTexImage2D.apply( gl, arguments );
 
-				_gl.bindBuffer( 34962, buffers.position );
-				_gl.bufferData( 34962, object.positionArray, 35048 );
+			} catch ( error ) {
 
-				state.enableAttribute( programAttributes.position );
-				_gl.vertexAttribPointer( programAttributes.position, 3, 5126, false, 0, 0 );
+				console.error( 'THREE.WebGLState:', error );
 
 			}
 
-			if ( object.hasNormals ) {
-
-				_gl.bindBuffer( 34962, buffers.normal );
-				_gl.bufferData( 34962, object.normalArray, 35048 );
+		}
 
-				state.enableAttribute( programAttributes.normal );
-				_gl.vertexAttribPointer( programAttributes.normal, 3, 5126, false, 0, 0 );
+		function texImage2D() {
 
-			}
+			try {
 
-			if ( object.hasUvs ) {
+				gl.texImage2D.apply( gl, arguments );
 
-				_gl.bindBuffer( 34962, buffers.uv );
-				_gl.bufferData( 34962, object.uvArray, 35048 );
+			} catch ( error ) {
 
-				state.enableAttribute( programAttributes.uv );
-				_gl.vertexAttribPointer( programAttributes.uv, 2, 5126, false, 0, 0 );
+				console.error( 'THREE.WebGLState:', error );
 
 			}
 
-			if ( object.hasColors ) {
+		}
 
-				_gl.bindBuffer( 34962, buffers.color );
-				_gl.bufferData( 34962, object.colorArray, 35048 );
+		function texImage3D() {
 
-				state.enableAttribute( programAttributes.color );
-				_gl.vertexAttribPointer( programAttributes.color, 3, 5126, false, 0, 0 );
+			try {
 
-			}
+				gl.texImage3D.apply( gl, arguments );
 
-			state.disableUnusedAttributes();
+			} catch ( error ) {
 
-			_gl.drawArrays( 4, 0, object.count );
+				console.error( 'THREE.WebGLState:', error );
 
-			object.count = 0;
+			}
 
-		};
+		}
 
-		var tempScene = new Scene();
+		//
 
-		this.renderBufferDirect = function ( camera, scene, geometry, material, object, group ) {
+		function scissor( scissor ) {
 
-			if ( scene === null ) { scene = tempScene; } // renderBufferDirect second parameter used to be fog (could be null)
+			if ( currentScissor.equals( scissor ) === false ) {
 
-			var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );
+				gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
+				currentScissor.copy( scissor );
 
-			var program = setProgram( camera, scene, material, object );
+			}
 
-			state.setMaterial( material, frontFaceCW );
+		}
 
-			var updateBuffers = false;
+		function viewport( viewport ) {
 
-			if ( _currentGeometryProgram.geometry !== geometry.id ||
-				_currentGeometryProgram.program !== program.id ||
-				_currentGeometryProgram.wireframe !== ( material.wireframe === true ) ) {
+			if ( currentViewport.equals( viewport ) === false ) {
 
-				_currentGeometryProgram.geometry = geometry.id;
-				_currentGeometryProgram.program = program.id;
-				_currentGeometryProgram.wireframe = material.wireframe === true;
-				updateBuffers = true;
+				gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
+				currentViewport.copy( viewport );
 
 			}
 
-			if ( material.morphTargets || material.morphNormals ) {
+		}
 
-				morphtargets.update( object, geometry, material, program );
+		//
 
-				updateBuffers = true;
+		function reset() {
 
-			}
+			for ( var i = 0; i < enabledAttributes.length; i ++ ) {
 
-			if ( object.isInstancedMesh === true ) {
+				if ( enabledAttributes[ i ] === 1 ) {
 
-				updateBuffers = true;
+					gl.disableVertexAttribArray( i );
+					enabledAttributes[ i ] = 0;
+
+				}
 
 			}
 
-			//
+			enabledCapabilities = {};
 
-			var index = geometry.index;
-			var position = geometry.attributes.position;
+			currentTextureSlot = null;
+			currentBoundTextures = {};
 
-			//
+			currentProgram = null;
 
-			if ( index === null ) {
+			currentBlending = null;
 
-				if ( position === undefined || position.count === 0 ) { return; }
+			currentFlipSided = null;
+			currentCullFace = null;
 
-			} else if ( index.count === 0 ) {
+			colorBuffer.reset();
+			depthBuffer.reset();
+			stencilBuffer.reset();
 
-				return;
+		}
 
-			}
+		return {
 
-			//
+			buffers: {
+				color: colorBuffer,
+				depth: depthBuffer,
+				stencil: stencilBuffer
+			},
 
-			var rangeFactor = 1;
+			initAttributes: initAttributes,
+			enableAttribute: enableAttribute,
+			enableAttributeAndDivisor: enableAttributeAndDivisor,
+			disableUnusedAttributes: disableUnusedAttributes,
+			vertexAttribPointer: vertexAttribPointer,
+			enable: enable,
+			disable: disable,
 
-			if ( material.wireframe === true ) {
+			useProgram: useProgram,
 
-				index = geometries.getWireframeAttribute( geometry );
-				rangeFactor = 2;
+			setBlending: setBlending,
+			setMaterial: setMaterial,
 
-			}
+			setFlipSided: setFlipSided,
+			setCullFace: setCullFace,
 
-			var attribute;
-			var renderer = bufferRenderer;
+			setLineWidth: setLineWidth,
+			setPolygonOffset: setPolygonOffset,
 
-			if ( index !== null ) {
+			setScissorTest: setScissorTest,
 
-				attribute = attributes.get( index );
+			activeTexture: activeTexture,
+			bindTexture: bindTexture,
+			unbindTexture: unbindTexture,
+			compressedTexImage2D: compressedTexImage2D,
+			texImage2D: texImage2D,
+			texImage3D: texImage3D,
 
-				renderer = indexedBufferRenderer;
-				renderer.setIndex( attribute );
+			scissor: scissor,
+			viewport: viewport,
 
-			}
+			reset: reset
 
-			if ( updateBuffers ) {
+		};
 
-				setupVertexAttributes( object, geometry, material, program );
+	}
 
-				if ( index !== null ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-					_gl.bindBuffer( 34963, attribute.buffer );
+	function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) {
 
-				}
+		var isWebGL2 = capabilities.isWebGL2;
+		var maxTextures = capabilities.maxTextures;
+		var maxCubemapSize = capabilities.maxCubemapSize;
+		var maxTextureSize = capabilities.maxTextureSize;
+		var maxSamples = capabilities.maxSamples;
 
-			}
+		var _videoTextures = new WeakMap();
+		var _canvas;
 
-			//
+		// cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
+		// also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
+		// Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d).
 
-			var dataCount = ( index !== null ) ? index.count : position.count;
+		var useOffscreenCanvas = false;
 
-			var rangeStart = geometry.drawRange.start * rangeFactor;
-			var rangeCount = geometry.drawRange.count * rangeFactor;
+		try {
 
-			var groupStart = group !== null ? group.start * rangeFactor : 0;
-			var groupCount = group !== null ? group.count * rangeFactor : Infinity;
+			useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
+				&& ( new OffscreenCanvas( 1, 1 ).getContext( "2d" ) ) !== null;
 
-			var drawStart = Math.max( rangeStart, groupStart );
-			var drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;
+		} catch ( err ) {
 
-			var drawCount = Math.max( 0, drawEnd - drawStart + 1 );
+			// Ignore any errors
 
-			if ( drawCount === 0 ) { return; }
+		}
 
-			//
+		function createCanvas( width, height ) {
 
-			if ( object.isMesh ) {
+			// Use OffscreenCanvas when available. Specially needed in web workers
 
-				if ( material.wireframe === true ) {
+			return useOffscreenCanvas ?
+				new OffscreenCanvas( width, height ) :
+				document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
 
-					state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
-					renderer.setMode( 1 );
+		}
 
-				} else {
+		function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) {
 
-					renderer.setMode( 4 );
+			var scale = 1;
 
-				}
+			// handle case if texture exceeds max size
 
-			} else if ( object.isLine ) {
+			if ( image.width > maxSize || image.height > maxSize ) {
 
-				var lineWidth = material.linewidth;
+				scale = maxSize / Math.max( image.width, image.height );
 
-				if ( lineWidth === undefined ) { lineWidth = 1; } // Not using Line*Material
+			}
 
-				state.setLineWidth( lineWidth * getTargetPixelRatio() );
+			// only perform resize if necessary
 
-				if ( object.isLineSegments ) {
+			if ( scale < 1 || needsPowerOfTwo === true ) {
 
-					renderer.setMode( 1 );
+				// only perform resize for certain image types
 
-				} else if ( object.isLineLoop ) {
+				if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
+					( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
+					( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
 
-					renderer.setMode( 2 );
+					var floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor;
 
-				} else {
+					var width = floor( scale * image.width );
+					var height = floor( scale * image.height );
 
-					renderer.setMode( 3 );
+					if ( _canvas === undefined ) { _canvas = createCanvas( width, height ); }
 
-				}
+					// cube textures can't reuse the same canvas
 
-			} else if ( object.isPoints ) {
+					var canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas;
 
-				renderer.setMode( 0 );
+					canvas.width = width;
+					canvas.height = height;
 
-			} else if ( object.isSprite ) {
+					var context = canvas.getContext( '2d' );
+					context.drawImage( image, 0, 0, width, height );
 
-				renderer.setMode( 4 );
+					console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
 
-			}
+					return canvas;
 
-			if ( object.isInstancedMesh ) {
+				} else {
 
-				renderer.renderInstances( geometry, drawStart, drawCount, object.count );
+					if ( 'data' in image ) {
 
-			} else if ( geometry.isInstancedBufferGeometry ) {
+						console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' );
 
-				renderer.renderInstances( geometry, drawStart, drawCount, geometry.maxInstancedCount );
+					}
 
-			} else {
+					return image;
 
-				renderer.render( drawStart, drawCount );
+				}
 
 			}
 
-		};
-
-		function setupVertexAttributes( object, geometry, material, program ) {
+			return image;
 
-			if ( capabilities.isWebGL2 === false && ( object.isInstancedMesh || geometry.isInstancedBufferGeometry ) ) {
+		}
 
-				if ( extensions.get( 'ANGLE_instanced_arrays' ) === null ) { return; }
+		function isPowerOfTwo( image ) {
 
-			}
+			return MathUtils.isPowerOfTwo( image.width ) && MathUtils.isPowerOfTwo( image.height );
 
-			state.initAttributes();
+		}
 
-			var geometryAttributes = geometry.attributes;
+		function textureNeedsPowerOfTwo( texture ) {
 
-			var programAttributes = program.getAttributes();
+			if ( isWebGL2 ) { return false; }
 
-			var materialDefaultAttributeValues = material.defaultAttributeValues;
+			return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) ||
+				( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter );
 
-			for ( var name in programAttributes ) {
+		}
 
-				var programAttribute = programAttributes[ name ];
+		function textureNeedsGenerateMipmaps( texture, supportsMips ) {
 
-				if ( programAttribute >= 0 ) {
-
-					var geometryAttribute = geometryAttributes[ name ];
-
-					if ( geometryAttribute !== undefined ) {
+			return texture.generateMipmaps && supportsMips &&
+				texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
 
-						var normalized = geometryAttribute.normalized;
-						var size = geometryAttribute.itemSize;
+		}
 
-						var attribute = attributes.get( geometryAttribute );
+		function generateMipmap( target, texture, width, height ) {
 
-						// TODO Attribute may not be available on context restore
+			_gl.generateMipmap( target );
 
-						if ( attribute === undefined ) { continue; }
+			var textureProperties = properties.get( texture );
 
-						var buffer = attribute.buffer;
-						var type = attribute.type;
-						var bytesPerElement = attribute.bytesPerElement;
+			// Note: Math.log( x ) * Math.LOG2E used instead of Math.log2( x ) which is not supported by IE11
+			textureProperties.__maxMipLevel = Math.log( Math.max( width, height ) ) * Math.LOG2E;
 
-						if ( geometryAttribute.isInterleavedBufferAttribute ) {
+		}
 
-							var data = geometryAttribute.data;
-							var stride = data.stride;
-							var offset = geometryAttribute.offset;
+		function getInternalFormat( internalFormatName, glFormat, glType ) {
 
-							if ( data && data.isInstancedInterleavedBuffer ) {
+			if ( isWebGL2 === false ) { return glFormat; }
 
-								state.enableAttributeAndDivisor( programAttribute, data.meshPerAttribute );
+			if ( internalFormatName !== null ) {
 
-								if ( geometry.maxInstancedCount === undefined ) {
+				if ( _gl[ internalFormatName ] !== undefined ) { return _gl[ internalFormatName ]; }
 
-									geometry.maxInstancedCount = data.meshPerAttribute * data.count;
+				console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
 
-								}
+			}
 
-							} else {
+			var internalFormat = glFormat;
 
-								state.enableAttribute( programAttribute );
+			if ( glFormat === 6403 ) {
 
-							}
+				if ( glType === 5126 ) { internalFormat = 33326; }
+				if ( glType === 5131 ) { internalFormat = 33325; }
+				if ( glType === 5121 ) { internalFormat = 33321; }
 
-							_gl.bindBuffer( 34962, buffer );
-							state.vertexAttribPointer( programAttribute, size, type, normalized, stride * bytesPerElement, offset * bytesPerElement );
+			}
 
-						} else {
+			if ( glFormat === 6407 ) {
 
-							if ( geometryAttribute.isInstancedBufferAttribute ) {
+				if ( glType === 5126 ) { internalFormat = 34837; }
+				if ( glType === 5131 ) { internalFormat = 34843; }
+				if ( glType === 5121 ) { internalFormat = 32849; }
 
-								state.enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute );
+			}
 
-								if ( geometry.maxInstancedCount === undefined ) {
+			if ( glFormat === 6408 ) {
 
-									geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * geometryAttribute.count;
+				if ( glType === 5126 ) { internalFormat = 34836; }
+				if ( glType === 5131 ) { internalFormat = 34842; }
+				if ( glType === 5121 ) { internalFormat = 32856; }
 
-								}
+			}
 
-							} else {
+			if ( internalFormat === 33325 || internalFormat === 33326 ||
+				internalFormat === 34842 || internalFormat === 34836 ) {
 
-								state.enableAttribute( programAttribute );
+				extensions.get( 'EXT_color_buffer_float' );
 
-							}
+			}
 
-							_gl.bindBuffer( 34962, buffer );
-							state.vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 );
+			return internalFormat;
 
-						}
+		}
 
-					} else if ( name === 'instanceMatrix' ) {
+		// Fallback filters for non-power-of-2 textures
 
-						var attribute = attributes.get( object.instanceMatrix );
+		function filterFallback( f ) {
 
-						// TODO Attribute may not be available on context restore
+			if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
 
-						if ( attribute === undefined ) { continue; }
+				return 9728;
 
-						var buffer = attribute.buffer;
-						var type = attribute.type;
+			}
 
-						state.enableAttributeAndDivisor( programAttribute + 0, 1 );
-						state.enableAttributeAndDivisor( programAttribute + 1, 1 );
-						state.enableAttributeAndDivisor( programAttribute + 2, 1 );
-						state.enableAttributeAndDivisor( programAttribute + 3, 1 );
+			return 9729;
 
-						_gl.bindBuffer( 34962, buffer );
+		}
 
-						_gl.vertexAttribPointer( programAttribute + 0, 4, type, false, 64, 0 );
-						_gl.vertexAttribPointer( programAttribute + 1, 4, type, false, 64, 16 );
-						_gl.vertexAttribPointer( programAttribute + 2, 4, type, false, 64, 32 );
-						_gl.vertexAttribPointer( programAttribute + 3, 4, type, false, 64, 48 );
+		//
 
-					} else if ( materialDefaultAttributeValues !== undefined ) {
+		function onTextureDispose( event ) {
 
-						var value = materialDefaultAttributeValues[ name ];
+			var texture = event.target;
 
-						if ( value !== undefined ) {
+			texture.removeEventListener( 'dispose', onTextureDispose );
 
-							switch ( value.length ) {
+			deallocateTexture( texture );
 
-								case 2:
-									_gl.vertexAttrib2fv( programAttribute, value );
-									break;
+			if ( texture.isVideoTexture ) {
 
-								case 3:
-									_gl.vertexAttrib3fv( programAttribute, value );
-									break;
+				_videoTextures.delete( texture );
 
-								case 4:
-									_gl.vertexAttrib4fv( programAttribute, value );
-									break;
+			}
 
-								default:
-									_gl.vertexAttrib1fv( programAttribute, value );
+			info.memory.textures --;
 
-							}
+		}
 
-						}
+		function onRenderTargetDispose( event ) {
 
-					}
+			var renderTarget = event.target;
 
-				}
+			renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
 
-			}
+			deallocateRenderTarget( renderTarget );
 
-			state.disableUnusedAttributes();
+			info.memory.textures --;
 
 		}
 
-		// Compile
-
-		this.compile = function ( scene, camera ) {
-
-			currentRenderState = renderStates.get( scene, camera );
-			currentRenderState.init();
+		//
 
-			scene.traverse( function ( object ) {
+		function deallocateTexture( texture ) {
 
-				if ( object.isLight ) {
+			var textureProperties = properties.get( texture );
 
-					currentRenderState.pushLight( object );
+			if ( textureProperties.__webglInit === undefined ) { return; }
 
-					if ( object.castShadow ) {
+			_gl.deleteTexture( textureProperties.__webglTexture );
 
-						currentRenderState.pushShadow( object );
+			properties.remove( texture );
 
-					}
+		}
 
-				}
+		function deallocateRenderTarget( renderTarget ) {
 
-			} );
+			var renderTargetProperties = properties.get( renderTarget );
+			var textureProperties = properties.get( renderTarget.texture );
 
-			currentRenderState.setupLights( camera );
+			if ( ! renderTarget ) { return; }
 
-			var compiled = {};
+			if ( textureProperties.__webglTexture !== undefined ) {
 
-			scene.traverse( function ( object ) {
+				_gl.deleteTexture( textureProperties.__webglTexture );
 
-				if ( object.material ) {
+			}
 
-					if ( Array.isArray( object.material ) ) {
+			if ( renderTarget.depthTexture ) {
 
-						for ( var i = 0; i < object.material.length; i ++ ) {
+				renderTarget.depthTexture.dispose();
 
-							if ( object.material[ i ].uuid in compiled === false ) {
+			}
 
-								initMaterial( object.material[ i ], scene, object );
-								compiled[ object.material[ i ].uuid ] = true;
+			if ( renderTarget.isWebGLCubeRenderTarget ) {
 
-							}
+				for ( var i = 0; i < 6; i ++ ) {
 
-						}
+					_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
+					if ( renderTargetProperties.__webglDepthbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] ); }
 
-					} else if ( object.material.uuid in compiled === false ) {
+				}
 
-						initMaterial( object.material, scene, object );
-						compiled[ object.material.uuid ] = true;
+			} else {
 
-					}
+				_gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
+				if ( renderTargetProperties.__webglDepthbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer ); }
+				if ( renderTargetProperties.__webglMultisampledFramebuffer ) { _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer ); }
+				if ( renderTargetProperties.__webglColorRenderbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer ); }
+				if ( renderTargetProperties.__webglDepthRenderbuffer ) { _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer ); }
 
-				}
+			}
 
-			} );
+			properties.remove( renderTarget.texture );
+			properties.remove( renderTarget );
 
-		};
+		}
 
-		// Animation Loop
+		//
 
-		var onAnimationFrameCallback = null;
+		var textureUnits = 0;
 
-		function onAnimationFrame( time ) {
+		function resetTextureUnits() {
 
-			if ( xr.isPresenting ) { return; }
-			if ( onAnimationFrameCallback ) { onAnimationFrameCallback( time ); }
+			textureUnits = 0;
 
 		}
 
-		var animation = new WebGLAnimation();
-		animation.setAnimationLoop( onAnimationFrame );
+		function allocateTextureUnit() {
 
-		if ( typeof window !== 'undefined' ) { animation.setContext( window ); }
+			var textureUnit = textureUnits;
 
-		this.setAnimationLoop = function ( callback ) {
+			if ( textureUnit >= maxTextures ) {
 
-			onAnimationFrameCallback = callback;
-			xr.setAnimationLoop( callback );
+				console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures );
 
-			animation.start();
+			}
 
-		};
+			textureUnits += 1;
 
-		// Rendering
+			return textureUnit;
 
-		this.render = function ( scene, camera ) {
+		}
 
-			var renderTarget, forceClear;
+		//
 
-			if ( arguments[ 2 ] !== undefined ) {
+		function setTexture2D( texture, slot ) {
 
-				console.warn( 'THREE.WebGLRenderer.render(): the renderTarget argument has been removed. Use .setRenderTarget() instead.' );
-				renderTarget = arguments[ 2 ];
+			var textureProperties = properties.get( texture );
 
-			}
+			if ( texture.isVideoTexture ) { updateVideoTexture( texture ); }
 
-			if ( arguments[ 3 ] !== undefined ) {
+			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
 
-				console.warn( 'THREE.WebGLRenderer.render(): the forceClear argument has been removed. Use .clear() instead.' );
-				forceClear = arguments[ 3 ];
+				var image = texture.image;
 
-			}
+				if ( image === undefined ) {
 
-			if ( ! ( camera && camera.isCamera ) ) {
+					console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined' );
 
-				console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
-				return;
+				} else if ( image.complete === false ) {
 
-			}
+					console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' );
 
-			if ( _isContextLost ) { return; }
+				} else {
 
-			// reset caching for this frame
+					uploadTexture( textureProperties, texture, slot );
+					return;
 
-			_currentGeometryProgram.geometry = null;
-			_currentGeometryProgram.program = null;
-			_currentGeometryProgram.wireframe = false;
-			_currentMaterialId = - 1;
-			_currentCamera = null;
+				}
 
-			// update scene graph
+			}
 
-			if ( scene.autoUpdate === true ) { scene.updateMatrixWorld(); }
+			state.activeTexture( 33984 + slot );
+			state.bindTexture( 3553, textureProperties.__webglTexture );
 
-			// update camera matrices and frustum
+		}
 
-			if ( camera.parent === null ) { camera.updateMatrixWorld(); }
+		function setTexture2DArray( texture, slot ) {
 
-			if ( xr.enabled && xr.isPresenting ) {
+			var textureProperties = properties.get( texture );
 
-				camera = xr.getCamera( camera );
+			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
+
+				uploadTexture( textureProperties, texture, slot );
+				return;
 
 			}
 
-			//
-			scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget );
+			state.activeTexture( 33984 + slot );
+			state.bindTexture( 35866, textureProperties.__webglTexture );
 
-			currentRenderState = renderStates.get( scene, camera );
-			currentRenderState.init();
+		}
 
-			_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
-			_frustum.setFromProjectionMatrix( _projScreenMatrix );
+		function setTexture3D( texture, slot ) {
 
-			_localClippingEnabled = this.localClippingEnabled;
-			_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
+			var textureProperties = properties.get( texture );
 
-			currentRenderList = renderLists.get( scene, camera );
-			currentRenderList.init();
+			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
 
-			projectObject( scene, camera, 0, _this.sortObjects );
+				uploadTexture( textureProperties, texture, slot );
+				return;
 
-			currentRenderList.finish();
+			}
 
-			if ( _this.sortObjects === true ) {
+			state.activeTexture( 33984 + slot );
+			state.bindTexture( 32879, textureProperties.__webglTexture );
 
-				currentRenderList.sort( _opaqueSort, _transparentSort );
+		}
 
-			}
+		function setTextureCube( texture, slot ) {
 
-			//
+			if ( texture.image.length !== 6 ) { return; }
 
-			if ( _clippingEnabled ) { _clipping.beginShadows(); }
+			var textureProperties = properties.get( texture );
 
-			var shadowsArray = currentRenderState.state.shadowsArray;
+			if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
 
-			shadowMap.render( shadowsArray, scene, camera );
+				initTexture( textureProperties, texture );
 
-			currentRenderState.setupLights( camera );
+				state.activeTexture( 33984 + slot );
+				state.bindTexture( 34067, textureProperties.__webglTexture );
 
-			if ( _clippingEnabled ) { _clipping.endShadows(); }
+				_gl.pixelStorei( 37440, texture.flipY );
 
-			//
+				var isCompressed = ( texture && ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture ) );
+				var isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture );
 
-			if ( this.info.autoReset ) { this.info.reset(); }
+				var cubeImage = [];
 
-			if ( renderTarget !== undefined ) {
+				for ( var i = 0; i < 6; i ++ ) {
 
-				this.setRenderTarget( renderTarget );
+					if ( ! isCompressed && ! isDataTexture ) {
 
-			}
+						cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, maxCubemapSize );
 
-			//
+					} else {
 
-			background.render( currentRenderList, scene, camera, forceClear );
+						cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
 
-			// render scene
+					}
 
-			var opaqueObjects = currentRenderList.opaque;
-			var transparentObjects = currentRenderList.transparent;
+				}
 
-			if ( scene.overrideMaterial ) {
+				var image = cubeImage[ 0 ],
+					supportsMips = isPowerOfTwo( image ) || isWebGL2,
+					glFormat = utils.convert( texture.format ),
+					glType = utils.convert( texture.type ),
+					glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
 
-				var overrideMaterial = scene.overrideMaterial;
+				setTextureParameters( 34067, texture, supportsMips );
 
-				if ( opaqueObjects.length ) { renderObjects( opaqueObjects, scene, camera, overrideMaterial ); }
-				if ( transparentObjects.length ) { renderObjects( transparentObjects, scene, camera, overrideMaterial ); }
+				var mipmaps;
 
-			} else {
+				if ( isCompressed ) {
 
-				// opaque pass (front-to-back order)
+					for ( var i = 0; i < 6; i ++ ) {
 
-				if ( opaqueObjects.length ) { renderObjects( opaqueObjects, scene, camera ); }
+						mipmaps = cubeImage[ i ].mipmaps;
 
-				// transparent pass (back-to-front order)
+						for ( var j = 0; j < mipmaps.length; j ++ ) {
 
-				if ( transparentObjects.length ) { renderObjects( transparentObjects, scene, camera ); }
+							var mipmap = mipmaps[ j ];
 
-			}
+							if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
 
-			//
+								if ( glFormat !== null ) {
 
-			scene.onAfterRender( _this, scene, camera );
+									state.compressedTexImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
 
-			//
+								} else {
 
-			if ( _currentRenderTarget !== null ) {
+									console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' );
 
-				// Generate mipmap if we're using any kind of mipmap filtering
+								}
 
-				textures.updateRenderTargetMipmap( _currentRenderTarget );
+							} else {
 
-				// resolve multisample renderbuffers to a single-sample texture if necessary
+								state.texImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
 
-				textures.updateMultisampleRenderTarget( _currentRenderTarget );
+							}
 
-			}
+						}
 
-			// Ensure depth buffer writing is enabled so it can be cleared on next render
+					}
 
-			state.buffers.depth.setTest( true );
-			state.buffers.depth.setMask( true );
-			state.buffers.color.setMask( true );
+					textureProperties.__maxMipLevel = mipmaps.length - 1;
 
-			state.setPolygonOffset( false );
+				} else {
 
-			// _gl.finish();
+					mipmaps = texture.mipmaps;
 
-			currentRenderList = null;
-			currentRenderState = null;
+					for ( var i = 0; i < 6; i ++ ) {
 
-		};
+						if ( isDataTexture ) {
 
-		function projectObject( object, camera, groupOrder, sortObjects ) {
+							state.texImage2D( 34069 + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
 
-			if ( object.visible === false ) { return; }
+							for ( var j = 0; j < mipmaps.length; j ++ ) {
 
-			var visible = object.layers.test( camera.layers );
+								var mipmap = mipmaps[ j ];
+								var mipmapImage = mipmap.image[ i ].image;
 
-			if ( visible ) {
+								state.texImage2D( 34069 + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data );
 
-				if ( object.isGroup ) {
+							}
 
-					groupOrder = object.renderOrder;
+						} else {
 
-				} else if ( object.isLOD ) {
+							state.texImage2D( 34069 + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] );
 
-					if ( object.autoUpdate === true ) { object.update( camera ); }
+							for ( var j = 0; j < mipmaps.length; j ++ ) {
 
-				} else if ( object.isLight ) {
+								var mipmap = mipmaps[ j ];
 
-					currentRenderState.pushLight( object );
+								state.texImage2D( 34069 + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] );
 
-					if ( object.castShadow ) {
+							}
 
-						currentRenderState.pushShadow( object );
+						}
 
 					}
 
-				} else if ( object.isSprite ) {
+					textureProperties.__maxMipLevel = mipmaps.length;
 
-					if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
+				}
 
-						if ( sortObjects ) {
+				if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
 
-							_vector3.setFromMatrixPosition( object.matrixWorld )
-								.applyMatrix4( _projScreenMatrix );
+					// We assume images for cube map have the same size.
+					generateMipmap( 34067, texture, image.width, image.height );
 
-						}
+				}
 
-						var geometry = objects.update( object );
-						var material = object.material;
+				textureProperties.__version = texture.version;
 
-						if ( material.visible ) {
+				if ( texture.onUpdate ) { texture.onUpdate( texture ); }
 
-							currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
+			} else {
 
-						}
+				state.activeTexture( 33984 + slot );
+				state.bindTexture( 34067, textureProperties.__webglTexture );
 
-					}
+			}
 
-				} else if ( object.isImmediateRenderObject ) {
+		}
 
-					if ( sortObjects ) {
+		function setTextureCubeDynamic( texture, slot ) {
 
-						_vector3.setFromMatrixPosition( object.matrixWorld )
-							.applyMatrix4( _projScreenMatrix );
+			state.activeTexture( 33984 + slot );
+			state.bindTexture( 34067, properties.get( texture ).__webglTexture );
 
-					}
+		}
 
-					currentRenderList.push( object, null, object.material, groupOrder, _vector3.z, null );
+		var wrappingToGL = {};
+		wrappingToGL[ RepeatWrapping ] = 10497;
+		wrappingToGL[ ClampToEdgeWrapping ] = 33071;
+		wrappingToGL[ MirroredRepeatWrapping ] = 33648;
 
-				} else if ( object.isMesh || object.isLine || object.isPoints ) {
+		var filterToGL = {};
+		filterToGL[ NearestFilter ] = 9728;
+		filterToGL[ NearestMipmapNearestFilter ] = 9984;
+		filterToGL[ NearestMipmapLinearFilter ] = 9986;
+		filterToGL[ LinearFilter ] = 9729;
+		filterToGL[ LinearMipmapNearestFilter ] = 9985;
+		filterToGL[ LinearMipmapLinearFilter ] = 9987;
 
-					if ( object.isSkinnedMesh ) {
+		function setTextureParameters( textureType, texture, supportsMips ) {
 
-						// update skeleton only once in a frame
+			if ( supportsMips ) {
 
-						if ( object.skeleton.frame !== info.render.frame ) {
+				_gl.texParameteri( textureType, 10242, wrappingToGL[ texture.wrapS ] );
+				_gl.texParameteri( textureType, 10243, wrappingToGL[ texture.wrapT ] );
 
-							object.skeleton.update();
-							object.skeleton.frame = info.render.frame;
+				if ( textureType === 32879 || textureType === 35866 ) {
 
-						}
+					_gl.texParameteri( textureType, 32882, wrappingToGL[ texture.wrapR ] );
 
-					}
+				}
 
-					if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
+				_gl.texParameteri( textureType, 10240, filterToGL[ texture.magFilter ] );
+				_gl.texParameteri( textureType, 10241, filterToGL[ texture.minFilter ] );
 
-						if ( sortObjects ) {
+			} else {
 
-							_vector3.setFromMatrixPosition( object.matrixWorld )
-								.applyMatrix4( _projScreenMatrix );
+				_gl.texParameteri( textureType, 10242, 33071 );
+				_gl.texParameteri( textureType, 10243, 33071 );
 
-						}
+				if ( textureType === 32879 || textureType === 35866 ) {
 
-						var geometry = objects.update( object );
-						var material = object.material;
+					_gl.texParameteri( textureType, 32882, 33071 );
 
-						if ( Array.isArray( material ) ) {
+				}
 
-							var groups = geometry.groups;
+				if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) {
 
-							for ( var i = 0, l = groups.length; i < l; i ++ ) {
+					console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' );
 
-								var group = groups[ i ];
-								var groupMaterial = material[ group.materialIndex ];
+				}
 
-								if ( groupMaterial && groupMaterial.visible ) {
+				_gl.texParameteri( textureType, 10240, filterFallback( texture.magFilter ) );
+				_gl.texParameteri( textureType, 10241, filterFallback( texture.minFilter ) );
 
-									currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
+				if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) {
 
-								}
+					console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' );
 
-							}
+				}
 
-						} else if ( material.visible ) {
+			}
 
-							currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
+			var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
 
-						}
+			if ( extension ) {
 
-					}
+				if ( texture.type === FloatType && extensions.get( 'OES_texture_float_linear' ) === null ) { return; }
+				if ( texture.type === HalfFloatType && ( isWebGL2 || extensions.get( 'OES_texture_half_float_linear' ) ) === null ) { return; }
+
+				if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
+
+					_gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
+					properties.get( texture ).__currentAnisotropy = texture.anisotropy;
 
 				}
 
 			}
 
-			var children = object.children;
-
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
+		}
 
-				projectObject( children[ i ], camera, groupOrder, sortObjects );
+		function initTexture( textureProperties, texture ) {
 
-			}
+			if ( textureProperties.__webglInit === undefined ) {
 
-		}
+				textureProperties.__webglInit = true;
 
-		function renderObjects( renderList, scene, camera, overrideMaterial ) {
+				texture.addEventListener( 'dispose', onTextureDispose );
 
-			for ( var i = 0, l = renderList.length; i < l; i ++ ) {
+				textureProperties.__webglTexture = _gl.createTexture();
 
-				var renderItem = renderList[ i ];
+				info.memory.textures ++;
 
-				var object = renderItem.object;
-				var geometry = renderItem.geometry;
-				var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
-				var group = renderItem.group;
+			}
 
-				if ( camera.isArrayCamera ) {
+		}
 
-					_currentArrayCamera = camera;
+		function uploadTexture( textureProperties, texture, slot ) {
 
-					var cameras = camera.cameras;
+			var textureType = 3553;
 
-					for ( var j = 0, jl = cameras.length; j < jl; j ++ ) {
+			if ( texture.isDataTexture2DArray ) { textureType = 35866; }
+			if ( texture.isDataTexture3D ) { textureType = 32879; }
 
-						var camera2 = cameras[ j ];
+			initTexture( textureProperties, texture );
 
-						if ( object.layers.test( camera2.layers ) ) {
+			state.activeTexture( 33984 + slot );
+			state.bindTexture( textureType, textureProperties.__webglTexture );
 
-							state.viewport( _currentViewport.copy( camera2.viewport ) );
+			_gl.pixelStorei( 37440, texture.flipY );
+			_gl.pixelStorei( 37441, texture.premultiplyAlpha );
+			_gl.pixelStorei( 3317, texture.unpackAlignment );
 
-							currentRenderState.setupLights( camera2 );
+			var needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false;
+			var image = resizeImage( texture.image, needsPowerOfTwo, false, maxTextureSize );
 
-							renderObject( object, scene, camera2, geometry, material, group );
+			var supportsMips = isPowerOfTwo( image ) || isWebGL2,
+				glFormat = utils.convert( texture.format ),
+				glType = utils.convert( texture.type ),
+				glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
 
-						}
+			setTextureParameters( textureType, texture, supportsMips );
 
-					}
+			var mipmap, mipmaps = texture.mipmaps;
 
-				} else {
+			if ( texture.isDepthTexture ) {
 
-					_currentArrayCamera = null;
+				// populate depth texture with dummy data
 
-					renderObject( object, scene, camera, geometry, material, group );
+				glInternalFormat = 6402;
 
-				}
+				if ( isWebGL2 ) {
 
-			}
+					if ( texture.type === FloatType ) {
 
-		}
+						glInternalFormat = 36012;
 
-		function renderObject( object, scene, camera, geometry, material, group ) {
+					} else if ( texture.type === UnsignedIntType ) {
 
-			object.onBeforeRender( _this, scene, camera, geometry, material, group );
-			currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
+						glInternalFormat = 33190;
 
-			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
-			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
+					} else if ( texture.type === UnsignedInt248Type ) {
 
-			if ( object.isImmediateRenderObject ) {
+						glInternalFormat = 35056;
 
-				var program = setProgram( camera, scene, material, object );
+					} else {
 
-				state.setMaterial( material );
+						glInternalFormat = 33189; // WebGL2 requires sized internalformat for glTexImage2D
 
-				_currentGeometryProgram.geometry = null;
-				_currentGeometryProgram.program = null;
-				_currentGeometryProgram.wireframe = false;
+					}
 
-				renderObjectImmediate( object, program );
+				} else {
 
-			} else {
+					if ( texture.type === FloatType ) {
 
-				_this.renderBufferDirect( camera, scene, geometry, material, object, group );
+						console.error( 'WebGLRenderer: Floating point depth texture requires WebGL2.' );
 
-			}
+					}
 
-			object.onAfterRender( _this, scene, camera, geometry, material, group );
-			currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
+				}
 
-		}
+				// validation checks for WebGL 1
 
-		function initMaterial( material, scene, object ) {
+				if ( texture.format === DepthFormat && glInternalFormat === 6402 ) {
 
-			var materialProperties = properties.get( material );
+					// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
+					// DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
+					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
+					if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) {
 
-			var lights = currentRenderState.state.lights;
-			var shadowsArray = currentRenderState.state.shadowsArray;
+						console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' );
 
-			var lightsStateVersion = lights.state.version;
+						texture.type = UnsignedShortType;
+						glType = utils.convert( texture.type );
 
-			var parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, _clipping.numPlanes, _clipping.numIntersection, object );
-			var programCacheKey = programCache.getProgramCacheKey( parameters );
+					}
 
-			var program = materialProperties.program;
-			var programChange = true;
+				}
 
-			if ( program === undefined ) {
+				if ( texture.format === DepthStencilFormat && glInternalFormat === 6402 ) {
 
-				// new material
-				material.addEventListener( 'dispose', onMaterialDispose );
+					// Depth stencil textures need the DEPTH_STENCIL internal format
+					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
+					glInternalFormat = 34041;
 
-			} else if ( program.cacheKey !== programCacheKey ) {
+					// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
+					// DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
+					// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
+					if ( texture.type !== UnsignedInt248Type ) {
 
-				// changed glsl or parameters
-				releaseMaterialProgramReference( material );
+						console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' );
 
-			} else if ( materialProperties.lightsStateVersion !== lightsStateVersion ) {
+						texture.type = UnsignedInt248Type;
+						glType = utils.convert( texture.type );
 
-				materialProperties.lightsStateVersion = lightsStateVersion;
+					}
 
-				programChange = false;
+				}
 
-			} else if ( parameters.shaderID !== undefined ) {
+				//
 
-				// same glsl and uniform list
-				return;
+				state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null );
 
-			} else {
+			} else if ( texture.isDataTexture ) {
 
-				// only rebuild uniform list
-				programChange = false;
+				// use manually created mipmaps if available
+				// if there are no manual mipmaps
+				// set 0 level mipmap and then use GL to generate other mipmap levels
 
-			}
+				if ( mipmaps.length > 0 && supportsMips ) {
 
-			if ( programChange ) {
+					for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
 
-				program = programCache.acquireProgram( parameters, programCacheKey );
+						mipmap = mipmaps[ i ];
+						state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
 
-				materialProperties.program = program;
-				materialProperties.uniforms = parameters.uniforms;
-				materialProperties.outputEncoding = parameters.outputEncoding;
-				material.program = program;
+					}
 
-			}
+					texture.generateMipmaps = false;
+					textureProperties.__maxMipLevel = mipmaps.length - 1;
 
-			var programAttributes = program.getAttributes();
+				} else {
 
-			if ( material.morphTargets ) {
+					state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data );
+					textureProperties.__maxMipLevel = 0;
 
-				material.numSupportedMorphTargets = 0;
+				}
 
-				for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
+			} else if ( texture.isCompressedTexture ) {
 
-					if ( programAttributes[ 'morphTarget' + i ] >= 0 ) {
+				for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
 
-						material.numSupportedMorphTargets ++;
+					mipmap = mipmaps[ i ];
 
-					}
+					if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
 
-				}
+						if ( glFormat !== null ) {
 
-			}
+							state.compressedTexImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
 
-			if ( material.morphNormals ) {
+						} else {
 
-				material.numSupportedMorphNormals = 0;
+							console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
 
-				for ( var i = 0; i < _this.maxMorphNormals; i ++ ) {
+						}
 
-					if ( programAttributes[ 'morphNormal' + i ] >= 0 ) {
+					} else {
 
-						material.numSupportedMorphNormals ++;
+						state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
 
 					}
 
 				}
 
-			}
+				textureProperties.__maxMipLevel = mipmaps.length - 1;
 
-			var uniforms = materialProperties.uniforms;
+			} else if ( texture.isDataTexture2DArray ) {
 
-			if ( ! material.isShaderMaterial &&
-				! material.isRawShaderMaterial ||
-				material.clipping === true ) {
+				state.texImage3D( 35866, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
+				textureProperties.__maxMipLevel = 0;
 
-				materialProperties.numClippingPlanes = _clipping.numPlanes;
-				materialProperties.numIntersection = _clipping.numIntersection;
-				uniforms.clippingPlanes = _clipping.uniform;
+			} else if ( texture.isDataTexture3D ) {
 
-			}
+				state.texImage3D( 32879, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
+				textureProperties.__maxMipLevel = 0;
 
-			materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null;
-			materialProperties.fog = scene.fog;
+			} else {
 
-			// store the light setup it was created for
+				// regular Texture (image, video, canvas)
 
-			materialProperties.needsLights = materialNeedsLights( material );
-			materialProperties.lightsStateVersion = lightsStateVersion;
+				// use manually created mipmaps if available
+				// if there are no manual mipmaps
+				// set 0 level mipmap and then use GL to generate other mipmap levels
 
-			if ( materialProperties.needsLights ) {
+				if ( mipmaps.length > 0 && supportsMips ) {
 
-				// wire up the material to this renderer's lighting state
+					for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
 
-				uniforms.ambientLightColor.value = lights.state.ambient;
-				uniforms.lightProbe.value = lights.state.probe;
-				uniforms.directionalLights.value = lights.state.directional;
-				uniforms.directionalLightShadows.value = lights.state.directionalShadow;
-				uniforms.spotLights.value = lights.state.spot;
-				uniforms.spotLightShadows.value = lights.state.spotShadow;
-				uniforms.rectAreaLights.value = lights.state.rectArea;
-				uniforms.pointLights.value = lights.state.point;
-				uniforms.pointLightShadows.value = lights.state.pointShadow;
-				uniforms.hemisphereLights.value = lights.state.hemi;
+						mipmap = mipmaps[ i ];
+						state.texImage2D( 3553, i, glInternalFormat, glFormat, glType, mipmap );
 
-				uniforms.directionalShadowMap.value = lights.state.directionalShadowMap;
-				uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix;
-				uniforms.spotShadowMap.value = lights.state.spotShadowMap;
-				uniforms.spotShadowMatrix.value = lights.state.spotShadowMatrix;
-				uniforms.pointShadowMap.value = lights.state.pointShadowMap;
-				uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix;
-				// TODO (abelnation): add area lights shadow info to uniforms
+					}
 
-			}
+					texture.generateMipmaps = false;
+					textureProperties.__maxMipLevel = mipmaps.length - 1;
 
-			var progUniforms = materialProperties.program.getUniforms(),
-				uniformsList =
-					WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
+				} else {
 
-			materialProperties.uniformsList = uniformsList;
+					state.texImage2D( 3553, 0, glInternalFormat, glFormat, glType, image );
+					textureProperties.__maxMipLevel = 0;
 
-		}
+				}
 
-		function setProgram( camera, scene, material, object ) {
+			}
 
-			textures.resetTextureUnits();
+			if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
 
-			var fog = scene.fog;
-			var environment = material.isMeshStandardMaterial ? scene.environment : null;
-			var encoding = ( _currentRenderTarget === null ) ? _this.outputEncoding : _currentRenderTarget.texture.encoding;
+				generateMipmap( textureType, texture, image.width, image.height );
 
-			var materialProperties = properties.get( material );
-			var lights = currentRenderState.state.lights;
+			}
 
-			if ( _clippingEnabled ) {
+			textureProperties.__version = texture.version;
 
-				if ( _localClippingEnabled || camera !== _currentCamera ) {
+			if ( texture.onUpdate ) { texture.onUpdate( texture ); }
 
-					var useCache =
-						camera === _currentCamera &&
-						material.id === _currentMaterialId;
+		}
 
-					// we might want to call this function with some ClippingGroup
-					// object instead of the material, once it becomes feasible
-					// (#8465, #8379)
-					_clipping.setState(
-						material.clippingPlanes, material.clipIntersection, material.clipShadows,
-						camera, materialProperties, useCache );
+		// Render targets
 
-				}
+		// Setup storage for target texture and bind it to correct framebuffer
+		function setupFrameBufferTexture( framebuffer, renderTarget, attachment, textureTarget ) {
 
-			}
+			var glFormat = utils.convert( renderTarget.texture.format );
+			var glType = utils.convert( renderTarget.texture.type );
+			var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
+			state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
+			_gl.bindFramebuffer( 36160, framebuffer );
+			_gl.framebufferTexture2D( 36160, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
+			_gl.bindFramebuffer( 36160, null );
 
-			if ( material.version === materialProperties.__version ) {
+		}
 
-				if ( materialProperties.program === undefined ) {
+		// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
+		function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) {
 
-					initMaterial( material, scene, object );
+			_gl.bindRenderbuffer( 36161, renderbuffer );
 
-				} else if ( material.fog && materialProperties.fog !== fog ) {
+			if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
 
-					initMaterial( material, scene, object );
+				var glInternalFormat = 33189;
 
-				} else if ( materialProperties.environment !== environment ) {
+				if ( isMultisample ) {
 
-					initMaterial( material, scene, object );
+					var depthTexture = renderTarget.depthTexture;
 
-				} else if ( materialProperties.needsLights && ( materialProperties.lightsStateVersion !== lights.state.version ) ) {
+					if ( depthTexture && depthTexture.isDepthTexture ) {
 
-					initMaterial( material, scene, object );
+						if ( depthTexture.type === FloatType ) {
 
-				} else if ( materialProperties.numClippingPlanes !== undefined &&
-					( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
-					materialProperties.numIntersection !== _clipping.numIntersection ) ) {
+							glInternalFormat = 36012;
 
-					initMaterial( material, scene, object );
+						} else if ( depthTexture.type === UnsignedIntType ) {
 
-				} else if ( materialProperties.outputEncoding !== encoding ) {
+							glInternalFormat = 33190;
 
-					initMaterial( material, scene, object );
+						}
 
-				}
+					}
 
-			} else {
+					var samples = getRenderTargetSamples( renderTarget );
 
-				initMaterial( material, scene, object );
-				materialProperties.__version = material.version;
+					_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
 
-			}
+				} else {
 
-			var refreshProgram = false;
-			var refreshMaterial = false;
-			var refreshLights = false;
+					_gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height );
 
-			var program = materialProperties.program,
-				p_uniforms = program.getUniforms(),
-				m_uniforms = materialProperties.uniforms;
+				}
 
-			if ( state.useProgram( program.program ) ) {
+				_gl.framebufferRenderbuffer( 36160, 36096, 36161, renderbuffer );
 
-				refreshProgram = true;
-				refreshMaterial = true;
-				refreshLights = true;
+			} else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
 
-			}
+				if ( isMultisample ) {
 
-			if ( material.id !== _currentMaterialId ) {
+					var samples = getRenderTargetSamples( renderTarget );
 
-				_currentMaterialId = material.id;
+					_gl.renderbufferStorageMultisample( 36161, samples, 35056, renderTarget.width, renderTarget.height );
 
-				refreshMaterial = true;
+				} else {
 
-			}
+					_gl.renderbufferStorage( 36161, 34041, renderTarget.width, renderTarget.height );
 
-			if ( refreshProgram || _currentCamera !== camera ) {
+				}
 
-				p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
 
-				if ( capabilities.logarithmicDepthBuffer ) {
+				_gl.framebufferRenderbuffer( 36160, 33306, 36161, renderbuffer );
 
-					p_uniforms.setValue( _gl, 'logDepthBufFC',
-						2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
+			} else {
 
-				}
+				var glFormat = utils.convert( renderTarget.texture.format );
+				var glType = utils.convert( renderTarget.texture.type );
+				var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
 
-				if ( _currentCamera !== camera ) {
+				if ( isMultisample ) {
 
-					_currentCamera = camera;
+					var samples = getRenderTargetSamples( renderTarget );
 
-					// lighting uniforms depend on the camera so enforce an update
-					// now, in case this material supports lights - or later, when
-					// the next material that does gets activated:
+					_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
 
-					refreshMaterial = true;		// set to true on material change
-					refreshLights = true;		// remains set until update done
+				} else {
+
+					_gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height );
 
 				}
 
-				// load material specific uniforms
-				// (shader material also gets them for the sake of genericity)
+			}
 
-				if ( material.isShaderMaterial ||
-					material.isMeshPhongMaterial ||
-					material.isMeshToonMaterial ||
-					material.isMeshStandardMaterial ||
-					material.envMap ) {
+			_gl.bindRenderbuffer( 36161, null );
 
-					var uCamPos = p_uniforms.map.cameraPosition;
+		}
 
-					if ( uCamPos !== undefined ) {
+		// Setup resources for a Depth Texture for a FBO (needs an extension)
+		function setupDepthTexture( framebuffer, renderTarget ) {
 
-						uCamPos.setValue( _gl,
-							_vector3.setFromMatrixPosition( camera.matrixWorld ) );
+			var isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget );
+			if ( isCube ) { throw new Error( 'Depth Texture with cube render targets is not supported' ); }
 
-					}
+			_gl.bindFramebuffer( 36160, framebuffer );
 
-				}
+			if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) {
 
-				if ( material.isMeshPhongMaterial ||
-					material.isMeshToonMaterial ||
-					material.isMeshLambertMaterial ||
-					material.isMeshBasicMaterial ||
-					material.isMeshStandardMaterial ||
-					material.isShaderMaterial ) {
+				throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' );
 
-					p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true );
+			}
 
-				}
+			// upload an empty depth texture with framebuffer size
+			if ( ! properties.get( renderTarget.depthTexture ).__webglTexture ||
+					renderTarget.depthTexture.image.width !== renderTarget.width ||
+					renderTarget.depthTexture.image.height !== renderTarget.height ) {
 
-				if ( material.isMeshPhongMaterial ||
-					material.isMeshToonMaterial ||
-					material.isMeshLambertMaterial ||
-					material.isMeshBasicMaterial ||
-					material.isMeshStandardMaterial ||
-					material.isShaderMaterial ||
-					material.skinning ) {
+				renderTarget.depthTexture.image.width = renderTarget.width;
+				renderTarget.depthTexture.image.height = renderTarget.height;
+				renderTarget.depthTexture.needsUpdate = true;
 
-					p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
+			}
 
-				}
+			setTexture2D( renderTarget.depthTexture, 0 );
 
-			}
+			var webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
 
-			// skinning uniforms must be set even if material didn't change
-			// auto-setting of texture unit for bone texture must go before other textures
-			// otherwise textures used for skinning can take over texture units reserved for other material textures
+			if ( renderTarget.depthTexture.format === DepthFormat ) {
 
-			if ( material.skinning ) {
+				_gl.framebufferTexture2D( 36160, 36096, 3553, webglDepthTexture, 0 );
 
-				p_uniforms.setOptional( _gl, object, 'bindMatrix' );
-				p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
+			} else if ( renderTarget.depthTexture.format === DepthStencilFormat ) {
 
-				var skeleton = object.skeleton;
+				_gl.framebufferTexture2D( 36160, 33306, 3553, webglDepthTexture, 0 );
 
-				if ( skeleton ) {
+			} else {
 
-					var bones = skeleton.bones;
+				throw new Error( 'Unknown depthTexture format' );
 
-					if ( capabilities.floatVertexTextures ) {
+			}
 
-						if ( skeleton.boneTexture === undefined ) {
+		}
 
-							// layout (1 matrix = 4 pixels)
-							//      RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
-							//  with  8x8  pixel texture max   16 bones * 4 pixels =  (8 * 8)
-							//       16x16 pixel texture max   64 bones * 4 pixels = (16 * 16)
-							//       32x32 pixel texture max  256 bones * 4 pixels = (32 * 32)
-							//       64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
+		// Setup GL resources for a non-texture depth buffer
+		function setupDepthRenderbuffer( renderTarget ) {
 
+			var renderTargetProperties = properties.get( renderTarget );
 
-							var size = Math.sqrt( bones.length * 4 ); // 4 pixels needed for 1 matrix
-							size = MathUtils.ceilPowerOfTwo( size );
-							size = Math.max( size, 4 );
+			var isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
 
-							var boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
-							boneMatrices.set( skeleton.boneMatrices ); // copy current values
+			if ( renderTarget.depthTexture ) {
 
-							var boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
+				if ( isCube ) { throw new Error( 'target.depthTexture not supported in Cube render targets' ); }
 
-							skeleton.boneMatrices = boneMatrices;
-							skeleton.boneTexture = boneTexture;
-							skeleton.boneTextureSize = size;
+				setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );
 
-						}
+			} else {
 
-						p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures );
-						p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
+				if ( isCube ) {
 
-					} else {
+					renderTargetProperties.__webglDepthbuffer = [];
 
-						p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
+					for ( var i = 0; i < 6; i ++ ) {
+
+						_gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer[ i ] );
+						renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
+						setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false );
 
 					}
 
+				} else {
+
+					_gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer );
+					renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
+					setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false );
+
 				}
 
 			}
 
-			if ( refreshMaterial || materialProperties.receiveShadow !== object.receiveShadow ) {
+			_gl.bindFramebuffer( 36160, null );
 
-				materialProperties.receiveShadow = object.receiveShadow;
-				p_uniforms.setValue( _gl, 'receiveShadow', object.receiveShadow );
+		}
 
-			}
+		// Set up GL resources for the render target
+		function setupRenderTarget( renderTarget ) {
 
-			if ( refreshMaterial ) {
+			var renderTargetProperties = properties.get( renderTarget );
+			var textureProperties = properties.get( renderTarget.texture );
 
-				p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure );
-				p_uniforms.setValue( _gl, 'toneMappingWhitePoint', _this.toneMappingWhitePoint );
-
-				if ( materialProperties.needsLights ) {
-
-					// the current material requires lighting info
-
-					// note: all lighting uniforms are always set correctly
-					// they simply reference the renderer's state for their
-					// values
-					//
-					// use the current material's .needsUpdate flags to set
-					// the GL state when required
+			renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
 
-					markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
+			textureProperties.__webglTexture = _gl.createTexture();
 
-				}
+			info.memory.textures ++;
 
-				// refresh uniforms common to several materials
+			var isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
+			var isMultisample = ( renderTarget.isWebGLMultisampleRenderTarget === true );
+			var supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
 
-				if ( fog && material.fog ) {
+			// Handles WebGL2 RGBFormat fallback - #18858
 
-					refreshUniformsFog( m_uniforms, fog );
+			if ( isWebGL2 && renderTarget.texture.format === RGBFormat && ( renderTarget.texture.type === FloatType || renderTarget.texture.type === HalfFloatType ) ) {
 
-				}
+				renderTarget.texture.format = RGBAFormat;
 
-				if ( material.isMeshBasicMaterial ) {
+				console.warn( 'THREE.WebGLRenderer: Rendering to textures with RGB format is not supported. Using RGBA format instead.' );
 
-					refreshUniformsCommon( m_uniforms, material );
+			}
 
-				} else if ( material.isMeshLambertMaterial ) {
+			// Setup framebuffer
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsLambert( m_uniforms, material );
+			if ( isCube ) {
 
-				} else if ( material.isMeshToonMaterial ) {
+				renderTargetProperties.__webglFramebuffer = [];
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsToon( m_uniforms, material );
+				for ( var i = 0; i < 6; i ++ ) {
 
-				} else if ( material.isMeshPhongMaterial ) {
+					renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsPhong( m_uniforms, material );
+				}
 
-				} else if ( material.isMeshStandardMaterial ) {
+			} else {
 
-					refreshUniformsCommon( m_uniforms, material, environment );
+				renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
 
-					if ( material.isMeshPhysicalMaterial ) {
+				if ( isMultisample ) {
 
-						refreshUniformsPhysical( m_uniforms, material, environment );
+					if ( isWebGL2 ) {
 
-					} else {
+						renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
+						renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer();
 
-						refreshUniformsStandard( m_uniforms, material, environment );
+						_gl.bindRenderbuffer( 36161, renderTargetProperties.__webglColorRenderbuffer );
 
-					}
+						var glFormat = utils.convert( renderTarget.texture.format );
+						var glType = utils.convert( renderTarget.texture.type );
+						var glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType );
+						var samples = getRenderTargetSamples( renderTarget );
+						_gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height );
 
-				} else if ( material.isMeshMatcapMaterial ) {
+						_gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer );
+						_gl.framebufferRenderbuffer( 36160, 36064, 36161, renderTargetProperties.__webglColorRenderbuffer );
+						_gl.bindRenderbuffer( 36161, null );
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsMatcap( m_uniforms, material );
+						if ( renderTarget.depthBuffer ) {
 
-				} else if ( material.isMeshDepthMaterial ) {
+							renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
+							setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true );
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsDepth( m_uniforms, material );
+						}
 
-				} else if ( material.isMeshDistanceMaterial ) {
+						_gl.bindFramebuffer( 36160, null );
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsDistance( m_uniforms, material );
 
-				} else if ( material.isMeshNormalMaterial ) {
+					} else {
 
-					refreshUniformsCommon( m_uniforms, material );
-					refreshUniformsNormal( m_uniforms, material );
+						console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
 
-				} else if ( material.isLineBasicMaterial ) {
+					}
 
-					refreshUniformsLine( m_uniforms, material );
+				}
 
-					if ( material.isLineDashedMaterial ) {
+			}
 
-						refreshUniformsDash( m_uniforms, material );
+			// Setup color buffer
 
-					}
+			if ( isCube ) {
 
-				} else if ( material.isPointsMaterial ) {
+				state.bindTexture( 34067, textureProperties.__webglTexture );
+				setTextureParameters( 34067, renderTarget.texture, supportsMips );
 
-					refreshUniformsPoints( m_uniforms, material );
+				for ( var i = 0; i < 6; i ++ ) {
 
-				} else if ( material.isSpriteMaterial ) {
+					setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, 36064, 34069 + i );
 
-					refreshUniformsSprites( m_uniforms, material );
+				}
 
-				} else if ( material.isShadowMaterial ) {
+				if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
 
-					m_uniforms.color.value.copy( material.color );
-					m_uniforms.opacity.value = material.opacity;
+					generateMipmap( 34067, renderTarget.texture, renderTarget.width, renderTarget.height );
 
 				}
 
-				// RectAreaLight Texture
-				// TODO (mrdoob): Find a nicer implementation
+				state.bindTexture( 34067, null );
 
-				if ( m_uniforms.ltc_1 !== undefined ) { m_uniforms.ltc_1.value = UniformsLib.LTC_1; }
-				if ( m_uniforms.ltc_2 !== undefined ) { m_uniforms.ltc_2.value = UniformsLib.LTC_2; }
+			} else {
 
-				WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
+				state.bindTexture( 3553, textureProperties.__webglTexture );
+				setTextureParameters( 3553, renderTarget.texture, supportsMips );
+				setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, 36064, 3553 );
 
-				if ( material.isShaderMaterial ) {
+				if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) {
 
-					material.uniformsNeedUpdate = false; // #15581
+					generateMipmap( 3553, renderTarget.texture, renderTarget.width, renderTarget.height );
 
 				}
 
-			}
-
-			if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {
-
-				WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
-				material.uniformsNeedUpdate = false;
+				state.bindTexture( 3553, null );
 
 			}
 
-			if ( material.isSpriteMaterial ) {
-
-				p_uniforms.setValue( _gl, 'center', object.center );
-
-			}
+			// Setup depth and stencil buffers
 
-			// common matrices
+			if ( renderTarget.depthBuffer ) {
 
-			p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
-			p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix );
-			p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
+				setupDepthRenderbuffer( renderTarget );
 
-			return program;
+			}
 
 		}
 
-		// Uniforms (refresh uniforms objects)
+		function updateRenderTargetMipmap( renderTarget ) {
 
-		function refreshUniformsCommon( uniforms, material, environment ) {
+			var texture = renderTarget.texture;
+			var supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
 
-			uniforms.opacity.value = material.opacity;
+			if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
 
-			if ( material.color ) {
+				var target = renderTarget.isWebGLCubeRenderTarget ? 34067 : 3553;
+				var webglTexture = properties.get( texture ).__webglTexture;
 
-				uniforms.diffuse.value.copy( material.color );
+				state.bindTexture( target, webglTexture );
+				generateMipmap( target, texture, renderTarget.width, renderTarget.height );
+				state.bindTexture( target, null );
 
 			}
 
-			if ( material.emissive ) {
+		}
 
-				uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
+		function updateMultisampleRenderTarget( renderTarget ) {
 
-			}
+			if ( renderTarget.isWebGLMultisampleRenderTarget ) {
 
-			if ( material.map ) {
+				if ( isWebGL2 ) {
 
-				uniforms.map.value = material.map;
+					var renderTargetProperties = properties.get( renderTarget );
 
-			}
+					_gl.bindFramebuffer( 36008, renderTargetProperties.__webglMultisampledFramebuffer );
+					_gl.bindFramebuffer( 36009, renderTargetProperties.__webglFramebuffer );
 
-			if ( material.alphaMap ) {
+					var width = renderTarget.width;
+					var height = renderTarget.height;
+					var mask = 16384;
 
-				uniforms.alphaMap.value = material.alphaMap;
+					if ( renderTarget.depthBuffer ) { mask |= 256; }
+					if ( renderTarget.stencilBuffer ) { mask |= 1024; }
 
-			}
+					_gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, 9728 );
 
-			if ( material.specularMap ) {
+					_gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer ); // see #18905
 
-				uniforms.specularMap.value = material.specularMap;
+				} else {
+
+					console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
+
+				}
 
 			}
 
-			var envMap = material.envMap || environment;
+		}
 
-			if ( envMap ) {
+		function getRenderTargetSamples( renderTarget ) {
 
-				uniforms.envMap.value = envMap;
+			return ( isWebGL2 && renderTarget.isWebGLMultisampleRenderTarget ) ?
+				Math.min( maxSamples, renderTarget.samples ) : 0;
 
-				uniforms.flipEnvMap.value = envMap.isCubeTexture ? - 1 : 1;
+		}
 
-				uniforms.reflectivity.value = material.reflectivity;
-				uniforms.refractionRatio.value = material.refractionRatio;
+		function updateVideoTexture( texture ) {
 
-				uniforms.maxMipLevel.value = properties.get( envMap ).__maxMipLevel;
+			var frame = info.render.frame;
 
-			}
+			// Check the last frame we updated the VideoTexture
 
-			if ( material.lightMap ) {
+			if ( _videoTextures.get( texture ) !== frame ) {
 
-				uniforms.lightMap.value = material.lightMap;
-				uniforms.lightMapIntensity.value = material.lightMapIntensity;
+				_videoTextures.set( texture, frame );
+				texture.update();
 
 			}
 
-			if ( material.aoMap ) {
+		}
 
-				uniforms.aoMap.value = material.aoMap;
-				uniforms.aoMapIntensity.value = material.aoMapIntensity;
+		// backwards compatibility
 
-			}
+		var warnedTexture2D = false;
+		var warnedTextureCube = false;
 
-			// uv repeat and offset setting priorities
-			// 1. color map
-			// 2. specular map
-			// 3. normal map
-			// 4. bump map
-			// 5. alpha map
-			// 6. emissive map
+		function safeSetTexture2D( texture, slot ) {
 
-			var uvScaleMap;
+			if ( texture && texture.isWebGLRenderTarget ) {
 
-			if ( material.map ) {
+				if ( warnedTexture2D === false ) {
 
-				uvScaleMap = material.map;
+					console.warn( "THREE.WebGLTextures.safeSetTexture2D: don't use render targets as textures. Use their .texture property instead." );
+					warnedTexture2D = true;
 
-			} else if ( material.specularMap ) {
+				}
 
-				uvScaleMap = material.specularMap;
+				texture = texture.texture;
 
-			} else if ( material.displacementMap ) {
+			}
 
-				uvScaleMap = material.displacementMap;
+			setTexture2D( texture, slot );
 
-			} else if ( material.normalMap ) {
+		}
 
-				uvScaleMap = material.normalMap;
+		function safeSetTextureCube( texture, slot ) {
 
-			} else if ( material.bumpMap ) {
+			if ( texture && texture.isWebGLCubeRenderTarget ) {
 
-				uvScaleMap = material.bumpMap;
+				if ( warnedTextureCube === false ) {
 
-			} else if ( material.roughnessMap ) {
+					console.warn( "THREE.WebGLTextures.safeSetTextureCube: don't use cube render targets as textures. Use their .texture property instead." );
+					warnedTextureCube = true;
 
-				uvScaleMap = material.roughnessMap;
+				}
 
-			} else if ( material.metalnessMap ) {
+				texture = texture.texture;
 
-				uvScaleMap = material.metalnessMap;
+			}
 
-			} else if ( material.alphaMap ) {
+			// currently relying on the fact that WebGLCubeRenderTarget.texture is a Texture and NOT a CubeTexture
+			// TODO: unify these code paths
+			if ( ( texture && texture.isCubeTexture ) ||
+				( Array.isArray( texture.image ) && texture.image.length === 6 ) ) {
 
-				uvScaleMap = material.alphaMap;
+				// CompressedTexture can have Array in image :/
 
-			} else if ( material.emissiveMap ) {
+				// this function alone should take care of cube textures
+				setTextureCube( texture, slot );
 
-				uvScaleMap = material.emissiveMap;
+			} else {
+
+				// assumed: texture property of THREE.WebGLCubeRenderTarget
+				setTextureCubeDynamic( texture, slot );
 
 			}
 
-			if ( uvScaleMap !== undefined ) {
+		}
 
-				// backwards compatibility
-				if ( uvScaleMap.isWebGLRenderTarget ) {
+		//
 
-					uvScaleMap = uvScaleMap.texture;
+		this.allocateTextureUnit = allocateTextureUnit;
+		this.resetTextureUnits = resetTextureUnits;
 
-				}
+		this.setTexture2D = setTexture2D;
+		this.setTexture2DArray = setTexture2DArray;
+		this.setTexture3D = setTexture3D;
+		this.setTextureCube = setTextureCube;
+		this.setTextureCubeDynamic = setTextureCubeDynamic;
+		this.setupRenderTarget = setupRenderTarget;
+		this.updateRenderTargetMipmap = updateRenderTargetMipmap;
+		this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
 
-				if ( uvScaleMap.matrixAutoUpdate === true ) {
+		this.safeSetTexture2D = safeSetTexture2D;
+		this.safeSetTextureCube = safeSetTextureCube;
 
-					uvScaleMap.updateMatrix();
+	}
 
-				}
+	/**
+	 * @author thespite / http://www.twitter.com/thespite
+	 */
 
-				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
+	function WebGLUtils( gl, extensions, capabilities ) {
 
-			}
+		var isWebGL2 = capabilities.isWebGL2;
 
-			// uv repeat and offset setting priorities for uv2
-			// 1. ao map
-			// 2. light map
+		function convert( p ) {
 
-			var uv2ScaleMap;
+			var extension;
 
-			if ( material.aoMap ) {
+			if ( p === UnsignedByteType ) { return 5121; }
+			if ( p === UnsignedShort4444Type ) { return 32819; }
+			if ( p === UnsignedShort5551Type ) { return 32820; }
+			if ( p === UnsignedShort565Type ) { return 33635; }
 
-				uv2ScaleMap = material.aoMap;
+			if ( p === ByteType ) { return 5120; }
+			if ( p === ShortType ) { return 5122; }
+			if ( p === UnsignedShortType ) { return 5123; }
+			if ( p === IntType ) { return 5124; }
+			if ( p === UnsignedIntType ) { return 5125; }
+			if ( p === FloatType ) { return 5126; }
 
-			} else if ( material.lightMap ) {
+			if ( p === HalfFloatType ) {
 
-				uv2ScaleMap = material.lightMap;
+				if ( isWebGL2 ) { return 5131; }
 
-			}
+				extension = extensions.get( 'OES_texture_half_float' );
 
-			if ( uv2ScaleMap !== undefined ) {
+				if ( extension !== null ) {
 
-				// backwards compatibility
-				if ( uv2ScaleMap.isWebGLRenderTarget ) {
+					return extension.HALF_FLOAT_OES;
 
-					uv2ScaleMap = uv2ScaleMap.texture;
+				} else {
+
+					return null;
 
 				}
 
-				if ( uv2ScaleMap.matrixAutoUpdate === true ) {
+			}
 
-					uv2ScaleMap.updateMatrix();
+			if ( p === AlphaFormat ) { return 6406; }
+			if ( p === RGBFormat ) { return 6407; }
+			if ( p === RGBAFormat ) { return 6408; }
+			if ( p === LuminanceFormat ) { return 6409; }
+			if ( p === LuminanceAlphaFormat ) { return 6410; }
+			if ( p === DepthFormat ) { return 6402; }
+			if ( p === DepthStencilFormat ) { return 34041; }
+			if ( p === RedFormat ) { return 6403; }
 
-				}
+			// WebGL2 formats.
 
-				uniforms.uv2Transform.value.copy( uv2ScaleMap.matrix );
+			if ( p === RedIntegerFormat ) { return 36244; }
+			if ( p === RGFormat ) { return 33319; }
+			if ( p === RGIntegerFormat ) { return 33320; }
+			if ( p === RGBIntegerFormat ) { return 36248; }
+			if ( p === RGBAIntegerFormat ) { return 36249; }
 
-			}
+			if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format ||
+				p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) {
 
-		}
+				extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
 
-		function refreshUniformsLine( uniforms, material ) {
+				if ( extension !== null ) {
 
-			uniforms.diffuse.value.copy( material.color );
-			uniforms.opacity.value = material.opacity;
+					if ( p === RGB_S3TC_DXT1_Format ) { return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; }
+					if ( p === RGBA_S3TC_DXT1_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; }
+					if ( p === RGBA_S3TC_DXT3_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; }
+					if ( p === RGBA_S3TC_DXT5_Format ) { return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; }
 
-		}
+				} else {
 
-		function refreshUniformsDash( uniforms, material ) {
+					return null;
 
-			uniforms.dashSize.value = material.dashSize;
-			uniforms.totalSize.value = material.dashSize + material.gapSize;
-			uniforms.scale.value = material.scale;
+				}
 
-		}
+			}
 
-		function refreshUniformsPoints( uniforms, material ) {
+			if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format ||
+				p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) {
 
-			uniforms.diffuse.value.copy( material.color );
-			uniforms.opacity.value = material.opacity;
-			uniforms.size.value = material.size * _pixelRatio;
-			uniforms.scale.value = _height * 0.5;
+				extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
 
-			if ( material.map ) {
+				if ( extension !== null ) {
 
-				uniforms.map.value = material.map;
+					if ( p === RGB_PVRTC_4BPPV1_Format ) { return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; }
+					if ( p === RGB_PVRTC_2BPPV1_Format ) { return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; }
+					if ( p === RGBA_PVRTC_4BPPV1_Format ) { return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; }
+					if ( p === RGBA_PVRTC_2BPPV1_Format ) { return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; }
 
-			}
+				} else {
 
-			if ( material.alphaMap ) {
+					return null;
 
-				uniforms.alphaMap.value = material.alphaMap;
+				}
 
 			}
 
-			// uv repeat and offset setting priorities
-			// 1. color map
-			// 2. alpha map
+			if ( p === RGB_ETC1_Format ) {
 
-			var uvScaleMap;
+				extension = extensions.get( 'WEBGL_compressed_texture_etc1' );
 
-			if ( material.map ) {
+				if ( extension !== null ) {
 
-				uvScaleMap = material.map;
+					return extension.COMPRESSED_RGB_ETC1_WEBGL;
 
-			} else if ( material.alphaMap ) {
+				} else {
 
-				uvScaleMap = material.alphaMap;
+					return null;
+
+				}
 
 			}
 
-			if ( uvScaleMap !== undefined ) {
+			if ( p === RGB_ETC2_Format || p === RGBA_ETC2_EAC_Format ) {
 
-				if ( uvScaleMap.matrixAutoUpdate === true ) {
+				extension = extensions.get( 'WEBGL_compressed_texture_etc' );
 
-					uvScaleMap.updateMatrix();
+				if ( extension !== null ) {
 
-				}
+					if ( p === RGB_ETC2_Format ) { return extension.COMPRESSED_RGB8_ETC2; }
+					if ( p === RGBA_ETC2_EAC_Format ) { return extension.COMPRESSED_RGBA8_ETC2_EAC; }
 
-				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
+				}
 
 			}
 
-		}
+			if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format ||
+				p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format ||
+				p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format ||
+				p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format ||
+				p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format ||
+				p === SRGB8_ALPHA8_ASTC_4x4_Format || p === SRGB8_ALPHA8_ASTC_5x4_Format || p === SRGB8_ALPHA8_ASTC_5x5_Format ||
+				p === SRGB8_ALPHA8_ASTC_6x5_Format || p === SRGB8_ALPHA8_ASTC_6x6_Format || p === SRGB8_ALPHA8_ASTC_8x5_Format ||
+				p === SRGB8_ALPHA8_ASTC_8x6_Format || p === SRGB8_ALPHA8_ASTC_8x8_Format || p === SRGB8_ALPHA8_ASTC_10x5_Format ||
+				p === SRGB8_ALPHA8_ASTC_10x6_Format || p === SRGB8_ALPHA8_ASTC_10x8_Format || p === SRGB8_ALPHA8_ASTC_10x10_Format ||
+				p === SRGB8_ALPHA8_ASTC_12x10_Format || p === SRGB8_ALPHA8_ASTC_12x12_Format ) {
 
-		function refreshUniformsSprites( uniforms, material ) {
+				extension = extensions.get( 'WEBGL_compressed_texture_astc' );
 
-			uniforms.diffuse.value.copy( material.color );
-			uniforms.opacity.value = material.opacity;
-			uniforms.rotation.value = material.rotation;
+				if ( extension !== null ) {
 
-			if ( material.map ) {
+					// TODO Complete?
 
-				uniforms.map.value = material.map;
+					return p;
 
-			}
+				} else {
 
-			if ( material.alphaMap ) {
+					return null;
 
-				uniforms.alphaMap.value = material.alphaMap;
+				}
 
 			}
 
-			// uv repeat and offset setting priorities
-			// 1. color map
-			// 2. alpha map
-
-			var uvScaleMap;
-
-			if ( material.map ) {
-
-				uvScaleMap = material.map;
+			if ( p === RGBA_BPTC_Format ) {
 
-			} else if ( material.alphaMap ) {
+				extension = extensions.get( 'EXT_texture_compression_bptc' );
 
-				uvScaleMap = material.alphaMap;
+				if ( extension !== null ) {
 
-			}
+					// TODO Complete?
 
-			if ( uvScaleMap !== undefined ) {
+					return p;
 
-				if ( uvScaleMap.matrixAutoUpdate === true ) {
+				} else {
 
-					uvScaleMap.updateMatrix();
+					return null;
 
 				}
 
-				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
-
 			}
 
-		}
+			if ( p === UnsignedInt248Type ) {
 
-		function refreshUniformsFog( uniforms, fog ) {
+				if ( isWebGL2 ) { return 34042; }
 
-			uniforms.fogColor.value.copy( fog.color );
+				extension = extensions.get( 'WEBGL_depth_texture' );
 
-			if ( fog.isFog ) {
+				if ( extension !== null ) {
 
-				uniforms.fogNear.value = fog.near;
-				uniforms.fogFar.value = fog.far;
+					return extension.UNSIGNED_INT_24_8_WEBGL;
 
-			} else if ( fog.isFogExp2 ) {
+				} else {
 
-				uniforms.fogDensity.value = fog.density;
+					return null;
+
+				}
 
 			}
 
 		}
 
-		function refreshUniformsLambert( uniforms, material ) {
+		return { convert: convert };
 
-			if ( material.emissiveMap ) {
+	}
 
-				uniforms.emissiveMap.value = material.emissiveMap;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			}
+	function ArrayCamera( array ) {
 
-		}
+		PerspectiveCamera.call( this );
 
-		function refreshUniformsPhong( uniforms, material ) {
+		this.cameras = array || [];
 
-			uniforms.specular.value.copy( material.specular );
-			uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
-
-			if ( material.emissiveMap ) {
+	}
 
-				uniforms.emissiveMap.value = material.emissiveMap;
+	ArrayCamera.prototype = Object.assign( Object.create( PerspectiveCamera.prototype ), {
 
-			}
+		constructor: ArrayCamera,
 
-			if ( material.bumpMap ) {
+		isArrayCamera: true
 
-				uniforms.bumpMap.value = material.bumpMap;
-				uniforms.bumpScale.value = material.bumpScale;
-				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			if ( material.normalMap ) {
+	function Group() {
 
-				uniforms.normalMap.value = material.normalMap;
-				uniforms.normalScale.value.copy( material.normalScale );
-				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
+		Object3D.call( this );
 
-			}
+		this.type = 'Group';
 
-			if ( material.displacementMap ) {
+	}
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+	Group.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-			}
+		constructor: Group,
 
-		}
+		isGroup: true
 
-		function refreshUniformsToon( uniforms, material ) {
+	} );
 
-			uniforms.specular.value.copy( material.specular );
-			uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
+	/**
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-			if ( material.gradientMap ) {
+	function WebXRController() {
 
-				uniforms.gradientMap.value = material.gradientMap;
+		this._targetRay = null;
+		this._grip = null;
 
-			}
+	}
 
-			if ( material.emissiveMap ) {
+	Object.assign( WebXRController.prototype, {
 
-				uniforms.emissiveMap.value = material.emissiveMap;
+		constructor: WebXRController,
 
-			}
+		getTargetRaySpace: function () {
 
-			if ( material.bumpMap ) {
+			if ( this._targetRay === null ) {
 
-				uniforms.bumpMap.value = material.bumpMap;
-				uniforms.bumpScale.value = material.bumpScale;
-				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
+				this._targetRay = new Group();
+				this._targetRay.matrixAutoUpdate = false;
+				this._targetRay.visible = false;
 
 			}
 
-			if ( material.normalMap ) {
+			return this._targetRay;
 
-				uniforms.normalMap.value = material.normalMap;
-				uniforms.normalScale.value.copy( material.normalScale );
-				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
+		},
 
-			}
+		getGripSpace: function () {
 
-			if ( material.displacementMap ) {
+			if ( this._grip === null ) {
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+				this._grip = new Group();
+				this._grip.matrixAutoUpdate = false;
+				this._grip.visible = false;
 
 			}
 
-		}
-
-		function refreshUniformsStandard( uniforms, material, environment ) {
-
-			uniforms.roughness.value = material.roughness;
-			uniforms.metalness.value = material.metalness;
-
-			if ( material.roughnessMap ) {
+			return this._grip;
 
-				uniforms.roughnessMap.value = material.roughnessMap;
+		},
 
-			}
+		dispatchEvent: function ( event ) {
 
-			if ( material.metalnessMap ) {
+			if ( this._targetRay !== null ) {
 
-				uniforms.metalnessMap.value = material.metalnessMap;
+				this._targetRay.dispatchEvent( event );
 
 			}
 
-			if ( material.emissiveMap ) {
+			if ( this._grip !== null ) {
 
-				uniforms.emissiveMap.value = material.emissiveMap;
+				this._grip.dispatchEvent( event );
 
 			}
 
-			if ( material.bumpMap ) {
-
-				uniforms.bumpMap.value = material.bumpMap;
-				uniforms.bumpScale.value = material.bumpScale;
-				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
-
-			}
+			return this;
 
-			if ( material.normalMap ) {
+		},
 
-				uniforms.normalMap.value = material.normalMap;
-				uniforms.normalScale.value.copy( material.normalScale );
-				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
+		disconnect: function ( inputSource ) {
 
-			}
+			this.dispatchEvent( { type: 'disconnected', data: inputSource } );
 
-			if ( material.displacementMap ) {
+			if ( this._targetRay !== null ) {
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+				this._targetRay.visible = false;
 
 			}
 
-			if ( material.envMap || environment ) {
+			if ( this._grip !== null ) {
 
-				//uniforms.envMap.value = material.envMap; // part of uniforms common
-				uniforms.envMapIntensity.value = material.envMapIntensity;
+				this._grip.visible = false;
 
 			}
 
-		}
-
-		function refreshUniformsPhysical( uniforms, material, environment ) {
-
-			refreshUniformsStandard( uniforms, material, environment );
-
-			uniforms.reflectivity.value = material.reflectivity; // also part of uniforms common
-
-			uniforms.clearcoat.value = material.clearcoat;
-			uniforms.clearcoatRoughness.value = material.clearcoatRoughness;
-			if ( material.sheen ) { uniforms.sheen.value.copy( material.sheen ); }
+			return this;
 
-			if ( material.clearcoatMap ) {
+		},
 
-				uniforms.clearcoatMap.value = material.clearcoatMap;
+		update: function ( inputSource, frame, referenceSpace ) {
 
-			}
+			var inputPose = null;
+			var gripPose = null;
 
-			if ( material.clearcoatRoughnessMap ) {
+			var targetRay = this._targetRay;
+			var grip = this._grip;
 
-				uniforms.clearcoatRoughnessMap.value = material.clearcoatRoughnessMap;
+			if ( inputSource ) {
 
-			}
+				if ( targetRay !== null ) {
 
-			if ( material.clearcoatNormalMap ) {
+					inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
 
-				uniforms.clearcoatNormalScale.value.copy( material.clearcoatNormalScale );
-				uniforms.clearcoatNormalMap.value = material.clearcoatNormalMap;
+					if ( inputPose !== null ) {
 
-				if ( material.side === BackSide ) {
+						targetRay.matrix.fromArray( inputPose.transform.matrix );
+						targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
 
-					uniforms.clearcoatNormalScale.value.negate();
+					}
 
 				}
 
-			}
+				if ( grip !== null && inputSource.gripSpace ) {
 
-			uniforms.transparency.value = material.transparency;
+					gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
 
-		}
+					if ( gripPose !== null ) {
 
-		function refreshUniformsMatcap( uniforms, material ) {
+						grip.matrix.fromArray( gripPose.transform.matrix );
+						grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
 
-			if ( material.matcap ) {
+					}
 
-				uniforms.matcap.value = material.matcap;
+				}
 
 			}
 
-			if ( material.bumpMap ) {
+			if ( targetRay !== null ) {
 
-				uniforms.bumpMap.value = material.bumpMap;
-				uniforms.bumpScale.value = material.bumpScale;
-				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
+				targetRay.visible = ( inputPose !== null );
 
 			}
 
-			if ( material.normalMap ) {
+			if ( grip !== null ) {
 
-				uniforms.normalMap.value = material.normalMap;
-				uniforms.normalScale.value.copy( material.normalScale );
-				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
+				grip.visible = ( gripPose !== null );
 
 			}
 
-			if ( material.displacementMap ) {
+			return this;
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+		}
 
-			}
+	} );
 
-		}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		function refreshUniformsDepth( uniforms, material ) {
+	function WebXRManager( renderer, gl ) {
 
-			if ( material.displacementMap ) {
+		var scope = this;
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+		var session = null;
 
-			}
+		var framebufferScaleFactor = 1.0;
 
-		}
+		var referenceSpace = null;
+		var referenceSpaceType = 'local-floor';
 
-		function refreshUniformsDistance( uniforms, material ) {
+		var pose = null;
 
-			if ( material.displacementMap ) {
+		var controllers = [];
+		var inputSourcesMap = new Map();
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+		//
 
-			}
+		var cameraL = new PerspectiveCamera();
+		cameraL.layers.enable( 1 );
+		cameraL.viewport = new Vector4();
 
-			uniforms.referencePosition.value.copy( material.referencePosition );
-			uniforms.nearDistance.value = material.nearDistance;
-			uniforms.farDistance.value = material.farDistance;
+		var cameraR = new PerspectiveCamera();
+		cameraR.layers.enable( 2 );
+		cameraR.viewport = new Vector4();
 
-		}
+		var cameras = [ cameraL, cameraR ];
 
-		function refreshUniformsNormal( uniforms, material ) {
+		var cameraVR = new ArrayCamera();
+		cameraVR.layers.enable( 1 );
+		cameraVR.layers.enable( 2 );
 
-			if ( material.bumpMap ) {
+		var _currentDepthNear = null;
+		var _currentDepthFar = null;
 
-				uniforms.bumpMap.value = material.bumpMap;
-				uniforms.bumpScale.value = material.bumpScale;
-				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
+		//
 
-			}
+		this.enabled = false;
 
-			if ( material.normalMap ) {
+		this.isPresenting = false;
 
-				uniforms.normalMap.value = material.normalMap;
-				uniforms.normalScale.value.copy( material.normalScale );
-				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
+		this.getController = function ( index ) {
 
-			}
+			var controller = controllers[ index ];
 
-			if ( material.displacementMap ) {
+			if ( controller === undefined ) {
 
-				uniforms.displacementMap.value = material.displacementMap;
-				uniforms.displacementScale.value = material.displacementScale;
-				uniforms.displacementBias.value = material.displacementBias;
+				controller = new WebXRController();
+				controllers[ index ] = controller;
 
 			}
 
-		}
+			return controller.getTargetRaySpace();
 
-		// If uniforms are marked as clean, they don't need to be loaded to the GPU.
+		};
 
-		function markUniformsLightsNeedsUpdate( uniforms, value ) {
+		this.getControllerGrip = function ( index ) {
 
-			uniforms.ambientLightColor.needsUpdate = value;
-			uniforms.lightProbe.needsUpdate = value;
+			var controller = controllers[ index ];
 
-			uniforms.directionalLights.needsUpdate = value;
-			uniforms.directionalLightShadows.needsUpdate = value;
-			uniforms.pointLights.needsUpdate = value;
-			uniforms.pointLightShadows.needsUpdate = value;
-			uniforms.spotLights.needsUpdate = value;
-			uniforms.spotLightShadows.needsUpdate = value;
-			uniforms.rectAreaLights.needsUpdate = value;
-			uniforms.hemisphereLights.needsUpdate = value;
+			if ( controller === undefined ) {
 
-		}
+				controller = new WebXRController();
+				controllers[ index ] = controller;
 
-		function materialNeedsLights( material ) {
+			}
 
-			return material.isMeshLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial ||
-				material.isMeshStandardMaterial || material.isShadowMaterial ||
-				( material.isShaderMaterial && material.lights === true );
+			return controller.getGripSpace();
 
-		}
+		};
 
 		//
-		this.setFramebuffer = function ( value ) {
-
-			if ( _framebuffer !== value && _currentRenderTarget === null ) { _gl.bindFramebuffer( 36160, value ); }
 
-			_framebuffer = value;
+		function onSessionEvent( event ) {
 
-		};
+			var controller = inputSourcesMap.get( event.inputSource );
 
-		this.getActiveCubeFace = function () {
+			if ( controller ) {
 
-			return _currentActiveCubeFace;
+				controller.dispatchEvent( { type: event.type } );
 
-		};
+			}
 
-		this.getActiveMipmapLevel = function () {
+		}
 
-			return _currentActiveMipmapLevel;
+		function onSessionEnd() {
 
-		};
+			inputSourcesMap.forEach( function ( controller, inputSource ) {
 
-		this.getRenderTarget = function () {
+				controller.disconnect( inputSource );
 
-			return _currentRenderTarget;
+			} );
 
-		};
+			inputSourcesMap.clear();
 
-		this.setRenderTarget = function ( renderTarget, activeCubeFace, activeMipmapLevel ) {
+			//
 
-			_currentRenderTarget = renderTarget;
-			_currentActiveCubeFace = activeCubeFace;
-			_currentActiveMipmapLevel = activeMipmapLevel;
+			renderer.setFramebuffer( null );
+			renderer.setRenderTarget( renderer.getRenderTarget() ); // Hack #15830
+			animation.stop();
 
-			if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
+			scope.isPresenting = false;
 
-				textures.setupRenderTarget( renderTarget );
+			scope.dispatchEvent( { type: 'sessionend' } );
 
-			}
+		}
 
-			var framebuffer = _framebuffer;
-			var isCube = false;
+		function onRequestReferenceSpace( value ) {
 
-			if ( renderTarget ) {
+			referenceSpace = value;
 
-				var __webglFramebuffer = properties.get( renderTarget ).__webglFramebuffer;
+			animation.setContext( session );
+			animation.start();
 
-				if ( renderTarget.isWebGLCubeRenderTarget ) {
+			scope.isPresenting = true;
 
-					framebuffer = __webglFramebuffer[ activeCubeFace || 0 ];
-					isCube = true;
+			scope.dispatchEvent( { type: 'sessionstart' } );
 
-				} else if ( renderTarget.isWebGLMultisampleRenderTarget ) {
+		}
 
-					framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer;
+		this.setFramebufferScaleFactor = function ( value ) {
 
-				} else {
+			framebufferScaleFactor = value;
 
-					framebuffer = __webglFramebuffer;
+			if ( scope.isPresenting === true ) {
 
-				}
+				console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting.' );
 
-				_currentViewport.copy( renderTarget.viewport );
-				_currentScissor.copy( renderTarget.scissor );
-				_currentScissorTest = renderTarget.scissorTest;
+			}
 
-			} else {
+		};
 
-				_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor();
-				_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor();
-				_currentScissorTest = _scissorTest;
+		this.setReferenceSpaceType = function ( value ) {
 
-			}
+			referenceSpaceType = value;
 
-			if ( _currentFramebuffer !== framebuffer ) {
+			if ( scope.isPresenting === true ) {
 
-				_gl.bindFramebuffer( 36160, framebuffer );
-				_currentFramebuffer = framebuffer;
+				console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting.' );
 
 			}
 
-			state.viewport( _currentViewport );
-			state.scissor( _currentScissor );
-			state.setScissorTest( _currentScissorTest );
-
-			if ( isCube ) {
+		};
 
-				var textureProperties = properties.get( renderTarget.texture );
-				_gl.framebufferTexture2D( 36160, 36064, 34069 + ( activeCubeFace || 0 ), textureProperties.__webglTexture, activeMipmapLevel || 0 );
+		this.getReferenceSpace = function () {
 
-			}
+			return referenceSpace;
 
 		};
 
-		this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
+		this.getSession = function () {
 
-			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
+			return session;
 
-				console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
-				return;
+		};
 
-			}
+		this.setSession = function ( value ) {
 
-			var framebuffer = properties.get( renderTarget ).__webglFramebuffer;
+			session = value;
 
-			if ( renderTarget.isWebGLCubeRenderTarget && activeCubeFaceIndex !== undefined ) {
+			if ( session !== null ) {
 
-				framebuffer = framebuffer[ activeCubeFaceIndex ];
+				session.addEventListener( 'select', onSessionEvent );
+				session.addEventListener( 'selectstart', onSessionEvent );
+				session.addEventListener( 'selectend', onSessionEvent );
+				session.addEventListener( 'squeeze', onSessionEvent );
+				session.addEventListener( 'squeezestart', onSessionEvent );
+				session.addEventListener( 'squeezeend', onSessionEvent );
+				session.addEventListener( 'end', onSessionEnd );
 
-			}
+				var attributes = gl.getContextAttributes();
 
-			if ( framebuffer ) {
+				var layerInit = {
+					antialias: attributes.antialias,
+					alpha: attributes.alpha,
+					depth: attributes.depth,
+					stencil: attributes.stencil,
+					framebufferScaleFactor: framebufferScaleFactor
+				};
 
-				var restore = false;
+				// eslint-disable-next-line no-undef
+				var baseLayer = new XRWebGLLayer( session, gl, layerInit );
 
-				if ( framebuffer !== _currentFramebuffer ) {
+				session.updateRenderState( { baseLayer: baseLayer } );
 
-					_gl.bindFramebuffer( 36160, framebuffer );
+				session.requestReferenceSpace( referenceSpaceType ).then( onRequestReferenceSpace );
 
-					restore = true;
+				//
 
-				}
+				session.addEventListener( 'inputsourceschange', updateInputSources );
 
-				try {
+			}
 
-					var texture = renderTarget.texture;
-					var textureFormat = texture.format;
-					var textureType = texture.type;
+		};
 
-					if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( 35739 ) ) {
+		function updateInputSources( event ) {
 
-						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
-						return;
+			var inputSources = session.inputSources;
 
-					}
+			// Assign inputSources to available controllers
 
-					if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( 35738 ) && // IE11, Edge and Chrome Mac < 52 (#9513)
-						! ( textureType === FloatType && ( capabilities.isWebGL2 || extensions.get( 'OES_texture_float' ) || extensions.get( 'WEBGL_color_buffer_float' ) ) ) && // Chrome Mac >= 52 and Firefox
-						! ( textureType === HalfFloatType && ( capabilities.isWebGL2 ? extensions.get( 'EXT_color_buffer_float' ) : extensions.get( 'EXT_color_buffer_half_float' ) ) ) ) {
+			for ( var i = 0; i < controllers.length; i ++ ) {
 
-						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
-						return;
+				inputSourcesMap.set( inputSources[ i ], controllers[ i ] );
 
-					}
+			}
 
-					if ( _gl.checkFramebufferStatus( 36160 ) === 36053 ) {
+			// Notify disconnected
 
-						// the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604)
+			for ( var i = 0; i < event.removed.length; i ++ ) {
 
-						if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
+				var inputSource = event.removed[ i ];
+				var controller = inputSourcesMap.get( inputSource );
 
-							_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
+				if ( controller ) {
 
-						}
+					controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
+					inputSourcesMap.delete( inputSource );
 
-					} else {
+				}
 
-						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );
+			}
 
-					}
+			// Notify connected
 
-				} finally {
+			for ( var i = 0; i < event.added.length; i ++ ) {
 
-					if ( restore ) {
+				var inputSource = event.added[ i ];
+				var controller = inputSourcesMap.get( inputSource );
 
-						_gl.bindFramebuffer( 36160, _currentFramebuffer );
+				if ( controller ) {
 
-					}
+					controller.dispatchEvent( { type: 'connected', data: inputSource } );
 
 				}
 
 			}
 
-		};
-
-		this.copyFramebufferToTexture = function ( position, texture, level ) {
+		}
 
-			if ( level === undefined ) { level = 0; }
+		//
 
-			var levelScale = Math.pow( 2, - level );
-			var width = Math.floor( texture.image.width * levelScale );
-			var height = Math.floor( texture.image.height * levelScale );
-			var glFormat = utils.convert( texture.format );
+		var cameraLPos = new Vector3();
+		var cameraRPos = new Vector3();
 
-			textures.setTexture2D( texture, 0 );
+		/**
+		 * @author jsantell / https://www.jsantell.com/
+		 *
+		 * Assumes 2 cameras that are parallel and share an X-axis, and that
+		 * the cameras' projection and world matrices have already been set.
+		 * And that near and far planes are identical for both cameras.
+		 * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
+		 */
+		function setProjectionFromUnion( camera, cameraL, cameraR ) {
 
-			_gl.copyTexImage2D( 3553, level, glFormat, position.x, position.y, width, height, 0 );
+			cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
+			cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
 
-			state.unbindTexture();
+			var ipd = cameraLPos.distanceTo( cameraRPos );
 
-		};
+			var projL = cameraL.projectionMatrix.elements;
+			var projR = cameraR.projectionMatrix.elements;
 
-		this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level ) {
+			// VR systems will have identical far and near planes, and
+			// most likely identical top and bottom frustum extents.
+			// Use the left camera for these values.
+			var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
+			var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
+			var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
+			var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
 
-			if ( level === undefined ) { level = 0; }
+			var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
+			var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
+			var left = near * leftFov;
+			var right = near * rightFov;
 
-			var width = srcTexture.image.width;
-			var height = srcTexture.image.height;
-			var glFormat = utils.convert( dstTexture.format );
-			var glType = utils.convert( dstTexture.type );
+			// Calculate the new camera's position offset from the
+			// left camera. xOffset should be roughly half `ipd`.
+			var zOffset = ipd / ( - leftFov + rightFov );
+			var xOffset = zOffset * - leftFov;
 
-			textures.setTexture2D( dstTexture, 0 );
+			// TODO: Better way to apply this offset?
+			cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
+			camera.translateX( xOffset );
+			camera.translateZ( zOffset );
+			camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
+			camera.matrixWorldInverse.getInverse( camera.matrixWorld );
 
-			if ( srcTexture.isDataTexture ) {
+			// Find the union of the frustum values of the cameras and scale
+			// the values so that the near plane's position does not change in world space,
+			// although must now be relative to the new union camera.
+			var near2 = near + zOffset;
+			var far2 = far + zOffset;
+			var left2 = left - xOffset;
+			var right2 = right + ( ipd - xOffset );
+			var top2 = topFov * far / far2 * near2;
+			var bottom2 = bottomFov * far / far2 * near2;
 
-				_gl.texSubImage2D( 3553, level, position.x, position.y, width, height, glFormat, glType, srcTexture.image.data );
+			camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
 
-			} else {
+		}
 
-				if ( srcTexture.isCompressedTexture ) {
+		function updateCamera( camera, parent ) {
 
-					_gl.compressedTexSubImage2D( 3553, level, position.x, position.y, srcTexture.mipmaps[ 0 ].width, srcTexture.mipmaps[ 0 ].height, glFormat, srcTexture.mipmaps[ 0 ].data );
+			if ( parent === null ) {
 
-				} else {
+				camera.matrixWorld.copy( camera.matrix );
 
-					_gl.texSubImage2D( 3553, level, position.x, position.y, glFormat, glType, srcTexture.image );
+			} else {
 
-				}
+				camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
 
 			}
 
-			// Generate mipmaps only when copying level 0
-			if ( level === 0 && dstTexture.generateMipmaps ) { _gl.generateMipmap( 3553 ); }
+			camera.matrixWorldInverse.getInverse( camera.matrixWorld );
 
-			state.unbindTexture();
+		}
 
-		};
+		this.getCamera = function ( camera ) {
 
-		this.initTexture = function ( texture ) {
+			cameraVR.near = cameraR.near = cameraL.near = camera.near;
+			cameraVR.far = cameraR.far = cameraL.far = camera.far;
 
-			textures.setTexture2D( texture, 0 );
+			if ( _currentDepthNear !== cameraVR.near || _currentDepthFar !== cameraVR.far ) {
 
-			state.unbindTexture();
+				// Note that the new renderState won't apply until the next frame. See #18320
 
-		};
+				session.updateRenderState( {
+					depthNear: cameraVR.near,
+					depthFar: cameraVR.far
+				} );
 
-		if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
+				_currentDepthNear = cameraVR.near;
+				_currentDepthFar = cameraVR.far;
 
-			__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef
+			}
 
-		}
+			var parent = camera.parent;
+			var cameras = cameraVR.cameras;
 
-	}
+			updateCamera( cameraVR, parent );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+			for ( var i = 0; i < cameras.length; i ++ ) {
 
-	function FogExp2( color, density ) {
+				updateCamera( cameras[ i ], parent );
 
-		this.name = '';
+			}
 
-		this.color = new Color( color );
-		this.density = ( density !== undefined ) ? density : 0.00025;
+			// update camera and its children
 
-	}
+			camera.matrixWorld.copy( cameraVR.matrixWorld );
 
-	Object.assign( FogExp2.prototype, {
+			var children = camera.children;
 
-		isFogExp2: true,
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-		clone: function () {
+				children[ i ].updateMatrixWorld( true );
 
-			return new FogExp2( this.color, this.density );
+			}
 
-		},
+			// update projection matrix for proper view frustum culling
 
-		toJSON: function ( /* meta */ ) {
+			if ( cameras.length === 2 ) {
 
-			return {
-				type: 'FogExp2',
-				color: this.color.getHex(),
-				density: this.density
-			};
+				setProjectionFromUnion( cameraVR, cameraL, cameraR );
 
-		}
+			} else {
 
-	} );
+				// assume single camera setup (AR)
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+				cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
 
-	function Fog( color, near, far ) {
+			}
 
-		this.name = '';
+			return cameraVR;
 
-		this.color = new Color( color );
+		};
 
-		this.near = ( near !== undefined ) ? near : 1;
-		this.far = ( far !== undefined ) ? far : 1000;
+		// Animation Loop
 
-	}
+		var onAnimationFrameCallback = null;
 
-	Object.assign( Fog.prototype, {
+		function onAnimationFrame( time, frame ) {
 
-		isFog: true,
+			pose = frame.getViewerPose( referenceSpace );
 
-		clone: function () {
+			if ( pose !== null ) {
 
-			return new Fog( this.color, this.near, this.far );
+				var views = pose.views;
+				var baseLayer = session.renderState.baseLayer;
 
-		},
+				renderer.setFramebuffer( baseLayer.framebuffer );
 
-		toJSON: function ( /* meta */ ) {
+				var cameraVRNeedsUpdate = false;
 
-			return {
-				type: 'Fog',
-				color: this.color.getHex(),
-				near: this.near,
-				far: this.far
-			};
+				// check if it's necessary to rebuild cameraVR's camera list
 
-		}
+				if ( views.length !== cameraVR.cameras.length ) {
 
-	} );
+					cameraVR.cameras.length = 0;
+					cameraVRNeedsUpdate = true;
 
-	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
-	 */
+				}
 
-	function InterleavedBuffer( array, stride ) {
+				for ( var i = 0; i < views.length; i ++ ) {
 
-		this.array = array;
-		this.stride = stride;
-		this.count = array !== undefined ? array.length / stride : 0;
+					var view = views[ i ];
+					var viewport = baseLayer.getViewport( view );
 
-		this.usage = StaticDrawUsage;
-		this.updateRange = { offset: 0, count: - 1 };
+					var camera = cameras[ i ];
+					camera.matrix.fromArray( view.transform.matrix );
+					camera.projectionMatrix.fromArray( view.projectionMatrix );
+					camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
 
-		this.version = 0;
+					if ( i === 0 ) {
 
-	}
+						cameraVR.matrix.copy( camera.matrix );
 
-	Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', {
+					}
 
-		set: function ( value ) {
+					if ( cameraVRNeedsUpdate === true ) {
 
-			if ( value === true ) { this.version ++; }
+						cameraVR.cameras.push( camera );
 
-		}
+					}
 
-	} );
+				}
 
-	Object.assign( InterleavedBuffer.prototype, {
+			}
 
-		isInterleavedBuffer: true,
+			//
 
-		onUploadCallback: function () {},
+			var inputSources = session.inputSources;
 
-		setUsage: function ( value ) {
+			for ( var i = 0; i < controllers.length; i ++ ) {
 
-			this.usage = value;
+				var controller = controllers[ i ];
+				var inputSource = inputSources[ i ];
 
-			return this;
+				controller.update( inputSource, frame, referenceSpace );
 
-		},
+			}
 
-		copy: function ( source ) {
+			if ( onAnimationFrameCallback ) { onAnimationFrameCallback( time, frame ); }
 
-			this.array = new source.array.constructor( source.array );
-			this.count = source.count;
-			this.stride = source.stride;
-			this.usage = source.usage;
+		}
 
-			return this;
+		var animation = new WebGLAnimation();
+		animation.setAnimationLoop( onAnimationFrame );
 
-		},
+		this.setAnimationLoop = function ( callback ) {
 
-		copyAt: function ( index1, attribute, index2 ) {
+			onAnimationFrameCallback = callback;
 
-			index1 *= this.stride;
-			index2 *= attribute.stride;
+		};
 
-			for ( var i = 0, l = this.stride; i < l; i ++ ) {
+		this.dispose = function () {};
 
-				this.array[ index1 + i ] = attribute.array[ index2 + i ];
+	}
 
-			}
+	Object.assign( WebXRManager.prototype, EventDispatcher.prototype );
 
-			return this;
+	/**
+	 * @author supereggbert / http://www.paulbrunt.co.uk/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author szimek / https://github.com/szimek/
+	 * @author tschw
+	 */
 
-		},
+	function WebGLRenderer( parameters ) {
 
-		set: function ( value, offset ) {
+		parameters = parameters || {};
 
-			if ( offset === undefined ) { offset = 0; }
+		var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ),
+			_context = parameters.context !== undefined ? parameters.context : null,
 
-			this.array.set( value, offset );
+			_alpha = parameters.alpha !== undefined ? parameters.alpha : false,
+			_depth = parameters.depth !== undefined ? parameters.depth : true,
+			_stencil = parameters.stencil !== undefined ? parameters.stencil : true,
+			_antialias = parameters.antialias !== undefined ? parameters.antialias : false,
+			_premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
+			_preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
+			_powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default',
+			_failIfMajorPerformanceCaveat = parameters.failIfMajorPerformanceCaveat !== undefined ? parameters.failIfMajorPerformanceCaveat : false;
 
-			return this;
+		var currentRenderList = null;
+		var currentRenderState = null;
 
-		},
+		// public properties
 
-		clone: function () {
+		this.domElement = _canvas;
 
-			return new this.constructor().copy( this );
+		// Debug configuration container
+		this.debug = {
 
-		},
+			/**
+			 * Enables error checking and reporting when shader programs are being compiled
+			 * @type {boolean}
+			 */
+			checkShaderErrors: true
+		};
 
-		onUpload: function ( callback ) {
+		// clearing
 
-			this.onUploadCallback = callback;
+		this.autoClear = true;
+		this.autoClearColor = true;
+		this.autoClearDepth = true;
+		this.autoClearStencil = true;
 
-			return this;
+		// scene graph
 
-		}
+		this.sortObjects = true;
 
-	} );
+		// user-defined clipping
 
-	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
-	 */
+		this.clippingPlanes = [];
+		this.localClippingEnabled = false;
 
-	var _vector$6 = new Vector3();
+		// physically based shading
 
-	function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) {
+		this.gammaFactor = 2.0;	// for backwards compatibility
+		this.outputEncoding = LinearEncoding;
 
-		this.data = interleavedBuffer;
-		this.itemSize = itemSize;
-		this.offset = offset;
+		// physical lights
 
-		this.normalized = normalized === true;
+		this.physicallyCorrectLights = false;
 
-	}
+		// tone mapping
 
-	Object.defineProperties( InterleavedBufferAttribute.prototype, {
+		this.toneMapping = NoToneMapping;
+		this.toneMappingExposure = 1.0;
+		this.toneMappingWhitePoint = 1.0;
 
-		count: {
+		// morphs
 
-			get: function () {
+		this.maxMorphTargets = 8;
+		this.maxMorphNormals = 4;
 
-				return this.data.count;
+		// internal properties
 
-			}
+		var _this = this,
 
-		},
+			_isContextLost = false,
 
-		array: {
+			// internal state cache
 
-			get: function () {
+			_framebuffer = null,
 
-				return this.data.array;
+			_currentActiveCubeFace = 0,
+			_currentActiveMipmapLevel = 0,
+			_currentRenderTarget = null,
+			_currentFramebuffer = null,
+			_currentMaterialId = - 1,
 
-			}
+			// geometry and program caching
 
-		}
+			_currentGeometryProgram = {
+				geometry: null,
+				program: null,
+				wireframe: false
+			},
 
-	} );
+			_currentCamera = null,
+			_currentArrayCamera = null,
 
-	Object.assign( InterleavedBufferAttribute.prototype, {
+			_currentViewport = new Vector4(),
+			_currentScissor = new Vector4(),
+			_currentScissorTest = null,
 
-		isInterleavedBufferAttribute: true,
+			//
 
-		applyMatrix4: function ( m ) {
+			_width = _canvas.width,
+			_height = _canvas.height,
 
-			for ( var i = 0, l = this.data.count; i < l; i ++ ) {
+			_pixelRatio = 1,
+			_opaqueSort = null,
+			_transparentSort = null,
 
-				_vector$6.x = this.getX( i );
-				_vector$6.y = this.getY( i );
-				_vector$6.z = this.getZ( i );
+			_viewport = new Vector4( 0, 0, _width, _height ),
+			_scissor = new Vector4( 0, 0, _width, _height ),
+			_scissorTest = false,
 
-				_vector$6.applyMatrix4( m );
+			// frustum
 
-				this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z );
+			_frustum = new Frustum(),
 
-			}
+			// clipping
 
-			return this;
+			_clipping = new WebGLClipping(),
+			_clippingEnabled = false,
+			_localClippingEnabled = false,
 
-		},
+			// camera matrices cache
 
-		setX: function ( index, x ) {
+			_projScreenMatrix = new Matrix4(),
 
-			this.data.array[ index * this.data.stride + this.offset ] = x;
+			_vector3 = new Vector3();
 
-			return this;
+		function getTargetPixelRatio() {
 
-		},
+			return _currentRenderTarget === null ? _pixelRatio : 1;
 
-		setY: function ( index, y ) {
+		}
 
-			this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
+		// initialize
 
-			return this;
+		var _gl;
 
-		},
+		try {
 
-		setZ: function ( index, z ) {
+			var contextAttributes = {
+				alpha: _alpha,
+				depth: _depth,
+				stencil: _stencil,
+				antialias: _antialias,
+				premultipliedAlpha: _premultipliedAlpha,
+				preserveDrawingBuffer: _preserveDrawingBuffer,
+				powerPreference: _powerPreference,
+				failIfMajorPerformanceCaveat: _failIfMajorPerformanceCaveat,
+				xrCompatible: true
+			};
 
-			this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
+			// event listeners must be registered before WebGL context is created, see #12753
 
-			return this;
+			_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
+			_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
 
-		},
+			_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
 
-		setW: function ( index, w ) {
+			if ( _gl === null ) {
 
-			this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
+				if ( _canvas.getContext( 'webgl' ) !== null ) {
 
-			return this;
+					throw new Error( 'Error creating WebGL context with your selected attributes.' );
 
-		},
+				} else {
 
-		getX: function ( index ) {
+					throw new Error( 'Error creating WebGL context.' );
 
-			return this.data.array[ index * this.data.stride + this.offset ];
+				}
 
-		},
+			}
 
-		getY: function ( index ) {
+			// Some experimental-webgl implementations do not have getShaderPrecisionFormat
 
-			return this.data.array[ index * this.data.stride + this.offset + 1 ];
+			if ( _gl.getShaderPrecisionFormat === undefined ) {
 
-		},
+				_gl.getShaderPrecisionFormat = function () {
 
-		getZ: function ( index ) {
+					return { 'rangeMin': 1, 'rangeMax': 1, 'precision': 1 };
 
-			return this.data.array[ index * this.data.stride + this.offset + 2 ];
+				};
 
-		},
+			}
 
-		getW: function ( index ) {
+		} catch ( error ) {
 
-			return this.data.array[ index * this.data.stride + this.offset + 3 ];
+			console.error( 'THREE.WebGLRenderer: ' + error.message );
+			throw error;
 
-		},
+		}
 
-		setXY: function ( index, x, y ) {
+		var extensions, capabilities, state, info;
+		var properties, textures, attributes, geometries, objects;
+		var programCache, renderLists, renderStates;
 
-			index = index * this.data.stride + this.offset;
+		var background, morphtargets, bufferRenderer, indexedBufferRenderer;
 
-			this.data.array[ index + 0 ] = x;
-			this.data.array[ index + 1 ] = y;
+		var utils;
 
-			return this;
+		function initGLContext() {
 
-		},
+			extensions = new WebGLExtensions( _gl );
 
-		setXYZ: function ( index, x, y, z ) {
+			capabilities = new WebGLCapabilities( _gl, extensions, parameters );
 
-			index = index * this.data.stride + this.offset;
+			if ( capabilities.isWebGL2 === false ) {
 
-			this.data.array[ index + 0 ] = x;
-			this.data.array[ index + 1 ] = y;
-			this.data.array[ index + 2 ] = z;
+				extensions.get( 'WEBGL_depth_texture' );
+				extensions.get( 'OES_texture_float' );
+				extensions.get( 'OES_texture_half_float' );
+				extensions.get( 'OES_texture_half_float_linear' );
+				extensions.get( 'OES_standard_derivatives' );
+				extensions.get( 'OES_element_index_uint' );
+				extensions.get( 'ANGLE_instanced_arrays' );
 
-			return this;
+			}
 
-		},
+			extensions.get( 'OES_texture_float_linear' );
 
-		setXYZW: function ( index, x, y, z, w ) {
+			utils = new WebGLUtils( _gl, extensions, capabilities );
 
-			index = index * this.data.stride + this.offset;
+			state = new WebGLState( _gl, extensions, capabilities );
+			state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
+			state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
 
-			this.data.array[ index + 0 ] = x;
-			this.data.array[ index + 1 ] = y;
-			this.data.array[ index + 2 ] = z;
-			this.data.array[ index + 3 ] = w;
+			info = new WebGLInfo( _gl );
+			properties = new WebGLProperties();
+			textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
+			attributes = new WebGLAttributes( _gl, capabilities );
+			geometries = new WebGLGeometries( _gl, attributes, info );
+			objects = new WebGLObjects( _gl, geometries, attributes, info );
+			morphtargets = new WebGLMorphtargets( _gl );
+			programCache = new WebGLPrograms( _this, extensions, capabilities );
+			renderLists = new WebGLRenderLists();
+			renderStates = new WebGLRenderStates();
 
-			return this;
+			background = new WebGLBackground( _this, state, objects, _premultipliedAlpha );
 
-		}
+			bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );
+			indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info, capabilities );
 
-	} );
+			info.programs = programCache.programs;
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  map: new THREE.Texture( <Image> ),
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *  rotation: <float>,
-	 *  sizeAttenuation: <bool>
-	 * }
-	 */
+			_this.capabilities = capabilities;
+			_this.extensions = extensions;
+			_this.properties = properties;
+			_this.renderLists = renderLists;
+			_this.state = state;
+			_this.info = info;
 
-	function SpriteMaterial( parameters ) {
+		}
 
-		Material.call( this );
+		initGLContext();
 
-		this.type = 'SpriteMaterial';
+		// xr
 
-		this.color = new Color( 0xffffff );
+		var xr = new WebXRManager( _this, _gl );
 
-		this.map = null;
+		this.xr = xr;
 
-		this.alphaMap = null;
+		// shadow map
 
-		this.rotation = 0;
-
-		this.sizeAttenuation = true;
+		var shadowMap = new WebGLShadowMap( _this, objects, capabilities.maxTextureSize );
 
-		this.transparent = true;
+		this.shadowMap = shadowMap;
 
-		this.setValues( parameters );
+		// API
 
-	}
+		this.getContext = function () {
 
-	SpriteMaterial.prototype = Object.create( Material.prototype );
-	SpriteMaterial.prototype.constructor = SpriteMaterial;
-	SpriteMaterial.prototype.isSpriteMaterial = true;
+			return _gl;
 
-	SpriteMaterial.prototype.copy = function ( source ) {
+		};
 
-		Material.prototype.copy.call( this, source );
+		this.getContextAttributes = function () {
 
-		this.color.copy( source.color );
+			return _gl.getContextAttributes();
 
-		this.map = source.map;
+		};
 
-		this.alphaMap = source.alphaMap;
+		this.forceContextLoss = function () {
 
-		this.rotation = source.rotation;
+			var extension = extensions.get( 'WEBGL_lose_context' );
+			if ( extension ) { extension.loseContext(); }
 
-		this.sizeAttenuation = source.sizeAttenuation;
+		};
 
-		return this;
+		this.forceContextRestore = function () {
 
-	};
+			var extension = extensions.get( 'WEBGL_lose_context' );
+			if ( extension ) { extension.restoreContext(); }
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+		};
 
-	var _geometry;
+		this.getPixelRatio = function () {
 
-	var _intersectPoint = new Vector3();
-	var _worldScale = new Vector3();
-	var _mvPosition = new Vector3();
+			return _pixelRatio;
 
-	var _alignedPosition = new Vector2();
-	var _rotatedPosition = new Vector2();
-	var _viewWorldMatrix = new Matrix4();
+		};
 
-	var _vA$1 = new Vector3();
-	var _vB$1 = new Vector3();
-	var _vC$1 = new Vector3();
+		this.setPixelRatio = function ( value ) {
 
-	var _uvA$1 = new Vector2();
-	var _uvB$1 = new Vector2();
-	var _uvC$1 = new Vector2();
+			if ( value === undefined ) { return; }
 
-	function Sprite( material ) {
+			_pixelRatio = value;
 
-		Object3D.call( this );
+			this.setSize( _width, _height, false );
 
-		this.type = 'Sprite';
+		};
 
-		if ( _geometry === undefined ) {
+		this.getSize = function ( target ) {
 
-			_geometry = new BufferGeometry();
+			if ( target === undefined ) {
 
-			var float32Array = new Float32Array( [
-				- 0.5, - 0.5, 0, 0, 0,
-				0.5, - 0.5, 0, 1, 0,
-				0.5, 0.5, 0, 1, 1,
-				- 0.5, 0.5, 0, 0, 1
-			] );
+				console.warn( 'WebGLRenderer: .getsize() now requires a Vector2 as an argument' );
 
-			var interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
+				target = new Vector2();
 
-			_geometry.setIndex( [ 0, 1, 2,	0, 2, 3 ] );
-			_geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
-			_geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
+			}
 
-		}
+			return target.set( _width, _height );
 
-		this.geometry = _geometry;
-		this.material = ( material !== undefined ) ? material : new SpriteMaterial();
+		};
 
-		this.center = new Vector2( 0.5, 0.5 );
+		this.setSize = function ( width, height, updateStyle ) {
 
-	}
+			if ( xr.isPresenting ) {
 
-	Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
+				console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' );
+				return;
 
-		constructor: Sprite,
+			}
 
-		isSprite: true,
+			_width = width;
+			_height = height;
 
-		raycast: function ( raycaster, intersects ) {
+			_canvas.width = Math.floor( width * _pixelRatio );
+			_canvas.height = Math.floor( height * _pixelRatio );
 
-			if ( raycaster.camera === null ) {
+			if ( updateStyle !== false ) {
 
-				console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
+				_canvas.style.width = width + 'px';
+				_canvas.style.height = height + 'px';
 
 			}
 
-			_worldScale.setFromMatrixScale( this.matrixWorld );
+			this.setViewport( 0, 0, width, height );
 
-			_viewWorldMatrix.copy( raycaster.camera.matrixWorld );
-			this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
+		};
 
-			_mvPosition.setFromMatrixPosition( this.modelViewMatrix );
+		this.getDrawingBufferSize = function ( target ) {
 
-			if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
+			if ( target === undefined ) {
 
-				_worldScale.multiplyScalar( - _mvPosition.z );
+				console.warn( 'WebGLRenderer: .getdrawingBufferSize() now requires a Vector2 as an argument' );
+
+				target = new Vector2();
 
 			}
 
-			var rotation = this.material.rotation;
-			var sin, cos;
-			if ( rotation !== 0 ) {
+			return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor();
 
-				cos = Math.cos( rotation );
-				sin = Math.sin( rotation );
+		};
 
-			}
+		this.setDrawingBufferSize = function ( width, height, pixelRatio ) {
 
-			var center = this.center;
+			_width = width;
+			_height = height;
 
-			transformVertex( _vA$1.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
-			transformVertex( _vB$1.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
-			transformVertex( _vC$1.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
+			_pixelRatio = pixelRatio;
 
-			_uvA$1.set( 0, 0 );
-			_uvB$1.set( 1, 0 );
-			_uvC$1.set( 1, 1 );
+			_canvas.width = Math.floor( width * pixelRatio );
+			_canvas.height = Math.floor( height * pixelRatio );
 
-			// check first triangle
-			var intersect = raycaster.ray.intersectTriangle( _vA$1, _vB$1, _vC$1, false, _intersectPoint );
+			this.setViewport( 0, 0, width, height );
 
-			if ( intersect === null ) {
+		};
 
-				// check second triangle
-				transformVertex( _vB$1.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
-				_uvB$1.set( 0, 1 );
+		this.getCurrentViewport = function ( target ) {
 
-				intersect = raycaster.ray.intersectTriangle( _vA$1, _vC$1, _vB$1, false, _intersectPoint );
-				if ( intersect === null ) {
+			if ( target === undefined ) {
 
-					return;
+				console.warn( 'WebGLRenderer: .getCurrentViewport() now requires a Vector4 as an argument' );
 
-				}
+				target = new Vector4();
 
 			}
 
-			var distance = raycaster.ray.origin.distanceTo( _intersectPoint );
+			return target.copy( _currentViewport );
 
-			if ( distance < raycaster.near || distance > raycaster.far ) { return; }
+		};
 
-			intersects.push( {
+		this.getViewport = function ( target ) {
 
-				distance: distance,
-				point: _intersectPoint.clone(),
-				uv: Triangle.getUV( _intersectPoint, _vA$1, _vB$1, _vC$1, _uvA$1, _uvB$1, _uvC$1, new Vector2() ),
-				face: null,
-				object: this
+			return target.copy( _viewport );
 
-			} );
+		};
 
-		},
+		this.setViewport = function ( x, y, width, height ) {
 
-		clone: function () {
+			if ( x.isVector4 ) {
 
-			return new this.constructor( this.material ).copy( this );
+				_viewport.set( x.x, x.y, x.z, x.w );
 
-		},
+			} else {
 
-		copy: function ( source ) {
+				_viewport.set( x, y, width, height );
 
-			Object3D.prototype.copy.call( this, source );
+			}
 
-			if ( source.center !== undefined ) { this.center.copy( source.center ); }
+			state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
 
-			return this;
+		};
 
-		}
+		this.getScissor = function ( target ) {
 
+			return target.copy( _scissor );
 
-	} );
+		};
 
-	function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
+		this.setScissor = function ( x, y, width, height ) {
 
-		// compute position in camera space
-		_alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale );
+			if ( x.isVector4 ) {
 
-		// to check if rotation is not zero
-		if ( sin !== undefined ) {
+				_scissor.set( x.x, x.y, x.z, x.w );
 
-			_rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y );
-			_rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y );
+			} else {
 
-		} else {
+				_scissor.set( x, y, width, height );
 
-			_rotatedPosition.copy( _alignedPosition );
+			}
 
-		}
+			state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
 
+		};
 
-		vertexPosition.copy( mvPosition );
-		vertexPosition.x += _rotatedPosition.x;
-		vertexPosition.y += _rotatedPosition.y;
+		this.getScissorTest = function () {
 
-		// transform to world space
-		vertexPosition.applyMatrix4( _viewWorldMatrix );
+			return _scissorTest;
 
-	}
+		};
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		this.setScissorTest = function ( boolean ) {
 
-	var _v1$4 = new Vector3();
-	var _v2$2 = new Vector3();
+			state.setScissorTest( _scissorTest = boolean );
 
-	function LOD() {
+		};
 
-		Object3D.call( this );
+		this.setOpaqueSort = function ( method ) {
 
-		this._currentLevel = 0;
+			_opaqueSort = method;
 
-		this.type = 'LOD';
+		};
 
-		Object.defineProperties( this, {
-			levels: {
-				enumerable: true,
-				value: []
-			}
-		} );
+		this.setTransparentSort = function ( method ) {
 
-		this.autoUpdate = true;
+			_transparentSort = method;
 
-	}
+		};
 
-	LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
+		// Clearing
 
-		constructor: LOD,
+		this.getClearColor = function () {
 
-		isLOD: true,
+			return background.getClearColor();
 
-		copy: function ( source ) {
+		};
 
-			Object3D.prototype.copy.call( this, source, false );
+		this.setClearColor = function () {
 
-			var levels = source.levels;
+			background.setClearColor.apply( background, arguments );
 
-			for ( var i = 0, l = levels.length; i < l; i ++ ) {
+		};
 
-				var level = levels[ i ];
+		this.getClearAlpha = function () {
 
-				this.addLevel( level.object.clone(), level.distance );
+			return background.getClearAlpha();
 
-			}
+		};
 
-			this.autoUpdate = source.autoUpdate;
+		this.setClearAlpha = function () {
 
-			return this;
+			background.setClearAlpha.apply( background, arguments );
 
-		},
+		};
 
-		addLevel: function ( object, distance ) {
+		this.clear = function ( color, depth, stencil ) {
 
-			if ( distance === undefined ) { distance = 0; }
+			var bits = 0;
 
-			distance = Math.abs( distance );
+			if ( color === undefined || color ) { bits |= 16384; }
+			if ( depth === undefined || depth ) { bits |= 256; }
+			if ( stencil === undefined || stencil ) { bits |= 1024; }
 
-			var levels = this.levels;
+			_gl.clear( bits );
 
-			for ( var l = 0; l < levels.length; l ++ ) {
+		};
 
-				if ( distance < levels[ l ].distance ) {
+		this.clearColor = function () {
 
-					break;
+			this.clear( true, false, false );
 
-				}
+		};
 
-			}
+		this.clearDepth = function () {
 
-			levels.splice( l, 0, { distance: distance, object: object } );
+			this.clear( false, true, false );
 
-			this.add( object );
+		};
 
-			return this;
+		this.clearStencil = function () {
 
-		},
+			this.clear( false, false, true );
 
-		getCurrentLevel: function () {
+		};
 
-			return this._currentLevel;
+		//
 
-		},
+		this.dispose = function () {
 
-		getObjectForDistance: function ( distance ) {
+			_canvas.removeEventListener( 'webglcontextlost', onContextLost, false );
+			_canvas.removeEventListener( 'webglcontextrestored', onContextRestore, false );
 
-			var levels = this.levels;
+			renderLists.dispose();
+			renderStates.dispose();
+			properties.dispose();
+			objects.dispose();
 
-			if ( levels.length > 0 ) {
+			xr.dispose();
 
-				for ( var i = 1, l = levels.length; i < l; i ++ ) {
+			animation.stop();
 
-					if ( distance < levels[ i ].distance ) {
+		};
 
-						break;
+		// Events
 
-					}
+		function onContextLost( event ) {
 
-				}
+			event.preventDefault();
 
-				return levels[ i - 1 ].object;
+			console.log( 'THREE.WebGLRenderer: Context Lost.' );
 
-			}
+			_isContextLost = true;
 
-			return null;
+		}
 
-		},
+		function onContextRestore( /* event */ ) {
 
-		raycast: function ( raycaster, intersects ) {
+			console.log( 'THREE.WebGLRenderer: Context Restored.' );
 
-			var levels = this.levels;
+			_isContextLost = false;
 
-			if ( levels.length > 0 ) {
+			initGLContext();
 
-				_v1$4.setFromMatrixPosition( this.matrixWorld );
+		}
 
-				var distance = raycaster.ray.origin.distanceTo( _v1$4 );
+		function onMaterialDispose( event ) {
 
-				this.getObjectForDistance( distance ).raycast( raycaster, intersects );
+			var material = event.target;
 
-			}
+			material.removeEventListener( 'dispose', onMaterialDispose );
 
-		},
+			deallocateMaterial( material );
 
-		update: function ( camera ) {
+		}
 
-			var levels = this.levels;
+		// Buffer deallocation
 
-			if ( levels.length > 1 ) {
+		function deallocateMaterial( material ) {
 
-				_v1$4.setFromMatrixPosition( camera.matrixWorld );
-				_v2$2.setFromMatrixPosition( this.matrixWorld );
+			releaseMaterialProgramReference( material );
 
-				var distance = _v1$4.distanceTo( _v2$2 ) / camera.zoom;
+			properties.remove( material );
 
-				levels[ 0 ].object.visible = true;
+		}
 
-				for ( var i = 1, l = levels.length; i < l; i ++ ) {
 
-					if ( distance >= levels[ i ].distance ) {
+		function releaseMaterialProgramReference( material ) {
 
-						levels[ i - 1 ].object.visible = false;
-						levels[ i ].object.visible = true;
+			var programInfo = properties.get( material ).program;
 
-					} else {
+			material.program = undefined;
 
-						break;
+			if ( programInfo !== undefined ) {
 
-					}
+				programCache.releaseProgram( programInfo );
 
-				}
+			}
 
-				this._currentLevel = i - 1;
+		}
 
-				for ( ; i < l; i ++ ) {
+		// Buffer rendering
 
-					levels[ i ].object.visible = false;
+		function renderObjectImmediate( object, program ) {
 
-				}
+			object.render( function ( object ) {
 
-			}
+				_this.renderBufferImmediate( object, program );
 
-		},
+			} );
 
-		toJSON: function ( meta ) {
+		}
 
-			var data = Object3D.prototype.toJSON.call( this, meta );
+		this.renderBufferImmediate = function ( object, program ) {
 
-			if ( this.autoUpdate === false ) { data.object.autoUpdate = false; }
+			state.initAttributes();
 
-			data.object.levels = [];
+			var buffers = properties.get( object );
 
-			var levels = this.levels;
+			if ( object.hasPositions && ! buffers.position ) { buffers.position = _gl.createBuffer(); }
+			if ( object.hasNormals && ! buffers.normal ) { buffers.normal = _gl.createBuffer(); }
+			if ( object.hasUvs && ! buffers.uv ) { buffers.uv = _gl.createBuffer(); }
+			if ( object.hasColors && ! buffers.color ) { buffers.color = _gl.createBuffer(); }
 
-			for ( var i = 0, l = levels.length; i < l; i ++ ) {
+			var programAttributes = program.getAttributes();
 
-				var level = levels[ i ];
+			if ( object.hasPositions ) {
 
-				data.object.levels.push( {
-					object: level.object.uuid,
-					distance: level.distance
-				} );
+				_gl.bindBuffer( 34962, buffers.position );
+				_gl.bufferData( 34962, object.positionArray, 35048 );
+
+				state.enableAttribute( programAttributes.position );
+				_gl.vertexAttribPointer( programAttributes.position, 3, 5126, false, 0, 0 );
 
 			}
 
-			return data;
+			if ( object.hasNormals ) {
 
-		}
+				_gl.bindBuffer( 34962, buffers.normal );
+				_gl.bufferData( 34962, object.normalArray, 35048 );
 
-	} );
+				state.enableAttribute( programAttributes.normal );
+				_gl.vertexAttribPointer( programAttributes.normal, 3, 5126, false, 0, 0 );
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author ikerr / http://verold.com
-	 */
+			}
 
-	function SkinnedMesh( geometry, material ) {
+			if ( object.hasUvs ) {
 
-		if ( geometry && geometry.isGeometry ) {
+				_gl.bindBuffer( 34962, buffers.uv );
+				_gl.bufferData( 34962, object.uvArray, 35048 );
 
-			console.error( 'THREE.SkinnedMesh no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
+				state.enableAttribute( programAttributes.uv );
+				_gl.vertexAttribPointer( programAttributes.uv, 2, 5126, false, 0, 0 );
 
-		}
+			}
 
-		Mesh.call( this, geometry, material );
+			if ( object.hasColors ) {
 
-		this.type = 'SkinnedMesh';
+				_gl.bindBuffer( 34962, buffers.color );
+				_gl.bufferData( 34962, object.colorArray, 35048 );
 
-		this.bindMode = 'attached';
-		this.bindMatrix = new Matrix4();
-		this.bindMatrixInverse = new Matrix4();
+				state.enableAttribute( programAttributes.color );
+				_gl.vertexAttribPointer( programAttributes.color, 3, 5126, false, 0, 0 );
 
-	}
+			}
 
-	SkinnedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {
+			state.disableUnusedAttributes();
 
-		constructor: SkinnedMesh,
+			_gl.drawArrays( 4, 0, object.count );
 
-		isSkinnedMesh: true,
+			object.count = 0;
 
-		bind: function ( skeleton, bindMatrix ) {
+		};
 
-			this.skeleton = skeleton;
+		var tempScene = new Scene();
 
-			if ( bindMatrix === undefined ) {
+		this.renderBufferDirect = function ( camera, scene, geometry, material, object, group ) {
 
-				this.updateMatrixWorld( true );
+			if ( scene === null ) { scene = tempScene; } // renderBufferDirect second parameter used to be fog (could be null)
 
-				this.skeleton.calculateInverses();
+			var frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );
 
-				bindMatrix = this.matrixWorld;
+			var program = setProgram( camera, scene, material, object );
 
-			}
+			state.setMaterial( material, frontFaceCW );
 
-			this.bindMatrix.copy( bindMatrix );
-			this.bindMatrixInverse.getInverse( bindMatrix );
+			var updateBuffers = false;
 
-		},
+			if ( _currentGeometryProgram.geometry !== geometry.id ||
+				_currentGeometryProgram.program !== program.id ||
+				_currentGeometryProgram.wireframe !== ( material.wireframe === true ) ) {
 
-		pose: function () {
+				_currentGeometryProgram.geometry = geometry.id;
+				_currentGeometryProgram.program = program.id;
+				_currentGeometryProgram.wireframe = material.wireframe === true;
+				updateBuffers = true;
 
-			this.skeleton.pose();
+			}
 
-		},
+			if ( material.morphTargets || material.morphNormals ) {
 
-		normalizeSkinWeights: function () {
+				morphtargets.update( object, geometry, material, program );
 
-			var vector = new Vector4();
+				updateBuffers = true;
 
-			var skinWeight = this.geometry.attributes.skinWeight;
+			}
 
-			for ( var i = 0, l = skinWeight.count; i < l; i ++ ) {
+			if ( object.isInstancedMesh === true ) {
 
-				vector.x = skinWeight.getX( i );
-				vector.y = skinWeight.getY( i );
-				vector.z = skinWeight.getZ( i );
-				vector.w = skinWeight.getW( i );
+				updateBuffers = true;
 
-				var scale = 1.0 / vector.manhattanLength();
+			}
 
-				if ( scale !== Infinity ) {
+			//
 
-					vector.multiplyScalar( scale );
+			var index = geometry.index;
+			var position = geometry.attributes.position;
 
-				} else {
+			//
 
-					vector.set( 1, 0, 0, 0 ); // do something reasonable
+			if ( index === null ) {
 
-				}
+				if ( position === undefined || position.count === 0 ) { return; }
 
-				skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
+			} else if ( index.count === 0 ) {
+
+				return;
 
 			}
 
-		},
+			//
 
-		updateMatrixWorld: function ( force ) {
+			var rangeFactor = 1;
 
-			Mesh.prototype.updateMatrixWorld.call( this, force );
+			if ( material.wireframe === true ) {
 
-			if ( this.bindMode === 'attached' ) {
+				index = geometries.getWireframeAttribute( geometry );
+				rangeFactor = 2;
 
-				this.bindMatrixInverse.getInverse( this.matrixWorld );
+			}
 
-			} else if ( this.bindMode === 'detached' ) {
+			var attribute;
+			var renderer = bufferRenderer;
 
-				this.bindMatrixInverse.getInverse( this.bindMatrix );
+			if ( index !== null ) {
 
-			} else {
+				attribute = attributes.get( index );
 
-				console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
+				renderer = indexedBufferRenderer;
+				renderer.setIndex( attribute );
 
 			}
 
-		},
-
-		clone: function () {
+			if ( updateBuffers ) {
 
-			return new this.constructor( this.geometry, this.material ).copy( this );
+				setupVertexAttributes( object, geometry, material, program );
 
-		},
+				if ( index !== null ) {
 
-		boneTransform: ( function () {
+					_gl.bindBuffer( 34963, attribute.buffer );
 
-			var basePosition = new Vector3();
+				}
 
-			var skinIndex = new Vector4();
-			var skinWeight = new Vector4();
+			}
 
-			var vector = new Vector3();
-			var matrix = new Matrix4();
+			//
 
-			return function ( index, target ) {
+			var dataCount = ( index !== null ) ? index.count : position.count;
 
-				var skeleton = this.skeleton;
-				var geometry = this.geometry;
+			var rangeStart = geometry.drawRange.start * rangeFactor;
+			var rangeCount = geometry.drawRange.count * rangeFactor;
 
-				skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
-				skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
+			var groupStart = group !== null ? group.start * rangeFactor : 0;
+			var groupCount = group !== null ? group.count * rangeFactor : Infinity;
 
-				basePosition.fromBufferAttribute( geometry.attributes.position, index ).applyMatrix4( this.bindMatrix );
+			var drawStart = Math.max( rangeStart, groupStart );
+			var drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1;
 
-				target.set( 0, 0, 0 );
+			var drawCount = Math.max( 0, drawEnd - drawStart + 1 );
 
-				for ( var i = 0; i < 4; i ++ ) {
+			if ( drawCount === 0 ) { return; }
 
-					var weight = skinWeight.getComponent( i );
+			//
 
-					if ( weight !== 0 ) {
+			if ( object.isMesh ) {
 
-						var boneIndex = skinIndex.getComponent( i );
+				if ( material.wireframe === true ) {
 
-						matrix.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
+					state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() );
+					renderer.setMode( 1 );
 
-						target.addScaledVector( vector.copy( basePosition ).applyMatrix4( matrix ), weight );
+				} else {
 
-					}
+					renderer.setMode( 4 );
 
 				}
 
-				return target.applyMatrix4( this.bindMatrixInverse );
+			} else if ( object.isLine ) {
 
-			};
+				var lineWidth = material.linewidth;
 
-		}() )
+				if ( lineWidth === undefined ) { lineWidth = 1; } // Not using Line*Material
 
-	} );
+				state.setLineWidth( lineWidth * getTargetPixelRatio() );
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author michael guerrero / http://realitymeltdown.com
-	 * @author ikerr / http://verold.com
-	 */
+				if ( object.isLineSegments ) {
 
-	var _offsetMatrix = new Matrix4();
-	var _identityMatrix = new Matrix4();
+					renderer.setMode( 1 );
 
-	function Skeleton( bones, boneInverses ) {
+				} else if ( object.isLineLoop ) {
 
-		// copy the bone array
+					renderer.setMode( 2 );
 
-		bones = bones || [];
+				} else {
 
-		this.bones = bones.slice( 0 );
-		this.boneMatrices = new Float32Array( this.bones.length * 16 );
+					renderer.setMode( 3 );
 
-		this.frame = - 1;
+				}
 
-		// use the supplied bone inverses or calculate the inverses
+			} else if ( object.isPoints ) {
 
-		if ( boneInverses === undefined ) {
+				renderer.setMode( 0 );
 
-			this.calculateInverses();
+			} else if ( object.isSprite ) {
 
-		} else {
+				renderer.setMode( 4 );
 
-			if ( this.bones.length === boneInverses.length ) {
+			}
 
-				this.boneInverses = boneInverses.slice( 0 );
+			if ( object.isInstancedMesh ) {
+
+				renderer.renderInstances( geometry, drawStart, drawCount, object.count );
+
+			} else if ( geometry.isInstancedBufferGeometry ) {
+
+				renderer.renderInstances( geometry, drawStart, drawCount, geometry.maxInstancedCount );
 
 			} else {
 
-				console.warn( 'THREE.Skeleton boneInverses is the wrong length.' );
+				renderer.render( drawStart, drawCount );
 
-				this.boneInverses = [];
+			}
 
-				for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
+		};
 
-					this.boneInverses.push( new Matrix4() );
+		function setupVertexAttributes( object, geometry, material, program ) {
 
-				}
+			if ( capabilities.isWebGL2 === false && ( object.isInstancedMesh || geometry.isInstancedBufferGeometry ) ) {
+
+				if ( extensions.get( 'ANGLE_instanced_arrays' ) === null ) { return; }
 
 			}
 
-		}
+			state.initAttributes();
 
-	}
+			var geometryAttributes = geometry.attributes;
 
-	Object.assign( Skeleton.prototype, {
+			var programAttributes = program.getAttributes();
 
-		calculateInverses: function () {
+			var materialDefaultAttributeValues = material.defaultAttributeValues;
 
-			this.boneInverses = [];
+			for ( var name in programAttributes ) {
 
-			for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
+				var programAttribute = programAttributes[ name ];
 
-				var inverse = new Matrix4();
+				if ( programAttribute >= 0 ) {
 
-				if ( this.bones[ i ] ) {
+					var geometryAttribute = geometryAttributes[ name ];
 
-					inverse.getInverse( this.bones[ i ].matrixWorld );
+					if ( geometryAttribute !== undefined ) {
 
-				}
+						var normalized = geometryAttribute.normalized;
+						var size = geometryAttribute.itemSize;
 
-				this.boneInverses.push( inverse );
+						var attribute = attributes.get( geometryAttribute );
 
-			}
+						// TODO Attribute may not be available on context restore
 
-		},
+						if ( attribute === undefined ) { continue; }
 
-		pose: function () {
+						var buffer = attribute.buffer;
+						var type = attribute.type;
+						var bytesPerElement = attribute.bytesPerElement;
 
-			var bone, i, il;
+						if ( geometryAttribute.isInterleavedBufferAttribute ) {
 
-			// recover the bind-time world matrices
+							var data = geometryAttribute.data;
+							var stride = data.stride;
+							var offset = geometryAttribute.offset;
 
-			for ( i = 0, il = this.bones.length; i < il; i ++ ) {
+							if ( data && data.isInstancedInterleavedBuffer ) {
 
-				bone = this.bones[ i ];
+								state.enableAttributeAndDivisor( programAttribute, data.meshPerAttribute );
 
-				if ( bone ) {
+								if ( geometry.maxInstancedCount === undefined ) {
 
-					bone.matrixWorld.getInverse( this.boneInverses[ i ] );
+									geometry.maxInstancedCount = data.meshPerAttribute * data.count;
 
-				}
+								}
 
-			}
+							} else {
 
-			// compute the local matrices, positions, rotations and scales
+								state.enableAttribute( programAttribute );
 
-			for ( i = 0, il = this.bones.length; i < il; i ++ ) {
+							}
 
-				bone = this.bones[ i ];
+							_gl.bindBuffer( 34962, buffer );
+							state.vertexAttribPointer( programAttribute, size, type, normalized, stride * bytesPerElement, offset * bytesPerElement );
 
-				if ( bone ) {
+						} else {
 
-					if ( bone.parent && bone.parent.isBone ) {
+							if ( geometryAttribute.isInstancedBufferAttribute ) {
 
-						bone.matrix.getInverse( bone.parent.matrixWorld );
-						bone.matrix.multiply( bone.matrixWorld );
+								state.enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute );
 
-					} else {
+								if ( geometry.maxInstancedCount === undefined ) {
 
-						bone.matrix.copy( bone.matrixWorld );
+									geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * geometryAttribute.count;
 
-					}
+								}
 
-					bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
+							} else {
 
-				}
+								state.enableAttribute( programAttribute );
 
-			}
+							}
 
-		},
+							_gl.bindBuffer( 34962, buffer );
+							state.vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 );
 
-		update: function () {
+						}
 
-			var bones = this.bones;
-			var boneInverses = this.boneInverses;
-			var boneMatrices = this.boneMatrices;
-			var boneTexture = this.boneTexture;
+					} else if ( name === 'instanceMatrix' ) {
 
-			// flatten bone matrices to array
+						var attribute = attributes.get( object.instanceMatrix );
 
-			for ( var i = 0, il = bones.length; i < il; i ++ ) {
+						// TODO Attribute may not be available on context restore
 
-				// compute the offset between the current and the original transform
+						if ( attribute === undefined ) { continue; }
 
-				var matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
+						var buffer = attribute.buffer;
+						var type = attribute.type;
 
-				_offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
-				_offsetMatrix.toArray( boneMatrices, i * 16 );
+						state.enableAttributeAndDivisor( programAttribute + 0, 1 );
+						state.enableAttributeAndDivisor( programAttribute + 1, 1 );
+						state.enableAttributeAndDivisor( programAttribute + 2, 1 );
+						state.enableAttributeAndDivisor( programAttribute + 3, 1 );
 
-			}
+						_gl.bindBuffer( 34962, buffer );
 
-			if ( boneTexture !== undefined ) {
+						_gl.vertexAttribPointer( programAttribute + 0, 4, type, false, 64, 0 );
+						_gl.vertexAttribPointer( programAttribute + 1, 4, type, false, 64, 16 );
+						_gl.vertexAttribPointer( programAttribute + 2, 4, type, false, 64, 32 );
+						_gl.vertexAttribPointer( programAttribute + 3, 4, type, false, 64, 48 );
 
-				boneTexture.needsUpdate = true;
+					} else if ( materialDefaultAttributeValues !== undefined ) {
 
-			}
+						var value = materialDefaultAttributeValues[ name ];
 
-		},
+						if ( value !== undefined ) {
 
-		clone: function () {
+							switch ( value.length ) {
 
-			return new Skeleton( this.bones, this.boneInverses );
+								case 2:
+									_gl.vertexAttrib2fv( programAttribute, value );
+									break;
 
-		},
+								case 3:
+									_gl.vertexAttrib3fv( programAttribute, value );
+									break;
 
-		getBoneByName: function ( name ) {
+								case 4:
+									_gl.vertexAttrib4fv( programAttribute, value );
+									break;
 
-			for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
+								default:
+									_gl.vertexAttrib1fv( programAttribute, value );
 
-				var bone = this.bones[ i ];
+							}
 
-				if ( bone.name === name ) {
+						}
 
-					return bone;
+					}
 
 				}
 
 			}
 
-			return undefined;
+			state.disableUnusedAttributes();
 
-		},
+		}
 
-		dispose: function ( ) {
+		// Compile
 
-			if ( this.boneTexture ) {
+		this.compile = function ( scene, camera ) {
 
-				this.boneTexture.dispose();
+			currentRenderState = renderStates.get( scene, camera );
+			currentRenderState.init();
 
-				this.boneTexture = undefined;
+			scene.traverse( function ( object ) {
 
-			}
+				if ( object.isLight ) {
 
-		}
+					currentRenderState.pushLight( object );
 
-	} );
+					if ( object.castShadow ) {
 
-	/**
-	 * @author mikael emtinger / http://gomo.se/
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author ikerr / http://verold.com
-	 */
+						currentRenderState.pushShadow( object );
 
-	function Bone() {
+					}
 
-		Object3D.call( this );
+				}
 
-		this.type = 'Bone';
+			} );
 
-	}
+			currentRenderState.setupLights( camera );
 
-	Bone.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			var compiled = {};
 
-		constructor: Bone,
+			scene.traverse( function ( object ) {
 
-		isBone: true
+				if ( object.material ) {
 
-	} );
+					if ( Array.isArray( object.material ) ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+						for ( var i = 0; i < object.material.length; i ++ ) {
 
-	var _instanceLocalMatrix = new Matrix4();
-	var _instanceWorldMatrix = new Matrix4();
+							if ( object.material[ i ].uuid in compiled === false ) {
 
-	var _instanceIntersects = [];
+								initMaterial( object.material[ i ], scene, object );
+								compiled[ object.material[ i ].uuid ] = true;
 
-	var _mesh = new Mesh();
+							}
 
-	function InstancedMesh( geometry, material, count ) {
+						}
 
-		Mesh.call( this, geometry, material );
+					} else if ( object.material.uuid in compiled === false ) {
 
-		this.instanceMatrix = new BufferAttribute( new Float32Array( count * 16 ), 16 );
+						initMaterial( object.material, scene, object );
+						compiled[ object.material.uuid ] = true;
 
-		this.count = count;
+					}
 
-		this.frustumCulled = false;
+				}
 
-	}
+			} );
 
-	InstancedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {
+		};
 
-		constructor: InstancedMesh,
+		// Animation Loop
 
-		isInstancedMesh: true,
+		var onAnimationFrameCallback = null;
 
-		getMatrixAt: function ( index, matrix ) {
+		function onAnimationFrame( time ) {
 
-			matrix.fromArray( this.instanceMatrix.array, index * 16 );
+			if ( xr.isPresenting ) { return; }
+			if ( onAnimationFrameCallback ) { onAnimationFrameCallback( time ); }
 
-		},
+		}
 
-		raycast: function ( raycaster, intersects ) {
+		var animation = new WebGLAnimation();
+		animation.setAnimationLoop( onAnimationFrame );
 
-			var matrixWorld = this.matrixWorld;
-			var raycastTimes = this.count;
+		if ( typeof window !== 'undefined' ) { animation.setContext( window ); }
 
-			_mesh.geometry = this.geometry;
-			_mesh.material = this.material;
+		this.setAnimationLoop = function ( callback ) {
 
-			if ( _mesh.material === undefined ) { return; }
+			onAnimationFrameCallback = callback;
+			xr.setAnimationLoop( callback );
 
-			for ( var instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
+			animation.start();
 
-				// calculate the world matrix for each instance
+		};
 
-				this.getMatrixAt( instanceId, _instanceLocalMatrix );
+		// Rendering
 
-				_instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
+		this.render = function ( scene, camera ) {
 
-				// the mesh represents this single instance
+			var renderTarget, forceClear;
 
-				_mesh.matrixWorld = _instanceWorldMatrix;
+			if ( arguments[ 2 ] !== undefined ) {
 
-				_mesh.raycast( raycaster, _instanceIntersects );
+				console.warn( 'THREE.WebGLRenderer.render(): the renderTarget argument has been removed. Use .setRenderTarget() instead.' );
+				renderTarget = arguments[ 2 ];
 
-				// process the result of raycast
+			}
 
-				for ( var i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
+			if ( arguments[ 3 ] !== undefined ) {
 
-					var intersect = _instanceIntersects[ i ];
-					intersect.instanceId = instanceId;
-					intersect.object = this;
-					intersects.push( intersect );
+				console.warn( 'THREE.WebGLRenderer.render(): the forceClear argument has been removed. Use .clear() instead.' );
+				forceClear = arguments[ 3 ];
 
-				}
+			}
 
-				_instanceIntersects.length = 0;
+			if ( ! ( camera && camera.isCamera ) ) {
+
+				console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
+				return;
 
 			}
 
-		},
+			if ( _isContextLost ) { return; }
 
-		setMatrixAt: function ( index, matrix ) {
+			// reset caching for this frame
 
-			matrix.toArray( this.instanceMatrix.array, index * 16 );
+			_currentGeometryProgram.geometry = null;
+			_currentGeometryProgram.program = null;
+			_currentGeometryProgram.wireframe = false;
+			_currentMaterialId = - 1;
+			_currentCamera = null;
 
-		},
+			// update scene graph
 
-		updateMorphTargets: function () {
+			if ( scene.autoUpdate === true ) { scene.updateMatrixWorld(); }
 
-		}
+			// update camera matrices and frustum
 
-	} );
+			if ( camera.parent === null ) { camera.updateMatrixWorld(); }
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *
-	 *  linewidth: <float>,
-	 *  linecap: "round",
-	 *  linejoin: "round"
-	 * }
-	 */
+			if ( xr.enabled && xr.isPresenting ) {
 
-	function LineBasicMaterial( parameters ) {
+				camera = xr.getCamera( camera );
 
-		Material.call( this );
+			}
 
-		this.type = 'LineBasicMaterial';
+			//
+			scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget );
 
-		this.color = new Color( 0xffffff );
+			currentRenderState = renderStates.get( scene, camera );
+			currentRenderState.init();
 
-		this.linewidth = 1;
-		this.linecap = 'round';
-		this.linejoin = 'round';
+			_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
+			_frustum.setFromProjectionMatrix( _projScreenMatrix );
 
-		this.setValues( parameters );
+			_localClippingEnabled = this.localClippingEnabled;
+			_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
 
-	}
+			currentRenderList = renderLists.get( scene, camera );
+			currentRenderList.init();
 
-	LineBasicMaterial.prototype = Object.create( Material.prototype );
-	LineBasicMaterial.prototype.constructor = LineBasicMaterial;
+			projectObject( scene, camera, 0, _this.sortObjects );
 
-	LineBasicMaterial.prototype.isLineBasicMaterial = true;
+			currentRenderList.finish();
 
-	LineBasicMaterial.prototype.copy = function ( source ) {
+			if ( _this.sortObjects === true ) {
 
-		Material.prototype.copy.call( this, source );
+				currentRenderList.sort( _opaqueSort, _transparentSort );
 
-		this.color.copy( source.color );
+			}
 
-		this.linewidth = source.linewidth;
-		this.linecap = source.linecap;
-		this.linejoin = source.linejoin;
+			//
 
-		return this;
+			if ( _clippingEnabled ) { _clipping.beginShadows(); }
 
-	};
+			var shadowsArray = currentRenderState.state.shadowsArray;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			shadowMap.render( shadowsArray, scene, camera );
 
-	var _start = new Vector3();
-	var _end = new Vector3();
-	var _inverseMatrix$1 = new Matrix4();
-	var _ray$1 = new Ray();
-	var _sphere$2 = new Sphere();
+			currentRenderState.setupLights( camera );
 
-	function Line( geometry, material, mode ) {
+			if ( _clippingEnabled ) { _clipping.endShadows(); }
 
-		if ( mode === 1 ) {
+			//
 
-			console.error( 'THREE.Line: parameter THREE.LinePieces no longer supported. Use THREE.LineSegments instead.' );
+			if ( this.info.autoReset ) { this.info.reset(); }
 
-		}
+			if ( renderTarget !== undefined ) {
 
-		Object3D.call( this );
+				this.setRenderTarget( renderTarget );
 
-		this.type = 'Line';
+			}
 
-		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
-		this.material = material !== undefined ? material : new LineBasicMaterial();
+			//
 
-	}
+			background.render( currentRenderList, scene, camera, forceClear );
 
-	Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			// render scene
 
-		constructor: Line,
+			var opaqueObjects = currentRenderList.opaque;
+			var transparentObjects = currentRenderList.transparent;
 
-		isLine: true,
+			if ( scene.overrideMaterial ) {
 
-		computeLineDistances: function () {
+				var overrideMaterial = scene.overrideMaterial;
 
-			var geometry = this.geometry;
+				if ( opaqueObjects.length ) { renderObjects( opaqueObjects, scene, camera, overrideMaterial ); }
+				if ( transparentObjects.length ) { renderObjects( transparentObjects, scene, camera, overrideMaterial ); }
 
-			if ( geometry.isBufferGeometry ) {
+			} else {
 
-				// we assume non-indexed geometry
+				// opaque pass (front-to-back order)
 
-				if ( geometry.index === null ) {
+				if ( opaqueObjects.length ) { renderObjects( opaqueObjects, scene, camera ); }
 
-					var positionAttribute = geometry.attributes.position;
-					var lineDistances = [ 0 ];
+				// transparent pass (back-to-front order)
 
-					for ( var i = 1, l = positionAttribute.count; i < l; i ++ ) {
+				if ( transparentObjects.length ) { renderObjects( transparentObjects, scene, camera ); }
 
-						_start.fromBufferAttribute( positionAttribute, i - 1 );
-						_end.fromBufferAttribute( positionAttribute, i );
+			}
 
-						lineDistances[ i ] = lineDistances[ i - 1 ];
-						lineDistances[ i ] += _start.distanceTo( _end );
+			//
 
-					}
+			scene.onAfterRender( _this, scene, camera );
 
-					geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
+			//
 
-				} else {
+			if ( _currentRenderTarget !== null ) {
 
-					console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
+				// Generate mipmap if we're using any kind of mipmap filtering
 
-				}
+				textures.updateRenderTargetMipmap( _currentRenderTarget );
 
-			} else if ( geometry.isGeometry ) {
+				// resolve multisample renderbuffers to a single-sample texture if necessary
 
-				var vertices = geometry.vertices;
-				var lineDistances = geometry.lineDistances;
+				textures.updateMultisampleRenderTarget( _currentRenderTarget );
 
-				lineDistances[ 0 ] = 0;
+			}
 
-				for ( var i = 1, l = vertices.length; i < l; i ++ ) {
+			// Ensure depth buffer writing is enabled so it can be cleared on next render
 
-					lineDistances[ i ] = lineDistances[ i - 1 ];
-					lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
+			state.buffers.depth.setTest( true );
+			state.buffers.depth.setMask( true );
+			state.buffers.color.setMask( true );
 
-				}
+			state.setPolygonOffset( false );
 
-			}
+			// _gl.finish();
 
-			return this;
+			currentRenderList = null;
+			currentRenderState = null;
 
-		},
+		};
 
-		raycast: function ( raycaster, intersects ) {
+		function projectObject( object, camera, groupOrder, sortObjects ) {
 
-			var geometry = this.geometry;
-			var matrixWorld = this.matrixWorld;
-			var threshold = raycaster.params.Line.threshold;
+			if ( object.visible === false ) { return; }
 
-			// Checking boundingSphere distance to ray
+			var visible = object.layers.test( camera.layers );
 
-			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
+			if ( visible ) {
 
-			_sphere$2.copy( geometry.boundingSphere );
-			_sphere$2.applyMatrix4( matrixWorld );
-			_sphere$2.radius += threshold;
+				if ( object.isGroup ) {
 
-			if ( raycaster.ray.intersectsSphere( _sphere$2 ) === false ) { return; }
+					groupOrder = object.renderOrder;
 
-			//
+				} else if ( object.isLOD ) {
 
-			_inverseMatrix$1.getInverse( matrixWorld );
-			_ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 );
+					if ( object.autoUpdate === true ) { object.update( camera ); }
 
-			var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
-			var localThresholdSq = localThreshold * localThreshold;
+				} else if ( object.isLight ) {
 
-			var vStart = new Vector3();
-			var vEnd = new Vector3();
-			var interSegment = new Vector3();
-			var interRay = new Vector3();
-			var step = ( this && this.isLineSegments ) ? 2 : 1;
+					currentRenderState.pushLight( object );
 
-			if ( geometry.isBufferGeometry ) {
+					if ( object.castShadow ) {
 
-				var index = geometry.index;
-				var attributes = geometry.attributes;
-				var positions = attributes.position.array;
+						currentRenderState.pushShadow( object );
 
-				if ( index !== null ) {
+					}
 
-					var indices = index.array;
+				} else if ( object.isSprite ) {
 
-					for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
+					if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
 
-						var a = indices[ i ];
-						var b = indices[ i + 1 ];
+						if ( sortObjects ) {
 
-						vStart.fromArray( positions, a * 3 );
-						vEnd.fromArray( positions, b * 3 );
+							_vector3.setFromMatrixPosition( object.matrixWorld )
+								.applyMatrix4( _projScreenMatrix );
 
-						var distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
+						}
 
-						if ( distSq > localThresholdSq ) { continue; }
+						var geometry = objects.update( object );
+						var material = object.material;
 
-						interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
+						if ( material.visible ) {
 
-						var distance = raycaster.ray.origin.distanceTo( interRay );
+							currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
 
-						if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
+						}
 
-						intersects.push( {
+					}
 
-							distance: distance,
-							// What do we want? intersection point on the ray or on the segment??
-							// point: raycaster.ray.at( distance ),
-							point: interSegment.clone().applyMatrix4( this.matrixWorld ),
-							index: i,
-							face: null,
-							faceIndex: null,
-							object: this
+				} else if ( object.isImmediateRenderObject ) {
 
-						} );
+					if ( sortObjects ) {
+
+						_vector3.setFromMatrixPosition( object.matrixWorld )
+							.applyMatrix4( _projScreenMatrix );
 
 					}
 
-				} else {
+					currentRenderList.push( object, null, object.material, groupOrder, _vector3.z, null );
 
-					for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
+				} else if ( object.isMesh || object.isLine || object.isPoints ) {
 
-						vStart.fromArray( positions, 3 * i );
-						vEnd.fromArray( positions, 3 * i + 3 );
+					if ( object.isSkinnedMesh ) {
 
-						var distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
+						// update skeleton only once in a frame
 
-						if ( distSq > localThresholdSq ) { continue; }
+						if ( object.skeleton.frame !== info.render.frame ) {
 
-						interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
+							object.skeleton.update();
+							object.skeleton.frame = info.render.frame;
 
-						var distance = raycaster.ray.origin.distanceTo( interRay );
+						}
 
-						if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
+					}
 
-						intersects.push( {
+					if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
 
-							distance: distance,
-							// What do we want? intersection point on the ray or on the segment??
-							// point: raycaster.ray.at( distance ),
-							point: interSegment.clone().applyMatrix4( this.matrixWorld ),
-							index: i,
-							face: null,
-							faceIndex: null,
-							object: this
+						if ( sortObjects ) {
 
-						} );
+							_vector3.setFromMatrixPosition( object.matrixWorld )
+								.applyMatrix4( _projScreenMatrix );
 
-					}
+						}
 
-				}
+						var geometry = objects.update( object );
+						var material = object.material;
 
-			} else if ( geometry.isGeometry ) {
+						if ( Array.isArray( material ) ) {
 
-				var vertices = geometry.vertices;
-				var nbVertices = vertices.length;
+							var groups = geometry.groups;
 
-				for ( var i = 0; i < nbVertices - 1; i += step ) {
+							for ( var i = 0, l = groups.length; i < l; i ++ ) {
 
-					var distSq = _ray$1.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
+								var group = groups[ i ];
+								var groupMaterial = material[ group.materialIndex ];
 
-					if ( distSq > localThresholdSq ) { continue; }
+								if ( groupMaterial && groupMaterial.visible ) {
 
-					interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
+									currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
 
-					var distance = raycaster.ray.origin.distanceTo( interRay );
+								}
 
-					if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
+							}
 
-					intersects.push( {
+						} else if ( material.visible ) {
 
-						distance: distance,
-						// What do we want? intersection point on the ray or on the segment??
-						// point: raycaster.ray.at( distance ),
-						point: interSegment.clone().applyMatrix4( this.matrixWorld ),
-						index: i,
-						face: null,
-						faceIndex: null,
-						object: this
+							currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
 
-					} );
+						}
+
+					}
 
 				}
 
 			}
 
-		},
+			var children = object.children;
 
-		clone: function () {
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-			return new this.constructor( this.geometry, this.material ).copy( this );
+				projectObject( children[ i ], camera, groupOrder, sortObjects );
+
+			}
 
 		}
 
-	} );
+		function renderObjects( renderList, scene, camera, overrideMaterial ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			for ( var i = 0, l = renderList.length; i < l; i ++ ) {
 
-	var _start$1 = new Vector3();
-	var _end$1 = new Vector3();
+				var renderItem = renderList[ i ];
 
-	function LineSegments( geometry, material ) {
+				var object = renderItem.object;
+				var geometry = renderItem.geometry;
+				var material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
+				var group = renderItem.group;
 
-		Line.call( this, geometry, material );
+				if ( camera.isArrayCamera ) {
 
-		this.type = 'LineSegments';
+					_currentArrayCamera = camera;
 
-	}
+					var cameras = camera.cameras;
 
-	LineSegments.prototype = Object.assign( Object.create( Line.prototype ), {
+					for ( var j = 0, jl = cameras.length; j < jl; j ++ ) {
 
-		constructor: LineSegments,
+						var camera2 = cameras[ j ];
 
-		isLineSegments: true,
+						if ( object.layers.test( camera2.layers ) ) {
 
-		computeLineDistances: function () {
+							state.viewport( _currentViewport.copy( camera2.viewport ) );
 
-			var geometry = this.geometry;
+							currentRenderState.setupLights( camera2 );
 
-			if ( geometry.isBufferGeometry ) {
+							renderObject( object, scene, camera2, geometry, material, group );
 
-				// we assume non-indexed geometry
+						}
 
-				if ( geometry.index === null ) {
+					}
 
-					var positionAttribute = geometry.attributes.position;
-					var lineDistances = [];
+				} else {
 
-					for ( var i = 0, l = positionAttribute.count; i < l; i += 2 ) {
+					_currentArrayCamera = null;
 
-						_start$1.fromBufferAttribute( positionAttribute, i );
-						_end$1.fromBufferAttribute( positionAttribute, i + 1 );
+					renderObject( object, scene, camera, geometry, material, group );
 
-						lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
-						lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 );
+				}
 
-					}
+			}
 
-					geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
+		}
 
-				} else {
+		function renderObject( object, scene, camera, geometry, material, group ) {
 
-					console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
+			object.onBeforeRender( _this, scene, camera, geometry, material, group );
+			currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
 
-				}
+			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
+			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
 
-			} else if ( geometry.isGeometry ) {
+			if ( object.isImmediateRenderObject ) {
 
-				var vertices = geometry.vertices;
-				var lineDistances = geometry.lineDistances;
+				var program = setProgram( camera, scene, material, object );
 
-				for ( var i = 0, l = vertices.length; i < l; i += 2 ) {
+				state.setMaterial( material );
 
-					_start$1.copy( vertices[ i ] );
-					_end$1.copy( vertices[ i + 1 ] );
+				_currentGeometryProgram.geometry = null;
+				_currentGeometryProgram.program = null;
+				_currentGeometryProgram.wireframe = false;
 
-					lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
-					lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 );
+				renderObjectImmediate( object, program );
 
-				}
+			} else {
+
+				_this.renderBufferDirect( camera, scene, geometry, material, object, group );
 
 			}
 
-			return this;
+			object.onAfterRender( _this, scene, camera, geometry, material, group );
+			currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
 
 		}
 
-	} );
+		function initMaterial( material, scene, object ) {
 
-	/**
-	 * @author mgreter / http://github.com/mgreter
-	 */
+			var materialProperties = properties.get( material );
 
-	function LineLoop( geometry, material ) {
+			var lights = currentRenderState.state.lights;
+			var shadowsArray = currentRenderState.state.shadowsArray;
 
-		Line.call( this, geometry, material );
+			var lightsStateVersion = lights.state.version;
 
-		this.type = 'LineLoop';
+			var parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, _clipping.numPlanes, _clipping.numIntersection, object );
+			var programCacheKey = programCache.getProgramCacheKey( parameters );
 
-	}
+			var program = materialProperties.program;
+			var programChange = true;
 
-	LineLoop.prototype = Object.assign( Object.create( Line.prototype ), {
+			if ( program === undefined ) {
 
-		constructor: LineLoop,
+				// new material
+				material.addEventListener( 'dispose', onMaterialDispose );
 
-		isLineLoop: true,
+			} else if ( program.cacheKey !== programCacheKey ) {
 
-	} );
+				// changed glsl or parameters
+				releaseMaterialProgramReference( material );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *  map: new THREE.Texture( <Image> ),
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  size: <float>,
-	 *  sizeAttenuation: <bool>
-	 *
-	 *  morphTargets: <bool>
-	 * }
-	 */
+			} else if ( materialProperties.lightsStateVersion !== lightsStateVersion ) {
 
-	function PointsMaterial( parameters ) {
+				materialProperties.lightsStateVersion = lightsStateVersion;
 
-		Material.call( this );
+				programChange = false;
 
-		this.type = 'PointsMaterial';
+			} else if ( parameters.shaderID !== undefined ) {
 
-		this.color = new Color( 0xffffff );
+				// same glsl and uniform list
+				return;
 
-		this.map = null;
+			} else {
 
-		this.alphaMap = null;
+				// only rebuild uniform list
+				programChange = false;
 
-		this.size = 1;
-		this.sizeAttenuation = true;
+			}
 
-		this.morphTargets = false;
+			if ( programChange ) {
 
-		this.setValues( parameters );
+				program = programCache.acquireProgram( parameters, programCacheKey );
 
-	}
+				materialProperties.program = program;
+				materialProperties.uniforms = parameters.uniforms;
+				materialProperties.outputEncoding = parameters.outputEncoding;
+				material.program = program;
 
-	PointsMaterial.prototype = Object.create( Material.prototype );
-	PointsMaterial.prototype.constructor = PointsMaterial;
+			}
 
-	PointsMaterial.prototype.isPointsMaterial = true;
+			var programAttributes = program.getAttributes();
 
-	PointsMaterial.prototype.copy = function ( source ) {
+			if ( material.morphTargets ) {
 
-		Material.prototype.copy.call( this, source );
+				material.numSupportedMorphTargets = 0;
 
-		this.color.copy( source.color );
+				for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
 
-		this.map = source.map;
+					if ( programAttributes[ 'morphTarget' + i ] >= 0 ) {
 
-		this.alphaMap = source.alphaMap;
+						material.numSupportedMorphTargets ++;
 
-		this.size = source.size;
-		this.sizeAttenuation = source.sizeAttenuation;
+					}
 
-		this.morphTargets = source.morphTargets;
+				}
 
-		return this;
+			}
 
-	};
+			if ( material.morphNormals ) {
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+				material.numSupportedMorphNormals = 0;
 
-	var _inverseMatrix$2 = new Matrix4();
-	var _ray$2 = new Ray();
-	var _sphere$3 = new Sphere();
-	var _position$1 = new Vector3();
+				for ( var i = 0; i < _this.maxMorphNormals; i ++ ) {
 
-	function Points( geometry, material ) {
+					if ( programAttributes[ 'morphNormal' + i ] >= 0 ) {
 
-		Object3D.call( this );
+						material.numSupportedMorphNormals ++;
 
-		this.type = 'Points';
+					}
 
-		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
-		this.material = material !== undefined ? material : new PointsMaterial();
+				}
 
-		this.updateMorphTargets();
+			}
 
-	}
+			var uniforms = materialProperties.uniforms;
 
-	Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			if ( ! material.isShaderMaterial &&
+				! material.isRawShaderMaterial ||
+				material.clipping === true ) {
 
-		constructor: Points,
+				materialProperties.numClippingPlanes = _clipping.numPlanes;
+				materialProperties.numIntersection = _clipping.numIntersection;
+				uniforms.clippingPlanes = _clipping.uniform;
 
-		isPoints: true,
+			}
 
-		raycast: function ( raycaster, intersects ) {
+			materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null;
+			materialProperties.fog = scene.fog;
 
-			var geometry = this.geometry;
-			var matrixWorld = this.matrixWorld;
-			var threshold = raycaster.params.Points.threshold;
+			// store the light setup it was created for
 
-			// Checking boundingSphere distance to ray
+			materialProperties.needsLights = materialNeedsLights( material );
+			materialProperties.lightsStateVersion = lightsStateVersion;
 
-			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
+			if ( materialProperties.needsLights ) {
 
-			_sphere$3.copy( geometry.boundingSphere );
-			_sphere$3.applyMatrix4( matrixWorld );
-			_sphere$3.radius += threshold;
+				// wire up the material to this renderer's lighting state
 
-			if ( raycaster.ray.intersectsSphere( _sphere$3 ) === false ) { return; }
+				uniforms.ambientLightColor.value = lights.state.ambient;
+				uniforms.lightProbe.value = lights.state.probe;
+				uniforms.directionalLights.value = lights.state.directional;
+				uniforms.directionalLightShadows.value = lights.state.directionalShadow;
+				uniforms.spotLights.value = lights.state.spot;
+				uniforms.spotLightShadows.value = lights.state.spotShadow;
+				uniforms.rectAreaLights.value = lights.state.rectArea;
+				uniforms.pointLights.value = lights.state.point;
+				uniforms.pointLightShadows.value = lights.state.pointShadow;
+				uniforms.hemisphereLights.value = lights.state.hemi;
 
-			//
+				uniforms.directionalShadowMap.value = lights.state.directionalShadowMap;
+				uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix;
+				uniforms.spotShadowMap.value = lights.state.spotShadowMap;
+				uniforms.spotShadowMatrix.value = lights.state.spotShadowMatrix;
+				uniforms.pointShadowMap.value = lights.state.pointShadowMap;
+				uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix;
+				// TODO (abelnation): add area lights shadow info to uniforms
 
-			_inverseMatrix$2.getInverse( matrixWorld );
-			_ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 );
+			}
 
-			var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
-			var localThresholdSq = localThreshold * localThreshold;
+			var progUniforms = materialProperties.program.getUniforms(),
+				uniformsList =
+					WebGLUniforms.seqWithValue( progUniforms.seq, uniforms );
 
-			if ( geometry.isBufferGeometry ) {
+			materialProperties.uniformsList = uniformsList;
 
-				var index = geometry.index;
-				var attributes = geometry.attributes;
-				var positions = attributes.position.array;
+		}
 
-				if ( index !== null ) {
+		function setProgram( camera, scene, material, object ) {
 
-					var indices = index.array;
+			textures.resetTextureUnits();
 
-					for ( var i = 0, il = indices.length; i < il; i ++ ) {
+			var fog = scene.fog;
+			var environment = material.isMeshStandardMaterial ? scene.environment : null;
+			var encoding = ( _currentRenderTarget === null ) ? _this.outputEncoding : _currentRenderTarget.texture.encoding;
 
-						var a = indices[ i ];
+			var materialProperties = properties.get( material );
+			var lights = currentRenderState.state.lights;
 
-						_position$1.fromArray( positions, a * 3 );
+			if ( _clippingEnabled ) {
 
-						testPoint( _position$1, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
+				if ( _localClippingEnabled || camera !== _currentCamera ) {
 
-					}
+					var useCache =
+						camera === _currentCamera &&
+						material.id === _currentMaterialId;
 
-				} else {
+					// we might want to call this function with some ClippingGroup
+					// object instead of the material, once it becomes feasible
+					// (#8465, #8379)
+					_clipping.setState(
+						material.clippingPlanes, material.clipIntersection, material.clipShadows,
+						camera, materialProperties, useCache );
 
-					for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
+				}
 
-						_position$1.fromArray( positions, i * 3 );
+			}
 
-						testPoint( _position$1, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
+			if ( material.version === materialProperties.__version ) {
 
-					}
+				if ( materialProperties.program === undefined ) {
 
-				}
+					initMaterial( material, scene, object );
 
-			} else {
+				} else if ( material.fog && materialProperties.fog !== fog ) {
 
-				var vertices = geometry.vertices;
+					initMaterial( material, scene, object );
 
-				for ( var i = 0, l = vertices.length; i < l; i ++ ) {
+				} else if ( materialProperties.environment !== environment ) {
 
-					testPoint( vertices[ i ], i, localThresholdSq, matrixWorld, raycaster, intersects, this );
+					initMaterial( material, scene, object );
 
-				}
+				} else if ( materialProperties.needsLights && ( materialProperties.lightsStateVersion !== lights.state.version ) ) {
 
-			}
+					initMaterial( material, scene, object );
 
-		},
+				} else if ( materialProperties.numClippingPlanes !== undefined &&
+					( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
+					materialProperties.numIntersection !== _clipping.numIntersection ) ) {
 
-		updateMorphTargets: function () {
+					initMaterial( material, scene, object );
 
-			var geometry = this.geometry;
-			var m, ml, name;
+				} else if ( materialProperties.outputEncoding !== encoding ) {
 
-			if ( geometry.isBufferGeometry ) {
+					initMaterial( material, scene, object );
 
-				var morphAttributes = geometry.morphAttributes;
-				var keys = Object.keys( morphAttributes );
+				}
 
-				if ( keys.length > 0 ) {
-
-					var morphAttribute = morphAttributes[ keys[ 0 ] ];
+			} else {
 
-					if ( morphAttribute !== undefined ) {
+				initMaterial( material, scene, object );
+				materialProperties.__version = material.version;
 
-						this.morphTargetInfluences = [];
-						this.morphTargetDictionary = {};
+			}
 
-						for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
+			var refreshProgram = false;
+			var refreshMaterial = false;
+			var refreshLights = false;
 
-							name = morphAttribute[ m ].name || String( m );
+			var program = materialProperties.program,
+				p_uniforms = program.getUniforms(),
+				m_uniforms = materialProperties.uniforms;
 
-							this.morphTargetInfluences.push( 0 );
-							this.morphTargetDictionary[ name ] = m;
+			if ( state.useProgram( program.program ) ) {
 
-						}
+				refreshProgram = true;
+				refreshMaterial = true;
+				refreshLights = true;
 
-					}
+			}
 
-				}
+			if ( material.id !== _currentMaterialId ) {
 
-			} else {
+				_currentMaterialId = material.id;
 
-				var morphTargets = geometry.morphTargets;
+				refreshMaterial = true;
 
-				if ( morphTargets !== undefined && morphTargets.length > 0 ) {
+			}
 
-					console.error( 'THREE.Points.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
+			if ( refreshProgram || _currentCamera !== camera ) {
 
-				}
+				p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
 
-			}
+				if ( capabilities.logarithmicDepthBuffer ) {
 
-		},
+					p_uniforms.setValue( _gl, 'logDepthBufFC',
+						2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
 
-		clone: function () {
+				}
 
-			return new this.constructor( this.geometry, this.material ).copy( this );
+				if ( _currentCamera !== camera ) {
 
-		}
+					_currentCamera = camera;
 
-	} );
+					// lighting uniforms depend on the camera so enforce an update
+					// now, in case this material supports lights - or later, when
+					// the next material that does gets activated:
 
-	function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
+					refreshMaterial = true;		// set to true on material change
+					refreshLights = true;		// remains set until update done
 
-		var rayPointDistanceSq = _ray$2.distanceSqToPoint( point );
+				}
 
-		if ( rayPointDistanceSq < localThresholdSq ) {
+				// load material specific uniforms
+				// (shader material also gets them for the sake of genericity)
 
-			var intersectPoint = new Vector3();
+				if ( material.isShaderMaterial ||
+					material.isMeshPhongMaterial ||
+					material.isMeshToonMaterial ||
+					material.isMeshStandardMaterial ||
+					material.envMap ) {
 
-			_ray$2.closestPointToPoint( point, intersectPoint );
-			intersectPoint.applyMatrix4( matrixWorld );
+					var uCamPos = p_uniforms.map.cameraPosition;
 
-			var distance = raycaster.ray.origin.distanceTo( intersectPoint );
+					if ( uCamPos !== undefined ) {
 
-			if ( distance < raycaster.near || distance > raycaster.far ) { return; }
+						uCamPos.setValue( _gl,
+							_vector3.setFromMatrixPosition( camera.matrixWorld ) );
 
-			intersects.push( {
+					}
 
-				distance: distance,
-				distanceToRay: Math.sqrt( rayPointDistanceSq ),
-				point: intersectPoint,
-				index: index,
-				face: null,
-				object: object
+				}
 
-			} );
+				if ( material.isMeshPhongMaterial ||
+					material.isMeshToonMaterial ||
+					material.isMeshLambertMaterial ||
+					material.isMeshBasicMaterial ||
+					material.isMeshStandardMaterial ||
+					material.isShaderMaterial ) {
 
-		}
+					p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true );
 
-	}
+				}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				if ( material.isMeshPhongMaterial ||
+					material.isMeshToonMaterial ||
+					material.isMeshLambertMaterial ||
+					material.isMeshBasicMaterial ||
+					material.isMeshStandardMaterial ||
+					material.isShaderMaterial ||
+					material.skinning ) {
 
-	function VideoTexture( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
+					p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
 
-		Texture.call( this, video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
+				}
 
-		this.format = format !== undefined ? format : RGBFormat;
+			}
 
-		this.minFilter = minFilter !== undefined ? minFilter : LinearFilter;
-		this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
+			// skinning uniforms must be set even if material didn't change
+			// auto-setting of texture unit for bone texture must go before other textures
+			// otherwise textures used for skinning can take over texture units reserved for other material textures
 
-		this.generateMipmaps = false;
+			if ( material.skinning ) {
 
-	}
+				p_uniforms.setOptional( _gl, object, 'bindMatrix' );
+				p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' );
 
-	VideoTexture.prototype = Object.assign( Object.create( Texture.prototype ), {
+				var skeleton = object.skeleton;
 
-		constructor: VideoTexture,
+				if ( skeleton ) {
 
-		isVideoTexture: true,
+					var bones = skeleton.bones;
 
-		update: function () {
+					if ( capabilities.floatVertexTextures ) {
 
-			var video = this.image;
+						if ( skeleton.boneTexture === undefined ) {
 
-			if ( video.readyState >= video.HAVE_CURRENT_DATA ) {
+							// layout (1 matrix = 4 pixels)
+							//      RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
+							//  with  8x8  pixel texture max   16 bones * 4 pixels =  (8 * 8)
+							//       16x16 pixel texture max   64 bones * 4 pixels = (16 * 16)
+							//       32x32 pixel texture max  256 bones * 4 pixels = (32 * 32)
+							//       64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
 
-				this.needsUpdate = true;
 
-			}
+							var size = Math.sqrt( bones.length * 4 ); // 4 pixels needed for 1 matrix
+							size = MathUtils.ceilPowerOfTwo( size );
+							size = Math.max( size, 4 );
 
-		}
+							var boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
+							boneMatrices.set( skeleton.boneMatrices ); // copy current values
 
-	} );
+							var boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+							skeleton.boneMatrices = boneMatrices;
+							skeleton.boneTexture = boneTexture;
+							skeleton.boneTextureSize = size;
 
-	function CompressedTexture( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) {
+						}
 
-		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
+						p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures );
+						p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize );
 
-		this.image = { width: width, height: height };
-		this.mipmaps = mipmaps;
+					} else {
 
-		// no flipping for cube textures
-		// (also flipping doesn't work for compressed textures )
+						p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' );
 
-		this.flipY = false;
+					}
 
-		// can't generate mipmaps for compressed textures
-		// mips must be embedded in DDS files
+				}
 
-		this.generateMipmaps = false;
+			}
 
-	}
+			if ( refreshMaterial || materialProperties.receiveShadow !== object.receiveShadow ) {
 
-	CompressedTexture.prototype = Object.create( Texture.prototype );
-	CompressedTexture.prototype.constructor = CompressedTexture;
+				materialProperties.receiveShadow = object.receiveShadow;
+				p_uniforms.setValue( _gl, 'receiveShadow', object.receiveShadow );
 
-	CompressedTexture.prototype.isCompressedTexture = true;
+			}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			if ( refreshMaterial ) {
 
-	function CanvasTexture( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
+				p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure );
+				p_uniforms.setValue( _gl, 'toneMappingWhitePoint', _this.toneMappingWhitePoint );
 
-		Texture.call( this, canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
+				if ( materialProperties.needsLights ) {
 
-		this.needsUpdate = true;
+					// the current material requires lighting info
 
-	}
+					// note: all lighting uniforms are always set correctly
+					// they simply reference the renderer's state for their
+					// values
+					//
+					// use the current material's .needsUpdate flags to set
+					// the GL state when required
 
-	CanvasTexture.prototype = Object.create( Texture.prototype );
-	CanvasTexture.prototype.constructor = CanvasTexture;
-	CanvasTexture.prototype.isCanvasTexture = true;
+					markUniformsLightsNeedsUpdate( m_uniforms, refreshLights );
 
-	/**
-	 * @author Matt DesLauriers / @mattdesl
-	 * @author atix / arthursilber.de
-	 */
+				}
 
-	function DepthTexture( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format ) {
+				// refresh uniforms common to several materials
 
-		format = format !== undefined ? format : DepthFormat;
+				if ( fog && material.fog ) {
 
-		if ( format !== DepthFormat && format !== DepthStencilFormat ) {
+					refreshUniformsFog( m_uniforms, fog );
 
-			throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' );
+				}
 
-		}
+				if ( material.isMeshBasicMaterial ) {
 
-		if ( type === undefined && format === DepthFormat ) { type = UnsignedShortType; }
-		if ( type === undefined && format === DepthStencilFormat ) { type = UnsignedInt248Type; }
+					refreshUniformsCommon( m_uniforms, material );
 
-		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
+				} else if ( material.isMeshLambertMaterial ) {
 
-		this.image = { width: width, height: height };
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsLambert( m_uniforms, material );
 
-		this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
-		this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
+				} else if ( material.isMeshToonMaterial ) {
 
-		this.flipY = false;
-		this.generateMipmaps	= false;
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsToon( m_uniforms, material );
 
-	}
+				} else if ( material.isMeshPhongMaterial ) {
 
-	DepthTexture.prototype = Object.create( Texture.prototype );
-	DepthTexture.prototype.constructor = DepthTexture;
-	DepthTexture.prototype.isDepthTexture = true;
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsPhong( m_uniforms, material );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+				} else if ( material.isMeshStandardMaterial ) {
 
-	function WireframeGeometry( geometry ) {
+					refreshUniformsCommon( m_uniforms, material, environment );
 
-		BufferGeometry.call( this );
+					if ( material.isMeshPhysicalMaterial ) {
 
-		this.type = 'WireframeGeometry';
+						refreshUniformsPhysical( m_uniforms, material, environment );
 
-		// buffer
+					} else {
 
-		var vertices = [];
+						refreshUniformsStandard( m_uniforms, material, environment );
 
-		// helper variables
+					}
 
-		var i, j, l, o, ol;
-		var edge = [ 0, 0 ], edges = {}, e, edge1, edge2;
-		var key, keys = [ 'a', 'b', 'c' ];
-		var vertex;
+				} else if ( material.isMeshMatcapMaterial ) {
 
-		// different logic for Geometry and BufferGeometry
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsMatcap( m_uniforms, material );
 
-		if ( geometry && geometry.isGeometry ) {
+				} else if ( material.isMeshDepthMaterial ) {
 
-			// create a data structure that contains all edges without duplicates
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsDepth( m_uniforms, material );
 
-			var faces = geometry.faces;
+				} else if ( material.isMeshDistanceMaterial ) {
 
-			for ( i = 0, l = faces.length; i < l; i ++ ) {
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsDistance( m_uniforms, material );
 
-				var face = faces[ i ];
+				} else if ( material.isMeshNormalMaterial ) {
 
-				for ( j = 0; j < 3; j ++ ) {
+					refreshUniformsCommon( m_uniforms, material );
+					refreshUniformsNormal( m_uniforms, material );
 
-					edge1 = face[ keys[ j ] ];
-					edge2 = face[ keys[ ( j + 1 ) % 3 ] ];
-					edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates
-					edge[ 1 ] = Math.max( edge1, edge2 );
+				} else if ( material.isLineBasicMaterial ) {
 
-					key = edge[ 0 ] + ',' + edge[ 1 ];
+					refreshUniformsLine( m_uniforms, material );
 
-					if ( edges[ key ] === undefined ) {
+					if ( material.isLineDashedMaterial ) {
 
-						edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] };
+						refreshUniformsDash( m_uniforms, material );
 
 					}
 
-				}
+				} else if ( material.isPointsMaterial ) {
 
-			}
+					refreshUniformsPoints( m_uniforms, material );
 
-			// generate vertices
+				} else if ( material.isSpriteMaterial ) {
 
-			for ( key in edges ) {
+					refreshUniformsSprites( m_uniforms, material );
 
-				e = edges[ key ];
+				} else if ( material.isShadowMaterial ) {
 
-				vertex = geometry.vertices[ e.index1 ];
-				vertices.push( vertex.x, vertex.y, vertex.z );
+					m_uniforms.color.value.copy( material.color );
+					m_uniforms.opacity.value = material.opacity;
 
-				vertex = geometry.vertices[ e.index2 ];
-				vertices.push( vertex.x, vertex.y, vertex.z );
+				}
 
-			}
+				// RectAreaLight Texture
+				// TODO (mrdoob): Find a nicer implementation
 
-		} else if ( geometry && geometry.isBufferGeometry ) {
+				if ( m_uniforms.ltc_1 !== undefined ) { m_uniforms.ltc_1.value = UniformsLib.LTC_1; }
+				if ( m_uniforms.ltc_2 !== undefined ) { m_uniforms.ltc_2.value = UniformsLib.LTC_2; }
 
-			var position, indices, groups;
-			var group, start, count;
-			var index1, index2;
+				WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
 
-			vertex = new Vector3();
+				if ( material.isShaderMaterial ) {
 
-			if ( geometry.index !== null ) {
+					material.uniformsNeedUpdate = false; // #15581
 
-				// indexed BufferGeometry
+				}
 
-				position = geometry.attributes.position;
-				indices = geometry.index;
-				groups = geometry.groups;
+			}
 
-				if ( groups.length === 0 ) {
+			if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) {
 
-					groups = [ { start: 0, count: indices.count, materialIndex: 0 } ];
+				WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures );
+				material.uniformsNeedUpdate = false;
 
-				}
+			}
 
-				// create a data structure that contains all eges without duplicates
+			if ( material.isSpriteMaterial ) {
 
-				for ( o = 0, ol = groups.length; o < ol; ++ o ) {
+				p_uniforms.setValue( _gl, 'center', object.center );
 
-					group = groups[ o ];
+			}
 
-					start = group.start;
-					count = group.count;
+			// common matrices
 
-					for ( i = start, l = ( start + count ); i < l; i += 3 ) {
+			p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
+			p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix );
+			p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld );
 
-						for ( j = 0; j < 3; j ++ ) {
+			return program;
 
-							edge1 = indices.getX( i + j );
-							edge2 = indices.getX( i + ( j + 1 ) % 3 );
-							edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates
-							edge[ 1 ] = Math.max( edge1, edge2 );
+		}
 
-							key = edge[ 0 ] + ',' + edge[ 1 ];
+		// Uniforms (refresh uniforms objects)
 
-							if ( edges[ key ] === undefined ) {
+		function refreshUniformsCommon( uniforms, material, environment ) {
 
-								edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] };
+			uniforms.opacity.value = material.opacity;
 
-							}
+			if ( material.color ) {
 
-						}
+				uniforms.diffuse.value.copy( material.color );
 
-					}
+			}
 
-				}
+			if ( material.emissive ) {
 
-				// generate vertices
+				uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
 
-				for ( key in edges ) {
+			}
 
-					e = edges[ key ];
+			if ( material.map ) {
 
-					vertex.fromBufferAttribute( position, e.index1 );
-					vertices.push( vertex.x, vertex.y, vertex.z );
+				uniforms.map.value = material.map;
 
-					vertex.fromBufferAttribute( position, e.index2 );
-					vertices.push( vertex.x, vertex.y, vertex.z );
+			}
 
-				}
+			if ( material.alphaMap ) {
 
-			} else {
+				uniforms.alphaMap.value = material.alphaMap;
 
-				// non-indexed BufferGeometry
+			}
 
-				position = geometry.attributes.position;
+			if ( material.specularMap ) {
 
-				for ( i = 0, l = ( position.count / 3 ); i < l; i ++ ) {
+				uniforms.specularMap.value = material.specularMap;
 
-					for ( j = 0; j < 3; j ++ ) {
+			}
 
-						// three edges per triangle, an edge is represented as (index1, index2)
-						// e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
+			var envMap = material.envMap || environment;
 
-						index1 = 3 * i + j;
-						vertex.fromBufferAttribute( position, index1 );
-						vertices.push( vertex.x, vertex.y, vertex.z );
+			if ( envMap ) {
 
-						index2 = 3 * i + ( ( j + 1 ) % 3 );
-						vertex.fromBufferAttribute( position, index2 );
-						vertices.push( vertex.x, vertex.y, vertex.z );
+				uniforms.envMap.value = envMap;
 
-					}
+				uniforms.flipEnvMap.value = envMap.isCubeTexture ? - 1 : 1;
 
-				}
+				uniforms.reflectivity.value = material.reflectivity;
+				uniforms.refractionRatio.value = material.refractionRatio;
+
+				uniforms.maxMipLevel.value = properties.get( envMap ).__maxMipLevel;
 
 			}
 
-		}
+			if ( material.lightMap ) {
 
-		// build geometry
+				uniforms.lightMap.value = material.lightMap;
+				uniforms.lightMapIntensity.value = material.lightMapIntensity;
 
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+			}
 
-	}
+			if ( material.aoMap ) {
 
-	WireframeGeometry.prototype = Object.create( BufferGeometry.prototype );
-	WireframeGeometry.prototype.constructor = WireframeGeometry;
+				uniforms.aoMap.value = material.aoMap;
+				uniforms.aoMapIntensity.value = material.aoMapIntensity;
 
-	/**
-	 * @author zz85 / https://github.com/zz85
-	 * @author Mugen87 / https://github.com/Mugen87
-	 *
-	 * Parametric Surfaces Geometry
-	 * based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html
-	 */
+			}
 
-	// ParametricGeometry
+			// uv repeat and offset setting priorities
+			// 1. color map
+			// 2. specular map
+			// 3. normal map
+			// 4. bump map
+			// 5. alpha map
+			// 6. emissive map
 
-	function ParametricGeometry( func, slices, stacks ) {
+			var uvScaleMap;
 
-		Geometry.call( this );
+			if ( material.map ) {
 
-		this.type = 'ParametricGeometry';
+				uvScaleMap = material.map;
 
-		this.parameters = {
-			func: func,
-			slices: slices,
-			stacks: stacks
-		};
+			} else if ( material.specularMap ) {
 
-		this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
-		this.mergeVertices();
+				uvScaleMap = material.specularMap;
 
-	}
+			} else if ( material.displacementMap ) {
 
-	ParametricGeometry.prototype = Object.create( Geometry.prototype );
-	ParametricGeometry.prototype.constructor = ParametricGeometry;
+				uvScaleMap = material.displacementMap;
 
-	// ParametricBufferGeometry
+			} else if ( material.normalMap ) {
 
-	function ParametricBufferGeometry( func, slices, stacks ) {
+				uvScaleMap = material.normalMap;
 
-		BufferGeometry.call( this );
+			} else if ( material.bumpMap ) {
 
-		this.type = 'ParametricBufferGeometry';
+				uvScaleMap = material.bumpMap;
 
-		this.parameters = {
-			func: func,
-			slices: slices,
-			stacks: stacks
-		};
+			} else if ( material.roughnessMap ) {
 
-		// buffers
+				uvScaleMap = material.roughnessMap;
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+			} else if ( material.metalnessMap ) {
 
-		var EPS = 0.00001;
+				uvScaleMap = material.metalnessMap;
 
-		var normal = new Vector3();
+			} else if ( material.alphaMap ) {
 
-		var p0 = new Vector3(), p1 = new Vector3();
-		var pu = new Vector3(), pv = new Vector3();
+				uvScaleMap = material.alphaMap;
 
-		var i, j;
+			} else if ( material.emissiveMap ) {
 
-		if ( func.length < 3 ) {
+				uvScaleMap = material.emissiveMap;
 
-			console.error( 'THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter.' );
+			}
 
-		}
+			if ( uvScaleMap !== undefined ) {
 
-		// generate vertices, normals and uvs
+				// backwards compatibility
+				if ( uvScaleMap.isWebGLRenderTarget ) {
 
-		var sliceCount = slices + 1;
+					uvScaleMap = uvScaleMap.texture;
 
-		for ( i = 0; i <= stacks; i ++ ) {
+				}
 
-			var v = i / stacks;
+				if ( uvScaleMap.matrixAutoUpdate === true ) {
 
-			for ( j = 0; j <= slices; j ++ ) {
+					uvScaleMap.updateMatrix();
 
-				var u = j / slices;
+				}
 
-				// vertex
+				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
 
-				func( u, v, p0 );
-				vertices.push( p0.x, p0.y, p0.z );
-
-				// normal
+			}
 
-				// approximate tangent vectors via finite differences
+			// uv repeat and offset setting priorities for uv2
+			// 1. ao map
+			// 2. light map
 
-				if ( u - EPS >= 0 ) {
+			var uv2ScaleMap;
 
-					func( u - EPS, v, p1 );
-					pu.subVectors( p0, p1 );
+			if ( material.aoMap ) {
 
-				} else {
+				uv2ScaleMap = material.aoMap;
 
-					func( u + EPS, v, p1 );
-					pu.subVectors( p1, p0 );
+			} else if ( material.lightMap ) {
 
-				}
+				uv2ScaleMap = material.lightMap;
 
-				if ( v - EPS >= 0 ) {
+			}
 
-					func( u, v - EPS, p1 );
-					pv.subVectors( p0, p1 );
+			if ( uv2ScaleMap !== undefined ) {
 
-				} else {
+				// backwards compatibility
+				if ( uv2ScaleMap.isWebGLRenderTarget ) {
 
-					func( u, v + EPS, p1 );
-					pv.subVectors( p1, p0 );
+					uv2ScaleMap = uv2ScaleMap.texture;
 
 				}
 
-				// cross product of tangent vectors returns surface normal
+				if ( uv2ScaleMap.matrixAutoUpdate === true ) {
 
-				normal.crossVectors( pu, pv ).normalize();
-				normals.push( normal.x, normal.y, normal.z );
+					uv2ScaleMap.updateMatrix();
 
-				// uv
+				}
 
-				uvs.push( u, v );
+				uniforms.uv2Transform.value.copy( uv2ScaleMap.matrix );
 
 			}
 
 		}
 
-		// generate indices
-
-		for ( i = 0; i < stacks; i ++ ) {
-
-			for ( j = 0; j < slices; j ++ ) {
+		function refreshUniformsLine( uniforms, material ) {
 
-				var a = i * sliceCount + j;
-				var b = i * sliceCount + j + 1;
-				var c = ( i + 1 ) * sliceCount + j + 1;
-				var d = ( i + 1 ) * sliceCount + j;
+			uniforms.diffuse.value.copy( material.color );
+			uniforms.opacity.value = material.opacity;
 
-				// faces one and two
+		}
 
-				indices.push( a, b, d );
-				indices.push( b, c, d );
+		function refreshUniformsDash( uniforms, material ) {
 
-			}
+			uniforms.dashSize.value = material.dashSize;
+			uniforms.totalSize.value = material.dashSize + material.gapSize;
+			uniforms.scale.value = material.scale;
 
 		}
 
-		// build geometry
-
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+		function refreshUniformsPoints( uniforms, material ) {
 
-	}
+			uniforms.diffuse.value.copy( material.color );
+			uniforms.opacity.value = material.opacity;
+			uniforms.size.value = material.size * _pixelRatio;
+			uniforms.scale.value = _height * 0.5;
 
-	ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
+			if ( material.map ) {
 
-	/**
-	 * @author clockworkgeek / https://github.com/clockworkgeek
-	 * @author timothypratley / https://github.com/timothypratley
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+				uniforms.map.value = material.map;
 
-	// PolyhedronGeometry
+			}
 
-	function PolyhedronGeometry( vertices, indices, radius, detail ) {
+			if ( material.alphaMap ) {
 
-		Geometry.call( this );
+				uniforms.alphaMap.value = material.alphaMap;
 
-		this.type = 'PolyhedronGeometry';
+			}
 
-		this.parameters = {
-			vertices: vertices,
-			indices: indices,
-			radius: radius,
-			detail: detail
-		};
+			// uv repeat and offset setting priorities
+			// 1. color map
+			// 2. alpha map
 
-		this.fromBufferGeometry( new PolyhedronBufferGeometry( vertices, indices, radius, detail ) );
-		this.mergeVertices();
+			var uvScaleMap;
 
-	}
+			if ( material.map ) {
 
-	PolyhedronGeometry.prototype = Object.create( Geometry.prototype );
-	PolyhedronGeometry.prototype.constructor = PolyhedronGeometry;
+				uvScaleMap = material.map;
 
-	// PolyhedronBufferGeometry
+			} else if ( material.alphaMap ) {
 
-	function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
+				uvScaleMap = material.alphaMap;
 
-		BufferGeometry.call( this );
+			}
 
-		this.type = 'PolyhedronBufferGeometry';
+			if ( uvScaleMap !== undefined ) {
 
-		this.parameters = {
-			vertices: vertices,
-			indices: indices,
-			radius: radius,
-			detail: detail
-		};
+				if ( uvScaleMap.matrixAutoUpdate === true ) {
 
-		radius = radius || 1;
-		detail = detail || 0;
+					uvScaleMap.updateMatrix();
 
-		// default buffer data
+				}
 
-		var vertexBuffer = [];
-		var uvBuffer = [];
+				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
 
-		// the subdivision creates the vertex buffer data
+			}
 
-		subdivide( detail );
+		}
 
-		// all vertices should lie on a conceptual sphere with a given radius
+		function refreshUniformsSprites( uniforms, material ) {
 
-		applyRadius( radius );
+			uniforms.diffuse.value.copy( material.color );
+			uniforms.opacity.value = material.opacity;
+			uniforms.rotation.value = material.rotation;
 
-		// finally, create the uv data
+			if ( material.map ) {
 
-		generateUVs();
+				uniforms.map.value = material.map;
 
-		// build non-indexed geometry
+			}
 
-		this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
+			if ( material.alphaMap ) {
 
-		if ( detail === 0 ) {
+				uniforms.alphaMap.value = material.alphaMap;
 
-			this.computeVertexNormals(); // flat normals
+			}
 
-		} else {
+			// uv repeat and offset setting priorities
+			// 1. color map
+			// 2. alpha map
 
-			this.normalizeNormals(); // smooth normals
+			var uvScaleMap;
 
-		}
+			if ( material.map ) {
 
-		// helper functions
+				uvScaleMap = material.map;
 
-		function subdivide( detail ) {
+			} else if ( material.alphaMap ) {
 
-			var a = new Vector3();
-			var b = new Vector3();
-			var c = new Vector3();
+				uvScaleMap = material.alphaMap;
 
-			// iterate over all faces and apply a subdivison with the given detail value
+			}
 
-			for ( var i = 0; i < indices.length; i += 3 ) {
+			if ( uvScaleMap !== undefined ) {
 
-				// get the vertices of the face
+				if ( uvScaleMap.matrixAutoUpdate === true ) {
 
-				getVertexByIndex( indices[ i + 0 ], a );
-				getVertexByIndex( indices[ i + 1 ], b );
-				getVertexByIndex( indices[ i + 2 ], c );
+					uvScaleMap.updateMatrix();
 
-				// perform subdivision
+				}
 
-				subdivideFace( a, b, c, detail );
+				uniforms.uvTransform.value.copy( uvScaleMap.matrix );
 
 			}
 
 		}
 
-		function subdivideFace( a, b, c, detail ) {
+		function refreshUniformsFog( uniforms, fog ) {
 
-			var cols = Math.pow( 2, detail );
+			uniforms.fogColor.value.copy( fog.color );
 
-			// we use this multidimensional array as a data structure for creating the subdivision
+			if ( fog.isFog ) {
 
-			var v = [];
+				uniforms.fogNear.value = fog.near;
+				uniforms.fogFar.value = fog.far;
 
-			var i, j;
+			} else if ( fog.isFogExp2 ) {
 
-			// construct all of the vertices for this subdivision
+				uniforms.fogDensity.value = fog.density;
 
-			for ( i = 0; i <= cols; i ++ ) {
+			}
 
-				v[ i ] = [];
+		}
 
-				var aj = a.clone().lerp( c, i / cols );
-				var bj = b.clone().lerp( c, i / cols );
+		function refreshUniformsLambert( uniforms, material ) {
 
-				var rows = cols - i;
+			if ( material.emissiveMap ) {
 
-				for ( j = 0; j <= rows; j ++ ) {
+				uniforms.emissiveMap.value = material.emissiveMap;
 
-					if ( j === 0 && i === cols ) {
+			}
 
-						v[ i ][ j ] = aj;
+		}
 
-					} else {
+		function refreshUniformsPhong( uniforms, material ) {
 
-						v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
+			uniforms.specular.value.copy( material.specular );
+			uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
 
-					}
+			if ( material.emissiveMap ) {
 
-				}
+				uniforms.emissiveMap.value = material.emissiveMap;
 
 			}
 
-			// construct all of the faces
-
-			for ( i = 0; i < cols; i ++ ) {
-
-				for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
+			if ( material.bumpMap ) {
 
-					var k = Math.floor( j / 2 );
+				uniforms.bumpMap.value = material.bumpMap;
+				uniforms.bumpScale.value = material.bumpScale;
+				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
 
-					if ( j % 2 === 0 ) {
+			}
 
-						pushVertex( v[ i ][ k + 1 ] );
-						pushVertex( v[ i + 1 ][ k ] );
-						pushVertex( v[ i ][ k ] );
+			if ( material.normalMap ) {
 
-					} else {
+				uniforms.normalMap.value = material.normalMap;
+				uniforms.normalScale.value.copy( material.normalScale );
+				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
 
-						pushVertex( v[ i ][ k + 1 ] );
-						pushVertex( v[ i + 1 ][ k + 1 ] );
-						pushVertex( v[ i + 1 ][ k ] );
+			}
 
-					}
+			if ( material.displacementMap ) {
 
-				}
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
 			}
 
 		}
 
-		function applyRadius( radius ) {
+		function refreshUniformsToon( uniforms, material ) {
 
-			var vertex = new Vector3();
+			uniforms.specular.value.copy( material.specular );
+			uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )
 
-			// iterate over the entire buffer and apply the radius to each vertex
+			if ( material.gradientMap ) {
 
-			for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
+				uniforms.gradientMap.value = material.gradientMap;
 
-				vertex.x = vertexBuffer[ i + 0 ];
-				vertex.y = vertexBuffer[ i + 1 ];
-				vertex.z = vertexBuffer[ i + 2 ];
+			}
 
-				vertex.normalize().multiplyScalar( radius );
+			if ( material.emissiveMap ) {
 
-				vertexBuffer[ i + 0 ] = vertex.x;
-				vertexBuffer[ i + 1 ] = vertex.y;
-				vertexBuffer[ i + 2 ] = vertex.z;
+				uniforms.emissiveMap.value = material.emissiveMap;
 
 			}
 
-		}
-
-		function generateUVs() {
+			if ( material.bumpMap ) {
 
-			var vertex = new Vector3();
+				uniforms.bumpMap.value = material.bumpMap;
+				uniforms.bumpScale.value = material.bumpScale;
+				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
 
-			for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
+			}
 
-				vertex.x = vertexBuffer[ i + 0 ];
-				vertex.y = vertexBuffer[ i + 1 ];
-				vertex.z = vertexBuffer[ i + 2 ];
+			if ( material.normalMap ) {
 
-				var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
-				var v = inclination( vertex ) / Math.PI + 0.5;
-				uvBuffer.push( u, 1 - v );
+				uniforms.normalMap.value = material.normalMap;
+				uniforms.normalScale.value.copy( material.normalScale );
+				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
 
 			}
 
-			correctUVs();
+			if ( material.displacementMap ) {
 
-			correctSeam();
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
+
+			}
 
 		}
 
-		function correctSeam() {
+		function refreshUniformsStandard( uniforms, material, environment ) {
 
-			// handle case when face straddles the seam, see #3269
+			uniforms.roughness.value = material.roughness;
+			uniforms.metalness.value = material.metalness;
 
-			for ( var i = 0; i < uvBuffer.length; i += 6 ) {
+			if ( material.roughnessMap ) {
 
-				// uv data of a single face
+				uniforms.roughnessMap.value = material.roughnessMap;
 
-				var x0 = uvBuffer[ i + 0 ];
-				var x1 = uvBuffer[ i + 2 ];
-				var x2 = uvBuffer[ i + 4 ];
+			}
 
-				var max = Math.max( x0, x1, x2 );
-				var min = Math.min( x0, x1, x2 );
+			if ( material.metalnessMap ) {
 
-				// 0.9 is somewhat arbitrary
+				uniforms.metalnessMap.value = material.metalnessMap;
 
-				if ( max > 0.9 && min < 0.1 ) {
+			}
 
-					if ( x0 < 0.2 ) { uvBuffer[ i + 0 ] += 1; }
-					if ( x1 < 0.2 ) { uvBuffer[ i + 2 ] += 1; }
-					if ( x2 < 0.2 ) { uvBuffer[ i + 4 ] += 1; }
+			if ( material.emissiveMap ) {
 
-				}
+				uniforms.emissiveMap.value = material.emissiveMap;
 
 			}
 
-		}
-
-		function pushVertex( vertex ) {
+			if ( material.bumpMap ) {
 
-			vertexBuffer.push( vertex.x, vertex.y, vertex.z );
+				uniforms.bumpMap.value = material.bumpMap;
+				uniforms.bumpScale.value = material.bumpScale;
+				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
 
-		}
+			}
 
-		function getVertexByIndex( index, vertex ) {
+			if ( material.normalMap ) {
 
-			var stride = index * 3;
+				uniforms.normalMap.value = material.normalMap;
+				uniforms.normalScale.value.copy( material.normalScale );
+				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
 
-			vertex.x = vertices[ stride + 0 ];
-			vertex.y = vertices[ stride + 1 ];
-			vertex.z = vertices[ stride + 2 ];
+			}
 
-		}
+			if ( material.displacementMap ) {
 
-		function correctUVs() {
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
-			var a = new Vector3();
-			var b = new Vector3();
-			var c = new Vector3();
+			}
 
-			var centroid = new Vector3();
+			if ( material.envMap || environment ) {
 
-			var uvA = new Vector2();
-			var uvB = new Vector2();
-			var uvC = new Vector2();
+				//uniforms.envMap.value = material.envMap; // part of uniforms common
+				uniforms.envMapIntensity.value = material.envMapIntensity;
 
-			for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
+			}
 
-				a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
-				b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
-				c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
+		}
 
-				uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
-				uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
-				uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
+		function refreshUniformsPhysical( uniforms, material, environment ) {
 
-				centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
+			refreshUniformsStandard( uniforms, material, environment );
 
-				var azi = azimuth( centroid );
+			uniforms.reflectivity.value = material.reflectivity; // also part of uniforms common
 
-				correctUV( uvA, j + 0, a, azi );
-				correctUV( uvB, j + 2, b, azi );
-				correctUV( uvC, j + 4, c, azi );
+			uniforms.clearcoat.value = material.clearcoat;
+			uniforms.clearcoatRoughness.value = material.clearcoatRoughness;
+			if ( material.sheen ) { uniforms.sheen.value.copy( material.sheen ); }
 
-			}
+			if ( material.clearcoatMap ) {
 
-		}
+				uniforms.clearcoatMap.value = material.clearcoatMap;
 
-		function correctUV( uv, stride, vector, azimuth ) {
+			}
 
-			if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
+			if ( material.clearcoatRoughnessMap ) {
 
-				uvBuffer[ stride ] = uv.x - 1;
+				uniforms.clearcoatRoughnessMap.value = material.clearcoatRoughnessMap;
 
 			}
 
-			if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
+			if ( material.clearcoatNormalMap ) {
 
-				uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
+				uniforms.clearcoatNormalScale.value.copy( material.clearcoatNormalScale );
+				uniforms.clearcoatNormalMap.value = material.clearcoatNormalMap;
 
-			}
+				if ( material.side === BackSide ) {
 
-		}
+					uniforms.clearcoatNormalScale.value.negate();
 
-		// Angle around the Y axis, counter-clockwise when looking from above.
+				}
 
-		function azimuth( vector ) {
+			}
 
-			return Math.atan2( vector.z, - vector.x );
+			uniforms.transparency.value = material.transparency;
 
 		}
 
+		function refreshUniformsMatcap( uniforms, material ) {
 
-		// Angle above the XZ plane.
+			if ( material.matcap ) {
 
-		function inclination( vector ) {
+				uniforms.matcap.value = material.matcap;
 
-			return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
+			}
 
-		}
+			if ( material.bumpMap ) {
 
-	}
+				uniforms.bumpMap.value = material.bumpMap;
+				uniforms.bumpScale.value = material.bumpScale;
+				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
 
-	PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
+			}
 
-	/**
-	 * @author timothypratley / https://github.com/timothypratley
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+			if ( material.normalMap ) {
 
-	// TetrahedronGeometry
+				uniforms.normalMap.value = material.normalMap;
+				uniforms.normalScale.value.copy( material.normalScale );
+				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
 
-	function TetrahedronGeometry( radius, detail ) {
+			}
 
-		Geometry.call( this );
+			if ( material.displacementMap ) {
 
-		this.type = 'TetrahedronGeometry';
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+			}
 
-		this.fromBufferGeometry( new TetrahedronBufferGeometry( radius, detail ) );
-		this.mergeVertices();
+		}
 
-	}
+		function refreshUniformsDepth( uniforms, material ) {
 
-	TetrahedronGeometry.prototype = Object.create( Geometry.prototype );
-	TetrahedronGeometry.prototype.constructor = TetrahedronGeometry;
+			if ( material.displacementMap ) {
 
-	// TetrahedronBufferGeometry
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
-	function TetrahedronBufferGeometry( radius, detail ) {
+			}
 
-		var vertices = [
-			1, 1, 1, 	- 1, - 1, 1, 	- 1, 1, - 1, 	1, - 1, - 1
-		];
+		}
 
-		var indices = [
-			2, 1, 0, 	0, 3, 2,	1, 3, 0,	2, 3, 1
-		];
+		function refreshUniformsDistance( uniforms, material ) {
 
-		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
+			if ( material.displacementMap ) {
 
-		this.type = 'TetrahedronBufferGeometry';
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+			}
 
-	}
+			uniforms.referencePosition.value.copy( material.referencePosition );
+			uniforms.nearDistance.value = material.nearDistance;
+			uniforms.farDistance.value = material.farDistance;
 
-	TetrahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
-	TetrahedronBufferGeometry.prototype.constructor = TetrahedronBufferGeometry;
+		}
 
-	/**
-	 * @author timothypratley / https://github.com/timothypratley
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+		function refreshUniformsNormal( uniforms, material ) {
 
-	// OctahedronGeometry
+			if ( material.bumpMap ) {
 
-	function OctahedronGeometry( radius, detail ) {
+				uniforms.bumpMap.value = material.bumpMap;
+				uniforms.bumpScale.value = material.bumpScale;
+				if ( material.side === BackSide ) { uniforms.bumpScale.value *= - 1; }
 
-		Geometry.call( this );
+			}
 
-		this.type = 'OctahedronGeometry';
+			if ( material.normalMap ) {
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+				uniforms.normalMap.value = material.normalMap;
+				uniforms.normalScale.value.copy( material.normalScale );
+				if ( material.side === BackSide ) { uniforms.normalScale.value.negate(); }
 
-		this.fromBufferGeometry( new OctahedronBufferGeometry( radius, detail ) );
-		this.mergeVertices();
+			}
 
-	}
+			if ( material.displacementMap ) {
 
-	OctahedronGeometry.prototype = Object.create( Geometry.prototype );
-	OctahedronGeometry.prototype.constructor = OctahedronGeometry;
-
-	// OctahedronBufferGeometry
+				uniforms.displacementMap.value = material.displacementMap;
+				uniforms.displacementScale.value = material.displacementScale;
+				uniforms.displacementBias.value = material.displacementBias;
 
-	function OctahedronBufferGeometry( radius, detail ) {
+			}
 
-		var vertices = [
-			1, 0, 0, 	- 1, 0, 0,	0, 1, 0,
-			0, - 1, 0, 	0, 0, 1,	0, 0, - 1
-		];
+		}
 
-		var indices = [
-			0, 2, 4,	0, 4, 3,	0, 3, 5,
-			0, 5, 2,	1, 2, 5,	1, 5, 3,
-			1, 3, 4,	1, 4, 2
-		];
+		// If uniforms are marked as clean, they don't need to be loaded to the GPU.
 
-		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
+		function markUniformsLightsNeedsUpdate( uniforms, value ) {
 
-		this.type = 'OctahedronBufferGeometry';
+			uniforms.ambientLightColor.needsUpdate = value;
+			uniforms.lightProbe.needsUpdate = value;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+			uniforms.directionalLights.needsUpdate = value;
+			uniforms.directionalLightShadows.needsUpdate = value;
+			uniforms.pointLights.needsUpdate = value;
+			uniforms.pointLightShadows.needsUpdate = value;
+			uniforms.spotLights.needsUpdate = value;
+			uniforms.spotLightShadows.needsUpdate = value;
+			uniforms.rectAreaLights.needsUpdate = value;
+			uniforms.hemisphereLights.needsUpdate = value;
 
-	}
+		}
 
-	OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
-	OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
+		function materialNeedsLights( material ) {
 
-	/**
-	 * @author timothypratley / https://github.com/timothypratley
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+			return material.isMeshLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial ||
+				material.isMeshStandardMaterial || material.isShadowMaterial ||
+				( material.isShaderMaterial && material.lights === true );
 
-	// IcosahedronGeometry
+		}
 
-	function IcosahedronGeometry( radius, detail ) {
+		//
+		this.setFramebuffer = function ( value ) {
 
-		Geometry.call( this );
+			if ( _framebuffer !== value && _currentRenderTarget === null ) { _gl.bindFramebuffer( 36160, value ); }
 
-		this.type = 'IcosahedronGeometry';
+			_framebuffer = value;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
 		};
 
-		this.fromBufferGeometry( new IcosahedronBufferGeometry( radius, detail ) );
-		this.mergeVertices();
-
-	}
-
-	IcosahedronGeometry.prototype = Object.create( Geometry.prototype );
-	IcosahedronGeometry.prototype.constructor = IcosahedronGeometry;
+		this.getActiveCubeFace = function () {
 
-	// IcosahedronBufferGeometry
+			return _currentActiveCubeFace;
 
-	function IcosahedronBufferGeometry( radius, detail ) {
+		};
 
-		var t = ( 1 + Math.sqrt( 5 ) ) / 2;
+		this.getActiveMipmapLevel = function () {
 
-		var vertices = [
-			- 1, t, 0, 	1, t, 0, 	- 1, - t, 0, 	1, - t, 0,
-			 0, - 1, t, 	0, 1, t,	0, - 1, - t, 	0, 1, - t,
-			 t, 0, - 1, 	t, 0, 1, 	- t, 0, - 1, 	- t, 0, 1
-		];
+			return _currentActiveMipmapLevel;
 
-		var indices = [
-			 0, 11, 5, 	0, 5, 1, 	0, 1, 7, 	0, 7, 10, 	0, 10, 11,
-			 1, 5, 9, 	5, 11, 4,	11, 10, 2,	10, 7, 6,	7, 1, 8,
-			 3, 9, 4, 	3, 4, 2,	3, 2, 6,	3, 6, 8,	3, 8, 9,
-			 4, 9, 5, 	2, 4, 11,	6, 2, 10,	8, 6, 7,	9, 8, 1
-		];
+		};
 
-		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
+		this.getRenderTarget = function () {
 
-		this.type = 'IcosahedronBufferGeometry';
+			return _currentRenderTarget;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
 		};
 
-	}
-
-	IcosahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
-	IcosahedronBufferGeometry.prototype.constructor = IcosahedronBufferGeometry;
+		this.setRenderTarget = function ( renderTarget, activeCubeFace, activeMipmapLevel ) {
 
-	/**
-	 * @author Abe Pazos / https://hamoid.com
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+			_currentRenderTarget = renderTarget;
+			_currentActiveCubeFace = activeCubeFace;
+			_currentActiveMipmapLevel = activeMipmapLevel;
 
-	// DodecahedronGeometry
+			if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
 
-	function DodecahedronGeometry( radius, detail ) {
+				textures.setupRenderTarget( renderTarget );
 
-		Geometry.call( this );
+			}
 
-		this.type = 'DodecahedronGeometry';
+			var framebuffer = _framebuffer;
+			var isCube = false;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+			if ( renderTarget ) {
 
-		this.fromBufferGeometry( new DodecahedronBufferGeometry( radius, detail ) );
-		this.mergeVertices();
+				var __webglFramebuffer = properties.get( renderTarget ).__webglFramebuffer;
 
-	}
+				if ( renderTarget.isWebGLCubeRenderTarget ) {
 
-	DodecahedronGeometry.prototype = Object.create( Geometry.prototype );
-	DodecahedronGeometry.prototype.constructor = DodecahedronGeometry;
+					framebuffer = __webglFramebuffer[ activeCubeFace || 0 ];
+					isCube = true;
 
-	// DodecahedronBufferGeometry
+				} else if ( renderTarget.isWebGLMultisampleRenderTarget ) {
 
-	function DodecahedronBufferGeometry( radius, detail ) {
+					framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer;
 
-		var t = ( 1 + Math.sqrt( 5 ) ) / 2;
-		var r = 1 / t;
+				} else {
 
-		var vertices = [
+					framebuffer = __webglFramebuffer;
 
-			// (±1, ±1, ±1)
-			- 1, - 1, - 1,	- 1, - 1, 1,
-			- 1, 1, - 1, - 1, 1, 1,
-			1, - 1, - 1, 1, - 1, 1,
-			1, 1, - 1, 1, 1, 1,
+				}
 
-			// (0, ±1/φ, ±φ)
-			 0, - r, - t, 0, - r, t,
-			 0, r, - t, 0, r, t,
+				_currentViewport.copy( renderTarget.viewport );
+				_currentScissor.copy( renderTarget.scissor );
+				_currentScissorTest = renderTarget.scissorTest;
 
-			// (±1/φ, ±φ, 0)
-			- r, - t, 0, - r, t, 0,
-			 r, - t, 0, r, t, 0,
+			} else {
 
-			// (±φ, 0, ±1/φ)
-			- t, 0, - r, t, 0, - r,
-			- t, 0, r, t, 0, r
-		];
+				_currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor();
+				_currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor();
+				_currentScissorTest = _scissorTest;
 
-		var indices = [
-			3, 11, 7, 	3, 7, 15, 	3, 15, 13,
-			7, 19, 17, 	7, 17, 6, 	7, 6, 15,
-			17, 4, 8, 	17, 8, 10, 	17, 10, 6,
-			8, 0, 16, 	8, 16, 2, 	8, 2, 10,
-			0, 12, 1, 	0, 1, 18, 	0, 18, 16,
-			6, 10, 2, 	6, 2, 13, 	6, 13, 15,
-			2, 16, 18, 	2, 18, 3, 	2, 3, 13,
-			18, 1, 9, 	18, 9, 11, 	18, 11, 3,
-			4, 14, 12, 	4, 12, 0, 	4, 0, 8,
-			11, 9, 5, 	11, 5, 19, 	11, 19, 7,
-			19, 5, 14, 	19, 14, 4, 	19, 4, 17,
-			1, 12, 14, 	1, 14, 5, 	1, 5, 9
-		];
+			}
 
-		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
+			if ( _currentFramebuffer !== framebuffer ) {
 
-		this.type = 'DodecahedronBufferGeometry';
+				_gl.bindFramebuffer( 36160, framebuffer );
+				_currentFramebuffer = framebuffer;
 
-		this.parameters = {
-			radius: radius,
-			detail: detail
-		};
+			}
 
-	}
+			state.viewport( _currentViewport );
+			state.scissor( _currentScissor );
+			state.setScissorTest( _currentScissorTest );
 
-	DodecahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
-	DodecahedronBufferGeometry.prototype.constructor = DodecahedronBufferGeometry;
+			if ( isCube ) {
 
-	/**
-	 * @author oosmoxiecode / https://github.com/oosmoxiecode
-	 * @author WestLangley / https://github.com/WestLangley
-	 * @author zz85 / https://github.com/zz85
-	 * @author miningold / https://github.com/miningold
-	 * @author jonobr1 / https://github.com/jonobr1
-	 * @author Mugen87 / https://github.com/Mugen87
-	 *
-	 */
+				var textureProperties = properties.get( renderTarget.texture );
+				_gl.framebufferTexture2D( 36160, 36064, 34069 + ( activeCubeFace || 0 ), textureProperties.__webglTexture, activeMipmapLevel || 0 );
 
-	// TubeGeometry
+			}
 
-	function TubeGeometry( path, tubularSegments, radius, radialSegments, closed, taper ) {
+		};
 
-		Geometry.call( this );
+		this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
 
-		this.type = 'TubeGeometry';
+			if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
 
-		this.parameters = {
-			path: path,
-			tubularSegments: tubularSegments,
-			radius: radius,
-			radialSegments: radialSegments,
-			closed: closed
-		};
+				console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
+				return;
 
-		if ( taper !== undefined ) { console.warn( 'THREE.TubeGeometry: taper has been removed.' ); }
+			}
 
-		var bufferGeometry = new TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed );
+			var framebuffer = properties.get( renderTarget ).__webglFramebuffer;
 
-		// expose internals
+			if ( renderTarget.isWebGLCubeRenderTarget && activeCubeFaceIndex !== undefined ) {
 
-		this.tangents = bufferGeometry.tangents;
-		this.normals = bufferGeometry.normals;
-		this.binormals = bufferGeometry.binormals;
+				framebuffer = framebuffer[ activeCubeFaceIndex ];
 
-		// create geometry
+			}
 
-		this.fromBufferGeometry( bufferGeometry );
-		this.mergeVertices();
+			if ( framebuffer ) {
 
-	}
+				var restore = false;
 
-	TubeGeometry.prototype = Object.create( Geometry.prototype );
-	TubeGeometry.prototype.constructor = TubeGeometry;
+				if ( framebuffer !== _currentFramebuffer ) {
 
-	// TubeBufferGeometry
+					_gl.bindFramebuffer( 36160, framebuffer );
 
-	function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed ) {
+					restore = true;
 
-		BufferGeometry.call( this );
+				}
 
-		this.type = 'TubeBufferGeometry';
+				try {
 
-		this.parameters = {
-			path: path,
-			tubularSegments: tubularSegments,
-			radius: radius,
-			radialSegments: radialSegments,
-			closed: closed
-		};
+					var texture = renderTarget.texture;
+					var textureFormat = texture.format;
+					var textureType = texture.type;
 
-		tubularSegments = tubularSegments || 64;
-		radius = radius || 1;
-		radialSegments = radialSegments || 8;
-		closed = closed || false;
+					if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( 35739 ) ) {
 
-		var frames = path.computeFrenetFrames( tubularSegments, closed );
+						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
+						return;
 
-		// expose internals
+					}
 
-		this.tangents = frames.tangents;
-		this.normals = frames.normals;
-		this.binormals = frames.binormals;
+					if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( 35738 ) && // IE11, Edge and Chrome Mac < 52 (#9513)
+						! ( textureType === FloatType && ( capabilities.isWebGL2 || extensions.get( 'OES_texture_float' ) || extensions.get( 'WEBGL_color_buffer_float' ) ) ) && // Chrome Mac >= 52 and Firefox
+						! ( textureType === HalfFloatType && ( capabilities.isWebGL2 ? extensions.get( 'EXT_color_buffer_float' ) : extensions.get( 'EXT_color_buffer_half_float' ) ) ) ) {
 
-		// helper variables
+						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
+						return;
 
-		var vertex = new Vector3();
-		var normal = new Vector3();
-		var uv = new Vector2();
-		var P = new Vector3();
+					}
 
-		var i, j;
+					if ( _gl.checkFramebufferStatus( 36160 ) === 36053 ) {
 
-		// buffer
+						// the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604)
 
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
-		var indices = [];
+						if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) {
 
-		// create buffer data
+							_gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer );
 
-		generateBufferData();
+						}
 
-		// build geometry
+					} else {
 
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+						console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );
 
-		// functions
+					}
 
-		function generateBufferData() {
+				} finally {
 
-			for ( i = 0; i < tubularSegments; i ++ ) {
+					if ( restore ) {
 
-				generateSegment( i );
+						_gl.bindFramebuffer( 36160, _currentFramebuffer );
 
-			}
+					}
 
-			// if the geometry is not closed, generate the last row of vertices and normals
-			// at the regular position on the given path
-			//
-			// if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
+				}
 
-			generateSegment( ( closed === false ) ? tubularSegments : 0 );
+			}
 
-			// uvs are generated in a separate function.
-			// this makes it easy compute correct values for closed geometries
+		};
 
-			generateUVs();
+		this.copyFramebufferToTexture = function ( position, texture, level ) {
 
-			// finally create faces
+			if ( level === undefined ) { level = 0; }
 
-			generateIndices();
+			var levelScale = Math.pow( 2, - level );
+			var width = Math.floor( texture.image.width * levelScale );
+			var height = Math.floor( texture.image.height * levelScale );
+			var glFormat = utils.convert( texture.format );
 
-		}
+			textures.setTexture2D( texture, 0 );
 
-		function generateSegment( i ) {
+			_gl.copyTexImage2D( 3553, level, glFormat, position.x, position.y, width, height, 0 );
 
-			// we use getPointAt to sample evenly distributed points from the given path
+			state.unbindTexture();
 
-			P = path.getPointAt( i / tubularSegments, P );
+		};
 
-			// retrieve corresponding normal and binormal
+		this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level ) {
 
-			var N = frames.normals[ i ];
-			var B = frames.binormals[ i ];
+			if ( level === undefined ) { level = 0; }
 
-			// generate normals and vertices for the current segment
+			var width = srcTexture.image.width;
+			var height = srcTexture.image.height;
+			var glFormat = utils.convert( dstTexture.format );
+			var glType = utils.convert( dstTexture.type );
 
-			for ( j = 0; j <= radialSegments; j ++ ) {
+			textures.setTexture2D( dstTexture, 0 );
 
-				var v = j / radialSegments * Math.PI * 2;
+			if ( srcTexture.isDataTexture ) {
 
-				var sin = Math.sin( v );
-				var cos = - Math.cos( v );
+				_gl.texSubImage2D( 3553, level, position.x, position.y, width, height, glFormat, glType, srcTexture.image.data );
 
-				// normal
+			} else {
 
-				normal.x = ( cos * N.x + sin * B.x );
-				normal.y = ( cos * N.y + sin * B.y );
-				normal.z = ( cos * N.z + sin * B.z );
-				normal.normalize();
+				if ( srcTexture.isCompressedTexture ) {
 
-				normals.push( normal.x, normal.y, normal.z );
+					_gl.compressedTexSubImage2D( 3553, level, position.x, position.y, srcTexture.mipmaps[ 0 ].width, srcTexture.mipmaps[ 0 ].height, glFormat, srcTexture.mipmaps[ 0 ].data );
 
-				// vertex
+				} else {
 
-				vertex.x = P.x + radius * normal.x;
-				vertex.y = P.y + radius * normal.y;
-				vertex.z = P.z + radius * normal.z;
+					_gl.texSubImage2D( 3553, level, position.x, position.y, glFormat, glType, srcTexture.image );
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+				}
 
 			}
 
-		}
+			// Generate mipmaps only when copying level 0
+			if ( level === 0 && dstTexture.generateMipmaps ) { _gl.generateMipmap( 3553 ); }
 
-		function generateIndices() {
+			state.unbindTexture();
 
-			for ( j = 1; j <= tubularSegments; j ++ ) {
+		};
 
-				for ( i = 1; i <= radialSegments; i ++ ) {
+		this.initTexture = function ( texture ) {
 
-					var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
-					var b = ( radialSegments + 1 ) * j + ( i - 1 );
-					var c = ( radialSegments + 1 ) * j + i;
-					var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
+			textures.setTexture2D( texture, 0 );
 
-					// faces
+			state.unbindTexture();
 
-					indices.push( a, b, d );
-					indices.push( b, c, d );
+		};
 
-				}
+		if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
 
-			}
+			__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef
 
 		}
 
-		function generateUVs() {
+	}
 
-			for ( i = 0; i <= tubularSegments; i ++ ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				for ( j = 0; j <= radialSegments; j ++ ) {
+	function FogExp2( color, density ) {
 
-					uv.x = i / tubularSegments;
-					uv.y = j / radialSegments;
+		this.name = '';
 
-					uvs.push( uv.x, uv.y );
+		this.color = new Color( color );
+		this.density = ( density !== undefined ) ? density : 0.00025;
 
-				}
+	}
 
-			}
+	Object.assign( FogExp2.prototype, {
 
-		}
+		isFogExp2: true,
 
-	}
+		clone: function () {
 
-	TubeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	TubeBufferGeometry.prototype.constructor = TubeBufferGeometry;
+			return new FogExp2( this.color, this.density );
 
-	TubeBufferGeometry.prototype.toJSON = function () {
+		},
 
-		var data = BufferGeometry.prototype.toJSON.call( this );
+		toJSON: function ( /* meta */ ) {
 
-		data.path = this.parameters.path.toJSON();
+			return {
+				type: 'FogExp2',
+				color: this.color.getHex(),
+				density: this.density
+			};
 
-		return data;
+		}
 
-	};
+	} );
 
 	/**
-	 * @author oosmoxiecode
-	 * @author Mugen87 / https://github.com/Mugen87
-	 *
-	 * based on http://www.blackpawn.com/texts/pqtorus/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
 	 */
 
-	// TorusKnotGeometry
-
-	function TorusKnotGeometry( radius, tube, tubularSegments, radialSegments, p, q, heightScale ) {
-
-		Geometry.call( this );
-
-		this.type = 'TorusKnotGeometry';
+	function Fog( color, near, far ) {
 
-		this.parameters = {
-			radius: radius,
-			tube: tube,
-			tubularSegments: tubularSegments,
-			radialSegments: radialSegments,
-			p: p,
-			q: q
-		};
+		this.name = '';
 
-		if ( heightScale !== undefined ) { console.warn( 'THREE.TorusKnotGeometry: heightScale has been deprecated. Use .scale( x, y, z ) instead.' ); }
+		this.color = new Color( color );
 
-		this.fromBufferGeometry( new TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) );
-		this.mergeVertices();
+		this.near = ( near !== undefined ) ? near : 1;
+		this.far = ( far !== undefined ) ? far : 1000;
 
 	}
 
-	TorusKnotGeometry.prototype = Object.create( Geometry.prototype );
-	TorusKnotGeometry.prototype.constructor = TorusKnotGeometry;
+	Object.assign( Fog.prototype, {
 
-	// TorusKnotBufferGeometry
+		isFog: true,
 
-	function TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) {
+		clone: function () {
 
-		BufferGeometry.call( this );
+			return new Fog( this.color, this.near, this.far );
 
-		this.type = 'TorusKnotBufferGeometry';
+		},
 
-		this.parameters = {
-			radius: radius,
-			tube: tube,
-			tubularSegments: tubularSegments,
-			radialSegments: radialSegments,
-			p: p,
-			q: q
-		};
+		toJSON: function ( /* meta */ ) {
 
-		radius = radius || 1;
-		tube = tube || 0.4;
-		tubularSegments = Math.floor( tubularSegments ) || 64;
-		radialSegments = Math.floor( radialSegments ) || 8;
-		p = p || 2;
-		q = q || 3;
+			return {
+				type: 'Fog',
+				color: this.color.getHex(),
+				near: this.near,
+				far: this.far
+			};
 
-		// buffers
+		}
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+	} );
 
-		// helper variables
+	/**
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 */
 
-		var i, j;
+	function InterleavedBuffer( array, stride ) {
 
-		var vertex = new Vector3();
-		var normal = new Vector3();
+		this.array = array;
+		this.stride = stride;
+		this.count = array !== undefined ? array.length / stride : 0;
 
-		var P1 = new Vector3();
-		var P2 = new Vector3();
+		this.usage = StaticDrawUsage;
+		this.updateRange = { offset: 0, count: - 1 };
 
-		var B = new Vector3();
-		var T = new Vector3();
-		var N = new Vector3();
+		this.version = 0;
 
-		// generate vertices, normals and uvs
+	}
 
-		for ( i = 0; i <= tubularSegments; ++ i ) {
+	Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', {
 
-			// the radian "u" is used to calculate the position on the torus curve of the current tubular segement
+		set: function ( value ) {
 
-			var u = i / tubularSegments * p * Math.PI * 2;
+			if ( value === true ) { this.version ++; }
 
-			// now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
-			// these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions
+		}
 
-			calculatePositionOnCurve( u, p, q, radius, P1 );
-			calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );
+	} );
 
-			// calculate orthonormal basis
+	Object.assign( InterleavedBuffer.prototype, {
 
-			T.subVectors( P2, P1 );
-			N.addVectors( P2, P1 );
-			B.crossVectors( T, N );
-			N.crossVectors( B, T );
+		isInterleavedBuffer: true,
 
-			// normalize B, N. T can be ignored, we don't use it
+		onUploadCallback: function () {},
 
-			B.normalize();
-			N.normalize();
+		setUsage: function ( value ) {
 
-			for ( j = 0; j <= radialSegments; ++ j ) {
+			this.usage = value;
 
-				// now calculate the vertices. they are nothing more than an extrusion of the torus curve.
-				// because we extrude a shape in the xy-plane, there is no need to calculate a z-value.
+			return this;
 
-				var v = j / radialSegments * Math.PI * 2;
-				var cx = - tube * Math.cos( v );
-				var cy = tube * Math.sin( v );
+		},
 
-				// now calculate the final vertex position.
-				// first we orient the extrusion with our basis vectos, then we add it to the current position on the curve
+		copy: function ( source ) {
 
-				vertex.x = P1.x + ( cx * N.x + cy * B.x );
-				vertex.y = P1.y + ( cx * N.y + cy * B.y );
-				vertex.z = P1.z + ( cx * N.z + cy * B.z );
+			this.array = new source.array.constructor( source.array );
+			this.count = source.count;
+			this.stride = source.stride;
+			this.usage = source.usage;
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+			return this;
 
-				// normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
+		},
 
-				normal.subVectors( vertex, P1 ).normalize();
+		copyAt: function ( index1, attribute, index2 ) {
 
-				normals.push( normal.x, normal.y, normal.z );
+			index1 *= this.stride;
+			index2 *= attribute.stride;
 
-				// uv
+			for ( var i = 0, l = this.stride; i < l; i ++ ) {
 
-				uvs.push( i / tubularSegments );
-				uvs.push( j / radialSegments );
+				this.array[ index1 + i ] = attribute.array[ index2 + i ];
 
 			}
 
-		}
-
-		// generate indices
-
-		for ( j = 1; j <= tubularSegments; j ++ ) {
-
-			for ( i = 1; i <= radialSegments; i ++ ) {
+			return this;
 
-				// indices
+		},
 
-				var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
-				var b = ( radialSegments + 1 ) * j + ( i - 1 );
-				var c = ( radialSegments + 1 ) * j + i;
-				var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
+		set: function ( value, offset ) {
 
-				// faces
+			if ( offset === undefined ) { offset = 0; }
 
-				indices.push( a, b, d );
-				indices.push( b, c, d );
+			this.array.set( value, offset );
 
-			}
+			return this;
 
-		}
+		},
 
-		// build geometry
+		clone: function () {
 
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+			return new this.constructor().copy( this );
 
-		// this function calculates the current position on the torus curve
+		},
 
-		function calculatePositionOnCurve( u, p, q, radius, position ) {
+		onUpload: function ( callback ) {
 
-			var cu = Math.cos( u );
-			var su = Math.sin( u );
-			var quOverP = q / p * u;
-			var cs = Math.cos( quOverP );
+			this.onUploadCallback = callback;
 
-			position.x = radius * ( 2 + cs ) * 0.5 * cu;
-			position.y = radius * ( 2 + cs ) * su * 0.5;
-			position.z = radius * Math.sin( quOverP ) * 0.5;
+			return this;
 
 		}
 
-	}
-
-	TorusKnotBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	TorusKnotBufferGeometry.prototype.constructor = TorusKnotBufferGeometry;
+	} );
 
 	/**
-	 * @author oosmoxiecode
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author benaadams / https://twitter.com/ben_a_adams
 	 */
 
-	// TorusGeometry
-
-	function TorusGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
-
-		Geometry.call( this );
+	var _vector$6 = new Vector3();
 
-		this.type = 'TorusGeometry';
+	function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) {
 
-		this.parameters = {
-			radius: radius,
-			tube: tube,
-			radialSegments: radialSegments,
-			tubularSegments: tubularSegments,
-			arc: arc
-		};
+		this.data = interleavedBuffer;
+		this.itemSize = itemSize;
+		this.offset = offset;
 
-		this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) );
-		this.mergeVertices();
+		this.normalized = normalized === true;
 
 	}
 
-	TorusGeometry.prototype = Object.create( Geometry.prototype );
-	TorusGeometry.prototype.constructor = TorusGeometry;
+	Object.defineProperties( InterleavedBufferAttribute.prototype, {
 
-	// TorusBufferGeometry
+		count: {
 
-	function TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
+			get: function () {
 
-		BufferGeometry.call( this );
+				return this.data.count;
 
-		this.type = 'TorusBufferGeometry';
+			}
 
-		this.parameters = {
-			radius: radius,
-			tube: tube,
-			radialSegments: radialSegments,
-			tubularSegments: tubularSegments,
-			arc: arc
-		};
+		},
 
-		radius = radius || 1;
-		tube = tube || 0.4;
-		radialSegments = Math.floor( radialSegments ) || 8;
-		tubularSegments = Math.floor( tubularSegments ) || 6;
-		arc = arc || Math.PI * 2;
+		array: {
 
-		// buffers
+			get: function () {
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+				return this.data.array;
 
-		// helper variables
+			}
 
-		var center = new Vector3();
-		var vertex = new Vector3();
-		var normal = new Vector3();
+		}
 
-		var j, i;
+	} );
 
-		// generate vertices, normals and uvs
+	Object.assign( InterleavedBufferAttribute.prototype, {
 
-		for ( j = 0; j <= radialSegments; j ++ ) {
+		isInterleavedBufferAttribute: true,
 
-			for ( i = 0; i <= tubularSegments; i ++ ) {
+		applyMatrix4: function ( m ) {
 
-				var u = i / tubularSegments * arc;
-				var v = j / radialSegments * Math.PI * 2;
+			for ( var i = 0, l = this.data.count; i < l; i ++ ) {
 
-				// vertex
+				_vector$6.x = this.getX( i );
+				_vector$6.y = this.getY( i );
+				_vector$6.z = this.getZ( i );
 
-				vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
-				vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
-				vertex.z = tube * Math.sin( v );
+				_vector$6.applyMatrix4( m );
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+				this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z );
 
-				// normal
+			}
 
-				center.x = radius * Math.cos( u );
-				center.y = radius * Math.sin( u );
-				normal.subVectors( vertex, center ).normalize();
+			return this;
 
-				normals.push( normal.x, normal.y, normal.z );
+		},
 
-				// uv
+		setX: function ( index, x ) {
 
-				uvs.push( i / tubularSegments );
-				uvs.push( j / radialSegments );
+			this.data.array[ index * this.data.stride + this.offset ] = x;
 
-			}
+			return this;
 
-		}
+		},
 
-		// generate indices
+		setY: function ( index, y ) {
 
-		for ( j = 1; j <= radialSegments; j ++ ) {
+			this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
 
-			for ( i = 1; i <= tubularSegments; i ++ ) {
+			return this;
 
-				// indices
+		},
 
-				var a = ( tubularSegments + 1 ) * j + i - 1;
-				var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
-				var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
-				var d = ( tubularSegments + 1 ) * j + i;
+		setZ: function ( index, z ) {
 
-				// faces
+			this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
 
-				indices.push( a, b, d );
-				indices.push( b, c, d );
+			return this;
 
-			}
+		},
 
-		}
+		setW: function ( index, w ) {
 
-		// build geometry
+			this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
 
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+			return this;
 
-	}
+		},
 
-	TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
+		getX: function ( index ) {
 
-	/**
-	 * @author Mugen87 / https://github.com/Mugen87
-	 * Port from https://github.com/mapbox/earcut (v2.2.2)
-	 */
+			return this.data.array[ index * this.data.stride + this.offset ];
 
-	var Earcut = {
+		},
 
-		triangulate: function ( data, holeIndices, dim ) {
+		getY: function ( index ) {
 
-			dim = dim || 2;
+			return this.data.array[ index * this.data.stride + this.offset + 1 ];
 
-			var hasHoles = holeIndices && holeIndices.length,
-				outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length,
-				outerNode = linkedList( data, 0, outerLen, dim, true ),
-				triangles = [];
+		},
 
-			if ( ! outerNode || outerNode.next === outerNode.prev ) { return triangles; }
+		getZ: function ( index ) {
 
-			var minX, minY, maxX, maxY, x, y, invSize;
+			return this.data.array[ index * this.data.stride + this.offset + 2 ];
 
-			if ( hasHoles ) { outerNode = eliminateHoles( data, holeIndices, outerNode, dim ); }
+		},
 
-			// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
-			if ( data.length > 80 * dim ) {
+		getW: function ( index ) {
 
-				minX = maxX = data[ 0 ];
-				minY = maxY = data[ 1 ];
+			return this.data.array[ index * this.data.stride + this.offset + 3 ];
 
-				for ( var i = dim; i < outerLen; i += dim ) {
+		},
 
-					x = data[ i ];
-					y = data[ i + 1 ];
-					if ( x < minX ) { minX = x; }
-					if ( y < minY ) { minY = y; }
-					if ( x > maxX ) { maxX = x; }
-					if ( y > maxY ) { maxY = y; }
+		setXY: function ( index, x, y ) {
 
-				}
+			index = index * this.data.stride + this.offset;
 
-				// minX, minY and invSize are later used to transform coords into integers for z-order calculation
-				invSize = Math.max( maxX - minX, maxY - minY );
-				invSize = invSize !== 0 ? 1 / invSize : 0;
+			this.data.array[ index + 0 ] = x;
+			this.data.array[ index + 1 ] = y;
 
-			}
+			return this;
 
-			earcutLinked( outerNode, triangles, dim, minX, minY, invSize );
+		},
 
-			return triangles;
+		setXYZ: function ( index, x, y, z ) {
 
-		}
+			index = index * this.data.stride + this.offset;
 
-	};
+			this.data.array[ index + 0 ] = x;
+			this.data.array[ index + 1 ] = y;
+			this.data.array[ index + 2 ] = z;
 
-	// create a circular doubly linked list from polygon points in the specified winding order
-	function linkedList( data, start, end, dim, clockwise ) {
+			return this;
 
-		var i, last;
+		},
 
-		if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) {
+		setXYZW: function ( index, x, y, z, w ) {
 
-			for ( i = start; i < end; i += dim ) { last = insertNode( i, data[ i ], data[ i + 1 ], last ); }
+			index = index * this.data.stride + this.offset;
 
-		} else {
+			this.data.array[ index + 0 ] = x;
+			this.data.array[ index + 1 ] = y;
+			this.data.array[ index + 2 ] = z;
+			this.data.array[ index + 3 ] = w;
 
-			for ( i = end - dim; i >= start; i -= dim ) { last = insertNode( i, data[ i ], data[ i + 1 ], last ); }
+			return this;
 
 		}
 
-		if ( last && equals( last, last.next ) ) {
+	} );
 
-			removeNode( last );
-			last = last.next;
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  map: new THREE.Texture( <Image> ),
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *  rotation: <float>,
+	 *  sizeAttenuation: <bool>
+	 * }
+	 */
 
-		}
+	function SpriteMaterial( parameters ) {
 
-		return last;
+		Material.call( this );
 
-	}
+		this.type = 'SpriteMaterial';
 
-	// eliminate colinear or duplicate points
-	function filterPoints( start, end ) {
+		this.color = new Color( 0xffffff );
 
-		if ( ! start ) { return start; }
-		if ( ! end ) { end = start; }
+		this.map = null;
 
-		var p = start,
-			again;
-		do {
+		this.alphaMap = null;
 
-			again = false;
+		this.rotation = 0;
 
-			if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) {
+		this.sizeAttenuation = true;
 
-				removeNode( p );
-				p = end = p.prev;
-				if ( p === p.next ) { break; }
-				again = true;
+		this.transparent = true;
 
-			} else {
+		this.setValues( parameters );
 
-				p = p.next;
+	}
 
-			}
+	SpriteMaterial.prototype = Object.create( Material.prototype );
+	SpriteMaterial.prototype.constructor = SpriteMaterial;
+	SpriteMaterial.prototype.isSpriteMaterial = true;
 
-		} while ( again || p !== end );
+	SpriteMaterial.prototype.copy = function ( source ) {
 
-		return end;
+		Material.prototype.copy.call( this, source );
 
-	}
+		this.color.copy( source.color );
 
-	// main ear slicing loop which triangulates a polygon (given as a linked list)
-	function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) {
+		this.map = source.map;
 
-		if ( ! ear ) { return; }
+		this.alphaMap = source.alphaMap;
 
-		// interlink polygon nodes in z-order
-		if ( ! pass && invSize ) { indexCurve( ear, minX, minY, invSize ); }
+		this.rotation = source.rotation;
 
-		var stop = ear,
-			prev, next;
+		this.sizeAttenuation = source.sizeAttenuation;
 
-		// iterate through ears, slicing them one by one
-		while ( ear.prev !== ear.next ) {
+		return this;
 
-			prev = ear.prev;
-			next = ear.next;
+	};
 
-			if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) {
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				// cut off the triangle
-				triangles.push( prev.i / dim );
-				triangles.push( ear.i / dim );
-				triangles.push( next.i / dim );
+	var _geometry;
 
-				removeNode( ear );
+	var _intersectPoint = new Vector3();
+	var _worldScale = new Vector3();
+	var _mvPosition = new Vector3();
 
-				// skipping the next vertex leads to less sliver triangles
-				ear = next.next;
-				stop = next.next;
+	var _alignedPosition = new Vector2();
+	var _rotatedPosition = new Vector2();
+	var _viewWorldMatrix = new Matrix4();
 
-				continue;
+	var _vA$1 = new Vector3();
+	var _vB$1 = new Vector3();
+	var _vC$1 = new Vector3();
 
-			}
+	var _uvA$1 = new Vector2();
+	var _uvB$1 = new Vector2();
+	var _uvC$1 = new Vector2();
 
-			ear = next;
+	function Sprite( material ) {
 
-			// if we looped through the whole remaining polygon and can't find any more ears
-			if ( ear === stop ) {
+		Object3D.call( this );
 
-				// try filtering points and slicing again
-				if ( ! pass ) {
+		this.type = 'Sprite';
 
-					earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 );
+		if ( _geometry === undefined ) {
 
-					// if this didn't work, try curing all small self-intersections locally
+			_geometry = new BufferGeometry();
 
-				} else if ( pass === 1 ) {
+			var float32Array = new Float32Array( [
+				- 0.5, - 0.5, 0, 0, 0,
+				0.5, - 0.5, 0, 1, 0,
+				0.5, 0.5, 0, 1, 1,
+				- 0.5, 0.5, 0, 0, 1
+			] );
 
-					ear = cureLocalIntersections( filterPoints( ear ), triangles, dim );
-					earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 );
+			var interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
 
-					// as a last resort, try splitting the remaining polygon into two
+			_geometry.setIndex( [ 0, 1, 2,	0, 2, 3 ] );
+			_geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
+			_geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
 
-				} else if ( pass === 2 ) {
+		}
 
-					splitEarcut( ear, triangles, dim, minX, minY, invSize );
+		this.geometry = _geometry;
+		this.material = ( material !== undefined ) ? material : new SpriteMaterial();
 
-				}
+		this.center = new Vector2( 0.5, 0.5 );
 
-				break;
+	}
 
-			}
+	Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-		}
+		constructor: Sprite,
 
-	}
+		isSprite: true,
 
-	// check whether a polygon node forms a valid ear with adjacent nodes
-	function isEar( ear ) {
+		raycast: function ( raycaster, intersects ) {
 
-		var a = ear.prev,
-			b = ear,
-			c = ear.next;
+			if ( raycaster.camera === null ) {
 
-		if ( area( a, b, c ) >= 0 ) { return false; } // reflex, can't be an ear
+				console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
 
-		// now make sure we don't have other points inside the potential ear
-		var p = ear.next.next;
+			}
 
-		while ( p !== ear.prev ) {
+			_worldScale.setFromMatrixScale( this.matrixWorld );
 
-			if ( pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
-				area( p.prev, p, p.next ) >= 0 ) { return false; }
-			p = p.next;
+			_viewWorldMatrix.copy( raycaster.camera.matrixWorld );
+			this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
 
-		}
+			_mvPosition.setFromMatrixPosition( this.modelViewMatrix );
 
-		return true;
+			if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
 
-	}
+				_worldScale.multiplyScalar( - _mvPosition.z );
 
-	function isEarHashed( ear, minX, minY, invSize ) {
+			}
 
-		var a = ear.prev,
-			b = ear,
-			c = ear.next;
+			var rotation = this.material.rotation;
+			var sin, cos;
+			if ( rotation !== 0 ) {
 
-		if ( area( a, b, c ) >= 0 ) { return false; } // reflex, can't be an ear
+				cos = Math.cos( rotation );
+				sin = Math.sin( rotation );
 
-		// triangle bbox; min & max are calculated like this for speed
-		var minTX = a.x < b.x ? ( a.x < c.x ? a.x : c.x ) : ( b.x < c.x ? b.x : c.x ),
-			minTY = a.y < b.y ? ( a.y < c.y ? a.y : c.y ) : ( b.y < c.y ? b.y : c.y ),
-			maxTX = a.x > b.x ? ( a.x > c.x ? a.x : c.x ) : ( b.x > c.x ? b.x : c.x ),
-			maxTY = a.y > b.y ? ( a.y > c.y ? a.y : c.y ) : ( b.y > c.y ? b.y : c.y );
+			}
 
-		// z-order range for the current triangle bbox;
-		var minZ = zOrder( minTX, minTY, minX, minY, invSize ),
-			maxZ = zOrder( maxTX, maxTY, minX, minY, invSize );
-
-		var p = ear.prevZ,
-			n = ear.nextZ;
-
-		// look for points inside the triangle in both directions
-		while ( p && p.z >= minZ && n && n.z <= maxZ ) {
+			var center = this.center;
 
-			if ( p !== ear.prev && p !== ear.next &&
-				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
-				area( p.prev, p, p.next ) >= 0 ) { return false; }
-			p = p.prevZ;
+			transformVertex( _vA$1.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
+			transformVertex( _vB$1.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
+			transformVertex( _vC$1.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
 
-			if ( n !== ear.prev && n !== ear.next &&
-				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) &&
-				area( n.prev, n, n.next ) >= 0 ) { return false; }
-			n = n.nextZ;
+			_uvA$1.set( 0, 0 );
+			_uvB$1.set( 1, 0 );
+			_uvC$1.set( 1, 1 );
 
-		}
+			// check first triangle
+			var intersect = raycaster.ray.intersectTriangle( _vA$1, _vB$1, _vC$1, false, _intersectPoint );
 
-		// look for remaining points in decreasing z-order
-		while ( p && p.z >= minZ ) {
+			if ( intersect === null ) {
 
-			if ( p !== ear.prev && p !== ear.next &&
-				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
-				area( p.prev, p, p.next ) >= 0 ) { return false; }
-			p = p.prevZ;
+				// check second triangle
+				transformVertex( _vB$1.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
+				_uvB$1.set( 0, 1 );
 
-		}
+				intersect = raycaster.ray.intersectTriangle( _vA$1, _vC$1, _vB$1, false, _intersectPoint );
+				if ( intersect === null ) {
 
-		// look for remaining points in increasing z-order
-		while ( n && n.z <= maxZ ) {
+					return;
 
-			if ( n !== ear.prev && n !== ear.next &&
-				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) &&
-				area( n.prev, n, n.next ) >= 0 ) { return false; }
-			n = n.nextZ;
+				}
 
-		}
+			}
 
-		return true;
+			var distance = raycaster.ray.origin.distanceTo( _intersectPoint );
 
-	}
+			if ( distance < raycaster.near || distance > raycaster.far ) { return; }
 
-	// go through all polygon nodes and cure small local self-intersections
-	function cureLocalIntersections( start, triangles, dim ) {
+			intersects.push( {
 
-		var p = start;
-		do {
+				distance: distance,
+				point: _intersectPoint.clone(),
+				uv: Triangle.getUV( _intersectPoint, _vA$1, _vB$1, _vC$1, _uvA$1, _uvB$1, _uvC$1, new Vector2() ),
+				face: null,
+				object: this
 
-			var a = p.prev,
-				b = p.next.next;
+			} );
 
-			if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) {
+		},
 
-				triangles.push( a.i / dim );
-				triangles.push( p.i / dim );
-				triangles.push( b.i / dim );
+		clone: function () {
 
-				// remove two nodes involved
-				removeNode( p );
-				removeNode( p.next );
+			return new this.constructor( this.material ).copy( this );
 
-				p = start = b;
+		},
 
-			}
+		copy: function ( source ) {
 
-			p = p.next;
+			Object3D.prototype.copy.call( this, source );
 
-		} while ( p !== start );
+			if ( source.center !== undefined ) { this.center.copy( source.center ); }
 
-		return filterPoints( p );
+			return this;
 
-	}
+		}
 
-	// try splitting polygon into two and triangulate them independently
-	function splitEarcut( start, triangles, dim, minX, minY, invSize ) {
 
-		// look for a valid diagonal that divides the polygon into two
-		var a = start;
-		do {
+	} );
 
-			var b = a.next.next;
-			while ( b !== a.prev ) {
+	function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
 
-				if ( a.i !== b.i && isValidDiagonal( a, b ) ) {
+		// compute position in camera space
+		_alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale );
 
-					// split the polygon in two by the diagonal
-					var c = splitPolygon( a, b );
+		// to check if rotation is not zero
+		if ( sin !== undefined ) {
 
-					// filter colinear points around the cuts
-					a = filterPoints( a, a.next );
-					c = filterPoints( c, c.next );
+			_rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y );
+			_rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y );
 
-					// run earcut on each half
-					earcutLinked( a, triangles, dim, minX, minY, invSize );
-					earcutLinked( c, triangles, dim, minX, minY, invSize );
-					return;
+		} else {
 
-				}
+			_rotatedPosition.copy( _alignedPosition );
 
-				b = b.next;
+		}
 
-			}
 
-			a = a.next;
+		vertexPosition.copy( mvPosition );
+		vertexPosition.x += _rotatedPosition.x;
+		vertexPosition.y += _rotatedPosition.y;
 
-		} while ( a !== start );
+		// transform to world space
+		vertexPosition.applyMatrix4( _viewWorldMatrix );
 
 	}
 
-	// link every hole into the outer loop, producing a single-ring polygon without holes
-	function eliminateHoles( data, holeIndices, outerNode, dim ) {
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		var queue = [],
-			i, len, start, end, list;
+	var _v1$4 = new Vector3();
+	var _v2$2 = new Vector3();
 
-		for ( i = 0, len = holeIndices.length; i < len; i ++ ) {
+	function LOD() {
 
-			start = holeIndices[ i ] * dim;
-			end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length;
-			list = linkedList( data, start, end, dim, false );
-			if ( list === list.next ) { list.steiner = true; }
-			queue.push( getLeftmost( list ) );
+		Object3D.call( this );
 
-		}
+		this._currentLevel = 0;
 
-		queue.sort( compareX );
+		this.type = 'LOD';
 
-		// process holes from left to right
-		for ( i = 0; i < queue.length; i ++ ) {
+		Object.defineProperties( this, {
+			levels: {
+				enumerable: true,
+				value: []
+			}
+		} );
 
-			eliminateHole( queue[ i ], outerNode );
-			outerNode = filterPoints( outerNode, outerNode.next );
+		this.autoUpdate = true;
 
-		}
+	}
 
-		return outerNode;
+	LOD.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-	}
+		constructor: LOD,
 
-	function compareX( a, b ) {
+		isLOD: true,
 
-		return a.x - b.x;
+		copy: function ( source ) {
 
-	}
+			Object3D.prototype.copy.call( this, source, false );
 
-	// find a bridge between vertices that connects hole with an outer ring and and link it
-	function eliminateHole( hole, outerNode ) {
+			var levels = source.levels;
 
-		outerNode = findHoleBridge( hole, outerNode );
-		if ( outerNode ) {
+			for ( var i = 0, l = levels.length; i < l; i ++ ) {
 
-			var b = splitPolygon( outerNode, hole );
+				var level = levels[ i ];
 
-			// filter collinear points around the cuts
-			filterPoints( outerNode, outerNode.next );
-			filterPoints( b, b.next );
+				this.addLevel( level.object.clone(), level.distance );
 
-		}
+			}
 
-	}
+			this.autoUpdate = source.autoUpdate;
 
-	// David Eberly's algorithm for finding a bridge between hole and outer polygon
-	function findHoleBridge( hole, outerNode ) {
+			return this;
 
-		var p = outerNode,
-			hx = hole.x,
-			hy = hole.y,
-			qx = - Infinity,
-			m;
+		},
 
-		// find a segment intersected by a ray from the hole's leftmost point to the left;
-		// segment's endpoint with lesser x will be potential connection point
-		do {
+		addLevel: function ( object, distance ) {
 
-			if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) {
+			if ( distance === undefined ) { distance = 0; }
 
-				var x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y );
-				if ( x <= hx && x > qx ) {
+			distance = Math.abs( distance );
 
-					qx = x;
-					if ( x === hx ) {
+			var levels = this.levels;
 
-						if ( hy === p.y ) { return p; }
-						if ( hy === p.next.y ) { return p.next; }
+			for ( var l = 0; l < levels.length; l ++ ) {
 
-					}
+				if ( distance < levels[ l ].distance ) {
 
-					m = p.x < p.next.x ? p : p.next;
+					break;
 
 				}
 
 			}
 
-			p = p.next;
+			levels.splice( l, 0, { distance: distance, object: object } );
 
-		} while ( p !== outerNode );
+			this.add( object );
 
-		if ( ! m ) { return null; }
+			return this;
 
-		if ( hx === qx ) { return m; } // hole touches outer segment; pick leftmost endpoint
+		},
 
-		// look for points inside the triangle of hole point, segment intersection and endpoint;
-		// if there are no points found, we have a valid connection;
-		// otherwise choose the point of the minimum angle with the ray as connection point
+		getCurrentLevel: function () {
 
-		var stop = m,
-			mx = m.x,
-			my = m.y,
-			tanMin = Infinity,
-			tan;
+			return this._currentLevel;
 
-		p = m;
+		},
 
-		do {
+		getObjectForDistance: function ( distance ) {
 
-			if ( hx >= p.x && p.x >= mx && hx !== p.x &&
-					pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) {
+			var levels = this.levels;
 
-				tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential
+			if ( levels.length > 0 ) {
 
-				if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) {
+				for ( var i = 1, l = levels.length; i < l; i ++ ) {
 
-					m = p;
-					tanMin = tan;
+					if ( distance < levels[ i ].distance ) {
+
+						break;
+
+					}
 
 				}
 
+				return levels[ i - 1 ].object;
+
 			}
 
-			p = p.next;
+			return null;
 
-		} while ( p !== stop );
+		},
 
-		return m;
+		raycast: function ( raycaster, intersects ) {
 
-	}
+			var levels = this.levels;
 
-	// whether sector in vertex m contains sector in vertex p in the same coordinates
-	function sectorContainsSector( m, p ) {
+			if ( levels.length > 0 ) {
 
-		return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0;
+				_v1$4.setFromMatrixPosition( this.matrixWorld );
 
-	}
+				var distance = raycaster.ray.origin.distanceTo( _v1$4 );
 
-	// interlink polygon nodes in z-order
-	function indexCurve( start, minX, minY, invSize ) {
+				this.getObjectForDistance( distance ).raycast( raycaster, intersects );
 
-		var p = start;
-		do {
+			}
 
-			if ( p.z === null ) { p.z = zOrder( p.x, p.y, minX, minY, invSize ); }
-			p.prevZ = p.prev;
-			p.nextZ = p.next;
-			p = p.next;
+		},
 
-		} while ( p !== start );
+		update: function ( camera ) {
 
-		p.prevZ.nextZ = null;
-		p.prevZ = null;
+			var levels = this.levels;
 
-		sortLinked( p );
+			if ( levels.length > 1 ) {
 
-	}
+				_v1$4.setFromMatrixPosition( camera.matrixWorld );
+				_v2$2.setFromMatrixPosition( this.matrixWorld );
 
-	// Simon Tatham's linked list merge sort algorithm
-	// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
-	function sortLinked( list ) {
+				var distance = _v1$4.distanceTo( _v2$2 ) / camera.zoom;
 
-		var i, p, q, e, tail, numMerges, pSize, qSize,
-			inSize = 1;
+				levels[ 0 ].object.visible = true;
 
-		do {
+				for ( var i = 1, l = levels.length; i < l; i ++ ) {
 
-			p = list;
-			list = null;
-			tail = null;
-			numMerges = 0;
+					if ( distance >= levels[ i ].distance ) {
 
-			while ( p ) {
+						levels[ i - 1 ].object.visible = false;
+						levels[ i ].object.visible = true;
 
-				numMerges ++;
-				q = p;
-				pSize = 0;
-				for ( i = 0; i < inSize; i ++ ) {
+					} else {
 
-					pSize ++;
-					q = q.nextZ;
-					if ( ! q ) { break; }
+						break;
 
-				}
+					}
 
-				qSize = inSize;
+				}
 
-				while ( pSize > 0 || ( qSize > 0 && q ) ) {
+				this._currentLevel = i - 1;
 
-					if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) {
+				for ( ; i < l; i ++ ) {
 
-						e = p;
-						p = p.nextZ;
-						pSize --;
+					levels[ i ].object.visible = false;
 
-					} else {
+				}
 
-						e = q;
-						q = q.nextZ;
-						qSize --;
+			}
 
-					}
+		},
 
-					if ( tail ) { tail.nextZ = e; }
-					else { list = e; }
+		toJSON: function ( meta ) {
 
-					e.prevZ = tail;
-					tail = e;
+			var data = Object3D.prototype.toJSON.call( this, meta );
 
-				}
+			if ( this.autoUpdate === false ) { data.object.autoUpdate = false; }
 
-				p = q;
+			data.object.levels = [];
 
-			}
+			var levels = this.levels;
 
-			tail.nextZ = null;
-			inSize *= 2;
+			for ( var i = 0, l = levels.length; i < l; i ++ ) {
 
-		} while ( numMerges > 1 );
+				var level = levels[ i ];
 
-		return list;
+				data.object.levels.push( {
+					object: level.object.uuid,
+					distance: level.distance
+				} );
 
-	}
+			}
 
-	// z-order of a point given coords and inverse of the longer side of data bbox
-	function zOrder( x, y, minX, minY, invSize ) {
+			return data;
 
-		// coords are transformed into non-negative 15-bit integer range
-		x = 32767 * ( x - minX ) * invSize;
-		y = 32767 * ( y - minY ) * invSize;
+		}
 
-		x = ( x | ( x << 8 ) ) & 0x00FF00FF;
-		x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
-		x = ( x | ( x << 2 ) ) & 0x33333333;
-		x = ( x | ( x << 1 ) ) & 0x55555555;
+	} );
 
-		y = ( y | ( y << 8 ) ) & 0x00FF00FF;
-		y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
-		y = ( y | ( y << 2 ) ) & 0x33333333;
-		y = ( y | ( y << 1 ) ) & 0x55555555;
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author ikerr / http://verold.com
+	 */
 
-		return x | ( y << 1 );
+	function SkinnedMesh( geometry, material ) {
 
-	}
+		if ( geometry && geometry.isGeometry ) {
 
-	// find the leftmost node of a polygon ring
-	function getLeftmost( start ) {
+			console.error( 'THREE.SkinnedMesh no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
 
-		var p = start,
-			leftmost = start;
-		do {
+		}
 
-			if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) { leftmost = p; }
-			p = p.next;
+		Mesh.call( this, geometry, material );
 
-		} while ( p !== start );
+		this.type = 'SkinnedMesh';
 
-		return leftmost;
+		this.bindMode = 'attached';
+		this.bindMatrix = new Matrix4();
+		this.bindMatrixInverse = new Matrix4();
 
 	}
 
-	// check if a point lies within a convex triangle
-	function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) {
+	SkinnedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {
 
-		return ( cx - px ) * ( ay - py ) - ( ax - px ) * ( cy - py ) >= 0 &&
-				( ax - px ) * ( by - py ) - ( bx - px ) * ( ay - py ) >= 0 &&
-				( bx - px ) * ( cy - py ) - ( cx - px ) * ( by - py ) >= 0;
+		constructor: SkinnedMesh,
 
-	}
+		isSkinnedMesh: true,
 
-	// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
-	function isValidDiagonal( a, b ) {
+		bind: function ( skeleton, bindMatrix ) {
 
-		return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // dones't intersect other edges
-			( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible
-			( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors
-			equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case
+			this.skeleton = skeleton;
 
-	}
+			if ( bindMatrix === undefined ) {
 
-	// signed area of a triangle
-	function area( p, q, r ) {
+				this.updateMatrixWorld( true );
 
-		return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y );
+				this.skeleton.calculateInverses();
 
-	}
+				bindMatrix = this.matrixWorld;
 
-	// check if two points are equal
-	function equals( p1, p2 ) {
+			}
 
-		return p1.x === p2.x && p1.y === p2.y;
+			this.bindMatrix.copy( bindMatrix );
+			this.bindMatrixInverse.getInverse( bindMatrix );
 
-	}
+		},
 
-	// check if two segments intersect
-	function intersects( p1, q1, p2, q2 ) {
+		pose: function () {
 
-		var o1 = sign( area( p1, q1, p2 ) );
-		var o2 = sign( area( p1, q1, q2 ) );
-		var o3 = sign( area( p2, q2, p1 ) );
-		var o4 = sign( area( p2, q2, q1 ) );
+			this.skeleton.pose();
 
-		if ( o1 !== o2 && o3 !== o4 ) { return true; } // general case
+		},
 
-		if ( o1 === 0 && onSegment( p1, p2, q1 ) ) { return true; } // p1, q1 and p2 are collinear and p2 lies on p1q1
-		if ( o2 === 0 && onSegment( p1, q2, q1 ) ) { return true; } // p1, q1 and q2 are collinear and q2 lies on p1q1
-		if ( o3 === 0 && onSegment( p2, p1, q2 ) ) { return true; } // p2, q2 and p1 are collinear and p1 lies on p2q2
-		if ( o4 === 0 && onSegment( p2, q1, q2 ) ) { return true; } // p2, q2 and q1 are collinear and q1 lies on p2q2
+		normalizeSkinWeights: function () {
 
-		return false;
+			var vector = new Vector4();
 
-	}
+			var skinWeight = this.geometry.attributes.skinWeight;
 
-	// for collinear points p, q, r, check if point q lies on segment pr
-	function onSegment( p, q, r ) {
+			for ( var i = 0, l = skinWeight.count; i < l; i ++ ) {
 
-		return q.x <= Math.max( p.x, r.x ) && q.x >= Math.min( p.x, r.x ) && q.y <= Math.max( p.y, r.y ) && q.y >= Math.min( p.y, r.y );
+				vector.x = skinWeight.getX( i );
+				vector.y = skinWeight.getY( i );
+				vector.z = skinWeight.getZ( i );
+				vector.w = skinWeight.getW( i );
 
-	}
+				var scale = 1.0 / vector.manhattanLength();
 
-	function sign( num ) {
+				if ( scale !== Infinity ) {
 
-		return num > 0 ? 1 : num < 0 ? - 1 : 0;
+					vector.multiplyScalar( scale );
 
-	}
+				} else {
 
-	// check if a polygon diagonal intersects any polygon segments
-	function intersectsPolygon( a, b ) {
+					vector.set( 1, 0, 0, 0 ); // do something reasonable
 
-		var p = a;
-		do {
+				}
 
-			if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
-					intersects( p, p.next, a, b ) ) { return true; }
-			p = p.next;
+				skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
 
-		} while ( p !== a );
+			}
 
-		return false;
+		},
 
-	}
+		updateMatrixWorld: function ( force ) {
 
-	// check if a polygon diagonal is locally inside the polygon
-	function locallyInside( a, b ) {
+			Mesh.prototype.updateMatrixWorld.call( this, force );
 
-		return area( a.prev, a, a.next ) < 0 ?
-			area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 :
-			area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0;
+			if ( this.bindMode === 'attached' ) {
 
-	}
+				this.bindMatrixInverse.getInverse( this.matrixWorld );
 
-	// check if the middle point of a polygon diagonal is inside the polygon
-	function middleInside( a, b ) {
+			} else if ( this.bindMode === 'detached' ) {
 
-		var p = a,
-			inside = false,
-			px = ( a.x + b.x ) / 2,
-			py = ( a.y + b.y ) / 2;
-		do {
+				this.bindMatrixInverse.getInverse( this.bindMatrix );
 
-			if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y &&
-					( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) )
-				{ inside = ! inside; }
-			p = p.next;
+			} else {
 
-		} while ( p !== a );
+				console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
 
-		return inside;
+			}
 
-	}
+		},
 
-	// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
-	// if one belongs to the outer ring and another to a hole, it merges it into a single ring
-	function splitPolygon( a, b ) {
+		clone: function () {
 
-		var a2 = new Node( a.i, a.x, a.y ),
-			b2 = new Node( b.i, b.x, b.y ),
-			an = a.next,
-			bp = b.prev;
+			return new this.constructor( this.geometry, this.material ).copy( this );
 
-		a.next = b;
-		b.prev = a;
+		},
 
-		a2.next = an;
-		an.prev = a2;
+		boneTransform: ( function () {
 
-		b2.next = a2;
-		a2.prev = b2;
+			var basePosition = new Vector3();
 
-		bp.next = b2;
-		b2.prev = bp;
+			var skinIndex = new Vector4();
+			var skinWeight = new Vector4();
 
-		return b2;
+			var vector = new Vector3();
+			var matrix = new Matrix4();
 
-	}
+			return function ( index, target ) {
 
-	// create a node and optionally link it with previous one (in a circular doubly linked list)
-	function insertNode( i, x, y, last ) {
+				var skeleton = this.skeleton;
+				var geometry = this.geometry;
 
-		var p = new Node( i, x, y );
+				skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
+				skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
 
-		if ( ! last ) {
+				basePosition.fromBufferAttribute( geometry.attributes.position, index ).applyMatrix4( this.bindMatrix );
 
-			p.prev = p;
-			p.next = p;
+				target.set( 0, 0, 0 );
 
-		} else {
+				for ( var i = 0; i < 4; i ++ ) {
 
-			p.next = last.next;
-			p.prev = last;
-			last.next.prev = p;
-			last.next = p;
+					var weight = skinWeight.getComponent( i );
 
-		}
+					if ( weight !== 0 ) {
 
-		return p;
+						var boneIndex = skinIndex.getComponent( i );
 
-	}
+						matrix.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
 
-	function removeNode( p ) {
+						target.addScaledVector( vector.copy( basePosition ).applyMatrix4( matrix ), weight );
 
-		p.next.prev = p.prev;
-		p.prev.next = p.next;
+					}
 
-		if ( p.prevZ ) { p.prevZ.nextZ = p.nextZ; }
-		if ( p.nextZ ) { p.nextZ.prevZ = p.prevZ; }
+				}
 
-	}
+				return target.applyMatrix4( this.bindMatrixInverse );
 
-	function Node( i, x, y ) {
+			};
 
-		// vertex index in coordinates array
-		this.i = i;
+		}() )
 
-		// vertex coordinates
-		this.x = x;
-		this.y = y;
+	} );
 
-		// previous and next vertex nodes in a polygon ring
-		this.prev = null;
-		this.next = null;
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author michael guerrero / http://realitymeltdown.com
+	 * @author ikerr / http://verold.com
+	 */
 
-		// z-order curve value
-		this.z = null;
+	var _offsetMatrix = new Matrix4();
+	var _identityMatrix = new Matrix4();
 
-		// previous and next nodes in z-order
-		this.prevZ = null;
-		this.nextZ = null;
+	function Skeleton( bones, boneInverses ) {
 
-		// indicates whether this is a steiner point
-		this.steiner = false;
+		// copy the bone array
 
-	}
+		bones = bones || [];
 
-	function signedArea( data, start, end, dim ) {
+		this.bones = bones.slice( 0 );
+		this.boneMatrices = new Float32Array( this.bones.length * 16 );
 
-		var sum = 0;
-		for ( var i = start, j = end - dim; i < end; i += dim ) {
+		this.frame = - 1;
 
-			sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] );
-			j = i;
+		// use the supplied bone inverses or calculate the inverses
 
-		}
+		if ( boneInverses === undefined ) {
 
-		return sum;
+			this.calculateInverses();
 
-	}
+		} else {
 
-	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 */
+			if ( this.bones.length === boneInverses.length ) {
 
-	var ShapeUtils = {
+				this.boneInverses = boneInverses.slice( 0 );
 
-		// calculate area of the contour polygon
+			} else {
 
-		area: function ( contour ) {
+				console.warn( 'THREE.Skeleton boneInverses is the wrong length.' );
 
-			var n = contour.length;
-			var a = 0.0;
+				this.boneInverses = [];
 
-			for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
+				for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
 
-				a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
+					this.boneInverses.push( new Matrix4() );
+
+				}
 
 			}
 
-			return a * 0.5;
+		}
 
-		},
+	}
 
-		isClockWise: function ( pts ) {
+	Object.assign( Skeleton.prototype, {
 
-			return ShapeUtils.area( pts ) < 0;
+		calculateInverses: function () {
 
-		},
+			this.boneInverses = [];
 
-		triangulateShape: function ( contour, holes ) {
+			for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
 
-			var vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
-			var holeIndices = []; // array of hole indices
-			var faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
+				var inverse = new Matrix4();
 
-			removeDupEndPts( contour );
-			addContour( vertices, contour );
+				if ( this.bones[ i ] ) {
 
-			//
+					inverse.getInverse( this.bones[ i ].matrixWorld );
 
-			var holeIndex = contour.length;
+				}
 
-			holes.forEach( removeDupEndPts );
+				this.boneInverses.push( inverse );
 
-			for ( var i = 0; i < holes.length; i ++ ) {
+			}
 
-				holeIndices.push( holeIndex );
-				holeIndex += holes[ i ].length;
-				addContour( vertices, holes[ i ] );
+		},
 
-			}
+		pose: function () {
 
-			//
+			var bone, i, il;
 
-			var triangles = Earcut.triangulate( vertices, holeIndices );
+			// recover the bind-time world matrices
 
-			//
+			for ( i = 0, il = this.bones.length; i < il; i ++ ) {
 
-			for ( var i = 0; i < triangles.length; i += 3 ) {
+				bone = this.bones[ i ];
 
-				faces.push( triangles.slice( i, i + 3 ) );
+				if ( bone ) {
+
+					bone.matrixWorld.getInverse( this.boneInverses[ i ] );
+
+				}
 
 			}
 
-			return faces;
+			// compute the local matrices, positions, rotations and scales
 
-		}
+			for ( i = 0, il = this.bones.length; i < il; i ++ ) {
 
-	};
+				bone = this.bones[ i ];
 
-	function removeDupEndPts( points ) {
+				if ( bone ) {
 
-		var l = points.length;
+					if ( bone.parent && bone.parent.isBone ) {
 
-		if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
+						bone.matrix.getInverse( bone.parent.matrixWorld );
+						bone.matrix.multiply( bone.matrixWorld );
 
-			points.pop();
+					} else {
 
-		}
+						bone.matrix.copy( bone.matrixWorld );
 
-	}
+					}
 
-	function addContour( vertices, contour ) {
+					bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
 
-		for ( var i = 0; i < contour.length; i ++ ) {
+				}
 
-			vertices.push( contour[ i ].x );
-			vertices.push( contour[ i ].y );
+			}
 
-		}
+		},
 
-	}
+		update: function () {
 
-	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 *
-	 * Creates extruded geometry from a path shape.
-	 *
-	 * parameters = {
-	 *
-	 *  curveSegments: <int>, // number of points on the curves
-	 *  steps: <int>, // number of points for z-side extrusions / used for subdividing segments of extrude spline too
-	 *  depth: <float>, // Depth to extrude the shape
-	 *
-	 *  bevelEnabled: <bool>, // turn on bevel
-	 *  bevelThickness: <float>, // how deep into the original shape bevel goes
-	 *  bevelSize: <float>, // how far from shape outline (including bevelOffset) is bevel
-	 *  bevelOffset: <float>, // how far from shape outline does bevel start
-	 *  bevelSegments: <int>, // number of bevel layers
-	 *
-	 *  extrudePath: <THREE.Curve> // curve to extrude shape along
-	 *
-	 *  UVGenerator: <Object> // object that provides UV generator functions
-	 *
-	 * }
-	 */
+			var bones = this.bones;
+			var boneInverses = this.boneInverses;
+			var boneMatrices = this.boneMatrices;
+			var boneTexture = this.boneTexture;
 
-	// ExtrudeGeometry
+			// flatten bone matrices to array
 
-	function ExtrudeGeometry( shapes, options ) {
+			for ( var i = 0, il = bones.length; i < il; i ++ ) {
 
-		Geometry.call( this );
+				// compute the offset between the current and the original transform
 
-		this.type = 'ExtrudeGeometry';
+				var matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
 
-		this.parameters = {
-			shapes: shapes,
-			options: options
-		};
+				_offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
+				_offsetMatrix.toArray( boneMatrices, i * 16 );
 
-		this.fromBufferGeometry( new ExtrudeBufferGeometry( shapes, options ) );
-		this.mergeVertices();
+			}
 
-	}
+			if ( boneTexture !== undefined ) {
 
-	ExtrudeGeometry.prototype = Object.create( Geometry.prototype );
-	ExtrudeGeometry.prototype.constructor = ExtrudeGeometry;
+				boneTexture.needsUpdate = true;
 
-	ExtrudeGeometry.prototype.toJSON = function () {
+			}
 
-		var data = Geometry.prototype.toJSON.call( this );
+		},
 
-		var shapes = this.parameters.shapes;
-		var options = this.parameters.options;
+		clone: function () {
 
-		return toJSON( shapes, options, data );
+			return new Skeleton( this.bones, this.boneInverses );
 
-	};
+		},
 
-	// ExtrudeBufferGeometry
+		getBoneByName: function ( name ) {
 
-	function ExtrudeBufferGeometry( shapes, options ) {
+			for ( var i = 0, il = this.bones.length; i < il; i ++ ) {
 
-		BufferGeometry.call( this );
+				var bone = this.bones[ i ];
 
-		this.type = 'ExtrudeBufferGeometry';
+				if ( bone.name === name ) {
 
-		this.parameters = {
-			shapes: shapes,
-			options: options
-		};
+					return bone;
 
-		shapes = Array.isArray( shapes ) ? shapes : [ shapes ];
+				}
 
-		var scope = this;
+			}
 
-		var verticesArray = [];
-		var uvArray = [];
+			return undefined;
 
-		for ( var i = 0, l = shapes.length; i < l; i ++ ) {
+		},
 
-			var shape = shapes[ i ];
-			addShape( shape );
+		dispose: function ( ) {
 
-		}
+			if ( this.boneTexture ) {
 
-		// build geometry
+				this.boneTexture.dispose();
 
-		this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) );
+				this.boneTexture = undefined;
 
-		this.computeVertexNormals();
+			}
 
-		// functions
+		}
 
-		function addShape( shape ) {
+	} );
 
-			var placeholder = [];
+	/**
+	 * @author mikael emtinger / http://gomo.se/
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author ikerr / http://verold.com
+	 */
 
-			// options
+	function Bone() {
 
-			var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
-			var steps = options.steps !== undefined ? options.steps : 1;
-			var depth = options.depth !== undefined ? options.depth : 100;
+		Object3D.call( this );
 
-			var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true;
-			var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6;
-			var bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2;
-			var bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0;
-			var bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
+		this.type = 'Bone';
 
-			var extrudePath = options.extrudePath;
+	}
 
-			var uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator;
+	Bone.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-			// deprecated options
+		constructor: Bone,
 
-			if ( options.amount !== undefined ) {
+		isBone: true
 
-				console.warn( 'THREE.ExtrudeBufferGeometry: amount has been renamed to depth.' );
-				depth = options.amount;
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			//
+	var _instanceLocalMatrix = new Matrix4();
+	var _instanceWorldMatrix = new Matrix4();
 
-			var extrudePts, extrudeByPath = false;
-			var splineTube, binormal, normal, position2;
+	var _instanceIntersects = [];
 
-			if ( extrudePath ) {
+	var _mesh = new Mesh();
 
-				extrudePts = extrudePath.getSpacedPoints( steps );
+	function InstancedMesh( geometry, material, count ) {
 
-				extrudeByPath = true;
-				bevelEnabled = false; // bevels not supported for path extrusion
+		Mesh.call( this, geometry, material );
 
-				// SETUP TNB variables
+		this.instanceMatrix = new BufferAttribute( new Float32Array( count * 16 ), 16 );
 
-				// TODO1 - have a .isClosed in spline?
+		this.count = count;
 
-				splineTube = extrudePath.computeFrenetFrames( steps, false );
+		this.frustumCulled = false;
 
-				// console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
+	}
 
-				binormal = new Vector3();
-				normal = new Vector3();
-				position2 = new Vector3();
+	InstancedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {
 
-			}
+		constructor: InstancedMesh,
 
-			// Safeguards if bevels are not enabled
+		isInstancedMesh: true,
 
-			if ( ! bevelEnabled ) {
+		getMatrixAt: function ( index, matrix ) {
 
-				bevelSegments = 0;
-				bevelThickness = 0;
-				bevelSize = 0;
-				bevelOffset = 0;
+			matrix.fromArray( this.instanceMatrix.array, index * 16 );
 
-			}
+		},
 
-			// Variables initialization
+		raycast: function ( raycaster, intersects ) {
 
-			var ahole, h, hl; // looping of holes
+			var matrixWorld = this.matrixWorld;
+			var raycastTimes = this.count;
 
-			var shapePoints = shape.extractPoints( curveSegments );
+			_mesh.geometry = this.geometry;
+			_mesh.material = this.material;
 
-			var vertices = shapePoints.shape;
-			var holes = shapePoints.holes;
+			if ( _mesh.material === undefined ) { return; }
 
-			var reverse = ! ShapeUtils.isClockWise( vertices );
+			for ( var instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
 
-			if ( reverse ) {
+				// calculate the world matrix for each instance
 
-				vertices = vertices.reverse();
+				this.getMatrixAt( instanceId, _instanceLocalMatrix );
 
-				// Maybe we should also check if holes are in the opposite direction, just to be safe ...
+				_instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
 
-				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+				// the mesh represents this single instance
 
-					ahole = holes[ h ];
+				_mesh.matrixWorld = _instanceWorldMatrix;
 
-					if ( ShapeUtils.isClockWise( ahole ) ) {
+				_mesh.raycast( raycaster, _instanceIntersects );
 
-						holes[ h ] = ahole.reverse();
+				// process the result of raycast
 
-					}
+				for ( var i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
+
+					var intersect = _instanceIntersects[ i ];
+					intersect.instanceId = instanceId;
+					intersect.object = this;
+					intersects.push( intersect );
 
 				}
 
+				_instanceIntersects.length = 0;
+
 			}
 
+		},
 
-			var faces = ShapeUtils.triangulateShape( vertices, holes );
+		setMatrixAt: function ( index, matrix ) {
 
-			/* Vertices */
+			matrix.toArray( this.instanceMatrix.array, index * 16 );
 
-			var contour = vertices; // vertices has all points but contour has only points of circumference
+		},
 
-			for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+		updateMorphTargets: function () {
 
-				ahole = holes[ h ];
+		}
 
-				vertices = vertices.concat( ahole );
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *
+	 *  linewidth: <float>,
+	 *  linecap: "round",
+	 *  linejoin: "round"
+	 * }
+	 */
 
+	function LineBasicMaterial( parameters ) {
 
-			function scalePt2( pt, vec, size ) {
+		Material.call( this );
 
-				if ( ! vec ) { console.error( "THREE.ExtrudeGeometry: vec does not exist" ); }
+		this.type = 'LineBasicMaterial';
 
-				return vec.clone().multiplyScalar( size ).add( pt );
+		this.color = new Color( 0xffffff );
 
-			}
+		this.linewidth = 1;
+		this.linecap = 'round';
+		this.linejoin = 'round';
 
-			var b, bs, t, z,
-				vert, vlen = vertices.length,
-				face, flen = faces.length;
+		this.setValues( parameters );
 
+	}
 
-			// Find directions for point movement
+	LineBasicMaterial.prototype = Object.create( Material.prototype );
+	LineBasicMaterial.prototype.constructor = LineBasicMaterial;
 
+	LineBasicMaterial.prototype.isLineBasicMaterial = true;
 
-			function getBevelVec( inPt, inPrev, inNext ) {
+	LineBasicMaterial.prototype.copy = function ( source ) {
 
-				// computes for inPt the corresponding point inPt' on a new contour
-				//   shifted by 1 unit (length of normalized vector) to the left
-				// if we walk along contour clockwise, this new contour is outside the old one
-				//
-				// inPt' is the intersection of the two lines parallel to the two
-				//  adjacent edges of inPt at a distance of 1 unit on the left side.
+		Material.prototype.copy.call( this, source );
 
-				var v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt
+		this.color.copy( source.color );
 
-				// good reading for geometry algorithms (here: line-line intersection)
-				// http://geomalgorithms.com/a05-_intersect-1.html
+		this.linewidth = source.linewidth;
+		this.linecap = source.linecap;
+		this.linejoin = source.linejoin;
 
-				var v_prev_x = inPt.x - inPrev.x,
-					v_prev_y = inPt.y - inPrev.y;
-				var v_next_x = inNext.x - inPt.x,
-					v_next_y = inNext.y - inPt.y;
+		return this;
 
-				var v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
+	};
 
-				// check for collinear edges
-				var collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				if ( Math.abs( collinear0 ) > Number.EPSILON ) {
+	var _start = new Vector3();
+	var _end = new Vector3();
+	var _inverseMatrix$1 = new Matrix4();
+	var _ray$1 = new Ray();
+	var _sphere$2 = new Sphere();
 
-					// not collinear
+	function Line( geometry, material, mode ) {
 
-					// length of vectors for normalizing
+		if ( mode === 1 ) {
 
-					var v_prev_len = Math.sqrt( v_prev_lensq );
-					var v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
+			console.error( 'THREE.Line: parameter THREE.LinePieces no longer supported. Use THREE.LineSegments instead.' );
 
-					// shift adjacent points by unit vectors to the left
+		}
 
-					var ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
-					var ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
+		Object3D.call( this );
 
-					var ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
-					var ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
+		this.type = 'Line';
 
-					// scaling factor for v_prev to intersection point
+		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
+		this.material = material !== undefined ? material : new LineBasicMaterial();
 
-					var sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
-							( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
-						( v_prev_x * v_next_y - v_prev_y * v_next_x );
-
-					// vector from inPt to intersection point
-
-					v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
-					v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
+	}
 
-					// Don't normalize!, otherwise sharp corners become ugly
-					//  but prevent crazy spikes
-					var v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y );
-					if ( v_trans_lensq <= 2 ) {
+	Line.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-						return new Vector2( v_trans_x, v_trans_y );
+		constructor: Line,
 
-					} else {
+		isLine: true,
 
-						shrink_by = Math.sqrt( v_trans_lensq / 2 );
+		computeLineDistances: function () {
 
-					}
+			var geometry = this.geometry;
 
-				} else {
+			if ( geometry.isBufferGeometry ) {
 
-					// handle special case of collinear edges
+				// we assume non-indexed geometry
 
-					var direction_eq = false; // assumes: opposite
-					if ( v_prev_x > Number.EPSILON ) {
+				if ( geometry.index === null ) {
 
-						if ( v_next_x > Number.EPSILON ) {
+					var positionAttribute = geometry.attributes.position;
+					var lineDistances = [ 0 ];
 
-							direction_eq = true;
+					for ( var i = 1, l = positionAttribute.count; i < l; i ++ ) {
 
-						}
+						_start.fromBufferAttribute( positionAttribute, i - 1 );
+						_end.fromBufferAttribute( positionAttribute, i );
 
-					} else {
+						lineDistances[ i ] = lineDistances[ i - 1 ];
+						lineDistances[ i ] += _start.distanceTo( _end );
 
-						if ( v_prev_x < - Number.EPSILON ) {
+					}
 
-							if ( v_next_x < - Number.EPSILON ) {
+					geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
 
-								direction_eq = true;
+				} else {
 
-							}
+					console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
 
-						} else {
+				}
 
-							if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) {
+			} else if ( geometry.isGeometry ) {
 
-								direction_eq = true;
+				var vertices = geometry.vertices;
+				var lineDistances = geometry.lineDistances;
 
-							}
+				lineDistances[ 0 ] = 0;
 
-						}
+				for ( var i = 1, l = vertices.length; i < l; i ++ ) {
 
-					}
+					lineDistances[ i ] = lineDistances[ i - 1 ];
+					lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
 
-					if ( direction_eq ) {
+				}
 
-						// console.log("Warning: lines are a straight sequence");
-						v_trans_x = - v_prev_y;
-						v_trans_y = v_prev_x;
-						shrink_by = Math.sqrt( v_prev_lensq );
+			}
 
-					} else {
+			return this;
 
-						// console.log("Warning: lines are a straight spike");
-						v_trans_x = v_prev_x;
-						v_trans_y = v_prev_y;
-						shrink_by = Math.sqrt( v_prev_lensq / 2 );
+		},
 
-					}
+		raycast: function ( raycaster, intersects ) {
 
-				}
+			var geometry = this.geometry;
+			var matrixWorld = this.matrixWorld;
+			var threshold = raycaster.params.Line.threshold;
 
-				return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
+			// Checking boundingSphere distance to ray
 
-			}
+			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
 
+			_sphere$2.copy( geometry.boundingSphere );
+			_sphere$2.applyMatrix4( matrixWorld );
+			_sphere$2.radius += threshold;
 
-			var contourMovements = [];
+			if ( raycaster.ray.intersectsSphere( _sphere$2 ) === false ) { return; }
 
-			for ( var i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
+			//
 
-				if ( j === il ) { j = 0; }
-				if ( k === il ) { k = 0; }
+			_inverseMatrix$1.getInverse( matrixWorld );
+			_ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 );
 
-				//  (j)---(i)---(k)
-				// console.log('i,j,k', i, j , k)
+			var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
+			var localThresholdSq = localThreshold * localThreshold;
 
-				contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
+			var vStart = new Vector3();
+			var vEnd = new Vector3();
+			var interSegment = new Vector3();
+			var interRay = new Vector3();
+			var step = ( this && this.isLineSegments ) ? 2 : 1;
 
-			}
+			if ( geometry.isBufferGeometry ) {
 
-			var holesMovements = [],
-				oneHoleMovements, verticesMovements = contourMovements.concat();
+				var index = geometry.index;
+				var attributes = geometry.attributes;
+				var positions = attributes.position.array;
 
-			for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+				if ( index !== null ) {
 
-				ahole = holes[ h ];
+					var indices = index.array;
 
-				oneHoleMovements = [];
+					for ( var i = 0, l = indices.length - 1; i < l; i += step ) {
 
-				for ( i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
+						var a = indices[ i ];
+						var b = indices[ i + 1 ];
 
-					if ( j === il ) { j = 0; }
-					if ( k === il ) { k = 0; }
+						vStart.fromArray( positions, a * 3 );
+						vEnd.fromArray( positions, b * 3 );
 
-					//  (j)---(i)---(k)
-					oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
+						var distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
 
-				}
+						if ( distSq > localThresholdSq ) { continue; }
 
-				holesMovements.push( oneHoleMovements );
-				verticesMovements = verticesMovements.concat( oneHoleMovements );
+						interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
 
-			}
+						var distance = raycaster.ray.origin.distanceTo( interRay );
 
+						if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
 
-			// Loop bevelSegments, 1 for the front, 1 for the back
+						intersects.push( {
 
-			for ( b = 0; b < bevelSegments; b ++ ) {
+							distance: distance,
+							// What do we want? intersection point on the ray or on the segment??
+							// point: raycaster.ray.at( distance ),
+							point: interSegment.clone().applyMatrix4( this.matrixWorld ),
+							index: i,
+							face: null,
+							faceIndex: null,
+							object: this
 
-				//for ( b = bevelSegments; b > 0; b -- ) {
+						} );
 
-				t = b / bevelSegments;
-				z = bevelThickness * Math.cos( t * Math.PI / 2 );
-				bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
+					}
 
-				// contract shape
+				} else {
 
-				for ( i = 0, il = contour.length; i < il; i ++ ) {
+					for ( var i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
 
-					vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
+						vStart.fromArray( positions, 3 * i );
+						vEnd.fromArray( positions, 3 * i + 3 );
 
-					v( vert.x, vert.y, - z );
+						var distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment );
 
-				}
+						if ( distSq > localThresholdSq ) { continue; }
 
-				// expand holes
+						interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
 
-				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+						var distance = raycaster.ray.origin.distanceTo( interRay );
 
-					ahole = holes[ h ];
-					oneHoleMovements = holesMovements[ h ];
+						if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
 
-					for ( i = 0, il = ahole.length; i < il; i ++ ) {
+						intersects.push( {
 
-						vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
+							distance: distance,
+							// What do we want? intersection point on the ray or on the segment??
+							// point: raycaster.ray.at( distance ),
+							point: interSegment.clone().applyMatrix4( this.matrixWorld ),
+							index: i,
+							face: null,
+							faceIndex: null,
+							object: this
 
-						v( vert.x, vert.y, - z );
+						} );
 
 					}
 
 				}
 
-			}
-
-			bs = bevelSize + bevelOffset;
+			} else if ( geometry.isGeometry ) {
 
-			// Back facing vertices
+				var vertices = geometry.vertices;
+				var nbVertices = vertices.length;
 
-			for ( i = 0; i < vlen; i ++ ) {
+				for ( var i = 0; i < nbVertices - 1; i += step ) {
 
-				vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
+					var distSq = _ray$1.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
 
-				if ( ! extrudeByPath ) {
+					if ( distSq > localThresholdSq ) { continue; }
 
-					v( vert.x, vert.y, 0 );
+					interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation
 
-				} else {
+					var distance = raycaster.ray.origin.distanceTo( interRay );
 
-					// v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
+					if ( distance < raycaster.near || distance > raycaster.far ) { continue; }
 
-					normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x );
-					binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y );
+					intersects.push( {
 
-					position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal );
+						distance: distance,
+						// What do we want? intersection point on the ray or on the segment??
+						// point: raycaster.ray.at( distance ),
+						point: interSegment.clone().applyMatrix4( this.matrixWorld ),
+						index: i,
+						face: null,
+						faceIndex: null,
+						object: this
 
-					v( position2.x, position2.y, position2.z );
+					} );
 
 				}
 
 			}
 
-			// Add stepped vertices...
-			// Including front facing vertices
-
-			var s;
+		},
 
-			for ( s = 1; s <= steps; s ++ ) {
+		clone: function () {
 
-				for ( i = 0; i < vlen; i ++ ) {
+			return new this.constructor( this.geometry, this.material ).copy( this );
 
-					vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
+		}
 
-					if ( ! extrudeByPath ) {
+	} );
 
-						v( vert.x, vert.y, depth / steps * s );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-					} else {
+	var _start$1 = new Vector3();
+	var _end$1 = new Vector3();
 
-						// v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
+	function LineSegments( geometry, material ) {
 
-						normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x );
-						binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y );
+		Line.call( this, geometry, material );
 
-						position2.copy( extrudePts[ s ] ).add( normal ).add( binormal );
+		this.type = 'LineSegments';
 
-						v( position2.x, position2.y, position2.z );
+	}
 
-					}
+	LineSegments.prototype = Object.assign( Object.create( Line.prototype ), {
 
-				}
+		constructor: LineSegments,
 
-			}
+		isLineSegments: true,
 
+		computeLineDistances: function () {
 
-			// Add bevel segments planes
+			var geometry = this.geometry;
 
-			//for ( b = 1; b <= bevelSegments; b ++ ) {
-			for ( b = bevelSegments - 1; b >= 0; b -- ) {
+			if ( geometry.isBufferGeometry ) {
 
-				t = b / bevelSegments;
-				z = bevelThickness * Math.cos( t * Math.PI / 2 );
-				bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
+				// we assume non-indexed geometry
 
-				// contract shape
+				if ( geometry.index === null ) {
 
-				for ( i = 0, il = contour.length; i < il; i ++ ) {
+					var positionAttribute = geometry.attributes.position;
+					var lineDistances = [];
 
-					vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
-					v( vert.x, vert.y, depth + z );
+					for ( var i = 0, l = positionAttribute.count; i < l; i += 2 ) {
 
-				}
+						_start$1.fromBufferAttribute( positionAttribute, i );
+						_end$1.fromBufferAttribute( positionAttribute, i + 1 );
 
-				// expand holes
+						lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
+						lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 );
 
-				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+					}
 
-					ahole = holes[ h ];
-					oneHoleMovements = holesMovements[ h ];
+					geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
 
-					for ( i = 0, il = ahole.length; i < il; i ++ ) {
+				} else {
 
-						vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
+					console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
 
-						if ( ! extrudeByPath ) {
+				}
 
-							v( vert.x, vert.y, depth + z );
+			} else if ( geometry.isGeometry ) {
 
-						} else {
+				var vertices = geometry.vertices;
+				var lineDistances = geometry.lineDistances;
 
-							v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
+				for ( var i = 0, l = vertices.length; i < l; i += 2 ) {
 
-						}
+					_start$1.copy( vertices[ i ] );
+					_end$1.copy( vertices[ i + 1 ] );
 
-					}
+					lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
+					lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 );
 
 				}
 
 			}
 
-			/* Faces */
+			return this;
 
-			// Top and bottom faces
+		}
 
-			buildLidFaces();
+	} );
 
-			// Sides faces
+	/**
+	 * @author mgreter / http://github.com/mgreter
+	 */
 
-			buildSideFaces();
+	function LineLoop( geometry, material ) {
 
+		Line.call( this, geometry, material );
 
-			/////  Internal functions
+		this.type = 'LineLoop';
 
-			function buildLidFaces() {
+	}
 
-				var start = verticesArray.length / 3;
+	LineLoop.prototype = Object.assign( Object.create( Line.prototype ), {
 
-				if ( bevelEnabled ) {
+		constructor: LineLoop,
 
-					var layer = 0; // steps + 1
-					var offset = vlen * layer;
+		isLineLoop: true,
 
-					// Bottom faces
+	} );
 
-					for ( i = 0; i < flen; i ++ ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *  map: new THREE.Texture( <Image> ),
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  size: <float>,
+	 *  sizeAttenuation: <bool>
+	 *
+	 *  morphTargets: <bool>
+	 * }
+	 */
 
-						face = faces[ i ];
-						f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset );
+	function PointsMaterial( parameters ) {
 
-					}
+		Material.call( this );
 
-					layer = steps + bevelSegments * 2;
-					offset = vlen * layer;
+		this.type = 'PointsMaterial';
 
-					// Top faces
+		this.color = new Color( 0xffffff );
 
-					for ( i = 0; i < flen; i ++ ) {
+		this.map = null;
 
-						face = faces[ i ];
-						f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
+		this.alphaMap = null;
 
-					}
+		this.size = 1;
+		this.sizeAttenuation = true;
 
-				} else {
+		this.morphTargets = false;
 
-					// Bottom faces
+		this.setValues( parameters );
 
-					for ( i = 0; i < flen; i ++ ) {
+	}
 
-						face = faces[ i ];
-						f3( face[ 2 ], face[ 1 ], face[ 0 ] );
+	PointsMaterial.prototype = Object.create( Material.prototype );
+	PointsMaterial.prototype.constructor = PointsMaterial;
 
-					}
+	PointsMaterial.prototype.isPointsMaterial = true;
 
-					// Top faces
+	PointsMaterial.prototype.copy = function ( source ) {
 
-					for ( i = 0; i < flen; i ++ ) {
+		Material.prototype.copy.call( this, source );
 
-						face = faces[ i ];
-						f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
+		this.color.copy( source.color );
 
-					}
+		this.map = source.map;
 
-				}
+		this.alphaMap = source.alphaMap;
 
-				scope.addGroup( start, verticesArray.length / 3 - start, 0 );
+		this.size = source.size;
+		this.sizeAttenuation = source.sizeAttenuation;
 
-			}
+		this.morphTargets = source.morphTargets;
 
-			// Create faces for the z-sides of the shape
+		return this;
 
-			function buildSideFaces() {
+	};
 
-				var start = verticesArray.length / 3;
-				var layeroffset = 0;
-				sidewalls( contour, layeroffset );
-				layeroffset += contour.length;
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
+	var _inverseMatrix$2 = new Matrix4();
+	var _ray$2 = new Ray();
+	var _sphere$3 = new Sphere();
+	var _position$1 = new Vector3();
 
-					ahole = holes[ h ];
-					sidewalls( ahole, layeroffset );
+	function Points( geometry, material ) {
 
-					//, true
-					layeroffset += ahole.length;
+		Object3D.call( this );
 
-				}
+		this.type = 'Points';
 
+		this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
+		this.material = material !== undefined ? material : new PointsMaterial();
 
-				scope.addGroup( start, verticesArray.length / 3 - start, 1 );
+		this.updateMorphTargets();
 
+	}
 
-			}
+	Points.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-			function sidewalls( contour, layeroffset ) {
+		constructor: Points,
 
-				var j, k;
-				i = contour.length;
+		isPoints: true,
 
-				while ( -- i >= 0 ) {
+		raycast: function ( raycaster, intersects ) {
 
-					j = i;
-					k = i - 1;
-					if ( k < 0 ) { k = contour.length - 1; }
+			var geometry = this.geometry;
+			var matrixWorld = this.matrixWorld;
+			var threshold = raycaster.params.Points.threshold;
 
-					//console.log('b', i,j, i-1, k,vertices.length);
+			// Checking boundingSphere distance to ray
 
-					var s = 0,
-						sl = steps + bevelSegments * 2;
+			if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); }
 
-					for ( s = 0; s < sl; s ++ ) {
+			_sphere$3.copy( geometry.boundingSphere );
+			_sphere$3.applyMatrix4( matrixWorld );
+			_sphere$3.radius += threshold;
 
-						var slen1 = vlen * s;
-						var slen2 = vlen * ( s + 1 );
+			if ( raycaster.ray.intersectsSphere( _sphere$3 ) === false ) { return; }
 
-						var a = layeroffset + j + slen1,
-							b = layeroffset + k + slen1,
-							c = layeroffset + k + slen2,
-							d = layeroffset + j + slen2;
+			//
 
-						f4( a, b, c, d );
+			_inverseMatrix$2.getInverse( matrixWorld );
+			_ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 );
 
-					}
+			var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
+			var localThresholdSq = localThreshold * localThreshold;
 
-				}
+			if ( geometry.isBufferGeometry ) {
 
-			}
+				var index = geometry.index;
+				var attributes = geometry.attributes;
+				var positions = attributes.position.array;
 
-			function v( x, y, z ) {
+				if ( index !== null ) {
 
-				placeholder.push( x );
-				placeholder.push( y );
-				placeholder.push( z );
+					var indices = index.array;
 
-			}
+					for ( var i = 0, il = indices.length; i < il; i ++ ) {
 
+						var a = indices[ i ];
 
-			function f3( a, b, c ) {
+						_position$1.fromArray( positions, a * 3 );
 
-				addVertex( a );
-				addVertex( b );
-				addVertex( c );
+						testPoint( _position$1, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
 
-				var nextIndex = verticesArray.length / 3;
-				var uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
+					}
 
-				addUV( uvs[ 0 ] );
-				addUV( uvs[ 1 ] );
-				addUV( uvs[ 2 ] );
+				} else {
 
-			}
+					for ( var i = 0, l = positions.length / 3; i < l; i ++ ) {
 
-			function f4( a, b, c, d ) {
+						_position$1.fromArray( positions, i * 3 );
 
-				addVertex( a );
-				addVertex( b );
-				addVertex( d );
+						testPoint( _position$1, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
 
-				addVertex( b );
-				addVertex( c );
-				addVertex( d );
+					}
 
+				}
 
-				var nextIndex = verticesArray.length / 3;
-				var uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
+			} else {
 
-				addUV( uvs[ 0 ] );
-				addUV( uvs[ 1 ] );
-				addUV( uvs[ 3 ] );
+				var vertices = geometry.vertices;
 
-				addUV( uvs[ 1 ] );
-				addUV( uvs[ 2 ] );
-				addUV( uvs[ 3 ] );
+				for ( var i = 0, l = vertices.length; i < l; i ++ ) {
 
-			}
+					testPoint( vertices[ i ], i, localThresholdSq, matrixWorld, raycaster, intersects, this );
 
-			function addVertex( index ) {
-
-				verticesArray.push( placeholder[ index * 3 + 0 ] );
-				verticesArray.push( placeholder[ index * 3 + 1 ] );
-				verticesArray.push( placeholder[ index * 3 + 2 ] );
+				}
 
 			}
 
+		},
 
-			function addUV( vector2 ) {
-
-				uvArray.push( vector2.x );
-				uvArray.push( vector2.y );
+		updateMorphTargets: function () {
 
-			}
+			var geometry = this.geometry;
+			var m, ml, name;
 
-		}
+			if ( geometry.isBufferGeometry ) {
 
-	}
+				var morphAttributes = geometry.morphAttributes;
+				var keys = Object.keys( morphAttributes );
 
-	ExtrudeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	ExtrudeBufferGeometry.prototype.constructor = ExtrudeBufferGeometry;
+				if ( keys.length > 0 ) {
 
-	ExtrudeBufferGeometry.prototype.toJSON = function () {
+					var morphAttribute = morphAttributes[ keys[ 0 ] ];
 
-		var data = BufferGeometry.prototype.toJSON.call( this );
+					if ( morphAttribute !== undefined ) {
 
-		var shapes = this.parameters.shapes;
-		var options = this.parameters.options;
+						this.morphTargetInfluences = [];
+						this.morphTargetDictionary = {};
 
-		return toJSON( shapes, options, data );
+						for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
 
-	};
+							name = morphAttribute[ m ].name || String( m );
 
-	//
+							this.morphTargetInfluences.push( 0 );
+							this.morphTargetDictionary[ name ] = m;
 
-	var WorldUVGenerator = {
+						}
 
-		generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) {
+					}
 
-			var a_x = vertices[ indexA * 3 ];
-			var a_y = vertices[ indexA * 3 + 1 ];
-			var b_x = vertices[ indexB * 3 ];
-			var b_y = vertices[ indexB * 3 + 1 ];
-			var c_x = vertices[ indexC * 3 ];
-			var c_y = vertices[ indexC * 3 + 1 ];
+				}
 
-			return [
-				new Vector2( a_x, a_y ),
-				new Vector2( b_x, b_y ),
-				new Vector2( c_x, c_y )
-			];
+			} else {
 
-		},
+				var morphTargets = geometry.morphTargets;
 
-		generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) {
+				if ( morphTargets !== undefined && morphTargets.length > 0 ) {
 
-			var a_x = vertices[ indexA * 3 ];
-			var a_y = vertices[ indexA * 3 + 1 ];
-			var a_z = vertices[ indexA * 3 + 2 ];
-			var b_x = vertices[ indexB * 3 ];
-			var b_y = vertices[ indexB * 3 + 1 ];
-			var b_z = vertices[ indexB * 3 + 2 ];
-			var c_x = vertices[ indexC * 3 ];
-			var c_y = vertices[ indexC * 3 + 1 ];
-			var c_z = vertices[ indexC * 3 + 2 ];
-			var d_x = vertices[ indexD * 3 ];
-			var d_y = vertices[ indexD * 3 + 1 ];
-			var d_z = vertices[ indexD * 3 + 2 ];
+					console.error( 'THREE.Points.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' );
 
-			if ( Math.abs( a_y - b_y ) < 0.01 ) {
+				}
 
-				return [
-					new Vector2( a_x, 1 - a_z ),
-					new Vector2( b_x, 1 - b_z ),
-					new Vector2( c_x, 1 - c_z ),
-					new Vector2( d_x, 1 - d_z )
-				];
+			}
 
-			} else {
+		},
 
-				return [
-					new Vector2( a_y, 1 - a_z ),
-					new Vector2( b_y, 1 - b_z ),
-					new Vector2( c_y, 1 - c_z ),
-					new Vector2( d_y, 1 - d_z )
-				];
+		clone: function () {
 
-			}
+			return new this.constructor( this.geometry, this.material ).copy( this );
 
 		}
-	};
 
-	function toJSON( shapes, options, data ) {
+	} );
 
-		//
+	function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
 
-		data.shapes = [];
+		var rayPointDistanceSq = _ray$2.distanceSqToPoint( point );
 
-		if ( Array.isArray( shapes ) ) {
+		if ( rayPointDistanceSq < localThresholdSq ) {
 
-			for ( var i = 0, l = shapes.length; i < l; i ++ ) {
+			var intersectPoint = new Vector3();
 
-				var shape = shapes[ i ];
+			_ray$2.closestPointToPoint( point, intersectPoint );
+			intersectPoint.applyMatrix4( matrixWorld );
 
-				data.shapes.push( shape.uuid );
+			var distance = raycaster.ray.origin.distanceTo( intersectPoint );
 
-			}
+			if ( distance < raycaster.near || distance > raycaster.far ) { return; }
 
-		} else {
+			intersects.push( {
 
-			data.shapes.push( shapes.uuid );
+				distance: distance,
+				distanceToRay: Math.sqrt( rayPointDistanceSq ),
+				point: intersectPoint,
+				index: index,
+				face: null,
+				object: object
+
+			} );
 
 		}
 
-		//
+	}
 
-		if ( options.extrudePath !== undefined ) { data.options.extrudePath = options.extrudePath.toJSON(); }
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		return data;
+	function VideoTexture( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
+
+		Texture.call( this, video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
+
+		this.format = format !== undefined ? format : RGBFormat;
+
+		this.minFilter = minFilter !== undefined ? minFilter : LinearFilter;
+		this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
+
+		this.generateMipmaps = false;
 
 	}
 
+	VideoTexture.prototype = Object.assign( Object.create( Texture.prototype ), {
+
+		constructor: VideoTexture,
+
+		isVideoTexture: true,
+
+		update: function () {
+
+			var video = this.image;
+
+			if ( video.readyState >= video.HAVE_CURRENT_DATA ) {
+
+				this.needsUpdate = true;
+
+			}
+
+		}
+
+	} );
+
 	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
 	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * Text = 3D Text
-	 *
-	 * parameters = {
-	 *  font: <THREE.Font>, // font
-	 *
-	 *  size: <float>, // size of the text
-	 *  height: <float>, // thickness to extrude text
-	 *  curveSegments: <int>, // number of points on the curves
-	 *
-	 *  bevelEnabled: <bool>, // turn on bevel
-	 *  bevelThickness: <float>, // how deep into text bevel goes
-	 *  bevelSize: <float>, // how far from text outline (including bevelOffset) is bevel
-	 *  bevelOffset: <float> // how far from text outline does bevel start
-	 * }
 	 */
 
-	// TextGeometry
+	function CompressedTexture( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) {
 
-	function TextGeometry( text, parameters ) {
+		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
 
-		Geometry.call( this );
+		this.image = { width: width, height: height };
+		this.mipmaps = mipmaps;
 
-		this.type = 'TextGeometry';
+		// no flipping for cube textures
+		// (also flipping doesn't work for compressed textures )
 
-		this.parameters = {
-			text: text,
-			parameters: parameters
-		};
+		this.flipY = false;
 
-		this.fromBufferGeometry( new TextBufferGeometry( text, parameters ) );
-		this.mergeVertices();
+		// can't generate mipmaps for compressed textures
+		// mips must be embedded in DDS files
+
+		this.generateMipmaps = false;
 
 	}
 
-	TextGeometry.prototype = Object.create( Geometry.prototype );
-	TextGeometry.prototype.constructor = TextGeometry;
+	CompressedTexture.prototype = Object.create( Texture.prototype );
+	CompressedTexture.prototype.constructor = CompressedTexture;
 
-	// TextBufferGeometry
+	CompressedTexture.prototype.isCompressedTexture = true;
 
-	function TextBufferGeometry( text, parameters ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		parameters = parameters || {};
+	function CanvasTexture( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
 
-		var font = parameters.font;
+		Texture.call( this, canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
 
-		if ( ! ( font && font.isFont ) ) {
+		this.needsUpdate = true;
 
-			console.error( 'THREE.TextGeometry: font parameter is not an instance of THREE.Font.' );
-			return new Geometry();
+	}
 
-		}
+	CanvasTexture.prototype = Object.create( Texture.prototype );
+	CanvasTexture.prototype.constructor = CanvasTexture;
+	CanvasTexture.prototype.isCanvasTexture = true;
 
-		var shapes = font.generateShapes( text, parameters.size );
+	/**
+	 * @author Matt DesLauriers / @mattdesl
+	 * @author atix / arthursilber.de
+	 */
 
-		// translate parameters to ExtrudeGeometry API
+	function DepthTexture( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format ) {
 
-		parameters.depth = parameters.height !== undefined ? parameters.height : 50;
+		format = format !== undefined ? format : DepthFormat;
 
-		// defaults
+		if ( format !== DepthFormat && format !== DepthStencilFormat ) {
 
-		if ( parameters.bevelThickness === undefined ) { parameters.bevelThickness = 10; }
-		if ( parameters.bevelSize === undefined ) { parameters.bevelSize = 8; }
-		if ( parameters.bevelEnabled === undefined ) { parameters.bevelEnabled = false; }
+			throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' );
 
-		ExtrudeBufferGeometry.call( this, shapes, parameters );
+		}
 
-		this.type = 'TextBufferGeometry';
+		if ( type === undefined && format === DepthFormat ) { type = UnsignedShortType; }
+		if ( type === undefined && format === DepthStencilFormat ) { type = UnsignedInt248Type; }
+
+		Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
+
+		this.image = { width: width, height: height };
+
+		this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
+		this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
+
+		this.flipY = false;
+		this.generateMipmaps	= false;
 
 	}
 
-	TextBufferGeometry.prototype = Object.create( ExtrudeBufferGeometry.prototype );
-	TextBufferGeometry.prototype.constructor = TextBufferGeometry;
+	DepthTexture.prototype = Object.create( Texture.prototype );
+	DepthTexture.prototype.constructor = DepthTexture;
+	DepthTexture.prototype.isDepthTexture = true;
 
 	/**
 	 * @author mrdoob / http://mrdoob.com/
-	 * @author benaadams / https://twitter.com/ben_a_adams
 	 * @author Mugen87 / https://github.com/Mugen87
 	 */
 
-	// SphereGeometry
+	function WireframeGeometry( geometry ) {
 
-	function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
+		BufferGeometry.call( this );
 
-		Geometry.call( this );
+		this.type = 'WireframeGeometry';
 
-		this.type = 'SphereGeometry';
+		// buffer
 
-		this.parameters = {
-			radius: radius,
-			widthSegments: widthSegments,
-			heightSegments: heightSegments,
-			phiStart: phiStart,
-			phiLength: phiLength,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
-		};
+		var vertices = [];
 
-		this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
-		this.mergeVertices();
+		// helper variables
 
-	}
+		var i, j, l, o, ol;
+		var edge = [ 0, 0 ], edges = {}, e, edge1, edge2;
+		var key, keys = [ 'a', 'b', 'c' ];
+		var vertex;
 
-	SphereGeometry.prototype = Object.create( Geometry.prototype );
-	SphereGeometry.prototype.constructor = SphereGeometry;
+		// different logic for Geometry and BufferGeometry
 
-	// SphereBufferGeometry
+		if ( geometry && geometry.isGeometry ) {
 
-	function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
+			// create a data structure that contains all edges without duplicates
 
-		BufferGeometry.call( this );
+			var faces = geometry.faces;
 
-		this.type = 'SphereBufferGeometry';
+			for ( i = 0, l = faces.length; i < l; i ++ ) {
 
-		this.parameters = {
-			radius: radius,
-			widthSegments: widthSegments,
-			heightSegments: heightSegments,
-			phiStart: phiStart,
-			phiLength: phiLength,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
-		};
+				var face = faces[ i ];
 
-		radius = radius || 1;
+				for ( j = 0; j < 3; j ++ ) {
 
-		widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
-		heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
+					edge1 = face[ keys[ j ] ];
+					edge2 = face[ keys[ ( j + 1 ) % 3 ] ];
+					edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates
+					edge[ 1 ] = Math.max( edge1, edge2 );
 
-		phiStart = phiStart !== undefined ? phiStart : 0;
-		phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
+					key = edge[ 0 ] + ',' + edge[ 1 ];
 
-		thetaStart = thetaStart !== undefined ? thetaStart : 0;
-		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
+					if ( edges[ key ] === undefined ) {
 
-		var thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
+						edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] };
 
-		var ix, iy;
+					}
 
-		var index = 0;
-		var grid = [];
+				}
 
-		var vertex = new Vector3();
-		var normal = new Vector3();
+			}
 
-		// buffers
+			// generate vertices
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+			for ( key in edges ) {
 
-		// generate vertices, normals and uvs
+				e = edges[ key ];
 
-		for ( iy = 0; iy <= heightSegments; iy ++ ) {
+				vertex = geometry.vertices[ e.index1 ];
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-			var verticesRow = [];
+				vertex = geometry.vertices[ e.index2 ];
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-			var v = iy / heightSegments;
+			}
 
-			// special case for the poles
+		} else if ( geometry && geometry.isBufferGeometry ) {
 
-			var uOffset = 0;
+			var position, indices, groups;
+			var group, start, count;
+			var index1, index2;
 
-			if ( iy == 0 && thetaStart == 0 ) {
+			vertex = new Vector3();
 
-				uOffset = 0.5 / widthSegments;
+			if ( geometry.index !== null ) {
 
-			} else if ( iy == heightSegments && thetaEnd == Math.PI ) {
+				// indexed BufferGeometry
 
-				uOffset = - 0.5 / widthSegments;
+				position = geometry.attributes.position;
+				indices = geometry.index;
+				groups = geometry.groups;
 
-			}
+				if ( groups.length === 0 ) {
 
-			for ( ix = 0; ix <= widthSegments; ix ++ ) {
+					groups = [ { start: 0, count: indices.count, materialIndex: 0 } ];
 
-				var u = ix / widthSegments;
+				}
 
-				// vertex
+				// create a data structure that contains all eges without duplicates
 
-				vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
-				vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
-				vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
+				for ( o = 0, ol = groups.length; o < ol; ++ o ) {
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+					group = groups[ o ];
 
-				// normal
+					start = group.start;
+					count = group.count;
 
-				normal.copy( vertex ).normalize();
-				normals.push( normal.x, normal.y, normal.z );
+					for ( i = start, l = ( start + count ); i < l; i += 3 ) {
 
-				// uv
+						for ( j = 0; j < 3; j ++ ) {
 
-				uvs.push( u + uOffset, 1 - v );
+							edge1 = indices.getX( i + j );
+							edge2 = indices.getX( i + ( j + 1 ) % 3 );
+							edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates
+							edge[ 1 ] = Math.max( edge1, edge2 );
 
-				verticesRow.push( index ++ );
+							key = edge[ 0 ] + ',' + edge[ 1 ];
 
-			}
+							if ( edges[ key ] === undefined ) {
 
-			grid.push( verticesRow );
+								edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] };
 
-		}
+							}
 
-		// indices
+						}
 
-		for ( iy = 0; iy < heightSegments; iy ++ ) {
+					}
 
-			for ( ix = 0; ix < widthSegments; ix ++ ) {
+				}
 
-				var a = grid[ iy ][ ix + 1 ];
-				var b = grid[ iy ][ ix ];
-				var c = grid[ iy + 1 ][ ix ];
-				var d = grid[ iy + 1 ][ ix + 1 ];
+				// generate vertices
 
-				if ( iy !== 0 || thetaStart > 0 ) { indices.push( a, b, d ); }
-				if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) { indices.push( b, c, d ); }
+				for ( key in edges ) {
+
+					e = edges[ key ];
+
+					vertex.fromBufferAttribute( position, e.index1 );
+					vertices.push( vertex.x, vertex.y, vertex.z );
+
+					vertex.fromBufferAttribute( position, e.index2 );
+					vertices.push( vertex.x, vertex.y, vertex.z );
+
+				}
+
+			} else {
+
+				// non-indexed BufferGeometry
+
+				position = geometry.attributes.position;
+
+				for ( i = 0, l = ( position.count / 3 ); i < l; i ++ ) {
+
+					for ( j = 0; j < 3; j ++ ) {
+
+						// three edges per triangle, an edge is represented as (index1, index2)
+						// e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
+
+						index1 = 3 * i + j;
+						vertex.fromBufferAttribute( position, index1 );
+						vertices.push( vertex.x, vertex.y, vertex.z );
+
+						index2 = 3 * i + ( ( j + 1 ) % 3 );
+						vertex.fromBufferAttribute( position, index2 );
+						vertices.push( vertex.x, vertex.y, vertex.z );
+
+					}
+
+				}
 
 			}
 
@@ -40078,72 +47403,57 @@ function simpleEnd(buf) {
 
 		// build geometry
 
-		this.setIndex( indices );
 		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
 	}
 
-	SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
+	WireframeGeometry.prototype = Object.create( BufferGeometry.prototype );
+	WireframeGeometry.prototype.constructor = WireframeGeometry;
 
 	/**
-	 * @author Kaleb Murphy
+	 * @author zz85 / https://github.com/zz85
 	 * @author Mugen87 / https://github.com/Mugen87
+	 *
+	 * Parametric Surfaces Geometry
+	 * based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html
 	 */
 
-	// RingGeometry
+	// ParametricGeometry
 
-	function RingGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
+	function ParametricGeometry( func, slices, stacks ) {
 
 		Geometry.call( this );
 
-		this.type = 'RingGeometry';
+		this.type = 'ParametricGeometry';
 
 		this.parameters = {
-			innerRadius: innerRadius,
-			outerRadius: outerRadius,
-			thetaSegments: thetaSegments,
-			phiSegments: phiSegments,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			func: func,
+			slices: slices,
+			stacks: stacks
 		};
 
-		this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) );
+		this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
 		this.mergeVertices();
 
 	}
 
-	RingGeometry.prototype = Object.create( Geometry.prototype );
-	RingGeometry.prototype.constructor = RingGeometry;
+	ParametricGeometry.prototype = Object.create( Geometry.prototype );
+	ParametricGeometry.prototype.constructor = ParametricGeometry;
 
-	// RingBufferGeometry
+	// ParametricBufferGeometry
 
-	function RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
+	function ParametricBufferGeometry( func, slices, stacks ) {
 
 		BufferGeometry.call( this );
 
-		this.type = 'RingBufferGeometry';
+		this.type = 'ParametricBufferGeometry';
 
 		this.parameters = {
-			innerRadius: innerRadius,
-			outerRadius: outerRadius,
-			thetaSegments: thetaSegments,
-			phiSegments: phiSegments,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			func: func,
+			slices: slices,
+			stacks: stacks
 		};
 
-		innerRadius = innerRadius || 0.5;
-		outerRadius = outerRadius || 1;
-
-		thetaStart = thetaStart !== undefined ? thetaStart : 0;
-		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
-
-		thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
-		phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;
-
 		// buffers
 
 		var indices = [];
@@ -40151,67 +47461,91 @@ function simpleEnd(buf) {
 		var normals = [];
 		var uvs = [];
 
-		// some helper variables
+		var EPS = 0.00001;
 
-		var segment;
-		var radius = innerRadius;
-		var radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
-		var vertex = new Vector3();
-		var uv = new Vector2();
-		var j, i;
+		var normal = new Vector3();
 
-		// generate vertices, normals and uvs
+		var p0 = new Vector3(), p1 = new Vector3();
+		var pu = new Vector3(), pv = new Vector3();
 
-		for ( j = 0; j <= phiSegments; j ++ ) {
+		var i, j;
 
-			for ( i = 0; i <= thetaSegments; i ++ ) {
+		if ( func.length < 3 ) {
 
-				// values are generate from the inside of the ring to the outside
+			console.error( 'THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter.' );
 
-				segment = thetaStart + i / thetaSegments * thetaLength;
+		}
 
-				// vertex
+		// generate vertices, normals and uvs
 
-				vertex.x = radius * Math.cos( segment );
-				vertex.y = radius * Math.sin( segment );
+		var sliceCount = slices + 1;
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+		for ( i = 0; i <= stacks; i ++ ) {
+
+			var v = i / stacks;
+
+			for ( j = 0; j <= slices; j ++ ) {
+
+				var u = j / slices;
+
+				// vertex
+
+				func( u, v, p0 );
+				vertices.push( p0.x, p0.y, p0.z );
 
 				// normal
 
-				normals.push( 0, 0, 1 );
+				// approximate tangent vectors via finite differences
 
-				// uv
+				if ( u - EPS >= 0 ) {
 
-				uv.x = ( vertex.x / outerRadius + 1 ) / 2;
-				uv.y = ( vertex.y / outerRadius + 1 ) / 2;
+					func( u - EPS, v, p1 );
+					pu.subVectors( p0, p1 );
 
-				uvs.push( uv.x, uv.y );
+				} else {
 
-			}
+					func( u + EPS, v, p1 );
+					pu.subVectors( p1, p0 );
 
-			// increase the radius for next row of vertices
+				}
 
-			radius += radiusStep;
+				if ( v - EPS >= 0 ) {
 
-		}
+					func( u, v - EPS, p1 );
+					pv.subVectors( p0, p1 );
 
-		// indices
+				} else {
 
-		for ( j = 0; j < phiSegments; j ++ ) {
+					func( u, v + EPS, p1 );
+					pv.subVectors( p1, p0 );
 
-			var thetaSegmentLevel = j * ( thetaSegments + 1 );
+				}
 
-			for ( i = 0; i < thetaSegments; i ++ ) {
+				// cross product of tangent vectors returns surface normal
 
-				segment = i + thetaSegmentLevel;
+				normal.crossVectors( pu, pv ).normalize();
+				normals.push( normal.x, normal.y, normal.z );
 
-				var a = segment;
-				var b = segment + thetaSegments + 1;
-				var c = segment + thetaSegments + 2;
-				var d = segment + 1;
+				// uv
 
-				// faces
+				uvs.push( u, v );
+
+			}
+
+		}
+
+		// generate indices
+
+		for ( i = 0; i < stacks; i ++ ) {
+
+			for ( j = 0; j < slices; j ++ ) {
+
+				var a = i * sliceCount + j;
+				var b = i * sliceCount + j + 1;
+				var c = ( i + 1 ) * sliceCount + j + 1;
+				var d = ( i + 1 ) * sliceCount + j;
+
+				// faces one and two
 
 				indices.push( a, b, d );
 				indices.push( b, c, d );
@@ -40229,593 +47563,685 @@ function simpleEnd(buf) {
 
 	}
 
-	RingBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	RingBufferGeometry.prototype.constructor = RingBufferGeometry;
+	ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
 
 	/**
-	 * @author zz85 / https://github.com/zz85
-	 * @author bhouston / http://clara.io
+	 * @author clockworkgeek / https://github.com/clockworkgeek
+	 * @author timothypratley / https://github.com/timothypratley
+	 * @author WestLangley / http://github.com/WestLangley
 	 * @author Mugen87 / https://github.com/Mugen87
 	 */
 
-	// LatheGeometry
+	// PolyhedronGeometry
 
-	function LatheGeometry( points, segments, phiStart, phiLength ) {
+	function PolyhedronGeometry( vertices, indices, radius, detail ) {
 
 		Geometry.call( this );
 
-		this.type = 'LatheGeometry';
+		this.type = 'PolyhedronGeometry';
 
 		this.parameters = {
-			points: points,
-			segments: segments,
-			phiStart: phiStart,
-			phiLength: phiLength
+			vertices: vertices,
+			indices: indices,
+			radius: radius,
+			detail: detail
 		};
 
-		this.fromBufferGeometry( new LatheBufferGeometry( points, segments, phiStart, phiLength ) );
+		this.fromBufferGeometry( new PolyhedronBufferGeometry( vertices, indices, radius, detail ) );
 		this.mergeVertices();
 
 	}
 
-	LatheGeometry.prototype = Object.create( Geometry.prototype );
-	LatheGeometry.prototype.constructor = LatheGeometry;
+	PolyhedronGeometry.prototype = Object.create( Geometry.prototype );
+	PolyhedronGeometry.prototype.constructor = PolyhedronGeometry;
 
-	// LatheBufferGeometry
+	// PolyhedronBufferGeometry
 
-	function LatheBufferGeometry( points, segments, phiStart, phiLength ) {
+	function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
 
 		BufferGeometry.call( this );
 
-		this.type = 'LatheBufferGeometry';
+		this.type = 'PolyhedronBufferGeometry';
 
 		this.parameters = {
-			points: points,
-			segments: segments,
-			phiStart: phiStart,
-			phiLength: phiLength
+			vertices: vertices,
+			indices: indices,
+			radius: radius,
+			detail: detail
 		};
 
-		segments = Math.floor( segments ) || 12;
-		phiStart = phiStart || 0;
-		phiLength = phiLength || Math.PI * 2;
-
-		// clamp phiLength so it's in range of [ 0, 2PI ]
-
-		phiLength = MathUtils.clamp( phiLength, 0, Math.PI * 2 );
-
-
-		// buffers
+		radius = radius || 1;
+		detail = detail || 0;
 
-		var indices = [];
-		var vertices = [];
-		var uvs = [];
+		// default buffer data
 
-		// helper variables
+		var vertexBuffer = [];
+		var uvBuffer = [];
 
-		var base;
-		var inverseSegments = 1.0 / segments;
-		var vertex = new Vector3();
-		var uv = new Vector2();
-		var i, j;
+		// the subdivision creates the vertex buffer data
 
-		// generate vertices and uvs
+		subdivide( detail );
 
-		for ( i = 0; i <= segments; i ++ ) {
+		// all vertices should lie on a conceptual sphere with a given radius
 
-			var phi = phiStart + i * inverseSegments * phiLength;
+		applyRadius( radius );
 
-			var sin = Math.sin( phi );
-			var cos = Math.cos( phi );
+		// finally, create the uv data
 
-			for ( j = 0; j <= ( points.length - 1 ); j ++ ) {
+		generateUVs();
 
-				// vertex
+		// build non-indexed geometry
 
-				vertex.x = points[ j ].x * sin;
-				vertex.y = points[ j ].y;
-				vertex.z = points[ j ].x * cos;
+		this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
 
-				vertices.push( vertex.x, vertex.y, vertex.z );
+		if ( detail === 0 ) {
 
-				// uv
+			this.computeVertexNormals(); // flat normals
 
-				uv.x = i / segments;
-				uv.y = j / ( points.length - 1 );
+		} else {
 
-				uvs.push( uv.x, uv.y );
+			this.normalizeNormals(); // smooth normals
 
+		}
 
-			}
+		// helper functions
 
-		}
+		function subdivide( detail ) {
 
-		// indices
+			var a = new Vector3();
+			var b = new Vector3();
+			var c = new Vector3();
 
-		for ( i = 0; i < segments; i ++ ) {
+			// iterate over all faces and apply a subdivison with the given detail value
 
-			for ( j = 0; j < ( points.length - 1 ); j ++ ) {
+			for ( var i = 0; i < indices.length; i += 3 ) {
 
-				base = j + i * points.length;
+				// get the vertices of the face
 
-				var a = base;
-				var b = base + points.length;
-				var c = base + points.length + 1;
-				var d = base + 1;
+				getVertexByIndex( indices[ i + 0 ], a );
+				getVertexByIndex( indices[ i + 1 ], b );
+				getVertexByIndex( indices[ i + 2 ], c );
 
-				// faces
+				// perform subdivision
 
-				indices.push( a, b, d );
-				indices.push( b, c, d );
+				subdivideFace( a, b, c, detail );
 
 			}
 
 		}
 
-		// build geometry
-
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+		function subdivideFace( a, b, c, detail ) {
 
-		// generate normals
+			var cols = Math.pow( 2, detail );
 
-		this.computeVertexNormals();
+			// we use this multidimensional array as a data structure for creating the subdivision
 
-		// if the geometry is closed, we need to average the normals along the seam.
-		// because the corresponding vertices are identical (but still have different UVs).
+			var v = [];
 
-		if ( phiLength === Math.PI * 2 ) {
+			var i, j;
 
-			var normals = this.attributes.normal.array;
-			var n1 = new Vector3();
-			var n2 = new Vector3();
-			var n = new Vector3();
+			// construct all of the vertices for this subdivision
 
-			// this is the buffer offset for the last line of vertices
+			for ( i = 0; i <= cols; i ++ ) {
 
-			base = segments * points.length * 3;
+				v[ i ] = [];
 
-			for ( i = 0, j = 0; i < points.length; i ++, j += 3 ) {
+				var aj = a.clone().lerp( c, i / cols );
+				var bj = b.clone().lerp( c, i / cols );
 
-				// select the normal of the vertex in the first line
+				var rows = cols - i;
 
-				n1.x = normals[ j + 0 ];
-				n1.y = normals[ j + 1 ];
-				n1.z = normals[ j + 2 ];
+				for ( j = 0; j <= rows; j ++ ) {
 
-				// select the normal of the vertex in the last line
+					if ( j === 0 && i === cols ) {
 
-				n2.x = normals[ base + j + 0 ];
-				n2.y = normals[ base + j + 1 ];
-				n2.z = normals[ base + j + 2 ];
+						v[ i ][ j ] = aj;
 
-				// average normals
+					} else {
 
-				n.addVectors( n1, n2 ).normalize();
+						v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
 
-				// assign the new values to both normals
+					}
 
-				normals[ j + 0 ] = normals[ base + j + 0 ] = n.x;
-				normals[ j + 1 ] = normals[ base + j + 1 ] = n.y;
-				normals[ j + 2 ] = normals[ base + j + 2 ] = n.z;
+				}
 
 			}
 
-		}
+			// construct all of the faces
 
-	}
+			for ( i = 0; i < cols; i ++ ) {
 
-	LatheBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	LatheBufferGeometry.prototype.constructor = LatheBufferGeometry;
+				for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
 
-	/**
-	 * @author jonobr1 / http://jonobr1.com
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+					var k = Math.floor( j / 2 );
 
-	// ShapeGeometry
+					if ( j % 2 === 0 ) {
 
-	function ShapeGeometry( shapes, curveSegments ) {
+						pushVertex( v[ i ][ k + 1 ] );
+						pushVertex( v[ i + 1 ][ k ] );
+						pushVertex( v[ i ][ k ] );
 
-		Geometry.call( this );
+					} else {
 
-		this.type = 'ShapeGeometry';
+						pushVertex( v[ i ][ k + 1 ] );
+						pushVertex( v[ i + 1 ][ k + 1 ] );
+						pushVertex( v[ i + 1 ][ k ] );
 
-		if ( typeof curveSegments === 'object' ) {
+					}
 
-			console.warn( 'THREE.ShapeGeometry: Options parameter has been removed.' );
+				}
 
-			curveSegments = curveSegments.curveSegments;
+			}
 
 		}
 
-		this.parameters = {
-			shapes: shapes,
-			curveSegments: curveSegments
-		};
+		function applyRadius( radius ) {
 
-		this.fromBufferGeometry( new ShapeBufferGeometry( shapes, curveSegments ) );
-		this.mergeVertices();
+			var vertex = new Vector3();
 
-	}
+			// iterate over the entire buffer and apply the radius to each vertex
 
-	ShapeGeometry.prototype = Object.create( Geometry.prototype );
-	ShapeGeometry.prototype.constructor = ShapeGeometry;
+			for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
 
-	ShapeGeometry.prototype.toJSON = function () {
+				vertex.x = vertexBuffer[ i + 0 ];
+				vertex.y = vertexBuffer[ i + 1 ];
+				vertex.z = vertexBuffer[ i + 2 ];
 
-		var data = Geometry.prototype.toJSON.call( this );
+				vertex.normalize().multiplyScalar( radius );
 
-		var shapes = this.parameters.shapes;
+				vertexBuffer[ i + 0 ] = vertex.x;
+				vertexBuffer[ i + 1 ] = vertex.y;
+				vertexBuffer[ i + 2 ] = vertex.z;
 
-		return toJSON$1( shapes, data );
+			}
 
-	};
+		}
 
-	// ShapeBufferGeometry
+		function generateUVs() {
 
-	function ShapeBufferGeometry( shapes, curveSegments ) {
+			var vertex = new Vector3();
 
-		BufferGeometry.call( this );
+			for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
 
-		this.type = 'ShapeBufferGeometry';
+				vertex.x = vertexBuffer[ i + 0 ];
+				vertex.y = vertexBuffer[ i + 1 ];
+				vertex.z = vertexBuffer[ i + 2 ];
 
-		this.parameters = {
-			shapes: shapes,
-			curveSegments: curveSegments
-		};
+				var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
+				var v = inclination( vertex ) / Math.PI + 0.5;
+				uvBuffer.push( u, 1 - v );
 
-		curveSegments = curveSegments || 12;
+			}
 
-		// buffers
+			correctUVs();
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+			correctSeam();
 
-		// helper variables
+		}
 
-		var groupStart = 0;
-		var groupCount = 0;
+		function correctSeam() {
 
-		// allow single and array values for "shapes" parameter
+			// handle case when face straddles the seam, see #3269
 
-		if ( Array.isArray( shapes ) === false ) {
+			for ( var i = 0; i < uvBuffer.length; i += 6 ) {
 
-			addShape( shapes );
+				// uv data of a single face
 
-		} else {
+				var x0 = uvBuffer[ i + 0 ];
+				var x1 = uvBuffer[ i + 2 ];
+				var x2 = uvBuffer[ i + 4 ];
 
-			for ( var i = 0; i < shapes.length; i ++ ) {
+				var max = Math.max( x0, x1, x2 );
+				var min = Math.min( x0, x1, x2 );
 
-				addShape( shapes[ i ] );
+				// 0.9 is somewhat arbitrary
 
-				this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
+				if ( max > 0.9 && min < 0.1 ) {
 
-				groupStart += groupCount;
-				groupCount = 0;
+					if ( x0 < 0.2 ) { uvBuffer[ i + 0 ] += 1; }
+					if ( x1 < 0.2 ) { uvBuffer[ i + 2 ] += 1; }
+					if ( x2 < 0.2 ) { uvBuffer[ i + 4 ] += 1; }
+
+				}
 
 			}
 
 		}
 
-		// build geometry
+		function pushVertex( vertex ) {
 
-		this.setIndex( indices );
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
-		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
+			vertexBuffer.push( vertex.x, vertex.y, vertex.z );
 
+		}
 
-		// helper functions
+		function getVertexByIndex( index, vertex ) {
 
-		function addShape( shape ) {
+			var stride = index * 3;
 
-			var i, l, shapeHole;
+			vertex.x = vertices[ stride + 0 ];
+			vertex.y = vertices[ stride + 1 ];
+			vertex.z = vertices[ stride + 2 ];
 
-			var indexOffset = vertices.length / 3;
-			var points = shape.extractPoints( curveSegments );
+		}
 
-			var shapeVertices = points.shape;
-			var shapeHoles = points.holes;
+		function correctUVs() {
 
-			// check direction of vertices
+			var a = new Vector3();
+			var b = new Vector3();
+			var c = new Vector3();
 
-			if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
+			var centroid = new Vector3();
 
-				shapeVertices = shapeVertices.reverse();
+			var uvA = new Vector2();
+			var uvB = new Vector2();
+			var uvC = new Vector2();
 
-			}
+			for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
 
-			for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
+				a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
+				b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
+				c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
 
-				shapeHole = shapeHoles[ i ];
+				uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
+				uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
+				uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
 
-				if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
+				centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
 
-					shapeHoles[ i ] = shapeHole.reverse();
+				var azi = azimuth( centroid );
 
-				}
+				correctUV( uvA, j + 0, a, azi );
+				correctUV( uvB, j + 2, b, azi );
+				correctUV( uvC, j + 4, c, azi );
 
 			}
 
-			var faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
+		}
 
-			// join vertices of inner and outer paths to a single array
+		function correctUV( uv, stride, vector, azimuth ) {
 
-			for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
+			if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
 
-				shapeHole = shapeHoles[ i ];
-				shapeVertices = shapeVertices.concat( shapeHole );
+				uvBuffer[ stride ] = uv.x - 1;
 
 			}
 
-			// vertices, normals, uvs
+			if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
 
-			for ( i = 0, l = shapeVertices.length; i < l; i ++ ) {
+				uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
 
-				var vertex = shapeVertices[ i ];
+			}
 
-				vertices.push( vertex.x, vertex.y, 0 );
-				normals.push( 0, 0, 1 );
-				uvs.push( vertex.x, vertex.y ); // world uvs
+		}
 
-			}
+		// Angle around the Y axis, counter-clockwise when looking from above.
 
-			// incides
+		function azimuth( vector ) {
 
-			for ( i = 0, l = faces.length; i < l; i ++ ) {
+			return Math.atan2( vector.z, - vector.x );
 
-				var face = faces[ i ];
+		}
 
-				var a = face[ 0 ] + indexOffset;
-				var b = face[ 1 ] + indexOffset;
-				var c = face[ 2 ] + indexOffset;
 
-				indices.push( a, b, c );
-				groupCount += 3;
+		// Angle above the XZ plane.
 
-			}
+		function inclination( vector ) {
+
+			return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
 
 		}
 
 	}
 
-	ShapeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	ShapeBufferGeometry.prototype.constructor = ShapeBufferGeometry;
-
-	ShapeBufferGeometry.prototype.toJSON = function () {
+	PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
 
-		var data = BufferGeometry.prototype.toJSON.call( this );
+	/**
+	 * @author timothypratley / https://github.com/timothypratley
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-		var shapes = this.parameters.shapes;
+	// TetrahedronGeometry
 
-		return toJSON$1( shapes, data );
+	function TetrahedronGeometry( radius, detail ) {
 
-	};
+		Geometry.call( this );
 
-	//
+		this.type = 'TetrahedronGeometry';
 
-	function toJSON$1( shapes, data ) {
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
-		data.shapes = [];
+		this.fromBufferGeometry( new TetrahedronBufferGeometry( radius, detail ) );
+		this.mergeVertices();
 
-		if ( Array.isArray( shapes ) ) {
+	}
 
-			for ( var i = 0, l = shapes.length; i < l; i ++ ) {
+	TetrahedronGeometry.prototype = Object.create( Geometry.prototype );
+	TetrahedronGeometry.prototype.constructor = TetrahedronGeometry;
 
-				var shape = shapes[ i ];
+	// TetrahedronBufferGeometry
 
-				data.shapes.push( shape.uuid );
+	function TetrahedronBufferGeometry( radius, detail ) {
 
-			}
+		var vertices = [
+			1, 1, 1, 	- 1, - 1, 1, 	- 1, 1, - 1, 	1, - 1, - 1
+		];
 
-		} else {
+		var indices = [
+			2, 1, 0, 	0, 3, 2,	1, 3, 0,	2, 3, 1
+		];
 
-			data.shapes.push( shapes.uuid );
+		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
 
-		}
+		this.type = 'TetrahedronBufferGeometry';
 
-		return data;
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
 	}
 
+	TetrahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
+	TetrahedronBufferGeometry.prototype.constructor = TetrahedronBufferGeometry;
+
 	/**
-	 * @author WestLangley / http://github.com/WestLangley
+	 * @author timothypratley / https://github.com/timothypratley
 	 * @author Mugen87 / https://github.com/Mugen87
 	 */
 
-	function EdgesGeometry( geometry, thresholdAngle ) {
+	// OctahedronGeometry
 
-		BufferGeometry.call( this );
+	function OctahedronGeometry( radius, detail ) {
 
-		this.type = 'EdgesGeometry';
+		Geometry.call( this );
+
+		this.type = 'OctahedronGeometry';
 
 		this.parameters = {
-			thresholdAngle: thresholdAngle
+			radius: radius,
+			detail: detail
 		};
 
-		thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
+		this.fromBufferGeometry( new OctahedronBufferGeometry( radius, detail ) );
+		this.mergeVertices();
 
-		// buffer
+	}
 
-		var vertices = [];
+	OctahedronGeometry.prototype = Object.create( Geometry.prototype );
+	OctahedronGeometry.prototype.constructor = OctahedronGeometry;
 
-		// helper variables
+	// OctahedronBufferGeometry
 
-		var thresholdDot = Math.cos( MathUtils.DEG2RAD * thresholdAngle );
-		var edge = [ 0, 0 ], edges = {}, edge1, edge2;
-		var key, keys = [ 'a', 'b', 'c' ];
+	function OctahedronBufferGeometry( radius, detail ) {
 
-		// prepare source geometry
+		var vertices = [
+			1, 0, 0, 	- 1, 0, 0,	0, 1, 0,
+			0, - 1, 0, 	0, 0, 1,	0, 0, - 1
+		];
 
-		var geometry2;
+		var indices = [
+			0, 2, 4,	0, 4, 3,	0, 3, 5,
+			0, 5, 2,	1, 2, 5,	1, 5, 3,
+			1, 3, 4,	1, 4, 2
+		];
 
-		if ( geometry.isBufferGeometry ) {
+		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
 
-			geometry2 = new Geometry();
-			geometry2.fromBufferGeometry( geometry );
+		this.type = 'OctahedronBufferGeometry';
 
-		} else {
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
-			geometry2 = geometry.clone();
+	}
 
-		}
+	OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
+	OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
 
-		geometry2.mergeVertices();
-		geometry2.computeFaceNormals();
+	/**
+	 * @author timothypratley / https://github.com/timothypratley
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-		var sourceVertices = geometry2.vertices;
-		var faces = geometry2.faces;
+	// IcosahedronGeometry
 
-		// now create a data structure where each entry represents an edge with its adjoining faces
+	function IcosahedronGeometry( radius, detail ) {
 
-		for ( var i = 0, l = faces.length; i < l; i ++ ) {
+		Geometry.call( this );
 
-			var face = faces[ i ];
+		this.type = 'IcosahedronGeometry';
 
-			for ( var j = 0; j < 3; j ++ ) {
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
-				edge1 = face[ keys[ j ] ];
-				edge2 = face[ keys[ ( j + 1 ) % 3 ] ];
-				edge[ 0 ] = Math.min( edge1, edge2 );
-				edge[ 1 ] = Math.max( edge1, edge2 );
+		this.fromBufferGeometry( new IcosahedronBufferGeometry( radius, detail ) );
+		this.mergeVertices();
 
-				key = edge[ 0 ] + ',' + edge[ 1 ];
+	}
 
-				if ( edges[ key ] === undefined ) {
+	IcosahedronGeometry.prototype = Object.create( Geometry.prototype );
+	IcosahedronGeometry.prototype.constructor = IcosahedronGeometry;
 
-					edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ], face1: i, face2: undefined };
+	// IcosahedronBufferGeometry
 
-				} else {
+	function IcosahedronBufferGeometry( radius, detail ) {
 
-					edges[ key ].face2 = i;
+		var t = ( 1 + Math.sqrt( 5 ) ) / 2;
 
-				}
+		var vertices = [
+			- 1, t, 0, 	1, t, 0, 	- 1, - t, 0, 	1, - t, 0,
+			 0, - 1, t, 	0, 1, t,	0, - 1, - t, 	0, 1, - t,
+			 t, 0, - 1, 	t, 0, 1, 	- t, 0, - 1, 	- t, 0, 1
+		];
 
-			}
+		var indices = [
+			 0, 11, 5, 	0, 5, 1, 	0, 1, 7, 	0, 7, 10, 	0, 10, 11,
+			 1, 5, 9, 	5, 11, 4,	11, 10, 2,	10, 7, 6,	7, 1, 8,
+			 3, 9, 4, 	3, 4, 2,	3, 2, 6,	3, 6, 8,	3, 8, 9,
+			 4, 9, 5, 	2, 4, 11,	6, 2, 10,	8, 6, 7,	9, 8, 1
+		];
 
-		}
+		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
 
-		// generate vertices
+		this.type = 'IcosahedronBufferGeometry';
 
-		for ( key in edges ) {
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
-			var e = edges[ key ];
+	}
 
-			// an edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. default = 1 degree.
+	IcosahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
+	IcosahedronBufferGeometry.prototype.constructor = IcosahedronBufferGeometry;
 
-			if ( e.face2 === undefined || faces[ e.face1 ].normal.dot( faces[ e.face2 ].normal ) <= thresholdDot ) {
+	/**
+	 * @author Abe Pazos / https://hamoid.com
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-				var vertex = sourceVertices[ e.index1 ];
-				vertices.push( vertex.x, vertex.y, vertex.z );
+	// DodecahedronGeometry
 
-				vertex = sourceVertices[ e.index2 ];
-				vertices.push( vertex.x, vertex.y, vertex.z );
+	function DodecahedronGeometry( radius, detail ) {
 
-			}
+		Geometry.call( this );
 
-		}
+		this.type = 'DodecahedronGeometry';
 
-		// build geometry
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
 
-		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.fromBufferGeometry( new DodecahedronBufferGeometry( radius, detail ) );
+		this.mergeVertices();
 
 	}
 
-	EdgesGeometry.prototype = Object.create( BufferGeometry.prototype );
-	EdgesGeometry.prototype.constructor = EdgesGeometry;
+	DodecahedronGeometry.prototype = Object.create( Geometry.prototype );
+	DodecahedronGeometry.prototype.constructor = DodecahedronGeometry;
+
+	// DodecahedronBufferGeometry
+
+	function DodecahedronBufferGeometry( radius, detail ) {
+
+		var t = ( 1 + Math.sqrt( 5 ) ) / 2;
+		var r = 1 / t;
+
+		var vertices = [
+
+			// (±1, ±1, ±1)
+			- 1, - 1, - 1,	- 1, - 1, 1,
+			- 1, 1, - 1, - 1, 1, 1,
+			1, - 1, - 1, 1, - 1, 1,
+			1, 1, - 1, 1, 1, 1,
+
+			// (0, ±1/φ, ±φ)
+			 0, - r, - t, 0, - r, t,
+			 0, r, - t, 0, r, t,
+
+			// (±1/φ, ±φ, 0)
+			- r, - t, 0, - r, t, 0,
+			 r, - t, 0, r, t, 0,
+
+			// (±φ, 0, ±1/φ)
+			- t, 0, - r, t, 0, - r,
+			- t, 0, r, t, 0, r
+		];
+
+		var indices = [
+			3, 11, 7, 	3, 7, 15, 	3, 15, 13,
+			7, 19, 17, 	7, 17, 6, 	7, 6, 15,
+			17, 4, 8, 	17, 8, 10, 	17, 10, 6,
+			8, 0, 16, 	8, 16, 2, 	8, 2, 10,
+			0, 12, 1, 	0, 1, 18, 	0, 18, 16,
+			6, 10, 2, 	6, 2, 13, 	6, 13, 15,
+			2, 16, 18, 	2, 18, 3, 	2, 3, 13,
+			18, 1, 9, 	18, 9, 11, 	18, 11, 3,
+			4, 14, 12, 	4, 12, 0, 	4, 0, 8,
+			11, 9, 5, 	11, 5, 19, 	11, 19, 7,
+			19, 5, 14, 	19, 14, 4, 	19, 4, 17,
+			1, 12, 14, 	1, 14, 5, 	1, 5, 9
+		];
+
+		PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
+
+		this.type = 'DodecahedronBufferGeometry';
+
+		this.parameters = {
+			radius: radius,
+			detail: detail
+		};
+
+	}
+
+	DodecahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
+	DodecahedronBufferGeometry.prototype.constructor = DodecahedronBufferGeometry;
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author oosmoxiecode / https://github.com/oosmoxiecode
+	 * @author WestLangley / https://github.com/WestLangley
+	 * @author zz85 / https://github.com/zz85
+	 * @author miningold / https://github.com/miningold
+	 * @author jonobr1 / https://github.com/jonobr1
 	 * @author Mugen87 / https://github.com/Mugen87
+	 *
 	 */
 
-	// CylinderGeometry
+	// TubeGeometry
 
-	function CylinderGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
+	function TubeGeometry( path, tubularSegments, radius, radialSegments, closed, taper ) {
 
 		Geometry.call( this );
 
-		this.type = 'CylinderGeometry';
+		this.type = 'TubeGeometry';
 
 		this.parameters = {
-			radiusTop: radiusTop,
-			radiusBottom: radiusBottom,
-			height: height,
+			path: path,
+			tubularSegments: tubularSegments,
+			radius: radius,
 			radialSegments: radialSegments,
-			heightSegments: heightSegments,
-			openEnded: openEnded,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			closed: closed
 		};
 
-		this.fromBufferGeometry( new CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) );
+		if ( taper !== undefined ) { console.warn( 'THREE.TubeGeometry: taper has been removed.' ); }
+
+		var bufferGeometry = new TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed );
+
+		// expose internals
+
+		this.tangents = bufferGeometry.tangents;
+		this.normals = bufferGeometry.normals;
+		this.binormals = bufferGeometry.binormals;
+
+		// create geometry
+
+		this.fromBufferGeometry( bufferGeometry );
 		this.mergeVertices();
 
 	}
 
-	CylinderGeometry.prototype = Object.create( Geometry.prototype );
-	CylinderGeometry.prototype.constructor = CylinderGeometry;
+	TubeGeometry.prototype = Object.create( Geometry.prototype );
+	TubeGeometry.prototype.constructor = TubeGeometry;
 
-	// CylinderBufferGeometry
+	// TubeBufferGeometry
 
-	function CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
+	function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed ) {
 
 		BufferGeometry.call( this );
 
-		this.type = 'CylinderBufferGeometry';
+		this.type = 'TubeBufferGeometry';
 
 		this.parameters = {
-			radiusTop: radiusTop,
-			radiusBottom: radiusBottom,
-			height: height,
+			path: path,
+			tubularSegments: tubularSegments,
+			radius: radius,
 			radialSegments: radialSegments,
-			heightSegments: heightSegments,
-			openEnded: openEnded,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			closed: closed
 		};
 
-		var scope = this;
-
-		radiusTop = radiusTop !== undefined ? radiusTop : 1;
-		radiusBottom = radiusBottom !== undefined ? radiusBottom : 1;
-		height = height || 1;
-
-		radialSegments = Math.floor( radialSegments ) || 8;
-		heightSegments = Math.floor( heightSegments ) || 1;
+		tubularSegments = tubularSegments || 64;
+		radius = radius || 1;
+		radialSegments = radialSegments || 8;
+		closed = closed || false;
 
-		openEnded = openEnded !== undefined ? openEnded : false;
-		thetaStart = thetaStart !== undefined ? thetaStart : 0.0;
-		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
+		var frames = path.computeFrenetFrames( tubularSegments, closed );
 
-		// buffers
+		// expose internals
 
-		var indices = [];
-		var vertices = [];
-		var normals = [];
-		var uvs = [];
+		this.tangents = frames.tangents;
+		this.normals = frames.normals;
+		this.binormals = frames.binormals;
 
 		// helper variables
 
-		var index = 0;
-		var indexArray = [];
-		var halfHeight = height / 2;
-		var groupStart = 0;
+		var vertex = new Vector3();
+		var normal = new Vector3();
+		var uv = new Vector2();
+		var P = new Vector3();
 
-		// generate geometry
+		var i, j;
 
-		generateTorso();
+		// buffer
 
-		if ( openEnded === false ) {
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
+		var indices = [];
 
-			if ( radiusTop > 0 ) { generateCap( true ); }
-			if ( radiusBottom > 0 ) { generateCap( false ); }
+		// create buffer data
 
-		}
+		generateBufferData();
 
 		// build geometry
 
@@ -40824,317 +48250,367 @@ function simpleEnd(buf) {
 		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
 		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-		function generateTorso() {
+		// functions
 
-			var x, y;
-			var normal = new Vector3();
-			var vertex = new Vector3();
+		function generateBufferData() {
 
-			var groupCount = 0;
+			for ( i = 0; i < tubularSegments; i ++ ) {
 
-			// this will be used to calculate the normal
-			var slope = ( radiusBottom - radiusTop ) / height;
+				generateSegment( i );
 
-			// generate vertices, normals and uvs
+			}
 
-			for ( y = 0; y <= heightSegments; y ++ ) {
+			// if the geometry is not closed, generate the last row of vertices and normals
+			// at the regular position on the given path
+			//
+			// if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
 
-				var indexRow = [];
+			generateSegment( ( closed === false ) ? tubularSegments : 0 );
 
-				var v = y / heightSegments;
+			// uvs are generated in a separate function.
+			// this makes it easy compute correct values for closed geometries
 
-				// calculate the radius of the current row
+			generateUVs();
 
-				var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
+			// finally create faces
 
-				for ( x = 0; x <= radialSegments; x ++ ) {
+			generateIndices();
 
-					var u = x / radialSegments;
+		}
 
-					var theta = u * thetaLength + thetaStart;
+		function generateSegment( i ) {
 
-					var sinTheta = Math.sin( theta );
-					var cosTheta = Math.cos( theta );
+			// we use getPointAt to sample evenly distributed points from the given path
 
-					// vertex
+			P = path.getPointAt( i / tubularSegments, P );
 
-					vertex.x = radius * sinTheta;
-					vertex.y = - v * height + halfHeight;
-					vertex.z = radius * cosTheta;
-					vertices.push( vertex.x, vertex.y, vertex.z );
+			// retrieve corresponding normal and binormal
 
-					// normal
+			var N = frames.normals[ i ];
+			var B = frames.binormals[ i ];
 
-					normal.set( sinTheta, slope, cosTheta ).normalize();
-					normals.push( normal.x, normal.y, normal.z );
+			// generate normals and vertices for the current segment
 
-					// uv
+			for ( j = 0; j <= radialSegments; j ++ ) {
 
-					uvs.push( u, 1 - v );
+				var v = j / radialSegments * Math.PI * 2;
 
-					// save index of vertex in respective row
+				var sin = Math.sin( v );
+				var cos = - Math.cos( v );
 
-					indexRow.push( index ++ );
+				// normal
 
-				}
+				normal.x = ( cos * N.x + sin * B.x );
+				normal.y = ( cos * N.y + sin * B.y );
+				normal.z = ( cos * N.z + sin * B.z );
+				normal.normalize();
 
-				// now save vertices of the row in our index array
+				normals.push( normal.x, normal.y, normal.z );
 
-				indexArray.push( indexRow );
+				// vertex
+
+				vertex.x = P.x + radius * normal.x;
+				vertex.y = P.y + radius * normal.y;
+				vertex.z = P.z + radius * normal.z;
+
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
 			}
 
-			// generate indices
+		}
 
-			for ( x = 0; x < radialSegments; x ++ ) {
+		function generateIndices() {
 
-				for ( y = 0; y < heightSegments; y ++ ) {
+			for ( j = 1; j <= tubularSegments; j ++ ) {
 
-					// we use the index array to access the correct indices
+				for ( i = 1; i <= radialSegments; i ++ ) {
 
-					var a = indexArray[ y ][ x ];
-					var b = indexArray[ y + 1 ][ x ];
-					var c = indexArray[ y + 1 ][ x + 1 ];
-					var d = indexArray[ y ][ x + 1 ];
+					var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
+					var b = ( radialSegments + 1 ) * j + ( i - 1 );
+					var c = ( radialSegments + 1 ) * j + i;
+					var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
 
 					// faces
 
 					indices.push( a, b, d );
 					indices.push( b, c, d );
 
-					// update group counter
-
-					groupCount += 6;
-
 				}
 
 			}
 
-			// add a group to the geometry. this will ensure multi material support
+		}
 
-			scope.addGroup( groupStart, groupCount, 0 );
+		function generateUVs() {
 
-			// calculate new start value for groups
+			for ( i = 0; i <= tubularSegments; i ++ ) {
 
-			groupStart += groupCount;
+				for ( j = 0; j <= radialSegments; j ++ ) {
 
-		}
+					uv.x = i / tubularSegments;
+					uv.y = j / radialSegments;
 
-		function generateCap( top ) {
+					uvs.push( uv.x, uv.y );
 
-			var x, centerIndexStart, centerIndexEnd;
+				}
 
-			var uv = new Vector2();
-			var vertex = new Vector3();
+			}
 
-			var groupCount = 0;
+		}
 
-			var radius = ( top === true ) ? radiusTop : radiusBottom;
-			var sign = ( top === true ) ? 1 : - 1;
+	}
 
-			// save the index of the first center vertex
-			centerIndexStart = index;
+	TubeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	TubeBufferGeometry.prototype.constructor = TubeBufferGeometry;
 
-			// first we generate the center vertex data of the cap.
-			// because the geometry needs one set of uvs per face,
-			// we must generate a center vertex per face/segment
+	TubeBufferGeometry.prototype.toJSON = function () {
 
-			for ( x = 1; x <= radialSegments; x ++ ) {
+		var data = BufferGeometry.prototype.toJSON.call( this );
 
-				// vertex
+		data.path = this.parameters.path.toJSON();
 
-				vertices.push( 0, halfHeight * sign, 0 );
+		return data;
 
-				// normal
+	};
 
-				normals.push( 0, sign, 0 );
+	/**
+	 * @author oosmoxiecode
+	 * @author Mugen87 / https://github.com/Mugen87
+	 *
+	 * based on http://www.blackpawn.com/texts/pqtorus/
+	 */
 
-				// uv
+	// TorusKnotGeometry
 
-				uvs.push( 0.5, 0.5 );
+	function TorusKnotGeometry( radius, tube, tubularSegments, radialSegments, p, q, heightScale ) {
 
-				// increase index
+		Geometry.call( this );
 
-				index ++;
+		this.type = 'TorusKnotGeometry';
 
-			}
+		this.parameters = {
+			radius: radius,
+			tube: tube,
+			tubularSegments: tubularSegments,
+			radialSegments: radialSegments,
+			p: p,
+			q: q
+		};
 
-			// save the index of the last center vertex
+		if ( heightScale !== undefined ) { console.warn( 'THREE.TorusKnotGeometry: heightScale has been deprecated. Use .scale( x, y, z ) instead.' ); }
 
-			centerIndexEnd = index;
+		this.fromBufferGeometry( new TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) );
+		this.mergeVertices();
 
-			// now we generate the surrounding vertices, normals and uvs
+	}
 
-			for ( x = 0; x <= radialSegments; x ++ ) {
+	TorusKnotGeometry.prototype = Object.create( Geometry.prototype );
+	TorusKnotGeometry.prototype.constructor = TorusKnotGeometry;
 
-				var u = x / radialSegments;
-				var theta = u * thetaLength + thetaStart;
+	// TorusKnotBufferGeometry
 
-				var cosTheta = Math.cos( theta );
-				var sinTheta = Math.sin( theta );
+	function TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) {
 
-				// vertex
+		BufferGeometry.call( this );
 
-				vertex.x = radius * sinTheta;
-				vertex.y = halfHeight * sign;
-				vertex.z = radius * cosTheta;
-				vertices.push( vertex.x, vertex.y, vertex.z );
+		this.type = 'TorusKnotBufferGeometry';
 
-				// normal
+		this.parameters = {
+			radius: radius,
+			tube: tube,
+			tubularSegments: tubularSegments,
+			radialSegments: radialSegments,
+			p: p,
+			q: q
+		};
 
-				normals.push( 0, sign, 0 );
+		radius = radius || 1;
+		tube = tube || 0.4;
+		tubularSegments = Math.floor( tubularSegments ) || 64;
+		radialSegments = Math.floor( radialSegments ) || 8;
+		p = p || 2;
+		q = q || 3;
 
-				// uv
+		// buffers
 
-				uv.x = ( cosTheta * 0.5 ) + 0.5;
-				uv.y = ( sinTheta * 0.5 * sign ) + 0.5;
-				uvs.push( uv.x, uv.y );
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-				// increase index
+		// helper variables
 
-				index ++;
+		var i, j;
 
-			}
+		var vertex = new Vector3();
+		var normal = new Vector3();
 
-			// generate indices
+		var P1 = new Vector3();
+		var P2 = new Vector3();
 
-			for ( x = 0; x < radialSegments; x ++ ) {
+		var B = new Vector3();
+		var T = new Vector3();
+		var N = new Vector3();
 
-				var c = centerIndexStart + x;
-				var i = centerIndexEnd + x;
+		// generate vertices, normals and uvs
 
-				if ( top === true ) {
+		for ( i = 0; i <= tubularSegments; ++ i ) {
 
-					// face top
+			// the radian "u" is used to calculate the position on the torus curve of the current tubular segement
 
-					indices.push( i, i + 1, c );
+			var u = i / tubularSegments * p * Math.PI * 2;
 
-				} else {
+			// now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
+			// these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions
 
-					// face bottom
+			calculatePositionOnCurve( u, p, q, radius, P1 );
+			calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );
 
-					indices.push( i + 1, i, c );
+			// calculate orthonormal basis
 
-				}
+			T.subVectors( P2, P1 );
+			N.addVectors( P2, P1 );
+			B.crossVectors( T, N );
+			N.crossVectors( B, T );
 
-				groupCount += 3;
+			// normalize B, N. T can be ignored, we don't use it
 
-			}
+			B.normalize();
+			N.normalize();
 
-			// add a group to the geometry. this will ensure multi material support
+			for ( j = 0; j <= radialSegments; ++ j ) {
 
-			scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
+				// now calculate the vertices. they are nothing more than an extrusion of the torus curve.
+				// because we extrude a shape in the xy-plane, there is no need to calculate a z-value.
 
-			// calculate new start value for groups
+				var v = j / radialSegments * Math.PI * 2;
+				var cx = - tube * Math.cos( v );
+				var cy = tube * Math.sin( v );
 
-			groupStart += groupCount;
+				// now calculate the final vertex position.
+				// first we orient the extrusion with our basis vectos, then we add it to the current position on the curve
+
+				vertex.x = P1.x + ( cx * N.x + cy * B.x );
+				vertex.y = P1.y + ( cx * N.y + cy * B.y );
+				vertex.z = P1.z + ( cx * N.z + cy * B.z );
+
+				vertices.push( vertex.x, vertex.y, vertex.z );
+
+				// normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
+
+				normal.subVectors( vertex, P1 ).normalize();
+
+				normals.push( normal.x, normal.y, normal.z );
+
+				// uv
+
+				uvs.push( i / tubularSegments );
+				uvs.push( j / radialSegments );
+
+			}
 
 		}
 
-	}
+		// generate indices
 
-	CylinderBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	CylinderBufferGeometry.prototype.constructor = CylinderBufferGeometry;
+		for ( j = 1; j <= tubularSegments; j ++ ) {
 
-	/**
-	 * @author abelnation / http://github.com/abelnation
-	 */
+			for ( i = 1; i <= radialSegments; i ++ ) {
 
-	// ConeGeometry
+				// indices
 
-	function ConeGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
+				var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
+				var b = ( radialSegments + 1 ) * j + ( i - 1 );
+				var c = ( radialSegments + 1 ) * j + i;
+				var d = ( radialSegments + 1 ) * ( j - 1 ) + i;
 
-		CylinderGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
+				// faces
 
-		this.type = 'ConeGeometry';
+				indices.push( a, b, d );
+				indices.push( b, c, d );
 
-		this.parameters = {
-			radius: radius,
-			height: height,
-			radialSegments: radialSegments,
-			heightSegments: heightSegments,
-			openEnded: openEnded,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
-		};
+			}
 
-	}
+		}
 
-	ConeGeometry.prototype = Object.create( CylinderGeometry.prototype );
-	ConeGeometry.prototype.constructor = ConeGeometry;
+		// build geometry
 
-	// ConeBufferGeometry
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-	function ConeBufferGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
+		// this function calculates the current position on the torus curve
 
-		CylinderBufferGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
+		function calculatePositionOnCurve( u, p, q, radius, position ) {
 
-		this.type = 'ConeBufferGeometry';
+			var cu = Math.cos( u );
+			var su = Math.sin( u );
+			var quOverP = q / p * u;
+			var cs = Math.cos( quOverP );
 
-		this.parameters = {
-			radius: radius,
-			height: height,
-			radialSegments: radialSegments,
-			heightSegments: heightSegments,
-			openEnded: openEnded,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
-		};
+			position.x = radius * ( 2 + cs ) * 0.5 * cu;
+			position.y = radius * ( 2 + cs ) * su * 0.5;
+			position.z = radius * Math.sin( quOverP ) * 0.5;
+
+		}
 
 	}
 
-	ConeBufferGeometry.prototype = Object.create( CylinderBufferGeometry.prototype );
-	ConeBufferGeometry.prototype.constructor = ConeBufferGeometry;
+	TorusKnotBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	TorusKnotBufferGeometry.prototype.constructor = TorusKnotBufferGeometry;
 
 	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
+	 * @author oosmoxiecode
+	 * @author mrdoob / http://mrdoob.com/
 	 * @author Mugen87 / https://github.com/Mugen87
-	 * @author hughes
 	 */
 
-	// CircleGeometry
+	// TorusGeometry
 
-	function CircleGeometry( radius, segments, thetaStart, thetaLength ) {
+	function TorusGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
 
 		Geometry.call( this );
 
-		this.type = 'CircleGeometry';
+		this.type = 'TorusGeometry';
 
 		this.parameters = {
 			radius: radius,
-			segments: segments,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			tube: tube,
+			radialSegments: radialSegments,
+			tubularSegments: tubularSegments,
+			arc: arc
 		};
 
-		this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
+		this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) );
 		this.mergeVertices();
 
 	}
 
-	CircleGeometry.prototype = Object.create( Geometry.prototype );
-	CircleGeometry.prototype.constructor = CircleGeometry;
+	TorusGeometry.prototype = Object.create( Geometry.prototype );
+	TorusGeometry.prototype.constructor = TorusGeometry;
 
-	// CircleBufferGeometry
+	// TorusBufferGeometry
 
-	function CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) {
+	function TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
 
 		BufferGeometry.call( this );
 
-		this.type = 'CircleBufferGeometry';
+		this.type = 'TorusBufferGeometry';
 
 		this.parameters = {
 			radius: radius,
-			segments: segments,
-			thetaStart: thetaStart,
-			thetaLength: thetaLength
+			tube: tube,
+			radialSegments: radialSegments,
+			tubularSegments: tubularSegments,
+			arc: arc
 		};
 
 		radius = radius || 1;
-		segments = segments !== undefined ? Math.max( 3, segments ) : 8;
-
-		thetaStart = thetaStart !== undefined ? thetaStart : 0;
-		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
+		tube = tube || 0.4;
+		radialSegments = Math.floor( radialSegments ) || 8;
+		tubularSegments = Math.floor( tubularSegments ) || 6;
+		arc = arc || Math.PI * 2;
 
 		// buffers
 
@@ -41145,45 +48621,65 @@ function simpleEnd(buf) {
 
 		// helper variables
 
-		var i, s;
+		var center = new Vector3();
 		var vertex = new Vector3();
-		var uv = new Vector2();
+		var normal = new Vector3();
 
-		// center point
+		var j, i;
 
-		vertices.push( 0, 0, 0 );
-		normals.push( 0, 0, 1 );
-		uvs.push( 0.5, 0.5 );
+		// generate vertices, normals and uvs
 
-		for ( s = 0, i = 3; s <= segments; s ++, i += 3 ) {
+		for ( j = 0; j <= radialSegments; j ++ ) {
 
-			var segment = thetaStart + s / segments * thetaLength;
+			for ( i = 0; i <= tubularSegments; i ++ ) {
 
-			// vertex
+				var u = i / tubularSegments * arc;
+				var v = j / radialSegments * Math.PI * 2;
 
-			vertex.x = radius * Math.cos( segment );
-			vertex.y = radius * Math.sin( segment );
+				// vertex
 
-			vertices.push( vertex.x, vertex.y, vertex.z );
+				vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
+				vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
+				vertex.z = tube * Math.sin( v );
 
-			// normal
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-			normals.push( 0, 0, 1 );
+				// normal
 
-			// uvs
+				center.x = radius * Math.cos( u );
+				center.y = radius * Math.sin( u );
+				normal.subVectors( vertex, center ).normalize();
 
-			uv.x = ( vertices[ i ] / radius + 1 ) / 2;
-			uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
+				normals.push( normal.x, normal.y, normal.z );
 
-			uvs.push( uv.x, uv.y );
+				// uv
+
+				uvs.push( i / tubularSegments );
+				uvs.push( j / radialSegments );
+
+			}
 
 		}
 
-		// indices
+		// generate indices
 
-		for ( i = 1; i <= segments; i ++ ) {
+		for ( j = 1; j <= radialSegments; j ++ ) {
 
-			indices.push( i, i + 1, 0 );
+			for ( i = 1; i <= tubularSegments; i ++ ) {
+
+				// indices
+
+				var a = ( tubularSegments + 1 ) * j + i - 1;
+				var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
+				var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
+				var d = ( tubularSegments + 1 ) * j + i;
+
+				// faces
+
+				indices.push( a, b, d );
+				indices.push( b, c, d );
+
+			}
 
 		}
 
@@ -41196,1403 +48692,1077 @@ function simpleEnd(buf) {
 
 	}
 
-	CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
-	CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
-
-	var Geometries = /*#__PURE__*/Object.freeze({
-		__proto__: null,
-		WireframeGeometry: WireframeGeometry,
-		ParametricGeometry: ParametricGeometry,
-		ParametricBufferGeometry: ParametricBufferGeometry,
-		TetrahedronGeometry: TetrahedronGeometry,
-		TetrahedronBufferGeometry: TetrahedronBufferGeometry,
-		OctahedronGeometry: OctahedronGeometry,
-		OctahedronBufferGeometry: OctahedronBufferGeometry,
-		IcosahedronGeometry: IcosahedronGeometry,
-		IcosahedronBufferGeometry: IcosahedronBufferGeometry,
-		DodecahedronGeometry: DodecahedronGeometry,
-		DodecahedronBufferGeometry: DodecahedronBufferGeometry,
-		PolyhedronGeometry: PolyhedronGeometry,
-		PolyhedronBufferGeometry: PolyhedronBufferGeometry,
-		TubeGeometry: TubeGeometry,
-		TubeBufferGeometry: TubeBufferGeometry,
-		TorusKnotGeometry: TorusKnotGeometry,
-		TorusKnotBufferGeometry: TorusKnotBufferGeometry,
-		TorusGeometry: TorusGeometry,
-		TorusBufferGeometry: TorusBufferGeometry,
-		TextGeometry: TextGeometry,
-		TextBufferGeometry: TextBufferGeometry,
-		SphereGeometry: SphereGeometry,
-		SphereBufferGeometry: SphereBufferGeometry,
-		RingGeometry: RingGeometry,
-		RingBufferGeometry: RingBufferGeometry,
-		PlaneGeometry: PlaneGeometry,
-		PlaneBufferGeometry: PlaneBufferGeometry,
-		LatheGeometry: LatheGeometry,
-		LatheBufferGeometry: LatheBufferGeometry,
-		ShapeGeometry: ShapeGeometry,
-		ShapeBufferGeometry: ShapeBufferGeometry,
-		ExtrudeGeometry: ExtrudeGeometry,
-		ExtrudeBufferGeometry: ExtrudeBufferGeometry,
-		EdgesGeometry: EdgesGeometry,
-		ConeGeometry: ConeGeometry,
-		ConeBufferGeometry: ConeBufferGeometry,
-		CylinderGeometry: CylinderGeometry,
-		CylinderBufferGeometry: CylinderBufferGeometry,
-		CircleGeometry: CircleGeometry,
-		CircleBufferGeometry: CircleBufferGeometry,
-		BoxGeometry: BoxGeometry,
-		BoxBufferGeometry: BoxBufferGeometry
-	});
+	TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 *
-	 * parameters = {
-	 *  color: <THREE.Color>
-	 * }
+	 * @author Mugen87 / https://github.com/Mugen87
+	 * Port from https://github.com/mapbox/earcut (v2.2.2)
 	 */
 
-	function ShadowMaterial( parameters ) {
-
-		Material.call( this );
+	var Earcut = {
 
-		this.type = 'ShadowMaterial';
+		triangulate: function ( data, holeIndices, dim ) {
 
-		this.color = new Color( 0x000000 );
-		this.transparent = true;
+			dim = dim || 2;
 
-		this.setValues( parameters );
+			var hasHoles = holeIndices && holeIndices.length,
+				outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length,
+				outerNode = linkedList( data, 0, outerLen, dim, true ),
+				triangles = [];
 
-	}
+			if ( ! outerNode || outerNode.next === outerNode.prev ) { return triangles; }
 
-	ShadowMaterial.prototype = Object.create( Material.prototype );
-	ShadowMaterial.prototype.constructor = ShadowMaterial;
+			var minX, minY, maxX, maxY, x, y, invSize;
 
-	ShadowMaterial.prototype.isShadowMaterial = true;
+			if ( hasHoles ) { outerNode = eliminateHoles( data, holeIndices, outerNode, dim ); }
 
-	ShadowMaterial.prototype.copy = function ( source ) {
+			// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
+			if ( data.length > 80 * dim ) {
 
-		Material.prototype.copy.call( this, source );
+				minX = maxX = data[ 0 ];
+				minY = maxY = data[ 1 ];
 
-		this.color.copy( source.color );
+				for ( var i = dim; i < outerLen; i += dim ) {
 
-		return this;
+					x = data[ i ];
+					y = data[ i + 1 ];
+					if ( x < minX ) { minX = x; }
+					if ( y < minY ) { minY = y; }
+					if ( x > maxX ) { maxX = x; }
+					if ( y > maxY ) { maxY = y; }
 
-	};
+				}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				// minX, minY and invSize are later used to transform coords into integers for z-order calculation
+				invSize = Math.max( maxX - minX, maxY - minY );
+				invSize = invSize !== 0 ? 1 / invSize : 0;
 
-	function RawShaderMaterial( parameters ) {
+			}
 
-		ShaderMaterial.call( this, parameters );
+			earcutLinked( outerNode, triangles, dim, minX, minY, invSize );
 
-		this.type = 'RawShaderMaterial';
+			return triangles;
 
-	}
+		}
 
-	RawShaderMaterial.prototype = Object.create( ShaderMaterial.prototype );
-	RawShaderMaterial.prototype.constructor = RawShaderMaterial;
+	};
 
-	RawShaderMaterial.prototype.isRawShaderMaterial = true;
+	// create a circular doubly linked list from polygon points in the specified winding order
+	function linkedList( data, start, end, dim, clockwise ) {
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  roughness: <float>,
-	 *  metalness: <float>,
-	 *  opacity: <float>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  lightMap: new THREE.Texture( <Image> ),
-	 *  lightMapIntensity: <float>
-	 *
-	 *  aoMap: new THREE.Texture( <Image> ),
-	 *  aoMapIntensity: <float>
-	 *
-	 *  emissive: <hex>,
-	 *  emissiveIntensity: <float>
-	 *  emissiveMap: new THREE.Texture( <Image> ),
-	 *
-	 *  bumpMap: new THREE.Texture( <Image> ),
-	 *  bumpScale: <float>,
-	 *
-	 *  normalMap: new THREE.Texture( <Image> ),
-	 *  normalMapType: THREE.TangentSpaceNormalMap,
-	 *  normalScale: <Vector2>,
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  roughnessMap: new THREE.Texture( <Image> ),
-	 *
-	 *  metalnessMap: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
-	 *  envMapIntensity: <float>
-	 *
-	 *  refractionRatio: <float>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+		var i, last;
 
-	function MeshStandardMaterial( parameters ) {
+		if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) {
 
-		Material.call( this );
+			for ( i = start; i < end; i += dim ) { last = insertNode( i, data[ i ], data[ i + 1 ], last ); }
 
-		this.defines = { 'STANDARD': '' };
+		} else {
 
-		this.type = 'MeshStandardMaterial';
+			for ( i = end - dim; i >= start; i -= dim ) { last = insertNode( i, data[ i ], data[ i + 1 ], last ); }
 
-		this.color = new Color( 0xffffff ); // diffuse
-		this.roughness = 1.0;
-		this.metalness = 0.0;
+		}
 
-		this.map = null;
+		if ( last && equals( last, last.next ) ) {
 
-		this.lightMap = null;
-		this.lightMapIntensity = 1.0;
+			removeNode( last );
+			last = last.next;
 
-		this.aoMap = null;
-		this.aoMapIntensity = 1.0;
+		}
 
-		this.emissive = new Color( 0x000000 );
-		this.emissiveIntensity = 1.0;
-		this.emissiveMap = null;
+		return last;
 
-		this.bumpMap = null;
-		this.bumpScale = 1;
+	}
 
-		this.normalMap = null;
-		this.normalMapType = TangentSpaceNormalMap;
-		this.normalScale = new Vector2( 1, 1 );
+	// eliminate colinear or duplicate points
+	function filterPoints( start, end ) {
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+		if ( ! start ) { return start; }
+		if ( ! end ) { end = start; }
 
-		this.roughnessMap = null;
+		var p = start,
+			again;
+		do {
 
-		this.metalnessMap = null;
+			again = false;
 
-		this.alphaMap = null;
+			if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) {
 
-		this.envMap = null;
-		this.envMapIntensity = 1.0;
+				removeNode( p );
+				p = end = p.prev;
+				if ( p === p.next ) { break; }
+				again = true;
 
-		this.refractionRatio = 0.98;
+			} else {
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
-		this.wireframeLinecap = 'round';
-		this.wireframeLinejoin = 'round';
+				p = p.next;
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+			}
 
-		this.vertexTangents = false;
+		} while ( again || p !== end );
 
-		this.setValues( parameters );
+		return end;
 
 	}
 
-	MeshStandardMaterial.prototype = Object.create( Material.prototype );
-	MeshStandardMaterial.prototype.constructor = MeshStandardMaterial;
+	// main ear slicing loop which triangulates a polygon (given as a linked list)
+	function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) {
 
-	MeshStandardMaterial.prototype.isMeshStandardMaterial = true;
+		if ( ! ear ) { return; }
 
-	MeshStandardMaterial.prototype.copy = function ( source ) {
+		// interlink polygon nodes in z-order
+		if ( ! pass && invSize ) { indexCurve( ear, minX, minY, invSize ); }
 
-		Material.prototype.copy.call( this, source );
+		var stop = ear,
+			prev, next;
 
-		this.defines = { 'STANDARD': '' };
+		// iterate through ears, slicing them one by one
+		while ( ear.prev !== ear.next ) {
 
-		this.color.copy( source.color );
-		this.roughness = source.roughness;
-		this.metalness = source.metalness;
+			prev = ear.prev;
+			next = ear.next;
 
-		this.map = source.map;
+			if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) {
 
-		this.lightMap = source.lightMap;
-		this.lightMapIntensity = source.lightMapIntensity;
+				// cut off the triangle
+				triangles.push( prev.i / dim );
+				triangles.push( ear.i / dim );
+				triangles.push( next.i / dim );
 
-		this.aoMap = source.aoMap;
-		this.aoMapIntensity = source.aoMapIntensity;
+				removeNode( ear );
 
-		this.emissive.copy( source.emissive );
-		this.emissiveMap = source.emissiveMap;
-		this.emissiveIntensity = source.emissiveIntensity;
+				// skipping the next vertex leads to less sliver triangles
+				ear = next.next;
+				stop = next.next;
 
-		this.bumpMap = source.bumpMap;
-		this.bumpScale = source.bumpScale;
+				continue;
 
-		this.normalMap = source.normalMap;
-		this.normalMapType = source.normalMapType;
-		this.normalScale.copy( source.normalScale );
+			}
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+			ear = next;
 
-		this.roughnessMap = source.roughnessMap;
+			// if we looped through the whole remaining polygon and can't find any more ears
+			if ( ear === stop ) {
 
-		this.metalnessMap = source.metalnessMap;
+				// try filtering points and slicing again
+				if ( ! pass ) {
 
-		this.alphaMap = source.alphaMap;
+					earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 );
 
-		this.envMap = source.envMap;
-		this.envMapIntensity = source.envMapIntensity;
+					// if this didn't work, try curing all small self-intersections locally
 
-		this.refractionRatio = source.refractionRatio;
+				} else if ( pass === 1 ) {
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
-		this.wireframeLinecap = source.wireframeLinecap;
-		this.wireframeLinejoin = source.wireframeLinejoin;
+					ear = cureLocalIntersections( filterPoints( ear ), triangles, dim );
+					earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 );
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+					// as a last resort, try splitting the remaining polygon into two
 
-		this.vertexTangents = source.vertexTangents;
+				} else if ( pass === 2 ) {
 
-		return this;
+					splitEarcut( ear, triangles, dim, minX, minY, invSize );
 
-	};
+				}
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *  clearcoat: <float>,
-	 *  clearcoatMap: new THREE.Texture( <Image> ),
-	 *  clearcoatRoughness: <float>,
-	 *  clearcoatRoughnessMap: new THREE.Texture( <Image> ),
-	 *  clearcoatNormalScale: <Vector2>,
-	 *  clearcoatNormalMap: new THREE.Texture( <Image> ),
-	 *
-	 *  reflectivity: <float>,
-	 *
-	 *  sheen: <Color>,
-	 *
-	 *  transparency: <float>
-	 * }
-	 */
+				break;
 
-	function MeshPhysicalMaterial( parameters ) {
+			}
 
-		MeshStandardMaterial.call( this );
+		}
 
-		this.defines = {
+	}
 
-			'STANDARD': '',
-			'PHYSICAL': ''
+	// check whether a polygon node forms a valid ear with adjacent nodes
+	function isEar( ear ) {
 
-		};
+		var a = ear.prev,
+			b = ear,
+			c = ear.next;
 
-		this.type = 'MeshPhysicalMaterial';
+		if ( area( a, b, c ) >= 0 ) { return false; } // reflex, can't be an ear
 
-		this.clearcoat = 0.0;
-		this.clearcoatMap = null;
-		this.clearcoatRoughness = 0.0;
-		this.clearcoatRoughnessMap = null;
-		this.clearcoatNormalScale = new Vector2( 1, 1 );
-		this.clearcoatNormalMap = null;
+		// now make sure we don't have other points inside the potential ear
+		var p = ear.next.next;
 
-		this.reflectivity = 0.5; // maps to F0 = 0.04
+		while ( p !== ear.prev ) {
 
-		this.sheen = null; // null will disable sheen bsdf
+			if ( pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
+				area( p.prev, p, p.next ) >= 0 ) { return false; }
+			p = p.next;
 
-		this.transparency = 0.0;
+		}
 
-		this.setValues( parameters );
+		return true;
 
 	}
 
-	MeshPhysicalMaterial.prototype = Object.create( MeshStandardMaterial.prototype );
-	MeshPhysicalMaterial.prototype.constructor = MeshPhysicalMaterial;
-
-	MeshPhysicalMaterial.prototype.isMeshPhysicalMaterial = true;
+	function isEarHashed( ear, minX, minY, invSize ) {
 
-	MeshPhysicalMaterial.prototype.copy = function ( source ) {
+		var a = ear.prev,
+			b = ear,
+			c = ear.next;
 
-		MeshStandardMaterial.prototype.copy.call( this, source );
+		if ( area( a, b, c ) >= 0 ) { return false; } // reflex, can't be an ear
 
-		this.defines = {
+		// triangle bbox; min & max are calculated like this for speed
+		var minTX = a.x < b.x ? ( a.x < c.x ? a.x : c.x ) : ( b.x < c.x ? b.x : c.x ),
+			minTY = a.y < b.y ? ( a.y < c.y ? a.y : c.y ) : ( b.y < c.y ? b.y : c.y ),
+			maxTX = a.x > b.x ? ( a.x > c.x ? a.x : c.x ) : ( b.x > c.x ? b.x : c.x ),
+			maxTY = a.y > b.y ? ( a.y > c.y ? a.y : c.y ) : ( b.y > c.y ? b.y : c.y );
 
-			'STANDARD': '',
-			'PHYSICAL': ''
+		// z-order range for the current triangle bbox;
+		var minZ = zOrder( minTX, minTY, minX, minY, invSize ),
+			maxZ = zOrder( maxTX, maxTY, minX, minY, invSize );
 
-		};
+		var p = ear.prevZ,
+			n = ear.nextZ;
 
-		this.clearcoat = source.clearcoat;
-		this.clearcoatMap = source.clearcoatMap;
-		this.clearcoatRoughness = source.clearcoatRoughness;
-		this.clearcoatRoughnessMap = source.clearcoatRoughnessMap;
-		this.clearcoatNormalMap = source.clearcoatNormalMap;
-		this.clearcoatNormalScale.copy( source.clearcoatNormalScale );
+		// look for points inside the triangle in both directions
+		while ( p && p.z >= minZ && n && n.z <= maxZ ) {
 
-		this.reflectivity = source.reflectivity;
+			if ( p !== ear.prev && p !== ear.next &&
+				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
+				area( p.prev, p, p.next ) >= 0 ) { return false; }
+			p = p.prevZ;
 
-		if ( source.sheen ) {
+			if ( n !== ear.prev && n !== ear.next &&
+				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) &&
+				area( n.prev, n, n.next ) >= 0 ) { return false; }
+			n = n.nextZ;
 
-			this.sheen = ( this.sheen || new Color() ).copy( source.sheen );
+		}
 
-		} else {
+		// look for remaining points in decreasing z-order
+		while ( p && p.z >= minZ ) {
 
-			this.sheen = null;
+			if ( p !== ear.prev && p !== ear.next &&
+				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) &&
+				area( p.prev, p, p.next ) >= 0 ) { return false; }
+			p = p.prevZ;
 
 		}
 
-		this.transparency = source.transparency;
+		// look for remaining points in increasing z-order
+		while ( n && n.z <= maxZ ) {
 
-		return this;
+			if ( n !== ear.prev && n !== ear.next &&
+				pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) &&
+				area( n.prev, n, n.next ) >= 0 ) { return false; }
+			n = n.nextZ;
 
-	};
+		}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  specular: <hex>,
-	 *  shininess: <float>,
-	 *  opacity: <float>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  lightMap: new THREE.Texture( <Image> ),
-	 *  lightMapIntensity: <float>
-	 *
-	 *  aoMap: new THREE.Texture( <Image> ),
-	 *  aoMapIntensity: <float>
-	 *
-	 *  emissive: <hex>,
-	 *  emissiveIntensity: <float>
-	 *  emissiveMap: new THREE.Texture( <Image> ),
-	 *
-	 *  bumpMap: new THREE.Texture( <Image> ),
-	 *  bumpScale: <float>,
-	 *
-	 *  normalMap: new THREE.Texture( <Image> ),
-	 *  normalMapType: THREE.TangentSpaceNormalMap,
-	 *  normalScale: <Vector2>,
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  specularMap: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
-	 *  combine: THREE.MultiplyOperation,
-	 *  reflectivity: <float>,
-	 *  refractionRatio: <float>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+		return true;
 
-	function MeshPhongMaterial( parameters ) {
+	}
 
-		Material.call( this );
+	// go through all polygon nodes and cure small local self-intersections
+	function cureLocalIntersections( start, triangles, dim ) {
 
-		this.type = 'MeshPhongMaterial';
+		var p = start;
+		do {
 
-		this.color = new Color( 0xffffff ); // diffuse
-		this.specular = new Color( 0x111111 );
-		this.shininess = 30;
+			var a = p.prev,
+				b = p.next.next;
 
-		this.map = null;
+			if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) {
 
-		this.lightMap = null;
-		this.lightMapIntensity = 1.0;
+				triangles.push( a.i / dim );
+				triangles.push( p.i / dim );
+				triangles.push( b.i / dim );
 
-		this.aoMap = null;
-		this.aoMapIntensity = 1.0;
+				// remove two nodes involved
+				removeNode( p );
+				removeNode( p.next );
 
-		this.emissive = new Color( 0x000000 );
-		this.emissiveIntensity = 1.0;
-		this.emissiveMap = null;
+				p = start = b;
 
-		this.bumpMap = null;
-		this.bumpScale = 1;
+			}
 
-		this.normalMap = null;
-		this.normalMapType = TangentSpaceNormalMap;
-		this.normalScale = new Vector2( 1, 1 );
+			p = p.next;
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+		} while ( p !== start );
 
-		this.specularMap = null;
+		return filterPoints( p );
 
-		this.alphaMap = null;
+	}
 
-		this.envMap = null;
-		this.combine = MultiplyOperation;
-		this.reflectivity = 1;
-		this.refractionRatio = 0.98;
+	// try splitting polygon into two and triangulate them independently
+	function splitEarcut( start, triangles, dim, minX, minY, invSize ) {
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
-		this.wireframeLinecap = 'round';
-		this.wireframeLinejoin = 'round';
+		// look for a valid diagonal that divides the polygon into two
+		var a = start;
+		do {
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+			var b = a.next.next;
+			while ( b !== a.prev ) {
 
-		this.setValues( parameters );
+				if ( a.i !== b.i && isValidDiagonal( a, b ) ) {
 
-	}
+					// split the polygon in two by the diagonal
+					var c = splitPolygon( a, b );
 
-	MeshPhongMaterial.prototype = Object.create( Material.prototype );
-	MeshPhongMaterial.prototype.constructor = MeshPhongMaterial;
+					// filter colinear points around the cuts
+					a = filterPoints( a, a.next );
+					c = filterPoints( c, c.next );
 
-	MeshPhongMaterial.prototype.isMeshPhongMaterial = true;
+					// run earcut on each half
+					earcutLinked( a, triangles, dim, minX, minY, invSize );
+					earcutLinked( c, triangles, dim, minX, minY, invSize );
+					return;
 
-	MeshPhongMaterial.prototype.copy = function ( source ) {
+				}
 
-		Material.prototype.copy.call( this, source );
+				b = b.next;
 
-		this.color.copy( source.color );
-		this.specular.copy( source.specular );
-		this.shininess = source.shininess;
+			}
 
-		this.map = source.map;
+			a = a.next;
 
-		this.lightMap = source.lightMap;
-		this.lightMapIntensity = source.lightMapIntensity;
+		} while ( a !== start );
 
-		this.aoMap = source.aoMap;
-		this.aoMapIntensity = source.aoMapIntensity;
+	}
 
-		this.emissive.copy( source.emissive );
-		this.emissiveMap = source.emissiveMap;
-		this.emissiveIntensity = source.emissiveIntensity;
+	// link every hole into the outer loop, producing a single-ring polygon without holes
+	function eliminateHoles( data, holeIndices, outerNode, dim ) {
 
-		this.bumpMap = source.bumpMap;
-		this.bumpScale = source.bumpScale;
+		var queue = [],
+			i, len, start, end, list;
 
-		this.normalMap = source.normalMap;
-		this.normalMapType = source.normalMapType;
-		this.normalScale.copy( source.normalScale );
+		for ( i = 0, len = holeIndices.length; i < len; i ++ ) {
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+			start = holeIndices[ i ] * dim;
+			end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length;
+			list = linkedList( data, start, end, dim, false );
+			if ( list === list.next ) { list.steiner = true; }
+			queue.push( getLeftmost( list ) );
 
-		this.specularMap = source.specularMap;
+		}
 
-		this.alphaMap = source.alphaMap;
+		queue.sort( compareX );
 
-		this.envMap = source.envMap;
-		this.combine = source.combine;
-		this.reflectivity = source.reflectivity;
-		this.refractionRatio = source.refractionRatio;
+		// process holes from left to right
+		for ( i = 0; i < queue.length; i ++ ) {
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
-		this.wireframeLinecap = source.wireframeLinecap;
-		this.wireframeLinejoin = source.wireframeLinejoin;
+			eliminateHole( queue[ i ], outerNode );
+			outerNode = filterPoints( outerNode, outerNode.next );
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+		}
 
-		return this;
+		return outerNode;
 
-	};
+	}
 
-	/**
-	 * @author takahirox / http://github.com/takahirox
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  specular: <hex>,
-	 *  shininess: <float>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *  gradientMap: new THREE.Texture( <Image> ),
-	 *
-	 *  lightMap: new THREE.Texture( <Image> ),
-	 *  lightMapIntensity: <float>
-	 *
-	 *  aoMap: new THREE.Texture( <Image> ),
-	 *  aoMapIntensity: <float>
-	 *
-	 *  emissive: <hex>,
-	 *  emissiveIntensity: <float>
-	 *  emissiveMap: new THREE.Texture( <Image> ),
-	 *
-	 *  bumpMap: new THREE.Texture( <Image> ),
-	 *  bumpScale: <float>,
-	 *
-	 *  normalMap: new THREE.Texture( <Image> ),
-	 *  normalMapType: THREE.TangentSpaceNormalMap,
-	 *  normalScale: <Vector2>,
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  specularMap: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+	function compareX( a, b ) {
 
-	function MeshToonMaterial( parameters ) {
+		return a.x - b.x;
 
-		Material.call( this );
+	}
 
-		this.defines = { 'TOON': '' };
+	// find a bridge between vertices that connects hole with an outer ring and and link it
+	function eliminateHole( hole, outerNode ) {
 
-		this.type = 'MeshToonMaterial';
+		outerNode = findHoleBridge( hole, outerNode );
+		if ( outerNode ) {
 
-		this.color = new Color( 0xffffff );
-		this.specular = new Color( 0x111111 );
-		this.shininess = 30;
+			var b = splitPolygon( outerNode, hole );
 
-		this.map = null;
-		this.gradientMap = null;
+			// filter collinear points around the cuts
+			filterPoints( outerNode, outerNode.next );
+			filterPoints( b, b.next );
 
-		this.lightMap = null;
-		this.lightMapIntensity = 1.0;
+		}
 
-		this.aoMap = null;
-		this.aoMapIntensity = 1.0;
+	}
 
-		this.emissive = new Color( 0x000000 );
-		this.emissiveIntensity = 1.0;
-		this.emissiveMap = null;
+	// David Eberly's algorithm for finding a bridge between hole and outer polygon
+	function findHoleBridge( hole, outerNode ) {
 
-		this.bumpMap = null;
-		this.bumpScale = 1;
+		var p = outerNode,
+			hx = hole.x,
+			hy = hole.y,
+			qx = - Infinity,
+			m;
 
-		this.normalMap = null;
-		this.normalMapType = TangentSpaceNormalMap;
-		this.normalScale = new Vector2( 1, 1 );
+		// find a segment intersected by a ray from the hole's leftmost point to the left;
+		// segment's endpoint with lesser x will be potential connection point
+		do {
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+			if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) {
 
-		this.specularMap = null;
+				var x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y );
+				if ( x <= hx && x > qx ) {
 
-		this.alphaMap = null;
+					qx = x;
+					if ( x === hx ) {
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
-		this.wireframeLinecap = 'round';
-		this.wireframeLinejoin = 'round';
+						if ( hy === p.y ) { return p; }
+						if ( hy === p.next.y ) { return p.next; }
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+					}
 
-		this.setValues( parameters );
+					m = p.x < p.next.x ? p : p.next;
 
-	}
+				}
 
-	MeshToonMaterial.prototype = Object.create( Material.prototype );
-	MeshToonMaterial.prototype.constructor = MeshToonMaterial;
+			}
 
-	MeshToonMaterial.prototype.isMeshToonMaterial = true;
+			p = p.next;
 
-	MeshToonMaterial.prototype.copy = function ( source ) {
+		} while ( p !== outerNode );
 
-		Material.prototype.copy.call( this, source );
+		if ( ! m ) { return null; }
 
-		this.color.copy( source.color );
-		this.specular.copy( source.specular );
-		this.shininess = source.shininess;
+		if ( hx === qx ) { return m; } // hole touches outer segment; pick leftmost endpoint
 
-		this.map = source.map;
-		this.gradientMap = source.gradientMap;
+		// look for points inside the triangle of hole point, segment intersection and endpoint;
+		// if there are no points found, we have a valid connection;
+		// otherwise choose the point of the minimum angle with the ray as connection point
 
-		this.lightMap = source.lightMap;
-		this.lightMapIntensity = source.lightMapIntensity;
+		var stop = m,
+			mx = m.x,
+			my = m.y,
+			tanMin = Infinity,
+			tan;
 
-		this.aoMap = source.aoMap;
-		this.aoMapIntensity = source.aoMapIntensity;
+		p = m;
 
-		this.emissive.copy( source.emissive );
-		this.emissiveMap = source.emissiveMap;
-		this.emissiveIntensity = source.emissiveIntensity;
+		do {
 
-		this.bumpMap = source.bumpMap;
-		this.bumpScale = source.bumpScale;
+			if ( hx >= p.x && p.x >= mx && hx !== p.x &&
+					pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) {
 
-		this.normalMap = source.normalMap;
-		this.normalMapType = source.normalMapType;
-		this.normalScale.copy( source.normalScale );
+				tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+				if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) {
 
-		this.specularMap = source.specularMap;
+					m = p;
+					tanMin = tan;
 
-		this.alphaMap = source.alphaMap;
+				}
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
-		this.wireframeLinecap = source.wireframeLinecap;
-		this.wireframeLinejoin = source.wireframeLinejoin;
+			}
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
-
-		return this;
+			p = p.next;
 
-	};
+		} while ( p !== stop );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *  opacity: <float>,
-	 *
-	 *  bumpMap: new THREE.Texture( <Image> ),
-	 *  bumpScale: <float>,
-	 *
-	 *  normalMap: new THREE.Texture( <Image> ),
-	 *  normalMapType: THREE.TangentSpaceNormalMap,
-	 *  normalScale: <Vector2>,
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+		return m;
 
-	function MeshNormalMaterial( parameters ) {
+	}
 
-		Material.call( this );
+	// whether sector in vertex m contains sector in vertex p in the same coordinates
+	function sectorContainsSector( m, p ) {
 
-		this.type = 'MeshNormalMaterial';
+		return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0;
 
-		this.bumpMap = null;
-		this.bumpScale = 1;
+	}
 
-		this.normalMap = null;
-		this.normalMapType = TangentSpaceNormalMap;
-		this.normalScale = new Vector2( 1, 1 );
+	// interlink polygon nodes in z-order
+	function indexCurve( start, minX, minY, invSize ) {
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+		var p = start;
+		do {
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
+			if ( p.z === null ) { p.z = zOrder( p.x, p.y, minX, minY, invSize ); }
+			p.prevZ = p.prev;
+			p.nextZ = p.next;
+			p = p.next;
 
-		this.fog = false;
+		} while ( p !== start );
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+		p.prevZ.nextZ = null;
+		p.prevZ = null;
 
-		this.setValues( parameters );
+		sortLinked( p );
 
 	}
 
-	MeshNormalMaterial.prototype = Object.create( Material.prototype );
-	MeshNormalMaterial.prototype.constructor = MeshNormalMaterial;
-
-	MeshNormalMaterial.prototype.isMeshNormalMaterial = true;
-
-	MeshNormalMaterial.prototype.copy = function ( source ) {
-
-		Material.prototype.copy.call( this, source );
+	// Simon Tatham's linked list merge sort algorithm
+	// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
+	function sortLinked( list ) {
 
-		this.bumpMap = source.bumpMap;
-		this.bumpScale = source.bumpScale;
+		var i, p, q, e, tail, numMerges, pSize, qSize,
+			inSize = 1;
 
-		this.normalMap = source.normalMap;
-		this.normalMapType = source.normalMapType;
-		this.normalScale.copy( source.normalScale );
+		do {
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+			p = list;
+			list = null;
+			tail = null;
+			numMerges = 0;
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
+			while ( p ) {
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+				numMerges ++;
+				q = p;
+				pSize = 0;
+				for ( i = 0; i < inSize; i ++ ) {
 
-		return this;
+					pSize ++;
+					q = q.nextZ;
+					if ( ! q ) { break; }
 
-	};
+				}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  lightMap: new THREE.Texture( <Image> ),
-	 *  lightMapIntensity: <float>
-	 *
-	 *  aoMap: new THREE.Texture( <Image> ),
-	 *  aoMapIntensity: <float>
-	 *
-	 *  emissive: <hex>,
-	 *  emissiveIntensity: <float>
-	 *  emissiveMap: new THREE.Texture( <Image> ),
-	 *
-	 *  specularMap: new THREE.Texture( <Image> ),
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
-	 *  combine: THREE.Multiply,
-	 *  reflectivity: <float>,
-	 *  refractionRatio: <float>,
-	 *
-	 *  wireframe: <boolean>,
-	 *  wireframeLinewidth: <float>,
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+				qSize = inSize;
 
-	function MeshLambertMaterial( parameters ) {
+				while ( pSize > 0 || ( qSize > 0 && q ) ) {
 
-		Material.call( this );
+					if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) {
 
-		this.type = 'MeshLambertMaterial';
+						e = p;
+						p = p.nextZ;
+						pSize --;
 
-		this.color = new Color( 0xffffff ); // diffuse
+					} else {
 
-		this.map = null;
+						e = q;
+						q = q.nextZ;
+						qSize --;
 
-		this.lightMap = null;
-		this.lightMapIntensity = 1.0;
+					}
 
-		this.aoMap = null;
-		this.aoMapIntensity = 1.0;
+					if ( tail ) { tail.nextZ = e; }
+					else { list = e; }
 
-		this.emissive = new Color( 0x000000 );
-		this.emissiveIntensity = 1.0;
-		this.emissiveMap = null;
+					e.prevZ = tail;
+					tail = e;
 
-		this.specularMap = null;
+				}
 
-		this.alphaMap = null;
+				p = q;
 
-		this.envMap = null;
-		this.combine = MultiplyOperation;
-		this.reflectivity = 1;
-		this.refractionRatio = 0.98;
+			}
 
-		this.wireframe = false;
-		this.wireframeLinewidth = 1;
-		this.wireframeLinecap = 'round';
-		this.wireframeLinejoin = 'round';
+			tail.nextZ = null;
+			inSize *= 2;
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+		} while ( numMerges > 1 );
 
-		this.setValues( parameters );
+		return list;
 
 	}
 
-	MeshLambertMaterial.prototype = Object.create( Material.prototype );
-	MeshLambertMaterial.prototype.constructor = MeshLambertMaterial;
-
-	MeshLambertMaterial.prototype.isMeshLambertMaterial = true;
+	// z-order of a point given coords and inverse of the longer side of data bbox
+	function zOrder( x, y, minX, minY, invSize ) {
 
-	MeshLambertMaterial.prototype.copy = function ( source ) {
+		// coords are transformed into non-negative 15-bit integer range
+		x = 32767 * ( x - minX ) * invSize;
+		y = 32767 * ( y - minY ) * invSize;
 
-		Material.prototype.copy.call( this, source );
+		x = ( x | ( x << 8 ) ) & 0x00FF00FF;
+		x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
+		x = ( x | ( x << 2 ) ) & 0x33333333;
+		x = ( x | ( x << 1 ) ) & 0x55555555;
 
-		this.color.copy( source.color );
+		y = ( y | ( y << 8 ) ) & 0x00FF00FF;
+		y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
+		y = ( y | ( y << 2 ) ) & 0x33333333;
+		y = ( y | ( y << 1 ) ) & 0x55555555;
 
-		this.map = source.map;
+		return x | ( y << 1 );
 
-		this.lightMap = source.lightMap;
-		this.lightMapIntensity = source.lightMapIntensity;
+	}
 
-		this.aoMap = source.aoMap;
-		this.aoMapIntensity = source.aoMapIntensity;
+	// find the leftmost node of a polygon ring
+	function getLeftmost( start ) {
 
-		this.emissive.copy( source.emissive );
-		this.emissiveMap = source.emissiveMap;
-		this.emissiveIntensity = source.emissiveIntensity;
+		var p = start,
+			leftmost = start;
+		do {
 
-		this.specularMap = source.specularMap;
+			if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) { leftmost = p; }
+			p = p.next;
 
-		this.alphaMap = source.alphaMap;
+		} while ( p !== start );
 
-		this.envMap = source.envMap;
-		this.combine = source.combine;
-		this.reflectivity = source.reflectivity;
-		this.refractionRatio = source.refractionRatio;
+		return leftmost;
 
-		this.wireframe = source.wireframe;
-		this.wireframeLinewidth = source.wireframeLinewidth;
-		this.wireframeLinecap = source.wireframeLinecap;
-		this.wireframeLinejoin = source.wireframeLinejoin;
+	}
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+	// check if a point lies within a convex triangle
+	function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) {
 
-		return this;
+		return ( cx - px ) * ( ay - py ) - ( ax - px ) * ( cy - py ) >= 0 &&
+				( ax - px ) * ( by - py ) - ( bx - px ) * ( ay - py ) >= 0 &&
+				( bx - px ) * ( cy - py ) - ( cx - px ) * ( by - py ) >= 0;
 
-	};
+	}
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *
-	 *  matcap: new THREE.Texture( <Image> ),
-	 *
-	 *  map: new THREE.Texture( <Image> ),
-	 *
-	 *  bumpMap: new THREE.Texture( <Image> ),
-	 *  bumpScale: <float>,
-	 *
-	 *  normalMap: new THREE.Texture( <Image> ),
-	 *  normalMapType: THREE.TangentSpaceNormalMap,
-	 *  normalScale: <Vector2>,
-	 *
-	 *  displacementMap: new THREE.Texture( <Image> ),
-	 *  displacementScale: <float>,
-	 *  displacementBias: <float>,
-	 *
-	 *  alphaMap: new THREE.Texture( <Image> ),
-	 *
-	 *  skinning: <bool>,
-	 *  morphTargets: <bool>,
-	 *  morphNormals: <bool>
-	 * }
-	 */
+	// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
+	function isValidDiagonal( a, b ) {
 
-	function MeshMatcapMaterial( parameters ) {
+		return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // dones't intersect other edges
+			( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible
+			( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors
+			equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case
 
-		Material.call( this );
+	}
 
-		this.defines = { 'MATCAP': '' };
+	// signed area of a triangle
+	function area( p, q, r ) {
 
-		this.type = 'MeshMatcapMaterial';
+		return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y );
 
-		this.color = new Color( 0xffffff ); // diffuse
+	}
 
-		this.matcap = null;
+	// check if two points are equal
+	function equals( p1, p2 ) {
 
-		this.map = null;
+		return p1.x === p2.x && p1.y === p2.y;
 
-		this.bumpMap = null;
-		this.bumpScale = 1;
+	}
 
-		this.normalMap = null;
-		this.normalMapType = TangentSpaceNormalMap;
-		this.normalScale = new Vector2( 1, 1 );
+	// check if two segments intersect
+	function intersects( p1, q1, p2, q2 ) {
 
-		this.displacementMap = null;
-		this.displacementScale = 1;
-		this.displacementBias = 0;
+		var o1 = sign( area( p1, q1, p2 ) );
+		var o2 = sign( area( p1, q1, q2 ) );
+		var o3 = sign( area( p2, q2, p1 ) );
+		var o4 = sign( area( p2, q2, q1 ) );
 
-		this.alphaMap = null;
+		if ( o1 !== o2 && o3 !== o4 ) { return true; } // general case
 
-		this.skinning = false;
-		this.morphTargets = false;
-		this.morphNormals = false;
+		if ( o1 === 0 && onSegment( p1, p2, q1 ) ) { return true; } // p1, q1 and p2 are collinear and p2 lies on p1q1
+		if ( o2 === 0 && onSegment( p1, q2, q1 ) ) { return true; } // p1, q1 and q2 are collinear and q2 lies on p1q1
+		if ( o3 === 0 && onSegment( p2, p1, q2 ) ) { return true; } // p2, q2 and p1 are collinear and p1 lies on p2q2
+		if ( o4 === 0 && onSegment( p2, q1, q2 ) ) { return true; } // p2, q2 and q1 are collinear and q1 lies on p2q2
 
-		this.setValues( parameters );
+		return false;
 
 	}
 
-	MeshMatcapMaterial.prototype = Object.create( Material.prototype );
-	MeshMatcapMaterial.prototype.constructor = MeshMatcapMaterial;
-
-	MeshMatcapMaterial.prototype.isMeshMatcapMaterial = true;
+	// for collinear points p, q, r, check if point q lies on segment pr
+	function onSegment( p, q, r ) {
 
-	MeshMatcapMaterial.prototype.copy = function ( source ) {
+		return q.x <= Math.max( p.x, r.x ) && q.x >= Math.min( p.x, r.x ) && q.y <= Math.max( p.y, r.y ) && q.y >= Math.min( p.y, r.y );
 
-		Material.prototype.copy.call( this, source );
+	}
 
-		this.defines = { 'MATCAP': '' };
+	function sign( num ) {
 
-		this.color.copy( source.color );
+		return num > 0 ? 1 : num < 0 ? - 1 : 0;
 
-		this.matcap = source.matcap;
+	}
 
-		this.map = source.map;
+	// check if a polygon diagonal intersects any polygon segments
+	function intersectsPolygon( a, b ) {
 
-		this.bumpMap = source.bumpMap;
-		this.bumpScale = source.bumpScale;
+		var p = a;
+		do {
 
-		this.normalMap = source.normalMap;
-		this.normalMapType = source.normalMapType;
-		this.normalScale.copy( source.normalScale );
+			if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
+					intersects( p, p.next, a, b ) ) { return true; }
+			p = p.next;
 
-		this.displacementMap = source.displacementMap;
-		this.displacementScale = source.displacementScale;
-		this.displacementBias = source.displacementBias;
+		} while ( p !== a );
 
-		this.alphaMap = source.alphaMap;
+		return false;
 
-		this.skinning = source.skinning;
-		this.morphTargets = source.morphTargets;
-		this.morphNormals = source.morphNormals;
+	}
 
-		return this;
+	// check if a polygon diagonal is locally inside the polygon
+	function locallyInside( a, b ) {
 
-	};
+		return area( a.prev, a, a.next ) < 0 ?
+			area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 :
+			area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0;
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 *
-	 * parameters = {
-	 *  color: <hex>,
-	 *  opacity: <float>,
-	 *
-	 *  linewidth: <float>,
-	 *
-	 *  scale: <float>,
-	 *  dashSize: <float>,
-	 *  gapSize: <float>
-	 * }
-	 */
+	}
 
-	function LineDashedMaterial( parameters ) {
+	// check if the middle point of a polygon diagonal is inside the polygon
+	function middleInside( a, b ) {
 
-		LineBasicMaterial.call( this );
+		var p = a,
+			inside = false,
+			px = ( a.x + b.x ) / 2,
+			py = ( a.y + b.y ) / 2;
+		do {
 
-		this.type = 'LineDashedMaterial';
+			if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y &&
+					( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) )
+				{ inside = ! inside; }
+			p = p.next;
 
-		this.scale = 1;
-		this.dashSize = 3;
-		this.gapSize = 1;
+		} while ( p !== a );
 
-		this.setValues( parameters );
+		return inside;
 
 	}
 
-	LineDashedMaterial.prototype = Object.create( LineBasicMaterial.prototype );
-	LineDashedMaterial.prototype.constructor = LineDashedMaterial;
+	// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
+	// if one belongs to the outer ring and another to a hole, it merges it into a single ring
+	function splitPolygon( a, b ) {
 
-	LineDashedMaterial.prototype.isLineDashedMaterial = true;
+		var a2 = new Node( a.i, a.x, a.y ),
+			b2 = new Node( b.i, b.x, b.y ),
+			an = a.next,
+			bp = b.prev;
 
-	LineDashedMaterial.prototype.copy = function ( source ) {
+		a.next = b;
+		b.prev = a;
 
-		LineBasicMaterial.prototype.copy.call( this, source );
+		a2.next = an;
+		an.prev = a2;
 
-		this.scale = source.scale;
-		this.dashSize = source.dashSize;
-		this.gapSize = source.gapSize;
+		b2.next = a2;
+		a2.prev = b2;
 
-		return this;
+		bp.next = b2;
+		b2.prev = bp;
 
-	};
+		return b2;
 
-	var Materials = /*#__PURE__*/Object.freeze({
-		__proto__: null,
-		ShadowMaterial: ShadowMaterial,
-		SpriteMaterial: SpriteMaterial,
-		RawShaderMaterial: RawShaderMaterial,
-		ShaderMaterial: ShaderMaterial,
-		PointsMaterial: PointsMaterial,
-		MeshPhysicalMaterial: MeshPhysicalMaterial,
-		MeshStandardMaterial: MeshStandardMaterial,
-		MeshPhongMaterial: MeshPhongMaterial,
-		MeshToonMaterial: MeshToonMaterial,
-		MeshNormalMaterial: MeshNormalMaterial,
-		MeshLambertMaterial: MeshLambertMaterial,
-		MeshDepthMaterial: MeshDepthMaterial,
-		MeshDistanceMaterial: MeshDistanceMaterial,
-		MeshBasicMaterial: MeshBasicMaterial,
-		MeshMatcapMaterial: MeshMatcapMaterial,
-		LineDashedMaterial: LineDashedMaterial,
-		LineBasicMaterial: LineBasicMaterial,
-		Material: Material
-	});
+	}
 
-	/**
-	 * @author tschw
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 */
+	// create a node and optionally link it with previous one (in a circular doubly linked list)
+	function insertNode( i, x, y, last ) {
 
-	var AnimationUtils = {
+		var p = new Node( i, x, y );
 
-		// same as Array.prototype.slice, but also works on typed arrays
-		arraySlice: function ( array, from, to ) {
+		if ( ! last ) {
 
-			if ( AnimationUtils.isTypedArray( array ) ) {
+			p.prev = p;
+			p.next = p;
 
-				// in ios9 array.subarray(from, undefined) will return empty array
-				// but array.subarray(from) or array.subarray(from, len) is correct
-				return new array.constructor( array.subarray( from, to !== undefined ? to : array.length ) );
+		} else {
 
-			}
+			p.next = last.next;
+			p.prev = last;
+			last.next.prev = p;
+			last.next = p;
 
-			return array.slice( from, to );
+		}
 
-		},
+		return p;
 
-		// converts an array to a specific type
-		convertArray: function ( array, type, forceClone ) {
+	}
 
-			if ( ! array || // let 'undefined' and 'null' pass
-				! forceClone && array.constructor === type ) { return array; }
+	function removeNode( p ) {
 
-			if ( typeof type.BYTES_PER_ELEMENT === 'number' ) {
+		p.next.prev = p.prev;
+		p.prev.next = p.next;
 
-				return new type( array ); // create typed array
+		if ( p.prevZ ) { p.prevZ.nextZ = p.nextZ; }
+		if ( p.nextZ ) { p.nextZ.prevZ = p.prevZ; }
 
-			}
+	}
 
-			return Array.prototype.slice.call( array ); // create Array
+	function Node( i, x, y ) {
 
-		},
+		// vertex index in coordinates array
+		this.i = i;
 
-		isTypedArray: function ( object ) {
+		// vertex coordinates
+		this.x = x;
+		this.y = y;
 
-			return ArrayBuffer.isView( object ) &&
-				! ( object instanceof DataView );
+		// previous and next vertex nodes in a polygon ring
+		this.prev = null;
+		this.next = null;
 
-		},
+		// z-order curve value
+		this.z = null;
 
-		// returns an array by which times and values can be sorted
-		getKeyframeOrder: function ( times ) {
+		// previous and next nodes in z-order
+		this.prevZ = null;
+		this.nextZ = null;
 
-			function compareTime( i, j ) {
+		// indicates whether this is a steiner point
+		this.steiner = false;
 
-				return times[ i ] - times[ j ];
+	}
 
-			}
+	function signedArea( data, start, end, dim ) {
 
-			var n = times.length;
-			var result = new Array( n );
-			for ( var i = 0; i !== n; ++ i ) { result[ i ] = i; }
+		var sum = 0;
+		for ( var i = start, j = end - dim; i < end; i += dim ) {
 
-			result.sort( compareTime );
+			sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] );
+			j = i;
 
-			return result;
+		}
 
-		},
+		return sum;
 
-		// uses the array previously returned by 'getKeyframeOrder' to sort data
-		sortedArray: function ( values, stride, order ) {
+	}
 
-			var nValues = values.length;
-			var result = new values.constructor( nValues );
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 */
 
-			for ( var i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) {
+	var ShapeUtils = {
 
-				var srcOffset = order[ i ] * stride;
+		// calculate area of the contour polygon
 
-				for ( var j = 0; j !== stride; ++ j ) {
+		area: function ( contour ) {
 
-					result[ dstOffset ++ ] = values[ srcOffset + j ];
+			var n = contour.length;
+			var a = 0.0;
 
-				}
+			for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
+
+				a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
 
 			}
 
-			return result;
+			return a * 0.5;
 
 		},
 
-		// function for parsing AOS keyframe formats
-		flattenJSON: function ( jsonKeys, times, values, valuePropertyName ) {
-
-			var i = 1, key = jsonKeys[ 0 ];
+		isClockWise: function ( pts ) {
 
-			while ( key !== undefined && key[ valuePropertyName ] === undefined ) {
+			return ShapeUtils.area( pts ) < 0;
 
-				key = jsonKeys[ i ++ ];
+		},
 
-			}
+		triangulateShape: function ( contour, holes ) {
 
-			if ( key === undefined ) { return; } // no data
+			var vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
+			var holeIndices = []; // array of hole indices
+			var faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
 
-			var value = key[ valuePropertyName ];
-			if ( value === undefined ) { return; } // no data
+			removeDupEndPts( contour );
+			addContour( vertices, contour );
 
-			if ( Array.isArray( value ) ) {
+			//
 
-				do {
+			var holeIndex = contour.length;
 
-					value = key[ valuePropertyName ];
+			holes.forEach( removeDupEndPts );
 
-					if ( value !== undefined ) {
+			for ( var i = 0; i < holes.length; i ++ ) {
 
-						times.push( key.time );
-						values.push.apply( values, value ); // push all elements
+				holeIndices.push( holeIndex );
+				holeIndex += holes[ i ].length;
+				addContour( vertices, holes[ i ] );
 
-					}
+			}
 
-					key = jsonKeys[ i ++ ];
+			//
 
-				} while ( key !== undefined );
+			var triangles = Earcut.triangulate( vertices, holeIndices );
 
-			} else if ( value.toArray !== undefined ) {
+			//
 
-				// ...assume THREE.Math-ish
+			for ( var i = 0; i < triangles.length; i += 3 ) {
 
-				do {
+				faces.push( triangles.slice( i, i + 3 ) );
 
-					value = key[ valuePropertyName ];
+			}
 
-					if ( value !== undefined ) {
+			return faces;
 
-						times.push( key.time );
-						value.toArray( values, values.length );
+		}
 
-					}
+	};
 
-					key = jsonKeys[ i ++ ];
+	function removeDupEndPts( points ) {
 
-				} while ( key !== undefined );
+		var l = points.length;
 
-			} else {
+		if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
 
-				// otherwise push as-is
+			points.pop();
 
-				do {
+		}
 
-					value = key[ valuePropertyName ];
+	}
 
-					if ( value !== undefined ) {
+	function addContour( vertices, contour ) {
 
-						times.push( key.time );
-						values.push( value );
+		for ( var i = 0; i < contour.length; i ++ ) {
 
-					}
+			vertices.push( contour[ i ].x );
+			vertices.push( contour[ i ].y );
 
-					key = jsonKeys[ i ++ ];
+		}
 
-				} while ( key !== undefined );
+	}
 
-			}
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 *
+	 * Creates extruded geometry from a path shape.
+	 *
+	 * parameters = {
+	 *
+	 *  curveSegments: <int>, // number of points on the curves
+	 *  steps: <int>, // number of points for z-side extrusions / used for subdividing segments of extrude spline too
+	 *  depth: <float>, // Depth to extrude the shape
+	 *
+	 *  bevelEnabled: <bool>, // turn on bevel
+	 *  bevelThickness: <float>, // how deep into the original shape bevel goes
+	 *  bevelSize: <float>, // how far from shape outline (including bevelOffset) is bevel
+	 *  bevelOffset: <float>, // how far from shape outline does bevel start
+	 *  bevelSegments: <int>, // number of bevel layers
+	 *
+	 *  extrudePath: <THREE.Curve> // curve to extrude shape along
+	 *
+	 *  UVGenerator: <Object> // object that provides UV generator functions
+	 *
+	 * }
+	 */
 
-		},
+	// ExtrudeGeometry
 
-		subclip: function ( sourceClip, name, startFrame, endFrame, fps ) {
+	function ExtrudeGeometry( shapes, options ) {
 
-			fps = fps || 30;
+		Geometry.call( this );
 
-			var clip = sourceClip.clone();
+		this.type = 'ExtrudeGeometry';
 
-			clip.name = name;
+		this.parameters = {
+			shapes: shapes,
+			options: options
+		};
 
-			var tracks = [];
+		this.fromBufferGeometry( new ExtrudeBufferGeometry( shapes, options ) );
+		this.mergeVertices();
 
-			for ( var i = 0; i < clip.tracks.length; ++ i ) {
+	}
 
-				var track = clip.tracks[ i ];
-				var valueSize = track.getValueSize();
+	ExtrudeGeometry.prototype = Object.create( Geometry.prototype );
+	ExtrudeGeometry.prototype.constructor = ExtrudeGeometry;
 
-				var times = [];
-				var values = [];
+	ExtrudeGeometry.prototype.toJSON = function () {
 
-				for ( var j = 0; j < track.times.length; ++ j ) {
+		var data = Geometry.prototype.toJSON.call( this );
 
-					var frame = track.times[ j ] * fps;
+		var shapes = this.parameters.shapes;
+		var options = this.parameters.options;
 
-					if ( frame < startFrame || frame >= endFrame ) { continue; }
+		return toJSON( shapes, options, data );
 
-					times.push( track.times[ j ] );
+	};
 
-					for ( var k = 0; k < valueSize; ++ k ) {
+	// ExtrudeBufferGeometry
 
-						values.push( track.values[ j * valueSize + k ] );
+	function ExtrudeBufferGeometry( shapes, options ) {
 
-					}
+		BufferGeometry.call( this );
 
-				}
+		this.type = 'ExtrudeBufferGeometry';
 
-				if ( times.length === 0 ) { continue; }
+		this.parameters = {
+			shapes: shapes,
+			options: options
+		};
 
-				track.times = AnimationUtils.convertArray( times, track.times.constructor );
-				track.values = AnimationUtils.convertArray( values, track.values.constructor );
+		shapes = Array.isArray( shapes ) ? shapes : [ shapes ];
 
-				tracks.push( track );
+		var scope = this;
 
-			}
+		var verticesArray = [];
+		var uvArray = [];
 
-			clip.tracks = tracks;
+		for ( var i = 0, l = shapes.length; i < l; i ++ ) {
 
-			// find minimum .times value across all tracks in the trimmed clip
+			var shape = shapes[ i ];
+			addShape( shape );
 
-			var minStartTime = Infinity;
+		}
 
-			for ( var i = 0; i < clip.tracks.length; ++ i ) {
+		// build geometry
 
-				if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) {
+		this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) );
 
-					minStartTime = clip.tracks[ i ].times[ 0 ];
+		this.computeVertexNormals();
 
-				}
+		// functions
 
-			}
+		function addShape( shape ) {
 
-			// shift all tracks such that clip begins at t=0
+			var placeholder = [];
 
-			for ( var i = 0; i < clip.tracks.length; ++ i ) {
+			// options
 
-				clip.tracks[ i ].shift( - 1 * minStartTime );
+			var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
+			var steps = options.steps !== undefined ? options.steps : 1;
+			var depth = options.depth !== undefined ? options.depth : 100;
 
-			}
+			var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true;
+			var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6;
+			var bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2;
+			var bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0;
+			var bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
 
-			clip.resetDuration();
+			var extrudePath = options.extrudePath;
 
-			return clip;
+			var uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator;
 
-		},
+			// deprecated options
 
-		makeClipAdditive: function ( targetClip, referenceFrame, referenceClip, fps ) {
+			if ( options.amount !== undefined ) {
 
-			if ( referenceFrame === undefined ) { referenceFrame = 0; }
-			if ( referenceClip === undefined ) { referenceClip = targetClip; }
-			if ( fps === undefined || fps <= 0 ) { fps = 30; }
+				console.warn( 'THREE.ExtrudeBufferGeometry: amount has been renamed to depth.' );
+				depth = options.amount;
 
-			var numTracks = targetClip.tracks.length;
-			var referenceTime = referenceFrame / fps;
+			}
 
-			// Make each track's values relative to the values at the reference frame
-			for ( var i = 0; i < numTracks; ++ i ) {
+			//
 
-				var referenceTrack = referenceClip.tracks[ i ];
-				var referenceTrackType = referenceTrack.ValueTypeName;
+			var extrudePts, extrudeByPath = false;
+			var splineTube, binormal, normal, position2;
 
-				// Skip this track if it's non-numeric
-				if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) { continue; }
+			if ( extrudePath ) {
 
-				// Find the track in the target clip whose name and type matches the reference track
-				var targetTrack = targetClip.tracks.find( function ( track ) {
+				extrudePts = extrudePath.getSpacedPoints( steps );
 
-					return track.name === referenceTrack.name
-					&& track.ValueTypeName === referenceTrackType;
+				extrudeByPath = true;
+				bevelEnabled = false; // bevels not supported for path extrusion
 
-				} );
+				// SETUP TNB variables
 
-				if ( targetTrack === undefined ) { continue; }
+				// TODO1 - have a .isClosed in spline?
 
-				var valueSize = referenceTrack.getValueSize();
-				var lastIndex = referenceTrack.times.length - 1;
-				var referenceValue;
+				splineTube = extrudePath.computeFrenetFrames( steps, false );
 
-				// Find the value to subtract out of the track
-				if ( referenceTime <= referenceTrack.times[ 0 ] ) {
+				// console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
 
-					// Reference frame is earlier than the first keyframe, so just use the first keyframe
-					referenceValue = AnimationUtils.arraySlice( referenceTrack.values, 0, referenceTrack.valueSize );
+				binormal = new Vector3();
+				normal = new Vector3();
+				position2 = new Vector3();
 
-				} else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) {
+			}
 
-					// Reference frame is after the last keyframe, so just use the last keyframe
-					var startIndex = lastIndex * valueSize;
-					referenceValue = AnimationUtils.arraySlice( referenceTrack.values, startIndex );
+			// Safeguards if bevels are not enabled
 
-				} else {
+			if ( ! bevelEnabled ) {
 
-					// Interpolate to the reference value
-					var interpolant = referenceTrack.createInterpolant();
-					interpolant.evaluate( referenceTime );
-					referenceValue = interpolant.resultBuffer;
+				bevelSegments = 0;
+				bevelThickness = 0;
+				bevelSize = 0;
+				bevelOffset = 0;
 
-				}
+			}
 
-				// Conjugate the quaternion
-				if ( referenceTrackType === 'quaternion' ) {
+			// Variables initialization
 
-					var referenceQuat = new Quaternion(
-						referenceValue[ 0 ],
-						referenceValue[ 1 ],
-						referenceValue[ 2 ],
-						referenceValue[ 3 ]
-					).normalize().conjugate();
-					referenceQuat.toArray( referenceValue );
+			var ahole, h, hl; // looping of holes
 
-				}
+			var shapePoints = shape.extractPoints( curveSegments );
 
-				// Subtract the reference value from all of the track values
+			var vertices = shapePoints.shape;
+			var holes = shapePoints.holes;
 
-				var numTimes = targetTrack.times.length;
-				for ( var j = 0; j < numTimes; ++ j ) {
+			var reverse = ! ShapeUtils.isClockWise( vertices );
 
-					var valueStart = j * valueSize;
+			if ( reverse ) {
 
-					if ( referenceTrackType === 'quaternion' ) {
+				vertices = vertices.reverse();
 
-						// Multiply the conjugate for quaternion track types
-						Quaternion.multiplyQuaternionsFlat(
-							targetTrack.values,
-							valueStart,
-							referenceValue,
-							0,
-							targetTrack.values,
-							valueStart
-						);
+				// Maybe we should also check if holes are in the opposite direction, just to be safe ...
 
-					} else {
+				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-						// Subtract each value for all other numeric track types
-						for ( var k = 0; k < valueSize; ++ k ) {
+					ahole = holes[ h ];
 
-							targetTrack.values[ valueStart + k ] -= referenceValue[ k ];
+					if ( ShapeUtils.isClockWise( ahole ) ) {
 
-						}
+						holes[ h ] = ahole.reverse();
 
 					}
 
@@ -42600,4756 +49770,5257 @@ function simpleEnd(buf) {
 
 			}
 
-			targetClip.blendMode = AdditiveAnimationBlendMode;
 
-			return targetClip;
+			var faces = ShapeUtils.triangulateShape( vertices, holes );
 
-		}
+			/* Vertices */
 
-	};
+			var contour = vertices; // vertices has all points but contour has only points of circumference
 
-	/**
-	 * Abstract base class of interpolants over parametric samples.
-	 *
-	 * The parameter domain is one dimensional, typically the time or a path
-	 * along a curve defined by the data.
-	 *
-	 * The sample values can have any dimensionality and derived classes may
-	 * apply special interpretations to the data.
-	 *
-	 * This class provides the interval seek in a Template Method, deferring
-	 * the actual interpolation to derived classes.
-	 *
-	 * Time complexity is O(1) for linear access crossing at most two points
-	 * and O(log N) for random access, where N is the number of positions.
-	 *
-	 * References:
-	 *
-	 * 		http://www.oodesign.com/template-method-pattern.html
-	 *
-	 * @author tschw
-	 */
+			for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-	function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+				ahole = holes[ h ];
 
-		this.parameterPositions = parameterPositions;
-		this._cachedIndex = 0;
+				vertices = vertices.concat( ahole );
 
-		this.resultBuffer = resultBuffer !== undefined ?
-			resultBuffer : new sampleValues.constructor( sampleSize );
-		this.sampleValues = sampleValues;
-		this.valueSize = sampleSize;
+			}
 
-	}
 
-	Object.assign( Interpolant.prototype, {
+			function scalePt2( pt, vec, size ) {
 
-		evaluate: function ( t ) {
+				if ( ! vec ) { console.error( "THREE.ExtrudeGeometry: vec does not exist" ); }
 
-			var pp = this.parameterPositions,
-				i1 = this._cachedIndex,
+				return vec.clone().multiplyScalar( size ).add( pt );
 
-				t1 = pp[ i1 ],
-				t0 = pp[ i1 - 1 ];
+			}
 
-			validate_interval: {
+			var b, bs, t, z,
+				vert, vlen = vertices.length,
+				face, flen = faces.length;
 
-				seek: {
 
-					var right;
+			// Find directions for point movement
 
-					linear_scan: {
 
-						//- See http://jsperf.com/comparison-to-undefined/3
-						//- slower code:
-						//-
-						//- 				if ( t >= t1 || t1 === undefined ) {
-						forward_scan: if ( ! ( t < t1 ) ) {
+			function getBevelVec( inPt, inPrev, inNext ) {
 
-							for ( var giveUpAt = i1 + 2; ; ) {
+				// computes for inPt the corresponding point inPt' on a new contour
+				//   shifted by 1 unit (length of normalized vector) to the left
+				// if we walk along contour clockwise, this new contour is outside the old one
+				//
+				// inPt' is the intersection of the two lines parallel to the two
+				//  adjacent edges of inPt at a distance of 1 unit on the left side.
 
-								if ( t1 === undefined ) {
+				var v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt
 
-									if ( t < t0 ) { break forward_scan; }
+				// good reading for geometry algorithms (here: line-line intersection)
+				// http://geomalgorithms.com/a05-_intersect-1.html
 
-									// after end
+				var v_prev_x = inPt.x - inPrev.x,
+					v_prev_y = inPt.y - inPrev.y;
+				var v_next_x = inNext.x - inPt.x,
+					v_next_y = inNext.y - inPt.y;
 
-									i1 = pp.length;
-									this._cachedIndex = i1;
-									return this.afterEnd_( i1 - 1, t, t0 );
+				var v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
 
-								}
+				// check for collinear edges
+				var collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
 
-								if ( i1 === giveUpAt ) { break; } // this loop
+				if ( Math.abs( collinear0 ) > Number.EPSILON ) {
 
-								t0 = t1;
-								t1 = pp[ ++ i1 ];
+					// not collinear
 
-								if ( t < t1 ) {
+					// length of vectors for normalizing
 
-									// we have arrived at the sought interval
-									break seek;
+					var v_prev_len = Math.sqrt( v_prev_lensq );
+					var v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
 
-								}
+					// shift adjacent points by unit vectors to the left
 
-							}
+					var ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
+					var ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
 
-							// prepare binary search on the right side of the index
-							right = pp.length;
-							break linear_scan;
+					var ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
+					var ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
 
-						}
+					// scaling factor for v_prev to intersection point
 
-						//- slower code:
-						//-					if ( t < t0 || t0 === undefined ) {
-						if ( ! ( t >= t0 ) ) {
+					var sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
+							( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
+						( v_prev_x * v_next_y - v_prev_y * v_next_x );
 
-							// looping?
+					// vector from inPt to intersection point
 
-							var t1global = pp[ 1 ];
+					v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
+					v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
 
-							if ( t < t1global ) {
+					// Don't normalize!, otherwise sharp corners become ugly
+					//  but prevent crazy spikes
+					var v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y );
+					if ( v_trans_lensq <= 2 ) {
 
-								i1 = 2; // + 1, using the scan for the details
-								t0 = t1global;
+						return new Vector2( v_trans_x, v_trans_y );
 
-							}
+					} else {
 
-							// linear reverse scan
+						shrink_by = Math.sqrt( v_trans_lensq / 2 );
 
-							for ( var giveUpAt = i1 - 2; ; ) {
+					}
 
-								if ( t0 === undefined ) {
+				} else {
 
-									// before start
+					// handle special case of collinear edges
 
-									this._cachedIndex = 0;
-									return this.beforeStart_( 0, t, t1 );
+					var direction_eq = false; // assumes: opposite
+					if ( v_prev_x > Number.EPSILON ) {
 
-								}
+						if ( v_next_x > Number.EPSILON ) {
 
-								if ( i1 === giveUpAt ) { break; } // this loop
+							direction_eq = true;
 
-								t1 = t0;
-								t0 = pp[ -- i1 - 1 ];
+						}
 
-								if ( t >= t0 ) {
+					} else {
 
-									// we have arrived at the sought interval
-									break seek;
+						if ( v_prev_x < - Number.EPSILON ) {
 
-								}
+							if ( v_next_x < - Number.EPSILON ) {
+
+								direction_eq = true;
 
 							}
 
-							// prepare binary search on the left side of the index
-							right = i1;
-							i1 = 0;
-							break linear_scan;
-
-						}
+						} else {
 
-						// the interval is valid
+							if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) {
 
-						break validate_interval;
+								direction_eq = true;
 
-					} // linear scan
+							}
 
-					// binary search
+						}
 
-					while ( i1 < right ) {
+					}
 
-						var mid = ( i1 + right ) >>> 1;
+					if ( direction_eq ) {
 
-						if ( t < pp[ mid ] ) {
+						// console.log("Warning: lines are a straight sequence");
+						v_trans_x = - v_prev_y;
+						v_trans_y = v_prev_x;
+						shrink_by = Math.sqrt( v_prev_lensq );
 
-							right = mid;
+					} else {
 
-						} else {
+						// console.log("Warning: lines are a straight spike");
+						v_trans_x = v_prev_x;
+						v_trans_y = v_prev_y;
+						shrink_by = Math.sqrt( v_prev_lensq / 2 );
 
-							i1 = mid + 1;
+					}
 
-						}
+				}
 
-					}
+				return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
 
-					t1 = pp[ i1 ];
-					t0 = pp[ i1 - 1 ];
+			}
 
-					// check boundary cases, again
 
-					if ( t0 === undefined ) {
+			var contourMovements = [];
 
-						this._cachedIndex = 0;
-						return this.beforeStart_( 0, t, t1 );
+			for ( var i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
 
-					}
+				if ( j === il ) { j = 0; }
+				if ( k === il ) { k = 0; }
 
-					if ( t1 === undefined ) {
+				//  (j)---(i)---(k)
+				// console.log('i,j,k', i, j , k)
 
-						i1 = pp.length;
-						this._cachedIndex = i1;
-						return this.afterEnd_( i1 - 1, t0, t );
+				contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
 
-					}
+			}
 
-				} // seek
+			var holesMovements = [],
+				oneHoleMovements, verticesMovements = contourMovements.concat();
 
-				this._cachedIndex = i1;
+			for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-				this.intervalChanged_( i1, t0, t1 );
+				ahole = holes[ h ];
 
-			} // validate_interval
+				oneHoleMovements = [];
 
-			return this.interpolate_( i1, t0, t, t1 );
+				for ( i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
 
-		},
+					if ( j === il ) { j = 0; }
+					if ( k === il ) { k = 0; }
 
-		settings: null, // optional, subclass-specific settings structure
-		// Note: The indirection allows central control of many interpolants.
+					//  (j)---(i)---(k)
+					oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
 
-		// --- Protected interface
+				}
 
-		DefaultSettings_: {},
+				holesMovements.push( oneHoleMovements );
+				verticesMovements = verticesMovements.concat( oneHoleMovements );
 
-		getSettings_: function () {
+			}
 
-			return this.settings || this.DefaultSettings_;
 
-		},
+			// Loop bevelSegments, 1 for the front, 1 for the back
 
-		copySampleValue_: function ( index ) {
+			for ( b = 0; b < bevelSegments; b ++ ) {
 
-			// copies a sample value to the result buffer
+				//for ( b = bevelSegments; b > 0; b -- ) {
 
-			var result = this.resultBuffer,
-				values = this.sampleValues,
-				stride = this.valueSize,
-				offset = index * stride;
+				t = b / bevelSegments;
+				z = bevelThickness * Math.cos( t * Math.PI / 2 );
+				bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
 
-			for ( var i = 0; i !== stride; ++ i ) {
+				// contract shape
 
-				result[ i ] = values[ offset + i ];
+				for ( i = 0, il = contour.length; i < il; i ++ ) {
 
-			}
+					vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
 
-			return result;
+					v( vert.x, vert.y, - z );
 
-		},
+				}
 
-		// Template methods for derived classes:
+				// expand holes
 
-		interpolate_: function ( /* i1, t0, t, t1 */ ) {
+				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-			throw new Error( 'call to abstract method' );
-			// implementations shall return this.resultBuffer
+					ahole = holes[ h ];
+					oneHoleMovements = holesMovements[ h ];
 
-		},
+					for ( i = 0, il = ahole.length; i < il; i ++ ) {
 
-		intervalChanged_: function ( /* i1, t0, t1 */ ) {
+						vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
 
-			// empty
+						v( vert.x, vert.y, - z );
 
-		}
+					}
 
-	} );
+				}
 
-	// DECLARE ALIAS AFTER assign prototype
-	Object.assign( Interpolant.prototype, {
+			}
 
-		//( 0, t, t0 ), returns this.resultBuffer
-		beforeStart_: Interpolant.prototype.copySampleValue_,
+			bs = bevelSize + bevelOffset;
 
-		//( N-1, tN-1, t ), returns this.resultBuffer
-		afterEnd_: Interpolant.prototype.copySampleValue_,
+			// Back facing vertices
 
-	} );
+			for ( i = 0; i < vlen; i ++ ) {
 
-	/**
-	 * Fast and simple cubic spline interpolant.
-	 *
-	 * It was derived from a Hermitian construction setting the first derivative
-	 * at each sample position to the linear slope between neighboring positions
-	 * over their parameter interval.
-	 *
-	 * @author tschw
-	 */
+				vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
 
-	function CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+				if ( ! extrudeByPath ) {
 
-		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+					v( vert.x, vert.y, 0 );
 
-		this._weightPrev = - 0;
-		this._offsetPrev = - 0;
-		this._weightNext = - 0;
-		this._offsetNext = - 0;
+				} else {
 
-	}
+					// v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
 
-	CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+					normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x );
+					binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y );
 
-		constructor: CubicInterpolant,
+					position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal );
 
-		DefaultSettings_: {
+					v( position2.x, position2.y, position2.z );
 
-			endingStart: ZeroCurvatureEnding,
-			endingEnd: ZeroCurvatureEnding
+				}
 
-		},
+			}
 
-		intervalChanged_: function ( i1, t0, t1 ) {
+			// Add stepped vertices...
+			// Including front facing vertices
 
-			var pp = this.parameterPositions,
-				iPrev = i1 - 2,
-				iNext = i1 + 1,
+			var s;
 
-				tPrev = pp[ iPrev ],
-				tNext = pp[ iNext ];
+			for ( s = 1; s <= steps; s ++ ) {
 
-			if ( tPrev === undefined ) {
+				for ( i = 0; i < vlen; i ++ ) {
 
-				switch ( this.getSettings_().endingStart ) {
+					vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
 
-					case ZeroSlopeEnding:
+					if ( ! extrudeByPath ) {
 
-						// f'(t0) = 0
-						iPrev = i1;
-						tPrev = 2 * t0 - t1;
+						v( vert.x, vert.y, depth / steps * s );
 
-						break;
+					} else {
 
-					case WrapAroundEnding:
+						// v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
 
-						// use the other end of the curve
-						iPrev = pp.length - 2;
-						tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ];
+						normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x );
+						binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y );
 
-						break;
+						position2.copy( extrudePts[ s ] ).add( normal ).add( binormal );
 
-					default: // ZeroCurvatureEnding
+						v( position2.x, position2.y, position2.z );
 
-						// f''(t0) = 0 a.k.a. Natural Spline
-						iPrev = i1;
-						tPrev = t1;
+					}
 
 				}
 
 			}
 
-			if ( tNext === undefined ) {
-
-				switch ( this.getSettings_().endingEnd ) {
 
-					case ZeroSlopeEnding:
+			// Add bevel segments planes
 
-						// f'(tN) = 0
-						iNext = i1;
-						tNext = 2 * t1 - t0;
+			//for ( b = 1; b <= bevelSegments; b ++ ) {
+			for ( b = bevelSegments - 1; b >= 0; b -- ) {
 
-						break;
+				t = b / bevelSegments;
+				z = bevelThickness * Math.cos( t * Math.PI / 2 );
+				bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
 
-					case WrapAroundEnding:
+				// contract shape
 
-						// use the other end of the curve
-						iNext = 1;
-						tNext = t1 + pp[ 1 ] - pp[ 0 ];
+				for ( i = 0, il = contour.length; i < il; i ++ ) {
 
-						break;
+					vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
+					v( vert.x, vert.y, depth + z );
 
-					default: // ZeroCurvatureEnding
+				}
 
-						// f''(tN) = 0, a.k.a. Natural Spline
-						iNext = i1 - 1;
-						tNext = t0;
+				// expand holes
 
-				}
+				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-			}
+					ahole = holes[ h ];
+					oneHoleMovements = holesMovements[ h ];
 
-			var halfDt = ( t1 - t0 ) * 0.5,
-				stride = this.valueSize;
+					for ( i = 0, il = ahole.length; i < il; i ++ ) {
 
-			this._weightPrev = halfDt / ( t0 - tPrev );
-			this._weightNext = halfDt / ( tNext - t1 );
-			this._offsetPrev = iPrev * stride;
-			this._offsetNext = iNext * stride;
+						vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
 
-		},
+						if ( ! extrudeByPath ) {
 
-		interpolate_: function ( i1, t0, t, t1 ) {
+							v( vert.x, vert.y, depth + z );
 
-			var result = this.resultBuffer,
-				values = this.sampleValues,
-				stride = this.valueSize,
+						} else {
 
-				o1 = i1 * stride,		o0 = o1 - stride,
-				oP = this._offsetPrev, 	oN = this._offsetNext,
-				wP = this._weightPrev,	wN = this._weightNext,
+							v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
 
-				p = ( t - t0 ) / ( t1 - t0 ),
-				pp = p * p,
-				ppp = pp * p;
+						}
 
-			// evaluate polynomials
+					}
 
-			var sP = - wP * ppp + 2 * wP * pp - wP * p;
-			var s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1;
-			var s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p;
-			var sN = wN * ppp - wN * pp;
+				}
 
-			// combine data linearly
+			}
 
-			for ( var i = 0; i !== stride; ++ i ) {
+			/* Faces */
 
-				result[ i ] =
-						sP * values[ oP + i ] +
-						s0 * values[ o0 + i ] +
-						s1 * values[ o1 + i ] +
-						sN * values[ oN + i ];
+			// Top and bottom faces
 
-			}
+			buildLidFaces();
 
-			return result;
+			// Sides faces
 
-		}
+			buildSideFaces();
 
-	} );
 
-	/**
-	 * @author tschw
-	 */
+			/////  Internal functions
 
-	function LinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+			function buildLidFaces() {
 
-		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+				var start = verticesArray.length / 3;
 
-	}
+				if ( bevelEnabled ) {
 
-	LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+					var layer = 0; // steps + 1
+					var offset = vlen * layer;
 
-		constructor: LinearInterpolant,
+					// Bottom faces
 
-		interpolate_: function ( i1, t0, t, t1 ) {
+					for ( i = 0; i < flen; i ++ ) {
 
-			var result = this.resultBuffer,
-				values = this.sampleValues,
-				stride = this.valueSize,
+						face = faces[ i ];
+						f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset );
 
-				offset1 = i1 * stride,
-				offset0 = offset1 - stride,
+					}
 
-				weight1 = ( t - t0 ) / ( t1 - t0 ),
-				weight0 = 1 - weight1;
+					layer = steps + bevelSegments * 2;
+					offset = vlen * layer;
 
-			for ( var i = 0; i !== stride; ++ i ) {
+					// Top faces
 
-				result[ i ] =
-						values[ offset0 + i ] * weight0 +
-						values[ offset1 + i ] * weight1;
+					for ( i = 0; i < flen; i ++ ) {
 
-			}
+						face = faces[ i ];
+						f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
 
-			return result;
+					}
 
-		}
+				} else {
 
-	} );
+					// Bottom faces
 
-	/**
-	 *
-	 * Interpolant that evaluates to the sample value at the position preceeding
-	 * the parameter.
-	 *
-	 * @author tschw
-	 */
+					for ( i = 0; i < flen; i ++ ) {
 
-	function DiscreteInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+						face = faces[ i ];
+						f3( face[ 2 ], face[ 1 ], face[ 0 ] );
 
-		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+					}
 
-	}
+					// Top faces
 
-	DiscreteInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+					for ( i = 0; i < flen; i ++ ) {
 
-		constructor: DiscreteInterpolant,
+						face = faces[ i ];
+						f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
 
-		interpolate_: function ( i1 /*, t0, t, t1 */ ) {
+					}
 
-			return this.copySampleValue_( i1 - 1 );
+				}
 
-		}
+				scope.addGroup( start, verticesArray.length / 3 - start, 0 );
 
-	} );
+			}
 
-	/**
-	 *
-	 * A timed sequence of keyframes for a specific property.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+			// Create faces for the z-sides of the shape
 
-	function KeyframeTrack( name, times, values, interpolation ) {
+			function buildSideFaces() {
 
-		if ( name === undefined ) { throw new Error( 'THREE.KeyframeTrack: track name is undefined' ); }
-		if ( times === undefined || times.length === 0 ) { throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name ); }
+				var start = verticesArray.length / 3;
+				var layeroffset = 0;
+				sidewalls( contour, layeroffset );
+				layeroffset += contour.length;
 
-		this.name = name;
+				for ( h = 0, hl = holes.length; h < hl; h ++ ) {
 
-		this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
-		this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
+					ahole = holes[ h ];
+					sidewalls( ahole, layeroffset );
 
-		this.setInterpolation( interpolation || this.DefaultInterpolation );
+					//, true
+					layeroffset += ahole.length;
 
-	}
+				}
 
-	// Static methods
 
-	Object.assign( KeyframeTrack, {
+				scope.addGroup( start, verticesArray.length / 3 - start, 1 );
 
-		// Serialization (in static context, because of constructor invocation
-		// and automatic invocation of .toJSON):
 
-		toJSON: function ( track ) {
+			}
 
-			var trackType = track.constructor;
+			function sidewalls( contour, layeroffset ) {
 
-			var json;
+				var j, k;
+				i = contour.length;
 
-			// derived classes can define a static toJSON method
-			if ( trackType.toJSON !== undefined ) {
+				while ( -- i >= 0 ) {
 
-				json = trackType.toJSON( track );
+					j = i;
+					k = i - 1;
+					if ( k < 0 ) { k = contour.length - 1; }
 
-			} else {
+					//console.log('b', i,j, i-1, k,vertices.length);
 
-				// by default, we assume the data can be serialized as-is
-				json = {
+					var s = 0,
+						sl = steps + bevelSegments * 2;
 
-					'name': track.name,
-					'times': AnimationUtils.convertArray( track.times, Array ),
-					'values': AnimationUtils.convertArray( track.values, Array )
+					for ( s = 0; s < sl; s ++ ) {
 
-				};
+						var slen1 = vlen * s;
+						var slen2 = vlen * ( s + 1 );
 
-				var interpolation = track.getInterpolation();
+						var a = layeroffset + j + slen1,
+							b = layeroffset + k + slen1,
+							c = layeroffset + k + slen2,
+							d = layeroffset + j + slen2;
 
-				if ( interpolation !== track.DefaultInterpolation ) {
+						f4( a, b, c, d );
 
-					json.interpolation = interpolation;
+					}
 
 				}
 
 			}
 
-			json.type = track.ValueTypeName; // mandatory
+			function v( x, y, z ) {
 
-			return json;
+				placeholder.push( x );
+				placeholder.push( y );
+				placeholder.push( z );
 
-		}
+			}
 
-	} );
 
-	Object.assign( KeyframeTrack.prototype, {
+			function f3( a, b, c ) {
 
-		constructor: KeyframeTrack,
+				addVertex( a );
+				addVertex( b );
+				addVertex( c );
 
-		TimeBufferType: Float32Array,
+				var nextIndex = verticesArray.length / 3;
+				var uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
 
-		ValueBufferType: Float32Array,
+				addUV( uvs[ 0 ] );
+				addUV( uvs[ 1 ] );
+				addUV( uvs[ 2 ] );
 
-		DefaultInterpolation: InterpolateLinear,
+			}
 
-		InterpolantFactoryMethodDiscrete: function ( result ) {
+			function f4( a, b, c, d ) {
 
-			return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
+				addVertex( a );
+				addVertex( b );
+				addVertex( d );
 
-		},
+				addVertex( b );
+				addVertex( c );
+				addVertex( d );
 
-		InterpolantFactoryMethodLinear: function ( result ) {
 
-			return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
+				var nextIndex = verticesArray.length / 3;
+				var uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
 
-		},
+				addUV( uvs[ 0 ] );
+				addUV( uvs[ 1 ] );
+				addUV( uvs[ 3 ] );
 
-		InterpolantFactoryMethodSmooth: function ( result ) {
+				addUV( uvs[ 1 ] );
+				addUV( uvs[ 2 ] );
+				addUV( uvs[ 3 ] );
 
-			return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
+			}
 
-		},
+			function addVertex( index ) {
 
-		setInterpolation: function ( interpolation ) {
+				verticesArray.push( placeholder[ index * 3 + 0 ] );
+				verticesArray.push( placeholder[ index * 3 + 1 ] );
+				verticesArray.push( placeholder[ index * 3 + 2 ] );
 
-			var factoryMethod;
+			}
 
-			switch ( interpolation ) {
 
-				case InterpolateDiscrete:
+			function addUV( vector2 ) {
 
-					factoryMethod = this.InterpolantFactoryMethodDiscrete;
+				uvArray.push( vector2.x );
+				uvArray.push( vector2.y );
 
-					break;
+			}
 
-				case InterpolateLinear:
+		}
 
-					factoryMethod = this.InterpolantFactoryMethodLinear;
+	}
 
-					break;
+	ExtrudeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	ExtrudeBufferGeometry.prototype.constructor = ExtrudeBufferGeometry;
 
-				case InterpolateSmooth:
+	ExtrudeBufferGeometry.prototype.toJSON = function () {
 
-					factoryMethod = this.InterpolantFactoryMethodSmooth;
+		var data = BufferGeometry.prototype.toJSON.call( this );
 
-					break;
+		var shapes = this.parameters.shapes;
+		var options = this.parameters.options;
 
-			}
+		return toJSON( shapes, options, data );
 
-			if ( factoryMethod === undefined ) {
+	};
 
-				var message = "unsupported interpolation for " +
-					this.ValueTypeName + " keyframe track named " + this.name;
+	//
 
-				if ( this.createInterpolant === undefined ) {
+	var WorldUVGenerator = {
 
-					// fall back to default, unless the default itself is messed up
-					if ( interpolation !== this.DefaultInterpolation ) {
+		generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) {
 
-						this.setInterpolation( this.DefaultInterpolation );
+			var a_x = vertices[ indexA * 3 ];
+			var a_y = vertices[ indexA * 3 + 1 ];
+			var b_x = vertices[ indexB * 3 ];
+			var b_y = vertices[ indexB * 3 + 1 ];
+			var c_x = vertices[ indexC * 3 ];
+			var c_y = vertices[ indexC * 3 + 1 ];
 
-					} else {
+			return [
+				new Vector2( a_x, a_y ),
+				new Vector2( b_x, b_y ),
+				new Vector2( c_x, c_y )
+			];
 
-						throw new Error( message ); // fatal, in this case
+		},
 
-					}
+		generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) {
 
-				}
+			var a_x = vertices[ indexA * 3 ];
+			var a_y = vertices[ indexA * 3 + 1 ];
+			var a_z = vertices[ indexA * 3 + 2 ];
+			var b_x = vertices[ indexB * 3 ];
+			var b_y = vertices[ indexB * 3 + 1 ];
+			var b_z = vertices[ indexB * 3 + 2 ];
+			var c_x = vertices[ indexC * 3 ];
+			var c_y = vertices[ indexC * 3 + 1 ];
+			var c_z = vertices[ indexC * 3 + 2 ];
+			var d_x = vertices[ indexD * 3 ];
+			var d_y = vertices[ indexD * 3 + 1 ];
+			var d_z = vertices[ indexD * 3 + 2 ];
 
-				console.warn( 'THREE.KeyframeTrack:', message );
-				return this;
+			if ( Math.abs( a_y - b_y ) < 0.01 ) {
 
-			}
+				return [
+					new Vector2( a_x, 1 - a_z ),
+					new Vector2( b_x, 1 - b_z ),
+					new Vector2( c_x, 1 - c_z ),
+					new Vector2( d_x, 1 - d_z )
+				];
 
-			this.createInterpolant = factoryMethod;
+			} else {
 
-			return this;
+				return [
+					new Vector2( a_y, 1 - a_z ),
+					new Vector2( b_y, 1 - b_z ),
+					new Vector2( c_y, 1 - c_z ),
+					new Vector2( d_y, 1 - d_z )
+				];
 
-		},
+			}
 
-		getInterpolation: function () {
+		}
+	};
 
-			switch ( this.createInterpolant ) {
+	function toJSON( shapes, options, data ) {
 
-				case this.InterpolantFactoryMethodDiscrete:
+		//
 
-					return InterpolateDiscrete;
+		data.shapes = [];
 
-				case this.InterpolantFactoryMethodLinear:
+		if ( Array.isArray( shapes ) ) {
 
-					return InterpolateLinear;
+			for ( var i = 0, l = shapes.length; i < l; i ++ ) {
 
-				case this.InterpolantFactoryMethodSmooth:
+				var shape = shapes[ i ];
 
-					return InterpolateSmooth;
+				data.shapes.push( shape.uuid );
 
 			}
 
-		},
+		} else {
 
-		getValueSize: function () {
+			data.shapes.push( shapes.uuid );
 
-			return this.values.length / this.times.length;
+		}
 
-		},
+		//
 
-		// move all keyframes either forwards or backwards in time
-		shift: function ( timeOffset ) {
+		if ( options.extrudePath !== undefined ) { data.options.extrudePath = options.extrudePath.toJSON(); }
 
-			if ( timeOffset !== 0.0 ) {
+		return data;
 
-				var times = this.times;
+	}
 
-				for ( var i = 0, n = times.length; i !== n; ++ i ) {
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * Text = 3D Text
+	 *
+	 * parameters = {
+	 *  font: <THREE.Font>, // font
+	 *
+	 *  size: <float>, // size of the text
+	 *  height: <float>, // thickness to extrude text
+	 *  curveSegments: <int>, // number of points on the curves
+	 *
+	 *  bevelEnabled: <bool>, // turn on bevel
+	 *  bevelThickness: <float>, // how deep into text bevel goes
+	 *  bevelSize: <float>, // how far from text outline (including bevelOffset) is bevel
+	 *  bevelOffset: <float> // how far from text outline does bevel start
+	 * }
+	 */
 
-					times[ i ] += timeOffset;
+	// TextGeometry
 
-				}
+	function TextGeometry( text, parameters ) {
 
-			}
+		Geometry.call( this );
 
-			return this;
+		this.type = 'TextGeometry';
 
-		},
+		this.parameters = {
+			text: text,
+			parameters: parameters
+		};
 
-		// scale all keyframe times by a factor (useful for frame <-> seconds conversions)
-		scale: function ( timeScale ) {
+		this.fromBufferGeometry( new TextBufferGeometry( text, parameters ) );
+		this.mergeVertices();
 
-			if ( timeScale !== 1.0 ) {
+	}
 
-				var times = this.times;
+	TextGeometry.prototype = Object.create( Geometry.prototype );
+	TextGeometry.prototype.constructor = TextGeometry;
 
-				for ( var i = 0, n = times.length; i !== n; ++ i ) {
+	// TextBufferGeometry
 
-					times[ i ] *= timeScale;
+	function TextBufferGeometry( text, parameters ) {
 
-				}
+		parameters = parameters || {};
 
-			}
+		var font = parameters.font;
 
-			return this;
+		if ( ! ( font && font.isFont ) ) {
 
-		},
+			console.error( 'THREE.TextGeometry: font parameter is not an instance of THREE.Font.' );
+			return new Geometry();
 
-		// removes keyframes before and after animation without changing any values within the range [startTime, endTime].
-		// IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
-		trim: function ( startTime, endTime ) {
+		}
 
-			var times = this.times,
-				nKeys = times.length,
-				from = 0,
-				to = nKeys - 1;
+		var shapes = font.generateShapes( text, parameters.size );
 
-			while ( from !== nKeys && times[ from ] < startTime ) {
+		// translate parameters to ExtrudeGeometry API
 
-				++ from;
+		parameters.depth = parameters.height !== undefined ? parameters.height : 50;
 
-			}
+		// defaults
 
-			while ( to !== - 1 && times[ to ] > endTime ) {
+		if ( parameters.bevelThickness === undefined ) { parameters.bevelThickness = 10; }
+		if ( parameters.bevelSize === undefined ) { parameters.bevelSize = 8; }
+		if ( parameters.bevelEnabled === undefined ) { parameters.bevelEnabled = false; }
 
-				-- to;
+		ExtrudeBufferGeometry.call( this, shapes, parameters );
 
-			}
+		this.type = 'TextBufferGeometry';
 
-			++ to; // inclusive -> exclusive bound
+	}
 
-			if ( from !== 0 || to !== nKeys ) {
+	TextBufferGeometry.prototype = Object.create( ExtrudeBufferGeometry.prototype );
+	TextBufferGeometry.prototype.constructor = TextBufferGeometry;
 
-				// empty tracks are forbidden, so keep at least one keyframe
-				if ( from >= to ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-					to = Math.max( to, 1 );
-					from = to - 1;
+	// SphereGeometry
 
-				}
+	function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
 
-				var stride = this.getValueSize();
-				this.times = AnimationUtils.arraySlice( times, from, to );
-				this.values = AnimationUtils.arraySlice( this.values, from * stride, to * stride );
+		Geometry.call( this );
 
-			}
+		this.type = 'SphereGeometry';
 
-			return this;
+		this.parameters = {
+			radius: radius,
+			widthSegments: widthSegments,
+			heightSegments: heightSegments,
+			phiStart: phiStart,
+			phiLength: phiLength,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-		},
+		this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
+		this.mergeVertices();
 
-		// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
-		validate: function () {
+	}
 
-			var valid = true;
+	SphereGeometry.prototype = Object.create( Geometry.prototype );
+	SphereGeometry.prototype.constructor = SphereGeometry;
 
-			var valueSize = this.getValueSize();
-			if ( valueSize - Math.floor( valueSize ) !== 0 ) {
+	// SphereBufferGeometry
 
-				console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
-				valid = false;
+	function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
 
-			}
+		BufferGeometry.call( this );
 
-			var times = this.times,
-				values = this.values,
+		this.type = 'SphereBufferGeometry';
 
-				nKeys = times.length;
+		this.parameters = {
+			radius: radius,
+			widthSegments: widthSegments,
+			heightSegments: heightSegments,
+			phiStart: phiStart,
+			phiLength: phiLength,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-			if ( nKeys === 0 ) {
+		radius = radius || 1;
 
-				console.error( 'THREE.KeyframeTrack: Track is empty.', this );
-				valid = false;
+		widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
+		heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
 
-			}
+		phiStart = phiStart !== undefined ? phiStart : 0;
+		phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
 
-			var prevTime = null;
+		thetaStart = thetaStart !== undefined ? thetaStart : 0;
+		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
 
-			for ( var i = 0; i !== nKeys; i ++ ) {
+		var thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
 
-				var currTime = times[ i ];
+		var ix, iy;
 
-				if ( typeof currTime === 'number' && isNaN( currTime ) ) {
+		var index = 0;
+		var grid = [];
 
-					console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
-					valid = false;
-					break;
+		var vertex = new Vector3();
+		var normal = new Vector3();
 
-				}
+		// buffers
 
-				if ( prevTime !== null && prevTime > currTime ) {
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-					console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
-					valid = false;
-					break;
+		// generate vertices, normals and uvs
 
-				}
+		for ( iy = 0; iy <= heightSegments; iy ++ ) {
 
-				prevTime = currTime;
+			var verticesRow = [];
 
-			}
+			var v = iy / heightSegments;
 
-			if ( values !== undefined ) {
+			// special case for the poles
 
-				if ( AnimationUtils.isTypedArray( values ) ) {
+			var uOffset = 0;
 
-					for ( var i = 0, n = values.length; i !== n; ++ i ) {
+			if ( iy == 0 && thetaStart == 0 ) {
 
-						var value = values[ i ];
+				uOffset = 0.5 / widthSegments;
 
-						if ( isNaN( value ) ) {
+			} else if ( iy == heightSegments && thetaEnd == Math.PI ) {
 
-							console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
-							valid = false;
-							break;
+				uOffset = - 0.5 / widthSegments;
 
-						}
+			}
 
-					}
+			for ( ix = 0; ix <= widthSegments; ix ++ ) {
 
-				}
+				var u = ix / widthSegments;
 
-			}
+				// vertex
 
-			return valid;
+				vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
+				vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
+				vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
 
-		},
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-		// removes equivalent sequential keys as common in morph target sequences
-		// (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
-		optimize: function () {
+				// normal
 
-			// times or values may be shared with other tracks, so overwriting is unsafe
-			var times = AnimationUtils.arraySlice( this.times ),
-				values = AnimationUtils.arraySlice( this.values ),
-				stride = this.getValueSize(),
+				normal.copy( vertex ).normalize();
+				normals.push( normal.x, normal.y, normal.z );
 
-				smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
+				// uv
 
-				writeIndex = 1,
-				lastIndex = times.length - 1;
+				uvs.push( u + uOffset, 1 - v );
 
-			for ( var i = 1; i < lastIndex; ++ i ) {
+				verticesRow.push( index ++ );
 
-				var keep = false;
+			}
 
-				var time = times[ i ];
-				var timeNext = times[ i + 1 ];
+			grid.push( verticesRow );
 
-				// remove adjacent keyframes scheduled at the same time
+		}
 
-				if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) {
+		// indices
 
-					if ( ! smoothInterpolation ) {
+		for ( iy = 0; iy < heightSegments; iy ++ ) {
 
-						// remove unnecessary keyframes same as their neighbors
+			for ( ix = 0; ix < widthSegments; ix ++ ) {
 
-						var offset = i * stride,
-							offsetP = offset - stride,
-							offsetN = offset + stride;
+				var a = grid[ iy ][ ix + 1 ];
+				var b = grid[ iy ][ ix ];
+				var c = grid[ iy + 1 ][ ix ];
+				var d = grid[ iy + 1 ][ ix + 1 ];
 
-						for ( var j = 0; j !== stride; ++ j ) {
+				if ( iy !== 0 || thetaStart > 0 ) { indices.push( a, b, d ); }
+				if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) { indices.push( b, c, d ); }
 
-							var value = values[ offset + j ];
+			}
 
-							if ( value !== values[ offsetP + j ] ||
-								value !== values[ offsetN + j ] ) {
+		}
 
-								keep = true;
-								break;
+		// build geometry
 
-							}
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-						}
+	}
 
-					} else {
+	SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
 
-						keep = true;
+	/**
+	 * @author Kaleb Murphy
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-					}
+	// RingGeometry
 
-				}
+	function RingGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
 
-				// in-place compaction
+		Geometry.call( this );
 
-				if ( keep ) {
+		this.type = 'RingGeometry';
 
-					if ( i !== writeIndex ) {
+		this.parameters = {
+			innerRadius: innerRadius,
+			outerRadius: outerRadius,
+			thetaSegments: thetaSegments,
+			phiSegments: phiSegments,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-						times[ writeIndex ] = times[ i ];
+		this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) );
+		this.mergeVertices();
 
-						var readOffset = i * stride,
-							writeOffset = writeIndex * stride;
+	}
 
-						for ( var j = 0; j !== stride; ++ j ) {
+	RingGeometry.prototype = Object.create( Geometry.prototype );
+	RingGeometry.prototype.constructor = RingGeometry;
 
-							values[ writeOffset + j ] = values[ readOffset + j ];
+	// RingBufferGeometry
 
-						}
+	function RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
 
-					}
+		BufferGeometry.call( this );
 
-					++ writeIndex;
+		this.type = 'RingBufferGeometry';
 
-				}
+		this.parameters = {
+			innerRadius: innerRadius,
+			outerRadius: outerRadius,
+			thetaSegments: thetaSegments,
+			phiSegments: phiSegments,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-			}
+		innerRadius = innerRadius || 0.5;
+		outerRadius = outerRadius || 1;
 
-			// flush last keyframe (compaction looks ahead)
+		thetaStart = thetaStart !== undefined ? thetaStart : 0;
+		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
 
-			if ( lastIndex > 0 ) {
+		thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
+		phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;
 
-				times[ writeIndex ] = times[ lastIndex ];
+		// buffers
 
-				for ( var readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-					values[ writeOffset + j ] = values[ readOffset + j ];
+		// some helper variables
 
-				}
+		var segment;
+		var radius = innerRadius;
+		var radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
+		var vertex = new Vector3();
+		var uv = new Vector2();
+		var j, i;
 
-				++ writeIndex;
+		// generate vertices, normals and uvs
 
-			}
+		for ( j = 0; j <= phiSegments; j ++ ) {
 
-			if ( writeIndex !== times.length ) {
+			for ( i = 0; i <= thetaSegments; i ++ ) {
 
-				this.times = AnimationUtils.arraySlice( times, 0, writeIndex );
-				this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride );
+				// values are generate from the inside of the ring to the outside
 
-			} else {
+				segment = thetaStart + i / thetaSegments * thetaLength;
 
-				this.times = times;
-				this.values = values;
+				// vertex
 
-			}
+				vertex.x = radius * Math.cos( segment );
+				vertex.y = radius * Math.sin( segment );
 
-			return this;
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-		},
+				// normal
 
-		clone: function () {
+				normals.push( 0, 0, 1 );
 
-			var times = AnimationUtils.arraySlice( this.times, 0 );
-			var values = AnimationUtils.arraySlice( this.values, 0 );
+				// uv
 
-			var TypedKeyframeTrack = this.constructor;
-			var track = new TypedKeyframeTrack( this.name, times, values );
+				uv.x = ( vertex.x / outerRadius + 1 ) / 2;
+				uv.y = ( vertex.y / outerRadius + 1 ) / 2;
 
-			// Interpolant argument to constructor is not saved, so copy the factory method directly.
-			track.createInterpolant = this.createInterpolant;
+				uvs.push( uv.x, uv.y );
 
-			return track;
+			}
 
-		}
+			// increase the radius for next row of vertices
 
-	} );
+			radius += radiusStep;
 
-	/**
-	 *
-	 * A Track of Boolean keyframe values.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+		}
 
-	function BooleanKeyframeTrack( name, times, values ) {
+		// indices
 
-		KeyframeTrack.call( this, name, times, values );
+		for ( j = 0; j < phiSegments; j ++ ) {
 
-	}
+			var thetaSegmentLevel = j * ( thetaSegments + 1 );
 
-	BooleanKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+			for ( i = 0; i < thetaSegments; i ++ ) {
 
-		constructor: BooleanKeyframeTrack,
+				segment = i + thetaSegmentLevel;
 
-		ValueTypeName: 'bool',
-		ValueBufferType: Array,
+				var a = segment;
+				var b = segment + thetaSegments + 1;
+				var c = segment + thetaSegments + 2;
+				var d = segment + 1;
 
-		DefaultInterpolation: InterpolateDiscrete,
+				// faces
 
-		InterpolantFactoryMethodLinear: undefined,
-		InterpolantFactoryMethodSmooth: undefined
+				indices.push( a, b, d );
+				indices.push( b, c, d );
 
-		// Note: Actually this track could have a optimized / compressed
-		// representation of a single value and a custom interpolant that
-		// computes "firstValue ^ isOdd( index )".
+			}
 
-	} );
+		}
 
-	/**
-	 *
-	 * A Track of keyframe values that represent color.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
-
-	function ColorKeyframeTrack( name, times, values, interpolation ) {
+		// build geometry
 
-		KeyframeTrack.call( this, name, times, values, interpolation );
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
 	}
 
-	ColorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+	RingBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	RingBufferGeometry.prototype.constructor = RingBufferGeometry;
 
-		constructor: ColorKeyframeTrack,
+	/**
+	 * @author zz85 / https://github.com/zz85
+	 * @author bhouston / http://clara.io
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-		ValueTypeName: 'color'
+	// LatheGeometry
 
-		// ValueBufferType is inherited
+	function LatheGeometry( points, segments, phiStart, phiLength ) {
 
-		// DefaultInterpolation is inherited
+		Geometry.call( this );
 
-		// Note: Very basic implementation and nothing special yet.
-		// However, this is the place for color space parameterization.
+		this.type = 'LatheGeometry';
 
-	} );
+		this.parameters = {
+			points: points,
+			segments: segments,
+			phiStart: phiStart,
+			phiLength: phiLength
+		};
 
-	/**
-	 *
-	 * A Track of numeric keyframe values.
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+		this.fromBufferGeometry( new LatheBufferGeometry( points, segments, phiStart, phiLength ) );
+		this.mergeVertices();
 
-	function NumberKeyframeTrack( name, times, values, interpolation ) {
+	}
 
-		KeyframeTrack.call( this, name, times, values, interpolation );
+	LatheGeometry.prototype = Object.create( Geometry.prototype );
+	LatheGeometry.prototype.constructor = LatheGeometry;
 
-	}
+	// LatheBufferGeometry
 
-	NumberKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+	function LatheBufferGeometry( points, segments, phiStart, phiLength ) {
 
-		constructor: NumberKeyframeTrack,
+		BufferGeometry.call( this );
 
-		ValueTypeName: 'number'
+		this.type = 'LatheBufferGeometry';
 
-		// ValueBufferType is inherited
+		this.parameters = {
+			points: points,
+			segments: segments,
+			phiStart: phiStart,
+			phiLength: phiLength
+		};
 
-		// DefaultInterpolation is inherited
+		segments = Math.floor( segments ) || 12;
+		phiStart = phiStart || 0;
+		phiLength = phiLength || Math.PI * 2;
 
-	} );
+		// clamp phiLength so it's in range of [ 0, 2PI ]
 
-	/**
-	 * Spherical linear unit quaternion interpolant.
-	 *
-	 * @author tschw
-	 */
+		phiLength = MathUtils.clamp( phiLength, 0, Math.PI * 2 );
 
-	function QuaternionLinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+		// buffers
 
-	}
+		var indices = [];
+		var vertices = [];
+		var uvs = [];
 
-	QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+		// helper variables
 
-		constructor: QuaternionLinearInterpolant,
+		var base;
+		var inverseSegments = 1.0 / segments;
+		var vertex = new Vector3();
+		var uv = new Vector2();
+		var i, j;
 
-		interpolate_: function ( i1, t0, t, t1 ) {
+		// generate vertices and uvs
 
-			var result = this.resultBuffer,
-				values = this.sampleValues,
-				stride = this.valueSize,
+		for ( i = 0; i <= segments; i ++ ) {
 
-				offset = i1 * stride,
+			var phi = phiStart + i * inverseSegments * phiLength;
 
-				alpha = ( t - t0 ) / ( t1 - t0 );
+			var sin = Math.sin( phi );
+			var cos = Math.cos( phi );
 
-			for ( var end = offset + stride; offset !== end; offset += 4 ) {
+			for ( j = 0; j <= ( points.length - 1 ); j ++ ) {
 
-				Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha );
+				// vertex
 
-			}
+				vertex.x = points[ j ].x * sin;
+				vertex.y = points[ j ].y;
+				vertex.z = points[ j ].x * cos;
 
-			return result;
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-		}
+				// uv
 
-	} );
+				uv.x = i / segments;
+				uv.y = j / ( points.length - 1 );
 
-	/**
-	 *
-	 * A Track of quaternion keyframe values.
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+				uvs.push( uv.x, uv.y );
 
-	function QuaternionKeyframeTrack( name, times, values, interpolation ) {
 
-		KeyframeTrack.call( this, name, times, values, interpolation );
+			}
 
-	}
+		}
 
-	QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+		// indices
 
-		constructor: QuaternionKeyframeTrack,
+		for ( i = 0; i < segments; i ++ ) {
 
-		ValueTypeName: 'quaternion',
+			for ( j = 0; j < ( points.length - 1 ); j ++ ) {
 
-		// ValueBufferType is inherited
+				base = j + i * points.length;
 
-		DefaultInterpolation: InterpolateLinear,
+				var a = base;
+				var b = base + points.length;
+				var c = base + points.length + 1;
+				var d = base + 1;
 
-		InterpolantFactoryMethodLinear: function ( result ) {
+				// faces
 
-			return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
+				indices.push( a, b, d );
+				indices.push( b, c, d );
 
-		},
+			}
 
-		InterpolantFactoryMethodSmooth: undefined // not yet implemented
+		}
 
-	} );
+		// build geometry
 
-	/**
-	 *
-	 * A Track that interpolates Strings
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-	function StringKeyframeTrack( name, times, values, interpolation ) {
+		// generate normals
 
-		KeyframeTrack.call( this, name, times, values, interpolation );
+		this.computeVertexNormals();
 
-	}
+		// if the geometry is closed, we need to average the normals along the seam.
+		// because the corresponding vertices are identical (but still have different UVs).
 
-	StringKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+		if ( phiLength === Math.PI * 2 ) {
 
-		constructor: StringKeyframeTrack,
+			var normals = this.attributes.normal.array;
+			var n1 = new Vector3();
+			var n2 = new Vector3();
+			var n = new Vector3();
 
-		ValueTypeName: 'string',
-		ValueBufferType: Array,
+			// this is the buffer offset for the last line of vertices
 
-		DefaultInterpolation: InterpolateDiscrete,
+			base = segments * points.length * 3;
 
-		InterpolantFactoryMethodLinear: undefined,
+			for ( i = 0, j = 0; i < points.length; i ++, j += 3 ) {
 
-		InterpolantFactoryMethodSmooth: undefined
+				// select the normal of the vertex in the first line
 
-	} );
+				n1.x = normals[ j + 0 ];
+				n1.y = normals[ j + 1 ];
+				n1.z = normals[ j + 2 ];
 
-	/**
-	 *
-	 * A Track of vectored keyframe values.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 */
+				// select the normal of the vertex in the last line
 
-	function VectorKeyframeTrack( name, times, values, interpolation ) {
+				n2.x = normals[ base + j + 0 ];
+				n2.y = normals[ base + j + 1 ];
+				n2.z = normals[ base + j + 2 ];
 
-		KeyframeTrack.call( this, name, times, values, interpolation );
+				// average normals
 
-	}
+				n.addVectors( n1, n2 ).normalize();
 
-	VectorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+				// assign the new values to both normals
 
-		constructor: VectorKeyframeTrack,
+				normals[ j + 0 ] = normals[ base + j + 0 ] = n.x;
+				normals[ j + 1 ] = normals[ base + j + 1 ] = n.y;
+				normals[ j + 2 ] = normals[ base + j + 2 ] = n.z;
 
-		ValueTypeName: 'vector'
+			}
 
-		// ValueBufferType is inherited
+		}
 
-		// DefaultInterpolation is inherited
+	}
 
-	} );
+	LatheBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	LatheBufferGeometry.prototype.constructor = LatheBufferGeometry;
 
 	/**
-	 *
-	 * Reusable set of Tracks that represent an animation.
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
+	 * @author jonobr1 / http://jonobr1.com
+	 * @author Mugen87 / https://github.com/Mugen87
 	 */
 
-	function AnimationClip( name, duration, tracks, blendMode ) {
+	// ShapeGeometry
 
-		this.name = name;
-		this.tracks = tracks;
-		this.duration = ( duration !== undefined ) ? duration : - 1;
-		this.blendMode = ( blendMode !== undefined ) ? blendMode : NormalAnimationBlendMode;
+	function ShapeGeometry( shapes, curveSegments ) {
 
-		this.uuid = MathUtils.generateUUID();
+		Geometry.call( this );
 
-		// this means it should figure out its duration by scanning the tracks
-		if ( this.duration < 0 ) {
+		this.type = 'ShapeGeometry';
 
-			this.resetDuration();
+		if ( typeof curveSegments === 'object' ) {
+
+			console.warn( 'THREE.ShapeGeometry: Options parameter has been removed.' );
+
+			curveSegments = curveSegments.curveSegments;
 
 		}
 
-	}
+		this.parameters = {
+			shapes: shapes,
+			curveSegments: curveSegments
+		};
 
-	function getTrackTypeForValueTypeName( typeName ) {
+		this.fromBufferGeometry( new ShapeBufferGeometry( shapes, curveSegments ) );
+		this.mergeVertices();
 
-		switch ( typeName.toLowerCase() ) {
+	}
 
-			case 'scalar':
-			case 'double':
-			case 'float':
-			case 'number':
-			case 'integer':
+	ShapeGeometry.prototype = Object.create( Geometry.prototype );
+	ShapeGeometry.prototype.constructor = ShapeGeometry;
 
-				return NumberKeyframeTrack;
+	ShapeGeometry.prototype.toJSON = function () {
 
-			case 'vector':
-			case 'vector2':
-			case 'vector3':
-			case 'vector4':
+		var data = Geometry.prototype.toJSON.call( this );
 
-				return VectorKeyframeTrack;
+		var shapes = this.parameters.shapes;
 
-			case 'color':
+		return toJSON$1( shapes, data );
 
-				return ColorKeyframeTrack;
+	};
 
-			case 'quaternion':
+	// ShapeBufferGeometry
 
-				return QuaternionKeyframeTrack;
+	function ShapeBufferGeometry( shapes, curveSegments ) {
 
-			case 'bool':
-			case 'boolean':
+		BufferGeometry.call( this );
 
-				return BooleanKeyframeTrack;
+		this.type = 'ShapeBufferGeometry';
 
-			case 'string':
+		this.parameters = {
+			shapes: shapes,
+			curveSegments: curveSegments
+		};
 
-				return StringKeyframeTrack;
+		curveSegments = curveSegments || 12;
 
-		}
+		// buffers
 
-		throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-	}
+		// helper variables
 
-	function parseKeyframeTrack( json ) {
+		var groupStart = 0;
+		var groupCount = 0;
 
-		if ( json.type === undefined ) {
+		// allow single and array values for "shapes" parameter
 
-			throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
+		if ( Array.isArray( shapes ) === false ) {
 
-		}
+			addShape( shapes );
 
-		var trackType = getTrackTypeForValueTypeName( json.type );
+		} else {
 
-		if ( json.times === undefined ) {
+			for ( var i = 0; i < shapes.length; i ++ ) {
 
-			var times = [], values = [];
+				addShape( shapes[ i ] );
 
-			AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
+				this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
 
-			json.times = times;
-			json.values = values;
+				groupStart += groupCount;
+				groupCount = 0;
+
+			}
 
 		}
 
-		// derived classes can define a static parse method
-		if ( trackType.parse !== undefined ) {
+		// build geometry
 
-			return trackType.parse( json );
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-		} else {
 
-			// by default, we assume a constructor compatible with the base
-			return new trackType( json.name, json.times, json.values, json.interpolation );
+		// helper functions
 
-		}
+		function addShape( shape ) {
 
-	}
+			var i, l, shapeHole;
 
-	Object.assign( AnimationClip, {
+			var indexOffset = vertices.length / 3;
+			var points = shape.extractPoints( curveSegments );
 
-		parse: function ( json ) {
+			var shapeVertices = points.shape;
+			var shapeHoles = points.holes;
 
-			var tracks = [],
-				jsonTracks = json.tracks,
-				frameTime = 1.0 / ( json.fps || 1.0 );
+			// check direction of vertices
 
-			for ( var i = 0, n = jsonTracks.length; i !== n; ++ i ) {
+			if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
 
-				tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
+				shapeVertices = shapeVertices.reverse();
 
 			}
 
-			return new AnimationClip( json.name, json.duration, tracks, json.blendMode );
+			for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
 
-		},
+				shapeHole = shapeHoles[ i ];
 
-		toJSON: function ( clip ) {
+				if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
 
-			var tracks = [],
-				clipTracks = clip.tracks;
+					shapeHoles[ i ] = shapeHole.reverse();
 
-			var json = {
+				}
 
-				'name': clip.name,
-				'duration': clip.duration,
-				'tracks': tracks,
-				'uuid': clip.uuid,
-				'blendMode': clip.blendMode
+			}
 
-			};
+			var faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
 
-			for ( var i = 0, n = clipTracks.length; i !== n; ++ i ) {
+			// join vertices of inner and outer paths to a single array
 
-				tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
+			for ( i = 0, l = shapeHoles.length; i < l; i ++ ) {
+
+				shapeHole = shapeHoles[ i ];
+				shapeVertices = shapeVertices.concat( shapeHole );
 
 			}
 
-			return json;
+			// vertices, normals, uvs
 
-		},
+			for ( i = 0, l = shapeVertices.length; i < l; i ++ ) {
 
-		CreateFromMorphTargetSequence: function ( name, morphTargetSequence, fps, noLoop ) {
+				var vertex = shapeVertices[ i ];
 
-			var numMorphTargets = morphTargetSequence.length;
-			var tracks = [];
+				vertices.push( vertex.x, vertex.y, 0 );
+				normals.push( 0, 0, 1 );
+				uvs.push( vertex.x, vertex.y ); // world uvs
 
-			for ( var i = 0; i < numMorphTargets; i ++ ) {
+			}
 
-				var times = [];
-				var values = [];
+			// incides
 
-				times.push(
-					( i + numMorphTargets - 1 ) % numMorphTargets,
-					i,
-					( i + 1 ) % numMorphTargets );
+			for ( i = 0, l = faces.length; i < l; i ++ ) {
 
-				values.push( 0, 1, 0 );
+				var face = faces[ i ];
 
-				var order = AnimationUtils.getKeyframeOrder( times );
-				times = AnimationUtils.sortedArray( times, 1, order );
-				values = AnimationUtils.sortedArray( values, 1, order );
+				var a = face[ 0 ] + indexOffset;
+				var b = face[ 1 ] + indexOffset;
+				var c = face[ 2 ] + indexOffset;
 
-				// if there is a key at the first frame, duplicate it as the
-				// last frame as well for perfect loop.
-				if ( ! noLoop && times[ 0 ] === 0 ) {
+				indices.push( a, b, c );
+				groupCount += 3;
 
-					times.push( numMorphTargets );
-					values.push( values[ 0 ] );
+			}
 
-				}
+		}
 
-				tracks.push(
-					new NumberKeyframeTrack(
-						'.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
-						times, values
-					).scale( 1.0 / fps ) );
+	}
 
-			}
+	ShapeBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	ShapeBufferGeometry.prototype.constructor = ShapeBufferGeometry;
 
-			return new AnimationClip( name, - 1, tracks );
+	ShapeBufferGeometry.prototype.toJSON = function () {
 
-		},
+		var data = BufferGeometry.prototype.toJSON.call( this );
 
-		findByName: function ( objectOrClipArray, name ) {
+		var shapes = this.parameters.shapes;
 
-			var clipArray = objectOrClipArray;
+		return toJSON$1( shapes, data );
 
-			if ( ! Array.isArray( objectOrClipArray ) ) {
+	};
 
-				var o = objectOrClipArray;
-				clipArray = o.geometry && o.geometry.animations || o.animations;
+	//
 
-			}
+	function toJSON$1( shapes, data ) {
 
-			for ( var i = 0; i < clipArray.length; i ++ ) {
+		data.shapes = [];
 
-				if ( clipArray[ i ].name === name ) {
+		if ( Array.isArray( shapes ) ) {
 
-					return clipArray[ i ];
+			for ( var i = 0, l = shapes.length; i < l; i ++ ) {
 
-				}
+				var shape = shapes[ i ];
+
+				data.shapes.push( shape.uuid );
 
 			}
 
-			return null;
+		} else {
 
-		},
+			data.shapes.push( shapes.uuid );
 
-		CreateClipsFromMorphTargetSequences: function ( morphTargets, fps, noLoop ) {
+		}
 
-			var animationToMorphTargets = {};
+		return data;
 
-			// tested with https://regex101.com/ on trick sequences
-			// such flamingo_flyA_003, flamingo_run1_003, crdeath0059
-			var pattern = /^([\w-]*?)([\d]+)$/;
+	}
 
-			// sort morph target names into animation groups based
-			// patterns like Walk_001, Walk_002, Run_001, Run_002
-			for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-				var morphTarget = morphTargets[ i ];
-				var parts = morphTarget.name.match( pattern );
+	function EdgesGeometry( geometry, thresholdAngle ) {
 
-				if ( parts && parts.length > 1 ) {
+		BufferGeometry.call( this );
 
-					var name = parts[ 1 ];
+		this.type = 'EdgesGeometry';
 
-					var animationMorphTargets = animationToMorphTargets[ name ];
-					if ( ! animationMorphTargets ) {
+		this.parameters = {
+			thresholdAngle: thresholdAngle
+		};
 
-						animationToMorphTargets[ name ] = animationMorphTargets = [];
+		thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
 
-					}
+		// buffer
 
-					animationMorphTargets.push( morphTarget );
+		var vertices = [];
 
-				}
+		// helper variables
 
-			}
+		var thresholdDot = Math.cos( MathUtils.DEG2RAD * thresholdAngle );
+		var edge = [ 0, 0 ], edges = {}, edge1, edge2;
+		var key, keys = [ 'a', 'b', 'c' ];
 
-			var clips = [];
+		// prepare source geometry
 
-			for ( var name in animationToMorphTargets ) {
+		var geometry2;
 
-				clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
+		if ( geometry.isBufferGeometry ) {
 
-			}
-
-			return clips;
-
-		},
+			geometry2 = new Geometry();
+			geometry2.fromBufferGeometry( geometry );
 
-		// parse the animation.hierarchy format
-		parseAnimation: function ( animation, bones ) {
+		} else {
 
-			if ( ! animation ) {
+			geometry2 = geometry.clone();
 
-				console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
-				return null;
+		}
 
-			}
+		geometry2.mergeVertices();
+		geometry2.computeFaceNormals();
 
-			var addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
+		var sourceVertices = geometry2.vertices;
+		var faces = geometry2.faces;
 
-				// only return track if there are actually keys.
-				if ( animationKeys.length !== 0 ) {
+		// now create a data structure where each entry represents an edge with its adjoining faces
 
-					var times = [];
-					var values = [];
+		for ( var i = 0, l = faces.length; i < l; i ++ ) {
 
-					AnimationUtils.flattenJSON( animationKeys, times, values, propertyName );
+			var face = faces[ i ];
 
-					// empty keys are filtered out, so check again
-					if ( times.length !== 0 ) {
+			for ( var j = 0; j < 3; j ++ ) {
 
-						destTracks.push( new trackType( trackName, times, values ) );
+				edge1 = face[ keys[ j ] ];
+				edge2 = face[ keys[ ( j + 1 ) % 3 ] ];
+				edge[ 0 ] = Math.min( edge1, edge2 );
+				edge[ 1 ] = Math.max( edge1, edge2 );
 
-					}
+				key = edge[ 0 ] + ',' + edge[ 1 ];
 
-				}
+				if ( edges[ key ] === undefined ) {
 
-			};
+					edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ], face1: i, face2: undefined };
 
-			var tracks = [];
+				} else {
 
-			var clipName = animation.name || 'default';
-			// automatic length determination in AnimationClip.
-			var duration = animation.length || - 1;
-			var fps = animation.fps || 30;
-			var blendMode = animation.blendMode;
+					edges[ key ].face2 = i;
 
-			var hierarchyTracks = animation.hierarchy || [];
+				}
 
-			for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
+			}
 
-				var animationKeys = hierarchyTracks[ h ].keys;
+		}
 
-				// skip empty tracks
-				if ( ! animationKeys || animationKeys.length === 0 ) { continue; }
+		// generate vertices
 
-				// process morph targets
-				if ( animationKeys[ 0 ].morphTargets ) {
+		for ( key in edges ) {
 
-					// figure out all morph targets used in this track
-					var morphTargetNames = {};
+			var e = edges[ key ];
 
-					for ( var k = 0; k < animationKeys.length; k ++ ) {
+			// an edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. default = 1 degree.
 
-						if ( animationKeys[ k ].morphTargets ) {
+			if ( e.face2 === undefined || faces[ e.face1 ].normal.dot( faces[ e.face2 ].normal ) <= thresholdDot ) {
 
-							for ( var m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
+				var vertex = sourceVertices[ e.index1 ];
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-								morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
+				vertex = sourceVertices[ e.index2 ];
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-							}
+			}
 
-						}
+		}
 
-					}
+		// build geometry
 
-					// create a track for each morph target with all zero
-					// morphTargetInfluences except for the keys in which
-					// the morphTarget is named.
-					for ( var morphTargetName in morphTargetNames ) {
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
 
-						var times = [];
-						var values = [];
+	}
 
-						for ( var m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
+	EdgesGeometry.prototype = Object.create( BufferGeometry.prototype );
+	EdgesGeometry.prototype.constructor = EdgesGeometry;
 
-							var animationKey = animationKeys[ k ];
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-							times.push( animationKey.time );
-							values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
+	// CylinderGeometry
 
-						}
+	function CylinderGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
 
-						tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
+		Geometry.call( this );
 
-					}
+		this.type = 'CylinderGeometry';
 
-					duration = morphTargetNames.length * ( fps || 1.0 );
+		this.parameters = {
+			radiusTop: radiusTop,
+			radiusBottom: radiusBottom,
+			height: height,
+			radialSegments: radialSegments,
+			heightSegments: heightSegments,
+			openEnded: openEnded,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-				} else {
+		this.fromBufferGeometry( new CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) );
+		this.mergeVertices();
 
-					// ...assume skeletal animation
+	}
 
-					var boneName = '.bones[' + bones[ h ].name + ']';
+	CylinderGeometry.prototype = Object.create( Geometry.prototype );
+	CylinderGeometry.prototype.constructor = CylinderGeometry;
 
-					addNonemptyTrack(
-						VectorKeyframeTrack, boneName + '.position',
-						animationKeys, 'pos', tracks );
+	// CylinderBufferGeometry
 
-					addNonemptyTrack(
-						QuaternionKeyframeTrack, boneName + '.quaternion',
-						animationKeys, 'rot', tracks );
+	function CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
 
-					addNonemptyTrack(
-						VectorKeyframeTrack, boneName + '.scale',
-						animationKeys, 'scl', tracks );
+		BufferGeometry.call( this );
 
-				}
+		this.type = 'CylinderBufferGeometry';
 
-			}
+		this.parameters = {
+			radiusTop: radiusTop,
+			radiusBottom: radiusBottom,
+			height: height,
+			radialSegments: radialSegments,
+			heightSegments: heightSegments,
+			openEnded: openEnded,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-			if ( tracks.length === 0 ) {
+		var scope = this;
 
-				return null;
+		radiusTop = radiusTop !== undefined ? radiusTop : 1;
+		radiusBottom = radiusBottom !== undefined ? radiusBottom : 1;
+		height = height || 1;
 
-			}
+		radialSegments = Math.floor( radialSegments ) || 8;
+		heightSegments = Math.floor( heightSegments ) || 1;
 
-			var clip = new AnimationClip( clipName, duration, tracks, blendMode );
+		openEnded = openEnded !== undefined ? openEnded : false;
+		thetaStart = thetaStart !== undefined ? thetaStart : 0.0;
+		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
 
-			return clip;
+		// buffers
 
-		}
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-	} );
+		// helper variables
 
-	Object.assign( AnimationClip.prototype, {
+		var index = 0;
+		var indexArray = [];
+		var halfHeight = height / 2;
+		var groupStart = 0;
 
-		resetDuration: function () {
+		// generate geometry
 
-			var tracks = this.tracks, duration = 0;
+		generateTorso();
 
-			for ( var i = 0, n = tracks.length; i !== n; ++ i ) {
+		if ( openEnded === false ) {
 
-				var track = this.tracks[ i ];
+			if ( radiusTop > 0 ) { generateCap( true ); }
+			if ( radiusBottom > 0 ) { generateCap( false ); }
 
-				duration = Math.max( duration, track.times[ track.times.length - 1 ] );
+		}
 
-			}
+		// build geometry
 
-			this.duration = duration;
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-			return this;
+		function generateTorso() {
 
-		},
+			var x, y;
+			var normal = new Vector3();
+			var vertex = new Vector3();
 
-		trim: function () {
+			var groupCount = 0;
 
-			for ( var i = 0; i < this.tracks.length; i ++ ) {
+			// this will be used to calculate the normal
+			var slope = ( radiusBottom - radiusTop ) / height;
 
-				this.tracks[ i ].trim( 0, this.duration );
+			// generate vertices, normals and uvs
 
-			}
+			for ( y = 0; y <= heightSegments; y ++ ) {
 
-			return this;
+				var indexRow = [];
 
-		},
+				var v = y / heightSegments;
 
-		validate: function () {
+				// calculate the radius of the current row
 
-			var valid = true;
+				var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
 
-			for ( var i = 0; i < this.tracks.length; i ++ ) {
+				for ( x = 0; x <= radialSegments; x ++ ) {
 
-				valid = valid && this.tracks[ i ].validate();
+					var u = x / radialSegments;
 
-			}
+					var theta = u * thetaLength + thetaStart;
 
-			return valid;
+					var sinTheta = Math.sin( theta );
+					var cosTheta = Math.cos( theta );
 
-		},
+					// vertex
 
-		optimize: function () {
+					vertex.x = radius * sinTheta;
+					vertex.y = - v * height + halfHeight;
+					vertex.z = radius * cosTheta;
+					vertices.push( vertex.x, vertex.y, vertex.z );
 
-			for ( var i = 0; i < this.tracks.length; i ++ ) {
+					// normal
 
-				this.tracks[ i ].optimize();
+					normal.set( sinTheta, slope, cosTheta ).normalize();
+					normals.push( normal.x, normal.y, normal.z );
 
-			}
+					// uv
 
-			return this;
+					uvs.push( u, 1 - v );
 
-		},
+					// save index of vertex in respective row
 
-		clone: function () {
+					indexRow.push( index ++ );
 
-			var tracks = [];
+				}
 
-			for ( var i = 0; i < this.tracks.length; i ++ ) {
+				// now save vertices of the row in our index array
 
-				tracks.push( this.tracks[ i ].clone() );
+				indexArray.push( indexRow );
 
 			}
 
-			return new AnimationClip( this.name, this.duration, tracks, this.blendMode );
-
-		}
-
-	} );
-
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
-
-	var Cache = {
-
-		enabled: false,
-
-		files: {},
-
-		add: function ( key, file ) {
+			// generate indices
 
-			if ( this.enabled === false ) { return; }
+			for ( x = 0; x < radialSegments; x ++ ) {
 
-			// console.log( 'THREE.Cache', 'Adding key:', key );
+				for ( y = 0; y < heightSegments; y ++ ) {
 
-			this.files[ key ] = file;
+					// we use the index array to access the correct indices
 
-		},
+					var a = indexArray[ y ][ x ];
+					var b = indexArray[ y + 1 ][ x ];
+					var c = indexArray[ y + 1 ][ x + 1 ];
+					var d = indexArray[ y ][ x + 1 ];
 
-		get: function ( key ) {
+					// faces
 
-			if ( this.enabled === false ) { return; }
+					indices.push( a, b, d );
+					indices.push( b, c, d );
 
-			// console.log( 'THREE.Cache', 'Checking key:', key );
+					// update group counter
 
-			return this.files[ key ];
+					groupCount += 6;
 
-		},
+				}
 
-		remove: function ( key ) {
+			}
 
-			delete this.files[ key ];
+			// add a group to the geometry. this will ensure multi material support
 
-		},
+			scope.addGroup( groupStart, groupCount, 0 );
 
-		clear: function () {
+			// calculate new start value for groups
 
-			this.files = {};
+			groupStart += groupCount;
 
 		}
 
-	};
+		function generateCap( top ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			var x, centerIndexStart, centerIndexEnd;
 
-	function LoadingManager( onLoad, onProgress, onError ) {
+			var uv = new Vector2();
+			var vertex = new Vector3();
 
-		var scope = this;
+			var groupCount = 0;
 
-		var isLoading = false;
-		var itemsLoaded = 0;
-		var itemsTotal = 0;
-		var urlModifier = undefined;
-		var handlers = [];
+			var radius = ( top === true ) ? radiusTop : radiusBottom;
+			var sign = ( top === true ) ? 1 : - 1;
 
-		// Refer to #5689 for the reason why we don't set .onStart
-		// in the constructor
+			// save the index of the first center vertex
+			centerIndexStart = index;
 
-		this.onStart = undefined;
-		this.onLoad = onLoad;
-		this.onProgress = onProgress;
-		this.onError = onError;
+			// first we generate the center vertex data of the cap.
+			// because the geometry needs one set of uvs per face,
+			// we must generate a center vertex per face/segment
 
-		this.itemStart = function ( url ) {
+			for ( x = 1; x <= radialSegments; x ++ ) {
 
-			itemsTotal ++;
+				// vertex
 
-			if ( isLoading === false ) {
+				vertices.push( 0, halfHeight * sign, 0 );
 
-				if ( scope.onStart !== undefined ) {
+				// normal
 
-					scope.onStart( url, itemsLoaded, itemsTotal );
+				normals.push( 0, sign, 0 );
 
-				}
+				// uv
 
-			}
+				uvs.push( 0.5, 0.5 );
 
-			isLoading = true;
+				// increase index
 
-		};
+				index ++;
 
-		this.itemEnd = function ( url ) {
+			}
 
-			itemsLoaded ++;
+			// save the index of the last center vertex
 
-			if ( scope.onProgress !== undefined ) {
+			centerIndexEnd = index;
 
-				scope.onProgress( url, itemsLoaded, itemsTotal );
+			// now we generate the surrounding vertices, normals and uvs
 
-			}
+			for ( x = 0; x <= radialSegments; x ++ ) {
 
-			if ( itemsLoaded === itemsTotal ) {
+				var u = x / radialSegments;
+				var theta = u * thetaLength + thetaStart;
 
-				isLoading = false;
+				var cosTheta = Math.cos( theta );
+				var sinTheta = Math.sin( theta );
 
-				if ( scope.onLoad !== undefined ) {
+				// vertex
 
-					scope.onLoad();
+				vertex.x = radius * sinTheta;
+				vertex.y = halfHeight * sign;
+				vertex.z = radius * cosTheta;
+				vertices.push( vertex.x, vertex.y, vertex.z );
 
-				}
+				// normal
 
-			}
+				normals.push( 0, sign, 0 );
 
-		};
+				// uv
 
-		this.itemError = function ( url ) {
+				uv.x = ( cosTheta * 0.5 ) + 0.5;
+				uv.y = ( sinTheta * 0.5 * sign ) + 0.5;
+				uvs.push( uv.x, uv.y );
 
-			if ( scope.onError !== undefined ) {
+				// increase index
 
-				scope.onError( url );
+				index ++;
 
 			}
 
-		};
+			// generate indices
 
-		this.resolveURL = function ( url ) {
+			for ( x = 0; x < radialSegments; x ++ ) {
 
-			if ( urlModifier ) {
+				var c = centerIndexStart + x;
+				var i = centerIndexEnd + x;
 
-				return urlModifier( url );
+				if ( top === true ) {
 
-			}
+					// face top
 
-			return url;
+					indices.push( i, i + 1, c );
 
-		};
+				} else {
 
-		this.setURLModifier = function ( transform ) {
+					// face bottom
 
-			urlModifier = transform;
+					indices.push( i + 1, i, c );
 
-			return this;
+				}
 
-		};
+				groupCount += 3;
 
-		this.addHandler = function ( regex, loader ) {
+			}
 
-			handlers.push( regex, loader );
+			// add a group to the geometry. this will ensure multi material support
 
-			return this;
+			scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
 
-		};
+			// calculate new start value for groups
 
-		this.removeHandler = function ( regex ) {
+			groupStart += groupCount;
 
-			var index = handlers.indexOf( regex );
+		}
 
-			if ( index !== - 1 ) {
+	}
 
-				handlers.splice( index, 2 );
+	CylinderBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	CylinderBufferGeometry.prototype.constructor = CylinderBufferGeometry;
 
-			}
+	/**
+	 * @author abelnation / http://github.com/abelnation
+	 */
 
-			return this;
+	// ConeGeometry
 
-		};
+	function ConeGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
 
-		this.getHandler = function ( file ) {
+		CylinderGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
 
-			for ( var i = 0, l = handlers.length; i < l; i += 2 ) {
+		this.type = 'ConeGeometry';
 
-				var regex = handlers[ i ];
-				var loader = handlers[ i + 1 ];
+		this.parameters = {
+			radius: radius,
+			height: height,
+			radialSegments: radialSegments,
+			heightSegments: heightSegments,
+			openEnded: openEnded,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-				if ( regex.global ) { regex.lastIndex = 0; } // see #17920
+	}
 
-				if ( regex.test( file ) ) {
+	ConeGeometry.prototype = Object.create( CylinderGeometry.prototype );
+	ConeGeometry.prototype.constructor = ConeGeometry;
 
-					return loader;
+	// ConeBufferGeometry
 
-				}
+	function ConeBufferGeometry( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
 
-			}
+		CylinderBufferGeometry.call( this, 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
 
-			return null;
+		this.type = 'ConeBufferGeometry';
 
+		this.parameters = {
+			radius: radius,
+			height: height,
+			radialSegments: radialSegments,
+			heightSegments: heightSegments,
+			openEnded: openEnded,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
 		};
 
 	}
 
-	var DefaultLoadingManager = new LoadingManager();
+	ConeBufferGeometry.prototype = Object.create( CylinderBufferGeometry.prototype );
+	ConeBufferGeometry.prototype.constructor = ConeBufferGeometry;
 
 	/**
-	 * @author alteredq / http://alteredqualia.com/
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 * @author Mugen87 / https://github.com/Mugen87
+	 * @author hughes
 	 */
 
-	function Loader( manager ) {
+	// CircleGeometry
 
-		this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
+	function CircleGeometry( radius, segments, thetaStart, thetaLength ) {
 
-		this.crossOrigin = 'anonymous';
-		this.path = '';
-		this.resourcePath = '';
+		Geometry.call( this );
 
-	}
+		this.type = 'CircleGeometry';
 
-	Object.assign( Loader.prototype, {
+		this.parameters = {
+			radius: radius,
+			segments: segments,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-		load: function ( /* url, onLoad, onProgress, onError */ ) {},
+		this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
+		this.mergeVertices();
 
-		loadAsync: function ( url, onProgress ) {
+	}
 
-			var scope = this;
+	CircleGeometry.prototype = Object.create( Geometry.prototype );
+	CircleGeometry.prototype.constructor = CircleGeometry;
 
-			return new Promise( function ( resolve, reject ) {
+	// CircleBufferGeometry
 
-				scope.load( url, resolve, onProgress, reject );
+	function CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) {
 
-			} );
+		BufferGeometry.call( this );
 
-		},
+		this.type = 'CircleBufferGeometry';
 
-		parse: function ( /* data */ ) {},
-
-		setCrossOrigin: function ( crossOrigin ) {
-
-			this.crossOrigin = crossOrigin;
-			return this;
-
-		},
-
-		setPath: function ( path ) {
-
-			this.path = path;
-			return this;
-
-		},
-
-		setResourcePath: function ( resourcePath ) {
-
-			this.resourcePath = resourcePath;
-			return this;
-
-		}
-
-	} );
-
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
-
-	var loading = {};
-
-	function FileLoader( manager ) {
-
-		Loader.call( this, manager );
-
-	}
-
-	FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
-
-		constructor: FileLoader,
-
-		load: function ( url, onLoad, onProgress, onError ) {
+		this.parameters = {
+			radius: radius,
+			segments: segments,
+			thetaStart: thetaStart,
+			thetaLength: thetaLength
+		};
 
-			if ( url === undefined ) { url = ''; }
+		radius = radius || 1;
+		segments = segments !== undefined ? Math.max( 3, segments ) : 8;
 
-			if ( this.path !== undefined ) { url = this.path + url; }
+		thetaStart = thetaStart !== undefined ? thetaStart : 0;
+		thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
 
-			url = this.manager.resolveURL( url );
+		// buffers
 
-			var scope = this;
+		var indices = [];
+		var vertices = [];
+		var normals = [];
+		var uvs = [];
 
-			var cached = Cache.get( url );
+		// helper variables
 
-			if ( cached !== undefined ) {
+		var i, s;
+		var vertex = new Vector3();
+		var uv = new Vector2();
 
-				scope.manager.itemStart( url );
+		// center point
 
-				setTimeout( function () {
+		vertices.push( 0, 0, 0 );
+		normals.push( 0, 0, 1 );
+		uvs.push( 0.5, 0.5 );
 
-					if ( onLoad ) { onLoad( cached ); }
+		for ( s = 0, i = 3; s <= segments; s ++, i += 3 ) {
 
-					scope.manager.itemEnd( url );
+			var segment = thetaStart + s / segments * thetaLength;
 
-				}, 0 );
+			// vertex
 
-				return cached;
+			vertex.x = radius * Math.cos( segment );
+			vertex.y = radius * Math.sin( segment );
 
-			}
+			vertices.push( vertex.x, vertex.y, vertex.z );
 
-			// Check if request is duplicate
+			// normal
 
-			if ( loading[ url ] !== undefined ) {
+			normals.push( 0, 0, 1 );
 
-				loading[ url ].push( {
+			// uvs
 
-					onLoad: onLoad,
-					onProgress: onProgress,
-					onError: onError
+			uv.x = ( vertices[ i ] / radius + 1 ) / 2;
+			uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
 
-				} );
+			uvs.push( uv.x, uv.y );
 
-				return;
+		}
 
-			}
+		// indices
 
-			// Check for data: URI
-			var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
-			var dataUriRegexResult = url.match( dataUriRegex );
+		for ( i = 1; i <= segments; i ++ ) {
 
-			// Safari can not handle Data URIs through XMLHttpRequest so process manually
-			if ( dataUriRegexResult ) {
+			indices.push( i, i + 1, 0 );
 
-				var mimeType = dataUriRegexResult[ 1 ];
-				var isBase64 = !! dataUriRegexResult[ 2 ];
-				var data = dataUriRegexResult[ 3 ];
+		}
 
-				data = decodeURIComponent( data );
+		// build geometry
 
-				if ( isBase64 ) { data = atob( data ); }
+		this.setIndex( indices );
+		this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
+		this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
 
-				try {
+	}
 
-					var response;
-					var responseType = ( this.responseType || '' ).toLowerCase();
+	CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
+	CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
 
-					switch ( responseType ) {
+	var Geometries = /*#__PURE__*/Object.freeze({
+		__proto__: null,
+		WireframeGeometry: WireframeGeometry,
+		ParametricGeometry: ParametricGeometry,
+		ParametricBufferGeometry: ParametricBufferGeometry,
+		TetrahedronGeometry: TetrahedronGeometry,
+		TetrahedronBufferGeometry: TetrahedronBufferGeometry,
+		OctahedronGeometry: OctahedronGeometry,
+		OctahedronBufferGeometry: OctahedronBufferGeometry,
+		IcosahedronGeometry: IcosahedronGeometry,
+		IcosahedronBufferGeometry: IcosahedronBufferGeometry,
+		DodecahedronGeometry: DodecahedronGeometry,
+		DodecahedronBufferGeometry: DodecahedronBufferGeometry,
+		PolyhedronGeometry: PolyhedronGeometry,
+		PolyhedronBufferGeometry: PolyhedronBufferGeometry,
+		TubeGeometry: TubeGeometry,
+		TubeBufferGeometry: TubeBufferGeometry,
+		TorusKnotGeometry: TorusKnotGeometry,
+		TorusKnotBufferGeometry: TorusKnotBufferGeometry,
+		TorusGeometry: TorusGeometry,
+		TorusBufferGeometry: TorusBufferGeometry,
+		TextGeometry: TextGeometry,
+		TextBufferGeometry: TextBufferGeometry,
+		SphereGeometry: SphereGeometry,
+		SphereBufferGeometry: SphereBufferGeometry,
+		RingGeometry: RingGeometry,
+		RingBufferGeometry: RingBufferGeometry,
+		PlaneGeometry: PlaneGeometry,
+		PlaneBufferGeometry: PlaneBufferGeometry,
+		LatheGeometry: LatheGeometry,
+		LatheBufferGeometry: LatheBufferGeometry,
+		ShapeGeometry: ShapeGeometry,
+		ShapeBufferGeometry: ShapeBufferGeometry,
+		ExtrudeGeometry: ExtrudeGeometry,
+		ExtrudeBufferGeometry: ExtrudeBufferGeometry,
+		EdgesGeometry: EdgesGeometry,
+		ConeGeometry: ConeGeometry,
+		ConeBufferGeometry: ConeBufferGeometry,
+		CylinderGeometry: CylinderGeometry,
+		CylinderBufferGeometry: CylinderBufferGeometry,
+		CircleGeometry: CircleGeometry,
+		CircleBufferGeometry: CircleBufferGeometry,
+		BoxGeometry: BoxGeometry,
+		BoxBufferGeometry: BoxBufferGeometry
+	});
 
-						case 'arraybuffer':
-						case 'blob':
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 *
+	 * parameters = {
+	 *  color: <THREE.Color>
+	 * }
+	 */
 
-							var view = new Uint8Array( data.length );
+	function ShadowMaterial( parameters ) {
 
-							for ( var i = 0; i < data.length; i ++ ) {
+		Material.call( this );
 
-								view[ i ] = data.charCodeAt( i );
+		this.type = 'ShadowMaterial';
 
-							}
+		this.color = new Color( 0x000000 );
+		this.transparent = true;
 
-							if ( responseType === 'blob' ) {
+		this.setValues( parameters );
 
-								response = new Blob( [ view.buffer ], { type: mimeType } );
+	}
 
-							} else {
+	ShadowMaterial.prototype = Object.create( Material.prototype );
+	ShadowMaterial.prototype.constructor = ShadowMaterial;
 
-								response = view.buffer;
+	ShadowMaterial.prototype.isShadowMaterial = true;
 
-							}
+	ShadowMaterial.prototype.copy = function ( source ) {
 
-							break;
+		Material.prototype.copy.call( this, source );
 
-						case 'document':
+		this.color.copy( source.color );
 
-							var parser = new DOMParser();
-							response = parser.parseFromString( data, mimeType );
+		return this;
 
-							break;
+	};
 
-						case 'json':
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-							response = JSON.parse( data );
+	function RawShaderMaterial( parameters ) {
 
-							break;
+		ShaderMaterial.call( this, parameters );
 
-						default: // 'text' or other
+		this.type = 'RawShaderMaterial';
 
-							response = data;
+	}
 
-							break;
+	RawShaderMaterial.prototype = Object.create( ShaderMaterial.prototype );
+	RawShaderMaterial.prototype.constructor = RawShaderMaterial;
 
-					}
+	RawShaderMaterial.prototype.isRawShaderMaterial = true;
 
-					// Wait for next browser tick like standard XMLHttpRequest event dispatching does
-					setTimeout( function () {
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  roughness: <float>,
+	 *  metalness: <float>,
+	 *  opacity: <float>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  lightMap: new THREE.Texture( <Image> ),
+	 *  lightMapIntensity: <float>
+	 *
+	 *  aoMap: new THREE.Texture( <Image> ),
+	 *  aoMapIntensity: <float>
+	 *
+	 *  emissive: <hex>,
+	 *  emissiveIntensity: <float>
+	 *  emissiveMap: new THREE.Texture( <Image> ),
+	 *
+	 *  bumpMap: new THREE.Texture( <Image> ),
+	 *  bumpScale: <float>,
+	 *
+	 *  normalMap: new THREE.Texture( <Image> ),
+	 *  normalMapType: THREE.TangentSpaceNormalMap,
+	 *  normalScale: <Vector2>,
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  roughnessMap: new THREE.Texture( <Image> ),
+	 *
+	 *  metalnessMap: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
+	 *  envMapIntensity: <float>
+	 *
+	 *  refractionRatio: <float>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
+	 */
 
-						if ( onLoad ) { onLoad( response ); }
+	function MeshStandardMaterial( parameters ) {
 
-						scope.manager.itemEnd( url );
+		Material.call( this );
 
-					}, 0 );
+		this.defines = { 'STANDARD': '' };
 
-				} catch ( error ) {
+		this.type = 'MeshStandardMaterial';
 
-					// Wait for next browser tick like standard XMLHttpRequest event dispatching does
-					setTimeout( function () {
+		this.color = new Color( 0xffffff ); // diffuse
+		this.roughness = 1.0;
+		this.metalness = 0.0;
 
-						if ( onError ) { onError( error ); }
+		this.map = null;
 
-						scope.manager.itemError( url );
-						scope.manager.itemEnd( url );
+		this.lightMap = null;
+		this.lightMapIntensity = 1.0;
 
-					}, 0 );
+		this.aoMap = null;
+		this.aoMapIntensity = 1.0;
 
-				}
+		this.emissive = new Color( 0x000000 );
+		this.emissiveIntensity = 1.0;
+		this.emissiveMap = null;
 
-			} else {
+		this.bumpMap = null;
+		this.bumpScale = 1;
 
-				// Initialise array for duplicate requests
+		this.normalMap = null;
+		this.normalMapType = TangentSpaceNormalMap;
+		this.normalScale = new Vector2( 1, 1 );
 
-				loading[ url ] = [];
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-				loading[ url ].push( {
+		this.roughnessMap = null;
 
-					onLoad: onLoad,
-					onProgress: onProgress,
-					onError: onError
+		this.metalnessMap = null;
 
-				} );
+		this.alphaMap = null;
 
-				var request = new XMLHttpRequest();
+		this.envMap = null;
+		this.envMapIntensity = 1.0;
 
-				request.open( 'GET', url, true );
+		this.refractionRatio = 0.98;
 
-				request.addEventListener( 'load', function ( event ) {
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
+		this.wireframeLinecap = 'round';
+		this.wireframeLinejoin = 'round';
 
-					var response = this.response;
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-					var callbacks = loading[ url ];
+		this.vertexTangents = false;
 
-					delete loading[ url ];
+		this.setValues( parameters );
 
-					if ( this.status === 200 || this.status === 0 ) {
+	}
 
-						// Some browsers return HTTP Status 0 when using non-http protocol
-						// e.g. 'file://' or 'data://'. Handle as success.
+	MeshStandardMaterial.prototype = Object.create( Material.prototype );
+	MeshStandardMaterial.prototype.constructor = MeshStandardMaterial;
 
-						if ( this.status === 0 ) { console.warn( 'THREE.FileLoader: HTTP Status 0 received.' ); }
+	MeshStandardMaterial.prototype.isMeshStandardMaterial = true;
 
-						// Add to cache only on HTTP success, so that we do not cache
-						// error response bodies as proper responses to requests.
-						Cache.add( url, response );
+	MeshStandardMaterial.prototype.copy = function ( source ) {
 
-						for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
+		Material.prototype.copy.call( this, source );
 
-							var callback = callbacks[ i ];
-							if ( callback.onLoad ) { callback.onLoad( response ); }
+		this.defines = { 'STANDARD': '' };
 
-						}
+		this.color.copy( source.color );
+		this.roughness = source.roughness;
+		this.metalness = source.metalness;
 
-						scope.manager.itemEnd( url );
+		this.map = source.map;
 
-					} else {
+		this.lightMap = source.lightMap;
+		this.lightMapIntensity = source.lightMapIntensity;
 
-						for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
+		this.aoMap = source.aoMap;
+		this.aoMapIntensity = source.aoMapIntensity;
 
-							var callback = callbacks[ i ];
-							if ( callback.onError ) { callback.onError( event ); }
+		this.emissive.copy( source.emissive );
+		this.emissiveMap = source.emissiveMap;
+		this.emissiveIntensity = source.emissiveIntensity;
 
-						}
+		this.bumpMap = source.bumpMap;
+		this.bumpScale = source.bumpScale;
 
-						scope.manager.itemError( url );
-						scope.manager.itemEnd( url );
+		this.normalMap = source.normalMap;
+		this.normalMapType = source.normalMapType;
+		this.normalScale.copy( source.normalScale );
 
-					}
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-				}, false );
+		this.roughnessMap = source.roughnessMap;
 
-				request.addEventListener( 'progress', function ( event ) {
+		this.metalnessMap = source.metalnessMap;
 
-					var callbacks = loading[ url ];
+		this.alphaMap = source.alphaMap;
 
-					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
+		this.envMap = source.envMap;
+		this.envMapIntensity = source.envMapIntensity;
 
-						var callback = callbacks[ i ];
-						if ( callback.onProgress ) { callback.onProgress( event ); }
+		this.refractionRatio = source.refractionRatio;
 
-					}
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
+		this.wireframeLinecap = source.wireframeLinecap;
+		this.wireframeLinejoin = source.wireframeLinejoin;
 
-				}, false );
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-				request.addEventListener( 'error', function ( event ) {
+		this.vertexTangents = source.vertexTangents;
 
-					var callbacks = loading[ url ];
+		return this;
 
-					delete loading[ url ];
+	};
 
-					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * parameters = {
+	 *  clearcoat: <float>,
+	 *  clearcoatMap: new THREE.Texture( <Image> ),
+	 *  clearcoatRoughness: <float>,
+	 *  clearcoatRoughnessMap: new THREE.Texture( <Image> ),
+	 *  clearcoatNormalScale: <Vector2>,
+	 *  clearcoatNormalMap: new THREE.Texture( <Image> ),
+	 *
+	 *  reflectivity: <float>,
+	 *
+	 *  sheen: <Color>,
+	 *
+	 *  transparency: <float>
+	 * }
+	 */
 
-						var callback = callbacks[ i ];
-						if ( callback.onError ) { callback.onError( event ); }
+	function MeshPhysicalMaterial( parameters ) {
 
-					}
+		MeshStandardMaterial.call( this );
 
-					scope.manager.itemError( url );
-					scope.manager.itemEnd( url );
+		this.defines = {
 
-				}, false );
+			'STANDARD': '',
+			'PHYSICAL': ''
 
-				request.addEventListener( 'abort', function ( event ) {
+		};
 
-					var callbacks = loading[ url ];
+		this.type = 'MeshPhysicalMaterial';
 
-					delete loading[ url ];
+		this.clearcoat = 0.0;
+		this.clearcoatMap = null;
+		this.clearcoatRoughness = 0.0;
+		this.clearcoatRoughnessMap = null;
+		this.clearcoatNormalScale = new Vector2( 1, 1 );
+		this.clearcoatNormalMap = null;
 
-					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
+		this.reflectivity = 0.5; // maps to F0 = 0.04
 
-						var callback = callbacks[ i ];
-						if ( callback.onError ) { callback.onError( event ); }
+		this.sheen = null; // null will disable sheen bsdf
 
-					}
+		this.transparency = 0.0;
 
-					scope.manager.itemError( url );
-					scope.manager.itemEnd( url );
+		this.setValues( parameters );
 
-				}, false );
+	}
 
-				if ( this.responseType !== undefined ) { request.responseType = this.responseType; }
-				if ( this.withCredentials !== undefined ) { request.withCredentials = this.withCredentials; }
+	MeshPhysicalMaterial.prototype = Object.create( MeshStandardMaterial.prototype );
+	MeshPhysicalMaterial.prototype.constructor = MeshPhysicalMaterial;
 
-				if ( request.overrideMimeType ) { request.overrideMimeType( this.mimeType !== undefined ? this.mimeType : 'text/plain' ); }
+	MeshPhysicalMaterial.prototype.isMeshPhysicalMaterial = true;
 
-				for ( var header in this.requestHeader ) {
+	MeshPhysicalMaterial.prototype.copy = function ( source ) {
 
-					request.setRequestHeader( header, this.requestHeader[ header ] );
+		MeshStandardMaterial.prototype.copy.call( this, source );
 
-				}
+		this.defines = {
 
-				request.send( null );
+			'STANDARD': '',
+			'PHYSICAL': ''
 
-			}
+		};
 
-			scope.manager.itemStart( url );
+		this.clearcoat = source.clearcoat;
+		this.clearcoatMap = source.clearcoatMap;
+		this.clearcoatRoughness = source.clearcoatRoughness;
+		this.clearcoatRoughnessMap = source.clearcoatRoughnessMap;
+		this.clearcoatNormalMap = source.clearcoatNormalMap;
+		this.clearcoatNormalScale.copy( source.clearcoatNormalScale );
 
-			return request;
+		this.reflectivity = source.reflectivity;
 
-		},
+		if ( source.sheen ) {
 
-		setResponseType: function ( value ) {
+			this.sheen = ( this.sheen || new Color() ).copy( source.sheen );
 
-			this.responseType = value;
-			return this;
+		} else {
 
-		},
+			this.sheen = null;
 
-		setWithCredentials: function ( value ) {
+		}
 
-			this.withCredentials = value;
-			return this;
+		this.transparency = source.transparency;
 
-		},
+		return this;
 
-		setMimeType: function ( value ) {
-
-			this.mimeType = value;
-			return this;
-
-		},
-
-		setRequestHeader: function ( value ) {
-
-			this.requestHeader = value;
-			return this;
-
-		}
-
-	} );
+	};
 
 	/**
-	 * @author bhouston / http://clara.io/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  specular: <hex>,
+	 *  shininess: <float>,
+	 *  opacity: <float>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  lightMap: new THREE.Texture( <Image> ),
+	 *  lightMapIntensity: <float>
+	 *
+	 *  aoMap: new THREE.Texture( <Image> ),
+	 *  aoMapIntensity: <float>
+	 *
+	 *  emissive: <hex>,
+	 *  emissiveIntensity: <float>
+	 *  emissiveMap: new THREE.Texture( <Image> ),
+	 *
+	 *  bumpMap: new THREE.Texture( <Image> ),
+	 *  bumpScale: <float>,
+	 *
+	 *  normalMap: new THREE.Texture( <Image> ),
+	 *  normalMapType: THREE.TangentSpaceNormalMap,
+	 *  normalScale: <Vector2>,
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  specularMap: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
+	 *  combine: THREE.MultiplyOperation,
+	 *  reflectivity: <float>,
+	 *  refractionRatio: <float>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
 	 */
 
-	function AnimationLoader( manager ) {
+	function MeshPhongMaterial( parameters ) {
 
-		Loader.call( this, manager );
+		Material.call( this );
 
-	}
+		this.type = 'MeshPhongMaterial';
 
-	AnimationLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		this.color = new Color( 0xffffff ); // diffuse
+		this.specular = new Color( 0x111111 );
+		this.shininess = 30;
 
-		constructor: AnimationLoader,
+		this.map = null;
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		this.lightMap = null;
+		this.lightMapIntensity = 1.0;
 
-			var scope = this;
+		this.aoMap = null;
+		this.aoMapIntensity = 1.0;
 
-			var loader = new FileLoader( scope.manager );
-			loader.setPath( scope.path );
-			loader.load( url, function ( text ) {
+		this.emissive = new Color( 0x000000 );
+		this.emissiveIntensity = 1.0;
+		this.emissiveMap = null;
 
-				onLoad( scope.parse( JSON.parse( text ) ) );
+		this.bumpMap = null;
+		this.bumpScale = 1;
 
-			}, onProgress, onError );
+		this.normalMap = null;
+		this.normalMapType = TangentSpaceNormalMap;
+		this.normalScale = new Vector2( 1, 1 );
 
-		},
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-		parse: function ( json ) {
+		this.specularMap = null;
 
-			var animations = [];
+		this.alphaMap = null;
 
-			for ( var i = 0; i < json.length; i ++ ) {
+		this.envMap = null;
+		this.combine = MultiplyOperation;
+		this.reflectivity = 1;
+		this.refractionRatio = 0.98;
 
-				var clip = AnimationClip.parse( json[ i ] );
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
+		this.wireframeLinecap = 'round';
+		this.wireframeLinejoin = 'round';
 
-				animations.push( clip );
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-			}
+		this.setValues( parameters );
 
-			return animations;
+	}
 
-		}
+	MeshPhongMaterial.prototype = Object.create( Material.prototype );
+	MeshPhongMaterial.prototype.constructor = MeshPhongMaterial;
 
-	} );
+	MeshPhongMaterial.prototype.isMeshPhongMaterial = true;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 *
-	 * Abstract Base class to block based textures loader (dds, pvr, ...)
-	 *
-	 * Sub classes have to implement the parse() method which will be used in load().
-	 */
+	MeshPhongMaterial.prototype.copy = function ( source ) {
 
-	function CompressedTextureLoader( manager ) {
+		Material.prototype.copy.call( this, source );
 
-		Loader.call( this, manager );
+		this.color.copy( source.color );
+		this.specular.copy( source.specular );
+		this.shininess = source.shininess;
 
-	}
+		this.map = source.map;
 
-	CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		this.lightMap = source.lightMap;
+		this.lightMapIntensity = source.lightMapIntensity;
 
-		constructor: CompressedTextureLoader,
+		this.aoMap = source.aoMap;
+		this.aoMapIntensity = source.aoMapIntensity;
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		this.emissive.copy( source.emissive );
+		this.emissiveMap = source.emissiveMap;
+		this.emissiveIntensity = source.emissiveIntensity;
 
-			var scope = this;
+		this.bumpMap = source.bumpMap;
+		this.bumpScale = source.bumpScale;
 
-			var images = [];
+		this.normalMap = source.normalMap;
+		this.normalMapType = source.normalMapType;
+		this.normalScale.copy( source.normalScale );
 
-			var texture = new CompressedTexture();
-			texture.image = images;
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-			var loader = new FileLoader( this.manager );
-			loader.setPath( this.path );
-			loader.setResponseType( 'arraybuffer' );
+		this.specularMap = source.specularMap;
 
-			function loadTexture( i ) {
+		this.alphaMap = source.alphaMap;
 
-				loader.load( url[ i ], function ( buffer ) {
+		this.envMap = source.envMap;
+		this.combine = source.combine;
+		this.reflectivity = source.reflectivity;
+		this.refractionRatio = source.refractionRatio;
 
-					var texDatas = scope.parse( buffer, true );
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
+		this.wireframeLinecap = source.wireframeLinecap;
+		this.wireframeLinejoin = source.wireframeLinejoin;
 
-					images[ i ] = {
-						width: texDatas.width,
-						height: texDatas.height,
-						format: texDatas.format,
-						mipmaps: texDatas.mipmaps
-					};
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-					loaded += 1;
+		return this;
 
-					if ( loaded === 6 ) {
+	};
 
-						if ( texDatas.mipmapCount === 1 )
-							{ texture.minFilter = LinearFilter; }
+	/**
+	 * @author takahirox / http://github.com/takahirox
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  specular: <hex>,
+	 *  shininess: <float>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *  gradientMap: new THREE.Texture( <Image> ),
+	 *
+	 *  lightMap: new THREE.Texture( <Image> ),
+	 *  lightMapIntensity: <float>
+	 *
+	 *  aoMap: new THREE.Texture( <Image> ),
+	 *  aoMapIntensity: <float>
+	 *
+	 *  emissive: <hex>,
+	 *  emissiveIntensity: <float>
+	 *  emissiveMap: new THREE.Texture( <Image> ),
+	 *
+	 *  bumpMap: new THREE.Texture( <Image> ),
+	 *  bumpScale: <float>,
+	 *
+	 *  normalMap: new THREE.Texture( <Image> ),
+	 *  normalMapType: THREE.TangentSpaceNormalMap,
+	 *  normalScale: <Vector2>,
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  specularMap: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
+	 */
 
-						texture.format = texDatas.format;
-						texture.needsUpdate = true;
+	function MeshToonMaterial( parameters ) {
 
-						if ( onLoad ) { onLoad( texture ); }
+		Material.call( this );
 
-					}
+		this.defines = { 'TOON': '' };
 
-				}, onProgress, onError );
+		this.type = 'MeshToonMaterial';
 
-			}
+		this.color = new Color( 0xffffff );
+		this.specular = new Color( 0x111111 );
+		this.shininess = 30;
 
-			if ( Array.isArray( url ) ) {
+		this.map = null;
+		this.gradientMap = null;
 
-				var loaded = 0;
+		this.lightMap = null;
+		this.lightMapIntensity = 1.0;
 
-				for ( var i = 0, il = url.length; i < il; ++ i ) {
+		this.aoMap = null;
+		this.aoMapIntensity = 1.0;
 
-					loadTexture( i );
+		this.emissive = new Color( 0x000000 );
+		this.emissiveIntensity = 1.0;
+		this.emissiveMap = null;
 
-				}
+		this.bumpMap = null;
+		this.bumpScale = 1;
 
-			} else {
+		this.normalMap = null;
+		this.normalMapType = TangentSpaceNormalMap;
+		this.normalScale = new Vector2( 1, 1 );
 
-				// compressed cubemap texture stored in a single DDS file
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-				loader.load( url, function ( buffer ) {
+		this.specularMap = null;
 
-					var texDatas = scope.parse( buffer, true );
+		this.alphaMap = null;
 
-					if ( texDatas.isCubemap ) {
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
+		this.wireframeLinecap = 'round';
+		this.wireframeLinejoin = 'round';
 
-						var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-						for ( var f = 0; f < faces; f ++ ) {
+		this.setValues( parameters );
 
-							images[ f ] = { mipmaps: [] };
+	}
 
-							for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
+	MeshToonMaterial.prototype = Object.create( Material.prototype );
+	MeshToonMaterial.prototype.constructor = MeshToonMaterial;
 
-								images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
-								images[ f ].format = texDatas.format;
-								images[ f ].width = texDatas.width;
-								images[ f ].height = texDatas.height;
+	MeshToonMaterial.prototype.isMeshToonMaterial = true;
 
-							}
+	MeshToonMaterial.prototype.copy = function ( source ) {
 
-						}
+		Material.prototype.copy.call( this, source );
 
-					} else {
+		this.color.copy( source.color );
+		this.specular.copy( source.specular );
+		this.shininess = source.shininess;
 
-						texture.image.width = texDatas.width;
-						texture.image.height = texDatas.height;
-						texture.mipmaps = texDatas.mipmaps;
+		this.map = source.map;
+		this.gradientMap = source.gradientMap;
 
-					}
+		this.lightMap = source.lightMap;
+		this.lightMapIntensity = source.lightMapIntensity;
 
-					if ( texDatas.mipmapCount === 1 ) {
+		this.aoMap = source.aoMap;
+		this.aoMapIntensity = source.aoMapIntensity;
 
-						texture.minFilter = LinearFilter;
+		this.emissive.copy( source.emissive );
+		this.emissiveMap = source.emissiveMap;
+		this.emissiveIntensity = source.emissiveIntensity;
 
-					}
+		this.bumpMap = source.bumpMap;
+		this.bumpScale = source.bumpScale;
 
-					texture.format = texDatas.format;
-					texture.needsUpdate = true;
+		this.normalMap = source.normalMap;
+		this.normalMapType = source.normalMapType;
+		this.normalScale.copy( source.normalScale );
 
-					if ( onLoad ) { onLoad( texture ); }
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-				}, onProgress, onError );
+		this.specularMap = source.specularMap;
 
-			}
+		this.alphaMap = source.alphaMap;
 
-			return texture;
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
+		this.wireframeLinecap = source.wireframeLinecap;
+		this.wireframeLinejoin = source.wireframeLinejoin;
 
-		}
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-	} );
+		return this;
+
+	};
 
 	/**
-	 * @author Nikos M. / https://github.com/foo123/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author WestLangley / http://github.com/WestLangley
 	 *
-	 * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
+	 * parameters = {
+	 *  opacity: <float>,
 	 *
-	 * Sub classes have to implement the parse() method which will be used in load().
+	 *  bumpMap: new THREE.Texture( <Image> ),
+	 *  bumpScale: <float>,
+	 *
+	 *  normalMap: new THREE.Texture( <Image> ),
+	 *  normalMapType: THREE.TangentSpaceNormalMap,
+	 *  normalScale: <Vector2>,
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
 	 */
 
-	function DataTextureLoader( manager ) {
-
-		Loader.call( this, manager );
-
-	}
+	function MeshNormalMaterial( parameters ) {
 
-	DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		Material.call( this );
 
-		constructor: DataTextureLoader,
+		this.type = 'MeshNormalMaterial';
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		this.bumpMap = null;
+		this.bumpScale = 1;
 
-			var scope = this;
+		this.normalMap = null;
+		this.normalMapType = TangentSpaceNormalMap;
+		this.normalScale = new Vector2( 1, 1 );
 
-			var texture = new DataTexture();
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-			var loader = new FileLoader( this.manager );
-			loader.setResponseType( 'arraybuffer' );
-			loader.setPath( this.path );
-			loader.load( url, function ( buffer ) {
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
 
-				var texData = scope.parse( buffer );
+		this.fog = false;
 
-				if ( ! texData ) { return; }
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-				if ( texData.image !== undefined ) {
+		this.setValues( parameters );
 
-					texture.image = texData.image;
+	}
 
-				} else if ( texData.data !== undefined ) {
+	MeshNormalMaterial.prototype = Object.create( Material.prototype );
+	MeshNormalMaterial.prototype.constructor = MeshNormalMaterial;
 
-					texture.image.width = texData.width;
-					texture.image.height = texData.height;
-					texture.image.data = texData.data;
+	MeshNormalMaterial.prototype.isMeshNormalMaterial = true;
 
-				}
+	MeshNormalMaterial.prototype.copy = function ( source ) {
 
-				texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
-				texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
+		Material.prototype.copy.call( this, source );
 
-				texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
-				texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
+		this.bumpMap = source.bumpMap;
+		this.bumpScale = source.bumpScale;
 
-				texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
+		this.normalMap = source.normalMap;
+		this.normalMapType = source.normalMapType;
+		this.normalScale.copy( source.normalScale );
 
-				if ( texData.format !== undefined ) {
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-					texture.format = texData.format;
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
 
-				}
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-				if ( texData.type !== undefined ) {
+		return this;
 
-					texture.type = texData.type;
+	};
 
-				}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  lightMap: new THREE.Texture( <Image> ),
+	 *  lightMapIntensity: <float>
+	 *
+	 *  aoMap: new THREE.Texture( <Image> ),
+	 *  aoMapIntensity: <float>
+	 *
+	 *  emissive: <hex>,
+	 *  emissiveIntensity: <float>
+	 *  emissiveMap: new THREE.Texture( <Image> ),
+	 *
+	 *  specularMap: new THREE.Texture( <Image> ),
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
+	 *  combine: THREE.Multiply,
+	 *  reflectivity: <float>,
+	 *  refractionRatio: <float>,
+	 *
+	 *  wireframe: <boolean>,
+	 *  wireframeLinewidth: <float>,
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
+	 */
 
-				if ( texData.mipmaps !== undefined ) {
+	function MeshLambertMaterial( parameters ) {
 
-					texture.mipmaps = texData.mipmaps;
-					texture.minFilter = LinearMipmapLinearFilter; // presumably...
+		Material.call( this );
 
-				}
+		this.type = 'MeshLambertMaterial';
 
-				if ( texData.mipmapCount === 1 ) {
+		this.color = new Color( 0xffffff ); // diffuse
 
-					texture.minFilter = LinearFilter;
+		this.map = null;
 
-				}
+		this.lightMap = null;
+		this.lightMapIntensity = 1.0;
 
-				texture.needsUpdate = true;
+		this.aoMap = null;
+		this.aoMapIntensity = 1.0;
 
-				if ( onLoad ) { onLoad( texture, texData ); }
+		this.emissive = new Color( 0x000000 );
+		this.emissiveIntensity = 1.0;
+		this.emissiveMap = null;
 
-			}, onProgress, onError );
+		this.specularMap = null;
 
+		this.alphaMap = null;
 
-			return texture;
-
-		}
-
-	} );
+		this.envMap = null;
+		this.combine = MultiplyOperation;
+		this.reflectivity = 1;
+		this.refractionRatio = 0.98;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		this.wireframe = false;
+		this.wireframeLinewidth = 1;
+		this.wireframeLinecap = 'round';
+		this.wireframeLinejoin = 'round';
 
-	function ImageLoader( manager ) {
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-		Loader.call( this, manager );
+		this.setValues( parameters );
 
 	}
 
-	ImageLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
-
-		constructor: ImageLoader,
-
-		load: function ( url, onLoad, onProgress, onError ) {
+	MeshLambertMaterial.prototype = Object.create( Material.prototype );
+	MeshLambertMaterial.prototype.constructor = MeshLambertMaterial;
 
-			if ( this.path !== undefined ) { url = this.path + url; }
+	MeshLambertMaterial.prototype.isMeshLambertMaterial = true;
 
-			url = this.manager.resolveURL( url );
+	MeshLambertMaterial.prototype.copy = function ( source ) {
 
-			var scope = this;
+		Material.prototype.copy.call( this, source );
 
-			var cached = Cache.get( url );
+		this.color.copy( source.color );
 
-			if ( cached !== undefined ) {
+		this.map = source.map;
 
-				scope.manager.itemStart( url );
+		this.lightMap = source.lightMap;
+		this.lightMapIntensity = source.lightMapIntensity;
 
-				setTimeout( function () {
+		this.aoMap = source.aoMap;
+		this.aoMapIntensity = source.aoMapIntensity;
 
-					if ( onLoad ) { onLoad( cached ); }
+		this.emissive.copy( source.emissive );
+		this.emissiveMap = source.emissiveMap;
+		this.emissiveIntensity = source.emissiveIntensity;
 
-					scope.manager.itemEnd( url );
+		this.specularMap = source.specularMap;
 
-				}, 0 );
+		this.alphaMap = source.alphaMap;
 
-				return cached;
+		this.envMap = source.envMap;
+		this.combine = source.combine;
+		this.reflectivity = source.reflectivity;
+		this.refractionRatio = source.refractionRatio;
 
-			}
+		this.wireframe = source.wireframe;
+		this.wireframeLinewidth = source.wireframeLinewidth;
+		this.wireframeLinecap = source.wireframeLinecap;
+		this.wireframeLinejoin = source.wireframeLinejoin;
 
-			var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-			function onImageLoad() {
+		return this;
 
-				image.removeEventListener( 'load', onImageLoad, false );
-				image.removeEventListener( 'error', onImageError, false );
+	};
 
-				Cache.add( url, this );
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *
+	 *  matcap: new THREE.Texture( <Image> ),
+	 *
+	 *  map: new THREE.Texture( <Image> ),
+	 *
+	 *  bumpMap: new THREE.Texture( <Image> ),
+	 *  bumpScale: <float>,
+	 *
+	 *  normalMap: new THREE.Texture( <Image> ),
+	 *  normalMapType: THREE.TangentSpaceNormalMap,
+	 *  normalScale: <Vector2>,
+	 *
+	 *  displacementMap: new THREE.Texture( <Image> ),
+	 *  displacementScale: <float>,
+	 *  displacementBias: <float>,
+	 *
+	 *  alphaMap: new THREE.Texture( <Image> ),
+	 *
+	 *  skinning: <bool>,
+	 *  morphTargets: <bool>,
+	 *  morphNormals: <bool>
+	 * }
+	 */
 
-				if ( onLoad ) { onLoad( this ); }
+	function MeshMatcapMaterial( parameters ) {
 
-				scope.manager.itemEnd( url );
+		Material.call( this );
 
-			}
+		this.defines = { 'MATCAP': '' };
 
-			function onImageError( event ) {
+		this.type = 'MeshMatcapMaterial';
 
-				image.removeEventListener( 'load', onImageLoad, false );
-				image.removeEventListener( 'error', onImageError, false );
+		this.color = new Color( 0xffffff ); // diffuse
 
-				if ( onError ) { onError( event ); }
+		this.matcap = null;
 
-				scope.manager.itemError( url );
-				scope.manager.itemEnd( url );
+		this.map = null;
 
-			}
+		this.bumpMap = null;
+		this.bumpScale = 1;
 
-			image.addEventListener( 'load', onImageLoad, false );
-			image.addEventListener( 'error', onImageError, false );
+		this.normalMap = null;
+		this.normalMapType = TangentSpaceNormalMap;
+		this.normalScale = new Vector2( 1, 1 );
 
-			if ( url.substr( 0, 5 ) !== 'data:' ) {
+		this.displacementMap = null;
+		this.displacementScale = 1;
+		this.displacementBias = 0;
 
-				if ( this.crossOrigin !== undefined ) { image.crossOrigin = this.crossOrigin; }
+		this.alphaMap = null;
 
-			}
+		this.skinning = false;
+		this.morphTargets = false;
+		this.morphNormals = false;
 
-			scope.manager.itemStart( url );
+		this.setValues( parameters );
 
-			image.src = url;
+	}
 
-			return image;
+	MeshMatcapMaterial.prototype = Object.create( Material.prototype );
+	MeshMatcapMaterial.prototype.constructor = MeshMatcapMaterial;
 
-		}
+	MeshMatcapMaterial.prototype.isMeshMatcapMaterial = true;
 
-	} );
+	MeshMatcapMaterial.prototype.copy = function ( source ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		Material.prototype.copy.call( this, source );
 
+		this.defines = { 'MATCAP': '' };
 
-	function CubeTextureLoader( manager ) {
+		this.color.copy( source.color );
 
-		Loader.call( this, manager );
+		this.matcap = source.matcap;
 
-	}
+		this.map = source.map;
 
-	CubeTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		this.bumpMap = source.bumpMap;
+		this.bumpScale = source.bumpScale;
 
-		constructor: CubeTextureLoader,
+		this.normalMap = source.normalMap;
+		this.normalMapType = source.normalMapType;
+		this.normalScale.copy( source.normalScale );
 
-		load: function ( urls, onLoad, onProgress, onError ) {
+		this.displacementMap = source.displacementMap;
+		this.displacementScale = source.displacementScale;
+		this.displacementBias = source.displacementBias;
 
-			var texture = new CubeTexture();
+		this.alphaMap = source.alphaMap;
 
-			var loader = new ImageLoader( this.manager );
-			loader.setCrossOrigin( this.crossOrigin );
-			loader.setPath( this.path );
+		this.skinning = source.skinning;
+		this.morphTargets = source.morphTargets;
+		this.morphNormals = source.morphNormals;
 
-			var loaded = 0;
+		return this;
 
-			function loadTexture( i ) {
+	};
 
-				loader.load( urls[ i ], function ( image ) {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 *
+	 * parameters = {
+	 *  color: <hex>,
+	 *  opacity: <float>,
+	 *
+	 *  linewidth: <float>,
+	 *
+	 *  scale: <float>,
+	 *  dashSize: <float>,
+	 *  gapSize: <float>
+	 * }
+	 */
 
-					texture.images[ i ] = image;
+	function LineDashedMaterial( parameters ) {
 
-					loaded ++;
+		LineBasicMaterial.call( this );
 
-					if ( loaded === 6 ) {
+		this.type = 'LineDashedMaterial';
 
-						texture.needsUpdate = true;
+		this.scale = 1;
+		this.dashSize = 3;
+		this.gapSize = 1;
 
-						if ( onLoad ) { onLoad( texture ); }
+		this.setValues( parameters );
 
-					}
+	}
 
-				}, undefined, onError );
+	LineDashedMaterial.prototype = Object.create( LineBasicMaterial.prototype );
+	LineDashedMaterial.prototype.constructor = LineDashedMaterial;
 
-			}
+	LineDashedMaterial.prototype.isLineDashedMaterial = true;
 
-			for ( var i = 0; i < urls.length; ++ i ) {
+	LineDashedMaterial.prototype.copy = function ( source ) {
 
-				loadTexture( i );
+		LineBasicMaterial.prototype.copy.call( this, source );
 
-			}
+		this.scale = source.scale;
+		this.dashSize = source.dashSize;
+		this.gapSize = source.gapSize;
 
-			return texture;
+		return this;
 
-		}
+	};
 
-	} );
+	var Materials = /*#__PURE__*/Object.freeze({
+		__proto__: null,
+		ShadowMaterial: ShadowMaterial,
+		SpriteMaterial: SpriteMaterial,
+		RawShaderMaterial: RawShaderMaterial,
+		ShaderMaterial: ShaderMaterial,
+		PointsMaterial: PointsMaterial,
+		MeshPhysicalMaterial: MeshPhysicalMaterial,
+		MeshStandardMaterial: MeshStandardMaterial,
+		MeshPhongMaterial: MeshPhongMaterial,
+		MeshToonMaterial: MeshToonMaterial,
+		MeshNormalMaterial: MeshNormalMaterial,
+		MeshLambertMaterial: MeshLambertMaterial,
+		MeshDepthMaterial: MeshDepthMaterial,
+		MeshDistanceMaterial: MeshDistanceMaterial,
+		MeshBasicMaterial: MeshBasicMaterial,
+		MeshMatcapMaterial: MeshMatcapMaterial,
+		LineDashedMaterial: LineDashedMaterial,
+		LineBasicMaterial: LineBasicMaterial,
+		Material: Material
+	});
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author tschw
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
 	 */
 
-	function TextureLoader( manager ) {
-
-		Loader.call( this, manager );
+	var AnimationUtils = {
 
-	}
+		// same as Array.prototype.slice, but also works on typed arrays
+		arraySlice: function ( array, from, to ) {
 
-	TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+			if ( AnimationUtils.isTypedArray( array ) ) {
 
-		constructor: TextureLoader,
+				// in ios9 array.subarray(from, undefined) will return empty array
+				// but array.subarray(from) or array.subarray(from, len) is correct
+				return new array.constructor( array.subarray( from, to !== undefined ? to : array.length ) );
 
-		load: function ( url, onLoad, onProgress, onError ) {
+			}
 
-			var texture = new Texture();
+			return array.slice( from, to );
 
-			var loader = new ImageLoader( this.manager );
-			loader.setCrossOrigin( this.crossOrigin );
-			loader.setPath( this.path );
+		},
 
-			loader.load( url, function ( image ) {
+		// converts an array to a specific type
+		convertArray: function ( array, type, forceClone ) {
 
-				texture.image = image;
+			if ( ! array || // let 'undefined' and 'null' pass
+				! forceClone && array.constructor === type ) { return array; }
 
-				// JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
-				var isJPEG = url.search( /\.jpe?g($|\?)/i ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;
+			if ( typeof type.BYTES_PER_ELEMENT === 'number' ) {
 
-				texture.format = isJPEG ? RGBFormat : RGBAFormat;
-				texture.needsUpdate = true;
+				return new type( array ); // create typed array
 
-				if ( onLoad !== undefined ) {
+			}
 
-					onLoad( texture );
+			return Array.prototype.slice.call( array ); // create Array
 
-				}
+		},
 
-			}, onProgress, onError );
+		isTypedArray: function ( object ) {
 
-			return texture;
+			return ArrayBuffer.isView( object ) &&
+				! ( object instanceof DataView );
 
-		}
+		},
 
-	} );
+		// returns an array by which times and values can be sorted
+		getKeyframeOrder: function ( times ) {
 
-	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * Extensible curve object
-	 *
-	 * Some common of curve methods:
-	 * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget )
-	 * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget )
-	 * .getPoints(), .getSpacedPoints()
-	 * .getLength()
-	 * .updateArcLengths()
-	 *
-	 * This following curves inherit from THREE.Curve:
-	 *
-	 * -- 2D curves --
-	 * THREE.ArcCurve
-	 * THREE.CubicBezierCurve
-	 * THREE.EllipseCurve
-	 * THREE.LineCurve
-	 * THREE.QuadraticBezierCurve
-	 * THREE.SplineCurve
-	 *
-	 * -- 3D curves --
-	 * THREE.CatmullRomCurve3
-	 * THREE.CubicBezierCurve3
-	 * THREE.LineCurve3
-	 * THREE.QuadraticBezierCurve3
-	 *
-	 * A series of curves can be represented as a THREE.CurvePath.
-	 *
-	 **/
+			function compareTime( i, j ) {
 
-	/**************************************************************
-	 *	Abstract Curve base class
-	 **************************************************************/
+				return times[ i ] - times[ j ];
 
-	function Curve() {
+			}
 
-		this.type = 'Curve';
+			var n = times.length;
+			var result = new Array( n );
+			for ( var i = 0; i !== n; ++ i ) { result[ i ] = i; }
 
-		this.arcLengthDivisions = 200;
+			result.sort( compareTime );
 
-	}
+			return result;
 
-	Object.assign( Curve.prototype, {
+		},
 
-		// Virtual base class method to overwrite and implement in subclasses
-		//	- t [0 .. 1]
+		// uses the array previously returned by 'getKeyframeOrder' to sort data
+		sortedArray: function ( values, stride, order ) {
 
-		getPoint: function ( /* t, optionalTarget */ ) {
+			var nValues = values.length;
+			var result = new values.constructor( nValues );
 
-			console.warn( 'THREE.Curve: .getPoint() not implemented.' );
-			return null;
+			for ( var i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) {
 
-		},
+				var srcOffset = order[ i ] * stride;
 
-		// Get point at relative position in curve according to arc length
-		// - u [0 .. 1]
+				for ( var j = 0; j !== stride; ++ j ) {
 
-		getPointAt: function ( u, optionalTarget ) {
+					result[ dstOffset ++ ] = values[ srcOffset + j ];
 
-			var t = this.getUtoTmapping( u );
-			return this.getPoint( t, optionalTarget );
+				}
 
-		},
+			}
 
-		// Get sequence of points using getPoint( t )
+			return result;
 
-		getPoints: function ( divisions ) {
+		},
 
-			if ( divisions === undefined ) { divisions = 5; }
+		// function for parsing AOS keyframe formats
+		flattenJSON: function ( jsonKeys, times, values, valuePropertyName ) {
 
-			var points = [];
+			var i = 1, key = jsonKeys[ 0 ];
 
-			for ( var d = 0; d <= divisions; d ++ ) {
+			while ( key !== undefined && key[ valuePropertyName ] === undefined ) {
 
-				points.push( this.getPoint( d / divisions ) );
+				key = jsonKeys[ i ++ ];
 
 			}
 
-			return points;
+			if ( key === undefined ) { return; } // no data
 
-		},
+			var value = key[ valuePropertyName ];
+			if ( value === undefined ) { return; } // no data
 
-		// Get sequence of points using getPointAt( u )
+			if ( Array.isArray( value ) ) {
 
-		getSpacedPoints: function ( divisions ) {
+				do {
 
-			if ( divisions === undefined ) { divisions = 5; }
+					value = key[ valuePropertyName ];
 
-			var points = [];
+					if ( value !== undefined ) {
 
-			for ( var d = 0; d <= divisions; d ++ ) {
+						times.push( key.time );
+						values.push.apply( values, value ); // push all elements
 
-				points.push( this.getPointAt( d / divisions ) );
+					}
 
-			}
+					key = jsonKeys[ i ++ ];
 
-			return points;
+				} while ( key !== undefined );
 
-		},
+			} else if ( value.toArray !== undefined ) {
 
-		// Get total curve arc length
+				// ...assume THREE.Math-ish
 
-		getLength: function () {
+				do {
 
-			var lengths = this.getLengths();
-			return lengths[ lengths.length - 1 ];
+					value = key[ valuePropertyName ];
 
-		},
+					if ( value !== undefined ) {
 
-		// Get list of cumulative segment lengths
+						times.push( key.time );
+						value.toArray( values, values.length );
 
-		getLengths: function ( divisions ) {
+					}
 
-			if ( divisions === undefined ) { divisions = this.arcLengthDivisions; }
+					key = jsonKeys[ i ++ ];
 
-			if ( this.cacheArcLengths &&
-				( this.cacheArcLengths.length === divisions + 1 ) &&
-				! this.needsUpdate ) {
+				} while ( key !== undefined );
 
-				return this.cacheArcLengths;
+			} else {
 
-			}
+				// otherwise push as-is
 
-			this.needsUpdate = false;
+				do {
 
-			var cache = [];
-			var current, last = this.getPoint( 0 );
-			var p, sum = 0;
+					value = key[ valuePropertyName ];
 
-			cache.push( 0 );
+					if ( value !== undefined ) {
 
-			for ( p = 1; p <= divisions; p ++ ) {
+						times.push( key.time );
+						values.push( value );
 
-				current = this.getPoint( p / divisions );
-				sum += current.distanceTo( last );
-				cache.push( sum );
-				last = current;
+					}
 
-			}
+					key = jsonKeys[ i ++ ];
 
-			this.cacheArcLengths = cache;
+				} while ( key !== undefined );
 
-			return cache; // { sums: cache, sum: sum }; Sum is in the last element.
+			}
 
 		},
 
-		updateArcLengths: function () {
+		subclip: function ( sourceClip, name, startFrame, endFrame, fps ) {
 
-			this.needsUpdate = true;
-			this.getLengths();
+			fps = fps || 30;
 
-		},
+			var clip = sourceClip.clone();
 
-		// Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
+			clip.name = name;
 
-		getUtoTmapping: function ( u, distance ) {
+			var tracks = [];
 
-			var arcLengths = this.getLengths();
+			for ( var i = 0; i < clip.tracks.length; ++ i ) {
 
-			var i = 0, il = arcLengths.length;
+				var track = clip.tracks[ i ];
+				var valueSize = track.getValueSize();
 
-			var targetArcLength; // The targeted u distance value to get
+				var times = [];
+				var values = [];
 
-			if ( distance ) {
+				for ( var j = 0; j < track.times.length; ++ j ) {
 
-				targetArcLength = distance;
+					var frame = track.times[ j ] * fps;
 
-			} else {
+					if ( frame < startFrame || frame >= endFrame ) { continue; }
 
-				targetArcLength = u * arcLengths[ il - 1 ];
+					times.push( track.times[ j ] );
 
-			}
+					for ( var k = 0; k < valueSize; ++ k ) {
 
-			// binary search for the index with largest value smaller than target u distance
+						values.push( track.values[ j * valueSize + k ] );
 
-			var low = 0, high = il - 1, comparison;
+					}
 
-			while ( low <= high ) {
+				}
 
-				i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
+				if ( times.length === 0 ) { continue; }
 
-				comparison = arcLengths[ i ] - targetArcLength;
+				track.times = AnimationUtils.convertArray( times, track.times.constructor );
+				track.values = AnimationUtils.convertArray( values, track.values.constructor );
 
-				if ( comparison < 0 ) {
+				tracks.push( track );
 
-					low = i + 1;
+			}
 
-				} else if ( comparison > 0 ) {
+			clip.tracks = tracks;
 
-					high = i - 1;
+			// find minimum .times value across all tracks in the trimmed clip
 
-				} else {
+			var minStartTime = Infinity;
 
-					high = i;
-					break;
+			for ( var i = 0; i < clip.tracks.length; ++ i ) {
 
-					// DONE
+				if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) {
+
+					minStartTime = clip.tracks[ i ].times[ 0 ];
 
 				}
 
 			}
 
-			i = high;
+			// shift all tracks such that clip begins at t=0
 
-			if ( arcLengths[ i ] === targetArcLength ) {
+			for ( var i = 0; i < clip.tracks.length; ++ i ) {
 
-				return i / ( il - 1 );
+				clip.tracks[ i ].shift( - 1 * minStartTime );
 
 			}
 
-			// we could get finer grain at lengths, or use simple interpolation between two points
-
-			var lengthBefore = arcLengths[ i ];
-			var lengthAfter = arcLengths[ i + 1 ];
-
-			var segmentLength = lengthAfter - lengthBefore;
+			clip.resetDuration();
 
-			// determine where we are between the 'before' and 'after' points
+			return clip;
 
-			var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
+		},
 
-			// add that fractional amount to t
+		makeClipAdditive: function ( targetClip, referenceFrame, referenceClip, fps ) {
 
-			var t = ( i + segmentFraction ) / ( il - 1 );
+			if ( referenceFrame === undefined ) { referenceFrame = 0; }
+			if ( referenceClip === undefined ) { referenceClip = targetClip; }
+			if ( fps === undefined || fps <= 0 ) { fps = 30; }
 
-			return t;
+			var numTracks = targetClip.tracks.length;
+			var referenceTime = referenceFrame / fps;
 
-		},
+			// Make each track's values relative to the values at the reference frame
+			for ( var i = 0; i < numTracks; ++ i ) {
 
-		// Returns a unit vector tangent at t
-		// In case any sub curve does not implement its tangent derivation,
-		// 2 points a small delta apart will be used to find its gradient
-		// which seems to give a reasonable approximation
+				var referenceTrack = referenceClip.tracks[ i ];
+				var referenceTrackType = referenceTrack.ValueTypeName;
 
-		getTangent: function ( t, optionalTarget ) {
+				// Skip this track if it's non-numeric
+				if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) { continue; }
 
-			var delta = 0.0001;
-			var t1 = t - delta;
-			var t2 = t + delta;
+				// Find the track in the target clip whose name and type matches the reference track
+				var targetTrack = targetClip.tracks.find( function ( track ) {
 
-			// Capping in case of danger
+					return track.name === referenceTrack.name
+					&& track.ValueTypeName === referenceTrackType;
 
-			if ( t1 < 0 ) { t1 = 0; }
-			if ( t2 > 1 ) { t2 = 1; }
+				} );
 
-			var pt1 = this.getPoint( t1 );
-			var pt2 = this.getPoint( t2 );
+				if ( targetTrack === undefined ) { continue; }
 
-			var tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() );
+				var valueSize = referenceTrack.getValueSize();
+				var lastIndex = referenceTrack.times.length - 1;
+				var referenceValue;
 
-			tangent.copy( pt2 ).sub( pt1 ).normalize();
+				// Find the value to subtract out of the track
+				if ( referenceTime <= referenceTrack.times[ 0 ] ) {
 
-			return tangent;
+					// Reference frame is earlier than the first keyframe, so just use the first keyframe
+					referenceValue = AnimationUtils.arraySlice( referenceTrack.values, 0, referenceTrack.valueSize );
 
-		},
+				} else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) {
 
-		getTangentAt: function ( u, optionalTarget ) {
+					// Reference frame is after the last keyframe, so just use the last keyframe
+					var startIndex = lastIndex * valueSize;
+					referenceValue = AnimationUtils.arraySlice( referenceTrack.values, startIndex );
 
-			var t = this.getUtoTmapping( u );
-			return this.getTangent( t, optionalTarget );
+				} else {
 
-		},
+					// Interpolate to the reference value
+					var interpolant = referenceTrack.createInterpolant();
+					interpolant.evaluate( referenceTime );
+					referenceValue = interpolant.resultBuffer;
 
-		computeFrenetFrames: function ( segments, closed ) {
+				}
 
-			// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
+				// Conjugate the quaternion
+				if ( referenceTrackType === 'quaternion' ) {
 
-			var normal = new Vector3();
+					var referenceQuat = new Quaternion(
+						referenceValue[ 0 ],
+						referenceValue[ 1 ],
+						referenceValue[ 2 ],
+						referenceValue[ 3 ]
+					).normalize().conjugate();
+					referenceQuat.toArray( referenceValue );
 
-			var tangents = [];
-			var normals = [];
-			var binormals = [];
+				}
 
-			var vec = new Vector3();
-			var mat = new Matrix4();
+				// Subtract the reference value from all of the track values
 
-			var i, u, theta;
+				var numTimes = targetTrack.times.length;
+				for ( var j = 0; j < numTimes; ++ j ) {
 
-			// compute the tangent vectors for each segment on the curve
+					var valueStart = j * valueSize;
 
-			for ( i = 0; i <= segments; i ++ ) {
+					if ( referenceTrackType === 'quaternion' ) {
 
-				u = i / segments;
+						// Multiply the conjugate for quaternion track types
+						Quaternion.multiplyQuaternionsFlat(
+							targetTrack.values,
+							valueStart,
+							referenceValue,
+							0,
+							targetTrack.values,
+							valueStart
+						);
 
-				tangents[ i ] = this.getTangentAt( u, new Vector3() );
-				tangents[ i ].normalize();
+					} else {
 
-			}
+						// Subtract each value for all other numeric track types
+						for ( var k = 0; k < valueSize; ++ k ) {
 
-			// select an initial normal vector perpendicular to the first tangent vector,
-			// and in the direction of the minimum tangent xyz component
+							targetTrack.values[ valueStart + k ] -= referenceValue[ k ];
 
-			normals[ 0 ] = new Vector3();
-			binormals[ 0 ] = new Vector3();
-			var min = Number.MAX_VALUE;
-			var tx = Math.abs( tangents[ 0 ].x );
-			var ty = Math.abs( tangents[ 0 ].y );
-			var tz = Math.abs( tangents[ 0 ].z );
+						}
 
-			if ( tx <= min ) {
+					}
 
-				min = tx;
-				normal.set( 1, 0, 0 );
+				}
 
 			}
 
-			if ( ty <= min ) {
+			targetClip.blendMode = AdditiveAnimationBlendMode;
 
-				min = ty;
-				normal.set( 0, 1, 0 );
+			return targetClip;
 
-			}
+		}
 
-			if ( tz <= min ) {
+	};
 
-				normal.set( 0, 0, 1 );
+	/**
+	 * Abstract base class of interpolants over parametric samples.
+	 *
+	 * The parameter domain is one dimensional, typically the time or a path
+	 * along a curve defined by the data.
+	 *
+	 * The sample values can have any dimensionality and derived classes may
+	 * apply special interpretations to the data.
+	 *
+	 * This class provides the interval seek in a Template Method, deferring
+	 * the actual interpolation to derived classes.
+	 *
+	 * Time complexity is O(1) for linear access crossing at most two points
+	 * and O(log N) for random access, where N is the number of positions.
+	 *
+	 * References:
+	 *
+	 * 		http://www.oodesign.com/template-method-pattern.html
+	 *
+	 * @author tschw
+	 */
 
-			}
+	function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-			vec.crossVectors( tangents[ 0 ], normal ).normalize();
+		this.parameterPositions = parameterPositions;
+		this._cachedIndex = 0;
 
-			normals[ 0 ].crossVectors( tangents[ 0 ], vec );
-			binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
+		this.resultBuffer = resultBuffer !== undefined ?
+			resultBuffer : new sampleValues.constructor( sampleSize );
+		this.sampleValues = sampleValues;
+		this.valueSize = sampleSize;
 
+	}
 
-			// compute the slowly-varying normal and binormal vectors for each segment on the curve
+	Object.assign( Interpolant.prototype, {
 
-			for ( i = 1; i <= segments; i ++ ) {
+		evaluate: function ( t ) {
 
-				normals[ i ] = normals[ i - 1 ].clone();
+			var pp = this.parameterPositions,
+				i1 = this._cachedIndex,
 
-				binormals[ i ] = binormals[ i - 1 ].clone();
+				t1 = pp[ i1 ],
+				t0 = pp[ i1 - 1 ];
 
-				vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
+			validate_interval: {
 
-				if ( vec.length() > Number.EPSILON ) {
+				seek: {
 
-					vec.normalize();
+					var right;
 
-					theta = Math.acos( MathUtils.clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
+					linear_scan: {
 
-					normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
+						//- See http://jsperf.com/comparison-to-undefined/3
+						//- slower code:
+						//-
+						//- 				if ( t >= t1 || t1 === undefined ) {
+						forward_scan: if ( ! ( t < t1 ) ) {
 
-				}
+							for ( var giveUpAt = i1 + 2; ; ) {
 
-				binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
+								if ( t1 === undefined ) {
 
-			}
+									if ( t < t0 ) { break forward_scan; }
 
-			// if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
+									// after end
 
-			if ( closed === true ) {
+									i1 = pp.length;
+									this._cachedIndex = i1;
+									return this.afterEnd_( i1 - 1, t, t0 );
 
-				theta = Math.acos( MathUtils.clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
-				theta /= segments;
+								}
 
-				if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
+								if ( i1 === giveUpAt ) { break; } // this loop
 
-					theta = - theta;
+								t0 = t1;
+								t1 = pp[ ++ i1 ];
 
-				}
+								if ( t < t1 ) {
 
-				for ( i = 1; i <= segments; i ++ ) {
+									// we have arrived at the sought interval
+									break seek;
 
-					// twist a little...
-					normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
-					binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
+								}
 
-				}
+							}
 
-			}
+							// prepare binary search on the right side of the index
+							right = pp.length;
+							break linear_scan;
 
-			return {
-				tangents: tangents,
-				normals: normals,
-				binormals: binormals
-			};
+						}
 
-		},
+						//- slower code:
+						//-					if ( t < t0 || t0 === undefined ) {
+						if ( ! ( t >= t0 ) ) {
 
-		clone: function () {
+							// looping?
 
-			return new this.constructor().copy( this );
+							var t1global = pp[ 1 ];
 
-		},
+							if ( t < t1global ) {
 
-		copy: function ( source ) {
+								i1 = 2; // + 1, using the scan for the details
+								t0 = t1global;
 
-			this.arcLengthDivisions = source.arcLengthDivisions;
+							}
 
-			return this;
+							// linear reverse scan
 
-		},
+							for ( var giveUpAt = i1 - 2; ; ) {
 
-		toJSON: function () {
+								if ( t0 === undefined ) {
 
-			var data = {
-				metadata: {
-					version: 4.5,
-					type: 'Curve',
-					generator: 'Curve.toJSON'
-				}
-			};
+									// before start
 
-			data.arcLengthDivisions = this.arcLengthDivisions;
-			data.type = this.type;
+									this._cachedIndex = 0;
+									return this.beforeStart_( 0, t, t1 );
 
-			return data;
+								}
 
-		},
+								if ( i1 === giveUpAt ) { break; } // this loop
 
-		fromJSON: function ( json ) {
+								t1 = t0;
+								t0 = pp[ -- i1 - 1 ];
 
-			this.arcLengthDivisions = json.arcLengthDivisions;
+								if ( t >= t0 ) {
 
-			return this;
+									// we have arrived at the sought interval
+									break seek;
 
-		}
+								}
 
-	} );
+							}
 
-	function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
+							// prepare binary search on the left side of the index
+							right = i1;
+							i1 = 0;
+							break linear_scan;
 
-		Curve.call( this );
+						}
 
-		this.type = 'EllipseCurve';
+						// the interval is valid
 
-		this.aX = aX || 0;
-		this.aY = aY || 0;
+						break validate_interval;
 
-		this.xRadius = xRadius || 1;
-		this.yRadius = yRadius || 1;
+					} // linear scan
 
-		this.aStartAngle = aStartAngle || 0;
-		this.aEndAngle = aEndAngle || 2 * Math.PI;
+					// binary search
 
-		this.aClockwise = aClockwise || false;
+					while ( i1 < right ) {
 
-		this.aRotation = aRotation || 0;
+						var mid = ( i1 + right ) >>> 1;
 
-	}
+						if ( t < pp[ mid ] ) {
 
-	EllipseCurve.prototype = Object.create( Curve.prototype );
-	EllipseCurve.prototype.constructor = EllipseCurve;
+							right = mid;
 
-	EllipseCurve.prototype.isEllipseCurve = true;
+						} else {
 
-	EllipseCurve.prototype.getPoint = function ( t, optionalTarget ) {
+							i1 = mid + 1;
 
-		var point = optionalTarget || new Vector2();
+						}
 
-		var twoPi = Math.PI * 2;
-		var deltaAngle = this.aEndAngle - this.aStartAngle;
-		var samePoints = Math.abs( deltaAngle ) < Number.EPSILON;
+					}
 
-		// ensures that deltaAngle is 0 .. 2 PI
-		while ( deltaAngle < 0 ) { deltaAngle += twoPi; }
-		while ( deltaAngle > twoPi ) { deltaAngle -= twoPi; }
+					t1 = pp[ i1 ];
+					t0 = pp[ i1 - 1 ];
 
-		if ( deltaAngle < Number.EPSILON ) {
+					// check boundary cases, again
 
-			if ( samePoints ) {
+					if ( t0 === undefined ) {
 
-				deltaAngle = 0;
+						this._cachedIndex = 0;
+						return this.beforeStart_( 0, t, t1 );
 
-			} else {
+					}
 
-				deltaAngle = twoPi;
+					if ( t1 === undefined ) {
 
-			}
+						i1 = pp.length;
+						this._cachedIndex = i1;
+						return this.afterEnd_( i1 - 1, t0, t );
 
-		}
+					}
 
-		if ( this.aClockwise === true && ! samePoints ) {
+				} // seek
 
-			if ( deltaAngle === twoPi ) {
+				this._cachedIndex = i1;
 
-				deltaAngle = - twoPi;
+				this.intervalChanged_( i1, t0, t1 );
 
-			} else {
+			} // validate_interval
 
-				deltaAngle = deltaAngle - twoPi;
+			return this.interpolate_( i1, t0, t, t1 );
 
-			}
+		},
 
-		}
+		settings: null, // optional, subclass-specific settings structure
+		// Note: The indirection allows central control of many interpolants.
 
-		var angle = this.aStartAngle + t * deltaAngle;
-		var x = this.aX + this.xRadius * Math.cos( angle );
-		var y = this.aY + this.yRadius * Math.sin( angle );
+		// --- Protected interface
 
-		if ( this.aRotation !== 0 ) {
+		DefaultSettings_: {},
 
-			var cos = Math.cos( this.aRotation );
-			var sin = Math.sin( this.aRotation );
+		getSettings_: function () {
 
-			var tx = x - this.aX;
-			var ty = y - this.aY;
+			return this.settings || this.DefaultSettings_;
 
-			// Rotate the point about the center of the ellipse.
-			x = tx * cos - ty * sin + this.aX;
-			y = tx * sin + ty * cos + this.aY;
+		},
 
-		}
+		copySampleValue_: function ( index ) {
 
-		return point.set( x, y );
+			// copies a sample value to the result buffer
 
-	};
+			var result = this.resultBuffer,
+				values = this.sampleValues,
+				stride = this.valueSize,
+				offset = index * stride;
 
-	EllipseCurve.prototype.copy = function ( source ) {
+			for ( var i = 0; i !== stride; ++ i ) {
 
-		Curve.prototype.copy.call( this, source );
+				result[ i ] = values[ offset + i ];
 
-		this.aX = source.aX;
-		this.aY = source.aY;
+			}
 
-		this.xRadius = source.xRadius;
-		this.yRadius = source.yRadius;
+			return result;
 
-		this.aStartAngle = source.aStartAngle;
-		this.aEndAngle = source.aEndAngle;
+		},
 
-		this.aClockwise = source.aClockwise;
+		// Template methods for derived classes:
 
-		this.aRotation = source.aRotation;
+		interpolate_: function ( /* i1, t0, t, t1 */ ) {
 
-		return this;
+			throw new Error( 'call to abstract method' );
+			// implementations shall return this.resultBuffer
 
-	};
+		},
 
+		intervalChanged_: function ( /* i1, t0, t1 */ ) {
 
-	EllipseCurve.prototype.toJSON = function () {
+			// empty
 
-		var data = Curve.prototype.toJSON.call( this );
+		}
 
-		data.aX = this.aX;
-		data.aY = this.aY;
+	} );
 
-		data.xRadius = this.xRadius;
-		data.yRadius = this.yRadius;
+	// DECLARE ALIAS AFTER assign prototype
+	Object.assign( Interpolant.prototype, {
 
-		data.aStartAngle = this.aStartAngle;
-		data.aEndAngle = this.aEndAngle;
+		//( 0, t, t0 ), returns this.resultBuffer
+		beforeStart_: Interpolant.prototype.copySampleValue_,
 
-		data.aClockwise = this.aClockwise;
+		//( N-1, tN-1, t ), returns this.resultBuffer
+		afterEnd_: Interpolant.prototype.copySampleValue_,
 
-		data.aRotation = this.aRotation;
+	} );
 
-		return data;
+	/**
+	 * Fast and simple cubic spline interpolant.
+	 *
+	 * It was derived from a Hermitian construction setting the first derivative
+	 * at each sample position to the linear slope between neighboring positions
+	 * over their parameter interval.
+	 *
+	 * @author tschw
+	 */
 
-	};
+	function CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-	EllipseCurve.prototype.fromJSON = function ( json ) {
+		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-		Curve.prototype.fromJSON.call( this, json );
+		this._weightPrev = - 0;
+		this._offsetPrev = - 0;
+		this._weightNext = - 0;
+		this._offsetNext = - 0;
 
-		this.aX = json.aX;
-		this.aY = json.aY;
+	}
 
-		this.xRadius = json.xRadius;
-		this.yRadius = json.yRadius;
+	CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
 
-		this.aStartAngle = json.aStartAngle;
-		this.aEndAngle = json.aEndAngle;
+		constructor: CubicInterpolant,
 
-		this.aClockwise = json.aClockwise;
+		DefaultSettings_: {
 
-		this.aRotation = json.aRotation;
+			endingStart: ZeroCurvatureEnding,
+			endingEnd: ZeroCurvatureEnding
 
-		return this;
+		},
 
-	};
+		intervalChanged_: function ( i1, t0, t1 ) {
 
-	function ArcCurve( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
+			var pp = this.parameterPositions,
+				iPrev = i1 - 2,
+				iNext = i1 + 1,
 
-		EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
+				tPrev = pp[ iPrev ],
+				tNext = pp[ iNext ];
 
-		this.type = 'ArcCurve';
+			if ( tPrev === undefined ) {
 
-	}
+				switch ( this.getSettings_().endingStart ) {
 
-	ArcCurve.prototype = Object.create( EllipseCurve.prototype );
-	ArcCurve.prototype.constructor = ArcCurve;
+					case ZeroSlopeEnding:
 
-	ArcCurve.prototype.isArcCurve = true;
+						// f'(t0) = 0
+						iPrev = i1;
+						tPrev = 2 * t0 - t1;
 
-	/**
-	 * @author zz85 https://github.com/zz85
-	 *
-	 * Centripetal CatmullRom Curve - which is useful for avoiding
-	 * cusps and self-intersections in non-uniform catmull rom curves.
-	 * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
-	 *
-	 * curve.type accepts centripetal(default), chordal and catmullrom
-	 * curve.tension is used for catmullrom which defaults to 0.5
-	 */
+						break;
 
+					case WrapAroundEnding:
 
-	/*
-	Based on an optimized c++ solution in
-	 - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
-	 - http://ideone.com/NoEbVM
+						// use the other end of the curve
+						iPrev = pp.length - 2;
+						tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ];
 
-	This CubicPoly class could be used for reusing some variables and calculations,
-	but for three.js curve use, it could be possible inlined and flatten into a single function call
-	which can be placed in CurveUtils.
-	*/
+						break;
 
-	function CubicPoly() {
+					default: // ZeroCurvatureEnding
 
-		var c0 = 0, c1 = 0, c2 = 0, c3 = 0;
-
-		/*
-		 * Compute coefficients for a cubic polynomial
-		 *   p(s) = c0 + c1*s + c2*s^2 + c3*s^3
-		 * such that
-		 *   p(0) = x0, p(1) = x1
-		 *  and
-		 *   p'(0) = t0, p'(1) = t1.
-		 */
-		function init( x0, x1, t0, t1 ) {
+						// f''(t0) = 0 a.k.a. Natural Spline
+						iPrev = i1;
+						tPrev = t1;
 
-			c0 = x0;
-			c1 = t0;
-			c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
-			c3 = 2 * x0 - 2 * x1 + t0 + t1;
+				}
 
-		}
+			}
 
-		return {
+			if ( tNext === undefined ) {
 
-			initCatmullRom: function ( x0, x1, x2, x3, tension ) {
+				switch ( this.getSettings_().endingEnd ) {
 
-				init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
+					case ZeroSlopeEnding:
 
-			},
+						// f'(tN) = 0
+						iNext = i1;
+						tNext = 2 * t1 - t0;
 
-			initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
+						break;
 
-				// compute tangents when parameterized in [t1,t2]
-				var t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
-				var t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
+					case WrapAroundEnding:
 
-				// rescale tangents for parametrization in [0,1]
-				t1 *= dt1;
-				t2 *= dt1;
+						// use the other end of the curve
+						iNext = 1;
+						tNext = t1 + pp[ 1 ] - pp[ 0 ];
 
-				init( x1, x2, t1, t2 );
+						break;
 
-			},
+					default: // ZeroCurvatureEnding
 
-			calc: function ( t ) {
+						// f''(tN) = 0, a.k.a. Natural Spline
+						iNext = i1 - 1;
+						tNext = t0;
 
-				var t2 = t * t;
-				var t3 = t2 * t;
-				return c0 + c1 * t + c2 * t2 + c3 * t3;
+				}
 
 			}
 
-		};
-
-	}
+			var halfDt = ( t1 - t0 ) * 0.5,
+				stride = this.valueSize;
 
-	//
+			this._weightPrev = halfDt / ( t0 - tPrev );
+			this._weightNext = halfDt / ( tNext - t1 );
+			this._offsetPrev = iPrev * stride;
+			this._offsetNext = iNext * stride;
 
-	var tmp = new Vector3();
-	var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
+		},
 
-	function CatmullRomCurve3( points, closed, curveType, tension ) {
+		interpolate_: function ( i1, t0, t, t1 ) {
 
-		Curve.call( this );
+			var result = this.resultBuffer,
+				values = this.sampleValues,
+				stride = this.valueSize,
 
-		this.type = 'CatmullRomCurve3';
+				o1 = i1 * stride,		o0 = o1 - stride,
+				oP = this._offsetPrev, 	oN = this._offsetNext,
+				wP = this._weightPrev,	wN = this._weightNext,
 
-		this.points = points || [];
-		this.closed = closed || false;
-		this.curveType = curveType || 'centripetal';
-		this.tension = tension || 0.5;
+				p = ( t - t0 ) / ( t1 - t0 ),
+				pp = p * p,
+				ppp = pp * p;
 
-	}
+			// evaluate polynomials
 
-	CatmullRomCurve3.prototype = Object.create( Curve.prototype );
-	CatmullRomCurve3.prototype.constructor = CatmullRomCurve3;
+			var sP = - wP * ppp + 2 * wP * pp - wP * p;
+			var s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1;
+			var s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p;
+			var sN = wN * ppp - wN * pp;
 
-	CatmullRomCurve3.prototype.isCatmullRomCurve3 = true;
+			// combine data linearly
 
-	CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
+			for ( var i = 0; i !== stride; ++ i ) {
 
-		var point = optionalTarget || new Vector3();
+				result[ i ] =
+						sP * values[ oP + i ] +
+						s0 * values[ o0 + i ] +
+						s1 * values[ o1 + i ] +
+						sN * values[ oN + i ];
 
-		var points = this.points;
-		var l = points.length;
+			}
 
-		var p = ( l - ( this.closed ? 0 : 1 ) ) * t;
-		var intPoint = Math.floor( p );
-		var weight = p - intPoint;
+			return result;
 
-		if ( this.closed ) {
+		}
 
-			intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
+	} );
 
-		} else if ( weight === 0 && intPoint === l - 1 ) {
+	/**
+	 * @author tschw
+	 */
 
-			intPoint = l - 2;
-			weight = 1;
+	function LinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-		}
+		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-		var p0, p1, p2, p3; // 4 points
+	}
 
-		if ( this.closed || intPoint > 0 ) {
+	LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
 
-			p0 = points[ ( intPoint - 1 ) % l ];
+		constructor: LinearInterpolant,
 
-		} else {
+		interpolate_: function ( i1, t0, t, t1 ) {
 
-			// extrapolate first point
-			tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
-			p0 = tmp;
+			var result = this.resultBuffer,
+				values = this.sampleValues,
+				stride = this.valueSize,
 
-		}
+				offset1 = i1 * stride,
+				offset0 = offset1 - stride,
 
-		p1 = points[ intPoint % l ];
-		p2 = points[ ( intPoint + 1 ) % l ];
+				weight1 = ( t - t0 ) / ( t1 - t0 ),
+				weight0 = 1 - weight1;
 
-		if ( this.closed || intPoint + 2 < l ) {
+			for ( var i = 0; i !== stride; ++ i ) {
 
-			p3 = points[ ( intPoint + 2 ) % l ];
+				result[ i ] =
+						values[ offset0 + i ] * weight0 +
+						values[ offset1 + i ] * weight1;
 
-		} else {
+			}
 
-			// extrapolate last point
-			tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
-			p3 = tmp;
+			return result;
 
 		}
 
-		if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
+	} );
 
-			// init Centripetal / Chordal Catmull-Rom
-			var pow = this.curveType === 'chordal' ? 0.5 : 0.25;
-			var dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
-			var dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
-			var dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
+	/**
+	 *
+	 * Interpolant that evaluates to the sample value at the position preceeding
+	 * the parameter.
+	 *
+	 * @author tschw
+	 */
 
-			// safety check for repeated points
-			if ( dt1 < 1e-4 ) { dt1 = 1.0; }
-			if ( dt0 < 1e-4 ) { dt0 = dt1; }
-			if ( dt2 < 1e-4 ) { dt2 = dt1; }
+	function DiscreteInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-			px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
-			py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
-			pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
+		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-		} else if ( this.curveType === 'catmullrom' ) {
+	}
 
-			px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
-			py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
-			pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
+	DiscreteInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+
+		constructor: DiscreteInterpolant,
+
+		interpolate_: function ( i1 /*, t0, t, t1 */ ) {
+
+			return this.copySampleValue_( i1 - 1 );
 
 		}
 
-		point.set(
-			px.calc( weight ),
-			py.calc( weight ),
-			pz.calc( weight )
-		);
+	} );
 
-		return point;
+	/**
+	 *
+	 * A timed sequence of keyframes for a specific property.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-	};
+	function KeyframeTrack( name, times, values, interpolation ) {
 
-	CatmullRomCurve3.prototype.copy = function ( source ) {
+		if ( name === undefined ) { throw new Error( 'THREE.KeyframeTrack: track name is undefined' ); }
+		if ( times === undefined || times.length === 0 ) { throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name ); }
 
-		Curve.prototype.copy.call( this, source );
+		this.name = name;
 
-		this.points = [];
+		this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
+		this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
 
-		for ( var i = 0, l = source.points.length; i < l; i ++ ) {
+		this.setInterpolation( interpolation || this.DefaultInterpolation );
 
-			var point = source.points[ i ];
+	}
 
-			this.points.push( point.clone() );
+	// Static methods
 
-		}
+	Object.assign( KeyframeTrack, {
 
-		this.closed = source.closed;
-		this.curveType = source.curveType;
-		this.tension = source.tension;
+		// Serialization (in static context, because of constructor invocation
+		// and automatic invocation of .toJSON):
 
-		return this;
+		toJSON: function ( track ) {
 
-	};
+			var trackType = track.constructor;
 
-	CatmullRomCurve3.prototype.toJSON = function () {
+			var json;
 
-		var data = Curve.prototype.toJSON.call( this );
+			// derived classes can define a static toJSON method
+			if ( trackType.toJSON !== undefined ) {
 
-		data.points = [];
+				json = trackType.toJSON( track );
 
-		for ( var i = 0, l = this.points.length; i < l; i ++ ) {
+			} else {
 
-			var point = this.points[ i ];
-			data.points.push( point.toArray() );
+				// by default, we assume the data can be serialized as-is
+				json = {
 
-		}
+					'name': track.name,
+					'times': AnimationUtils.convertArray( track.times, Array ),
+					'values': AnimationUtils.convertArray( track.values, Array )
 
-		data.closed = this.closed;
-		data.curveType = this.curveType;
-		data.tension = this.tension;
+				};
 
-		return data;
+				var interpolation = track.getInterpolation();
 
-	};
+				if ( interpolation !== track.DefaultInterpolation ) {
 
-	CatmullRomCurve3.prototype.fromJSON = function ( json ) {
+					json.interpolation = interpolation;
 
-		Curve.prototype.fromJSON.call( this, json );
+				}
 
-		this.points = [];
+			}
 
-		for ( var i = 0, l = json.points.length; i < l; i ++ ) {
+			json.type = track.ValueTypeName; // mandatory
 
-			var point = json.points[ i ];
-			this.points.push( new Vector3().fromArray( point ) );
+			return json;
 
 		}
 
-		this.closed = json.closed;
-		this.curveType = json.curveType;
-		this.tension = json.tension;
+	} );
 
-		return this;
+	Object.assign( KeyframeTrack.prototype, {
 
-	};
+		constructor: KeyframeTrack,
 
-	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 *
-	 * Bezier Curves formulas obtained from
-	 * http://en.wikipedia.org/wiki/Bézier_curve
-	 */
+		TimeBufferType: Float32Array,
 
-	function CatmullRom( t, p0, p1, p2, p3 ) {
+		ValueBufferType: Float32Array,
 
-		var v0 = ( p2 - p0 ) * 0.5;
-		var v1 = ( p3 - p1 ) * 0.5;
-		var t2 = t * t;
-		var t3 = t * t2;
-		return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
+		DefaultInterpolation: InterpolateLinear,
 
-	}
+		InterpolantFactoryMethodDiscrete: function ( result ) {
 
-	//
+			return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	function QuadraticBezierP0( t, p ) {
+		},
 
-		var k = 1 - t;
-		return k * k * p;
+		InterpolantFactoryMethodLinear: function ( result ) {
 
-	}
+			return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	function QuadraticBezierP1( t, p ) {
+		},
 
-		return 2 * ( 1 - t ) * t * p;
+		InterpolantFactoryMethodSmooth: function ( result ) {
 
-	}
+			return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	function QuadraticBezierP2( t, p ) {
+		},
 
-		return t * t * p;
+		setInterpolation: function ( interpolation ) {
 
-	}
+			var factoryMethod;
 
-	function QuadraticBezier( t, p0, p1, p2 ) {
+			switch ( interpolation ) {
 
-		return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) +
-			QuadraticBezierP2( t, p2 );
+				case InterpolateDiscrete:
 
-	}
+					factoryMethod = this.InterpolantFactoryMethodDiscrete;
 
-	//
+					break;
 
-	function CubicBezierP0( t, p ) {
+				case InterpolateLinear:
 
-		var k = 1 - t;
-		return k * k * k * p;
+					factoryMethod = this.InterpolantFactoryMethodLinear;
 
-	}
+					break;
 
-	function CubicBezierP1( t, p ) {
+				case InterpolateSmooth:
 
-		var k = 1 - t;
-		return 3 * k * k * t * p;
+					factoryMethod = this.InterpolantFactoryMethodSmooth;
 
-	}
+					break;
 
-	function CubicBezierP2( t, p ) {
+			}
 
-		return 3 * ( 1 - t ) * t * t * p;
+			if ( factoryMethod === undefined ) {
 
-	}
+				var message = "unsupported interpolation for " +
+					this.ValueTypeName + " keyframe track named " + this.name;
 
-	function CubicBezierP3( t, p ) {
+				if ( this.createInterpolant === undefined ) {
 
-		return t * t * t * p;
+					// fall back to default, unless the default itself is messed up
+					if ( interpolation !== this.DefaultInterpolation ) {
 
-	}
+						this.setInterpolation( this.DefaultInterpolation );
 
-	function CubicBezier( t, p0, p1, p2, p3 ) {
+					} else {
 
-		return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) +
-			CubicBezierP3( t, p3 );
+						throw new Error( message ); // fatal, in this case
 
-	}
+					}
 
-	function CubicBezierCurve( v0, v1, v2, v3 ) {
+				}
 
-		Curve.call( this );
+				console.warn( 'THREE.KeyframeTrack:', message );
+				return this;
 
-		this.type = 'CubicBezierCurve';
+			}
 
-		this.v0 = v0 || new Vector2();
-		this.v1 = v1 || new Vector2();
-		this.v2 = v2 || new Vector2();
-		this.v3 = v3 || new Vector2();
+			this.createInterpolant = factoryMethod;
 
-	}
+			return this;
 
-	CubicBezierCurve.prototype = Object.create( Curve.prototype );
-	CubicBezierCurve.prototype.constructor = CubicBezierCurve;
+		},
 
-	CubicBezierCurve.prototype.isCubicBezierCurve = true;
+		getInterpolation: function () {
 
-	CubicBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
+			switch ( this.createInterpolant ) {
 
-		var point = optionalTarget || new Vector2();
+				case this.InterpolantFactoryMethodDiscrete:
 
-		var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
+					return InterpolateDiscrete;
 
-		point.set(
-			CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
-			CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
-		);
+				case this.InterpolantFactoryMethodLinear:
 
-		return point;
+					return InterpolateLinear;
 
-	};
+				case this.InterpolantFactoryMethodSmooth:
 
-	CubicBezierCurve.prototype.copy = function ( source ) {
+					return InterpolateSmooth;
 
-		Curve.prototype.copy.call( this, source );
+			}
 
-		this.v0.copy( source.v0 );
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
-		this.v3.copy( source.v3 );
+		},
 
-		return this;
+		getValueSize: function () {
 
-	};
+			return this.values.length / this.times.length;
 
-	CubicBezierCurve.prototype.toJSON = function () {
+		},
 
-		var data = Curve.prototype.toJSON.call( this );
+		// move all keyframes either forwards or backwards in time
+		shift: function ( timeOffset ) {
 
-		data.v0 = this.v0.toArray();
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
-		data.v3 = this.v3.toArray();
+			if ( timeOffset !== 0.0 ) {
 
-		return data;
+				var times = this.times;
 
-	};
+				for ( var i = 0, n = times.length; i !== n; ++ i ) {
 
-	CubicBezierCurve.prototype.fromJSON = function ( json ) {
+					times[ i ] += timeOffset;
 
-		Curve.prototype.fromJSON.call( this, json );
+				}
 
-		this.v0.fromArray( json.v0 );
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
-		this.v3.fromArray( json.v3 );
+			}
 
-		return this;
+			return this;
 
-	};
+		},
 
-	function CubicBezierCurve3( v0, v1, v2, v3 ) {
+		// scale all keyframe times by a factor (useful for frame <-> seconds conversions)
+		scale: function ( timeScale ) {
 
-		Curve.call( this );
+			if ( timeScale !== 1.0 ) {
 
-		this.type = 'CubicBezierCurve3';
+				var times = this.times;
 
-		this.v0 = v0 || new Vector3();
-		this.v1 = v1 || new Vector3();
-		this.v2 = v2 || new Vector3();
-		this.v3 = v3 || new Vector3();
+				for ( var i = 0, n = times.length; i !== n; ++ i ) {
 
-	}
+					times[ i ] *= timeScale;
 
-	CubicBezierCurve3.prototype = Object.create( Curve.prototype );
-	CubicBezierCurve3.prototype.constructor = CubicBezierCurve3;
+				}
 
-	CubicBezierCurve3.prototype.isCubicBezierCurve3 = true;
+			}
 
-	CubicBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
+			return this;
 
-		var point = optionalTarget || new Vector3();
+		},
 
-		var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
+		// removes keyframes before and after animation without changing any values within the range [startTime, endTime].
+		// IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
+		trim: function ( startTime, endTime ) {
 
-		point.set(
-			CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
-			CubicBezier( t, v0.y, v1.y, v2.y, v3.y ),
-			CubicBezier( t, v0.z, v1.z, v2.z, v3.z )
-		);
+			var times = this.times,
+				nKeys = times.length,
+				from = 0,
+				to = nKeys - 1;
 
-		return point;
+			while ( from !== nKeys && times[ from ] < startTime ) {
 
-	};
+				++ from;
 
-	CubicBezierCurve3.prototype.copy = function ( source ) {
+			}
 
-		Curve.prototype.copy.call( this, source );
+			while ( to !== - 1 && times[ to ] > endTime ) {
 
-		this.v0.copy( source.v0 );
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
-		this.v3.copy( source.v3 );
+				-- to;
 
-		return this;
+			}
 
-	};
+			++ to; // inclusive -> exclusive bound
 
-	CubicBezierCurve3.prototype.toJSON = function () {
+			if ( from !== 0 || to !== nKeys ) {
 
-		var data = Curve.prototype.toJSON.call( this );
+				// empty tracks are forbidden, so keep at least one keyframe
+				if ( from >= to ) {
 
-		data.v0 = this.v0.toArray();
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
-		data.v3 = this.v3.toArray();
+					to = Math.max( to, 1 );
+					from = to - 1;
 
-		return data;
+				}
 
-	};
+				var stride = this.getValueSize();
+				this.times = AnimationUtils.arraySlice( times, from, to );
+				this.values = AnimationUtils.arraySlice( this.values, from * stride, to * stride );
 
-	CubicBezierCurve3.prototype.fromJSON = function ( json ) {
+			}
 
-		Curve.prototype.fromJSON.call( this, json );
+			return this;
 
-		this.v0.fromArray( json.v0 );
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
-		this.v3.fromArray( json.v3 );
-
-		return this;
-
-	};
+		},
 
-	function LineCurve( v1, v2 ) {
+		// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
+		validate: function () {
 
-		Curve.call( this );
+			var valid = true;
 
-		this.type = 'LineCurve';
+			var valueSize = this.getValueSize();
+			if ( valueSize - Math.floor( valueSize ) !== 0 ) {
 
-		this.v1 = v1 || new Vector2();
-		this.v2 = v2 || new Vector2();
+				console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
+				valid = false;
 
-	}
+			}
 
-	LineCurve.prototype = Object.create( Curve.prototype );
-	LineCurve.prototype.constructor = LineCurve;
+			var times = this.times,
+				values = this.values,
 
-	LineCurve.prototype.isLineCurve = true;
+				nKeys = times.length;
 
-	LineCurve.prototype.getPoint = function ( t, optionalTarget ) {
+			if ( nKeys === 0 ) {
 
-		var point = optionalTarget || new Vector2();
+				console.error( 'THREE.KeyframeTrack: Track is empty.', this );
+				valid = false;
 
-		if ( t === 1 ) {
+			}
 
-			point.copy( this.v2 );
+			var prevTime = null;
 
-		} else {
+			for ( var i = 0; i !== nKeys; i ++ ) {
 
-			point.copy( this.v2 ).sub( this.v1 );
-			point.multiplyScalar( t ).add( this.v1 );
+				var currTime = times[ i ];
 
-		}
+				if ( typeof currTime === 'number' && isNaN( currTime ) ) {
 
-		return point;
+					console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
+					valid = false;
+					break;
 
-	};
+				}
 
-	// Line curve is linear, so we can overwrite default getPointAt
+				if ( prevTime !== null && prevTime > currTime ) {
 
-	LineCurve.prototype.getPointAt = function ( u, optionalTarget ) {
+					console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
+					valid = false;
+					break;
 
-		return this.getPoint( u, optionalTarget );
+				}
 
-	};
+				prevTime = currTime;
 
-	LineCurve.prototype.getTangent = function ( t, optionalTarget ) {
+			}
 
-		var tangent = optionalTarget || new Vector2();
+			if ( values !== undefined ) {
 
-		var tangent = tangent.copy( this.v2 ).sub( this.v1 ).normalize();
+				if ( AnimationUtils.isTypedArray( values ) ) {
 
-		return tangent;
+					for ( var i = 0, n = values.length; i !== n; ++ i ) {
 
-	};
+						var value = values[ i ];
 
-	LineCurve.prototype.copy = function ( source ) {
+						if ( isNaN( value ) ) {
 
-		Curve.prototype.copy.call( this, source );
+							console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
+							valid = false;
+							break;
 
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
+						}
 
-		return this;
+					}
 
-	};
+				}
 
-	LineCurve.prototype.toJSON = function () {
+			}
 
-		var data = Curve.prototype.toJSON.call( this );
+			return valid;
 
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
+		},
 
-		return data;
+		// removes equivalent sequential keys as common in morph target sequences
+		// (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
+		optimize: function () {
 
-	};
+			// times or values may be shared with other tracks, so overwriting is unsafe
+			var times = AnimationUtils.arraySlice( this.times ),
+				values = AnimationUtils.arraySlice( this.values ),
+				stride = this.getValueSize(),
 
-	LineCurve.prototype.fromJSON = function ( json ) {
+				smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
 
-		Curve.prototype.fromJSON.call( this, json );
+				writeIndex = 1,
+				lastIndex = times.length - 1;
 
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
+			for ( var i = 1; i < lastIndex; ++ i ) {
 
-		return this;
+				var keep = false;
 
-	};
+				var time = times[ i ];
+				var timeNext = times[ i + 1 ];
 
-	function LineCurve3( v1, v2 ) {
+				// remove adjacent keyframes scheduled at the same time
 
-		Curve.call( this );
+				if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) {
 
-		this.type = 'LineCurve3';
+					if ( ! smoothInterpolation ) {
 
-		this.v1 = v1 || new Vector3();
-		this.v2 = v2 || new Vector3();
+						// remove unnecessary keyframes same as their neighbors
 
-	}
+						var offset = i * stride,
+							offsetP = offset - stride,
+							offsetN = offset + stride;
 
-	LineCurve3.prototype = Object.create( Curve.prototype );
-	LineCurve3.prototype.constructor = LineCurve3;
+						for ( var j = 0; j !== stride; ++ j ) {
 
-	LineCurve3.prototype.isLineCurve3 = true;
+							var value = values[ offset + j ];
 
-	LineCurve3.prototype.getPoint = function ( t, optionalTarget ) {
+							if ( value !== values[ offsetP + j ] ||
+								value !== values[ offsetN + j ] ) {
 
-		var point = optionalTarget || new Vector3();
+								keep = true;
+								break;
 
-		if ( t === 1 ) {
+							}
 
-			point.copy( this.v2 );
+						}
 
-		} else {
+					} else {
 
-			point.copy( this.v2 ).sub( this.v1 );
-			point.multiplyScalar( t ).add( this.v1 );
+						keep = true;
 
-		}
+					}
 
-		return point;
+				}
 
-	};
+				// in-place compaction
 
-	// Line curve is linear, so we can overwrite default getPointAt
+				if ( keep ) {
 
-	LineCurve3.prototype.getPointAt = function ( u, optionalTarget ) {
+					if ( i !== writeIndex ) {
 
-		return this.getPoint( u, optionalTarget );
+						times[ writeIndex ] = times[ i ];
 
-	};
+						var readOffset = i * stride,
+							writeOffset = writeIndex * stride;
 
-	LineCurve3.prototype.copy = function ( source ) {
+						for ( var j = 0; j !== stride; ++ j ) {
 
-		Curve.prototype.copy.call( this, source );
+							values[ writeOffset + j ] = values[ readOffset + j ];
 
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
+						}
 
-		return this;
+					}
 
-	};
+					++ writeIndex;
 
-	LineCurve3.prototype.toJSON = function () {
+				}
 
-		var data = Curve.prototype.toJSON.call( this );
+			}
 
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
+			// flush last keyframe (compaction looks ahead)
 
-		return data;
+			if ( lastIndex > 0 ) {
 
-	};
+				times[ writeIndex ] = times[ lastIndex ];
 
-	LineCurve3.prototype.fromJSON = function ( json ) {
+				for ( var readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
 
-		Curve.prototype.fromJSON.call( this, json );
+					values[ writeOffset + j ] = values[ readOffset + j ];
 
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
+				}
 
-		return this;
+				++ writeIndex;
 
-	};
+			}
 
-	function QuadraticBezierCurve( v0, v1, v2 ) {
+			if ( writeIndex !== times.length ) {
 
-		Curve.call( this );
+				this.times = AnimationUtils.arraySlice( times, 0, writeIndex );
+				this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride );
 
-		this.type = 'QuadraticBezierCurve';
+			} else {
 
-		this.v0 = v0 || new Vector2();
-		this.v1 = v1 || new Vector2();
-		this.v2 = v2 || new Vector2();
+				this.times = times;
+				this.values = values;
 
-	}
+			}
 
-	QuadraticBezierCurve.prototype = Object.create( Curve.prototype );
-	QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve;
+			return this;
 
-	QuadraticBezierCurve.prototype.isQuadraticBezierCurve = true;
+		},
 
-	QuadraticBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
+		clone: function () {
 
-		var point = optionalTarget || new Vector2();
+			var times = AnimationUtils.arraySlice( this.times, 0 );
+			var values = AnimationUtils.arraySlice( this.values, 0 );
 
-		var v0 = this.v0, v1 = this.v1, v2 = this.v2;
+			var TypedKeyframeTrack = this.constructor;
+			var track = new TypedKeyframeTrack( this.name, times, values );
 
-		point.set(
-			QuadraticBezier( t, v0.x, v1.x, v2.x ),
-			QuadraticBezier( t, v0.y, v1.y, v2.y )
-		);
+			// Interpolant argument to constructor is not saved, so copy the factory method directly.
+			track.createInterpolant = this.createInterpolant;
 
-		return point;
+			return track;
 
-	};
+		}
 
-	QuadraticBezierCurve.prototype.copy = function ( source ) {
+	} );
 
-		Curve.prototype.copy.call( this, source );
+	/**
+	 *
+	 * A Track of Boolean keyframe values.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-		this.v0.copy( source.v0 );
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
+	function BooleanKeyframeTrack( name, times, values ) {
 
-		return this;
+		KeyframeTrack.call( this, name, times, values );
 
-	};
+	}
 
-	QuadraticBezierCurve.prototype.toJSON = function () {
+	BooleanKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-		var data = Curve.prototype.toJSON.call( this );
+		constructor: BooleanKeyframeTrack,
 
-		data.v0 = this.v0.toArray();
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
+		ValueTypeName: 'bool',
+		ValueBufferType: Array,
 
-		return data;
+		DefaultInterpolation: InterpolateDiscrete,
 
-	};
+		InterpolantFactoryMethodLinear: undefined,
+		InterpolantFactoryMethodSmooth: undefined
 
-	QuadraticBezierCurve.prototype.fromJSON = function ( json ) {
+		// Note: Actually this track could have a optimized / compressed
+		// representation of a single value and a custom interpolant that
+		// computes "firstValue ^ isOdd( index )".
 
-		Curve.prototype.fromJSON.call( this, json );
+	} );
 
-		this.v0.fromArray( json.v0 );
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
+	/**
+	 *
+	 * A Track of keyframe values that represent color.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-		return this;
+	function ColorKeyframeTrack( name, times, values, interpolation ) {
 
-	};
+		KeyframeTrack.call( this, name, times, values, interpolation );
 
-	function QuadraticBezierCurve3( v0, v1, v2 ) {
+	}
 
-		Curve.call( this );
+	ColorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-		this.type = 'QuadraticBezierCurve3';
+		constructor: ColorKeyframeTrack,
 
-		this.v0 = v0 || new Vector3();
-		this.v1 = v1 || new Vector3();
-		this.v2 = v2 || new Vector3();
+		ValueTypeName: 'color'
 
-	}
+		// ValueBufferType is inherited
 
-	QuadraticBezierCurve3.prototype = Object.create( Curve.prototype );
-	QuadraticBezierCurve3.prototype.constructor = QuadraticBezierCurve3;
+		// DefaultInterpolation is inherited
 
-	QuadraticBezierCurve3.prototype.isQuadraticBezierCurve3 = true;
+		// Note: Very basic implementation and nothing special yet.
+		// However, this is the place for color space parameterization.
 
-	QuadraticBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
+	} );
 
-		var point = optionalTarget || new Vector3();
+	/**
+	 *
+	 * A Track of numeric keyframe values.
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-		var v0 = this.v0, v1 = this.v1, v2 = this.v2;
+	function NumberKeyframeTrack( name, times, values, interpolation ) {
 
-		point.set(
-			QuadraticBezier( t, v0.x, v1.x, v2.x ),
-			QuadraticBezier( t, v0.y, v1.y, v2.y ),
-			QuadraticBezier( t, v0.z, v1.z, v2.z )
-		);
+		KeyframeTrack.call( this, name, times, values, interpolation );
 
-		return point;
+	}
 
-	};
+	NumberKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-	QuadraticBezierCurve3.prototype.copy = function ( source ) {
+		constructor: NumberKeyframeTrack,
 
-		Curve.prototype.copy.call( this, source );
+		ValueTypeName: 'number'
 
-		this.v0.copy( source.v0 );
-		this.v1.copy( source.v1 );
-		this.v2.copy( source.v2 );
+		// ValueBufferType is inherited
 
-		return this;
+		// DefaultInterpolation is inherited
 
-	};
+	} );
 
-	QuadraticBezierCurve3.prototype.toJSON = function () {
+	/**
+	 * Spherical linear unit quaternion interpolant.
+	 *
+	 * @author tschw
+	 */
 
-		var data = Curve.prototype.toJSON.call( this );
+	function QuaternionLinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-		data.v0 = this.v0.toArray();
-		data.v1 = this.v1.toArray();
-		data.v2 = this.v2.toArray();
+		Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-		return data;
+	}
 
-	};
+	QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
 
-	QuadraticBezierCurve3.prototype.fromJSON = function ( json ) {
+		constructor: QuaternionLinearInterpolant,
 
-		Curve.prototype.fromJSON.call( this, json );
+		interpolate_: function ( i1, t0, t, t1 ) {
 
-		this.v0.fromArray( json.v0 );
-		this.v1.fromArray( json.v1 );
-		this.v2.fromArray( json.v2 );
+			var result = this.resultBuffer,
+				values = this.sampleValues,
+				stride = this.valueSize,
 
-		return this;
+				offset = i1 * stride,
 
-	};
+				alpha = ( t - t0 ) / ( t1 - t0 );
 
-	function SplineCurve( points /* array of Vector2 */ ) {
+			for ( var end = offset + stride; offset !== end; offset += 4 ) {
 
-		Curve.call( this );
+				Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha );
 
-		this.type = 'SplineCurve';
+			}
 
-		this.points = points || [];
+			return result;
 
-	}
+		}
 
-	SplineCurve.prototype = Object.create( Curve.prototype );
-	SplineCurve.prototype.constructor = SplineCurve;
+	} );
 
-	SplineCurve.prototype.isSplineCurve = true;
+	/**
+	 *
+	 * A Track of quaternion keyframe values.
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-	SplineCurve.prototype.getPoint = function ( t, optionalTarget ) {
+	function QuaternionKeyframeTrack( name, times, values, interpolation ) {
 
-		var point = optionalTarget || new Vector2();
+		KeyframeTrack.call( this, name, times, values, interpolation );
 
-		var points = this.points;
-		var p = ( points.length - 1 ) * t;
+	}
 
-		var intPoint = Math.floor( p );
-		var weight = p - intPoint;
+	QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-		var p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ];
-		var p1 = points[ intPoint ];
-		var p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
-		var p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
+		constructor: QuaternionKeyframeTrack,
 
-		point.set(
-			CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ),
-			CatmullRom( weight, p0.y, p1.y, p2.y, p3.y )
-		);
+		ValueTypeName: 'quaternion',
 
-		return point;
+		// ValueBufferType is inherited
 
-	};
+		DefaultInterpolation: InterpolateLinear,
 
-	SplineCurve.prototype.copy = function ( source ) {
+		InterpolantFactoryMethodLinear: function ( result ) {
 
-		Curve.prototype.copy.call( this, source );
+			return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
 
-		this.points = [];
+		},
 
-		for ( var i = 0, l = source.points.length; i < l; i ++ ) {
+		InterpolantFactoryMethodSmooth: undefined // not yet implemented
 
-			var point = source.points[ i ];
+	} );
 
-			this.points.push( point.clone() );
+	/**
+	 *
+	 * A Track that interpolates Strings
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-		}
+	function StringKeyframeTrack( name, times, values, interpolation ) {
 
-		return this;
+		KeyframeTrack.call( this, name, times, values, interpolation );
 
-	};
+	}
 
-	SplineCurve.prototype.toJSON = function () {
+	StringKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-		var data = Curve.prototype.toJSON.call( this );
+		constructor: StringKeyframeTrack,
 
-		data.points = [];
+		ValueTypeName: 'string',
+		ValueBufferType: Array,
 
-		for ( var i = 0, l = this.points.length; i < l; i ++ ) {
+		DefaultInterpolation: InterpolateDiscrete,
 
-			var point = this.points[ i ];
-			data.points.push( point.toArray() );
+		InterpolantFactoryMethodLinear: undefined,
 
-		}
+		InterpolantFactoryMethodSmooth: undefined
 
-		return data;
+	} );
 
-	};
+	/**
+	 *
+	 * A Track of vectored keyframe values.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-	SplineCurve.prototype.fromJSON = function ( json ) {
+	function VectorKeyframeTrack( name, times, values, interpolation ) {
 
-		Curve.prototype.fromJSON.call( this, json );
+		KeyframeTrack.call( this, name, times, values, interpolation );
 
-		this.points = [];
+	}
 
-		for ( var i = 0, l = json.points.length; i < l; i ++ ) {
+	VectorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
 
-			var point = json.points[ i ];
-			this.points.push( new Vector2().fromArray( point ) );
+		constructor: VectorKeyframeTrack,
 
-		}
+		ValueTypeName: 'vector'
 
-		return this;
+		// ValueBufferType is inherited
 
-	};
+		// DefaultInterpolation is inherited
 
-	var Curves = /*#__PURE__*/Object.freeze({
-		__proto__: null,
-		ArcCurve: ArcCurve,
-		CatmullRomCurve3: CatmullRomCurve3,
-		CubicBezierCurve: CubicBezierCurve,
-		CubicBezierCurve3: CubicBezierCurve3,
-		EllipseCurve: EllipseCurve,
-		LineCurve: LineCurve,
-		LineCurve3: LineCurve3,
-		QuadraticBezierCurve: QuadraticBezierCurve,
-		QuadraticBezierCurve3: QuadraticBezierCurve3,
-		SplineCurve: SplineCurve
-	});
+	} );
 
 	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
 	 *
-	 **/
+	 * Reusable set of Tracks that represent an animation.
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 */
 
-	/**************************************************************
-	 *	Curved Path - a curve path is simply a array of connected
-	 *  curves, but retains the api of a curve
-	 **************************************************************/
+	function AnimationClip( name, duration, tracks, blendMode ) {
 
-	function CurvePath() {
+		this.name = name;
+		this.tracks = tracks;
+		this.duration = ( duration !== undefined ) ? duration : - 1;
+		this.blendMode = ( blendMode !== undefined ) ? blendMode : NormalAnimationBlendMode;
 
-		Curve.call( this );
+		this.uuid = MathUtils.generateUUID();
 
-		this.type = 'CurvePath';
+		// this means it should figure out its duration by scanning the tracks
+		if ( this.duration < 0 ) {
 
-		this.curves = [];
-		this.autoClose = false; // Automatically closes the path
+			this.resetDuration();
 
-	}
+		}
 
-	CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
+	}
 
-		constructor: CurvePath,
+	function getTrackTypeForValueTypeName( typeName ) {
 
-		add: function ( curve ) {
+		switch ( typeName.toLowerCase() ) {
 
-			this.curves.push( curve );
+			case 'scalar':
+			case 'double':
+			case 'float':
+			case 'number':
+			case 'integer':
 
-		},
+				return NumberKeyframeTrack;
 
-		closePath: function () {
+			case 'vector':
+			case 'vector2':
+			case 'vector3':
+			case 'vector4':
 
-			// Add a line curve if start and end of lines are not connected
-			var startPoint = this.curves[ 0 ].getPoint( 0 );
-			var endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
+				return VectorKeyframeTrack;
 
-			if ( ! startPoint.equals( endPoint ) ) {
+			case 'color':
 
-				this.curves.push( new LineCurve( endPoint, startPoint ) );
+				return ColorKeyframeTrack;
 
-			}
+			case 'quaternion':
 
-		},
+				return QuaternionKeyframeTrack;
 
-		// To get accurate point with reference to
-		// entire path distance at time t,
-		// following has to be done:
+			case 'bool':
+			case 'boolean':
 
-		// 1. Length of each sub path have to be known
-		// 2. Locate and identify type of curve
-		// 3. Get t for the curve
-		// 4. Return curve.getPointAt(t')
+				return BooleanKeyframeTrack;
 
-		getPoint: function ( t ) {
+			case 'string':
 
-			var d = t * this.getLength();
-			var curveLengths = this.getCurveLengths();
-			var i = 0;
+				return StringKeyframeTrack;
 
-			// To think about boundaries points.
+		}
 
-			while ( i < curveLengths.length ) {
+		throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
 
-				if ( curveLengths[ i ] >= d ) {
+	}
 
-					var diff = curveLengths[ i ] - d;
-					var curve = this.curves[ i ];
+	function parseKeyframeTrack( json ) {
 
-					var segmentLength = curve.getLength();
-					var u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
+		if ( json.type === undefined ) {
 
-					return curve.getPointAt( u );
+			throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
 
-				}
+		}
 
-				i ++;
+		var trackType = getTrackTypeForValueTypeName( json.type );
 
-			}
+		if ( json.times === undefined ) {
 
-			return null;
+			var times = [], values = [];
 
-			// loop where sum != 0, sum > d , sum+1 <d
+			AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
 
-		},
+			json.times = times;
+			json.values = values;
 
-		// We cannot use the default THREE.Curve getPoint() with getLength() because in
-		// THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
-		// getPoint() depends on getLength
+		}
 
-		getLength: function () {
+		// derived classes can define a static parse method
+		if ( trackType.parse !== undefined ) {
 
-			var lens = this.getCurveLengths();
-			return lens[ lens.length - 1 ];
+			return trackType.parse( json );
 
-		},
+		} else {
 
-		// cacheLengths must be recalculated.
-		updateArcLengths: function () {
+			// by default, we assume a constructor compatible with the base
+			return new trackType( json.name, json.times, json.values, json.interpolation );
 
-			this.needsUpdate = true;
-			this.cacheLengths = null;
-			this.getCurveLengths();
+		}
 
-		},
+	}
 
-		// Compute lengths and cache them
-		// We cannot overwrite getLengths() because UtoT mapping uses it.
+	Object.assign( AnimationClip, {
 
-		getCurveLengths: function () {
+		parse: function ( json ) {
 
-			// We use cache values if curves and cache array are same length
+			var tracks = [],
+				jsonTracks = json.tracks,
+				frameTime = 1.0 / ( json.fps || 1.0 );
 
-			if ( this.cacheLengths && this.cacheLengths.length === this.curves.length ) {
+			for ( var i = 0, n = jsonTracks.length; i !== n; ++ i ) {
 
-				return this.cacheLengths;
+				tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
 
 			}
 
-			// Get length of sub-curve
-			// Push sums into cached array
+			return new AnimationClip( json.name, json.duration, tracks, json.blendMode );
 
-			var lengths = [], sums = 0;
+		},
 
-			for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
+		toJSON: function ( clip ) {
 
-				sums += this.curves[ i ].getLength();
-				lengths.push( sums );
+			var tracks = [],
+				clipTracks = clip.tracks;
 
-			}
+			var json = {
 
-			this.cacheLengths = lengths;
+				'name': clip.name,
+				'duration': clip.duration,
+				'tracks': tracks,
+				'uuid': clip.uuid,
+				'blendMode': clip.blendMode
 
-			return lengths;
+			};
 
-		},
+			for ( var i = 0, n = clipTracks.length; i !== n; ++ i ) {
 
-		getSpacedPoints: function ( divisions ) {
+				tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
 
-			if ( divisions === undefined ) { divisions = 40; }
+			}
 
-			var points = [];
+			return json;
 
-			for ( var i = 0; i <= divisions; i ++ ) {
+		},
 
-				points.push( this.getPoint( i / divisions ) );
+		CreateFromMorphTargetSequence: function ( name, morphTargetSequence, fps, noLoop ) {
 
-			}
+			var numMorphTargets = morphTargetSequence.length;
+			var tracks = [];
 
-			if ( this.autoClose ) {
+			for ( var i = 0; i < numMorphTargets; i ++ ) {
 
-				points.push( points[ 0 ] );
+				var times = [];
+				var values = [];
 
-			}
+				times.push(
+					( i + numMorphTargets - 1 ) % numMorphTargets,
+					i,
+					( i + 1 ) % numMorphTargets );
 
-			return points;
+				values.push( 0, 1, 0 );
 
-		},
+				var order = AnimationUtils.getKeyframeOrder( times );
+				times = AnimationUtils.sortedArray( times, 1, order );
+				values = AnimationUtils.sortedArray( values, 1, order );
 
-		getPoints: function ( divisions ) {
+				// if there is a key at the first frame, duplicate it as the
+				// last frame as well for perfect loop.
+				if ( ! noLoop && times[ 0 ] === 0 ) {
 
-			divisions = divisions || 12;
+					times.push( numMorphTargets );
+					values.push( values[ 0 ] );
 
-			var points = [], last;
+				}
 
-			for ( var i = 0, curves = this.curves; i < curves.length; i ++ ) {
+				tracks.push(
+					new NumberKeyframeTrack(
+						'.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
+						times, values
+					).scale( 1.0 / fps ) );
 
-				var curve = curves[ i ];
-				var resolution = ( curve && curve.isEllipseCurve ) ? divisions * 2
-					: ( curve && ( curve.isLineCurve || curve.isLineCurve3 ) ) ? 1
-						: ( curve && curve.isSplineCurve ) ? divisions * curve.points.length
-							: divisions;
+			}
 
-				var pts = curve.getPoints( resolution );
+			return new AnimationClip( name, - 1, tracks );
 
-				for ( var j = 0; j < pts.length; j ++ ) {
+		},
 
-					var point = pts[ j ];
+		findByName: function ( objectOrClipArray, name ) {
 
-					if ( last && last.equals( point ) ) { continue; } // ensures no consecutive points are duplicates
+			var clipArray = objectOrClipArray;
 
-					points.push( point );
-					last = point;
+			if ( ! Array.isArray( objectOrClipArray ) ) {
 
-				}
+				var o = objectOrClipArray;
+				clipArray = o.geometry && o.geometry.animations || o.animations;
 
 			}
 
-			if ( this.autoClose && points.length > 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) {
+			for ( var i = 0; i < clipArray.length; i ++ ) {
 
-				points.push( points[ 0 ] );
+				if ( clipArray[ i ].name === name ) {
+
+					return clipArray[ i ];
+
+				}
 
 			}
 
-			return points;
+			return null;
 
 		},
 
-		copy: function ( source ) {
+		CreateClipsFromMorphTargetSequences: function ( morphTargets, fps, noLoop ) {
 
-			Curve.prototype.copy.call( this, source );
+			var animationToMorphTargets = {};
 
-			this.curves = [];
+			// tested with https://regex101.com/ on trick sequences
+			// such flamingo_flyA_003, flamingo_run1_003, crdeath0059
+			var pattern = /^([\w-]*?)([\d]+)$/;
 
-			for ( var i = 0, l = source.curves.length; i < l; i ++ ) {
+			// sort morph target names into animation groups based
+			// patterns like Walk_001, Walk_002, Run_001, Run_002
+			for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
 
-				var curve = source.curves[ i ];
+				var morphTarget = morphTargets[ i ];
+				var parts = morphTarget.name.match( pattern );
 
-				this.curves.push( curve.clone() );
+				if ( parts && parts.length > 1 ) {
 
-			}
+					var name = parts[ 1 ];
 
-			this.autoClose = source.autoClose;
+					var animationMorphTargets = animationToMorphTargets[ name ];
+					if ( ! animationMorphTargets ) {
 
-			return this;
+						animationToMorphTargets[ name ] = animationMorphTargets = [];
 
-		},
+					}
 
-		toJSON: function () {
+					animationMorphTargets.push( morphTarget );
 
-			var data = Curve.prototype.toJSON.call( this );
+				}
 
-			data.autoClose = this.autoClose;
-			data.curves = [];
+			}
 
-			for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
+			var clips = [];
 
-				var curve = this.curves[ i ];
-				data.curves.push( curve.toJSON() );
+			for ( var name in animationToMorphTargets ) {
+
+				clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
 
 			}
 
-			return data;
+			return clips;
 
 		},
 
-		fromJSON: function ( json ) {
-
-			Curve.prototype.fromJSON.call( this, json );
-
-			this.autoClose = json.autoClose;
-			this.curves = [];
+		// parse the animation.hierarchy format
+		parseAnimation: function ( animation, bones ) {
 
-			for ( var i = 0, l = json.curves.length; i < l; i ++ ) {
+			if ( ! animation ) {
 
-				var curve = json.curves[ i ];
-				this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) );
+				console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
+				return null;
 
 			}
 
-			return this;
+			var addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
 
-		}
+				// only return track if there are actually keys.
+				if ( animationKeys.length !== 0 ) {
 
-	} );
+					var times = [];
+					var values = [];
 
-	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * Creates free form 2d path using series of points, lines or curves.
-	 **/
+					AnimationUtils.flattenJSON( animationKeys, times, values, propertyName );
 
-	function Path( points ) {
+					// empty keys are filtered out, so check again
+					if ( times.length !== 0 ) {
 
-		CurvePath.call( this );
+						destTracks.push( new trackType( trackName, times, values ) );
 
-		this.type = 'Path';
+					}
 
-		this.currentPoint = new Vector2();
+				}
 
-		if ( points ) {
+			};
 
-			this.setFromPoints( points );
+			var tracks = [];
 
-		}
+			var clipName = animation.name || 'default';
+			// automatic length determination in AnimationClip.
+			var duration = animation.length || - 1;
+			var fps = animation.fps || 30;
+			var blendMode = animation.blendMode;
 
-	}
+			var hierarchyTracks = animation.hierarchy || [];
 
-	Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {
+			for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
 
-		constructor: Path,
+				var animationKeys = hierarchyTracks[ h ].keys;
 
-		setFromPoints: function ( points ) {
+				// skip empty tracks
+				if ( ! animationKeys || animationKeys.length === 0 ) { continue; }
 
-			this.moveTo( points[ 0 ].x, points[ 0 ].y );
+				// process morph targets
+				if ( animationKeys[ 0 ].morphTargets ) {
 
-			for ( var i = 1, l = points.length; i < l; i ++ ) {
+					// figure out all morph targets used in this track
+					var morphTargetNames = {};
 
-				this.lineTo( points[ i ].x, points[ i ].y );
+					for ( var k = 0; k < animationKeys.length; k ++ ) {
 
-			}
+						if ( animationKeys[ k ].morphTargets ) {
 
-			return this;
+							for ( var m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
 
-		},
+								morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
 
-		moveTo: function ( x, y ) {
+							}
 
-			this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
+						}
 
-			return this;
+					}
 
-		},
+					// create a track for each morph target with all zero
+					// morphTargetInfluences except for the keys in which
+					// the morphTarget is named.
+					for ( var morphTargetName in morphTargetNames ) {
 
-		lineTo: function ( x, y ) {
+						var times = [];
+						var values = [];
 
-			var curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
-			this.curves.push( curve );
+						for ( var m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
 
-			this.currentPoint.set( x, y );
+							var animationKey = animationKeys[ k ];
 
-			return this;
+							times.push( animationKey.time );
+							values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
 
-		},
+						}
 
-		quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
+						tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
 
-			var curve = new QuadraticBezierCurve(
-				this.currentPoint.clone(),
-				new Vector2( aCPx, aCPy ),
-				new Vector2( aX, aY )
-			);
+					}
 
-			this.curves.push( curve );
+					duration = morphTargetNames.length * ( fps || 1.0 );
 
-			this.currentPoint.set( aX, aY );
+				} else {
 
-			return this;
+					// ...assume skeletal animation
 
-		},
+					var boneName = '.bones[' + bones[ h ].name + ']';
 
-		bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
+					addNonemptyTrack(
+						VectorKeyframeTrack, boneName + '.position',
+						animationKeys, 'pos', tracks );
 
-			var curve = new CubicBezierCurve(
-				this.currentPoint.clone(),
-				new Vector2( aCP1x, aCP1y ),
-				new Vector2( aCP2x, aCP2y ),
-				new Vector2( aX, aY )
-			);
+					addNonemptyTrack(
+						QuaternionKeyframeTrack, boneName + '.quaternion',
+						animationKeys, 'rot', tracks );
 
-			this.curves.push( curve );
+					addNonemptyTrack(
+						VectorKeyframeTrack, boneName + '.scale',
+						animationKeys, 'scl', tracks );
 
-			this.currentPoint.set( aX, aY );
+				}
 
-			return this;
+			}
 
-		},
+			if ( tracks.length === 0 ) {
 
-		splineThru: function ( pts /*Array of Vector*/ ) {
+				return null;
 
-			var npts = [ this.currentPoint.clone() ].concat( pts );
+			}
 
-			var curve = new SplineCurve( npts );
-			this.curves.push( curve );
+			var clip = new AnimationClip( clipName, duration, tracks, blendMode );
 
-			this.currentPoint.copy( pts[ pts.length - 1 ] );
+			return clip;
 
-			return this;
+		}
 
-		},
+	} );
 
-		arc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
+	Object.assign( AnimationClip.prototype, {
 
-			var x0 = this.currentPoint.x;
-			var y0 = this.currentPoint.y;
+		resetDuration: function () {
 
-			this.absarc( aX + x0, aY + y0, aRadius,
-				aStartAngle, aEndAngle, aClockwise );
+			var tracks = this.tracks, duration = 0;
 
-			return this;
+			for ( var i = 0, n = tracks.length; i !== n; ++ i ) {
 
-		},
+				var track = this.tracks[ i ];
 
-		absarc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
+				duration = Math.max( duration, track.times[ track.times.length - 1 ] );
 
-			this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
+			}
+
+			this.duration = duration;
 
 			return this;
 
 		},
 
-		ellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
+		trim: function () {
 
-			var x0 = this.currentPoint.x;
-			var y0 = this.currentPoint.y;
+			for ( var i = 0; i < this.tracks.length; i ++ ) {
 
-			this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
+				this.tracks[ i ].trim( 0, this.duration );
+
+			}
 
 			return this;
 
 		},
 
-		absellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
-
-			var curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
-
-			if ( this.curves.length > 0 ) {
-
-				// if a previous curve is present, attempt to join
-				var firstPoint = curve.getPoint( 0 );
+		validate: function () {
 
-				if ( ! firstPoint.equals( this.currentPoint ) ) {
+			var valid = true;
 
-					this.lineTo( firstPoint.x, firstPoint.y );
+			for ( var i = 0; i < this.tracks.length; i ++ ) {
 
-				}
+				valid = valid && this.tracks[ i ].validate();
 
 			}
 
-			this.curves.push( curve );
-
-			var lastPoint = curve.getPoint( 1 );
-			this.currentPoint.copy( lastPoint );
-
-			return this;
+			return valid;
 
 		},
 
-		copy: function ( source ) {
+		optimize: function () {
 
-			CurvePath.prototype.copy.call( this, source );
+			for ( var i = 0; i < this.tracks.length; i ++ ) {
 
-			this.currentPoint.copy( source.currentPoint );
+				this.tracks[ i ].optimize();
+
+			}
 
 			return this;
 
 		},
 
-		toJSON: function () {
-
-			var data = CurvePath.prototype.toJSON.call( this );
-
-			data.currentPoint = this.currentPoint.toArray();
-
-			return data;
+		clone: function () {
 
-		},
+			var tracks = [];
 
-		fromJSON: function ( json ) {
+			for ( var i = 0; i < this.tracks.length; i ++ ) {
 
-			CurvePath.prototype.fromJSON.call( this, json );
+				tracks.push( this.tracks[ i ].clone() );
 
-			this.currentPoint.fromArray( json.currentPoint );
+			}
 
-			return this;
+			return new AnimationClip( this.name, this.duration, tracks, this.blendMode );
 
 		}
 
 	} );
 
 	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * Defines a 2d shape plane using paths.
-	 **/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-	// STEP 1 Create a path.
-	// STEP 2 Turn path into shape.
-	// STEP 3 ExtrudeGeometry takes in Shape/Shapes
-	// STEP 3a - Extract points from each shape, turn to vertices
-	// STEP 3b - Triangulate each shape, add faces.
+	var Cache = {
 
-	function Shape( points ) {
+		enabled: false,
 
-		Path.call( this, points );
+		files: {},
 
-		this.uuid = MathUtils.generateUUID();
+		add: function ( key, file ) {
 
-		this.type = 'Shape';
+			if ( this.enabled === false ) { return; }
 
-		this.holes = [];
+			// console.log( 'THREE.Cache', 'Adding key:', key );
 
-	}
+			this.files[ key ] = file;
 
-	Shape.prototype = Object.assign( Object.create( Path.prototype ), {
+		},
 
-		constructor: Shape,
+		get: function ( key ) {
 
-		getPointsHoles: function ( divisions ) {
+			if ( this.enabled === false ) { return; }
 
-			var holesPts = [];
+			// console.log( 'THREE.Cache', 'Checking key:', key );
 
-			for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
+			return this.files[ key ];
 
-				holesPts[ i ] = this.holes[ i ].getPoints( divisions );
+		},
 
-			}
+		remove: function ( key ) {
 
-			return holesPts;
+			delete this.files[ key ];
 
 		},
 
-		// get points of shape and holes (keypoints based on segments parameter)
+		clear: function () {
 
-		extractPoints: function ( divisions ) {
+			this.files = {};
 
-			return {
+		}
 
-				shape: this.getPoints( divisions ),
-				holes: this.getPointsHoles( divisions )
+	};
 
-			};
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		},
+	function LoadingManager( onLoad, onProgress, onError ) {
 
-		copy: function ( source ) {
+		var scope = this;
 
-			Path.prototype.copy.call( this, source );
+		var isLoading = false;
+		var itemsLoaded = 0;
+		var itemsTotal = 0;
+		var urlModifier = undefined;
+		var handlers = [];
 
-			this.holes = [];
+		// Refer to #5689 for the reason why we don't set .onStart
+		// in the constructor
 
-			for ( var i = 0, l = source.holes.length; i < l; i ++ ) {
+		this.onStart = undefined;
+		this.onLoad = onLoad;
+		this.onProgress = onProgress;
+		this.onError = onError;
 
-				var hole = source.holes[ i ];
+		this.itemStart = function ( url ) {
 
-				this.holes.push( hole.clone() );
+			itemsTotal ++;
 
-			}
+			if ( isLoading === false ) {
 
-			return this;
+				if ( scope.onStart !== undefined ) {
 
-		},
+					scope.onStart( url, itemsLoaded, itemsTotal );
 
-		toJSON: function () {
+				}
 
-			var data = Path.prototype.toJSON.call( this );
+			}
 
-			data.uuid = this.uuid;
-			data.holes = [];
+			isLoading = true;
 
-			for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
+		};
 
-				var hole = this.holes[ i ];
-				data.holes.push( hole.toJSON() );
+		this.itemEnd = function ( url ) {
+
+			itemsLoaded ++;
+
+			if ( scope.onProgress !== undefined ) {
+
+				scope.onProgress( url, itemsLoaded, itemsTotal );
 
 			}
 
-			return data;
+			if ( itemsLoaded === itemsTotal ) {
 
-		},
+				isLoading = false;
 
-		fromJSON: function ( json ) {
+				if ( scope.onLoad !== undefined ) {
 
-			Path.prototype.fromJSON.call( this, json );
+					scope.onLoad();
 
-			this.uuid = json.uuid;
-			this.holes = [];
+				}
 
-			for ( var i = 0, l = json.holes.length; i < l; i ++ ) {
+			}
 
-				var hole = json.holes[ i ];
-				this.holes.push( new Path().fromJSON( hole ) );
+		};
+
+		this.itemError = function ( url ) {
+
+			if ( scope.onError !== undefined ) {
+
+				scope.onError( url );
 
 			}
 
-			return this;
+		};
 
-		}
+		this.resolveURL = function ( url ) {
 
-	} );
+			if ( urlModifier ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+				return urlModifier( url );
 
-	function Light( color, intensity ) {
+			}
 
-		Object3D.call( this );
+			return url;
 
-		this.type = 'Light';
+		};
 
-		this.color = new Color( color );
-		this.intensity = intensity !== undefined ? intensity : 1;
+		this.setURLModifier = function ( transform ) {
 
-		this.receiveShadow = undefined;
+			urlModifier = transform;
 
-	}
+			return this;
 
-	Light.prototype = Object.assign( Object.create( Object3D.prototype ), {
+		};
 
-		constructor: Light,
+		this.addHandler = function ( regex, loader ) {
 
-		isLight: true,
+			handlers.push( regex, loader );
 
-		copy: function ( source ) {
+			return this;
 
-			Object3D.prototype.copy.call( this, source );
+		};
 
-			this.color.copy( source.color );
-			this.intensity = source.intensity;
+		this.removeHandler = function ( regex ) {
+
+			var index = handlers.indexOf( regex );
+
+			if ( index !== - 1 ) {
+
+				handlers.splice( index, 2 );
+
+			}
 
 			return this;
 
-		},
+		};
 
-		toJSON: function ( meta ) {
+		this.getHandler = function ( file ) {
 
-			var data = Object3D.prototype.toJSON.call( this, meta );
+			for ( var i = 0, l = handlers.length; i < l; i += 2 ) {
 
-			data.object.color = this.color.getHex();
-			data.object.intensity = this.intensity;
+				var regex = handlers[ i ];
+				var loader = handlers[ i + 1 ];
 
-			if ( this.groundColor !== undefined ) { data.object.groundColor = this.groundColor.getHex(); }
+				if ( regex.global ) { regex.lastIndex = 0; } // see #17920
 
-			if ( this.distance !== undefined ) { data.object.distance = this.distance; }
-			if ( this.angle !== undefined ) { data.object.angle = this.angle; }
-			if ( this.decay !== undefined ) { data.object.decay = this.decay; }
-			if ( this.penumbra !== undefined ) { data.object.penumbra = this.penumbra; }
+				if ( regex.test( file ) ) {
 
-			if ( this.shadow !== undefined ) { data.object.shadow = this.shadow.toJSON(); }
+					return loader;
 
-			return data;
+				}
 
-		}
+			}
 
-	} );
+			return null;
+
+		};
+
+	}
+
+	var DefaultLoadingManager = new LoadingManager();
 
 	/**
 	 * @author alteredq / http://alteredqualia.com/
 	 */
 
-	function HemisphereLight( skyColor, groundColor, intensity ) {
+	function Loader( manager ) {
 
-		Light.call( this, skyColor, intensity );
+		this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
 
-		this.type = 'HemisphereLight';
+		this.crossOrigin = 'anonymous';
+		this.path = '';
+		this.resourcePath = '';
 
-		this.castShadow = undefined;
+	}
 
-		this.position.copy( Object3D.DefaultUp );
-		this.updateMatrix();
+	Object.assign( Loader.prototype, {
 
-		this.groundColor = new Color( groundColor );
+		load: function ( /* url, onLoad, onProgress, onError */ ) {},
 
-	}
+		loadAsync: function ( url, onProgress ) {
 
-	HemisphereLight.prototype = Object.assign( Object.create( Light.prototype ), {
+			var scope = this;
 
-		constructor: HemisphereLight,
+			return new Promise( function ( resolve, reject ) {
 
-		isHemisphereLight: true,
+				scope.load( url, resolve, onProgress, reject );
 
-		copy: function ( source ) {
+			} );
 
-			Light.prototype.copy.call( this, source );
+		},
 
-			this.groundColor.copy( source.groundColor );
+		parse: function ( /* data */ ) {},
+
+		setCrossOrigin: function ( crossOrigin ) {
+
+			this.crossOrigin = crossOrigin;
+			return this;
+
+		},
+
+		setPath: function ( path ) {
 
+			this.path = path;
+			return this;
+
+		},
+
+		setResourcePath: function ( resourcePath ) {
+
+			this.resourcePath = resourcePath;
 			return this;
 
 		}
@@ -47360,367 +55031,307 @@ function simpleEnd(buf) {
 	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function LightShadow( camera ) {
+	var loading = {};
 
-		this.camera = camera;
+	function FileLoader( manager ) {
 
-		this.bias = 0;
-		this.radius = 1;
+		Loader.call( this, manager );
 
-		this.mapSize = new Vector2( 512, 512 );
+	}
 
-		this.map = null;
-		this.mapPass = null;
-		this.matrix = new Matrix4();
+	FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-		this._frustum = new Frustum();
-		this._frameExtents = new Vector2( 1, 1 );
+		constructor: FileLoader,
 
-		this._viewportCount = 1;
+		load: function ( url, onLoad, onProgress, onError ) {
 
-		this._viewports = [
+			if ( url === undefined ) { url = ''; }
 
-			new Vector4( 0, 0, 1, 1 )
+			if ( this.path !== undefined ) { url = this.path + url; }
 
-		];
+			url = this.manager.resolveURL( url );
 
-	}
+			var scope = this;
 
-	Object.assign( LightShadow.prototype, {
+			var cached = Cache.get( url );
 
-		_projScreenMatrix: new Matrix4(),
+			if ( cached !== undefined ) {
 
-		_lightPositionWorld: new Vector3(),
+				scope.manager.itemStart( url );
 
-		_lookTarget: new Vector3(),
+				setTimeout( function () {
 
-		getViewportCount: function () {
+					if ( onLoad ) { onLoad( cached ); }
 
-			return this._viewportCount;
+					scope.manager.itemEnd( url );
 
-		},
+				}, 0 );
 
-		getFrustum: function () {
+				return cached;
 
-			return this._frustum;
+			}
 
-		},
+			// Check if request is duplicate
 
-		updateMatrices: function ( light ) {
+			if ( loading[ url ] !== undefined ) {
 
-			var shadowCamera = this.camera,
-				shadowMatrix = this.matrix,
-				projScreenMatrix = this._projScreenMatrix,
-				lookTarget = this._lookTarget,
-				lightPositionWorld = this._lightPositionWorld;
+				loading[ url ].push( {
 
-			lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
-			shadowCamera.position.copy( lightPositionWorld );
+					onLoad: onLoad,
+					onProgress: onProgress,
+					onError: onError
 
-			lookTarget.setFromMatrixPosition( light.target.matrixWorld );
-			shadowCamera.lookAt( lookTarget );
-			shadowCamera.updateMatrixWorld();
+				} );
 
-			projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
-			this._frustum.setFromProjectionMatrix( projScreenMatrix );
+				return;
 
-			shadowMatrix.set(
-				0.5, 0.0, 0.0, 0.5,
-				0.0, 0.5, 0.0, 0.5,
-				0.0, 0.0, 0.5, 0.5,
-				0.0, 0.0, 0.0, 1.0
-			);
+			}
 
-			shadowMatrix.multiply( shadowCamera.projectionMatrix );
-			shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
+			// Check for data: URI
+			var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
+			var dataUriRegexResult = url.match( dataUriRegex );
 
-		},
+			// Safari can not handle Data URIs through XMLHttpRequest so process manually
+			if ( dataUriRegexResult ) {
 
-		getViewport: function ( viewportIndex ) {
+				var mimeType = dataUriRegexResult[ 1 ];
+				var isBase64 = !! dataUriRegexResult[ 2 ];
+				var data = dataUriRegexResult[ 3 ];
 
-			return this._viewports[ viewportIndex ];
+				data = decodeURIComponent( data );
 
-		},
+				if ( isBase64 ) { data = atob( data ); }
 
-		getFrameExtents: function () {
+				try {
 
-			return this._frameExtents;
+					var response;
+					var responseType = ( this.responseType || '' ).toLowerCase();
 
-		},
+					switch ( responseType ) {
 
-		copy: function ( source ) {
+						case 'arraybuffer':
+						case 'blob':
 
-			this.camera = source.camera.clone();
+							var view = new Uint8Array( data.length );
 
-			this.bias = source.bias;
-			this.radius = source.radius;
+							for ( var i = 0; i < data.length; i ++ ) {
 
-			this.mapSize.copy( source.mapSize );
+								view[ i ] = data.charCodeAt( i );
 
-			return this;
+							}
 
-		},
+							if ( responseType === 'blob' ) {
 
-		clone: function () {
+								response = new Blob( [ view.buffer ], { type: mimeType } );
 
-			return new this.constructor().copy( this );
+							} else {
 
-		},
+								response = view.buffer;
 
-		toJSON: function () {
+							}
 
-			var object = {};
+							break;
 
-			if ( this.bias !== 0 ) { object.bias = this.bias; }
-			if ( this.radius !== 1 ) { object.radius = this.radius; }
-			if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) { object.mapSize = this.mapSize.toArray(); }
+						case 'document':
 
-			object.camera = this.camera.toJSON( false ).object;
-			delete object.camera.matrix;
+							var parser = new DOMParser();
+							response = parser.parseFromString( data, mimeType );
 
-			return object;
+							break;
 
-		}
+						case 'json':
 
-	} );
+							response = JSON.parse( data );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+							break;
 
-	function SpotLightShadow() {
+						default: // 'text' or other
 
-		LightShadow.call( this, new PerspectiveCamera( 50, 1, 0.5, 500 ) );
+							response = data;
 
-	}
+							break;
 
-	SpotLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
+					}
 
-		constructor: SpotLightShadow,
+					// Wait for next browser tick like standard XMLHttpRequest event dispatching does
+					setTimeout( function () {
 
-		isSpotLightShadow: true,
+						if ( onLoad ) { onLoad( response ); }
 
-		updateMatrices: function ( light ) {
+						scope.manager.itemEnd( url );
 
-			var camera = this.camera;
+					}, 0 );
 
-			var fov = MathUtils.RAD2DEG * 2 * light.angle;
-			var aspect = this.mapSize.width / this.mapSize.height;
-			var far = light.distance || camera.far;
+				} catch ( error ) {
 
-			if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) {
+					// Wait for next browser tick like standard XMLHttpRequest event dispatching does
+					setTimeout( function () {
 
-				camera.fov = fov;
-				camera.aspect = aspect;
-				camera.far = far;
-				camera.updateProjectionMatrix();
+						if ( onError ) { onError( error ); }
 
-			}
+						scope.manager.itemError( url );
+						scope.manager.itemEnd( url );
 
-			LightShadow.prototype.updateMatrices.call( this, light );
+					}, 0 );
 
-		}
+				}
 
-	} );
+			} else {
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+				// Initialise array for duplicate requests
 
-	function SpotLight( color, intensity, distance, angle, penumbra, decay ) {
+				loading[ url ] = [];
 
-		Light.call( this, color, intensity );
+				loading[ url ].push( {
 
-		this.type = 'SpotLight';
+					onLoad: onLoad,
+					onProgress: onProgress,
+					onError: onError
 
-		this.position.copy( Object3D.DefaultUp );
-		this.updateMatrix();
+				} );
 
-		this.target = new Object3D();
+				var request = new XMLHttpRequest();
 
-		Object.defineProperty( this, 'power', {
-			get: function () {
+				request.open( 'GET', url, true );
 
-				// intensity = power per solid angle.
-				// ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
-				return this.intensity * Math.PI;
+				request.addEventListener( 'load', function ( event ) {
 
-			},
-			set: function ( power ) {
+					var response = this.response;
 
-				// intensity = power per solid angle.
-				// ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
-				this.intensity = power / Math.PI;
+					var callbacks = loading[ url ];
 
-			}
-		} );
+					delete loading[ url ];
 
-		this.distance = ( distance !== undefined ) ? distance : 0;
-		this.angle = ( angle !== undefined ) ? angle : Math.PI / 3;
-		this.penumbra = ( penumbra !== undefined ) ? penumbra : 0;
-		this.decay = ( decay !== undefined ) ? decay : 1;	// for physically correct lights, should be 2.
+					if ( this.status === 200 || this.status === 0 ) {
 
-		this.shadow = new SpotLightShadow();
+						// Some browsers return HTTP Status 0 when using non-http protocol
+						// e.g. 'file://' or 'data://'. Handle as success.
 
-	}
+						if ( this.status === 0 ) { console.warn( 'THREE.FileLoader: HTTP Status 0 received.' ); }
 
-	SpotLight.prototype = Object.assign( Object.create( Light.prototype ), {
+						// Add to cache only on HTTP success, so that we do not cache
+						// error response bodies as proper responses to requests.
+						Cache.add( url, response );
 
-		constructor: SpotLight,
+						for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
-		isSpotLight: true,
+							var callback = callbacks[ i ];
+							if ( callback.onLoad ) { callback.onLoad( response ); }
 
-		copy: function ( source ) {
+						}
 
-			Light.prototype.copy.call( this, source );
+						scope.manager.itemEnd( url );
 
-			this.distance = source.distance;
-			this.angle = source.angle;
-			this.penumbra = source.penumbra;
-			this.decay = source.decay;
+					} else {
 
-			this.target = source.target.clone();
+						for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
-			this.shadow = source.shadow.clone();
+							var callback = callbacks[ i ];
+							if ( callback.onError ) { callback.onError( event ); }
 
-			return this;
+						}
 
-		}
+						scope.manager.itemError( url );
+						scope.manager.itemEnd( url );
 
-	} );
+					}
 
-	function PointLightShadow() {
+				}, false );
 
-		LightShadow.call( this, new PerspectiveCamera( 90, 1, 0.5, 500 ) );
+				request.addEventListener( 'progress', function ( event ) {
 
-		this._frameExtents = new Vector2( 4, 2 );
+					var callbacks = loading[ url ];
 
-		this._viewportCount = 6;
+					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
-		this._viewports = [
-			// These viewports map a cube-map onto a 2D texture with the
-			// following orientation:
-			//
-			//  xzXZ
-			//   y Y
-			//
-			// X - Positive x direction
-			// x - Negative x direction
-			// Y - Positive y direction
-			// y - Negative y direction
-			// Z - Positive z direction
-			// z - Negative z direction
+						var callback = callbacks[ i ];
+						if ( callback.onProgress ) { callback.onProgress( event ); }
 
-			// positive X
-			new Vector4( 2, 1, 1, 1 ),
-			// negative X
-			new Vector4( 0, 1, 1, 1 ),
-			// positive Z
-			new Vector4( 3, 1, 1, 1 ),
-			// negative Z
-			new Vector4( 1, 1, 1, 1 ),
-			// positive Y
-			new Vector4( 3, 0, 1, 1 ),
-			// negative Y
-			new Vector4( 1, 0, 1, 1 )
-		];
+					}
 
-		this._cubeDirections = [
-			new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
-			new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
-		];
+				}, false );
 
-		this._cubeUps = [
-			new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
-			new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ),	new Vector3( 0, 0, - 1 )
-		];
+				request.addEventListener( 'error', function ( event ) {
 
-	}
+					var callbacks = loading[ url ];
 
-	PointLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
+					delete loading[ url ];
 
-		constructor: PointLightShadow,
+					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
-		isPointLightShadow: true,
-
-		updateMatrices: function ( light, viewportIndex ) {
+						var callback = callbacks[ i ];
+						if ( callback.onError ) { callback.onError( event ); }
 
-			if ( viewportIndex === undefined ) { viewportIndex = 0; }
+					}
 
-			var camera = this.camera,
-				shadowMatrix = this.matrix,
-				lightPositionWorld = this._lightPositionWorld,
-				lookTarget = this._lookTarget,
-				projScreenMatrix = this._projScreenMatrix;
+					scope.manager.itemError( url );
+					scope.manager.itemEnd( url );
 
-			lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
-			camera.position.copy( lightPositionWorld );
+				}, false );
 
-			lookTarget.copy( camera.position );
-			lookTarget.add( this._cubeDirections[ viewportIndex ] );
-			camera.up.copy( this._cubeUps[ viewportIndex ] );
-			camera.lookAt( lookTarget );
-			camera.updateMatrixWorld();
+				request.addEventListener( 'abort', function ( event ) {
 
-			shadowMatrix.makeTranslation( - lightPositionWorld.x, - lightPositionWorld.y, - lightPositionWorld.z );
+					var callbacks = loading[ url ];
 
-			projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
-			this._frustum.setFromProjectionMatrix( projScreenMatrix );
+					delete loading[ url ];
 
-		}
+					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
-	} );
+						var callback = callbacks[ i ];
+						if ( callback.onError ) { callback.onError( event ); }
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+					}
 
+					scope.manager.itemError( url );
+					scope.manager.itemEnd( url );
 
-	function PointLight( color, intensity, distance, decay ) {
+				}, false );
 
-		Light.call( this, color, intensity );
+				if ( this.responseType !== undefined ) { request.responseType = this.responseType; }
+				if ( this.withCredentials !== undefined ) { request.withCredentials = this.withCredentials; }
 
-		this.type = 'PointLight';
+				if ( request.overrideMimeType ) { request.overrideMimeType( this.mimeType !== undefined ? this.mimeType : 'text/plain' ); }
 
-		Object.defineProperty( this, 'power', {
-			get: function () {
+				for ( var header in this.requestHeader ) {
 
-				// intensity = power per solid angle.
-				// ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
-				return this.intensity * 4 * Math.PI;
+					request.setRequestHeader( header, this.requestHeader[ header ] );
 
-			},
-			set: function ( power ) {
+				}
 
-				// intensity = power per solid angle.
-				// ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
-				this.intensity = power / ( 4 * Math.PI );
+				request.send( null );
 
 			}
-		} );
 
-		this.distance = ( distance !== undefined ) ? distance : 0;
-		this.decay = ( decay !== undefined ) ? decay : 1;	// for physically correct lights, should be 2.
+			scope.manager.itemStart( url );
 
-		this.shadow = new PointLightShadow();
+			return request;
 
-	}
+		},
 
-	PointLight.prototype = Object.assign( Object.create( Light.prototype ), {
+		setResponseType: function ( value ) {
 
-		constructor: PointLight,
+			this.responseType = value;
+			return this;
 
-		isPointLight: true,
+		},
 
-		copy: function ( source ) {
+		setWithCredentials: function ( value ) {
 
-			Light.prototype.copy.call( this, source );
+			this.withCredentials = value;
+			return this;
 
-			this.distance = source.distance;
-			this.decay = source.decay;
+		},
 
-			this.shadow = source.shadow.clone();
+		setMimeType: function ( value ) {
+
+			this.mimeType = value;
+			return this;
+
+		},
+
+		setRequestHeader: function ( value ) {
 
+			this.requestHeader = value;
 			return this;
 
 		}
@@ -47728,925 +55339,837 @@ function simpleEnd(buf) {
 	} );
 
 	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author arose / http://github.com/arose
+	 * @author bhouston / http://clara.io/
 	 */
 
-	function OrthographicCamera( left, right, top, bottom, near, far ) {
-
-		Camera.call( this );
-
-		this.type = 'OrthographicCamera';
-
-		this.zoom = 1;
-		this.view = null;
-
-		this.left = ( left !== undefined ) ? left : - 1;
-		this.right = ( right !== undefined ) ? right : 1;
-		this.top = ( top !== undefined ) ? top : 1;
-		this.bottom = ( bottom !== undefined ) ? bottom : - 1;
-
-		this.near = ( near !== undefined ) ? near : 0.1;
-		this.far = ( far !== undefined ) ? far : 2000;
+	function AnimationLoader( manager ) {
 
-		this.updateProjectionMatrix();
+		Loader.call( this, manager );
 
 	}
 
-	OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
-
-		constructor: OrthographicCamera,
+	AnimationLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-		isOrthographicCamera: true,
+		constructor: AnimationLoader,
 
-		copy: function ( source, recursive ) {
+		load: function ( url, onLoad, onProgress, onError ) {
 
-			Camera.prototype.copy.call( this, source, recursive );
+			var scope = this;
 
-			this.left = source.left;
-			this.right = source.right;
-			this.top = source.top;
-			this.bottom = source.bottom;
-			this.near = source.near;
-			this.far = source.far;
+			var loader = new FileLoader( scope.manager );
+			loader.setPath( scope.path );
+			loader.load( url, function ( text ) {
 
-			this.zoom = source.zoom;
-			this.view = source.view === null ? null : Object.assign( {}, source.view );
+				onLoad( scope.parse( JSON.parse( text ) ) );
 
-			return this;
+			}, onProgress, onError );
 
 		},
 
-		setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
+		parse: function ( json ) {
 
-			if ( this.view === null ) {
+			var animations = [];
 
-				this.view = {
-					enabled: true,
-					fullWidth: 1,
-					fullHeight: 1,
-					offsetX: 0,
-					offsetY: 0,
-					width: 1,
-					height: 1
-				};
+			for ( var i = 0; i < json.length; i ++ ) {
+
+				var clip = AnimationClip.parse( json[ i ] );
+
+				animations.push( clip );
 
 			}
 
-			this.view.enabled = true;
-			this.view.fullWidth = fullWidth;
-			this.view.fullHeight = fullHeight;
-			this.view.offsetX = x;
-			this.view.offsetY = y;
-			this.view.width = width;
-			this.view.height = height;
+			return animations;
 
-			this.updateProjectionMatrix();
+		}
 
-		},
+	} );
 
-		clearViewOffset: function () {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 *
+	 * Abstract Base class to block based textures loader (dds, pvr, ...)
+	 *
+	 * Sub classes have to implement the parse() method which will be used in load().
+	 */
 
-			if ( this.view !== null ) {
+	function CompressedTextureLoader( manager ) {
 
-				this.view.enabled = false;
+		Loader.call( this, manager );
 
-			}
+	}
 
-			this.updateProjectionMatrix();
+	CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-		},
+		constructor: CompressedTextureLoader,
 
-		updateProjectionMatrix: function () {
+		load: function ( url, onLoad, onProgress, onError ) {
 
-			var dx = ( this.right - this.left ) / ( 2 * this.zoom );
-			var dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
-			var cx = ( this.right + this.left ) / 2;
-			var cy = ( this.top + this.bottom ) / 2;
+			var scope = this;
 
-			var left = cx - dx;
-			var right = cx + dx;
-			var top = cy + dy;
-			var bottom = cy - dy;
+			var images = [];
 
-			if ( this.view !== null && this.view.enabled ) {
+			var texture = new CompressedTexture();
+			texture.image = images;
 
-				var scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom;
-				var scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom;
+			var loader = new FileLoader( this.manager );
+			loader.setPath( this.path );
+			loader.setResponseType( 'arraybuffer' );
 
-				left += scaleW * this.view.offsetX;
-				right = left + scaleW * this.view.width;
-				top -= scaleH * this.view.offsetY;
-				bottom = top - scaleH * this.view.height;
+			function loadTexture( i ) {
 
-			}
+				loader.load( url[ i ], function ( buffer ) {
 
-			this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far );
+					var texDatas = scope.parse( buffer, true );
 
-			this.projectionMatrixInverse.getInverse( this.projectionMatrix );
+					images[ i ] = {
+						width: texDatas.width,
+						height: texDatas.height,
+						format: texDatas.format,
+						mipmaps: texDatas.mipmaps
+					};
 
-		},
+					loaded += 1;
 
-		toJSON: function ( meta ) {
+					if ( loaded === 6 ) {
 
-			var data = Object3D.prototype.toJSON.call( this, meta );
+						if ( texDatas.mipmapCount === 1 )
+							{ texture.minFilter = LinearFilter; }
 
-			data.object.zoom = this.zoom;
-			data.object.left = this.left;
-			data.object.right = this.right;
-			data.object.top = this.top;
-			data.object.bottom = this.bottom;
-			data.object.near = this.near;
-			data.object.far = this.far;
+						texture.format = texDatas.format;
+						texture.needsUpdate = true;
 
-			if ( this.view !== null ) { data.object.view = Object.assign( {}, this.view ); }
+						if ( onLoad ) { onLoad( texture ); }
 
-			return data;
+					}
 
-		}
+				}, onProgress, onError );
 
-	} );
+			}
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			if ( Array.isArray( url ) ) {
 
-	function DirectionalLightShadow() {
+				var loaded = 0;
 
-		LightShadow.call( this, new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
+				for ( var i = 0, il = url.length; i < il; ++ i ) {
 
-	}
+					loadTexture( i );
 
-	DirectionalLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
+				}
 
-		constructor: DirectionalLightShadow,
+			} else {
 
-		isDirectionalLightShadow: true,
+				// compressed cubemap texture stored in a single DDS file
 
-		updateMatrices: function ( light ) {
+				loader.load( url, function ( buffer ) {
 
-			LightShadow.prototype.updateMatrices.call( this, light );
+					var texDatas = scope.parse( buffer, true );
 
-		}
+					if ( texDatas.isCubemap ) {
 
-	} );
+						var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+						for ( var f = 0; f < faces; f ++ ) {
 
-	function DirectionalLight( color, intensity ) {
+							images[ f ] = { mipmaps: [] };
 
-		Light.call( this, color, intensity );
+							for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
 
-		this.type = 'DirectionalLight';
+								images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
+								images[ f ].format = texDatas.format;
+								images[ f ].width = texDatas.width;
+								images[ f ].height = texDatas.height;
 
-		this.position.copy( Object3D.DefaultUp );
-		this.updateMatrix();
+							}
 
-		this.target = new Object3D();
+						}
 
-		this.shadow = new DirectionalLightShadow();
+					} else {
 
-	}
+						texture.image.width = texDatas.width;
+						texture.image.height = texDatas.height;
+						texture.mipmaps = texDatas.mipmaps;
 
-	DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {
+					}
 
-		constructor: DirectionalLight,
+					if ( texDatas.mipmapCount === 1 ) {
 
-		isDirectionalLight: true,
+						texture.minFilter = LinearFilter;
 
-		copy: function ( source ) {
+					}
 
-			Light.prototype.copy.call( this, source );
+					texture.format = texDatas.format;
+					texture.needsUpdate = true;
 
-			this.target = source.target.clone();
+					if ( onLoad ) { onLoad( texture ); }
 
-			this.shadow = source.shadow.clone();
+				}, onProgress, onError );
 
-			return this;
+			}
+
+			return texture;
 
 		}
 
 	} );
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
+	 * @author Nikos M. / https://github.com/foo123/
+	 *
+	 * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
+	 *
+	 * Sub classes have to implement the parse() method which will be used in load().
 	 */
 
-	function AmbientLight( color, intensity ) {
+	function DataTextureLoader( manager ) {
 
-		Light.call( this, color, intensity );
+		Loader.call( this, manager );
 
-		this.type = 'AmbientLight';
+	}
 
-		this.castShadow = undefined;
+	DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-	}
+		constructor: DataTextureLoader,
 
-	AmbientLight.prototype = Object.assign( Object.create( Light.prototype ), {
+		load: function ( url, onLoad, onProgress, onError ) {
 
-		constructor: AmbientLight,
+			var scope = this;
 
-		isAmbientLight: true
+			var texture = new DataTexture();
 
-	} );
+			var loader = new FileLoader( this.manager );
+			loader.setResponseType( 'arraybuffer' );
+			loader.setPath( this.path );
+			loader.load( url, function ( buffer ) {
 
-	/**
-	 * @author abelnation / http://github.com/abelnation
-	 */
+				var texData = scope.parse( buffer );
 
-	function RectAreaLight( color, intensity, width, height ) {
+				if ( ! texData ) { return; }
 
-		Light.call( this, color, intensity );
+				if ( texData.image !== undefined ) {
 
-		this.type = 'RectAreaLight';
+					texture.image = texData.image;
 
-		this.width = ( width !== undefined ) ? width : 10;
-		this.height = ( height !== undefined ) ? height : 10;
+				} else if ( texData.data !== undefined ) {
 
-	}
+					texture.image.width = texData.width;
+					texture.image.height = texData.height;
+					texture.image.data = texData.data;
 
-	RectAreaLight.prototype = Object.assign( Object.create( Light.prototype ), {
+				}
 
-		constructor: RectAreaLight,
+				texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
+				texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
 
-		isRectAreaLight: true,
+				texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
+				texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
 
-		copy: function ( source ) {
+				texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
 
-			Light.prototype.copy.call( this, source );
+				if ( texData.format !== undefined ) {
 
-			this.width = source.width;
-			this.height = source.height;
+					texture.format = texData.format;
 
-			return this;
+				}
 
-		},
+				if ( texData.type !== undefined ) {
 
-		toJSON: function ( meta ) {
+					texture.type = texData.type;
 
-			var data = Light.prototype.toJSON.call( this, meta );
+				}
 
-			data.object.width = this.width;
-			data.object.height = this.height;
+				if ( texData.mipmaps !== undefined ) {
 
-			return data;
+					texture.mipmaps = texData.mipmaps;
+					texture.minFilter = LinearMipmapLinearFilter; // presumably...
 
-		}
+				}
 
-	} );
+				if ( texData.mipmapCount === 1 ) {
 
-	/**
-	 * @author bhouston / http://clara.io
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * Primary reference:
-	 *   https://graphics.stanford.edu/papers/envmap/envmap.pdf
-	 *
-	 * Secondary reference:
-	 *   https://www.ppsloan.org/publications/StupidSH36.pdf
-	 */
+					texture.minFilter = LinearFilter;
 
-	// 3-band SH defined by 9 coefficients
+				}
 
-	function SphericalHarmonics3() {
+				texture.needsUpdate = true;
 
-		this.coefficients = [];
+				if ( onLoad ) { onLoad( texture, texData ); }
 
-		for ( var i = 0; i < 9; i ++ ) {
+			}, onProgress, onError );
 
-			this.coefficients.push( new Vector3() );
+
+			return texture;
 
 		}
 
-	}
+	} );
 
-	Object.assign( SphericalHarmonics3.prototype, {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		isSphericalHarmonics3: true,
+	function ImageLoader( manager ) {
 
-		set: function ( coefficients ) {
+		Loader.call( this, manager );
 
-			for ( var i = 0; i < 9; i ++ ) {
+	}
 
-				this.coefficients[ i ].copy( coefficients[ i ] );
+	ImageLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-			}
+		constructor: ImageLoader,
 
-			return this;
+		load: function ( url, onLoad, onProgress, onError ) {
 
-		},
+			if ( this.path !== undefined ) { url = this.path + url; }
 
-		zero: function () {
+			url = this.manager.resolveURL( url );
 
-			for ( var i = 0; i < 9; i ++ ) {
+			var scope = this;
 
-				this.coefficients[ i ].set( 0, 0, 0 );
+			var cached = Cache.get( url );
 
-			}
+			if ( cached !== undefined ) {
 
-			return this;
+				scope.manager.itemStart( url );
 
-		},
+				setTimeout( function () {
 
-		// get the radiance in the direction of the normal
-		// target is a Vector3
-		getAt: function ( normal, target ) {
+					if ( onLoad ) { onLoad( cached ); }
 
-			// normal is assumed to be unit length
+					scope.manager.itemEnd( url );
 
-			var x = normal.x, y = normal.y, z = normal.z;
+				}, 0 );
 
-			var coeff = this.coefficients;
+				return cached;
 
-			// band 0
-			target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );
+			}
 
-			// band 1
-			target.addScaledVector( coeff[ 1 ], 0.488603 * y );
-			target.addScaledVector( coeff[ 2 ], 0.488603 * z );
-			target.addScaledVector( coeff[ 3 ], 0.488603 * x );
+			var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
 
-			// band 2
-			target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) );
-			target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) );
-			target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
-			target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) );
-			target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
+			function onImageLoad() {
 
-			return target;
+				image.removeEventListener( 'load', onImageLoad, false );
+				image.removeEventListener( 'error', onImageError, false );
 
-		},
+				Cache.add( url, this );
 
-		// get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
-		// target is a Vector3
-		// https://graphics.stanford.edu/papers/envmap/envmap.pdf
-		getIrradianceAt: function ( normal, target ) {
+				if ( onLoad ) { onLoad( this ); }
 
-			// normal is assumed to be unit length
+				scope.manager.itemEnd( url );
 
-			var x = normal.x, y = normal.y, z = normal.z;
+			}
 
-			var coeff = this.coefficients;
+			function onImageError( event ) {
 
-			// band 0
-			target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095
+				image.removeEventListener( 'load', onImageLoad, false );
+				image.removeEventListener( 'error', onImageError, false );
 
-			// band 1
-			target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
-			target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z );
-			target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x );
+				if ( onError ) { onError( event ); }
 
-			// band 2
-			target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
-			target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z );
-			target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
-			target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z );
-			target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
+				scope.manager.itemError( url );
+				scope.manager.itemEnd( url );
 
-			return target;
+			}
 
-		},
+			image.addEventListener( 'load', onImageLoad, false );
+			image.addEventListener( 'error', onImageError, false );
 
-		add: function ( sh ) {
+			if ( url.substr( 0, 5 ) !== 'data:' ) {
 
-			for ( var i = 0; i < 9; i ++ ) {
-
-				this.coefficients[ i ].add( sh.coefficients[ i ] );
+				if ( this.crossOrigin !== undefined ) { image.crossOrigin = this.crossOrigin; }
 
 			}
 
-			return this;
+			scope.manager.itemStart( url );
 
-		},
+			image.src = url;
 
-		addScaledSH: function ( sh, s ) {
+			return image;
 
-			for ( var i = 0; i < 9; i ++ ) {
+		}
 
-				this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s );
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			return this;
 
-		},
+	function CubeTextureLoader( manager ) {
 
-		scale: function ( s ) {
+		Loader.call( this, manager );
 
-			for ( var i = 0; i < 9; i ++ ) {
+	}
 
-				this.coefficients[ i ].multiplyScalar( s );
+	CubeTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-			}
+		constructor: CubeTextureLoader,
 
-			return this;
+		load: function ( urls, onLoad, onProgress, onError ) {
 
-		},
+			var texture = new CubeTexture();
 
-		lerp: function ( sh, alpha ) {
+			var loader = new ImageLoader( this.manager );
+			loader.setCrossOrigin( this.crossOrigin );
+			loader.setPath( this.path );
 
-			for ( var i = 0; i < 9; i ++ ) {
+			var loaded = 0;
 
-				this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha );
+			function loadTexture( i ) {
 
-			}
+				loader.load( urls[ i ], function ( image ) {
 
-			return this;
+					texture.images[ i ] = image;
 
-		},
+					loaded ++;
 
-		equals: function ( sh ) {
+					if ( loaded === 6 ) {
 
-			for ( var i = 0; i < 9; i ++ ) {
+						texture.needsUpdate = true;
 
-				if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) {
+						if ( onLoad ) { onLoad( texture ); }
 
-					return false;
+					}
 
-				}
+				}, undefined, onError );
 
 			}
 
-			return true;
-
-		},
-
-		copy: function ( sh ) {
-
-			return this.set( sh.coefficients );
-
-		},
-
-		clone: function () {
-
-			return new this.constructor().copy( this );
-
-		},
-
-		fromArray: function ( array, offset ) {
+			for ( var i = 0; i < urls.length; ++ i ) {
 
-			if ( offset === undefined ) { offset = 0; }
+				loadTexture( i );
 
-			var coefficients = this.coefficients;
+			}
 
-			for ( var i = 0; i < 9; i ++ ) {
+			return texture;
 
-				coefficients[ i ].fromArray( array, offset + ( i * 3 ) );
+		}
 
-			}
+	} );
 
-			return this;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		},
+	function TextureLoader( manager ) {
 
-		toArray: function ( array, offset ) {
+		Loader.call( this, manager );
 
-			if ( array === undefined ) { array = []; }
-			if ( offset === undefined ) { offset = 0; }
+	}
 
-			var coefficients = this.coefficients;
+	TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-			for ( var i = 0; i < 9; i ++ ) {
+		constructor: TextureLoader,
 
-				coefficients[ i ].toArray( array, offset + ( i * 3 ) );
+		load: function ( url, onLoad, onProgress, onError ) {
 
-			}
+			var texture = new Texture();
 
-			return array;
+			var loader = new ImageLoader( this.manager );
+			loader.setCrossOrigin( this.crossOrigin );
+			loader.setPath( this.path );
 
-		}
+			loader.load( url, function ( image ) {
 
-	} );
+				texture.image = image;
 
-	Object.assign( SphericalHarmonics3, {
+				// JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
+				var isJPEG = url.search( /\.jpe?g($|\?)/i ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0;
 
-		// evaluate the basis functions
-		// shBasis is an Array[ 9 ]
-		getBasisAt: function ( normal, shBasis ) {
+				texture.format = isJPEG ? RGBFormat : RGBAFormat;
+				texture.needsUpdate = true;
 
-			// normal is assumed to be unit length
+				if ( onLoad !== undefined ) {
 
-			var x = normal.x, y = normal.y, z = normal.z;
+					onLoad( texture );
 
-			// band 0
-			shBasis[ 0 ] = 0.282095;
+				}
 
-			// band 1
-			shBasis[ 1 ] = 0.488603 * y;
-			shBasis[ 2 ] = 0.488603 * z;
-			shBasis[ 3 ] = 0.488603 * x;
+			}, onProgress, onError );
 
-			// band 2
-			shBasis[ 4 ] = 1.092548 * x * y;
-			shBasis[ 5 ] = 1.092548 * y * z;
-			shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 );
-			shBasis[ 7 ] = 1.092548 * x * z;
-			shBasis[ 8 ] = 0.546274 * ( x * x - y * y );
+			return texture;
 
 		}
 
 	} );
 
 	/**
-	 * @author WestLangley / http://github.com/WestLangley
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * Extensible curve object
 	 *
-	 * A LightProbe is a source of indirect-diffuse light
-	 */
+	 * Some common of curve methods:
+	 * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget )
+	 * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget )
+	 * .getPoints(), .getSpacedPoints()
+	 * .getLength()
+	 * .updateArcLengths()
+	 *
+	 * This following curves inherit from THREE.Curve:
+	 *
+	 * -- 2D curves --
+	 * THREE.ArcCurve
+	 * THREE.CubicBezierCurve
+	 * THREE.EllipseCurve
+	 * THREE.LineCurve
+	 * THREE.QuadraticBezierCurve
+	 * THREE.SplineCurve
+	 *
+	 * -- 3D curves --
+	 * THREE.CatmullRomCurve3
+	 * THREE.CubicBezierCurve3
+	 * THREE.LineCurve3
+	 * THREE.QuadraticBezierCurve3
+	 *
+	 * A series of curves can be represented as a THREE.CurvePath.
+	 *
+	 **/
 
-	function LightProbe( sh, intensity ) {
+	/**************************************************************
+	 *	Abstract Curve base class
+	 **************************************************************/
 
-		Light.call( this, undefined, intensity );
+	function Curve() {
 
-		this.type = 'LightProbe';
+		this.type = 'Curve';
 
-		this.sh = ( sh !== undefined ) ? sh : new SphericalHarmonics3();
+		this.arcLengthDivisions = 200;
 
 	}
 
-	LightProbe.prototype = Object.assign( Object.create( Light.prototype ), {
-
-		constructor: LightProbe,
-
-		isLightProbe: true,
-
-		copy: function ( source ) {
+	Object.assign( Curve.prototype, {
 
-			Light.prototype.copy.call( this, source );
+		// Virtual base class method to overwrite and implement in subclasses
+		//	- t [0 .. 1]
 
-			this.sh.copy( source.sh );
+		getPoint: function ( /* t, optionalTarget */ ) {
 
-			return this;
+			console.warn( 'THREE.Curve: .getPoint() not implemented.' );
+			return null;
 
 		},
 
-		fromJSON: function ( json ) {
+		// Get point at relative position in curve according to arc length
+		// - u [0 .. 1]
 
-			this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON();
-			this.sh.fromArray( json.sh );
+		getPointAt: function ( u, optionalTarget ) {
 
-			return this;
+			var t = this.getUtoTmapping( u );
+			return this.getPoint( t, optionalTarget );
 
 		},
 
-		toJSON: function ( meta ) {
-
-			var data = Light.prototype.toJSON.call( this, meta );
+		// Get sequence of points using getPoint( t )
 
-			data.object.sh = this.sh.toArray();
+		getPoints: function ( divisions ) {
 
-			return data;
+			if ( divisions === undefined ) { divisions = 5; }
 
-		}
+			var points = [];
 
-	} );
+			for ( var d = 0; d <= divisions; d ++ ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				points.push( this.getPoint( d / divisions ) );
 
-	function MaterialLoader( manager ) {
+			}
 
-		Loader.call( this, manager );
+			return points;
 
-		this.textures = {};
+		},
 
-	}
+		// Get sequence of points using getPointAt( u )
 
-	MaterialLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		getSpacedPoints: function ( divisions ) {
 
-		constructor: MaterialLoader,
+			if ( divisions === undefined ) { divisions = 5; }
 
-		load: function ( url, onLoad, onProgress, onError ) {
+			var points = [];
 
-			var scope = this;
+			for ( var d = 0; d <= divisions; d ++ ) {
 
-			var loader = new FileLoader( scope.manager );
-			loader.setPath( scope.path );
-			loader.load( url, function ( text ) {
+				points.push( this.getPointAt( d / divisions ) );
 
-				onLoad( scope.parse( JSON.parse( text ) ) );
+			}
 
-			}, onProgress, onError );
+			return points;
 
 		},
 
-		parse: function ( json ) {
+		// Get total curve arc length
 
-			var textures = this.textures;
+		getLength: function () {
 
-			function getTexture( name ) {
+			var lengths = this.getLengths();
+			return lengths[ lengths.length - 1 ];
 
-				if ( textures[ name ] === undefined ) {
+		},
 
-					console.warn( 'THREE.MaterialLoader: Undefined texture', name );
+		// Get list of cumulative segment lengths
 
-				}
+		getLengths: function ( divisions ) {
 
-				return textures[ name ];
+			if ( divisions === undefined ) { divisions = this.arcLengthDivisions; }
+
+			if ( this.cacheArcLengths &&
+				( this.cacheArcLengths.length === divisions + 1 ) &&
+				! this.needsUpdate ) {
+
+				return this.cacheArcLengths;
 
 			}
 
-			var material = new Materials[ json.type ]();
+			this.needsUpdate = false;
 
-			if ( json.uuid !== undefined ) { material.uuid = json.uuid; }
-			if ( json.name !== undefined ) { material.name = json.name; }
-			if ( json.color !== undefined ) { material.color.setHex( json.color ); }
-			if ( json.roughness !== undefined ) { material.roughness = json.roughness; }
-			if ( json.metalness !== undefined ) { material.metalness = json.metalness; }
-			if ( json.sheen !== undefined ) { material.sheen = new Color().setHex( json.sheen ); }
-			if ( json.emissive !== undefined ) { material.emissive.setHex( json.emissive ); }
-			if ( json.specular !== undefined ) { material.specular.setHex( json.specular ); }
-			if ( json.shininess !== undefined ) { material.shininess = json.shininess; }
-			if ( json.clearcoat !== undefined ) { material.clearcoat = json.clearcoat; }
-			if ( json.clearcoatRoughness !== undefined ) { material.clearcoatRoughness = json.clearcoatRoughness; }
-			if ( json.fog !== undefined ) { material.fog = json.fog; }
-			if ( json.flatShading !== undefined ) { material.flatShading = json.flatShading; }
-			if ( json.blending !== undefined ) { material.blending = json.blending; }
-			if ( json.combine !== undefined ) { material.combine = json.combine; }
-			if ( json.side !== undefined ) { material.side = json.side; }
-			if ( json.opacity !== undefined ) { material.opacity = json.opacity; }
-			if ( json.transparent !== undefined ) { material.transparent = json.transparent; }
-			if ( json.alphaTest !== undefined ) { material.alphaTest = json.alphaTest; }
-			if ( json.depthTest !== undefined ) { material.depthTest = json.depthTest; }
-			if ( json.depthWrite !== undefined ) { material.depthWrite = json.depthWrite; }
-			if ( json.colorWrite !== undefined ) { material.colorWrite = json.colorWrite; }
+			var cache = [];
+			var current, last = this.getPoint( 0 );
+			var p, sum = 0;
 
-			if ( json.stencilWrite !== undefined ) { material.stencilWrite = json.stencilWrite; }
-			if ( json.stencilWriteMask !== undefined ) { material.stencilWriteMask = json.stencilWriteMask; }
-			if ( json.stencilFunc !== undefined ) { material.stencilFunc = json.stencilFunc; }
-			if ( json.stencilRef !== undefined ) { material.stencilRef = json.stencilRef; }
-			if ( json.stencilFuncMask !== undefined ) { material.stencilFuncMask = json.stencilFuncMask; }
-			if ( json.stencilFail !== undefined ) { material.stencilFail = json.stencilFail; }
-			if ( json.stencilZFail !== undefined ) { material.stencilZFail = json.stencilZFail; }
-			if ( json.stencilZPass !== undefined ) { material.stencilZPass = json.stencilZPass; }
+			cache.push( 0 );
 
-			if ( json.wireframe !== undefined ) { material.wireframe = json.wireframe; }
-			if ( json.wireframeLinewidth !== undefined ) { material.wireframeLinewidth = json.wireframeLinewidth; }
-			if ( json.wireframeLinecap !== undefined ) { material.wireframeLinecap = json.wireframeLinecap; }
-			if ( json.wireframeLinejoin !== undefined ) { material.wireframeLinejoin = json.wireframeLinejoin; }
+			for ( p = 1; p <= divisions; p ++ ) {
 
-			if ( json.rotation !== undefined ) { material.rotation = json.rotation; }
+				current = this.getPoint( p / divisions );
+				sum += current.distanceTo( last );
+				cache.push( sum );
+				last = current;
 
-			if ( json.linewidth !== 1 ) { material.linewidth = json.linewidth; }
-			if ( json.dashSize !== undefined ) { material.dashSize = json.dashSize; }
-			if ( json.gapSize !== undefined ) { material.gapSize = json.gapSize; }
-			if ( json.scale !== undefined ) { material.scale = json.scale; }
+			}
 
-			if ( json.polygonOffset !== undefined ) { material.polygonOffset = json.polygonOffset; }
-			if ( json.polygonOffsetFactor !== undefined ) { material.polygonOffsetFactor = json.polygonOffsetFactor; }
-			if ( json.polygonOffsetUnits !== undefined ) { material.polygonOffsetUnits = json.polygonOffsetUnits; }
+			this.cacheArcLengths = cache;
 
-			if ( json.skinning !== undefined ) { material.skinning = json.skinning; }
-			if ( json.morphTargets !== undefined ) { material.morphTargets = json.morphTargets; }
-			if ( json.morphNormals !== undefined ) { material.morphNormals = json.morphNormals; }
-			if ( json.dithering !== undefined ) { material.dithering = json.dithering; }
+			return cache; // { sums: cache, sum: sum }; Sum is in the last element.
 
-			if ( json.vertexTangents !== undefined ) { material.vertexTangents = json.vertexTangents; }
+		},
 
-			if ( json.visible !== undefined ) { material.visible = json.visible; }
+		updateArcLengths: function () {
 
-			if ( json.toneMapped !== undefined ) { material.toneMapped = json.toneMapped; }
+			this.needsUpdate = true;
+			this.getLengths();
 
-			if ( json.userData !== undefined ) { material.userData = json.userData; }
+		},
 
-			if ( json.vertexColors !== undefined ) {
+		// Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
 
-				if ( typeof json.vertexColors === 'number' ) {
+		getUtoTmapping: function ( u, distance ) {
 
-					material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
+			var arcLengths = this.getLengths();
 
-				} else {
+			var i = 0, il = arcLengths.length;
 
-					material.vertexColors = json.vertexColors;
+			var targetArcLength; // The targeted u distance value to get
 
-				}
+			if ( distance ) {
 
-			}
+				targetArcLength = distance;
 
-			// Shader Material
+			} else {
 
-			if ( json.uniforms !== undefined ) {
+				targetArcLength = u * arcLengths[ il - 1 ];
 
-				for ( var name in json.uniforms ) {
+			}
 
-					var uniform = json.uniforms[ name ];
+			// binary search for the index with largest value smaller than target u distance
 
-					material.uniforms[ name ] = {};
+			var low = 0, high = il - 1, comparison;
 
-					switch ( uniform.type ) {
+			while ( low <= high ) {
 
-						case 't':
-							material.uniforms[ name ].value = getTexture( uniform.value );
-							break;
+				i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
 
-						case 'c':
-							material.uniforms[ name ].value = new Color().setHex( uniform.value );
-							break;
+				comparison = arcLengths[ i ] - targetArcLength;
 
-						case 'v2':
-							material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
-							break;
+				if ( comparison < 0 ) {
 
-						case 'v3':
-							material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
-							break;
+					low = i + 1;
 
-						case 'v4':
-							material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
-							break;
+				} else if ( comparison > 0 ) {
 
-						case 'm3':
-							material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
+					high = i - 1;
 
-						case 'm4':
-							material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
-							break;
+				} else {
 
-						default:
-							material.uniforms[ name ].value = uniform.value;
+					high = i;
+					break;
 
-					}
+					// DONE
 
 				}
 
 			}
 
-			if ( json.defines !== undefined ) { material.defines = json.defines; }
-			if ( json.vertexShader !== undefined ) { material.vertexShader = json.vertexShader; }
-			if ( json.fragmentShader !== undefined ) { material.fragmentShader = json.fragmentShader; }
-
-			if ( json.extensions !== undefined ) {
-
-				for ( var key in json.extensions ) {
+			i = high;
 
-					material.extensions[ key ] = json.extensions[ key ];
+			if ( arcLengths[ i ] === targetArcLength ) {
 
-				}
+				return i / ( il - 1 );
 
 			}
 
-			// Deprecated
+			// we could get finer grain at lengths, or use simple interpolation between two points
 
-			if ( json.shading !== undefined ) { material.flatShading = json.shading === 1; } // THREE.FlatShading
+			var lengthBefore = arcLengths[ i ];
+			var lengthAfter = arcLengths[ i + 1 ];
 
-			// for PointsMaterial
+			var segmentLength = lengthAfter - lengthBefore;
 
-			if ( json.size !== undefined ) { material.size = json.size; }
-			if ( json.sizeAttenuation !== undefined ) { material.sizeAttenuation = json.sizeAttenuation; }
+			// determine where we are between the 'before' and 'after' points
 
-			// maps
+			var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
 
-			if ( json.map !== undefined ) { material.map = getTexture( json.map ); }
-			if ( json.matcap !== undefined ) { material.matcap = getTexture( json.matcap ); }
+			// add that fractional amount to t
 
-			if ( json.alphaMap !== undefined ) { material.alphaMap = getTexture( json.alphaMap ); }
+			var t = ( i + segmentFraction ) / ( il - 1 );
 
-			if ( json.bumpMap !== undefined ) { material.bumpMap = getTexture( json.bumpMap ); }
-			if ( json.bumpScale !== undefined ) { material.bumpScale = json.bumpScale; }
+			return t;
 
-			if ( json.normalMap !== undefined ) { material.normalMap = getTexture( json.normalMap ); }
-			if ( json.normalMapType !== undefined ) { material.normalMapType = json.normalMapType; }
-			if ( json.normalScale !== undefined ) {
+		},
 
-				var normalScale = json.normalScale;
+		// Returns a unit vector tangent at t
+		// In case any sub curve does not implement its tangent derivation,
+		// 2 points a small delta apart will be used to find its gradient
+		// which seems to give a reasonable approximation
 
-				if ( Array.isArray( normalScale ) === false ) {
+		getTangent: function ( t, optionalTarget ) {
 
-					// Blender exporter used to export a scalar. See #7459
+			var delta = 0.0001;
+			var t1 = t - delta;
+			var t2 = t + delta;
 
-					normalScale = [ normalScale, normalScale ];
+			// Capping in case of danger
 
-				}
+			if ( t1 < 0 ) { t1 = 0; }
+			if ( t2 > 1 ) { t2 = 1; }
 
-				material.normalScale = new Vector2().fromArray( normalScale );
+			var pt1 = this.getPoint( t1 );
+			var pt2 = this.getPoint( t2 );
 
-			}
+			var tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() );
 
-			if ( json.displacementMap !== undefined ) { material.displacementMap = getTexture( json.displacementMap ); }
-			if ( json.displacementScale !== undefined ) { material.displacementScale = json.displacementScale; }
-			if ( json.displacementBias !== undefined ) { material.displacementBias = json.displacementBias; }
+			tangent.copy( pt2 ).sub( pt1 ).normalize();
 
-			if ( json.roughnessMap !== undefined ) { material.roughnessMap = getTexture( json.roughnessMap ); }
-			if ( json.metalnessMap !== undefined ) { material.metalnessMap = getTexture( json.metalnessMap ); }
+			return tangent;
 
-			if ( json.emissiveMap !== undefined ) { material.emissiveMap = getTexture( json.emissiveMap ); }
-			if ( json.emissiveIntensity !== undefined ) { material.emissiveIntensity = json.emissiveIntensity; }
+		},
 
-			if ( json.specularMap !== undefined ) { material.specularMap = getTexture( json.specularMap ); }
+		getTangentAt: function ( u, optionalTarget ) {
 
-			if ( json.envMap !== undefined ) { material.envMap = getTexture( json.envMap ); }
-			if ( json.envMapIntensity !== undefined ) { material.envMapIntensity = json.envMapIntensity; }
+			var t = this.getUtoTmapping( u );
+			return this.getTangent( t, optionalTarget );
 
-			if ( json.reflectivity !== undefined ) { material.reflectivity = json.reflectivity; }
-			if ( json.refractionRatio !== undefined ) { material.refractionRatio = json.refractionRatio; }
+		},
 
-			if ( json.lightMap !== undefined ) { material.lightMap = getTexture( json.lightMap ); }
-			if ( json.lightMapIntensity !== undefined ) { material.lightMapIntensity = json.lightMapIntensity; }
+		computeFrenetFrames: function ( segments, closed ) {
 
-			if ( json.aoMap !== undefined ) { material.aoMap = getTexture( json.aoMap ); }
-			if ( json.aoMapIntensity !== undefined ) { material.aoMapIntensity = json.aoMapIntensity; }
+			// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
 
-			if ( json.gradientMap !== undefined ) { material.gradientMap = getTexture( json.gradientMap ); }
+			var normal = new Vector3();
 
-			if ( json.clearcoatMap !== undefined ) { material.clearcoatMap = getTexture( json.clearcoatMap ); }
-			if ( json.clearcoatRoughnessMap !== undefined ) { material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap ); }
-			if ( json.clearcoatNormalMap !== undefined ) { material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap ); }
-			if ( json.clearcoatNormalScale !== undefined ) { material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale ); }
+			var tangents = [];
+			var normals = [];
+			var binormals = [];
 
-			return material;
+			var vec = new Vector3();
+			var mat = new Matrix4();
 
-		},
+			var i, u, theta;
 
-		setTextures: function ( value ) {
+			// compute the tangent vectors for each segment on the curve
 
-			this.textures = value;
-			return this;
+			for ( i = 0; i <= segments; i ++ ) {
 
-		}
+				u = i / segments;
 
-	} );
+				tangents[ i ] = this.getTangentAt( u, new Vector3() );
+				tangents[ i ].normalize();
 
-	/**
-	 * @author Don McCurdy / https://www.donmccurdy.com
-	 */
+			}
 
-	var LoaderUtils = {
+			// select an initial normal vector perpendicular to the first tangent vector,
+			// and in the direction of the minimum tangent xyz component
 
-		decodeText: function ( array ) {
+			normals[ 0 ] = new Vector3();
+			binormals[ 0 ] = new Vector3();
+			var min = Number.MAX_VALUE;
+			var tx = Math.abs( tangents[ 0 ].x );
+			var ty = Math.abs( tangents[ 0 ].y );
+			var tz = Math.abs( tangents[ 0 ].z );
 
-			if ( typeof TextDecoder !== 'undefined' ) {
+			if ( tx <= min ) {
 
-				return new TextDecoder().decode( array );
+				min = tx;
+				normal.set( 1, 0, 0 );
 
 			}
 
-			// Avoid the String.fromCharCode.apply(null, array) shortcut, which
-			// throws a "maximum call stack size exceeded" error for large arrays.
+			if ( ty <= min ) {
 
-			var s = '';
+				min = ty;
+				normal.set( 0, 1, 0 );
 
-			for ( var i = 0, il = array.length; i < il; i ++ ) {
+			}
 
-				// Implicitly assumes little-endian.
-				s += String.fromCharCode( array[ i ] );
+			if ( tz <= min ) {
+
+				normal.set( 0, 0, 1 );
 
 			}
 
-			try {
+			vec.crossVectors( tangents[ 0 ], normal ).normalize();
 
-				// merges multi-byte utf-8 characters.
+			normals[ 0 ].crossVectors( tangents[ 0 ], vec );
+			binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
 
-				return decodeURIComponent( escape( s ) );
 
-			} catch ( e ) { // see #16358
+			// compute the slowly-varying normal and binormal vectors for each segment on the curve
 
-				return s;
+			for ( i = 1; i <= segments; i ++ ) {
 
-			}
+				normals[ i ] = normals[ i - 1 ].clone();
 
-		},
+				binormals[ i ] = binormals[ i - 1 ].clone();
 
-		extractUrlBase: function ( url ) {
+				vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
 
-			var index = url.lastIndexOf( '/' );
+				if ( vec.length() > Number.EPSILON ) {
 
-			if ( index === - 1 ) { return './'; }
+					vec.normalize();
 
-			return url.substr( 0, index + 1 );
+					theta = Math.acos( MathUtils.clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
 
-		}
+					normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
 
-	};
+				}
 
-	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
-	 */
+				binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
 
-	function InstancedBufferGeometry() {
+			}
 
-		BufferGeometry.call( this );
+			// if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
 
-		this.type = 'InstancedBufferGeometry';
-		this.maxInstancedCount = undefined;
+			if ( closed === true ) {
 
-	}
+				theta = Math.acos( MathUtils.clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
+				theta /= segments;
 
-	InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry.prototype ), {
+				if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
 
-		constructor: InstancedBufferGeometry,
+					theta = - theta;
 
-		isInstancedBufferGeometry: true,
+				}
 
-		copy: function ( source ) {
+				for ( i = 1; i <= segments; i ++ ) {
 
-			BufferGeometry.prototype.copy.call( this, source );
+					// twist a little...
+					normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
+					binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
 
-			this.maxInstancedCount = source.maxInstancedCount;
+				}
 
-			return this;
+			}
+
+			return {
+				tangents: tangents,
+				normals: normals,
+				binormals: binormals
+			};
 
 		},
 
@@ -48656,1257 +56179,1336 @@ function simpleEnd(buf) {
 
 		},
 
-		toJSON: function () {
+		copy: function ( source ) {
 
-			var data = BufferGeometry.prototype.toJSON.call( this );
+			this.arcLengthDivisions = source.arcLengthDivisions;
 
-			data.maxInstancedCount = this.maxInstancedCount;
+			return this;
 
-			data.isInstancedBufferGeometry = true;
+		},
+
+		toJSON: function () {
+
+			var data = {
+				metadata: {
+					version: 4.5,
+					type: 'Curve',
+					generator: 'Curve.toJSON'
+				}
+			};
+
+			data.arcLengthDivisions = this.arcLengthDivisions;
+			data.type = this.type;
 
 			return data;
 
+		},
+
+		fromJSON: function ( json ) {
+
+			this.arcLengthDivisions = json.arcLengthDivisions;
+
+			return this;
+
 		}
 
 	} );
 
-	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
-	 */
-
-	function InstancedBufferAttribute( array, itemSize, normalized, meshPerAttribute ) {
+	function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
 
-		if ( typeof ( normalized ) === 'number' ) {
+		Curve.call( this );
 
-			meshPerAttribute = normalized;
+		this.type = 'EllipseCurve';
 
-			normalized = false;
+		this.aX = aX || 0;
+		this.aY = aY || 0;
 
-			console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' );
+		this.xRadius = xRadius || 1;
+		this.yRadius = yRadius || 1;
 
-		}
+		this.aStartAngle = aStartAngle || 0;
+		this.aEndAngle = aEndAngle || 2 * Math.PI;
 
-		BufferAttribute.call( this, array, itemSize, normalized );
+		this.aClockwise = aClockwise || false;
 
-		this.meshPerAttribute = meshPerAttribute || 1;
+		this.aRotation = aRotation || 0;
 
 	}
 
-	InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribute.prototype ), {
-
-		constructor: InstancedBufferAttribute,
+	EllipseCurve.prototype = Object.create( Curve.prototype );
+	EllipseCurve.prototype.constructor = EllipseCurve;
 
-		isInstancedBufferAttribute: true,
+	EllipseCurve.prototype.isEllipseCurve = true;
 
-		copy: function ( source ) {
+	EllipseCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
-			BufferAttribute.prototype.copy.call( this, source );
+		var point = optionalTarget || new Vector2();
 
-			this.meshPerAttribute = source.meshPerAttribute;
+		var twoPi = Math.PI * 2;
+		var deltaAngle = this.aEndAngle - this.aStartAngle;
+		var samePoints = Math.abs( deltaAngle ) < Number.EPSILON;
 
-			return this;
+		// ensures that deltaAngle is 0 .. 2 PI
+		while ( deltaAngle < 0 ) { deltaAngle += twoPi; }
+		while ( deltaAngle > twoPi ) { deltaAngle -= twoPi; }
 
-		},
+		if ( deltaAngle < Number.EPSILON ) {
 
-		toJSON: function ()	{
+			if ( samePoints ) {
 
-			var data = BufferAttribute.prototype.toJSON.call( this );
+				deltaAngle = 0;
 
-			data.meshPerAttribute = this.meshPerAttribute;
+			} else {
 
-			data.isInstancedBufferAttribute = true;
+				deltaAngle = twoPi;
 
-			return data;
+			}
 
 		}
 
-	} );
+		if ( this.aClockwise === true && ! samePoints ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			if ( deltaAngle === twoPi ) {
 
-	function BufferGeometryLoader( manager ) {
+				deltaAngle = - twoPi;
 
-		Loader.call( this, manager );
+			} else {
 
-	}
+				deltaAngle = deltaAngle - twoPi;
 
-	BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+			}
 
-		constructor: BufferGeometryLoader,
+		}
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		var angle = this.aStartAngle + t * deltaAngle;
+		var x = this.aX + this.xRadius * Math.cos( angle );
+		var y = this.aY + this.yRadius * Math.sin( angle );
 
-			var scope = this;
+		if ( this.aRotation !== 0 ) {
 
-			var loader = new FileLoader( scope.manager );
-			loader.setPath( scope.path );
-			loader.load( url, function ( text ) {
+			var cos = Math.cos( this.aRotation );
+			var sin = Math.sin( this.aRotation );
 
-				onLoad( scope.parse( JSON.parse( text ) ) );
+			var tx = x - this.aX;
+			var ty = y - this.aY;
 
-			}, onProgress, onError );
+			// Rotate the point about the center of the ellipse.
+			x = tx * cos - ty * sin + this.aX;
+			y = tx * sin + ty * cos + this.aY;
 
-		},
+		}
 
-		parse: function ( json ) {
+		return point.set( x, y );
 
-			var geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
+	};
 
-			var index = json.data.index;
+	EllipseCurve.prototype.copy = function ( source ) {
 
-			if ( index !== undefined ) {
+		Curve.prototype.copy.call( this, source );
 
-				var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
-				geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
+		this.aX = source.aX;
+		this.aY = source.aY;
 
-			}
+		this.xRadius = source.xRadius;
+		this.yRadius = source.yRadius;
 
-			var attributes = json.data.attributes;
+		this.aStartAngle = source.aStartAngle;
+		this.aEndAngle = source.aEndAngle;
 
-			for ( var key in attributes ) {
+		this.aClockwise = source.aClockwise;
 
-				var attribute = attributes[ key ];
-				var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
-				var bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
-				var bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
-				if ( attribute.name !== undefined ) { bufferAttribute.name = attribute.name; }
-				geometry.setAttribute( key, bufferAttribute );
+		this.aRotation = source.aRotation;
 
-			}
+		return this;
 
-			var morphAttributes = json.data.morphAttributes;
+	};
 
-			if ( morphAttributes ) {
 
-				for ( var key in morphAttributes ) {
+	EllipseCurve.prototype.toJSON = function () {
 
-					var attributeArray = morphAttributes[ key ];
+		var data = Curve.prototype.toJSON.call( this );
 
-					var array = [];
+		data.aX = this.aX;
+		data.aY = this.aY;
 
-					for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
+		data.xRadius = this.xRadius;
+		data.yRadius = this.yRadius;
 
-						var attribute = attributeArray[ i ];
-						var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
+		data.aStartAngle = this.aStartAngle;
+		data.aEndAngle = this.aEndAngle;
 
-						var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
-						if ( attribute.name !== undefined ) { bufferAttribute.name = attribute.name; }
-						array.push( bufferAttribute );
+		data.aClockwise = this.aClockwise;
 
-					}
+		data.aRotation = this.aRotation;
 
-					geometry.morphAttributes[ key ] = array;
+		return data;
 
-				}
+	};
 
-			}
+	EllipseCurve.prototype.fromJSON = function ( json ) {
 
-			var morphTargetsRelative = json.data.morphTargetsRelative;
+		Curve.prototype.fromJSON.call( this, json );
 
-			if ( morphTargetsRelative ) {
+		this.aX = json.aX;
+		this.aY = json.aY;
 
-				geometry.morphTargetsRelative = true;
+		this.xRadius = json.xRadius;
+		this.yRadius = json.yRadius;
 
-			}
+		this.aStartAngle = json.aStartAngle;
+		this.aEndAngle = json.aEndAngle;
 
-			var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
+		this.aClockwise = json.aClockwise;
 
-			if ( groups !== undefined ) {
+		this.aRotation = json.aRotation;
 
-				for ( var i = 0, n = groups.length; i !== n; ++ i ) {
+		return this;
 
-					var group = groups[ i ];
+	};
 
-					geometry.addGroup( group.start, group.count, group.materialIndex );
+	function ArcCurve( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
 
-				}
+		EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
 
-			}
+		this.type = 'ArcCurve';
 
-			var boundingSphere = json.data.boundingSphere;
+	}
 
-			if ( boundingSphere !== undefined ) {
+	ArcCurve.prototype = Object.create( EllipseCurve.prototype );
+	ArcCurve.prototype.constructor = ArcCurve;
 
-				var center = new Vector3();
+	ArcCurve.prototype.isArcCurve = true;
 
-				if ( boundingSphere.center !== undefined ) {
+	/**
+	 * @author zz85 https://github.com/zz85
+	 *
+	 * Centripetal CatmullRom Curve - which is useful for avoiding
+	 * cusps and self-intersections in non-uniform catmull rom curves.
+	 * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
+	 *
+	 * curve.type accepts centripetal(default), chordal and catmullrom
+	 * curve.tension is used for catmullrom which defaults to 0.5
+	 */
 
-					center.fromArray( boundingSphere.center );
 
-				}
+	/*
+	Based on an optimized c++ solution in
+	 - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
+	 - http://ideone.com/NoEbVM
 
-				geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
+	This CubicPoly class could be used for reusing some variables and calculations,
+	but for three.js curve use, it could be possible inlined and flatten into a single function call
+	which can be placed in CurveUtils.
+	*/
 
-			}
+	function CubicPoly() {
 
-			if ( json.name ) { geometry.name = json.name; }
-			if ( json.userData ) { geometry.userData = json.userData; }
+		var c0 = 0, c1 = 0, c2 = 0, c3 = 0;
 
-			return geometry;
+		/*
+		 * Compute coefficients for a cubic polynomial
+		 *   p(s) = c0 + c1*s + c2*s^2 + c3*s^3
+		 * such that
+		 *   p(0) = x0, p(1) = x1
+		 *  and
+		 *   p'(0) = t0, p'(1) = t1.
+		 */
+		function init( x0, x1, t0, t1 ) {
+
+			c0 = x0;
+			c1 = t0;
+			c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
+			c3 = 2 * x0 - 2 * x1 + t0 + t1;
 
 		}
 
-	} );
+		return {
 
-	var TYPED_ARRAYS = {
-		Int8Array: Int8Array,
-		Uint8Array: Uint8Array,
-		// Workaround for IE11 pre KB2929437. See #11440
-		Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
-		Int16Array: Int16Array,
-		Uint16Array: Uint16Array,
-		Int32Array: Int32Array,
-		Uint32Array: Uint32Array,
-		Float32Array: Float32Array,
-		Float64Array: Float64Array
-	};
+			initCatmullRom: function ( x0, x1, x2, x3, tension ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
 
-	function ObjectLoader( manager ) {
+			},
 
-		Loader.call( this, manager );
+			initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
 
-	}
+				// compute tangents when parameterized in [t1,t2]
+				var t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
+				var t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
 
-	ObjectLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+				// rescale tangents for parametrization in [0,1]
+				t1 *= dt1;
+				t2 *= dt1;
 
-		constructor: ObjectLoader,
+				init( x1, x2, t1, t2 );
 
-		load: function ( url, onLoad, onProgress, onError ) {
+			},
 
-			var scope = this;
+			calc: function ( t ) {
 
-			var path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
-			this.resourcePath = this.resourcePath || path;
+				var t2 = t * t;
+				var t3 = t2 * t;
+				return c0 + c1 * t + c2 * t2 + c3 * t3;
 
-			var loader = new FileLoader( scope.manager );
-			loader.setPath( this.path );
-			loader.load( url, function ( text ) {
+			}
 
-				var json = null;
+		};
 
-				try {
+	}
 
-					json = JSON.parse( text );
+	//
 
-				} catch ( error ) {
+	var tmp = new Vector3();
+	var px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
 
-					if ( onError !== undefined ) { onError( error ); }
+	function CatmullRomCurve3( points, closed, curveType, tension ) {
 
-					console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
+		Curve.call( this );
 
-					return;
+		this.type = 'CatmullRomCurve3';
 
-				}
+		this.points = points || [];
+		this.closed = closed || false;
+		this.curveType = curveType || 'centripetal';
+		this.tension = tension || 0.5;
 
-				var metadata = json.metadata;
+	}
 
-				if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
+	CatmullRomCurve3.prototype = Object.create( Curve.prototype );
+	CatmullRomCurve3.prototype.constructor = CatmullRomCurve3;
 
-					console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
-					return;
+	CatmullRomCurve3.prototype.isCatmullRomCurve3 = true;
 
-				}
+	CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
-				scope.parse( json, onLoad );
+		var point = optionalTarget || new Vector3();
 
-			}, onProgress, onError );
+		var points = this.points;
+		var l = points.length;
 
-		},
+		var p = ( l - ( this.closed ? 0 : 1 ) ) * t;
+		var intPoint = Math.floor( p );
+		var weight = p - intPoint;
 
-		parse: function ( json, onLoad ) {
+		if ( this.closed ) {
 
-			var shapes = this.parseShape( json.shapes );
-			var geometries = this.parseGeometries( json.geometries, shapes );
+			intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
 
-			var images = this.parseImages( json.images, function () {
+		} else if ( weight === 0 && intPoint === l - 1 ) {
 
-				if ( onLoad !== undefined ) { onLoad( object ); }
+			intPoint = l - 2;
+			weight = 1;
 
-			} );
+		}
 
-			var textures = this.parseTextures( json.textures, images );
-			var materials = this.parseMaterials( json.materials, textures );
+		var p0, p1, p2, p3; // 4 points
 
-			var object = this.parseObject( json.object, geometries, materials );
+		if ( this.closed || intPoint > 0 ) {
 
-			if ( json.animations ) {
+			p0 = points[ ( intPoint - 1 ) % l ];
 
-				object.animations = this.parseAnimations( json.animations );
+		} else {
 
-			}
+			// extrapolate first point
+			tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
+			p0 = tmp;
 
-			if ( json.images === undefined || json.images.length === 0 ) {
+		}
 
-				if ( onLoad !== undefined ) { onLoad( object ); }
+		p1 = points[ intPoint % l ];
+		p2 = points[ ( intPoint + 1 ) % l ];
 
-			}
+		if ( this.closed || intPoint + 2 < l ) {
 
-			return object;
+			p3 = points[ ( intPoint + 2 ) % l ];
 
-		},
+		} else {
 
-		parseShape: function ( json ) {
+			// extrapolate last point
+			tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
+			p3 = tmp;
 
-			var shapes = {};
+		}
 
-			if ( json !== undefined ) {
+		if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
 
-				for ( var i = 0, l = json.length; i < l; i ++ ) {
+			// init Centripetal / Chordal Catmull-Rom
+			var pow = this.curveType === 'chordal' ? 0.5 : 0.25;
+			var dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
+			var dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
+			var dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
 
-					var shape = new Shape().fromJSON( json[ i ] );
+			// safety check for repeated points
+			if ( dt1 < 1e-4 ) { dt1 = 1.0; }
+			if ( dt0 < 1e-4 ) { dt0 = dt1; }
+			if ( dt2 < 1e-4 ) { dt2 = dt1; }
 
-					shapes[ shape.uuid ] = shape;
+			px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
+			py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
+			pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
 
-				}
+		} else if ( this.curveType === 'catmullrom' ) {
 
-			}
+			px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
+			py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
+			pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
 
-			return shapes;
+		}
 
-		},
+		point.set(
+			px.calc( weight ),
+			py.calc( weight ),
+			pz.calc( weight )
+		);
 
-		parseGeometries: function ( json, shapes ) {
+		return point;
 
-			var geometries = {};
+	};
 
-			if ( json !== undefined ) {
+	CatmullRomCurve3.prototype.copy = function ( source ) {
 
-				var bufferGeometryLoader = new BufferGeometryLoader();
+		Curve.prototype.copy.call( this, source );
 
-				for ( var i = 0, l = json.length; i < l; i ++ ) {
+		this.points = [];
 
-					var geometry;
-					var data = json[ i ];
+		for ( var i = 0, l = source.points.length; i < l; i ++ ) {
 
-					switch ( data.type ) {
+			var point = source.points[ i ];
 
-						case 'PlaneGeometry':
-						case 'PlaneBufferGeometry':
+			this.points.push( point.clone() );
 
-							geometry = new Geometries[ data.type ](
-								data.width,
-								data.height,
-								data.widthSegments,
-								data.heightSegments
-							);
+		}
 
-							break;
+		this.closed = source.closed;
+		this.curveType = source.curveType;
+		this.tension = source.tension;
 
-						case 'BoxGeometry':
-						case 'BoxBufferGeometry':
-						case 'CubeGeometry': // backwards compatible
+		return this;
 
-							geometry = new Geometries[ data.type ](
-								data.width,
-								data.height,
-								data.depth,
-								data.widthSegments,
-								data.heightSegments,
-								data.depthSegments
-							);
+	};
 
-							break;
+	CatmullRomCurve3.prototype.toJSON = function () {
 
-						case 'CircleGeometry':
-						case 'CircleBufferGeometry':
+		var data = Curve.prototype.toJSON.call( this );
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.segments,
-								data.thetaStart,
-								data.thetaLength
-							);
+		data.points = [];
 
-							break;
+		for ( var i = 0, l = this.points.length; i < l; i ++ ) {
 
-						case 'CylinderGeometry':
-						case 'CylinderBufferGeometry':
+			var point = this.points[ i ];
+			data.points.push( point.toArray() );
 
-							geometry = new Geometries[ data.type ](
-								data.radiusTop,
-								data.radiusBottom,
-								data.height,
-								data.radialSegments,
-								data.heightSegments,
-								data.openEnded,
-								data.thetaStart,
-								data.thetaLength
-							);
+		}
 
-							break;
+		data.closed = this.closed;
+		data.curveType = this.curveType;
+		data.tension = this.tension;
 
-						case 'ConeGeometry':
-						case 'ConeBufferGeometry':
+		return data;
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.height,
-								data.radialSegments,
-								data.heightSegments,
-								data.openEnded,
-								data.thetaStart,
-								data.thetaLength
-							);
+	};
 
-							break;
+	CatmullRomCurve3.prototype.fromJSON = function ( json ) {
 
-						case 'SphereGeometry':
-						case 'SphereBufferGeometry':
+		Curve.prototype.fromJSON.call( this, json );
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.widthSegments,
-								data.heightSegments,
-								data.phiStart,
-								data.phiLength,
-								data.thetaStart,
-								data.thetaLength
-							);
+		this.points = [];
 
-							break;
+		for ( var i = 0, l = json.points.length; i < l; i ++ ) {
 
-						case 'DodecahedronGeometry':
-						case 'DodecahedronBufferGeometry':
-						case 'IcosahedronGeometry':
-						case 'IcosahedronBufferGeometry':
-						case 'OctahedronGeometry':
-						case 'OctahedronBufferGeometry':
-						case 'TetrahedronGeometry':
-						case 'TetrahedronBufferGeometry':
+			var point = json.points[ i ];
+			this.points.push( new Vector3().fromArray( point ) );
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.detail
-							);
+		}
 
-							break;
+		this.closed = json.closed;
+		this.curveType = json.curveType;
+		this.tension = json.tension;
 
-						case 'RingGeometry':
-						case 'RingBufferGeometry':
+		return this;
 
-							geometry = new Geometries[ data.type ](
-								data.innerRadius,
-								data.outerRadius,
-								data.thetaSegments,
-								data.phiSegments,
-								data.thetaStart,
-								data.thetaLength
-							);
+	};
 
-							break;
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 *
+	 * Bezier Curves formulas obtained from
+	 * http://en.wikipedia.org/wiki/Bézier_curve
+	 */
 
-						case 'TorusGeometry':
-						case 'TorusBufferGeometry':
+	function CatmullRom( t, p0, p1, p2, p3 ) {
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.tube,
-								data.radialSegments,
-								data.tubularSegments,
-								data.arc
-							);
+		var v0 = ( p2 - p0 ) * 0.5;
+		var v1 = ( p3 - p1 ) * 0.5;
+		var t2 = t * t;
+		var t3 = t * t2;
+		return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
 
-							break;
+	}
 
-						case 'TorusKnotGeometry':
-						case 'TorusKnotBufferGeometry':
+	//
 
-							geometry = new Geometries[ data.type ](
-								data.radius,
-								data.tube,
-								data.tubularSegments,
-								data.radialSegments,
-								data.p,
-								data.q
-							);
+	function QuadraticBezierP0( t, p ) {
 
-							break;
+		var k = 1 - t;
+		return k * k * p;
 
-						case 'TubeGeometry':
-						case 'TubeBufferGeometry':
+	}
 
-							// This only works for built-in curves (e.g. CatmullRomCurve3).
-							// User defined curves or instances of CurvePath will not be deserialized.
-							geometry = new Geometries[ data.type ](
-								new Curves[ data.path.type ]().fromJSON( data.path ),
-								data.tubularSegments,
-								data.radius,
-								data.radialSegments,
-								data.closed
-							);
+	function QuadraticBezierP1( t, p ) {
 
-							break;
+		return 2 * ( 1 - t ) * t * p;
 
-						case 'LatheGeometry':
-						case 'LatheBufferGeometry':
+	}
 
-							geometry = new Geometries[ data.type ](
-								data.points,
-								data.segments,
-								data.phiStart,
-								data.phiLength
-							);
+	function QuadraticBezierP2( t, p ) {
 
-							break;
+		return t * t * p;
 
-						case 'PolyhedronGeometry':
-						case 'PolyhedronBufferGeometry':
+	}
 
-							geometry = new Geometries[ data.type ](
-								data.vertices,
-								data.indices,
-								data.radius,
-								data.details
-							);
+	function QuadraticBezier( t, p0, p1, p2 ) {
 
-							break;
+		return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) +
+			QuadraticBezierP2( t, p2 );
 
-						case 'ShapeGeometry':
-						case 'ShapeBufferGeometry':
+	}
 
-							var geometryShapes = [];
+	//
 
-							for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {
+	function CubicBezierP0( t, p ) {
 
-								var shape = shapes[ data.shapes[ j ] ];
+		var k = 1 - t;
+		return k * k * k * p;
 
-								geometryShapes.push( shape );
+	}
 
-							}
+	function CubicBezierP1( t, p ) {
 
-							geometry = new Geometries[ data.type ](
-								geometryShapes,
-								data.curveSegments
-							);
+		var k = 1 - t;
+		return 3 * k * k * t * p;
 
-							break;
+	}
 
+	function CubicBezierP2( t, p ) {
 
-						case 'ExtrudeGeometry':
-						case 'ExtrudeBufferGeometry':
+		return 3 * ( 1 - t ) * t * t * p;
 
-							var geometryShapes = [];
+	}
 
-							for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {
+	function CubicBezierP3( t, p ) {
 
-								var shape = shapes[ data.shapes[ j ] ];
+		return t * t * t * p;
 
-								geometryShapes.push( shape );
+	}
 
-							}
+	function CubicBezier( t, p0, p1, p2, p3 ) {
 
-							var extrudePath = data.options.extrudePath;
+		return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) +
+			CubicBezierP3( t, p3 );
 
-							if ( extrudePath !== undefined ) {
+	}
 
-								data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );
+	function CubicBezierCurve( v0, v1, v2, v3 ) {
 
-							}
+		Curve.call( this );
 
-							geometry = new Geometries[ data.type ](
-								geometryShapes,
-								data.options
-							);
+		this.type = 'CubicBezierCurve';
 
-							break;
+		this.v0 = v0 || new Vector2();
+		this.v1 = v1 || new Vector2();
+		this.v2 = v2 || new Vector2();
+		this.v3 = v3 || new Vector2();
 
-						case 'BufferGeometry':
-						case 'InstancedBufferGeometry':
+	}
 
-							geometry = bufferGeometryLoader.parse( data );
+	CubicBezierCurve.prototype = Object.create( Curve.prototype );
+	CubicBezierCurve.prototype.constructor = CubicBezierCurve;
 
-							break;
+	CubicBezierCurve.prototype.isCubicBezierCurve = true;
 
-						case 'Geometry':
+	CubicBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
-							console.error( 'THREE.ObjectLoader: Loading "Geometry" is not supported anymore.' );
+		var point = optionalTarget || new Vector2();
 
-							break;
+		var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
 
-						default:
+		point.set(
+			CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
+			CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
+		);
 
-							console.warn( 'THREE.ObjectLoader: Unsupported geometry type "' + data.type + '"' );
+		return point;
 
-							continue;
+	};
 
-					}
+	CubicBezierCurve.prototype.copy = function ( source ) {
 
-					geometry.uuid = data.uuid;
+		Curve.prototype.copy.call( this, source );
 
-					if ( data.name !== undefined ) { geometry.name = data.name; }
-					if ( geometry.isBufferGeometry === true && data.userData !== undefined ) { geometry.userData = data.userData; }
+		this.v0.copy( source.v0 );
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
+		this.v3.copy( source.v3 );
 
-					geometries[ data.uuid ] = geometry;
+		return this;
 
-				}
+	};
 
-			}
+	CubicBezierCurve.prototype.toJSON = function () {
 
-			return geometries;
+		var data = Curve.prototype.toJSON.call( this );
 
-		},
+		data.v0 = this.v0.toArray();
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
+		data.v3 = this.v3.toArray();
 
-		parseMaterials: function ( json, textures ) {
+		return data;
 
-			var cache = {}; // MultiMaterial
-			var materials = {};
+	};
 
-			if ( json !== undefined ) {
+	CubicBezierCurve.prototype.fromJSON = function ( json ) {
 
-				var loader = new MaterialLoader();
-				loader.setTextures( textures );
+		Curve.prototype.fromJSON.call( this, json );
 
-				for ( var i = 0, l = json.length; i < l; i ++ ) {
+		this.v0.fromArray( json.v0 );
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
+		this.v3.fromArray( json.v3 );
 
-					var data = json[ i ];
+		return this;
 
-					if ( data.type === 'MultiMaterial' ) {
+	};
 
-						// Deprecated
+	function CubicBezierCurve3( v0, v1, v2, v3 ) {
 
-						var array = [];
+		Curve.call( this );
 
-						for ( var j = 0; j < data.materials.length; j ++ ) {
+		this.type = 'CubicBezierCurve3';
 
-							var material = data.materials[ j ];
+		this.v0 = v0 || new Vector3();
+		this.v1 = v1 || new Vector3();
+		this.v2 = v2 || new Vector3();
+		this.v3 = v3 || new Vector3();
 
-							if ( cache[ material.uuid ] === undefined ) {
+	}
 
-								cache[ material.uuid ] = loader.parse( material );
+	CubicBezierCurve3.prototype = Object.create( Curve.prototype );
+	CubicBezierCurve3.prototype.constructor = CubicBezierCurve3;
 
-							}
+	CubicBezierCurve3.prototype.isCubicBezierCurve3 = true;
 
-							array.push( cache[ material.uuid ] );
+	CubicBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
-						}
+		var point = optionalTarget || new Vector3();
 
-						materials[ data.uuid ] = array;
+		var v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
 
-					} else {
+		point.set(
+			CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
+			CubicBezier( t, v0.y, v1.y, v2.y, v3.y ),
+			CubicBezier( t, v0.z, v1.z, v2.z, v3.z )
+		);
 
-						if ( cache[ data.uuid ] === undefined ) {
+		return point;
 
-							cache[ data.uuid ] = loader.parse( data );
+	};
 
-						}
+	CubicBezierCurve3.prototype.copy = function ( source ) {
 
-						materials[ data.uuid ] = cache[ data.uuid ];
+		Curve.prototype.copy.call( this, source );
 
-					}
+		this.v0.copy( source.v0 );
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
+		this.v3.copy( source.v3 );
 
-				}
+		return this;
 
-			}
+	};
 
-			return materials;
+	CubicBezierCurve3.prototype.toJSON = function () {
 
-		},
+		var data = Curve.prototype.toJSON.call( this );
 
-		parseAnimations: function ( json ) {
+		data.v0 = this.v0.toArray();
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
+		data.v3 = this.v3.toArray();
 
-			var animations = [];
+		return data;
 
-			for ( var i = 0; i < json.length; i ++ ) {
+	};
 
-				var data = json[ i ];
+	CubicBezierCurve3.prototype.fromJSON = function ( json ) {
 
-				var clip = AnimationClip.parse( data );
+		Curve.prototype.fromJSON.call( this, json );
 
-				if ( data.uuid !== undefined ) { clip.uuid = data.uuid; }
+		this.v0.fromArray( json.v0 );
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
+		this.v3.fromArray( json.v3 );
 
-				animations.push( clip );
+		return this;
 
-			}
+	};
 
-			return animations;
+	function LineCurve( v1, v2 ) {
 
-		},
+		Curve.call( this );
 
-		parseImages: function ( json, onLoad ) {
+		this.type = 'LineCurve';
 
-			var scope = this;
-			var images = {};
+		this.v1 = v1 || new Vector2();
+		this.v2 = v2 || new Vector2();
 
-			function loadImage( url ) {
+	}
 
-				scope.manager.itemStart( url );
+	LineCurve.prototype = Object.create( Curve.prototype );
+	LineCurve.prototype.constructor = LineCurve;
 
-				return loader.load( url, function () {
+	LineCurve.prototype.isLineCurve = true;
 
-					scope.manager.itemEnd( url );
+	LineCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
-				}, undefined, function () {
+		var point = optionalTarget || new Vector2();
 
-					scope.manager.itemError( url );
-					scope.manager.itemEnd( url );
+		if ( t === 1 ) {
 
-				} );
+			point.copy( this.v2 );
 
-			}
+		} else {
 
-			if ( json !== undefined && json.length > 0 ) {
+			point.copy( this.v2 ).sub( this.v1 );
+			point.multiplyScalar( t ).add( this.v1 );
 
-				var manager = new LoadingManager( onLoad );
+		}
 
-				var loader = new ImageLoader( manager );
-				loader.setCrossOrigin( this.crossOrigin );
+		return point;
 
-				for ( var i = 0, il = json.length; i < il; i ++ ) {
+	};
 
-					var image = json[ i ];
-					var url = image.url;
+	// Line curve is linear, so we can overwrite default getPointAt
 
-					if ( Array.isArray( url ) ) {
+	LineCurve.prototype.getPointAt = function ( u, optionalTarget ) {
 
-						// load array of images e.g CubeTexture
+		return this.getPoint( u, optionalTarget );
 
-						images[ image.uuid ] = [];
+	};
 
-						for ( var j = 0, jl = url.length; j < jl; j ++ ) {
+	LineCurve.prototype.getTangent = function ( t, optionalTarget ) {
 
-							var currentUrl = url[ j ];
+		var tangent = optionalTarget || new Vector2();
 
-							var path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( currentUrl ) ? currentUrl : scope.resourcePath + currentUrl;
+		var tangent = tangent.copy( this.v2 ).sub( this.v1 ).normalize();
 
-							images[ image.uuid ].push( loadImage( path ) );
+		return tangent;
 
-						}
+	};
 
-					} else {
+	LineCurve.prototype.copy = function ( source ) {
 
-						// load single image
+		Curve.prototype.copy.call( this, source );
 
-						var path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( image.url ) ? image.url : scope.resourcePath + image.url;
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
 
-						images[ image.uuid ] = loadImage( path );
+		return this;
 
-					}
+	};
 
-				}
+	LineCurve.prototype.toJSON = function () {
 
-			}
+		var data = Curve.prototype.toJSON.call( this );
 
-			return images;
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
 
-		},
+		return data;
 
-		parseTextures: function ( json, images ) {
+	};
 
-			function parseConstant( value, type ) {
+	LineCurve.prototype.fromJSON = function ( json ) {
 
-				if ( typeof value === 'number' ) { return value; }
+		Curve.prototype.fromJSON.call( this, json );
 
-				console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
 
-				return type[ value ];
+		return this;
 
-			}
+	};
 
-			var textures = {};
+	function LineCurve3( v1, v2 ) {
 
-			if ( json !== undefined ) {
+		Curve.call( this );
 
-				for ( var i = 0, l = json.length; i < l; i ++ ) {
+		this.type = 'LineCurve3';
 
-					var data = json[ i ];
+		this.v1 = v1 || new Vector3();
+		this.v2 = v2 || new Vector3();
 
-					if ( data.image === undefined ) {
+	}
 
-						console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
+	LineCurve3.prototype = Object.create( Curve.prototype );
+	LineCurve3.prototype.constructor = LineCurve3;
 
-					}
+	LineCurve3.prototype.isLineCurve3 = true;
 
-					if ( images[ data.image ] === undefined ) {
+	LineCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
-						console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
+		var point = optionalTarget || new Vector3();
 
-					}
+		if ( t === 1 ) {
 
-					var texture;
+			point.copy( this.v2 );
 
-					if ( Array.isArray( images[ data.image ] ) ) {
+		} else {
 
-						texture = new CubeTexture( images[ data.image ] );
+			point.copy( this.v2 ).sub( this.v1 );
+			point.multiplyScalar( t ).add( this.v1 );
 
-					} else {
+		}
 
-						texture = new Texture( images[ data.image ] );
+		return point;
 
-					}
+	};
 
-					texture.needsUpdate = true;
+	// Line curve is linear, so we can overwrite default getPointAt
 
-					texture.uuid = data.uuid;
-
-					if ( data.name !== undefined ) { texture.name = data.name; }
+	LineCurve3.prototype.getPointAt = function ( u, optionalTarget ) {
 
-					if ( data.mapping !== undefined ) { texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING ); }
+		return this.getPoint( u, optionalTarget );
 
-					if ( data.offset !== undefined ) { texture.offset.fromArray( data.offset ); }
-					if ( data.repeat !== undefined ) { texture.repeat.fromArray( data.repeat ); }
-					if ( data.center !== undefined ) { texture.center.fromArray( data.center ); }
-					if ( data.rotation !== undefined ) { texture.rotation = data.rotation; }
+	};
 
-					if ( data.wrap !== undefined ) {
+	LineCurve3.prototype.copy = function ( source ) {
 
-						texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
-						texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
+		Curve.prototype.copy.call( this, source );
 
-					}
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
 
-					if ( data.format !== undefined ) { texture.format = data.format; }
-					if ( data.type !== undefined ) { texture.type = data.type; }
-					if ( data.encoding !== undefined ) { texture.encoding = data.encoding; }
+		return this;
 
-					if ( data.minFilter !== undefined ) { texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER ); }
-					if ( data.magFilter !== undefined ) { texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER ); }
-					if ( data.anisotropy !== undefined ) { texture.anisotropy = data.anisotropy; }
+	};
 
-					if ( data.flipY !== undefined ) { texture.flipY = data.flipY; }
+	LineCurve3.prototype.toJSON = function () {
 
-					if ( data.premultiplyAlpha !== undefined ) { texture.premultiplyAlpha = data.premultiplyAlpha; }
-					if ( data.unpackAlignment !== undefined ) { texture.unpackAlignment = data.unpackAlignment; }
+		var data = Curve.prototype.toJSON.call( this );
 
-					textures[ data.uuid ] = texture;
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
 
-				}
+		return data;
 
-			}
+	};
 
-			return textures;
+	LineCurve3.prototype.fromJSON = function ( json ) {
 
-		},
+		Curve.prototype.fromJSON.call( this, json );
 
-		parseObject: function ( data, geometries, materials ) {
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
 
-			var object;
+		return this;
 
-			function getGeometry( name ) {
+	};
 
-				if ( geometries[ name ] === undefined ) {
+	function QuadraticBezierCurve( v0, v1, v2 ) {
 
-					console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
+		Curve.call( this );
 
-				}
+		this.type = 'QuadraticBezierCurve';
 
-				return geometries[ name ];
+		this.v0 = v0 || new Vector2();
+		this.v1 = v1 || new Vector2();
+		this.v2 = v2 || new Vector2();
 
-			}
+	}
 
-			function getMaterial( name ) {
+	QuadraticBezierCurve.prototype = Object.create( Curve.prototype );
+	QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve;
 
-				if ( name === undefined ) { return undefined; }
+	QuadraticBezierCurve.prototype.isQuadraticBezierCurve = true;
 
-				if ( Array.isArray( name ) ) {
+	QuadraticBezierCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
-					var array = [];
+		var point = optionalTarget || new Vector2();
 
-					for ( var i = 0, l = name.length; i < l; i ++ ) {
+		var v0 = this.v0, v1 = this.v1, v2 = this.v2;
 
-						var uuid = name[ i ];
+		point.set(
+			QuadraticBezier( t, v0.x, v1.x, v2.x ),
+			QuadraticBezier( t, v0.y, v1.y, v2.y )
+		);
 
-						if ( materials[ uuid ] === undefined ) {
+		return point;
 
-							console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
+	};
 
-						}
+	QuadraticBezierCurve.prototype.copy = function ( source ) {
 
-						array.push( materials[ uuid ] );
+		Curve.prototype.copy.call( this, source );
 
-					}
+		this.v0.copy( source.v0 );
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
 
-					return array;
+		return this;
 
-				}
+	};
 
-				if ( materials[ name ] === undefined ) {
+	QuadraticBezierCurve.prototype.toJSON = function () {
 
-					console.warn( 'THREE.ObjectLoader: Undefined material', name );
+		var data = Curve.prototype.toJSON.call( this );
 
-				}
+		data.v0 = this.v0.toArray();
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
 
-				return materials[ name ];
+		return data;
 
-			}
+	};
 
-			switch ( data.type ) {
+	QuadraticBezierCurve.prototype.fromJSON = function ( json ) {
 
-				case 'Scene':
+		Curve.prototype.fromJSON.call( this, json );
 
-					object = new Scene();
+		this.v0.fromArray( json.v0 );
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
 
-					if ( data.background !== undefined ) {
+		return this;
 
-						if ( Number.isInteger( data.background ) ) {
+	};
 
-							object.background = new Color( data.background );
+	function QuadraticBezierCurve3( v0, v1, v2 ) {
 
-						}
+		Curve.call( this );
 
-					}
+		this.type = 'QuadraticBezierCurve3';
 
-					if ( data.fog !== undefined ) {
+		this.v0 = v0 || new Vector3();
+		this.v1 = v1 || new Vector3();
+		this.v2 = v2 || new Vector3();
 
-						if ( data.fog.type === 'Fog' ) {
+	}
 
-							object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
+	QuadraticBezierCurve3.prototype = Object.create( Curve.prototype );
+	QuadraticBezierCurve3.prototype.constructor = QuadraticBezierCurve3;
 
-						} else if ( data.fog.type === 'FogExp2' ) {
+	QuadraticBezierCurve3.prototype.isQuadraticBezierCurve3 = true;
 
-							object.fog = new FogExp2( data.fog.color, data.fog.density );
+	QuadraticBezierCurve3.prototype.getPoint = function ( t, optionalTarget ) {
 
-						}
+		var point = optionalTarget || new Vector3();
 
-					}
+		var v0 = this.v0, v1 = this.v1, v2 = this.v2;
 
-					break;
+		point.set(
+			QuadraticBezier( t, v0.x, v1.x, v2.x ),
+			QuadraticBezier( t, v0.y, v1.y, v2.y ),
+			QuadraticBezier( t, v0.z, v1.z, v2.z )
+		);
 
-				case 'PerspectiveCamera':
+		return point;
 
-					object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
+	};
 
-					if ( data.focus !== undefined ) { object.focus = data.focus; }
-					if ( data.zoom !== undefined ) { object.zoom = data.zoom; }
-					if ( data.filmGauge !== undefined ) { object.filmGauge = data.filmGauge; }
-					if ( data.filmOffset !== undefined ) { object.filmOffset = data.filmOffset; }
-					if ( data.view !== undefined ) { object.view = Object.assign( {}, data.view ); }
+	QuadraticBezierCurve3.prototype.copy = function ( source ) {
 
-					break;
+		Curve.prototype.copy.call( this, source );
 
-				case 'OrthographicCamera':
+		this.v0.copy( source.v0 );
+		this.v1.copy( source.v1 );
+		this.v2.copy( source.v2 );
 
-					object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
+		return this;
 
-					if ( data.zoom !== undefined ) { object.zoom = data.zoom; }
-					if ( data.view !== undefined ) { object.view = Object.assign( {}, data.view ); }
+	};
 
-					break;
+	QuadraticBezierCurve3.prototype.toJSON = function () {
 
-				case 'AmbientLight':
+		var data = Curve.prototype.toJSON.call( this );
 
-					object = new AmbientLight( data.color, data.intensity );
+		data.v0 = this.v0.toArray();
+		data.v1 = this.v1.toArray();
+		data.v2 = this.v2.toArray();
 
-					break;
+		return data;
 
-				case 'DirectionalLight':
+	};
 
-					object = new DirectionalLight( data.color, data.intensity );
+	QuadraticBezierCurve3.prototype.fromJSON = function ( json ) {
 
-					break;
+		Curve.prototype.fromJSON.call( this, json );
 
-				case 'PointLight':
+		this.v0.fromArray( json.v0 );
+		this.v1.fromArray( json.v1 );
+		this.v2.fromArray( json.v2 );
 
-					object = new PointLight( data.color, data.intensity, data.distance, data.decay );
+		return this;
 
-					break;
+	};
 
-				case 'RectAreaLight':
+	function SplineCurve( points /* array of Vector2 */ ) {
 
-					object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
+		Curve.call( this );
 
-					break;
+		this.type = 'SplineCurve';
 
-				case 'SpotLight':
+		this.points = points || [];
 
-					object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
+	}
 
-					break;
+	SplineCurve.prototype = Object.create( Curve.prototype );
+	SplineCurve.prototype.constructor = SplineCurve;
 
-				case 'HemisphereLight':
+	SplineCurve.prototype.isSplineCurve = true;
 
-					object = new HemisphereLight( data.color, data.groundColor, data.intensity );
+	SplineCurve.prototype.getPoint = function ( t, optionalTarget ) {
 
-					break;
+		var point = optionalTarget || new Vector2();
 
-				case 'LightProbe':
+		var points = this.points;
+		var p = ( points.length - 1 ) * t;
 
-					object = new LightProbe().fromJSON( data );
+		var intPoint = Math.floor( p );
+		var weight = p - intPoint;
 
-					break;
+		var p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ];
+		var p1 = points[ intPoint ];
+		var p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
+		var p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
 
-				case 'SkinnedMesh':
+		point.set(
+			CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ),
+			CatmullRom( weight, p0.y, p1.y, p2.y, p3.y )
+		);
 
-					console.warn( 'THREE.ObjectLoader.parseObject() does not support SkinnedMesh yet.' );
+		return point;
 
-				case 'Mesh':
+	};
 
-					var geometry = getGeometry( data.geometry );
-					var material = getMaterial( data.material );
+	SplineCurve.prototype.copy = function ( source ) {
 
-					object = new Mesh( geometry, material );
+		Curve.prototype.copy.call( this, source );
 
-					break;
+		this.points = [];
 
-				case 'InstancedMesh':
+		for ( var i = 0, l = source.points.length; i < l; i ++ ) {
 
-					var geometry = getGeometry( data.geometry );
-					var material = getMaterial( data.material );
-					var count = data.count;
-					var instanceMatrix = data.instanceMatrix;
+			var point = source.points[ i ];
 
-					object = new InstancedMesh( geometry, material, count );
-					object.instanceMatrix = new BufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
+			this.points.push( point.clone() );
 
-					break;
+		}
 
-				case 'LOD':
+		return this;
 
-					object = new LOD();
+	};
 
-					break;
+	SplineCurve.prototype.toJSON = function () {
 
-				case 'Line':
+		var data = Curve.prototype.toJSON.call( this );
 
-					object = new Line( getGeometry( data.geometry ), getMaterial( data.material ), data.mode );
+		data.points = [];
 
-					break;
+		for ( var i = 0, l = this.points.length; i < l; i ++ ) {
 
-				case 'LineLoop':
+			var point = this.points[ i ];
+			data.points.push( point.toArray() );
 
-					object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
+		}
 
-					break;
+		return data;
 
-				case 'LineSegments':
+	};
 
-					object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
+	SplineCurve.prototype.fromJSON = function ( json ) {
 
-					break;
+		Curve.prototype.fromJSON.call( this, json );
 
-				case 'PointCloud':
-				case 'Points':
+		this.points = [];
 
-					object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
+		for ( var i = 0, l = json.points.length; i < l; i ++ ) {
 
-					break;
+			var point = json.points[ i ];
+			this.points.push( new Vector2().fromArray( point ) );
 
-				case 'Sprite':
+		}
 
-					object = new Sprite( getMaterial( data.material ) );
+		return this;
 
-					break;
+	};
 
-				case 'Group':
+	var Curves = /*#__PURE__*/Object.freeze({
+		__proto__: null,
+		ArcCurve: ArcCurve,
+		CatmullRomCurve3: CatmullRomCurve3,
+		CubicBezierCurve: CubicBezierCurve,
+		CubicBezierCurve3: CubicBezierCurve3,
+		EllipseCurve: EllipseCurve,
+		LineCurve: LineCurve,
+		LineCurve3: LineCurve3,
+		QuadraticBezierCurve: QuadraticBezierCurve,
+		QuadraticBezierCurve3: QuadraticBezierCurve3,
+		SplineCurve: SplineCurve
+	});
 
-					object = new Group();
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 *
+	 **/
 
-					break;
+	/**************************************************************
+	 *	Curved Path - a curve path is simply a array of connected
+	 *  curves, but retains the api of a curve
+	 **************************************************************/
 
-				default:
+	function CurvePath() {
 
-					object = new Object3D();
+		Curve.call( this );
 
-			}
+		this.type = 'CurvePath';
 
-			object.uuid = data.uuid;
+		this.curves = [];
+		this.autoClose = false; // Automatically closes the path
 
-			if ( data.name !== undefined ) { object.name = data.name; }
+	}
 
-			if ( data.matrix !== undefined ) {
+	CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
 
-				object.matrix.fromArray( data.matrix );
+		constructor: CurvePath,
 
-				if ( data.matrixAutoUpdate !== undefined ) { object.matrixAutoUpdate = data.matrixAutoUpdate; }
-				if ( object.matrixAutoUpdate ) { object.matrix.decompose( object.position, object.quaternion, object.scale ); }
+		add: function ( curve ) {
 
-			} else {
+			this.curves.push( curve );
 
-				if ( data.position !== undefined ) { object.position.fromArray( data.position ); }
-				if ( data.rotation !== undefined ) { object.rotation.fromArray( data.rotation ); }
-				if ( data.quaternion !== undefined ) { object.quaternion.fromArray( data.quaternion ); }
-				if ( data.scale !== undefined ) { object.scale.fromArray( data.scale ); }
+		},
 
-			}
+		closePath: function () {
 
-			if ( data.castShadow !== undefined ) { object.castShadow = data.castShadow; }
-			if ( data.receiveShadow !== undefined ) { object.receiveShadow = data.receiveShadow; }
+			// Add a line curve if start and end of lines are not connected
+			var startPoint = this.curves[ 0 ].getPoint( 0 );
+			var endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
 
-			if ( data.shadow ) {
+			if ( ! startPoint.equals( endPoint ) ) {
 
-				if ( data.shadow.bias !== undefined ) { object.shadow.bias = data.shadow.bias; }
-				if ( data.shadow.radius !== undefined ) { object.shadow.radius = data.shadow.radius; }
-				if ( data.shadow.mapSize !== undefined ) { object.shadow.mapSize.fromArray( data.shadow.mapSize ); }
-				if ( data.shadow.camera !== undefined ) { object.shadow.camera = this.parseObject( data.shadow.camera ); }
+				this.curves.push( new LineCurve( endPoint, startPoint ) );
 
 			}
 
-			if ( data.visible !== undefined ) { object.visible = data.visible; }
-			if ( data.frustumCulled !== undefined ) { object.frustumCulled = data.frustumCulled; }
-			if ( data.renderOrder !== undefined ) { object.renderOrder = data.renderOrder; }
-			if ( data.userData !== undefined ) { object.userData = data.userData; }
-			if ( data.layers !== undefined ) { object.layers.mask = data.layers; }
-
-			if ( data.children !== undefined ) {
-
-				var children = data.children;
-
-				for ( var i = 0; i < children.length; i ++ ) {
-
-					object.add( this.parseObject( children[ i ], geometries, materials ) );
+		},
 
-				}
+		// To get accurate point with reference to
+		// entire path distance at time t,
+		// following has to be done:
 
-			}
+		// 1. Length of each sub path have to be known
+		// 2. Locate and identify type of curve
+		// 3. Get t for the curve
+		// 4. Return curve.getPointAt(t')
 
-			if ( data.type === 'LOD' ) {
+		getPoint: function ( t ) {
 
-				if ( data.autoUpdate !== undefined ) { object.autoUpdate = data.autoUpdate; }
+			var d = t * this.getLength();
+			var curveLengths = this.getCurveLengths();
+			var i = 0;
 
-				var levels = data.levels;
+			// To think about boundaries points.
 
-				for ( var l = 0; l < levels.length; l ++ ) {
+			while ( i < curveLengths.length ) {
 
-					var level = levels[ l ];
-					var child = object.getObjectByProperty( 'uuid', level.object );
+				if ( curveLengths[ i ] >= d ) {
 
-					if ( child !== undefined ) {
+					var diff = curveLengths[ i ] - d;
+					var curve = this.curves[ i ];
 
-						object.addLevel( child, level.distance );
+					var segmentLength = curve.getLength();
+					var u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
 
-					}
+					return curve.getPointAt( u );
 
 				}
 
+				i ++;
+
 			}
 
-			return object;
+			return null;
 
-		}
+			// loop where sum != 0, sum > d , sum+1 <d
 
-	} );
+		},
 
-	var TEXTURE_MAPPING = {
-		UVMapping: UVMapping,
-		CubeReflectionMapping: CubeReflectionMapping,
-		CubeRefractionMapping: CubeRefractionMapping,
-		EquirectangularReflectionMapping: EquirectangularReflectionMapping,
-		EquirectangularRefractionMapping: EquirectangularRefractionMapping,
-		SphericalReflectionMapping: SphericalReflectionMapping,
-		CubeUVReflectionMapping: CubeUVReflectionMapping,
-		CubeUVRefractionMapping: CubeUVRefractionMapping
-	};
+		// We cannot use the default THREE.Curve getPoint() with getLength() because in
+		// THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
+		// getPoint() depends on getLength
 
-	var TEXTURE_WRAPPING = {
-		RepeatWrapping: RepeatWrapping,
-		ClampToEdgeWrapping: ClampToEdgeWrapping,
-		MirroredRepeatWrapping: MirroredRepeatWrapping
-	};
+		getLength: function () {
 
-	var TEXTURE_FILTER = {
-		NearestFilter: NearestFilter,
-		NearestMipmapNearestFilter: NearestMipmapNearestFilter,
-		NearestMipmapLinearFilter: NearestMipmapLinearFilter,
-		LinearFilter: LinearFilter,
-		LinearMipmapNearestFilter: LinearMipmapNearestFilter,
-		LinearMipmapLinearFilter: LinearMipmapLinearFilter
-	};
+			var lens = this.getCurveLengths();
+			return lens[ lens.length - 1 ];
 
-	/**
-	 * @author thespite / http://clicktorelease.com/
-	 */
+		},
 
+		// cacheLengths must be recalculated.
+		updateArcLengths: function () {
 
-	function ImageBitmapLoader( manager ) {
+			this.needsUpdate = true;
+			this.cacheLengths = null;
+			this.getCurveLengths();
 
-		if ( typeof createImageBitmap === 'undefined' ) {
+		},
 
-			console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
+		// Compute lengths and cache them
+		// We cannot overwrite getLengths() because UtoT mapping uses it.
 
-		}
+		getCurveLengths: function () {
 
-		if ( typeof fetch === 'undefined' ) {
+			// We use cache values if curves and cache array are same length
 
-			console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
+			if ( this.cacheLengths && this.cacheLengths.length === this.curves.length ) {
 
-		}
+				return this.cacheLengths;
 
-		Loader.call( this, manager );
+			}
 
-		this.options = undefined;
+			// Get length of sub-curve
+			// Push sums into cached array
 
-	}
+			var lengths = [], sums = 0;
 
-	ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+			for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
 
-		constructor: ImageBitmapLoader,
+				sums += this.curves[ i ].getLength();
+				lengths.push( sums );
 
-		setOptions: function setOptions( options ) {
+			}
 
-			this.options = options;
+			this.cacheLengths = lengths;
 
-			return this;
+			return lengths;
 
 		},
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		getSpacedPoints: function ( divisions ) {
 
-			if ( url === undefined ) { url = ''; }
+			if ( divisions === undefined ) { divisions = 40; }
 
-			if ( this.path !== undefined ) { url = this.path + url; }
+			var points = [];
 
-			url = this.manager.resolveURL( url );
+			for ( var i = 0; i <= divisions; i ++ ) {
 
-			var scope = this;
+				points.push( this.getPoint( i / divisions ) );
 
-			var cached = Cache.get( url );
+			}
 
-			if ( cached !== undefined ) {
+			if ( this.autoClose ) {
 
-				scope.manager.itemStart( url );
+				points.push( points[ 0 ] );
 
-				setTimeout( function () {
+			}
 
-					if ( onLoad ) { onLoad( cached ); }
+			return points;
 
-					scope.manager.itemEnd( url );
+		},
 
-				}, 0 );
+		getPoints: function ( divisions ) {
 
-				return cached;
+			divisions = divisions || 12;
 
-			}
+			var points = [], last;
 
-			fetch( url ).then( function ( res ) {
+			for ( var i = 0, curves = this.curves; i < curves.length; i ++ ) {
 
-				return res.blob();
+				var curve = curves[ i ];
+				var resolution = ( curve && curve.isEllipseCurve ) ? divisions * 2
+					: ( curve && ( curve.isLineCurve || curve.isLineCurve3 ) ) ? 1
+						: ( curve && curve.isSplineCurve ) ? divisions * curve.points.length
+							: divisions;
 
-			} ).then( function ( blob ) {
+				var pts = curve.getPoints( resolution );
 
-				if ( scope.options === undefined ) {
+				for ( var j = 0; j < pts.length; j ++ ) {
 
-					// Workaround for FireFox. It causes an error if you pass options.
-					return createImageBitmap( blob );
+					var point = pts[ j ];
 
-				} else {
+					if ( last && last.equals( point ) ) { continue; } // ensures no consecutive points are duplicates
 
-					return createImageBitmap( blob, scope.options );
+					points.push( point );
+					last = point;
 
 				}
 
-			} ).then( function ( imageBitmap ) {
+			}
 
-				Cache.add( url, imageBitmap );
+			if ( this.autoClose && points.length > 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) {
 
-				if ( onLoad ) { onLoad( imageBitmap ); }
+				points.push( points[ 0 ] );
 
-				scope.manager.itemEnd( url );
+			}
 
-			} ).catch( function ( e ) {
+			return points;
 
-				if ( onError ) { onError( e ); }
+		},
 
-				scope.manager.itemError( url );
-				scope.manager.itemEnd( url );
+		copy: function ( source ) {
 
-			} );
+			Curve.prototype.copy.call( this, source );
 
-			scope.manager.itemStart( url );
+			this.curves = [];
+
+			for ( var i = 0, l = source.curves.length; i < l; i ++ ) {
+
+				var curve = source.curves[ i ];
+
+				this.curves.push( curve.clone() );
+
+			}
+
+			this.autoClose = source.autoClose;
+
+			return this;
+
+		},
+
+		toJSON: function () {
+
+			var data = Curve.prototype.toJSON.call( this );
+
+			data.autoClose = this.autoClose;
+			data.curves = [];
+
+			for ( var i = 0, l = this.curves.length; i < l; i ++ ) {
+
+				var curve = this.curves[ i ];
+				data.curves.push( curve.toJSON() );
+
+			}
+
+			return data;
+
+		},
+
+		fromJSON: function ( json ) {
+
+			Curve.prototype.fromJSON.call( this, json );
+
+			this.autoClose = json.autoClose;
+			this.curves = [];
+
+			for ( var i = 0, l = json.curves.length; i < l; i ++ ) {
+
+				var curve = json.curves[ i ];
+				this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) );
+
+			}
+
+			return this;
 
 		}
 
@@ -49914,744 +57516,767 @@ function simpleEnd(buf) {
 
 	/**
 	 * @author zz85 / http://www.lab4games.net/zz85/blog
-	 * minimal class for proxing functions to Path. Replaces old "extractSubpaths()"
+	 * Creates free form 2d path using series of points, lines or curves.
 	 **/
 
-	function ShapePath() {
+	function Path( points ) {
 
-		this.type = 'ShapePath';
+		CurvePath.call( this );
 
-		this.color = new Color();
+		this.type = 'Path';
 
-		this.subPaths = [];
-		this.currentPath = null;
+		this.currentPoint = new Vector2();
+
+		if ( points ) {
+
+			this.setFromPoints( points );
+
+		}
 
 	}
 
-	Object.assign( ShapePath.prototype, {
+	Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {
 
-		moveTo: function ( x, y ) {
+		constructor: Path,
 
-			this.currentPath = new Path();
-			this.subPaths.push( this.currentPath );
-			this.currentPath.moveTo( x, y );
+		setFromPoints: function ( points ) {
+
+			this.moveTo( points[ 0 ].x, points[ 0 ].y );
+
+			for ( var i = 1, l = points.length; i < l; i ++ ) {
+
+				this.lineTo( points[ i ].x, points[ i ].y );
+
+			}
 
 			return this;
 
 		},
 
-		lineTo: function ( x, y ) {
+		moveTo: function ( x, y ) {
 
-			this.currentPath.lineTo( x, y );
+			this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
 
 			return this;
 
 		},
 
-		quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
+		lineTo: function ( x, y ) {
 
-			this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY );
+			var curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
+			this.curves.push( curve );
+
+			this.currentPoint.set( x, y );
 
 			return this;
 
 		},
 
-		bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
+		quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
 
-			this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY );
+			var curve = new QuadraticBezierCurve(
+				this.currentPoint.clone(),
+				new Vector2( aCPx, aCPy ),
+				new Vector2( aX, aY )
+			);
+
+			this.curves.push( curve );
+
+			this.currentPoint.set( aX, aY );
 
 			return this;
 
 		},
 
-		splineThru: function ( pts ) {
+		bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
 
-			this.currentPath.splineThru( pts );
+			var curve = new CubicBezierCurve(
+				this.currentPoint.clone(),
+				new Vector2( aCP1x, aCP1y ),
+				new Vector2( aCP2x, aCP2y ),
+				new Vector2( aX, aY )
+			);
+
+			this.curves.push( curve );
+
+			this.currentPoint.set( aX, aY );
 
 			return this;
 
 		},
 
-		toShapes: function ( isCCW, noHoles ) {
+		splineThru: function ( pts /*Array of Vector*/ ) {
 
-			function toShapesNoHoles( inSubpaths ) {
+			var npts = [ this.currentPoint.clone() ].concat( pts );
 
-				var shapes = [];
+			var curve = new SplineCurve( npts );
+			this.curves.push( curve );
 
-				for ( var i = 0, l = inSubpaths.length; i < l; i ++ ) {
+			this.currentPoint.copy( pts[ pts.length - 1 ] );
 
-					var tmpPath = inSubpaths[ i ];
+			return this;
 
-					var tmpShape = new Shape();
-					tmpShape.curves = tmpPath.curves;
+		},
 
-					shapes.push( tmpShape );
+		arc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
 
-				}
+			var x0 = this.currentPoint.x;
+			var y0 = this.currentPoint.y;
 
-				return shapes;
+			this.absarc( aX + x0, aY + y0, aRadius,
+				aStartAngle, aEndAngle, aClockwise );
 
-			}
+			return this;
 
-			function isPointInsidePolygon( inPt, inPolygon ) {
+		},
 
-				var polyLen = inPolygon.length;
+		absarc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
 
-				// inPt on polygon contour => immediate success    or
-				// toggling of inside/outside at every single! intersection point of an edge
-				//  with the horizontal line through inPt, left of inPt
-				//  not counting lowerY endpoints of edges and whole edges on that line
-				var inside = false;
-				for ( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
+			this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
 
-					var edgeLowPt = inPolygon[ p ];
-					var edgeHighPt = inPolygon[ q ];
+			return this;
 
-					var edgeDx = edgeHighPt.x - edgeLowPt.x;
-					var edgeDy = edgeHighPt.y - edgeLowPt.y;
+		},
 
-					if ( Math.abs( edgeDy ) > Number.EPSILON ) {
+		ellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
 
-						// not parallel
-						if ( edgeDy < 0 ) {
+			var x0 = this.currentPoint.x;
+			var y0 = this.currentPoint.y;
 
-							edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
-							edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
+			this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
 
-						}
+			return this;
 
-						if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) 		{ continue; }
+		},
 
-						if ( inPt.y === edgeLowPt.y ) {
+		absellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
 
-							if ( inPt.x === edgeLowPt.x )		{ return	true; }		// inPt is on contour ?
-							// continue;				// no intersection or edgeLowPt => doesn't count !!!
+			var curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
 
-						} else {
+			if ( this.curves.length > 0 ) {
 
-							var perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
-							if ( perpEdge === 0 )				{ return	true; }		// inPt is on contour ?
-							if ( perpEdge < 0 ) 				{ continue; }
-							inside = ! inside;		// true intersection left of inPt
+				// if a previous curve is present, attempt to join
+				var firstPoint = curve.getPoint( 0 );
 
-						}
+				if ( ! firstPoint.equals( this.currentPoint ) ) {
 
-					} else {
+					this.lineTo( firstPoint.x, firstPoint.y );
 
-						// parallel or collinear
-						if ( inPt.y !== edgeLowPt.y ) 		{ continue; }			// parallel
-						// edge lies on the same horizontal line as inPt
-						if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
-							 ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) )		{ return	true; }	// inPt: Point on contour !
-						// continue;
+				}
 
-					}
+			}
 
-				}
+			this.curves.push( curve );
 
-				return	inside;
+			var lastPoint = curve.getPoint( 1 );
+			this.currentPoint.copy( lastPoint );
 
-			}
+			return this;
 
-			var isClockWise = ShapeUtils.isClockWise;
+		},
 
-			var subPaths = this.subPaths;
-			if ( subPaths.length === 0 ) { return []; }
+		copy: function ( source ) {
 
-			if ( noHoles === true )	{ return	toShapesNoHoles( subPaths ); }
+			CurvePath.prototype.copy.call( this, source );
 
+			this.currentPoint.copy( source.currentPoint );
 
-			var solid, tmpPath, tmpShape, shapes = [];
+			return this;
 
-			if ( subPaths.length === 1 ) {
+		},
 
-				tmpPath = subPaths[ 0 ];
-				tmpShape = new Shape();
-				tmpShape.curves = tmpPath.curves;
-				shapes.push( tmpShape );
-				return shapes;
+		toJSON: function () {
 
-			}
+			var data = CurvePath.prototype.toJSON.call( this );
 
-			var holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
-			holesFirst = isCCW ? ! holesFirst : holesFirst;
+			data.currentPoint = this.currentPoint.toArray();
 
-			// console.log("Holes first", holesFirst);
+			return data;
 
-			var betterShapeHoles = [];
-			var newShapes = [];
-			var newShapeHoles = [];
-			var mainIdx = 0;
-			var tmpPoints;
+		},
 
-			newShapes[ mainIdx ] = undefined;
-			newShapeHoles[ mainIdx ] = [];
+		fromJSON: function ( json ) {
 
-			for ( var i = 0, l = subPaths.length; i < l; i ++ ) {
+			CurvePath.prototype.fromJSON.call( this, json );
 
-				tmpPath = subPaths[ i ];
-				tmpPoints = tmpPath.getPoints();
-				solid = isClockWise( tmpPoints );
-				solid = isCCW ? ! solid : solid;
+			this.currentPoint.fromArray( json.currentPoint );
 
-				if ( solid ) {
+			return this;
 
-					if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) )	{ mainIdx ++; }
+		}
 
-					newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints };
-					newShapes[ mainIdx ].s.curves = tmpPath.curves;
+	} );
 
-					if ( holesFirst )	{ mainIdx ++; }
-					newShapeHoles[ mainIdx ] = [];
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * Defines a 2d shape plane using paths.
+	 **/
 
-					//console.log('cw', i);
+	// STEP 1 Create a path.
+	// STEP 2 Turn path into shape.
+	// STEP 3 ExtrudeGeometry takes in Shape/Shapes
+	// STEP 3a - Extract points from each shape, turn to vertices
+	// STEP 3b - Triangulate each shape, add faces.
 
-				} else {
+	function Shape( points ) {
 
-					newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
+		Path.call( this, points );
 
-					//console.log('ccw', i);
+		this.uuid = MathUtils.generateUUID();
 
-				}
+		this.type = 'Shape';
 
-			}
+		this.holes = [];
 
-			// only Holes? -> probably all Shapes with wrong orientation
-			if ( ! newShapes[ 0 ] )	{ return	toShapesNoHoles( subPaths ); }
+	}
 
+	Shape.prototype = Object.assign( Object.create( Path.prototype ), {
 
-			if ( newShapes.length > 1 ) {
+		constructor: Shape,
 
-				var ambiguous = false;
-				var toChange = [];
+		getPointsHoles: function ( divisions ) {
 
-				for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
+			var holesPts = [];
 
-					betterShapeHoles[ sIdx ] = [];
+			for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
 
-				}
+				holesPts[ i ] = this.holes[ i ].getPoints( divisions );
 
-				for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
+			}
 
-					var sho = newShapeHoles[ sIdx ];
+			return holesPts;
 
-					for ( var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
+		},
 
-						var ho = sho[ hIdx ];
-						var hole_unassigned = true;
+		// get points of shape and holes (keypoints based on segments parameter)
 
-						for ( var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
+		extractPoints: function ( divisions ) {
 
-							if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
+			return {
 
-								if ( sIdx !== s2Idx )	{ toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } ); }
-								if ( hole_unassigned ) {
+				shape: this.getPoints( divisions ),
+				holes: this.getPointsHoles( divisions )
 
-									hole_unassigned = false;
-									betterShapeHoles[ s2Idx ].push( ho );
+			};
 
-								} else {
+		},
 
-									ambiguous = true;
+		copy: function ( source ) {
 
-								}
+			Path.prototype.copy.call( this, source );
 
-							}
+			this.holes = [];
 
-						}
+			for ( var i = 0, l = source.holes.length; i < l; i ++ ) {
 
-						if ( hole_unassigned ) {
+				var hole = source.holes[ i ];
 
-							betterShapeHoles[ sIdx ].push( ho );
+				this.holes.push( hole.clone() );
 
-						}
+			}
 
-					}
+			return this;
 
-				}
-				// console.log("ambiguous: ", ambiguous);
+		},
 
-				if ( toChange.length > 0 ) {
+		toJSON: function () {
 
-					// console.log("to change: ", toChange);
-					if ( ! ambiguous )	{ newShapeHoles = betterShapeHoles; }
+			var data = Path.prototype.toJSON.call( this );
 
-				}
+			data.uuid = this.uuid;
+			data.holes = [];
+
+			for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
+
+				var hole = this.holes[ i ];
+				data.holes.push( hole.toJSON() );
 
 			}
 
-			var tmpHoles;
+			return data;
 
-			for ( var i = 0, il = newShapes.length; i < il; i ++ ) {
+		},
 
-				tmpShape = newShapes[ i ].s;
-				shapes.push( tmpShape );
-				tmpHoles = newShapeHoles[ i ];
+		fromJSON: function ( json ) {
 
-				for ( var j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
+			Path.prototype.fromJSON.call( this, json );
 
-					tmpShape.holes.push( tmpHoles[ j ].h );
+			this.uuid = json.uuid;
+			this.holes = [];
 
-				}
+			for ( var i = 0, l = json.holes.length; i < l; i ++ ) {
 
-			}
+				var hole = json.holes[ i ];
+				this.holes.push( new Path().fromJSON( hole ) );
 
-			//console.log("shape", shapes);
+			}
 
-			return shapes;
+			return this;
 
 		}
 
 	} );
 
 	/**
-	 * @author zz85 / http://www.lab4games.net/zz85/blog
 	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
 	 */
 
+	function Light( color, intensity ) {
 
-	function Font( data ) {
+		Object3D.call( this );
 
-		this.type = 'Font';
+		this.type = 'Light';
 
-		this.data = data;
+		this.color = new Color( color );
+		this.intensity = intensity !== undefined ? intensity : 1;
+
+		this.receiveShadow = undefined;
 
 	}
 
-	Object.assign( Font.prototype, {
+	Light.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-		isFont: true,
+		constructor: Light,
 
-		generateShapes: function ( text, size ) {
+		isLight: true,
 
-			if ( size === undefined ) { size = 100; }
+		copy: function ( source ) {
 
-			var shapes = [];
-			var paths = createPaths( text, size, this.data );
+			Object3D.prototype.copy.call( this, source );
 
-			for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
+			this.color.copy( source.color );
+			this.intensity = source.intensity;
 
-				Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
+			return this;
 
-			}
+		},
 
-			return shapes;
+		toJSON: function ( meta ) {
 
-		}
+			var data = Object3D.prototype.toJSON.call( this, meta );
 
-	} );
+			data.object.color = this.color.getHex();
+			data.object.intensity = this.intensity;
 
-	function createPaths( text, size, data ) {
+			if ( this.groundColor !== undefined ) { data.object.groundColor = this.groundColor.getHex(); }
 
-		var chars = Array.from ? Array.from( text ) : String( text ).split( '' ); // workaround for IE11, see #13988
-		var scale = size / data.resolution;
-		var line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale;
+			if ( this.distance !== undefined ) { data.object.distance = this.distance; }
+			if ( this.angle !== undefined ) { data.object.angle = this.angle; }
+			if ( this.decay !== undefined ) { data.object.decay = this.decay; }
+			if ( this.penumbra !== undefined ) { data.object.penumbra = this.penumbra; }
 
-		var paths = [];
+			if ( this.shadow !== undefined ) { data.object.shadow = this.shadow.toJSON(); }
 
-		var offsetX = 0, offsetY = 0;
+			return data;
 
-		for ( var i = 0; i < chars.length; i ++ ) {
+		}
 
-			var char = chars[ i ];
+	} );
 
-			if ( char === '\n' ) {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				offsetX = 0;
-				offsetY -= line_height;
+	function HemisphereLight( skyColor, groundColor, intensity ) {
 
-			} else {
+		Light.call( this, skyColor, intensity );
 
-				var ret = createPath( char, scale, offsetX, offsetY, data );
-				offsetX += ret.offsetX;
-				paths.push( ret.path );
+		this.type = 'HemisphereLight';
 
-			}
+		this.castShadow = undefined;
 
-		}
+		this.position.copy( Object3D.DefaultUp );
+		this.updateMatrix();
 
-		return paths;
+		this.groundColor = new Color( groundColor );
 
 	}
 
-	function createPath( char, scale, offsetX, offsetY, data ) {
+	HemisphereLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-		var glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
+		constructor: HemisphereLight,
 
-		if ( ! glyph ) {
+		isHemisphereLight: true,
 
-			console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' );
+		copy: function ( source ) {
 
-			return;
+			Light.prototype.copy.call( this, source );
 
-		}
+			this.groundColor.copy( source.groundColor );
 
-		var path = new ShapePath();
+			return this;
 
-		var x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2;
+		}
 
-		if ( glyph.o ) {
+	} );
 
-			var outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			for ( var i = 0, l = outline.length; i < l; ) {
+	function LightShadow( camera ) {
 
-				var action = outline[ i ++ ];
+		this.camera = camera;
 
-				switch ( action ) {
+		this.bias = 0;
+		this.radius = 1;
 
-					case 'm': // moveTo
+		this.mapSize = new Vector2( 512, 512 );
 
-						x = outline[ i ++ ] * scale + offsetX;
-						y = outline[ i ++ ] * scale + offsetY;
+		this.map = null;
+		this.mapPass = null;
+		this.matrix = new Matrix4();
 
-						path.moveTo( x, y );
+		this._frustum = new Frustum();
+		this._frameExtents = new Vector2( 1, 1 );
 
-						break;
+		this._viewportCount = 1;
 
-					case 'l': // lineTo
+		this._viewports = [
 
-						x = outline[ i ++ ] * scale + offsetX;
-						y = outline[ i ++ ] * scale + offsetY;
-
-						path.lineTo( x, y );
-
-						break;
-
-					case 'q': // quadraticCurveTo
-
-						cpx = outline[ i ++ ] * scale + offsetX;
-						cpy = outline[ i ++ ] * scale + offsetY;
-						cpx1 = outline[ i ++ ] * scale + offsetX;
-						cpy1 = outline[ i ++ ] * scale + offsetY;
-
-						path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
-
-						break;
-
-					case 'b': // bezierCurveTo
-
-						cpx = outline[ i ++ ] * scale + offsetX;
-						cpy = outline[ i ++ ] * scale + offsetY;
-						cpx1 = outline[ i ++ ] * scale + offsetX;
-						cpy1 = outline[ i ++ ] * scale + offsetY;
-						cpx2 = outline[ i ++ ] * scale + offsetX;
-						cpy2 = outline[ i ++ ] * scale + offsetY;
+			new Vector4( 0, 0, 1, 1 )
 
-						path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
+		];
 
-						break;
+	}
 
-				}
+	Object.assign( LightShadow.prototype, {
 
-			}
+		_projScreenMatrix: new Matrix4(),
 
-		}
+		_lightPositionWorld: new Vector3(),
 
-		return { offsetX: glyph.ha * scale, path: path };
+		_lookTarget: new Vector3(),
 
-	}
+		getViewportCount: function () {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			return this._viewportCount;
 
-	function FontLoader( manager ) {
+		},
 
-		Loader.call( this, manager );
+		getFrustum: function () {
 
-	}
+			return this._frustum;
 
-	FontLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		},
 
-		constructor: FontLoader,
+		updateMatrices: function ( light ) {
 
-		load: function ( url, onLoad, onProgress, onError ) {
+			var shadowCamera = this.camera,
+				shadowMatrix = this.matrix,
+				projScreenMatrix = this._projScreenMatrix,
+				lookTarget = this._lookTarget,
+				lightPositionWorld = this._lightPositionWorld;
 
-			var scope = this;
+			lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
+			shadowCamera.position.copy( lightPositionWorld );
 
-			var loader = new FileLoader( this.manager );
-			loader.setPath( this.path );
-			loader.load( url, function ( text ) {
+			lookTarget.setFromMatrixPosition( light.target.matrixWorld );
+			shadowCamera.lookAt( lookTarget );
+			shadowCamera.updateMatrixWorld();
 
-				var json;
+			projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
+			this._frustum.setFromProjectionMatrix( projScreenMatrix );
 
-				try {
+			shadowMatrix.set(
+				0.5, 0.0, 0.0, 0.5,
+				0.0, 0.5, 0.0, 0.5,
+				0.0, 0.0, 0.5, 0.5,
+				0.0, 0.0, 0.0, 1.0
+			);
 
-					json = JSON.parse( text );
+			shadowMatrix.multiply( shadowCamera.projectionMatrix );
+			shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
 
-				} catch ( e ) {
+		},
 
-					console.warn( 'THREE.FontLoader: typeface.js support is being deprecated. Use typeface.json instead.' );
-					json = JSON.parse( text.substring( 65, text.length - 2 ) );
+		getViewport: function ( viewportIndex ) {
 
-				}
+			return this._viewports[ viewportIndex ];
 
-				var font = scope.parse( json );
+		},
 
-				if ( onLoad ) { onLoad( font ); }
+		getFrameExtents: function () {
 
-			}, onProgress, onError );
+			return this._frameExtents;
 
 		},
 
-		parse: function ( json ) {
-
-			return new Font( json );
+		copy: function ( source ) {
 
-		}
+			this.camera = source.camera.clone();
 
-	} );
+			this.bias = source.bias;
+			this.radius = source.radius;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+			this.mapSize.copy( source.mapSize );
 
-	var _context;
+			return this;
 
-	var AudioContext = {
+		},
 
-		getContext: function () {
+		clone: function () {
 
-			if ( _context === undefined ) {
+			return new this.constructor().copy( this );
 
-				_context = new ( window.AudioContext || window.webkitAudioContext )();
+		},
 
-			}
+		toJSON: function () {
 
-			return _context;
+			var object = {};
 
-		},
+			if ( this.bias !== 0 ) { object.bias = this.bias; }
+			if ( this.radius !== 1 ) { object.radius = this.radius; }
+			if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) { object.mapSize = this.mapSize.toArray(); }
 
-		setContext: function ( value ) {
+			object.camera = this.camera.toJSON( false ).object;
+			delete object.camera.matrix;
 
-			_context = value;
+			return object;
 
 		}
 
-	};
+	} );
 
 	/**
-	 * @author Reece Aaron Lecrivain / http://reecenotes.com/
+	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function AudioLoader( manager ) {
+	function SpotLightShadow() {
 
-		Loader.call( this, manager );
+		LightShadow.call( this, new PerspectiveCamera( 50, 1, 0.5, 500 ) );
 
 	}
 
-	AudioLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+	SpotLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
 
-		constructor: AudioLoader,
+		constructor: SpotLightShadow,
 
-		load: function ( url, onLoad, onProgress, onError ) {
+		isSpotLightShadow: true,
 
-			var loader = new FileLoader( this.manager );
-			loader.setResponseType( 'arraybuffer' );
-			loader.setPath( this.path );
-			loader.load( url, function ( buffer ) {
+		updateMatrices: function ( light ) {
 
-				// Create a copy of the buffer. The `decodeAudioData` method
-				// detaches the buffer when complete, preventing reuse.
-				var bufferCopy = buffer.slice( 0 );
+			var camera = this.camera;
 
-				var context = AudioContext.getContext();
-				context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
+			var fov = MathUtils.RAD2DEG * 2 * light.angle;
+			var aspect = this.mapSize.width / this.mapSize.height;
+			var far = light.distance || camera.far;
 
-					onLoad( audioBuffer );
+			if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) {
 
-				} );
+				camera.fov = fov;
+				camera.aspect = aspect;
+				camera.far = far;
+				camera.updateProjectionMatrix();
 
-			}, onProgress, onError );
+			}
+
+			LightShadow.prototype.updateMatrices.call( this, light );
 
 		}
 
 	} );
 
 	/**
-	 * @author WestLangley / http://github.com/WestLangley
+	 * @author alteredq / http://alteredqualia.com/
 	 */
 
-	function HemisphereLightProbe( skyColor, groundColor, intensity ) {
+	function SpotLight( color, intensity, distance, angle, penumbra, decay ) {
 
-		LightProbe.call( this, undefined, intensity );
+		Light.call( this, color, intensity );
 
-		var color1 = new Color().set( skyColor );
-		var color2 = new Color().set( groundColor );
+		this.type = 'SpotLight';
 
-		var sky = new Vector3( color1.r, color1.g, color1.b );
-		var ground = new Vector3( color2.r, color2.g, color2.b );
+		this.position.copy( Object3D.DefaultUp );
+		this.updateMatrix();
 
-		// without extra factor of PI in the shader, should = 1 / Math.sqrt( Math.PI );
-		var c0 = Math.sqrt( Math.PI );
-		var c1 = c0 * Math.sqrt( 0.75 );
+		this.target = new Object3D();
 
-		this.sh.coefficients[ 0 ].copy( sky ).add( ground ).multiplyScalar( c0 );
-		this.sh.coefficients[ 1 ].copy( sky ).sub( ground ).multiplyScalar( c1 );
+		Object.defineProperty( this, 'power', {
+			get: function () {
 
-	}
+				// intensity = power per solid angle.
+				// ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
+				return this.intensity * Math.PI;
 
-	HemisphereLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
+			},
+			set: function ( power ) {
 
-		constructor: HemisphereLightProbe,
+				// intensity = power per solid angle.
+				// ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
+				this.intensity = power / Math.PI;
 
-		isHemisphereLightProbe: true,
+			}
+		} );
 
-		copy: function ( source ) { // modifying colors not currently supported
+		this.distance = ( distance !== undefined ) ? distance : 0;
+		this.angle = ( angle !== undefined ) ? angle : Math.PI / 3;
+		this.penumbra = ( penumbra !== undefined ) ? penumbra : 0;
+		this.decay = ( decay !== undefined ) ? decay : 1;	// for physically correct lights, should be 2.
 
-			LightProbe.prototype.copy.call( this, source );
+		this.shadow = new SpotLightShadow();
 
-			return this;
+	}
 
-		},
+	SpotLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-		toJSON: function ( meta ) {
+		constructor: SpotLight,
 
-			var data = LightProbe.prototype.toJSON.call( this, meta );
+		isSpotLight: true,
 
-			// data.sh = this.sh.toArray(); // todo
+		copy: function ( source ) {
 
-			return data;
+			Light.prototype.copy.call( this, source );
+
+			this.distance = source.distance;
+			this.angle = source.angle;
+			this.penumbra = source.penumbra;
+			this.decay = source.decay;
+
+			this.target = source.target.clone();
+
+			this.shadow = source.shadow.clone();
+
+			return this;
 
 		}
 
 	} );
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+	function PointLightShadow() {
 
-	function AmbientLightProbe( color, intensity ) {
+		LightShadow.call( this, new PerspectiveCamera( 90, 1, 0.5, 500 ) );
 
-		LightProbe.call( this, undefined, intensity );
+		this._frameExtents = new Vector2( 4, 2 );
 
-		var color1 = new Color().set( color );
+		this._viewportCount = 6;
 
-		// without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI );
-		this.sh.coefficients[ 0 ].set( color1.r, color1.g, color1.b ).multiplyScalar( 2 * Math.sqrt( Math.PI ) );
+		this._viewports = [
+			// These viewports map a cube-map onto a 2D texture with the
+			// following orientation:
+			//
+			//  xzXZ
+			//   y Y
+			//
+			// X - Positive x direction
+			// x - Negative x direction
+			// Y - Positive y direction
+			// y - Negative y direction
+			// Z - Positive z direction
+			// z - Negative z direction
 
-	}
+			// positive X
+			new Vector4( 2, 1, 1, 1 ),
+			// negative X
+			new Vector4( 0, 1, 1, 1 ),
+			// positive Z
+			new Vector4( 3, 1, 1, 1 ),
+			// negative Z
+			new Vector4( 1, 1, 1, 1 ),
+			// positive Y
+			new Vector4( 3, 0, 1, 1 ),
+			// negative Y
+			new Vector4( 1, 0, 1, 1 )
+		];
 
-	AmbientLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
+		this._cubeDirections = [
+			new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
+			new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
+		];
 
-		constructor: AmbientLightProbe,
+		this._cubeUps = [
+			new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
+			new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ),	new Vector3( 0, 0, - 1 )
+		];
 
-		isAmbientLightProbe: true,
+	}
 
-		copy: function ( source ) { // modifying color not currently supported
+	PointLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
 
-			LightProbe.prototype.copy.call( this, source );
+		constructor: PointLightShadow,
 
-			return this;
+		isPointLightShadow: true,
 
-		},
+		updateMatrices: function ( light, viewportIndex ) {
 
-		toJSON: function ( meta ) {
+			if ( viewportIndex === undefined ) { viewportIndex = 0; }
 
-			var data = LightProbe.prototype.toJSON.call( this, meta );
+			var camera = this.camera,
+				shadowMatrix = this.matrix,
+				lightPositionWorld = this._lightPositionWorld,
+				lookTarget = this._lookTarget,
+				projScreenMatrix = this._projScreenMatrix;
 
-			// data.sh = this.sh.toArray(); // todo
+			lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
+			camera.position.copy( lightPositionWorld );
 
-			return data;
+			lookTarget.copy( camera.position );
+			lookTarget.add( this._cubeDirections[ viewportIndex ] );
+			camera.up.copy( this._cubeUps[ viewportIndex ] );
+			camera.lookAt( lookTarget );
+			camera.updateMatrixWorld();
+
+			shadowMatrix.makeTranslation( - lightPositionWorld.x, - lightPositionWorld.y, - lightPositionWorld.z );
+
+			projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
+			this._frustum.setFromProjectionMatrix( projScreenMatrix );
 
 		}
 
 	} );
 
-	var _eyeRight = new Matrix4();
-	var _eyeLeft = new Matrix4();
-
 	/**
 	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function StereoCamera() {
-
-		this.type = 'StereoCamera';
-
-		this.aspect = 1;
-
-		this.eyeSep = 0.064;
-
-		this.cameraL = new PerspectiveCamera();
-		this.cameraL.layers.enable( 1 );
-		this.cameraL.matrixAutoUpdate = false;
-
-		this.cameraR = new PerspectiveCamera();
-		this.cameraR.layers.enable( 2 );
-		this.cameraR.matrixAutoUpdate = false;
-
-		this._cache = {
-			focus: null,
-			fov: null,
-			aspect: null,
-			near: null,
-			far: null,
-			zoom: null,
-			eyeSep: null
-		};
-
-	}
-
-	Object.assign( StereoCamera.prototype, {
 
-		update: function ( camera ) {
+	function PointLight( color, intensity, distance, decay ) {
 
-			var cache = this._cache;
+		Light.call( this, color, intensity );
 
-			var needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov ||
-				cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near ||
-				cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep;
+		this.type = 'PointLight';
 
-			if ( needsUpdate ) {
+		Object.defineProperty( this, 'power', {
+			get: function () {
 
-				cache.focus = camera.focus;
-				cache.fov = camera.fov;
-				cache.aspect = camera.aspect * this.aspect;
-				cache.near = camera.near;
-				cache.far = camera.far;
-				cache.zoom = camera.zoom;
-				cache.eyeSep = this.eyeSep;
+				// intensity = power per solid angle.
+				// ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
+				return this.intensity * 4 * Math.PI;
 
-				// Off-axis stereoscopic effect based on
-				// http://paulbourke.net/stereographics/stereorender/
+			},
+			set: function ( power ) {
 
-				var projectionMatrix = camera.projectionMatrix.clone();
-				var eyeSepHalf = cache.eyeSep / 2;
-				var eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus;
-				var ymax = ( cache.near * Math.tan( MathUtils.DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom;
-				var xmin, xmax;
+				// intensity = power per solid angle.
+				// ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
+				this.intensity = power / ( 4 * Math.PI );
 
-				// translate xOffset
+			}
+		} );
 
-				_eyeLeft.elements[ 12 ] = - eyeSepHalf;
-				_eyeRight.elements[ 12 ] = eyeSepHalf;
+		this.distance = ( distance !== undefined ) ? distance : 0;
+		this.decay = ( decay !== undefined ) ? decay : 1;	// for physically correct lights, should be 2.
 
-				// for left eye
+		this.shadow = new PointLightShadow();
 
-				xmin = - ymax * cache.aspect + eyeSepOnProjection;
-				xmax = ymax * cache.aspect + eyeSepOnProjection;
+	}
 
-				projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
-				projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+	PointLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-				this.cameraL.projectionMatrix.copy( projectionMatrix );
+		constructor: PointLight,
 
-				// for right eye
+		isPointLight: true,
 
-				xmin = - ymax * cache.aspect - eyeSepOnProjection;
-				xmax = ymax * cache.aspect - eyeSepOnProjection;
+		copy: function ( source ) {
 
-				projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
-				projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+			Light.prototype.copy.call( this, source );
 
-				this.cameraR.projectionMatrix.copy( projectionMatrix );
+			this.distance = source.distance;
+			this.decay = source.decay;
 
-			}
+			this.shadow = source.shadow.clone();
 
-			this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft );
-			this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight );
+			return this;
 
 		}
 
@@ -50659,369 +58284,392 @@ function simpleEnd(buf) {
 
 	/**
 	 * @author alteredq / http://alteredqualia.com/
+	 * @author arose / http://github.com/arose
 	 */
 
-	function Clock( autoStart ) {
+	function OrthographicCamera( left, right, top, bottom, near, far ) {
 
-		this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
+		Camera.call( this );
 
-		this.startTime = 0;
-		this.oldTime = 0;
-		this.elapsedTime = 0;
+		this.type = 'OrthographicCamera';
 
-		this.running = false;
+		this.zoom = 1;
+		this.view = null;
 
-	}
+		this.left = ( left !== undefined ) ? left : - 1;
+		this.right = ( right !== undefined ) ? right : 1;
+		this.top = ( top !== undefined ) ? top : 1;
+		this.bottom = ( bottom !== undefined ) ? bottom : - 1;
 
-	Object.assign( Clock.prototype, {
+		this.near = ( near !== undefined ) ? near : 0.1;
+		this.far = ( far !== undefined ) ? far : 2000;
 
-		start: function () {
+		this.updateProjectionMatrix();
 
-			this.startTime = ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732
+	}
 
-			this.oldTime = this.startTime;
-			this.elapsedTime = 0;
-			this.running = true;
+	OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
 
-		},
+		constructor: OrthographicCamera,
 
-		stop: function () {
+		isOrthographicCamera: true,
 
-			this.getElapsedTime();
-			this.running = false;
-			this.autoStart = false;
+		copy: function ( source, recursive ) {
 
-		},
+			Camera.prototype.copy.call( this, source, recursive );
 
-		getElapsedTime: function () {
+			this.left = source.left;
+			this.right = source.right;
+			this.top = source.top;
+			this.bottom = source.bottom;
+			this.near = source.near;
+			this.far = source.far;
 
-			this.getDelta();
-			return this.elapsedTime;
+			this.zoom = source.zoom;
+			this.view = source.view === null ? null : Object.assign( {}, source.view );
 
-		},
+			return this;
 
-		getDelta: function () {
+		},
 
-			var diff = 0;
+		setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
 
-			if ( this.autoStart && ! this.running ) {
+			if ( this.view === null ) {
 
-				this.start();
-				return 0;
+				this.view = {
+					enabled: true,
+					fullWidth: 1,
+					fullHeight: 1,
+					offsetX: 0,
+					offsetY: 0,
+					width: 1,
+					height: 1
+				};
 
 			}
 
-			if ( this.running ) {
+			this.view.enabled = true;
+			this.view.fullWidth = fullWidth;
+			this.view.fullHeight = fullHeight;
+			this.view.offsetX = x;
+			this.view.offsetY = y;
+			this.view.width = width;
+			this.view.height = height;
 
-				var newTime = ( typeof performance === 'undefined' ? Date : performance ).now();
+			this.updateProjectionMatrix();
 
-				diff = ( newTime - this.oldTime ) / 1000;
-				this.oldTime = newTime;
+		},
 
-				this.elapsedTime += diff;
+		clearViewOffset: function () {
 
-			}
+			if ( this.view !== null ) {
 
-			return diff;
+				this.view.enabled = false;
 
-		}
+			}
 
-	} );
+			this.updateProjectionMatrix();
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		},
 
-	var _position$2 = new Vector3();
-	var _quaternion$3 = new Quaternion();
-	var _scale$1 = new Vector3();
-	var _orientation = new Vector3();
+		updateProjectionMatrix: function () {
 
-	function AudioListener() {
+			var dx = ( this.right - this.left ) / ( 2 * this.zoom );
+			var dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
+			var cx = ( this.right + this.left ) / 2;
+			var cy = ( this.top + this.bottom ) / 2;
 
-		Object3D.call( this );
+			var left = cx - dx;
+			var right = cx + dx;
+			var top = cy + dy;
+			var bottom = cy - dy;
 
-		this.type = 'AudioListener';
+			if ( this.view !== null && this.view.enabled ) {
 
-		this.context = AudioContext.getContext();
+				var scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom;
+				var scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom;
 
-		this.gain = this.context.createGain();
-		this.gain.connect( this.context.destination );
+				left += scaleW * this.view.offsetX;
+				right = left + scaleW * this.view.width;
+				top -= scaleH * this.view.offsetY;
+				bottom = top - scaleH * this.view.height;
 
-		this.filter = null;
+			}
 
-		this.timeDelta = 0;
+			this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far );
 
-		// private
+			this.projectionMatrixInverse.getInverse( this.projectionMatrix );
 
-		this._clock = new Clock();
+		},
 
-	}
+		toJSON: function ( meta ) {
 
-	AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			var data = Object3D.prototype.toJSON.call( this, meta );
 
-		constructor: AudioListener,
+			data.object.zoom = this.zoom;
+			data.object.left = this.left;
+			data.object.right = this.right;
+			data.object.top = this.top;
+			data.object.bottom = this.bottom;
+			data.object.near = this.near;
+			data.object.far = this.far;
 
-		getInput: function () {
+			if ( this.view !== null ) { data.object.view = Object.assign( {}, this.view ); }
 
-			return this.gain;
+			return data;
 
-		},
+		}
 
-		removeFilter: function ( ) {
+	} );
 
-			if ( this.filter !== null ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				this.gain.disconnect( this.filter );
-				this.filter.disconnect( this.context.destination );
-				this.gain.connect( this.context.destination );
-				this.filter = null;
+	function DirectionalLightShadow() {
 
-			}
+		LightShadow.call( this, new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
 
-			return this;
+	}
 
-		},
+	DirectionalLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), {
 
-		getFilter: function () {
+		constructor: DirectionalLightShadow,
 
-			return this.filter;
+		isDirectionalLightShadow: true,
 
-		},
+		updateMatrices: function ( light ) {
 
-		setFilter: function ( value ) {
+			LightShadow.prototype.updateMatrices.call( this, light );
 
-			if ( this.filter !== null ) {
+		}
 
-				this.gain.disconnect( this.filter );
-				this.filter.disconnect( this.context.destination );
+	} );
 
-			} else {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-				this.gain.disconnect( this.context.destination );
+	function DirectionalLight( color, intensity ) {
 
-			}
+		Light.call( this, color, intensity );
 
-			this.filter = value;
-			this.gain.connect( this.filter );
-			this.filter.connect( this.context.destination );
+		this.type = 'DirectionalLight';
 
-			return this;
+		this.position.copy( Object3D.DefaultUp );
+		this.updateMatrix();
 
-		},
+		this.target = new Object3D();
 
-		getMasterVolume: function () {
+		this.shadow = new DirectionalLightShadow();
 
-			return this.gain.gain.value;
+	}
 
-		},
+	DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-		setMasterVolume: function ( value ) {
+		constructor: DirectionalLight,
 
-			this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
+		isDirectionalLight: true,
 
-			return this;
+		copy: function ( source ) {
 
-		},
+			Light.prototype.copy.call( this, source );
 
-		updateMatrixWorld: function ( force ) {
+			this.target = source.target.clone();
 
-			Object3D.prototype.updateMatrixWorld.call( this, force );
+			this.shadow = source.shadow.clone();
 
-			var listener = this.context.listener;
-			var up = this.up;
+			return this;
 
-			this.timeDelta = this._clock.getDelta();
+		}
 
-			this.matrixWorld.decompose( _position$2, _quaternion$3, _scale$1 );
+	} );
 
-			_orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion$3 );
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			if ( listener.positionX ) {
+	function AmbientLight( color, intensity ) {
 
-				// code path for Chrome (see #14393)
+		Light.call( this, color, intensity );
 
-				var endTime = this.context.currentTime + this.timeDelta;
+		this.type = 'AmbientLight';
 
-				listener.positionX.linearRampToValueAtTime( _position$2.x, endTime );
-				listener.positionY.linearRampToValueAtTime( _position$2.y, endTime );
-				listener.positionZ.linearRampToValueAtTime( _position$2.z, endTime );
-				listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime );
-				listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime );
-				listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime );
-				listener.upX.linearRampToValueAtTime( up.x, endTime );
-				listener.upY.linearRampToValueAtTime( up.y, endTime );
-				listener.upZ.linearRampToValueAtTime( up.z, endTime );
+		this.castShadow = undefined;
 
-			} else {
+	}
 
-				listener.setPosition( _position$2.x, _position$2.y, _position$2.z );
-				listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z );
+	AmbientLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-			}
+		constructor: AmbientLight,
 
-		}
+		isAmbientLight: true
 
 	} );
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Reece Aaron Lecrivain / http://reecenotes.com/
+	 * @author abelnation / http://github.com/abelnation
 	 */
 
-	function Audio( listener ) {
+	function RectAreaLight( color, intensity, width, height ) {
 
-		Object3D.call( this );
+		Light.call( this, color, intensity );
 
-		this.type = 'Audio';
+		this.type = 'RectAreaLight';
 
-		this.listener = listener;
-		this.context = listener.context;
+		this.width = ( width !== undefined ) ? width : 10;
+		this.height = ( height !== undefined ) ? height : 10;
 
-		this.gain = this.context.createGain();
-		this.gain.connect( listener.getInput() );
+	}
 
-		this.autoplay = false;
+	RectAreaLight.prototype = Object.assign( Object.create( Light.prototype ), {
 
-		this.buffer = null;
-		this.detune = 0;
-		this.loop = false;
-		this.loopStart = 0;
-		this.loopEnd = 0;
-		this.offset = 0;
-		this.duration = undefined;
-		this.playbackRate = 1;
-		this.isPlaying = false;
-		this.hasPlaybackControl = true;
-		this.sourceType = 'empty';
+		constructor: RectAreaLight,
 
-		this._startedAt = 0;
-		this._progress = 0;
+		isRectAreaLight: true,
 
-		this.filters = [];
+		copy: function ( source ) {
 
-	}
+			Light.prototype.copy.call( this, source );
 
-	Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
+			this.width = source.width;
+			this.height = source.height;
 
-		constructor: Audio,
+			return this;
 
-		getOutput: function () {
+		},
 
-			return this.gain;
+		toJSON: function ( meta ) {
 
-		},
+			var data = Light.prototype.toJSON.call( this, meta );
 
-		setNodeSource: function ( audioNode ) {
+			data.object.width = this.width;
+			data.object.height = this.height;
 
-			this.hasPlaybackControl = false;
-			this.sourceType = 'audioNode';
-			this.source = audioNode;
-			this.connect();
+			return data;
 
-			return this;
+		}
 
-		},
+	} );
 
-		setMediaElementSource: function ( mediaElement ) {
+	/**
+	 * @author bhouston / http://clara.io
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * Primary reference:
+	 *   https://graphics.stanford.edu/papers/envmap/envmap.pdf
+	 *
+	 * Secondary reference:
+	 *   https://www.ppsloan.org/publications/StupidSH36.pdf
+	 */
 
-			this.hasPlaybackControl = false;
-			this.sourceType = 'mediaNode';
-			this.source = this.context.createMediaElementSource( mediaElement );
-			this.connect();
+	// 3-band SH defined by 9 coefficients
 
-			return this;
+	function SphericalHarmonics3() {
 
-		},
+		this.coefficients = [];
 
-		setMediaStreamSource: function ( mediaStream ) {
+		for ( var i = 0; i < 9; i ++ ) {
 
-			this.hasPlaybackControl = false;
-			this.sourceType = 'mediaStreamNode';
-			this.source = this.context.createMediaStreamSource( mediaStream );
-			this.connect();
+			this.coefficients.push( new Vector3() );
 
-			return this;
+		}
 
-		},
+	}
 
-		setBuffer: function ( audioBuffer ) {
+	Object.assign( SphericalHarmonics3.prototype, {
 
-			this.buffer = audioBuffer;
-			this.sourceType = 'buffer';
+		isSphericalHarmonics3: true,
 
-			if ( this.autoplay ) { this.play(); }
+		set: function ( coefficients ) {
+
+			for ( var i = 0; i < 9; i ++ ) {
+
+				this.coefficients[ i ].copy( coefficients[ i ] );
+
+			}
 
 			return this;
 
 		},
 
-		play: function ( delay ) {
-
-			if ( delay === undefined ) { delay = 0; }
+		zero: function () {
 
-			if ( this.isPlaying === true ) {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				console.warn( 'THREE.Audio: Audio is already playing.' );
-				return;
+				this.coefficients[ i ].set( 0, 0, 0 );
 
 			}
 
-			if ( this.hasPlaybackControl === false ) {
+			return this;
 
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return;
+		},
 
-			}
+		// get the radiance in the direction of the normal
+		// target is a Vector3
+		getAt: function ( normal, target ) {
 
-			this._startedAt = this.context.currentTime + delay;
+			// normal is assumed to be unit length
 
-			var source = this.context.createBufferSource();
-			source.buffer = this.buffer;
-			source.loop = this.loop;
-			source.loopStart = this.loopStart;
-			source.loopEnd = this.loopEnd;
-			source.onended = this.onEnded.bind( this );
-			source.start( this._startedAt, this._progress + this.offset, this.duration );
+			var x = normal.x, y = normal.y, z = normal.z;
 
-			this.isPlaying = true;
+			var coeff = this.coefficients;
 
-			this.source = source;
+			// band 0
+			target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );
 
-			this.setDetune( this.detune );
-			this.setPlaybackRate( this.playbackRate );
+			// band 1
+			target.addScaledVector( coeff[ 1 ], 0.488603 * y );
+			target.addScaledVector( coeff[ 2 ], 0.488603 * z );
+			target.addScaledVector( coeff[ 3 ], 0.488603 * x );
 
-			return this.connect();
+			// band 2
+			target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) );
+			target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) );
+			target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
+			target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) );
+			target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
 
-		},
+			return target;
 
-		pause: function () {
+		},
 
-			if ( this.hasPlaybackControl === false ) {
+		// get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
+		// target is a Vector3
+		// https://graphics.stanford.edu/papers/envmap/envmap.pdf
+		getIrradianceAt: function ( normal, target ) {
 
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return;
+			// normal is assumed to be unit length
 
-			}
+			var x = normal.x, y = normal.y, z = normal.z;
 
-			if ( this.isPlaying === true ) {
+			var coeff = this.coefficients;
 
-				// update current progress
+			// band 0
+			target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095
 
-				this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
+			// band 1
+			target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
+			target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z );
+			target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x );
 
-				if ( this.loop === true ) {
+			// band 2
+			target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
+			target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z );
+			target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
+			target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z );
+			target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
 
-					// ensure _progress does not exceed duration with looped audios
+			return target;
 
-					this._progress = this._progress % ( this.duration || this.buffer.duration );
+		},
 
-				}
+		add: function ( sh ) {
 
-				this.source.stop();
-				this.source.onended = null;
+			for ( var i = 0; i < 9; i ++ ) {
 
-				this.isPlaying = false;
+				this.coefficients[ i ].add( sh.coefficients[ i ] );
 
 			}
 
@@ -51029,42 +58677,35 @@ function simpleEnd(buf) {
 
 		},
 
-		stop: function () {
+		addScaledSH: function ( sh, s ) {
 
-			if ( this.hasPlaybackControl === false ) {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return;
+				this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s );
 
 			}
 
-			this._progress = 0;
-
-			this.source.stop();
-			this.source.onended = null;
-			this.isPlaying = false;
-
 			return this;
 
 		},
 
-		connect: function () {
+		scale: function ( s ) {
 
-			if ( this.filters.length > 0 ) {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				this.source.connect( this.filters[ 0 ] );
+				this.coefficients[ i ].multiplyScalar( s );
 
-				for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
+			}
 
-					this.filters[ i - 1 ].connect( this.filters[ i ] );
+			return this;
 
-				}
+		},
 
-				this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
+		lerp: function ( sh, alpha ) {
 
-			} else {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				this.source.connect( this.getOutput() );
+				this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha );
 
 			}
 
@@ -51072,49 +58713,43 @@ function simpleEnd(buf) {
 
 		},
 
-		disconnect: function () {
-
-			if ( this.filters.length > 0 ) {
+		equals: function ( sh ) {
 
-				this.source.disconnect( this.filters[ 0 ] );
+			for ( var i = 0; i < 9; i ++ ) {
 
-				for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
+				if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) {
 
-					this.filters[ i - 1 ].disconnect( this.filters[ i ] );
+					return false;
 
 				}
 
-				this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
+			}
 
-			} else {
+			return true;
 
-				this.source.disconnect( this.getOutput() );
+		},
 
-			}
+		copy: function ( sh ) {
 
-			return this;
+			return this.set( sh.coefficients );
 
 		},
 
-		getFilters: function () {
+		clone: function () {
 
-			return this.filters;
+			return new this.constructor().copy( this );
 
 		},
 
-		setFilters: function ( value ) {
-
-			if ( ! value ) { value = []; }
+		fromArray: function ( array, offset ) {
 
-			if ( this.isPlaying === true ) {
+			if ( offset === undefined ) { offset = 0; }
 
-				this.disconnect();
-				this.filters = value;
-				this.connect();
+			var coefficients = this.coefficients;
 
-			} else {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				this.filters = value;
+				coefficients[ i ].fromArray( array, offset + ( i * 3 ) );
 
 			}
 
@@ -51122,3189 +58757,3183 @@ function simpleEnd(buf) {
 
 		},
 
-		setDetune: function ( value ) {
+		toArray: function ( array, offset ) {
 
-			this.detune = value;
+			if ( array === undefined ) { array = []; }
+			if ( offset === undefined ) { offset = 0; }
 
-			if ( this.source.detune === undefined ) { return; } // only set detune when available
+			var coefficients = this.coefficients;
 
-			if ( this.isPlaying === true ) {
+			for ( var i = 0; i < 9; i ++ ) {
 
-				this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
+				coefficients[ i ].toArray( array, offset + ( i * 3 ) );
 
 			}
 
-			return this;
-
-		},
+			return array;
 
-		getDetune: function () {
+		}
 
-			return this.detune;
+	} );
 
-		},
+	Object.assign( SphericalHarmonics3, {
 
-		getFilter: function () {
+		// evaluate the basis functions
+		// shBasis is an Array[ 9 ]
+		getBasisAt: function ( normal, shBasis ) {
 
-			return this.getFilters()[ 0 ];
+			// normal is assumed to be unit length
 
-		},
+			var x = normal.x, y = normal.y, z = normal.z;
 
-		setFilter: function ( filter ) {
+			// band 0
+			shBasis[ 0 ] = 0.282095;
 
-			return this.setFilters( filter ? [ filter ] : [] );
+			// band 1
+			shBasis[ 1 ] = 0.488603 * y;
+			shBasis[ 2 ] = 0.488603 * z;
+			shBasis[ 3 ] = 0.488603 * x;
 
-		},
+			// band 2
+			shBasis[ 4 ] = 1.092548 * x * y;
+			shBasis[ 5 ] = 1.092548 * y * z;
+			shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 );
+			shBasis[ 7 ] = 1.092548 * x * z;
+			shBasis[ 8 ] = 0.546274 * ( x * x - y * y );
 
-		setPlaybackRate: function ( value ) {
+		}
 
-			if ( this.hasPlaybackControl === false ) {
+	} );
 
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return;
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * A LightProbe is a source of indirect-diffuse light
+	 */
 
-			}
+	function LightProbe( sh, intensity ) {
 
-			this.playbackRate = value;
+		Light.call( this, undefined, intensity );
 
-			if ( this.isPlaying === true ) {
+		this.type = 'LightProbe';
 
-				this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
+		this.sh = ( sh !== undefined ) ? sh : new SphericalHarmonics3();
 
-			}
+	}
 
-			return this;
+	LightProbe.prototype = Object.assign( Object.create( Light.prototype ), {
 
-		},
+		constructor: LightProbe,
 
-		getPlaybackRate: function () {
+		isLightProbe: true,
 
-			return this.playbackRate;
+		copy: function ( source ) {
 
-		},
+			Light.prototype.copy.call( this, source );
 
-		onEnded: function () {
+			this.sh.copy( source.sh );
 
-			this.isPlaying = false;
+			return this;
 
 		},
 
-		getLoop: function () {
-
-			if ( this.hasPlaybackControl === false ) {
-
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return false;
+		fromJSON: function ( json ) {
 
-			}
+			this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON();
+			this.sh.fromArray( json.sh );
 
-			return this.loop;
+			return this;
 
 		},
 
-		setLoop: function ( value ) {
+		toJSON: function ( meta ) {
 
-			if ( this.hasPlaybackControl === false ) {
+			var data = Light.prototype.toJSON.call( this, meta );
 
-				console.warn( 'THREE.Audio: this Audio has no playback control.' );
-				return;
+			data.object.sh = this.sh.toArray();
 
-			}
+			return data;
 
-			this.loop = value;
-
-			if ( this.isPlaying === true ) {
-
-				this.source.loop = this.loop;
+		}
 
-			}
+	} );
 
-			return this;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		},
+	function MaterialLoader( manager ) {
 
-		setLoopStart: function ( value ) {
+		Loader.call( this, manager );
 
-			this.loopStart = value;
+		this.textures = {};
 
-			return this;
+	}
 
-		},
+	MaterialLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-		setLoopEnd: function ( value ) {
+		constructor: MaterialLoader,
 
-			this.loopEnd = value;
+		load: function ( url, onLoad, onProgress, onError ) {
 
-			return this;
+			var scope = this;
 
-		},
+			var loader = new FileLoader( scope.manager );
+			loader.setPath( scope.path );
+			loader.load( url, function ( text ) {
 
-		getVolume: function () {
+				onLoad( scope.parse( JSON.parse( text ) ) );
 
-			return this.gain.gain.value;
+			}, onProgress, onError );
 
 		},
 
-		setVolume: function ( value ) {
+		parse: function ( json ) {
 
-			this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
+			var textures = this.textures;
 
-			return this;
+			function getTexture( name ) {
 
-		}
+				if ( textures[ name ] === undefined ) {
 
-	} );
+					console.warn( 'THREE.MaterialLoader: Undefined texture', name );
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				}
 
-	var _position$3 = new Vector3();
-	var _quaternion$4 = new Quaternion();
-	var _scale$2 = new Vector3();
-	var _orientation$1 = new Vector3();
+				return textures[ name ];
 
-	function PositionalAudio( listener ) {
+			}
 
-		Audio.call( this, listener );
+			var material = new Materials[ json.type ]();
 
-		this.panner = this.context.createPanner();
-		this.panner.panningModel = 'HRTF';
-		this.panner.connect( this.gain );
+			if ( json.uuid !== undefined ) { material.uuid = json.uuid; }
+			if ( json.name !== undefined ) { material.name = json.name; }
+			if ( json.color !== undefined ) { material.color.setHex( json.color ); }
+			if ( json.roughness !== undefined ) { material.roughness = json.roughness; }
+			if ( json.metalness !== undefined ) { material.metalness = json.metalness; }
+			if ( json.sheen !== undefined ) { material.sheen = new Color().setHex( json.sheen ); }
+			if ( json.emissive !== undefined ) { material.emissive.setHex( json.emissive ); }
+			if ( json.specular !== undefined ) { material.specular.setHex( json.specular ); }
+			if ( json.shininess !== undefined ) { material.shininess = json.shininess; }
+			if ( json.clearcoat !== undefined ) { material.clearcoat = json.clearcoat; }
+			if ( json.clearcoatRoughness !== undefined ) { material.clearcoatRoughness = json.clearcoatRoughness; }
+			if ( json.fog !== undefined ) { material.fog = json.fog; }
+			if ( json.flatShading !== undefined ) { material.flatShading = json.flatShading; }
+			if ( json.blending !== undefined ) { material.blending = json.blending; }
+			if ( json.combine !== undefined ) { material.combine = json.combine; }
+			if ( json.side !== undefined ) { material.side = json.side; }
+			if ( json.opacity !== undefined ) { material.opacity = json.opacity; }
+			if ( json.transparent !== undefined ) { material.transparent = json.transparent; }
+			if ( json.alphaTest !== undefined ) { material.alphaTest = json.alphaTest; }
+			if ( json.depthTest !== undefined ) { material.depthTest = json.depthTest; }
+			if ( json.depthWrite !== undefined ) { material.depthWrite = json.depthWrite; }
+			if ( json.colorWrite !== undefined ) { material.colorWrite = json.colorWrite; }
 
-	}
+			if ( json.stencilWrite !== undefined ) { material.stencilWrite = json.stencilWrite; }
+			if ( json.stencilWriteMask !== undefined ) { material.stencilWriteMask = json.stencilWriteMask; }
+			if ( json.stencilFunc !== undefined ) { material.stencilFunc = json.stencilFunc; }
+			if ( json.stencilRef !== undefined ) { material.stencilRef = json.stencilRef; }
+			if ( json.stencilFuncMask !== undefined ) { material.stencilFuncMask = json.stencilFuncMask; }
+			if ( json.stencilFail !== undefined ) { material.stencilFail = json.stencilFail; }
+			if ( json.stencilZFail !== undefined ) { material.stencilZFail = json.stencilZFail; }
+			if ( json.stencilZPass !== undefined ) { material.stencilZPass = json.stencilZPass; }
 
-	PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
+			if ( json.wireframe !== undefined ) { material.wireframe = json.wireframe; }
+			if ( json.wireframeLinewidth !== undefined ) { material.wireframeLinewidth = json.wireframeLinewidth; }
+			if ( json.wireframeLinecap !== undefined ) { material.wireframeLinecap = json.wireframeLinecap; }
+			if ( json.wireframeLinejoin !== undefined ) { material.wireframeLinejoin = json.wireframeLinejoin; }
 
-		constructor: PositionalAudio,
+			if ( json.rotation !== undefined ) { material.rotation = json.rotation; }
 
-		getOutput: function () {
+			if ( json.linewidth !== 1 ) { material.linewidth = json.linewidth; }
+			if ( json.dashSize !== undefined ) { material.dashSize = json.dashSize; }
+			if ( json.gapSize !== undefined ) { material.gapSize = json.gapSize; }
+			if ( json.scale !== undefined ) { material.scale = json.scale; }
 
-			return this.panner;
+			if ( json.polygonOffset !== undefined ) { material.polygonOffset = json.polygonOffset; }
+			if ( json.polygonOffsetFactor !== undefined ) { material.polygonOffsetFactor = json.polygonOffsetFactor; }
+			if ( json.polygonOffsetUnits !== undefined ) { material.polygonOffsetUnits = json.polygonOffsetUnits; }
 
-		},
+			if ( json.skinning !== undefined ) { material.skinning = json.skinning; }
+			if ( json.morphTargets !== undefined ) { material.morphTargets = json.morphTargets; }
+			if ( json.morphNormals !== undefined ) { material.morphNormals = json.morphNormals; }
+			if ( json.dithering !== undefined ) { material.dithering = json.dithering; }
 
-		getRefDistance: function () {
+			if ( json.vertexTangents !== undefined ) { material.vertexTangents = json.vertexTangents; }
 
-			return this.panner.refDistance;
+			if ( json.visible !== undefined ) { material.visible = json.visible; }
 
-		},
+			if ( json.toneMapped !== undefined ) { material.toneMapped = json.toneMapped; }
 
-		setRefDistance: function ( value ) {
+			if ( json.userData !== undefined ) { material.userData = json.userData; }
 
-			this.panner.refDistance = value;
+			if ( json.vertexColors !== undefined ) {
 
-			return this;
+				if ( typeof json.vertexColors === 'number' ) {
 
-		},
+					material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
 
-		getRolloffFactor: function () {
+				} else {
 
-			return this.panner.rolloffFactor;
+					material.vertexColors = json.vertexColors;
 
-		},
+				}
 
-		setRolloffFactor: function ( value ) {
+			}
 
-			this.panner.rolloffFactor = value;
+			// Shader Material
 
-			return this;
+			if ( json.uniforms !== undefined ) {
 
-		},
+				for ( var name in json.uniforms ) {
 
-		getDistanceModel: function () {
+					var uniform = json.uniforms[ name ];
 
-			return this.panner.distanceModel;
+					material.uniforms[ name ] = {};
 
-		},
+					switch ( uniform.type ) {
 
-		setDistanceModel: function ( value ) {
+						case 't':
+							material.uniforms[ name ].value = getTexture( uniform.value );
+							break;
 
-			this.panner.distanceModel = value;
+						case 'c':
+							material.uniforms[ name ].value = new Color().setHex( uniform.value );
+							break;
 
-			return this;
+						case 'v2':
+							material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
+							break;
 
-		},
+						case 'v3':
+							material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
+							break;
 
-		getMaxDistance: function () {
+						case 'v4':
+							material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
+							break;
 
-			return this.panner.maxDistance;
+						case 'm3':
+							material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
 
-		},
+						case 'm4':
+							material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
+							break;
 
-		setMaxDistance: function ( value ) {
+						default:
+							material.uniforms[ name ].value = uniform.value;
 
-			this.panner.maxDistance = value;
+					}
 
-			return this;
+				}
 
-		},
+			}
 
-		setDirectionalCone: function ( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
+			if ( json.defines !== undefined ) { material.defines = json.defines; }
+			if ( json.vertexShader !== undefined ) { material.vertexShader = json.vertexShader; }
+			if ( json.fragmentShader !== undefined ) { material.fragmentShader = json.fragmentShader; }
 
-			this.panner.coneInnerAngle = coneInnerAngle;
-			this.panner.coneOuterAngle = coneOuterAngle;
-			this.panner.coneOuterGain = coneOuterGain;
+			if ( json.extensions !== undefined ) {
 
-			return this;
+				for ( var key in json.extensions ) {
 
-		},
+					material.extensions[ key ] = json.extensions[ key ];
 
-		updateMatrixWorld: function ( force ) {
+				}
 
-			Object3D.prototype.updateMatrixWorld.call( this, force );
+			}
 
-			if ( this.hasPlaybackControl === true && this.isPlaying === false ) { return; }
+			// Deprecated
 
-			this.matrixWorld.decompose( _position$3, _quaternion$4, _scale$2 );
+			if ( json.shading !== undefined ) { material.flatShading = json.shading === 1; } // THREE.FlatShading
 
-			_orientation$1.set( 0, 0, 1 ).applyQuaternion( _quaternion$4 );
+			// for PointsMaterial
 
-			var panner = this.panner;
+			if ( json.size !== undefined ) { material.size = json.size; }
+			if ( json.sizeAttenuation !== undefined ) { material.sizeAttenuation = json.sizeAttenuation; }
 
-			if ( panner.positionX ) {
+			// maps
 
-				// code path for Chrome and Firefox (see #14393)
+			if ( json.map !== undefined ) { material.map = getTexture( json.map ); }
+			if ( json.matcap !== undefined ) { material.matcap = getTexture( json.matcap ); }
 
-				var endTime = this.context.currentTime + this.listener.timeDelta;
+			if ( json.alphaMap !== undefined ) { material.alphaMap = getTexture( json.alphaMap ); }
 
-				panner.positionX.linearRampToValueAtTime( _position$3.x, endTime );
-				panner.positionY.linearRampToValueAtTime( _position$3.y, endTime );
-				panner.positionZ.linearRampToValueAtTime( _position$3.z, endTime );
-				panner.orientationX.linearRampToValueAtTime( _orientation$1.x, endTime );
-				panner.orientationY.linearRampToValueAtTime( _orientation$1.y, endTime );
-				panner.orientationZ.linearRampToValueAtTime( _orientation$1.z, endTime );
+			if ( json.bumpMap !== undefined ) { material.bumpMap = getTexture( json.bumpMap ); }
+			if ( json.bumpScale !== undefined ) { material.bumpScale = json.bumpScale; }
 
-			} else {
+			if ( json.normalMap !== undefined ) { material.normalMap = getTexture( json.normalMap ); }
+			if ( json.normalMapType !== undefined ) { material.normalMapType = json.normalMapType; }
+			if ( json.normalScale !== undefined ) {
 
-				panner.setPosition( _position$3.x, _position$3.y, _position$3.z );
-				panner.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z );
+				var normalScale = json.normalScale;
 
-			}
+				if ( Array.isArray( normalScale ) === false ) {
 
-		}
+					// Blender exporter used to export a scalar. See #7459
 
-	} );
+					normalScale = [ normalScale, normalScale ];
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				}
 
-	function AudioAnalyser( audio, fftSize ) {
+				material.normalScale = new Vector2().fromArray( normalScale );
 
-		this.analyser = audio.context.createAnalyser();
-		this.analyser.fftSize = fftSize !== undefined ? fftSize : 2048;
+			}
 
-		this.data = new Uint8Array( this.analyser.frequencyBinCount );
+			if ( json.displacementMap !== undefined ) { material.displacementMap = getTexture( json.displacementMap ); }
+			if ( json.displacementScale !== undefined ) { material.displacementScale = json.displacementScale; }
+			if ( json.displacementBias !== undefined ) { material.displacementBias = json.displacementBias; }
 
-		audio.getOutput().connect( this.analyser );
+			if ( json.roughnessMap !== undefined ) { material.roughnessMap = getTexture( json.roughnessMap ); }
+			if ( json.metalnessMap !== undefined ) { material.metalnessMap = getTexture( json.metalnessMap ); }
 
-	}
+			if ( json.emissiveMap !== undefined ) { material.emissiveMap = getTexture( json.emissiveMap ); }
+			if ( json.emissiveIntensity !== undefined ) { material.emissiveIntensity = json.emissiveIntensity; }
 
-	Object.assign( AudioAnalyser.prototype, {
+			if ( json.specularMap !== undefined ) { material.specularMap = getTexture( json.specularMap ); }
 
-		getFrequencyData: function () {
+			if ( json.envMap !== undefined ) { material.envMap = getTexture( json.envMap ); }
+			if ( json.envMapIntensity !== undefined ) { material.envMapIntensity = json.envMapIntensity; }
 
-			this.analyser.getByteFrequencyData( this.data );
+			if ( json.reflectivity !== undefined ) { material.reflectivity = json.reflectivity; }
+			if ( json.refractionRatio !== undefined ) { material.refractionRatio = json.refractionRatio; }
 
-			return this.data;
+			if ( json.lightMap !== undefined ) { material.lightMap = getTexture( json.lightMap ); }
+			if ( json.lightMapIntensity !== undefined ) { material.lightMapIntensity = json.lightMapIntensity; }
 
-		},
+			if ( json.aoMap !== undefined ) { material.aoMap = getTexture( json.aoMap ); }
+			if ( json.aoMapIntensity !== undefined ) { material.aoMapIntensity = json.aoMapIntensity; }
 
-		getAverageFrequency: function () {
+			if ( json.gradientMap !== undefined ) { material.gradientMap = getTexture( json.gradientMap ); }
 
-			var value = 0, data = this.getFrequencyData();
+			if ( json.clearcoatMap !== undefined ) { material.clearcoatMap = getTexture( json.clearcoatMap ); }
+			if ( json.clearcoatRoughnessMap !== undefined ) { material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap ); }
+			if ( json.clearcoatNormalMap !== undefined ) { material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap ); }
+			if ( json.clearcoatNormalScale !== undefined ) { material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale ); }
 
-			for ( var i = 0; i < data.length; i ++ ) {
+			return material;
 
-				value += data[ i ];
+		},
 
-			}
+		setTextures: function ( value ) {
 
-			return value / data.length;
+			this.textures = value;
+			return this;
 
 		}
 
 	} );
 
 	/**
-	 *
-	 * Buffered scene graph property that allows weighted accumulation.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
+	 * @author Don McCurdy / https://www.donmccurdy.com
 	 */
 
-	function PropertyMixer( binding, typeName, valueSize ) {
-
-		this.binding = binding;
-		this.valueSize = valueSize;
+	var LoaderUtils = {
 
-		var mixFunction,
-			mixFunctionAdditive,
-			setIdentity;
+		decodeText: function ( array ) {
 
-		// buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ]
-		//
-		// interpolators can use .buffer as their .result
-		// the data then goes to 'incoming'
-		//
-		// 'accu0' and 'accu1' are used frame-interleaved for
-		// the cumulative result and are compared to detect
-		// changes
-		//
-		// 'orig' stores the original state of the property
-		//
-		// 'add' is used for additive cumulative results
-		//
-		// 'work' is optional and is only present for quaternion types. It is used
-		// to store intermediate quaternion multiplication results
+			if ( typeof TextDecoder !== 'undefined' ) {
 
-		switch ( typeName ) {
+				return new TextDecoder().decode( array );
 
-			case 'quaternion':
-				mixFunction = this._slerp;
-				mixFunctionAdditive = this._slerpAdditive;
-				setIdentity = this._setAdditiveIdentityQuaternion;
+			}
 
-				this.buffer = new Float64Array( valueSize * 6 );
-				this._workIndex = 5;
-				break;
+			// Avoid the String.fromCharCode.apply(null, array) shortcut, which
+			// throws a "maximum call stack size exceeded" error for large arrays.
 
-			case 'string':
-			case 'bool':
-				mixFunction = this._select;
+			var s = '';
 
-				// Use the regular mix function and for additive on these types,
-				// additive is not relevant for non-numeric types
-				mixFunctionAdditive = this._select;
+			for ( var i = 0, il = array.length; i < il; i ++ ) {
 
-				setIdentity = this._setAdditiveIdentityOther;
+				// Implicitly assumes little-endian.
+				s += String.fromCharCode( array[ i ] );
 
-				this.buffer = new Array( valueSize * 5 );
-				break;
+			}
 
-			default:
-				mixFunction = this._lerp;
-				mixFunctionAdditive = this._lerpAdditive;
-				setIdentity = this._setAdditiveIdentityNumeric;
+			try {
 
-				this.buffer = new Float64Array( valueSize * 5 );
+				// merges multi-byte utf-8 characters.
 
-		}
+				return decodeURIComponent( escape( s ) );
 
-		this._mixBufferRegion = mixFunction;
-		this._mixBufferRegionAdditive = mixFunctionAdditive;
-		this._setIdentity = setIdentity;
-		this._origIndex = 3;
-		this._addIndex = 4;
+			} catch ( e ) { // see #16358
 
-		this.cumulativeWeight = 0;
-		this.cumulativeWeightAdditive = 0;
+				return s;
 
-		this.useCount = 0;
-		this.referenceCount = 0;
+			}
 
-	}
+		},
 
-	Object.assign( PropertyMixer.prototype, {
+		extractUrlBase: function ( url ) {
 
-		// accumulate data in the 'incoming' region into 'accu<i>'
-		accumulate: function ( accuIndex, weight ) {
+			var index = url.lastIndexOf( '/' );
 
-			// note: happily accumulating nothing when weight = 0, the caller knows
-			// the weight and shouldn't have made the call in the first place
+			if ( index === - 1 ) { return './'; }
 
-			var buffer = this.buffer,
-				stride = this.valueSize,
-				offset = accuIndex * stride + stride,
+			return url.substr( 0, index + 1 );
 
-				currentWeight = this.cumulativeWeight;
+		}
 
-			if ( currentWeight === 0 ) {
+	};
 
-				// accuN := incoming * weight
+	/**
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 */
 
-				for ( var i = 0; i !== stride; ++ i ) {
+	function InstancedBufferGeometry() {
 
-					buffer[ offset + i ] = buffer[ i ];
+		BufferGeometry.call( this );
 
-				}
+		this.type = 'InstancedBufferGeometry';
+		this.maxInstancedCount = undefined;
 
-				currentWeight = weight;
+	}
 
-			} else {
+	InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry.prototype ), {
 
-				// accuN := accuN + incoming * weight
+		constructor: InstancedBufferGeometry,
 
-				currentWeight += weight;
-				var mix = weight / currentWeight;
-				this._mixBufferRegion( buffer, offset, 0, mix, stride );
+		isInstancedBufferGeometry: true,
 
-			}
+		copy: function ( source ) {
 
-			this.cumulativeWeight = currentWeight;
+			BufferGeometry.prototype.copy.call( this, source );
 
-		},
+			this.maxInstancedCount = source.maxInstancedCount;
 
-		// accumulate data in the 'incoming' region into 'add'
-		accumulateAdditive: function ( weight ) {
+			return this;
 
-			var buffer = this.buffer,
-				stride = this.valueSize,
-				offset = stride * this._addIndex;
+		},
 
-			if ( this.cumulativeWeightAdditive === 0 ) {
+		clone: function () {
 
-				// add = identity
+			return new this.constructor().copy( this );
 
-				this._setIdentity();
+		},
 
-			}
+		toJSON: function () {
 
-			// add := add + incoming * weight
+			var data = BufferGeometry.prototype.toJSON.call( this );
 
-			this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride );
-			this.cumulativeWeightAdditive += weight;
+			data.maxInstancedCount = this.maxInstancedCount;
 
-		},
+			data.isInstancedBufferGeometry = true;
 
-		// apply the state of 'accu<i>' to the binding when accus differ
-		apply: function ( accuIndex ) {
+			return data;
 
-			var stride = this.valueSize,
-				buffer = this.buffer,
-				offset = accuIndex * stride + stride,
+		}
 
-				weight = this.cumulativeWeight,
-				weightAdditive = this.cumulativeWeightAdditive,
+	} );
 
-				binding = this.binding;
+	/**
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 */
 
-			this.cumulativeWeight = 0;
-			this.cumulativeWeightAdditive = 0;
+	function InstancedBufferAttribute( array, itemSize, normalized, meshPerAttribute ) {
 
-			if ( weight < 1 ) {
+		if ( typeof ( normalized ) === 'number' ) {
 
-				// accuN := accuN + original * ( 1 - cumulativeWeight )
+			meshPerAttribute = normalized;
 
-				var originalValueOffset = stride * this._origIndex;
+			normalized = false;
 
-				this._mixBufferRegion(
-					buffer, offset, originalValueOffset, 1 - weight, stride );
+			console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' );
 
-			}
+		}
 
-			if ( weightAdditive > 0 ) {
+		BufferAttribute.call( this, array, itemSize, normalized );
 
-				// accuN := accuN + additive accuN
+		this.meshPerAttribute = meshPerAttribute || 1;
 
-				this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride );
+	}
 
-			}
+	InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribute.prototype ), {
 
-			for ( var i = stride, e = stride + stride; i !== e; ++ i ) {
+		constructor: InstancedBufferAttribute,
 
-				if ( buffer[ i ] !== buffer[ i + stride ] ) {
+		isInstancedBufferAttribute: true,
 
-					// value has changed -> update scene graph
+		copy: function ( source ) {
 
-					binding.setValue( buffer, offset );
-					break;
+			BufferAttribute.prototype.copy.call( this, source );
 
-				}
+			this.meshPerAttribute = source.meshPerAttribute;
 
-			}
+			return this;
 
 		},
 
-		// remember the state of the bound property and copy it to both accus
-		saveOriginalState: function () {
+		toJSON: function ()	{
 
-			var binding = this.binding;
+			var data = BufferAttribute.prototype.toJSON.call( this );
 
-			var buffer = this.buffer,
-				stride = this.valueSize,
+			data.meshPerAttribute = this.meshPerAttribute;
 
-				originalValueOffset = stride * this._origIndex;
+			data.isInstancedBufferAttribute = true;
 
-			binding.getValue( buffer, originalValueOffset );
+			return data;
 
-			// accu[0..1] := orig -- initially detect changes against the original
-			for ( var i = stride, e = originalValueOffset; i !== e; ++ i ) {
+		}
 
-				buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ];
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			// Add to identity for additive
-			this._setIdentity();
+	function BufferGeometryLoader( manager ) {
 
-			this.cumulativeWeight = 0;
-			this.cumulativeWeightAdditive = 0;
+		Loader.call( this, manager );
 
-		},
+	}
 
-		// apply the state previously taken via 'saveOriginalState' to the binding
-		restoreOriginalState: function () {
+	BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-			var originalValueOffset = this.valueSize * 3;
-			this.binding.setValue( this.buffer, originalValueOffset );
+		constructor: BufferGeometryLoader,
 
-		},
+		load: function ( url, onLoad, onProgress, onError ) {
 
-		_setAdditiveIdentityNumeric: function () {
+			var scope = this;
 
-			var startIndex = this._addIndex * this.valueSize;
+			var loader = new FileLoader( scope.manager );
+			loader.setPath( scope.path );
+			loader.load( url, function ( text ) {
 
-			this.buffer.fill( 0, startIndex, startIndex + this.valueSize );
+				onLoad( scope.parse( JSON.parse( text ) ) );
+
+			}, onProgress, onError );
 
 		},
 
-		_setAdditiveIdentityQuaternion: function () {
+		parse: function ( json ) {
 
-			this._setAdditiveIdentityNumeric();
-			this.buffer[ this._addIndex * 4 + 3 ] = 1;
+			var geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
 
-		},
+			var index = json.data.index;
 
-		_setAdditiveIdentityOther: function () {
+			if ( index !== undefined ) {
 
-			var startIndex = this._origIndex * this.valueSize;
-			var targetIndex = this._addIndex * this.valueSize;
+				var typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
+				geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
 
-			this.buffer.copyWithin( targetIndex, startIndex, this.valueSize );
+			}
 
-		},
+			var attributes = json.data.attributes;
 
+			for ( var key in attributes ) {
 
-		// mix functions
+				var attribute = attributes[ key ];
+				var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
+				var bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
+				var bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
+				if ( attribute.name !== undefined ) { bufferAttribute.name = attribute.name; }
+				geometry.setAttribute( key, bufferAttribute );
 
-		_select: function ( buffer, dstOffset, srcOffset, t, stride ) {
+			}
 
-			if ( t >= 0.5 ) {
+			var morphAttributes = json.data.morphAttributes;
 
-				for ( var i = 0; i !== stride; ++ i ) {
+			if ( morphAttributes ) {
 
-					buffer[ dstOffset + i ] = buffer[ srcOffset + i ];
+				for ( var key in morphAttributes ) {
 
-				}
+					var attributeArray = morphAttributes[ key ];
 
-			}
+					var array = [];
 
-		},
+					for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
 
-		_slerp: function ( buffer, dstOffset, srcOffset, t ) {
+						var attribute = attributeArray[ i ];
+						var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
 
-			Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t );
+						var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
+						if ( attribute.name !== undefined ) { bufferAttribute.name = attribute.name; }
+						array.push( bufferAttribute );
 
-		},
+					}
 
-		_slerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) {
+					geometry.morphAttributes[ key ] = array;
 
-			var workOffset = this._workIndex * stride;
+				}
 
-			// Store result in intermediate buffer offset
-			Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset );
+			}
 
-			// Slerp to the intermediate result
-			Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t );
+			var morphTargetsRelative = json.data.morphTargetsRelative;
 
-		},
+			if ( morphTargetsRelative ) {
 
-		_lerp: function ( buffer, dstOffset, srcOffset, t, stride ) {
+				geometry.morphTargetsRelative = true;
 
-			var s = 1 - t;
+			}
 
-			for ( var i = 0; i !== stride; ++ i ) {
+			var groups = json.data.groups || json.data.drawcalls || json.data.offsets;
 
-				var j = dstOffset + i;
+			if ( groups !== undefined ) {
 
-				buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t;
+				for ( var i = 0, n = groups.length; i !== n; ++ i ) {
+
+					var group = groups[ i ];
+
+					geometry.addGroup( group.start, group.count, group.materialIndex );
+
+				}
 
 			}
 
-		},
+			var boundingSphere = json.data.boundingSphere;
 
-		_lerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) {
+			if ( boundingSphere !== undefined ) {
 
-			for ( var i = 0; i !== stride; ++ i ) {
+				var center = new Vector3();
 
-				var j = dstOffset + i;
+				if ( boundingSphere.center !== undefined ) {
 
-				buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t;
+					center.fromArray( boundingSphere.center );
+
+				}
+
+				geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
 
 			}
 
+			if ( json.name ) { geometry.name = json.name; }
+			if ( json.userData ) { geometry.userData = json.userData; }
+
+			return geometry;
+
 		}
 
 	} );
 
+	var TYPED_ARRAYS = {
+		Int8Array: Int8Array,
+		Uint8Array: Uint8Array,
+		// Workaround for IE11 pre KB2929437. See #11440
+		Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
+		Int16Array: Int16Array,
+		Uint16Array: Uint16Array,
+		Int32Array: Int32Array,
+		Uint32Array: Uint32Array,
+		Float32Array: Float32Array,
+		Float64Array: Float64Array
+	};
+
 	/**
-	 *
-	 * A reference to a real property in the scene graph.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
+	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	// Characters [].:/ are reserved for track binding syntax.
-	var _RESERVED_CHARS_RE = '\\[\\]\\.:\\/';
-	var _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' );
+	function ObjectLoader( manager ) {
 
-	// Attempts to allow node names from any language. ES5's `\w` regexp matches
-	// only latin characters, and the unicode \p{L} is not yet supported. So
-	// instead, we exclude reserved characters and match everything else.
-	var _wordChar = '[^' + _RESERVED_CHARS_RE + ']';
-	var _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']';
+		Loader.call( this, manager );
 
-	// Parent directories, delimited by '/' or ':'. Currently unused, but must
-	// be matched to parse the rest of the track name.
-	var _directoryRe = /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar );
+	}
 
-	// Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'.
-	var _nodeRe = /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot );
+	ObjectLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-	// Object on target node, and accessor. May not contain reserved
-	// characters. Accessor may contain any character except closing bracket.
-	var _objectRe = /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar );
+		constructor: ObjectLoader,
 
-	// Property and accessor. May not contain reserved characters. Accessor may
-	// contain any non-bracket characters.
-	var _propertyRe = /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar );
+		load: function ( url, onLoad, onProgress, onError ) {
 
-	var _trackRe = new RegExp( ''
-		+ '^'
-		+ _directoryRe
-		+ _nodeRe
-		+ _objectRe
-		+ _propertyRe
-		+ '$'
-	);
+			var scope = this;
 
-	var _supportedObjectNames = [ 'material', 'materials', 'bones' ];
+			var path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
+			this.resourcePath = this.resourcePath || path;
 
-	function Composite( targetGroup, path, optionalParsedPath ) {
+			var loader = new FileLoader( scope.manager );
+			loader.setPath( this.path );
+			loader.load( url, function ( text ) {
 
-		var parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
+				var json = null;
 
-		this._targetGroup = targetGroup;
-		this._bindings = targetGroup.subscribe_( path, parsedPath );
+				try {
 
-	}
+					json = JSON.parse( text );
 
-	Object.assign( Composite.prototype, {
+				} catch ( error ) {
 
-		getValue: function ( array, offset ) {
+					if ( onError !== undefined ) { onError( error ); }
 
-			this.bind(); // bind all binding
+					console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
 
-			var firstValidIndex = this._targetGroup.nCachedObjects_,
-				binding = this._bindings[ firstValidIndex ];
+					return;
 
-			// and only call .getValue on the first
-			if ( binding !== undefined ) { binding.getValue( array, offset ); }
+				}
 
-		},
+				var metadata = json.metadata;
 
-		setValue: function ( array, offset ) {
+				if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
 
-			var bindings = this._bindings;
+					console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
+					return;
 
-			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
+				}
 
-				bindings[ i ].setValue( array, offset );
+				scope.parse( json, onLoad );
 
-			}
+			}, onProgress, onError );
 
 		},
 
-		bind: function () {
-
-			var bindings = this._bindings;
+		parse: function ( json, onLoad ) {
 
-			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
+			var shapes = this.parseShape( json.shapes );
+			var geometries = this.parseGeometries( json.geometries, shapes );
 
-				bindings[ i ].bind();
+			var images = this.parseImages( json.images, function () {
 
-			}
+				if ( onLoad !== undefined ) { onLoad( object ); }
 
-		},
+			} );
 
-		unbind: function () {
+			var textures = this.parseTextures( json.textures, images );
+			var materials = this.parseMaterials( json.materials, textures );
 
-			var bindings = this._bindings;
+			var object = this.parseObject( json.object, geometries, materials );
 
-			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
+			if ( json.animations ) {
 
-				bindings[ i ].unbind();
+				object.animations = this.parseAnimations( json.animations );
 
 			}
 
-		}
-
-	} );
-
-
-	function PropertyBinding( rootNode, path, parsedPath ) {
+			if ( json.images === undefined || json.images.length === 0 ) {
 
-		this.path = path;
-		this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
+				if ( onLoad !== undefined ) { onLoad( object ); }
 
-		this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode;
+			}
 
-		this.rootNode = rootNode;
+			return object;
 
-	}
+		},
 
-	Object.assign( PropertyBinding, {
+		parseShape: function ( json ) {
 
-		Composite: Composite,
+			var shapes = {};
 
-		create: function ( root, path, parsedPath ) {
+			if ( json !== undefined ) {
 
-			if ( ! ( root && root.isAnimationObjectGroup ) ) {
+				for ( var i = 0, l = json.length; i < l; i ++ ) {
 
-				return new PropertyBinding( root, path, parsedPath );
+					var shape = new Shape().fromJSON( json[ i ] );
 
-			} else {
+					shapes[ shape.uuid ] = shape;
 
-				return new PropertyBinding.Composite( root, path, parsedPath );
+				}
 
 			}
 
+			return shapes;
+
 		},
 
-		/**
-		 * Replaces spaces with underscores and removes unsupported characters from
-		 * node names, to ensure compatibility with parseTrackName().
-		 *
-		 * @param {string} name Node name to be sanitized.
-		 * @return {string}
-		 */
-		sanitizeNodeName: function ( name ) {
+		parseGeometries: function ( json, shapes ) {
 
-			return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
+			var geometries = {};
 
-		},
+			if ( json !== undefined ) {
 
-		parseTrackName: function ( trackName ) {
+				var bufferGeometryLoader = new BufferGeometryLoader();
 
-			var matches = _trackRe.exec( trackName );
+				for ( var i = 0, l = json.length; i < l; i ++ ) {
 
-			if ( ! matches ) {
+					var geometry;
+					var data = json[ i ];
 
-				throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName );
+					switch ( data.type ) {
 
-			}
+						case 'PlaneGeometry':
+						case 'PlaneBufferGeometry':
 
-			var results = {
-				// directoryName: matches[ 1 ], // (tschw) currently unused
-				nodeName: matches[ 2 ],
-				objectName: matches[ 3 ],
-				objectIndex: matches[ 4 ],
-				propertyName: matches[ 5 ], // required
-				propertyIndex: matches[ 6 ]
-			};
+							geometry = new Geometries[ data.type ](
+								data.width,
+								data.height,
+								data.widthSegments,
+								data.heightSegments
+							);
 
-			var lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' );
+							break;
 
-			if ( lastDot !== undefined && lastDot !== - 1 ) {
+						case 'BoxGeometry':
+						case 'BoxBufferGeometry':
+						case 'CubeGeometry': // backwards compatible
 
-				var objectName = results.nodeName.substring( lastDot + 1 );
+							geometry = new Geometries[ data.type ](
+								data.width,
+								data.height,
+								data.depth,
+								data.widthSegments,
+								data.heightSegments,
+								data.depthSegments
+							);
 
-				// Object names must be checked against a whitelist. Otherwise, there
-				// is no way to parse 'foo.bar.baz': 'baz' must be a property, but
-				// 'bar' could be the objectName, or part of a nodeName (which can
-				// include '.' characters).
-				if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) {
+							break;
 
-					results.nodeName = results.nodeName.substring( 0, lastDot );
-					results.objectName = objectName;
+						case 'CircleGeometry':
+						case 'CircleBufferGeometry':
 
-				}
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.segments,
+								data.thetaStart,
+								data.thetaLength
+							);
 
-			}
+							break;
 
-			if ( results.propertyName === null || results.propertyName.length === 0 ) {
+						case 'CylinderGeometry':
+						case 'CylinderBufferGeometry':
 
-				throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName );
+							geometry = new Geometries[ data.type ](
+								data.radiusTop,
+								data.radiusBottom,
+								data.height,
+								data.radialSegments,
+								data.heightSegments,
+								data.openEnded,
+								data.thetaStart,
+								data.thetaLength
+							);
 
-			}
+							break;
 
-			return results;
+						case 'ConeGeometry':
+						case 'ConeBufferGeometry':
 
-		},
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.height,
+								data.radialSegments,
+								data.heightSegments,
+								data.openEnded,
+								data.thetaStart,
+								data.thetaLength
+							);
 
-		findNode: function ( root, nodeName ) {
+							break;
 
-			if ( ! nodeName || nodeName === "" || nodeName === "." || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
+						case 'SphereGeometry':
+						case 'SphereBufferGeometry':
 
-				return root;
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.widthSegments,
+								data.heightSegments,
+								data.phiStart,
+								data.phiLength,
+								data.thetaStart,
+								data.thetaLength
+							);
 
-			}
+							break;
 
-			// search into skeleton bones.
-			if ( root.skeleton ) {
+						case 'DodecahedronGeometry':
+						case 'DodecahedronBufferGeometry':
+						case 'IcosahedronGeometry':
+						case 'IcosahedronBufferGeometry':
+						case 'OctahedronGeometry':
+						case 'OctahedronBufferGeometry':
+						case 'TetrahedronGeometry':
+						case 'TetrahedronBufferGeometry':
 
-				var bone = root.skeleton.getBoneByName( nodeName );
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.detail
+							);
 
-				if ( bone !== undefined ) {
+							break;
 
-					return bone;
+						case 'RingGeometry':
+						case 'RingBufferGeometry':
 
-				}
+							geometry = new Geometries[ data.type ](
+								data.innerRadius,
+								data.outerRadius,
+								data.thetaSegments,
+								data.phiSegments,
+								data.thetaStart,
+								data.thetaLength
+							);
 
-			}
+							break;
 
-			// search into node subtree.
-			if ( root.children ) {
+						case 'TorusGeometry':
+						case 'TorusBufferGeometry':
 
-				var searchNodeSubtree = function ( children ) {
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.tube,
+								data.radialSegments,
+								data.tubularSegments,
+								data.arc
+							);
 
-					for ( var i = 0; i < children.length; i ++ ) {
+							break;
 
-						var childNode = children[ i ];
+						case 'TorusKnotGeometry':
+						case 'TorusKnotBufferGeometry':
 
-						if ( childNode.name === nodeName || childNode.uuid === nodeName ) {
+							geometry = new Geometries[ data.type ](
+								data.radius,
+								data.tube,
+								data.tubularSegments,
+								data.radialSegments,
+								data.p,
+								data.q
+							);
 
-							return childNode;
+							break;
 
-						}
+						case 'TubeGeometry':
+						case 'TubeBufferGeometry':
 
-						var result = searchNodeSubtree( childNode.children );
+							// This only works for built-in curves (e.g. CatmullRomCurve3).
+							// User defined curves or instances of CurvePath will not be deserialized.
+							geometry = new Geometries[ data.type ](
+								new Curves[ data.path.type ]().fromJSON( data.path ),
+								data.tubularSegments,
+								data.radius,
+								data.radialSegments,
+								data.closed
+							);
 
-						if ( result ) { return result; }
+							break;
 
-					}
+						case 'LatheGeometry':
+						case 'LatheBufferGeometry':
 
-					return null;
+							geometry = new Geometries[ data.type ](
+								data.points,
+								data.segments,
+								data.phiStart,
+								data.phiLength
+							);
 
-				};
+							break;
 
-				var subTreeNode = searchNodeSubtree( root.children );
+						case 'PolyhedronGeometry':
+						case 'PolyhedronBufferGeometry':
 
-				if ( subTreeNode ) {
+							geometry = new Geometries[ data.type ](
+								data.vertices,
+								data.indices,
+								data.radius,
+								data.details
+							);
 
-					return subTreeNode;
+							break;
 
-				}
+						case 'ShapeGeometry':
+						case 'ShapeBufferGeometry':
 
-			}
+							var geometryShapes = [];
 
-			return null;
+							for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {
 
-		}
+								var shape = shapes[ data.shapes[ j ] ];
 
-	} );
+								geometryShapes.push( shape );
 
-	Object.assign( PropertyBinding.prototype, { // prototype, continued
+							}
 
-		// these are used to "bind" a nonexistent property
-		_getValue_unavailable: function () {},
-		_setValue_unavailable: function () {},
+							geometry = new Geometries[ data.type ](
+								geometryShapes,
+								data.curveSegments
+							);
 
-		BindingType: {
-			Direct: 0,
-			EntireArray: 1,
-			ArrayElement: 2,
-			HasFromToArray: 3
-		},
+							break;
 
-		Versioning: {
-			None: 0,
-			NeedsUpdate: 1,
-			MatrixWorldNeedsUpdate: 2
-		},
 
-		GetterByBindingType: [
+						case 'ExtrudeGeometry':
+						case 'ExtrudeBufferGeometry':
 
-			function getValue_direct( buffer, offset ) {
+							var geometryShapes = [];
 
-				buffer[ offset ] = this.node[ this.propertyName ];
+							for ( var j = 0, jl = data.shapes.length; j < jl; j ++ ) {
 
-			},
+								var shape = shapes[ data.shapes[ j ] ];
 
-			function getValue_array( buffer, offset ) {
+								geometryShapes.push( shape );
 
-				var source = this.resolvedProperty;
+							}
 
-				for ( var i = 0, n = source.length; i !== n; ++ i ) {
+							var extrudePath = data.options.extrudePath;
 
-					buffer[ offset ++ ] = source[ i ];
+							if ( extrudePath !== undefined ) {
 
-				}
+								data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );
 
-			},
+							}
 
-			function getValue_arrayElement( buffer, offset ) {
+							geometry = new Geometries[ data.type ](
+								geometryShapes,
+								data.options
+							);
 
-				buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
+							break;
 
-			},
+						case 'BufferGeometry':
+						case 'InstancedBufferGeometry':
 
-			function getValue_toArray( buffer, offset ) {
+							geometry = bufferGeometryLoader.parse( data );
 
-				this.resolvedProperty.toArray( buffer, offset );
+							break;
 
-			}
+						case 'Geometry':
 
-		],
+							console.error( 'THREE.ObjectLoader: Loading "Geometry" is not supported anymore.' );
 
-		SetterByBindingTypeAndVersioning: [
+							break;
 
-			[
-				// Direct
+						default:
 
-				function setValue_direct( buffer, offset ) {
+							console.warn( 'THREE.ObjectLoader: Unsupported geometry type "' + data.type + '"' );
 
-					this.targetObject[ this.propertyName ] = buffer[ offset ];
+							continue;
 
-				},
+					}
 
-				function setValue_direct_setNeedsUpdate( buffer, offset ) {
+					geometry.uuid = data.uuid;
 
-					this.targetObject[ this.propertyName ] = buffer[ offset ];
-					this.targetObject.needsUpdate = true;
+					if ( data.name !== undefined ) { geometry.name = data.name; }
+					if ( geometry.isBufferGeometry === true && data.userData !== undefined ) { geometry.userData = data.userData; }
 
-				},
+					geometries[ data.uuid ] = geometry;
 
-				function setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
+				}
 
-					this.targetObject[ this.propertyName ] = buffer[ offset ];
-					this.targetObject.matrixWorldNeedsUpdate = true;
+			}
 
-				}
+			return geometries;
 
-			], [
+		},
 
-				// EntireArray
+		parseMaterials: function ( json, textures ) {
 
-				function setValue_array( buffer, offset ) {
+			var cache = {}; // MultiMaterial
+			var materials = {};
 
-					var dest = this.resolvedProperty;
+			if ( json !== undefined ) {
 
-					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
+				var loader = new MaterialLoader();
+				loader.setTextures( textures );
 
-						dest[ i ] = buffer[ offset ++ ];
+				for ( var i = 0, l = json.length; i < l; i ++ ) {
 
-					}
+					var data = json[ i ];
 
-				},
+					if ( data.type === 'MultiMaterial' ) {
 
-				function setValue_array_setNeedsUpdate( buffer, offset ) {
+						// Deprecated
 
-					var dest = this.resolvedProperty;
+						var array = [];
 
-					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
+						for ( var j = 0; j < data.materials.length; j ++ ) {
 
-						dest[ i ] = buffer[ offset ++ ];
+							var material = data.materials[ j ];
 
-					}
+							if ( cache[ material.uuid ] === undefined ) {
 
-					this.targetObject.needsUpdate = true;
+								cache[ material.uuid ] = loader.parse( material );
 
-				},
+							}
 
-				function setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
+							array.push( cache[ material.uuid ] );
 
-					var dest = this.resolvedProperty;
+						}
 
-					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
+						materials[ data.uuid ] = array;
 
-						dest[ i ] = buffer[ offset ++ ];
+					} else {
 
-					}
+						if ( cache[ data.uuid ] === undefined ) {
 
-					this.targetObject.matrixWorldNeedsUpdate = true;
+							cache[ data.uuid ] = loader.parse( data );
+
+						}
+
+						materials[ data.uuid ] = cache[ data.uuid ];
+
+					}
 
 				}
 
-			], [
+			}
 
-				// ArrayElement
+			return materials;
 
-				function setValue_arrayElement( buffer, offset ) {
+		},
 
-					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+		parseAnimations: function ( json ) {
 
-				},
+			var animations = [];
 
-				function setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
+			for ( var i = 0; i < json.length; i ++ ) {
 
-					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
-					this.targetObject.needsUpdate = true;
+				var data = json[ i ];
 
-				},
+				var clip = AnimationClip.parse( data );
 
-				function setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
+				if ( data.uuid !== undefined ) { clip.uuid = data.uuid; }
 
-					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
-					this.targetObject.matrixWorldNeedsUpdate = true;
+				animations.push( clip );
 
-				}
+			}
 
-			], [
+			return animations;
 
-				// HasToFromArray
+		},
 
-				function setValue_fromArray( buffer, offset ) {
+		parseImages: function ( json, onLoad ) {
 
-					this.resolvedProperty.fromArray( buffer, offset );
+			var scope = this;
+			var images = {};
 
-				},
+			function loadImage( url ) {
 
-				function setValue_fromArray_setNeedsUpdate( buffer, offset ) {
+				scope.manager.itemStart( url );
 
-					this.resolvedProperty.fromArray( buffer, offset );
-					this.targetObject.needsUpdate = true;
+				return loader.load( url, function () {
 
-				},
+					scope.manager.itemEnd( url );
 
-				function setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
+				}, undefined, function () {
 
-					this.resolvedProperty.fromArray( buffer, offset );
-					this.targetObject.matrixWorldNeedsUpdate = true;
+					scope.manager.itemError( url );
+					scope.manager.itemEnd( url );
 
-				}
+				} );
 
-			]
+			}
 
-		],
+			if ( json !== undefined && json.length > 0 ) {
 
-		getValue: function getValue_unbound( targetArray, offset ) {
+				var manager = new LoadingManager( onLoad );
 
-			this.bind();
-			this.getValue( targetArray, offset );
+				var loader = new ImageLoader( manager );
+				loader.setCrossOrigin( this.crossOrigin );
 
-			// Note: This class uses a State pattern on a per-method basis:
-			// 'bind' sets 'this.getValue' / 'setValue' and shadows the
-			// prototype version of these methods with one that represents
-			// the bound state. When the property is not found, the methods
-			// become no-ops.
+				for ( var i = 0, il = json.length; i < il; i ++ ) {
 
-		},
+					var image = json[ i ];
+					var url = image.url;
 
-		setValue: function getValue_unbound( sourceArray, offset ) {
+					if ( Array.isArray( url ) ) {
 
-			this.bind();
-			this.setValue( sourceArray, offset );
+						// load array of images e.g CubeTexture
 
-		},
+						images[ image.uuid ] = [];
 
-		// create getter / setter pair for a property in the scene graph
-		bind: function () {
+						for ( var j = 0, jl = url.length; j < jl; j ++ ) {
 
-			var targetObject = this.node,
-				parsedPath = this.parsedPath,
+							var currentUrl = url[ j ];
 
-				objectName = parsedPath.objectName,
-				propertyName = parsedPath.propertyName,
-				propertyIndex = parsedPath.propertyIndex;
+							var path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( currentUrl ) ? currentUrl : scope.resourcePath + currentUrl;
 
-			if ( ! targetObject ) {
+							images[ image.uuid ].push( loadImage( path ) );
 
-				targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName ) || this.rootNode;
+						}
 
-				this.node = targetObject;
+					} else {
 
-			}
+						// load single image
 
-			// set fail state so we can just 'return' on error
-			this.getValue = this._getValue_unavailable;
-			this.setValue = this._setValue_unavailable;
+						var path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( image.url ) ? image.url : scope.resourcePath + image.url;
 
-			// ensure there is a value node
-			if ( ! targetObject ) {
+						images[ image.uuid ] = loadImage( path );
 
-				console.error( 'THREE.PropertyBinding: Trying to update node for track: ' + this.path + ' but it wasn\'t found.' );
-				return;
+					}
+
+				}
 
 			}
 
-			if ( objectName ) {
+			return images;
 
-				var objectIndex = parsedPath.objectIndex;
+		},
 
-				// special cases were we need to reach deeper into the hierarchy to get the face materials....
-				switch ( objectName ) {
+		parseTextures: function ( json, images ) {
 
-					case 'materials':
+			function parseConstant( value, type ) {
 
-						if ( ! targetObject.material ) {
+				if ( typeof value === 'number' ) { return value; }
 
-							console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
-							return;
+				console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
 
-						}
+				return type[ value ];
 
-						if ( ! targetObject.material.materials ) {
+			}
 
-							console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this );
-							return;
+			var textures = {};
 
-						}
+			if ( json !== undefined ) {
 
-						targetObject = targetObject.material.materials;
+				for ( var i = 0, l = json.length; i < l; i ++ ) {
 
-						break;
+					var data = json[ i ];
 
-					case 'bones':
+					if ( data.image === undefined ) {
 
-						if ( ! targetObject.skeleton ) {
+						console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
 
-							console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this );
-							return;
+					}
 
-						}
+					if ( images[ data.image ] === undefined ) {
 
-						// potential future optimization: skip this if propertyIndex is already an integer
-						// and convert the integer string to a true integer.
+						console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
 
-						targetObject = targetObject.skeleton.bones;
+					}
 
-						// support resolving morphTarget names into indices.
-						for ( var i = 0; i < targetObject.length; i ++ ) {
+					var texture;
 
-							if ( targetObject[ i ].name === objectIndex ) {
+					if ( Array.isArray( images[ data.image ] ) ) {
 
-								objectIndex = i;
-								break;
+						texture = new CubeTexture( images[ data.image ] );
 
-							}
+					} else {
 
-						}
+						texture = new Texture( images[ data.image ] );
 
-						break;
+					}
 
-					default:
+					texture.needsUpdate = true;
 
-						if ( targetObject[ objectName ] === undefined ) {
+					texture.uuid = data.uuid;
 
-							console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this );
-							return;
+					if ( data.name !== undefined ) { texture.name = data.name; }
 
-						}
+					if ( data.mapping !== undefined ) { texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING ); }
 
-						targetObject = targetObject[ objectName ];
+					if ( data.offset !== undefined ) { texture.offset.fromArray( data.offset ); }
+					if ( data.repeat !== undefined ) { texture.repeat.fromArray( data.repeat ); }
+					if ( data.center !== undefined ) { texture.center.fromArray( data.center ); }
+					if ( data.rotation !== undefined ) { texture.rotation = data.rotation; }
 
-				}
+					if ( data.wrap !== undefined ) {
 
+						texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
+						texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
 
-				if ( objectIndex !== undefined ) {
+					}
 
-					if ( targetObject[ objectIndex ] === undefined ) {
+					if ( data.format !== undefined ) { texture.format = data.format; }
+					if ( data.type !== undefined ) { texture.type = data.type; }
+					if ( data.encoding !== undefined ) { texture.encoding = data.encoding; }
 
-						console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject );
-						return;
+					if ( data.minFilter !== undefined ) { texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER ); }
+					if ( data.magFilter !== undefined ) { texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER ); }
+					if ( data.anisotropy !== undefined ) { texture.anisotropy = data.anisotropy; }
 
-					}
+					if ( data.flipY !== undefined ) { texture.flipY = data.flipY; }
 
-					targetObject = targetObject[ objectIndex ];
+					if ( data.premultiplyAlpha !== undefined ) { texture.premultiplyAlpha = data.premultiplyAlpha; }
+					if ( data.unpackAlignment !== undefined ) { texture.unpackAlignment = data.unpackAlignment; }
+
+					textures[ data.uuid ] = texture;
 
 				}
 
 			}
 
-			// resolve property
-			var nodeProperty = targetObject[ propertyName ];
+			return textures;
 
-			if ( nodeProperty === undefined ) {
+		},
 
-				var nodeName = parsedPath.nodeName;
+		parseObject: function ( data, geometries, materials ) {
 
-				console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName +
-					'.' + propertyName + ' but it wasn\'t found.', targetObject );
-				return;
+			var object;
 
-			}
+			function getGeometry( name ) {
 
-			// determine versioning scheme
-			var versioning = this.Versioning.None;
+				if ( geometries[ name ] === undefined ) {
 
-			this.targetObject = targetObject;
+					console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
 
-			if ( targetObject.needsUpdate !== undefined ) { // material
+				}
 
-				versioning = this.Versioning.NeedsUpdate;
+				return geometries[ name ];
 
-			} else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform
+			}
 
-				versioning = this.Versioning.MatrixWorldNeedsUpdate;
+			function getMaterial( name ) {
 
-			}
+				if ( name === undefined ) { return undefined; }
 
-			// determine how the property gets bound
-			var bindingType = this.BindingType.Direct;
+				if ( Array.isArray( name ) ) {
 
-			if ( propertyIndex !== undefined ) {
+					var array = [];
 
-				// access a sub element of the property array (only primitives are supported right now)
+					for ( var i = 0, l = name.length; i < l; i ++ ) {
 
-				if ( propertyName === "morphTargetInfluences" ) {
+						var uuid = name[ i ];
 
-					// potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer.
+						if ( materials[ uuid ] === undefined ) {
 
-					// support resolving morphTarget names into indices.
-					if ( ! targetObject.geometry ) {
+							console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
 
-						console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this );
-						return;
+						}
+
+						array.push( materials[ uuid ] );
 
 					}
 
-					if ( targetObject.geometry.isBufferGeometry ) {
+					return array;
 
-						if ( ! targetObject.geometry.morphAttributes ) {
+				}
 
-							console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this );
-							return;
+				if ( materials[ name ] === undefined ) {
 
-						}
+					console.warn( 'THREE.ObjectLoader: Undefined material', name );
 
-						for ( var i = 0; i < this.node.geometry.morphAttributes.position.length; i ++ ) {
+				}
 
-							if ( targetObject.geometry.morphAttributes.position[ i ].name === propertyIndex ) {
+				return materials[ name ];
 
-								propertyIndex = i;
-								break;
+			}
 
-							}
+			switch ( data.type ) {
 
-						}
+				case 'Scene':
 
+					object = new Scene();
 
-					} else {
+					if ( data.background !== undefined ) {
 
-						if ( ! targetObject.geometry.morphTargets ) {
+						if ( Number.isInteger( data.background ) ) {
 
-							console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphTargets.', this );
-							return;
+							object.background = new Color( data.background );
 
 						}
 
-						for ( var i = 0; i < this.node.geometry.morphTargets.length; i ++ ) {
+					}
 
-							if ( targetObject.geometry.morphTargets[ i ].name === propertyIndex ) {
+					if ( data.fog !== undefined ) {
 
-								propertyIndex = i;
-								break;
+						if ( data.fog.type === 'Fog' ) {
 
-							}
+							object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
+
+						} else if ( data.fog.type === 'FogExp2' ) {
+
+							object.fog = new FogExp2( data.fog.color, data.fog.density );
 
 						}
 
 					}
 
-				}
+					break;
 
-				bindingType = this.BindingType.ArrayElement;
+				case 'PerspectiveCamera':
 
-				this.resolvedProperty = nodeProperty;
-				this.propertyIndex = propertyIndex;
+					object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
 
-			} else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) {
+					if ( data.focus !== undefined ) { object.focus = data.focus; }
+					if ( data.zoom !== undefined ) { object.zoom = data.zoom; }
+					if ( data.filmGauge !== undefined ) { object.filmGauge = data.filmGauge; }
+					if ( data.filmOffset !== undefined ) { object.filmOffset = data.filmOffset; }
+					if ( data.view !== undefined ) { object.view = Object.assign( {}, data.view ); }
 
-				// must use copy for Object3D.Euler/Quaternion
+					break;
 
-				bindingType = this.BindingType.HasFromToArray;
+				case 'OrthographicCamera':
 
-				this.resolvedProperty = nodeProperty;
+					object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
 
-			} else if ( Array.isArray( nodeProperty ) ) {
+					if ( data.zoom !== undefined ) { object.zoom = data.zoom; }
+					if ( data.view !== undefined ) { object.view = Object.assign( {}, data.view ); }
 
-				bindingType = this.BindingType.EntireArray;
+					break;
 
-				this.resolvedProperty = nodeProperty;
+				case 'AmbientLight':
 
-			} else {
+					object = new AmbientLight( data.color, data.intensity );
 
-				this.propertyName = propertyName;
+					break;
 
-			}
+				case 'DirectionalLight':
 
-			// select getter / setter
-			this.getValue = this.GetterByBindingType[ bindingType ];
-			this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
+					object = new DirectionalLight( data.color, data.intensity );
 
-		},
+					break;
 
-		unbind: function () {
+				case 'PointLight':
 
-			this.node = null;
+					object = new PointLight( data.color, data.intensity, data.distance, data.decay );
 
-			// back to the prototype version of getValue / setValue
-			// note: avoiding to mutate the shape of 'this' via 'delete'
-			this.getValue = this._getValue_unbound;
-			this.setValue = this._setValue_unbound;
+					break;
 
-		}
+				case 'RectAreaLight':
 
-	} );
+					object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
 
-	// DECLARE ALIAS AFTER assign prototype
-	Object.assign( PropertyBinding.prototype, {
+					break;
 
-		// initial state of these methods that calls 'bind'
-		_getValue_unbound: PropertyBinding.prototype.getValue,
-		_setValue_unbound: PropertyBinding.prototype.setValue,
+				case 'SpotLight':
 
-	} );
+					object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
 
-	/**
-	 *
-	 * A group of objects that receives a shared animation state.
-	 *
-	 * Usage:
-	 *
-	 *  - Add objects you would otherwise pass as 'root' to the
-	 *    constructor or the .clipAction method of AnimationMixer.
-	 *
-	 *  - Instead pass this object as 'root'.
-	 *
-	 *  - You can also add and remove objects later when the mixer
-	 *    is running.
-	 *
-	 * Note:
-	 *
-	 *    Objects of this class appear as one object to the mixer,
-	 *    so cache control of the individual objects must be done
-	 *    on the group.
-	 *
-	 * Limitation:
-	 *
-	 *  - The animated properties must be compatible among the
-	 *    all objects in the group.
-	 *
-	 *  - A single property can either be controlled through a
-	 *    target group or directly, but not both.
-	 *
-	 * @author tschw
-	 */
+					break;
 
-	function AnimationObjectGroup() {
+				case 'HemisphereLight':
 
-		this.uuid = MathUtils.generateUUID();
+					object = new HemisphereLight( data.color, data.groundColor, data.intensity );
 
-		// cached objects followed by the active ones
-		this._objects = Array.prototype.slice.call( arguments );
+					break;
 
-		this.nCachedObjects_ = 0; // threshold
-		// note: read by PropertyBinding.Composite
+				case 'LightProbe':
 
-		var indices = {};
-		this._indicesByUUID = indices; // for bookkeeping
+					object = new LightProbe().fromJSON( data );
 
-		for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
+					break;
 
-			indices[ arguments[ i ].uuid ] = i;
+				case 'SkinnedMesh':
 
-		}
+					console.warn( 'THREE.ObjectLoader.parseObject() does not support SkinnedMesh yet.' );
 
-		this._paths = []; // inside: string
-		this._parsedPaths = []; // inside: { we don't care, here }
-		this._bindings = []; // inside: Array< PropertyBinding >
-		this._bindingsIndicesByPath = {}; // inside: indices in these arrays
+				case 'Mesh':
 
-		var scope = this;
-
-		this.stats = {
-
-			objects: {
-				get total() {
+					var geometry = getGeometry( data.geometry );
+					var material = getMaterial( data.material );
 
-					return scope._objects.length;
+					object = new Mesh( geometry, material );
 
-				},
-				get inUse() {
+					break;
 
-					return this.total - scope.nCachedObjects_;
+				case 'InstancedMesh':
 
-				}
-			},
-			get bindingsPerObject() {
+					var geometry = getGeometry( data.geometry );
+					var material = getMaterial( data.material );
+					var count = data.count;
+					var instanceMatrix = data.instanceMatrix;
 
-				return scope._bindings.length;
+					object = new InstancedMesh( geometry, material, count );
+					object.instanceMatrix = new BufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
 
-			}
+					break;
 
-		};
+				case 'LOD':
 
-	}
+					object = new LOD();
 
-	Object.assign( AnimationObjectGroup.prototype, {
+					break;
 
-		isAnimationObjectGroup: true,
+				case 'Line':
 
-		add: function () {
+					object = new Line( getGeometry( data.geometry ), getMaterial( data.material ), data.mode );
 
-			var objects = this._objects,
-				nObjects = objects.length,
-				nCachedObjects = this.nCachedObjects_,
-				indicesByUUID = this._indicesByUUID,
-				paths = this._paths,
-				parsedPaths = this._parsedPaths,
-				bindings = this._bindings,
-				nBindings = bindings.length,
-				knownObject = undefined;
+					break;
 
-			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
+				case 'LineLoop':
 
-				var object = arguments[ i ],
-					uuid = object.uuid,
-					index = indicesByUUID[ uuid ];
+					object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
 
-				if ( index === undefined ) {
+					break;
 
-					// unknown object -> add it to the ACTIVE region
+				case 'LineSegments':
 
-					index = nObjects ++;
-					indicesByUUID[ uuid ] = index;
-					objects.push( object );
+					object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
 
-					// accounting is done, now do the same for all bindings
+					break;
 
-					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
+				case 'PointCloud':
+				case 'Points':
 
-						bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
+					object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
 
-					}
+					break;
 
-				} else if ( index < nCachedObjects ) {
+				case 'Sprite':
 
-					knownObject = objects[ index ];
+					object = new Sprite( getMaterial( data.material ) );
 
-					// move existing object to the ACTIVE region
+					break;
 
-					var firstActiveIndex = -- nCachedObjects,
-						lastCachedObject = objects[ firstActiveIndex ];
+				case 'Group':
 
-					indicesByUUID[ lastCachedObject.uuid ] = index;
-					objects[ index ] = lastCachedObject;
+					object = new Group();
 
-					indicesByUUID[ uuid ] = firstActiveIndex;
-					objects[ firstActiveIndex ] = object;
+					break;
 
-					// accounting is done, now do the same for all bindings
+				default:
 
-					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
+					object = new Object3D();
 
-						var bindingsForPath = bindings[ j ],
-							lastCached = bindingsForPath[ firstActiveIndex ],
-							binding = bindingsForPath[ index ];
+			}
 
-						bindingsForPath[ index ] = lastCached;
+			object.uuid = data.uuid;
 
-						if ( binding === undefined ) {
+			if ( data.name !== undefined ) { object.name = data.name; }
 
-							// since we do not bother to create new bindings
-							// for objects that are cached, the binding may
-							// or may not exist
+			if ( data.matrix !== undefined ) {
 
-							binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
+				object.matrix.fromArray( data.matrix );
 
-						}
+				if ( data.matrixAutoUpdate !== undefined ) { object.matrixAutoUpdate = data.matrixAutoUpdate; }
+				if ( object.matrixAutoUpdate ) { object.matrix.decompose( object.position, object.quaternion, object.scale ); }
 
-						bindingsForPath[ firstActiveIndex ] = binding;
+			} else {
 
-					}
+				if ( data.position !== undefined ) { object.position.fromArray( data.position ); }
+				if ( data.rotation !== undefined ) { object.rotation.fromArray( data.rotation ); }
+				if ( data.quaternion !== undefined ) { object.quaternion.fromArray( data.quaternion ); }
+				if ( data.scale !== undefined ) { object.scale.fromArray( data.scale ); }
 
-				} else if ( objects[ index ] !== knownObject ) {
+			}
 
-					console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
-						'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
+			if ( data.castShadow !== undefined ) { object.castShadow = data.castShadow; }
+			if ( data.receiveShadow !== undefined ) { object.receiveShadow = data.receiveShadow; }
 
-				} // else the object is already where we want it to be
+			if ( data.shadow ) {
 
-			} // for arguments
+				if ( data.shadow.bias !== undefined ) { object.shadow.bias = data.shadow.bias; }
+				if ( data.shadow.radius !== undefined ) { object.shadow.radius = data.shadow.radius; }
+				if ( data.shadow.mapSize !== undefined ) { object.shadow.mapSize.fromArray( data.shadow.mapSize ); }
+				if ( data.shadow.camera !== undefined ) { object.shadow.camera = this.parseObject( data.shadow.camera ); }
 
-			this.nCachedObjects_ = nCachedObjects;
+			}
 
-		},
+			if ( data.visible !== undefined ) { object.visible = data.visible; }
+			if ( data.frustumCulled !== undefined ) { object.frustumCulled = data.frustumCulled; }
+			if ( data.renderOrder !== undefined ) { object.renderOrder = data.renderOrder; }
+			if ( data.userData !== undefined ) { object.userData = data.userData; }
+			if ( data.layers !== undefined ) { object.layers.mask = data.layers; }
 
-		remove: function () {
+			if ( data.children !== undefined ) {
 
-			var objects = this._objects,
-				nCachedObjects = this.nCachedObjects_,
-				indicesByUUID = this._indicesByUUID,
-				bindings = this._bindings,
-				nBindings = bindings.length;
+				var children = data.children;
 
-			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
+				for ( var i = 0; i < children.length; i ++ ) {
 
-				var object = arguments[ i ],
-					uuid = object.uuid,
-					index = indicesByUUID[ uuid ];
+					object.add( this.parseObject( children[ i ], geometries, materials ) );
 
-				if ( index !== undefined && index >= nCachedObjects ) {
+				}
 
-					// move existing object into the CACHED region
+			}
 
-					var lastCachedIndex = nCachedObjects ++,
-						firstActiveObject = objects[ lastCachedIndex ];
+			if ( data.type === 'LOD' ) {
 
-					indicesByUUID[ firstActiveObject.uuid ] = index;
-					objects[ index ] = firstActiveObject;
+				if ( data.autoUpdate !== undefined ) { object.autoUpdate = data.autoUpdate; }
 
-					indicesByUUID[ uuid ] = lastCachedIndex;
-					objects[ lastCachedIndex ] = object;
+				var levels = data.levels;
 
-					// accounting is done, now do the same for all bindings
+				for ( var l = 0; l < levels.length; l ++ ) {
 
-					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
+					var level = levels[ l ];
+					var child = object.getObjectByProperty( 'uuid', level.object );
 
-						var bindingsForPath = bindings[ j ],
-							firstActive = bindingsForPath[ lastCachedIndex ],
-							binding = bindingsForPath[ index ];
+					if ( child !== undefined ) {
 
-						bindingsForPath[ index ] = firstActive;
-						bindingsForPath[ lastCachedIndex ] = binding;
+						object.addLevel( child, level.distance );
 
 					}
 
 				}
 
-			} // for arguments
-
-			this.nCachedObjects_ = nCachedObjects;
+			}
 
-		},
+			return object;
 
-		// remove & forget
-		uncache: function () {
+		}
 
-			var objects = this._objects,
-				nObjects = objects.length,
-				nCachedObjects = this.nCachedObjects_,
-				indicesByUUID = this._indicesByUUID,
-				bindings = this._bindings,
-				nBindings = bindings.length;
+	} );
 
-			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
+	var TEXTURE_MAPPING = {
+		UVMapping: UVMapping,
+		CubeReflectionMapping: CubeReflectionMapping,
+		CubeRefractionMapping: CubeRefractionMapping,
+		EquirectangularReflectionMapping: EquirectangularReflectionMapping,
+		EquirectangularRefractionMapping: EquirectangularRefractionMapping,
+		SphericalReflectionMapping: SphericalReflectionMapping,
+		CubeUVReflectionMapping: CubeUVReflectionMapping,
+		CubeUVRefractionMapping: CubeUVRefractionMapping
+	};
 
-				var object = arguments[ i ],
-					uuid = object.uuid,
-					index = indicesByUUID[ uuid ];
+	var TEXTURE_WRAPPING = {
+		RepeatWrapping: RepeatWrapping,
+		ClampToEdgeWrapping: ClampToEdgeWrapping,
+		MirroredRepeatWrapping: MirroredRepeatWrapping
+	};
 
-				if ( index !== undefined ) {
+	var TEXTURE_FILTER = {
+		NearestFilter: NearestFilter,
+		NearestMipmapNearestFilter: NearestMipmapNearestFilter,
+		NearestMipmapLinearFilter: NearestMipmapLinearFilter,
+		LinearFilter: LinearFilter,
+		LinearMipmapNearestFilter: LinearMipmapNearestFilter,
+		LinearMipmapLinearFilter: LinearMipmapLinearFilter
+	};
 
-					delete indicesByUUID[ uuid ];
+	/**
+	 * @author thespite / http://clicktorelease.com/
+	 */
 
-					if ( index < nCachedObjects ) {
 
-						// object is cached, shrink the CACHED region
+	function ImageBitmapLoader( manager ) {
 
-						var firstActiveIndex = -- nCachedObjects,
-							lastCachedObject = objects[ firstActiveIndex ],
-							lastIndex = -- nObjects,
-							lastObject = objects[ lastIndex ];
+		if ( typeof createImageBitmap === 'undefined' ) {
 
-						// last cached object takes this object's place
-						indicesByUUID[ lastCachedObject.uuid ] = index;
-						objects[ index ] = lastCachedObject;
+			console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
 
-						// last object goes to the activated slot and pop
-						indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
-						objects[ firstActiveIndex ] = lastObject;
-						objects.pop();
+		}
 
-						// accounting is done, now do the same for all bindings
+		if ( typeof fetch === 'undefined' ) {
 
-						for ( var j = 0, m = nBindings; j !== m; ++ j ) {
+			console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
 
-							var bindingsForPath = bindings[ j ],
-								lastCached = bindingsForPath[ firstActiveIndex ],
-								last = bindingsForPath[ lastIndex ];
+		}
 
-							bindingsForPath[ index ] = lastCached;
-							bindingsForPath[ firstActiveIndex ] = last;
-							bindingsForPath.pop();
+		Loader.call( this, manager );
 
-						}
+		this.options = undefined;
 
-					} else {
+	}
 
-						// object is active, just swap with the last and pop
+	ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-						var lastIndex = -- nObjects,
-							lastObject = objects[ lastIndex ];
+		constructor: ImageBitmapLoader,
 
-						indicesByUUID[ lastObject.uuid ] = index;
-						objects[ index ] = lastObject;
-						objects.pop();
+		setOptions: function setOptions( options ) {
 
-						// accounting is done, now do the same for all bindings
+			this.options = options;
 
-						for ( var j = 0, m = nBindings; j !== m; ++ j ) {
+			return this;
 
-							var bindingsForPath = bindings[ j ];
+		},
 
-							bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
-							bindingsForPath.pop();
+		load: function ( url, onLoad, onProgress, onError ) {
 
-						}
+			if ( url === undefined ) { url = ''; }
 
-					} // cached or active
+			if ( this.path !== undefined ) { url = this.path + url; }
 
-				} // if object is known
+			url = this.manager.resolveURL( url );
 
-			} // for arguments
+			var scope = this;
 
-			this.nCachedObjects_ = nCachedObjects;
+			var cached = Cache.get( url );
 
-		},
+			if ( cached !== undefined ) {
 
-		// Internal interface used by befriended PropertyBinding.Composite:
+				scope.manager.itemStart( url );
 
-		subscribe_: function ( path, parsedPath ) {
+				setTimeout( function () {
 
-			// returns an array of bindings for the given path that is changed
-			// according to the contained objects in the group
+					if ( onLoad ) { onLoad( cached ); }
 
-			var indicesByPath = this._bindingsIndicesByPath,
-				index = indicesByPath[ path ],
-				bindings = this._bindings;
+					scope.manager.itemEnd( url );
 
-			if ( index !== undefined ) { return bindings[ index ]; }
+				}, 0 );
 
-			var paths = this._paths,
-				parsedPaths = this._parsedPaths,
-				objects = this._objects,
-				nObjects = objects.length,
-				nCachedObjects = this.nCachedObjects_,
-				bindingsForPath = new Array( nObjects );
+				return cached;
 
-			index = bindings.length;
+			}
 
-			indicesByPath[ path ] = index;
+			fetch( url ).then( function ( res ) {
 
-			paths.push( path );
-			parsedPaths.push( parsedPath );
-			bindings.push( bindingsForPath );
+				return res.blob();
 
-			for ( var i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
+			} ).then( function ( blob ) {
 
-				var object = objects[ i ];
-				bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
+				if ( scope.options === undefined ) {
 
-			}
+					// Workaround for FireFox. It causes an error if you pass options.
+					return createImageBitmap( blob );
 
-			return bindingsForPath;
+				} else {
 
-		},
+					return createImageBitmap( blob, scope.options );
 
-		unsubscribe_: function ( path ) {
+				}
 
-			// tells the group to forget about a property path and no longer
-			// update the array previously obtained with 'subscribe_'
+			} ).then( function ( imageBitmap ) {
 
-			var indicesByPath = this._bindingsIndicesByPath,
-				index = indicesByPath[ path ];
+				Cache.add( url, imageBitmap );
 
-			if ( index !== undefined ) {
+				if ( onLoad ) { onLoad( imageBitmap ); }
 
-				var paths = this._paths,
-					parsedPaths = this._parsedPaths,
-					bindings = this._bindings,
-					lastBindingsIndex = bindings.length - 1,
-					lastBindings = bindings[ lastBindingsIndex ],
-					lastBindingsPath = path[ lastBindingsIndex ];
+				scope.manager.itemEnd( url );
 
-				indicesByPath[ lastBindingsPath ] = index;
+			} ).catch( function ( e ) {
 
-				bindings[ index ] = lastBindings;
-				bindings.pop();
+				if ( onError ) { onError( e ); }
 
-				parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
-				parsedPaths.pop();
+				scope.manager.itemError( url );
+				scope.manager.itemEnd( url );
 
-				paths[ index ] = paths[ lastBindingsIndex ];
-				paths.pop();
+			} );
 
-			}
+			scope.manager.itemStart( url );
 
 		}
 
 	} );
 
 	/**
-	 *
-	 * Action provided by AnimationMixer for scheduling clip playback on specific
-	 * objects.
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
-	 *
-	 */
-
-	function AnimationAction( mixer, clip, localRoot, blendMode ) {
-
-		this._mixer = mixer;
-		this._clip = clip;
-		this._localRoot = localRoot || null;
-		this.blendMode = blendMode || clip.blendMode;
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * minimal class for proxing functions to Path. Replaces old "extractSubpaths()"
+	 **/
 
-		var tracks = clip.tracks,
-			nTracks = tracks.length,
-			interpolants = new Array( nTracks );
+	function ShapePath() {
 
-		var interpolantSettings = {
-			endingStart: ZeroCurvatureEnding,
-			endingEnd: ZeroCurvatureEnding
-		};
+		this.type = 'ShapePath';
 
-		for ( var i = 0; i !== nTracks; ++ i ) {
+		this.color = new Color();
 
-			var interpolant = tracks[ i ].createInterpolant( null );
-			interpolants[ i ] = interpolant;
-			interpolant.settings = interpolantSettings;
+		this.subPaths = [];
+		this.currentPath = null;
 
-		}
+	}
 
-		this._interpolantSettings = interpolantSettings;
+	Object.assign( ShapePath.prototype, {
 
-		this._interpolants = interpolants; // bound by the mixer
+		moveTo: function ( x, y ) {
 
-		// inside: PropertyMixer (managed by the mixer)
-		this._propertyBindings = new Array( nTracks );
+			this.currentPath = new Path();
+			this.subPaths.push( this.currentPath );
+			this.currentPath.moveTo( x, y );
 
-		this._cacheIndex = null; // for the memory manager
-		this._byClipCacheIndex = null; // for the memory manager
+			return this;
 
-		this._timeScaleInterpolant = null;
-		this._weightInterpolant = null;
+		},
 
-		this.loop = LoopRepeat;
-		this._loopCount = - 1;
+		lineTo: function ( x, y ) {
 
-		// global mixer time when the action is to be started
-		// it's set back to 'null' upon start of the action
-		this._startTime = null;
+			this.currentPath.lineTo( x, y );
 
-		// scaled local time of the action
-		// gets clamped or wrapped to 0..clip.duration according to loop
-		this.time = 0;
+			return this;
 
-		this.timeScale = 1;
-		this._effectiveTimeScale = 1;
+		},
 
-		this.weight = 1;
-		this._effectiveWeight = 1;
+		quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
 
-		this.repetitions = Infinity; // no. of repetitions when looping
+			this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY );
 
-		this.paused = false; // true -> zero effective time scale
-		this.enabled = true; // false -> zero effective weight
+			return this;
 
-		this.clampWhenFinished = false;// keep feeding the last frame?
+		},
 
-		this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate
-		this.zeroSlopeAtEnd = true;// clips for start, loop and end
+		bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
 
-	}
+			this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY );
 
-	Object.assign( AnimationAction.prototype, {
+			return this;
 
-		// State & Scheduling
+		},
 
-		play: function () {
+		splineThru: function ( pts ) {
 
-			this._mixer._activateAction( this );
+			this.currentPath.splineThru( pts );
 
 			return this;
 
 		},
 
-		stop: function () {
+		toShapes: function ( isCCW, noHoles ) {
 
-			this._mixer._deactivateAction( this );
+			function toShapesNoHoles( inSubpaths ) {
 
-			return this.reset();
+				var shapes = [];
 
-		},
+				for ( var i = 0, l = inSubpaths.length; i < l; i ++ ) {
 
-		reset: function () {
+					var tmpPath = inSubpaths[ i ];
 
-			this.paused = false;
-			this.enabled = true;
+					var tmpShape = new Shape();
+					tmpShape.curves = tmpPath.curves;
 
-			this.time = 0; // restart clip
-			this._loopCount = - 1;// forget previous loops
-			this._startTime = null;// forget scheduling
+					shapes.push( tmpShape );
 
-			return this.stopFading().stopWarping();
+				}
 
-		},
+				return shapes;
 
-		isRunning: function () {
+			}
 
-			return this.enabled && ! this.paused && this.timeScale !== 0 &&
-				this._startTime === null && this._mixer._isActiveAction( this );
+			function isPointInsidePolygon( inPt, inPolygon ) {
 
-		},
+				var polyLen = inPolygon.length;
 
-		// return true when play has been called
-		isScheduled: function () {
+				// inPt on polygon contour => immediate success    or
+				// toggling of inside/outside at every single! intersection point of an edge
+				//  with the horizontal line through inPt, left of inPt
+				//  not counting lowerY endpoints of edges and whole edges on that line
+				var inside = false;
+				for ( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
 
-			return this._mixer._isActiveAction( this );
+					var edgeLowPt = inPolygon[ p ];
+					var edgeHighPt = inPolygon[ q ];
 
-		},
+					var edgeDx = edgeHighPt.x - edgeLowPt.x;
+					var edgeDy = edgeHighPt.y - edgeLowPt.y;
 
-		startAt: function ( time ) {
+					if ( Math.abs( edgeDy ) > Number.EPSILON ) {
 
-			this._startTime = time;
+						// not parallel
+						if ( edgeDy < 0 ) {
 
-			return this;
+							edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
+							edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
 
-		},
+						}
 
-		setLoop: function ( mode, repetitions ) {
+						if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) 		{ continue; }
 
-			this.loop = mode;
-			this.repetitions = repetitions;
+						if ( inPt.y === edgeLowPt.y ) {
 
-			return this;
+							if ( inPt.x === edgeLowPt.x )		{ return	true; }		// inPt is on contour ?
+							// continue;				// no intersection or edgeLowPt => doesn't count !!!
 
-		},
+						} else {
 
-		// Weight
+							var perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
+							if ( perpEdge === 0 )				{ return	true; }		// inPt is on contour ?
+							if ( perpEdge < 0 ) 				{ continue; }
+							inside = ! inside;		// true intersection left of inPt
 
-		// set the weight stopping any scheduled fading
-		// although .enabled = false yields an effective weight of zero, this
-		// method does *not* change .enabled, because it would be confusing
-		setEffectiveWeight: function ( weight ) {
+						}
 
-			this.weight = weight;
+					} else {
 
-			// note: same logic as when updated at runtime
-			this._effectiveWeight = this.enabled ? weight : 0;
+						// parallel or collinear
+						if ( inPt.y !== edgeLowPt.y ) 		{ continue; }			// parallel
+						// edge lies on the same horizontal line as inPt
+						if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
+							 ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) )		{ return	true; }	// inPt: Point on contour !
+						// continue;
 
-			return this.stopFading();
+					}
 
-		},
+				}
 
-		// return the weight considering fading and .enabled
-		getEffectiveWeight: function () {
+				return	inside;
 
-			return this._effectiveWeight;
+			}
 
-		},
+			var isClockWise = ShapeUtils.isClockWise;
 
-		fadeIn: function ( duration ) {
+			var subPaths = this.subPaths;
+			if ( subPaths.length === 0 ) { return []; }
 
-			return this._scheduleFading( duration, 0, 1 );
+			if ( noHoles === true )	{ return	toShapesNoHoles( subPaths ); }
 
-		},
 
-		fadeOut: function ( duration ) {
+			var solid, tmpPath, tmpShape, shapes = [];
 
-			return this._scheduleFading( duration, 1, 0 );
+			if ( subPaths.length === 1 ) {
 
-		},
+				tmpPath = subPaths[ 0 ];
+				tmpShape = new Shape();
+				tmpShape.curves = tmpPath.curves;
+				shapes.push( tmpShape );
+				return shapes;
 
-		crossFadeFrom: function ( fadeOutAction, duration, warp ) {
+			}
 
-			fadeOutAction.fadeOut( duration );
-			this.fadeIn( duration );
+			var holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
+			holesFirst = isCCW ? ! holesFirst : holesFirst;
 
-			if ( warp ) {
+			// console.log("Holes first", holesFirst);
 
-				var fadeInDuration = this._clip.duration,
-					fadeOutDuration = fadeOutAction._clip.duration,
+			var betterShapeHoles = [];
+			var newShapes = [];
+			var newShapeHoles = [];
+			var mainIdx = 0;
+			var tmpPoints;
 
-					startEndRatio = fadeOutDuration / fadeInDuration,
-					endStartRatio = fadeInDuration / fadeOutDuration;
+			newShapes[ mainIdx ] = undefined;
+			newShapeHoles[ mainIdx ] = [];
 
-				fadeOutAction.warp( 1.0, startEndRatio, duration );
-				this.warp( endStartRatio, 1.0, duration );
+			for ( var i = 0, l = subPaths.length; i < l; i ++ ) {
 
-			}
+				tmpPath = subPaths[ i ];
+				tmpPoints = tmpPath.getPoints();
+				solid = isClockWise( tmpPoints );
+				solid = isCCW ? ! solid : solid;
 
-			return this;
+				if ( solid ) {
 
-		},
+					if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) )	{ mainIdx ++; }
 
-		crossFadeTo: function ( fadeInAction, duration, warp ) {
+					newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints };
+					newShapes[ mainIdx ].s.curves = tmpPath.curves;
 
-			return fadeInAction.crossFadeFrom( this, duration, warp );
+					if ( holesFirst )	{ mainIdx ++; }
+					newShapeHoles[ mainIdx ] = [];
 
-		},
+					//console.log('cw', i);
 
-		stopFading: function () {
+				} else {
 
-			var weightInterpolant = this._weightInterpolant;
+					newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
 
-			if ( weightInterpolant !== null ) {
+					//console.log('ccw', i);
 
-				this._weightInterpolant = null;
-				this._mixer._takeBackControlInterpolant( weightInterpolant );
+				}
 
 			}
 
-			return this;
+			// only Holes? -> probably all Shapes with wrong orientation
+			if ( ! newShapes[ 0 ] )	{ return	toShapesNoHoles( subPaths ); }
 
-		},
 
-		// Time Scale Control
+			if ( newShapes.length > 1 ) {
 
-		// set the time scale stopping any scheduled warping
-		// although .paused = true yields an effective time scale of zero, this
-		// method does *not* change .paused, because it would be confusing
-		setEffectiveTimeScale: function ( timeScale ) {
+				var ambiguous = false;
+				var toChange = [];
 
-			this.timeScale = timeScale;
-			this._effectiveTimeScale = this.paused ? 0 : timeScale;
+				for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
 
-			return this.stopWarping();
+					betterShapeHoles[ sIdx ] = [];
 
-		},
+				}
 
-		// return the time scale considering warping and .paused
-		getEffectiveTimeScale: function () {
+				for ( var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
 
-			return this._effectiveTimeScale;
+					var sho = newShapeHoles[ sIdx ];
 
-		},
+					for ( var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
 
-		setDuration: function ( duration ) {
+						var ho = sho[ hIdx ];
+						var hole_unassigned = true;
 
-			this.timeScale = this._clip.duration / duration;
+						for ( var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
 
-			return this.stopWarping();
+							if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
 
-		},
+								if ( sIdx !== s2Idx )	{ toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } ); }
+								if ( hole_unassigned ) {
 
-		syncWith: function ( action ) {
+									hole_unassigned = false;
+									betterShapeHoles[ s2Idx ].push( ho );
 
-			this.time = action.time;
-			this.timeScale = action.timeScale;
+								} else {
 
-			return this.stopWarping();
+									ambiguous = true;
 
-		},
+								}
 
-		halt: function ( duration ) {
+							}
 
-			return this.warp( this._effectiveTimeScale, 0, duration );
+						}
 
-		},
+						if ( hole_unassigned ) {
 
-		warp: function ( startTimeScale, endTimeScale, duration ) {
+							betterShapeHoles[ sIdx ].push( ho );
 
-			var mixer = this._mixer, now = mixer.time,
-				interpolant = this._timeScaleInterpolant,
+						}
 
-				timeScale = this.timeScale;
+					}
 
-			if ( interpolant === null ) {
+				}
+				// console.log("ambiguous: ", ambiguous);
 
-				interpolant = mixer._lendControlInterpolant();
-				this._timeScaleInterpolant = interpolant;
+				if ( toChange.length > 0 ) {
+
+					// console.log("to change: ", toChange);
+					if ( ! ambiguous )	{ newShapeHoles = betterShapeHoles; }
+
+				}
 
 			}
 
-			var times = interpolant.parameterPositions,
-				values = interpolant.sampleValues;
+			var tmpHoles;
 
-			times[ 0 ] = now;
-			times[ 1 ] = now + duration;
+			for ( var i = 0, il = newShapes.length; i < il; i ++ ) {
 
-			values[ 0 ] = startTimeScale / timeScale;
-			values[ 1 ] = endTimeScale / timeScale;
+				tmpShape = newShapes[ i ].s;
+				shapes.push( tmpShape );
+				tmpHoles = newShapeHoles[ i ];
 
-			return this;
+				for ( var j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
 
-		},
+					tmpShape.holes.push( tmpHoles[ j ].h );
 
-		stopWarping: function () {
+				}
 
-			var timeScaleInterpolant = this._timeScaleInterpolant;
+			}
 
-			if ( timeScaleInterpolant !== null ) {
+			//console.log("shape", shapes);
 
-				this._timeScaleInterpolant = null;
-				this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
+			return shapes;
 
-			}
+		}
 
-			return this;
+	} );
 
-		},
+	/**
+	 * @author zz85 / http://www.lab4games.net/zz85/blog
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		// Object Accessors
 
-		getMixer: function () {
+	function Font( data ) {
 
-			return this._mixer;
+		this.type = 'Font';
 
-		},
+		this.data = data;
 
-		getClip: function () {
+	}
 
-			return this._clip;
+	Object.assign( Font.prototype, {
 
-		},
+		isFont: true,
 
-		getRoot: function () {
+		generateShapes: function ( text, size ) {
 
-			return this._localRoot || this._mixer._root;
+			if ( size === undefined ) { size = 100; }
 
-		},
+			var shapes = [];
+			var paths = createPaths( text, size, this.data );
 
-		// Interna
+			for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
 
-		_update: function ( time, deltaTime, timeDirection, accuIndex ) {
+				Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
 
-			// called by the mixer
+			}
 
-			if ( ! this.enabled ) {
+			return shapes;
 
-				// call ._updateWeight() to update ._effectiveWeight
+		}
 
-				this._updateWeight( time );
-				return;
+	} );
 
-			}
+	function createPaths( text, size, data ) {
 
-			var startTime = this._startTime;
+		var chars = Array.from ? Array.from( text ) : String( text ).split( '' ); // workaround for IE11, see #13988
+		var scale = size / data.resolution;
+		var line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale;
 
-			if ( startTime !== null ) {
+		var paths = [];
 
-				// check for scheduled start of action
+		var offsetX = 0, offsetY = 0;
 
-				var timeRunning = ( time - startTime ) * timeDirection;
-				if ( timeRunning < 0 || timeDirection === 0 ) {
+		for ( var i = 0; i < chars.length; i ++ ) {
 
-					return; // yet to come / don't decide when delta = 0
+			var char = chars[ i ];
 
-				}
+			if ( char === '\n' ) {
 
-				// start
+				offsetX = 0;
+				offsetY -= line_height;
 
-				this._startTime = null; // unschedule
-				deltaTime = timeDirection * timeRunning;
+			} else {
+
+				var ret = createPath( char, scale, offsetX, offsetY, data );
+				offsetX += ret.offsetX;
+				paths.push( ret.path );
 
 			}
 
-			// apply time scale and advance time
+		}
 
-			deltaTime *= this._updateTimeScale( time );
-			var clipTime = this._updateTime( deltaTime );
+		return paths;
 
-			// note: _updateTime may disable the action resulting in
-			// an effective weight of 0
+	}
 
-			var weight = this._updateWeight( time );
+	function createPath( char, scale, offsetX, offsetY, data ) {
 
-			if ( weight > 0 ) {
+		var glyph = data.glyphs[ char ] || data.glyphs[ '?' ];
 
-				var interpolants = this._interpolants;
-				var propertyMixers = this._propertyBindings;
+		if ( ! glyph ) {
 
-				switch ( this.blendMode ) {
+			console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' );
 
-					case AdditiveAnimationBlendMode:
+			return;
 
-						for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
+		}
 
-							interpolants[ j ].evaluate( clipTime );
-							propertyMixers[ j ].accumulateAdditive( weight );
+		var path = new ShapePath();
 
-						}
+		var x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2;
 
-						break;
+		if ( glyph.o ) {
 
-					case NormalAnimationBlendMode:
-					default:
+			var outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
 
-						for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
+			for ( var i = 0, l = outline.length; i < l; ) {
 
-							interpolants[ j ].evaluate( clipTime );
-							propertyMixers[ j ].accumulate( accuIndex, weight );
+				var action = outline[ i ++ ];
 
-						}
+				switch ( action ) {
 
-				}
+					case 'm': // moveTo
 
-			}
+						x = outline[ i ++ ] * scale + offsetX;
+						y = outline[ i ++ ] * scale + offsetY;
 
-		},
+						path.moveTo( x, y );
 
-		_updateWeight: function ( time ) {
+						break;
 
-			var weight = 0;
+					case 'l': // lineTo
 
-			if ( this.enabled ) {
+						x = outline[ i ++ ] * scale + offsetX;
+						y = outline[ i ++ ] * scale + offsetY;
 
-				weight = this.weight;
-				var interpolant = this._weightInterpolant;
+						path.lineTo( x, y );
 
-				if ( interpolant !== null ) {
+						break;
 
-					var interpolantValue = interpolant.evaluate( time )[ 0 ];
+					case 'q': // quadraticCurveTo
 
-					weight *= interpolantValue;
+						cpx = outline[ i ++ ] * scale + offsetX;
+						cpy = outline[ i ++ ] * scale + offsetY;
+						cpx1 = outline[ i ++ ] * scale + offsetX;
+						cpy1 = outline[ i ++ ] * scale + offsetY;
 
-					if ( time > interpolant.parameterPositions[ 1 ] ) {
+						path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
 
-						this.stopFading();
+						break;
 
-						if ( interpolantValue === 0 ) {
+					case 'b': // bezierCurveTo
 
-							// faded out, disable
-							this.enabled = false;
+						cpx = outline[ i ++ ] * scale + offsetX;
+						cpy = outline[ i ++ ] * scale + offsetY;
+						cpx1 = outline[ i ++ ] * scale + offsetX;
+						cpy1 = outline[ i ++ ] * scale + offsetY;
+						cpx2 = outline[ i ++ ] * scale + offsetX;
+						cpy2 = outline[ i ++ ] * scale + offsetY;
 
-						}
+						path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
 
-					}
+						break;
 
 				}
 
 			}
 
-			this._effectiveWeight = weight;
-			return weight;
-
-		},
+		}
 
-		_updateTimeScale: function ( time ) {
+		return { offsetX: glyph.ha * scale, path: path };
 
-			var timeScale = 0;
+	}
 
-			if ( ! this.paused ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				timeScale = this.timeScale;
+	function FontLoader( manager ) {
 
-				var interpolant = this._timeScaleInterpolant;
+		Loader.call( this, manager );
 
-				if ( interpolant !== null ) {
+	}
 
-					var interpolantValue = interpolant.evaluate( time )[ 0 ];
+	FontLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-					timeScale *= interpolantValue;
+		constructor: FontLoader,
 
-					if ( time > interpolant.parameterPositions[ 1 ] ) {
+		load: function ( url, onLoad, onProgress, onError ) {
 
-						this.stopWarping();
+			var scope = this;
 
-						if ( timeScale === 0 ) {
+			var loader = new FileLoader( this.manager );
+			loader.setPath( this.path );
+			loader.load( url, function ( text ) {
 
-							// motion has halted, pause
-							this.paused = true;
+				var json;
 
-						} else {
+				try {
 
-							// warp done - apply final time scale
-							this.timeScale = timeScale;
+					json = JSON.parse( text );
 
-						}
+				} catch ( e ) {
 
-					}
+					console.warn( 'THREE.FontLoader: typeface.js support is being deprecated. Use typeface.json instead.' );
+					json = JSON.parse( text.substring( 65, text.length - 2 ) );
 
 				}
 
-			}
+				var font = scope.parse( json );
 
-			this._effectiveTimeScale = timeScale;
-			return timeScale;
+				if ( onLoad ) { onLoad( font ); }
+
+			}, onProgress, onError );
 
 		},
 
-		_updateTime: function ( deltaTime ) {
+		parse: function ( json ) {
 
-			var time = this.time + deltaTime;
-			var duration = this._clip.duration;
-			var loop = this.loop;
-			var loopCount = this._loopCount;
+			return new Font( json );
 
-			var pingPong = ( loop === LoopPingPong );
+		}
 
-			if ( deltaTime === 0 ) {
+	} );
 
-				if ( loopCount === - 1 ) { return time; }
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
+	var _context;
 
-			}
+	var AudioContext = {
 
-			if ( loop === LoopOnce ) {
+		getContext: function () {
 
-				if ( loopCount === - 1 ) {
+			if ( _context === undefined ) {
 
-					// just started
+				_context = new ( window.AudioContext || window.webkitAudioContext )();
 
-					this._loopCount = 0;
-					this._setEndings( true, true, false );
+			}
 
-				}
+			return _context;
 
-				handle_stop: {
+		},
 
-					if ( time >= duration ) {
+		setContext: function ( value ) {
 
-						time = duration;
+			_context = value;
 
-					} else if ( time < 0 ) {
+		}
 
-						time = 0;
+	};
 
-					} else {
+	/**
+	 * @author Reece Aaron Lecrivain / http://reecenotes.com/
+	 */
 
-						this.time = time;
+	function AudioLoader( manager ) {
 
-						break handle_stop;
+		Loader.call( this, manager );
 
-					}
+	}
 
-					if ( this.clampWhenFinished ) { this.paused = true; }
-					else { this.enabled = false; }
+	AudioLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
-					this.time = time;
+		constructor: AudioLoader,
 
-					this._mixer.dispatchEvent( {
-						type: 'finished', action: this,
-						direction: deltaTime < 0 ? - 1 : 1
-					} );
+		load: function ( url, onLoad, onProgress, onError ) {
 
-				}
+			var loader = new FileLoader( this.manager );
+			loader.setResponseType( 'arraybuffer' );
+			loader.setPath( this.path );
+			loader.load( url, function ( buffer ) {
 
-			} else { // repetitive Repeat or PingPong
+				// Create a copy of the buffer. The `decodeAudioData` method
+				// detaches the buffer when complete, preventing reuse.
+				var bufferCopy = buffer.slice( 0 );
 
-				if ( loopCount === - 1 ) {
+				var context = AudioContext.getContext();
+				context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
 
-					// just started
+					onLoad( audioBuffer );
 
-					if ( deltaTime >= 0 ) {
+				} );
 
-						loopCount = 0;
+			}, onProgress, onError );
 
-						this._setEndings( true, this.repetitions === 0, pingPong );
+		}
 
-					} else {
+	} );
 
-						// when looping in reverse direction, the initial
-						// transition through zero counts as a repetition,
-						// so leave loopCount at -1
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-						this._setEndings( this.repetitions === 0, true, pingPong );
+	function HemisphereLightProbe( skyColor, groundColor, intensity ) {
 
-					}
+		LightProbe.call( this, undefined, intensity );
 
-				}
+		var color1 = new Color().set( skyColor );
+		var color2 = new Color().set( groundColor );
 
-				if ( time >= duration || time < 0 ) {
+		var sky = new Vector3( color1.r, color1.g, color1.b );
+		var ground = new Vector3( color2.r, color2.g, color2.b );
 
-					// wrap around
+		// without extra factor of PI in the shader, should = 1 / Math.sqrt( Math.PI );
+		var c0 = Math.sqrt( Math.PI );
+		var c1 = c0 * Math.sqrt( 0.75 );
 
-					var loopDelta = Math.floor( time / duration ); // signed
-					time -= duration * loopDelta;
+		this.sh.coefficients[ 0 ].copy( sky ).add( ground ).multiplyScalar( c0 );
+		this.sh.coefficients[ 1 ].copy( sky ).sub( ground ).multiplyScalar( c1 );
 
-					loopCount += Math.abs( loopDelta );
+	}
 
-					var pending = this.repetitions - loopCount;
+	HemisphereLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
 
-					if ( pending <= 0 ) {
+		constructor: HemisphereLightProbe,
 
-						// have to stop (switch state, clamp time, fire event)
+		isHemisphereLightProbe: true,
 
-						if ( this.clampWhenFinished ) { this.paused = true; }
-						else { this.enabled = false; }
+		copy: function ( source ) { // modifying colors not currently supported
 
-						time = deltaTime > 0 ? duration : 0;
+			LightProbe.prototype.copy.call( this, source );
 
-						this.time = time;
+			return this;
 
-						this._mixer.dispatchEvent( {
-							type: 'finished', action: this,
-							direction: deltaTime > 0 ? 1 : - 1
-						} );
+		},
 
-					} else {
+		toJSON: function ( meta ) {
 
-						// keep running
+			var data = LightProbe.prototype.toJSON.call( this, meta );
 
-						if ( pending === 1 ) {
+			// data.sh = this.sh.toArray(); // todo
 
-							// entering the last round
+			return data;
 
-							var atStart = deltaTime < 0;
-							this._setEndings( atStart, ! atStart, pingPong );
+		}
 
-						} else {
+	} );
 
-							this._setEndings( false, false, pingPong );
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-						}
+	function AmbientLightProbe( color, intensity ) {
 
-						this._loopCount = loopCount;
+		LightProbe.call( this, undefined, intensity );
 
-						this.time = time;
+		var color1 = new Color().set( color );
 
-						this._mixer.dispatchEvent( {
-							type: 'loop', action: this, loopDelta: loopDelta
-						} );
+		// without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI );
+		this.sh.coefficients[ 0 ].set( color1.r, color1.g, color1.b ).multiplyScalar( 2 * Math.sqrt( Math.PI ) );
 
-					}
+	}
 
-				} else {
+	AmbientLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), {
 
-					this.time = time;
+		constructor: AmbientLightProbe,
 
-				}
+		isAmbientLightProbe: true,
 
-				if ( pingPong && ( loopCount & 1 ) === 1 ) {
+		copy: function ( source ) { // modifying color not currently supported
 
-					// invert time for the "pong round"
+			LightProbe.prototype.copy.call( this, source );
 
-					return duration - time;
+			return this;
 
-				}
+		},
 
-			}
+		toJSON: function ( meta ) {
 
-			return time;
+			var data = LightProbe.prototype.toJSON.call( this, meta );
 
-		},
+			// data.sh = this.sh.toArray(); // todo
 
-		_setEndings: function ( atStart, atEnd, pingPong ) {
+			return data;
 
-			var settings = this._interpolantSettings;
+		}
 
-			if ( pingPong ) {
+	} );
 
-				settings.endingStart = ZeroSlopeEnding;
-				settings.endingEnd = ZeroSlopeEnding;
+	var _eyeRight = new Matrix4();
+	var _eyeLeft = new Matrix4();
 
-			} else {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				// assuming for LoopOnce atStart == atEnd == true
+	function StereoCamera() {
 
-				if ( atStart ) {
+		this.type = 'StereoCamera';
 
-					settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
+		this.aspect = 1;
 
-				} else {
+		this.eyeSep = 0.064;
 
-					settings.endingStart = WrapAroundEnding;
+		this.cameraL = new PerspectiveCamera();
+		this.cameraL.layers.enable( 1 );
+		this.cameraL.matrixAutoUpdate = false;
 
-				}
+		this.cameraR = new PerspectiveCamera();
+		this.cameraR.layers.enable( 2 );
+		this.cameraR.matrixAutoUpdate = false;
 
-				if ( atEnd ) {
+		this._cache = {
+			focus: null,
+			fov: null,
+			aspect: null,
+			near: null,
+			far: null,
+			zoom: null,
+			eyeSep: null
+		};
 
-					settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
+	}
 
-				} else {
+	Object.assign( StereoCamera.prototype, {
 
-					settings.endingEnd 	 = WrapAroundEnding;
+		update: function ( camera ) {
 
-				}
+			var cache = this._cache;
 
-			}
+			var needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov ||
+				cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near ||
+				cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep;
 
-		},
+			if ( needsUpdate ) {
 
-		_scheduleFading: function ( duration, weightNow, weightThen ) {
+				cache.focus = camera.focus;
+				cache.fov = camera.fov;
+				cache.aspect = camera.aspect * this.aspect;
+				cache.near = camera.near;
+				cache.far = camera.far;
+				cache.zoom = camera.zoom;
+				cache.eyeSep = this.eyeSep;
 
-			var mixer = this._mixer, now = mixer.time,
-				interpolant = this._weightInterpolant;
+				// Off-axis stereoscopic effect based on
+				// http://paulbourke.net/stereographics/stereorender/
 
-			if ( interpolant === null ) {
+				var projectionMatrix = camera.projectionMatrix.clone();
+				var eyeSepHalf = cache.eyeSep / 2;
+				var eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus;
+				var ymax = ( cache.near * Math.tan( MathUtils.DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom;
+				var xmin, xmax;
 
-				interpolant = mixer._lendControlInterpolant();
-				this._weightInterpolant = interpolant;
+				// translate xOffset
 
-			}
+				_eyeLeft.elements[ 12 ] = - eyeSepHalf;
+				_eyeRight.elements[ 12 ] = eyeSepHalf;
 
-			var times = interpolant.parameterPositions,
-				values = interpolant.sampleValues;
+				// for left eye
 
-			times[ 0 ] = now;
-			values[ 0 ] = weightNow;
-			times[ 1 ] = now + duration;
-			values[ 1 ] = weightThen;
+				xmin = - ymax * cache.aspect + eyeSepOnProjection;
+				xmax = ymax * cache.aspect + eyeSepOnProjection;
 
-			return this;
+				projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
+				projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+
+				this.cameraL.projectionMatrix.copy( projectionMatrix );
+
+				// for right eye
+
+				xmin = - ymax * cache.aspect - eyeSepOnProjection;
+				xmax = ymax * cache.aspect - eyeSepOnProjection;
+
+				projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
+				projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+
+				this.cameraR.projectionMatrix.copy( projectionMatrix );
+
+			}
+
+			this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft );
+			this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight );
 
 		}
 
 	} );
 
 	/**
-	 *
-	 * Player for AnimationClips.
-	 *
-	 *
-	 * @author Ben Houston / http://clara.io/
-	 * @author David Sarno / http://lighthaus.us/
-	 * @author tschw
+	 * @author alteredq / http://alteredqualia.com/
 	 */
 
-	function AnimationMixer( root ) {
+	function Clock( autoStart ) {
 
-		this._root = root;
-		this._initMemoryManager();
-		this._accuIndex = 0;
+		this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
 
-		this.time = 0;
+		this.startTime = 0;
+		this.oldTime = 0;
+		this.elapsedTime = 0;
 
-		this.timeScale = 1.0;
+		this.running = false;
 
 	}
 
-	AnimationMixer.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
+	Object.assign( Clock.prototype, {
 
-		constructor: AnimationMixer,
+		start: function () {
 
-		_bindAction: function ( action, prototypeAction ) {
+			this.startTime = ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732
 
-			var root = action._localRoot || this._root,
-				tracks = action._clip.tracks,
-				nTracks = tracks.length,
-				bindings = action._propertyBindings,
-				interpolants = action._interpolants,
-				rootUuid = root.uuid,
-				bindingsByRoot = this._bindingsByRootAndName,
-				bindingsByName = bindingsByRoot[ rootUuid ];
+			this.oldTime = this.startTime;
+			this.elapsedTime = 0;
+			this.running = true;
 
-			if ( bindingsByName === undefined ) {
+		},
 
-				bindingsByName = {};
-				bindingsByRoot[ rootUuid ] = bindingsByName;
+		stop: function () {
 
-			}
+			this.getElapsedTime();
+			this.running = false;
+			this.autoStart = false;
 
-			for ( var i = 0; i !== nTracks; ++ i ) {
+		},
 
-				var track = tracks[ i ],
-					trackName = track.name,
-					binding = bindingsByName[ trackName ];
+		getElapsedTime: function () {
 
-				if ( binding !== undefined ) {
+			this.getDelta();
+			return this.elapsedTime;
 
-					bindings[ i ] = binding;
+		},
 
-				} else {
+		getDelta: function () {
 
-					binding = bindings[ i ];
+			var diff = 0;
 
-					if ( binding !== undefined ) {
+			if ( this.autoStart && ! this.running ) {
 
-						// existing binding, make sure the cache knows
+				this.start();
+				return 0;
 
-						if ( binding._cacheIndex === null ) {
+			}
 
-							++ binding.referenceCount;
-							this._addInactiveBinding( binding, rootUuid, trackName );
+			if ( this.running ) {
 
-						}
+				var newTime = ( typeof performance === 'undefined' ? Date : performance ).now();
 
-						continue;
+				diff = ( newTime - this.oldTime ) / 1000;
+				this.oldTime = newTime;
 
-					}
+				this.elapsedTime += diff;
 
-					var path = prototypeAction && prototypeAction.
-						_propertyBindings[ i ].binding.parsedPath;
+			}
 
-					binding = new PropertyMixer(
-						PropertyBinding.create( root, trackName, path ),
-						track.ValueTypeName, track.getValueSize() );
+			return diff;
 
-					++ binding.referenceCount;
-					this._addInactiveBinding( binding, rootUuid, trackName );
+		}
 
-					bindings[ i ] = binding;
+	} );
 
-				}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-				interpolants[ i ].resultBuffer = binding.buffer;
+	var _position$2 = new Vector3();
+	var _quaternion$3 = new Quaternion();
+	var _scale$1 = new Vector3();
+	var _orientation = new Vector3();
 
-			}
+	function AudioListener() {
 
-		},
+		Object3D.call( this );
 
-		_activateAction: function ( action ) {
+		this.type = 'AudioListener';
 
-			if ( ! this._isActiveAction( action ) ) {
+		this.context = AudioContext.getContext();
 
-				if ( action._cacheIndex === null ) {
+		this.gain = this.context.createGain();
+		this.gain.connect( this.context.destination );
 
-					// this action has been forgotten by the cache, but the user
-					// appears to be still using it -> rebind
+		this.filter = null;
 
-					var rootUuid = ( action._localRoot || this._root ).uuid,
-						clipUuid = action._clip.uuid,
-						actionsForClip = this._actionsByClip[ clipUuid ];
+		this.timeDelta = 0;
 
-					this._bindAction( action,
-						actionsForClip && actionsForClip.knownActions[ 0 ] );
+		// private
 
-					this._addInactiveAction( action, clipUuid, rootUuid );
+		this._clock = new Clock();
 
-				}
+	}
 
-				var bindings = action._propertyBindings;
+	AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-				// increment reference counts / sort out state
-				for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
+		constructor: AudioListener,
 
-					var binding = bindings[ i ];
+		getInput: function () {
 
-					if ( binding.useCount ++ === 0 ) {
+			return this.gain;
 
-						this._lendBinding( binding );
-						binding.saveOriginalState();
+		},
 
-					}
+		removeFilter: function ( ) {
 
-				}
+			if ( this.filter !== null ) {
 
-				this._lendAction( action );
+				this.gain.disconnect( this.filter );
+				this.filter.disconnect( this.context.destination );
+				this.gain.connect( this.context.destination );
+				this.filter = null;
 
 			}
 
+			return this;
+
 		},
 
-		_deactivateAction: function ( action ) {
+		getFilter: function () {
 
-			if ( this._isActiveAction( action ) ) {
+			return this.filter;
 
-				var bindings = action._propertyBindings;
+		},
 
-				// decrement reference counts / sort out state
-				for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
+		setFilter: function ( value ) {
 
-					var binding = bindings[ i ];
+			if ( this.filter !== null ) {
 
-					if ( -- binding.useCount === 0 ) {
+				this.gain.disconnect( this.filter );
+				this.filter.disconnect( this.context.destination );
 
-						binding.restoreOriginalState();
-						this._takeBackBinding( binding );
+			} else {
 
-					}
+				this.gain.disconnect( this.context.destination );
 
-				}
+			}
 
-				this._takeBackAction( action );
+			this.filter = value;
+			this.gain.connect( this.filter );
+			this.filter.connect( this.context.destination );
 
-			}
+			return this;
 
 		},
 
-		// Memory manager
+		getMasterVolume: function () {
 
-		_initMemoryManager: function () {
+			return this.gain.gain.value;
 
-			this._actions = []; // 'nActiveActions' followed by inactive ones
-			this._nActiveActions = 0;
+		},
 
-			this._actionsByClip = {};
-			// inside:
-			// {
-			// 	knownActions: Array< AnimationAction > - used as prototypes
-			// 	actionByRoot: AnimationAction - lookup
-			// }
+		setMasterVolume: function ( value ) {
 
+			this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
 
-			this._bindings = []; // 'nActiveBindings' followed by inactive ones
-			this._nActiveBindings = 0;
+			return this;
 
-			this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
+		},
 
+		updateMatrixWorld: function ( force ) {
 
-			this._controlInterpolants = []; // same game as above
-			this._nActiveControlInterpolants = 0;
+			Object3D.prototype.updateMatrixWorld.call( this, force );
 
-			var scope = this;
+			var listener = this.context.listener;
+			var up = this.up;
 
-			this.stats = {
+			this.timeDelta = this._clock.getDelta();
 
-				actions: {
-					get total() {
+			this.matrixWorld.decompose( _position$2, _quaternion$3, _scale$1 );
 
-						return scope._actions.length;
+			_orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion$3 );
 
-					},
-					get inUse() {
+			if ( listener.positionX ) {
 
-						return scope._nActiveActions;
+				// code path for Chrome (see #14393)
 
-					}
-				},
-				bindings: {
-					get total() {
+				var endTime = this.context.currentTime + this.timeDelta;
 
-						return scope._bindings.length;
+				listener.positionX.linearRampToValueAtTime( _position$2.x, endTime );
+				listener.positionY.linearRampToValueAtTime( _position$2.y, endTime );
+				listener.positionZ.linearRampToValueAtTime( _position$2.z, endTime );
+				listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime );
+				listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime );
+				listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime );
+				listener.upX.linearRampToValueAtTime( up.x, endTime );
+				listener.upY.linearRampToValueAtTime( up.y, endTime );
+				listener.upZ.linearRampToValueAtTime( up.z, endTime );
 
-					},
-					get inUse() {
+			} else {
 
-						return scope._nActiveBindings;
+				listener.setPosition( _position$2.x, _position$2.y, _position$2.z );
+				listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z );
 
-					}
-				},
-				controlInterpolants: {
-					get total() {
+			}
 
-						return scope._controlInterpolants.length;
+		}
 
-					},
-					get inUse() {
+	} );
 
-						return scope._nActiveControlInterpolants;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Reece Aaron Lecrivain / http://reecenotes.com/
+	 */
 
-					}
-				}
+	function Audio( listener ) {
 
-			};
+		Object3D.call( this );
 
-		},
+		this.type = 'Audio';
 
-		// Memory management for AnimationAction objects
+		this.listener = listener;
+		this.context = listener.context;
 
-		_isActiveAction: function ( action ) {
+		this.gain = this.context.createGain();
+		this.gain.connect( listener.getInput() );
 
-			var index = action._cacheIndex;
-			return index !== null && index < this._nActiveActions;
+		this.autoplay = false;
 
-		},
+		this.buffer = null;
+		this.detune = 0;
+		this.loop = false;
+		this.loopStart = 0;
+		this.loopEnd = 0;
+		this.offset = 0;
+		this.duration = undefined;
+		this.playbackRate = 1;
+		this.isPlaying = false;
+		this.hasPlaybackControl = true;
+		this.sourceType = 'empty';
 
-		_addInactiveAction: function ( action, clipUuid, rootUuid ) {
+		this._startedAt = 0;
+		this._progress = 0;
 
-			var actions = this._actions,
-				actionsByClip = this._actionsByClip,
-				actionsForClip = actionsByClip[ clipUuid ];
+		this.filters = [];
 
-			if ( actionsForClip === undefined ) {
+	}
 
-				actionsForClip = {
+	Audio.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
-					knownActions: [ action ],
-					actionByRoot: {}
+		constructor: Audio,
 
-				};
+		getOutput: function () {
 
-				action._byClipCacheIndex = 0;
+			return this.gain;
 
-				actionsByClip[ clipUuid ] = actionsForClip;
+		},
 
-			} else {
+		setNodeSource: function ( audioNode ) {
 
-				var knownActions = actionsForClip.knownActions;
+			this.hasPlaybackControl = false;
+			this.sourceType = 'audioNode';
+			this.source = audioNode;
+			this.connect();
 
-				action._byClipCacheIndex = knownActions.length;
-				knownActions.push( action );
+			return this;
 
-			}
+		},
 
-			action._cacheIndex = actions.length;
-			actions.push( action );
+		setMediaElementSource: function ( mediaElement ) {
 
-			actionsForClip.actionByRoot[ rootUuid ] = action;
+			this.hasPlaybackControl = false;
+			this.sourceType = 'mediaNode';
+			this.source = this.context.createMediaElementSource( mediaElement );
+			this.connect();
+
+			return this;
 
 		},
 
-		_removeInactiveAction: function ( action ) {
+		setMediaStreamSource: function ( mediaStream ) {
 
-			var actions = this._actions,
-				lastInactiveAction = actions[ actions.length - 1 ],
-				cacheIndex = action._cacheIndex;
+			this.hasPlaybackControl = false;
+			this.sourceType = 'mediaStreamNode';
+			this.source = this.context.createMediaStreamSource( mediaStream );
+			this.connect();
 
-			lastInactiveAction._cacheIndex = cacheIndex;
-			actions[ cacheIndex ] = lastInactiveAction;
-			actions.pop();
+			return this;
 
-			action._cacheIndex = null;
+		},
 
+		setBuffer: function ( audioBuffer ) {
 
-			var clipUuid = action._clip.uuid,
-				actionsByClip = this._actionsByClip,
-				actionsForClip = actionsByClip[ clipUuid ],
-				knownActionsForClip = actionsForClip.knownActions,
+			this.buffer = audioBuffer;
+			this.sourceType = 'buffer';
 
-				lastKnownAction =
-					knownActionsForClip[ knownActionsForClip.length - 1 ],
+			if ( this.autoplay ) { this.play(); }
 
-				byClipCacheIndex = action._byClipCacheIndex;
+			return this;
 
-			lastKnownAction._byClipCacheIndex = byClipCacheIndex;
-			knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
-			knownActionsForClip.pop();
+		},
 
-			action._byClipCacheIndex = null;
+		play: function ( delay ) {
 
+			if ( delay === undefined ) { delay = 0; }
 
-			var actionByRoot = actionsForClip.actionByRoot,
-				rootUuid = ( action._localRoot || this._root ).uuid;
-
-			delete actionByRoot[ rootUuid ];
-
-			if ( knownActionsForClip.length === 0 ) {
+			if ( this.isPlaying === true ) {
 
-				delete actionsByClip[ clipUuid ];
+				console.warn( 'THREE.Audio: Audio is already playing.' );
+				return;
 
 			}
 
-			this._removeInactiveBindingsForAction( action );
+			if ( this.hasPlaybackControl === false ) {
 
-		},
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return;
 
-		_removeInactiveBindingsForAction: function ( action ) {
+			}
 
-			var bindings = action._propertyBindings;
-			for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
+			this._startedAt = this.context.currentTime + delay;
 
-				var binding = bindings[ i ];
+			var source = this.context.createBufferSource();
+			source.buffer = this.buffer;
+			source.loop = this.loop;
+			source.loopStart = this.loopStart;
+			source.loopEnd = this.loopEnd;
+			source.onended = this.onEnded.bind( this );
+			source.start( this._startedAt, this._progress + this.offset, this.duration );
 
-				if ( -- binding.referenceCount === 0 ) {
+			this.isPlaying = true;
 
-					this._removeInactiveBinding( binding );
+			this.source = source;
 
-				}
+			this.setDetune( this.detune );
+			this.setPlaybackRate( this.playbackRate );
 
-			}
+			return this.connect();
 
 		},
 
-		_lendAction: function ( action ) {
+		pause: function () {
 
-			// [ active actions |  inactive actions  ]
-			// [  active actions >| inactive actions ]
-			//                 s        a
-			//                  <-swap->
-			//                 a        s
+			if ( this.hasPlaybackControl === false ) {
 
-			var actions = this._actions,
-				prevIndex = action._cacheIndex,
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return;
 
-				lastActiveIndex = this._nActiveActions ++,
+			}
 
-				firstInactiveAction = actions[ lastActiveIndex ];
+			if ( this.isPlaying === true ) {
 
-			action._cacheIndex = lastActiveIndex;
-			actions[ lastActiveIndex ] = action;
+				// update current progress
 
-			firstInactiveAction._cacheIndex = prevIndex;
-			actions[ prevIndex ] = firstInactiveAction;
+				this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
 
-		},
+				if ( this.loop === true ) {
 
-		_takeBackAction: function ( action ) {
+					// ensure _progress does not exceed duration with looped audios
 
-			// [  active actions  | inactive actions ]
-			// [ active actions |< inactive actions  ]
-			//        a        s
-			//         <-swap->
-			//        s        a
+					this._progress = this._progress % ( this.duration || this.buffer.duration );
 
-			var actions = this._actions,
-				prevIndex = action._cacheIndex,
+				}
 
-				firstInactiveIndex = -- this._nActiveActions,
+				this.source.stop();
+				this.source.onended = null;
 
-				lastActiveAction = actions[ firstInactiveIndex ];
+				this.isPlaying = false;
 
-			action._cacheIndex = firstInactiveIndex;
-			actions[ firstInactiveIndex ] = action;
+			}
 
-			lastActiveAction._cacheIndex = prevIndex;
-			actions[ prevIndex ] = lastActiveAction;
+			return this;
 
 		},
 
-		// Memory management for PropertyMixer objects
+		stop: function () {
 
-		_addInactiveBinding: function ( binding, rootUuid, trackName ) {
+			if ( this.hasPlaybackControl === false ) {
 
-			var bindingsByRoot = this._bindingsByRootAndName,
-				bindingByName = bindingsByRoot[ rootUuid ],
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return;
 
-				bindings = this._bindings;
+			}
 
-			if ( bindingByName === undefined ) {
+			this._progress = 0;
 
-				bindingByName = {};
-				bindingsByRoot[ rootUuid ] = bindingByName;
+			this.source.stop();
+			this.source.onended = null;
+			this.isPlaying = false;
 
-			}
+			return this;
 
-			bindingByName[ trackName ] = binding;
+		},
 
-			binding._cacheIndex = bindings.length;
-			bindings.push( binding );
+		connect: function () {
 
-		},
+			if ( this.filters.length > 0 ) {
 
-		_removeInactiveBinding: function ( binding ) {
+				this.source.connect( this.filters[ 0 ] );
 
-			var bindings = this._bindings,
-				propBinding = binding.binding,
-				rootUuid = propBinding.rootNode.uuid,
-				trackName = propBinding.path,
-				bindingsByRoot = this._bindingsByRootAndName,
-				bindingByName = bindingsByRoot[ rootUuid ],
+				for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
 
-				lastInactiveBinding = bindings[ bindings.length - 1 ],
-				cacheIndex = binding._cacheIndex;
+					this.filters[ i - 1 ].connect( this.filters[ i ] );
 
-			lastInactiveBinding._cacheIndex = cacheIndex;
-			bindings[ cacheIndex ] = lastInactiveBinding;
-			bindings.pop();
+				}
 
-			delete bindingByName[ trackName ];
+				this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
 
-			if ( Object.keys( bindingByName ).length === 0 ) {
+			} else {
 
-				delete bindingsByRoot[ rootUuid ];
+				this.source.connect( this.getOutput() );
 
 			}
 
+			return this;
+
 		},
 
-		_lendBinding: function ( binding ) {
+		disconnect: function () {
 
-			var bindings = this._bindings,
-				prevIndex = binding._cacheIndex,
+			if ( this.filters.length > 0 ) {
 
-				lastActiveIndex = this._nActiveBindings ++,
+				this.source.disconnect( this.filters[ 0 ] );
 
-				firstInactiveBinding = bindings[ lastActiveIndex ];
+				for ( var i = 1, l = this.filters.length; i < l; i ++ ) {
 
-			binding._cacheIndex = lastActiveIndex;
-			bindings[ lastActiveIndex ] = binding;
+					this.filters[ i - 1 ].disconnect( this.filters[ i ] );
 
-			firstInactiveBinding._cacheIndex = prevIndex;
-			bindings[ prevIndex ] = firstInactiveBinding;
+				}
 
-		},
+				this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
 
-		_takeBackBinding: function ( binding ) {
+			} else {
 
-			var bindings = this._bindings,
-				prevIndex = binding._cacheIndex,
+				this.source.disconnect( this.getOutput() );
 
-				firstInactiveIndex = -- this._nActiveBindings,
+			}
 
-				lastActiveBinding = bindings[ firstInactiveIndex ];
+			return this;
 
-			binding._cacheIndex = firstInactiveIndex;
-			bindings[ firstInactiveIndex ] = binding;
+		},
 
-			lastActiveBinding._cacheIndex = prevIndex;
-			bindings[ prevIndex ] = lastActiveBinding;
+		getFilters: function () {
 
-		},
+			return this.filters;
 
+		},
 
-		// Memory management of Interpolants for weight and time scale
+		setFilters: function ( value ) {
 
-		_lendControlInterpolant: function () {
+			if ( ! value ) { value = []; }
 
-			var interpolants = this._controlInterpolants,
-				lastActiveIndex = this._nActiveControlInterpolants ++,
-				interpolant = interpolants[ lastActiveIndex ];
+			if ( this.isPlaying === true ) {
 
-			if ( interpolant === undefined ) {
+				this.disconnect();
+				this.filters = value;
+				this.connect();
 
-				interpolant = new LinearInterpolant(
-					new Float32Array( 2 ), new Float32Array( 2 ),
-					1, this._controlInterpolantsResultBuffer );
+			} else {
 
-				interpolant.__cacheIndex = lastActiveIndex;
-				interpolants[ lastActiveIndex ] = interpolant;
+				this.filters = value;
 
 			}
 
-			return interpolant;
+			return this;
 
 		},
 
-		_takeBackControlInterpolant: function ( interpolant ) {
+		setDetune: function ( value ) {
 
-			var interpolants = this._controlInterpolants,
-				prevIndex = interpolant.__cacheIndex,
+			this.detune = value;
 
-				firstInactiveIndex = -- this._nActiveControlInterpolants,
+			if ( this.source.detune === undefined ) { return; } // only set detune when available
 
-				lastActiveInterpolant = interpolants[ firstInactiveIndex ];
+			if ( this.isPlaying === true ) {
 
-			interpolant.__cacheIndex = firstInactiveIndex;
-			interpolants[ firstInactiveIndex ] = interpolant;
+				this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
 
-			lastActiveInterpolant.__cacheIndex = prevIndex;
-			interpolants[ prevIndex ] = lastActiveInterpolant;
+			}
+
+			return this;
 
 		},
 
-		_controlInterpolantsResultBuffer: new Float32Array( 1 ),
+		getDetune: function () {
 
-		// return an action for a clip optionally using a custom root target
-		// object (this method allocates a lot of dynamic memory in case a
-		// previously unknown clip/root combination is specified)
-		clipAction: function ( clip, optionalRoot, blendMode ) {
+			return this.detune;
 
-			var root = optionalRoot || this._root,
-				rootUuid = root.uuid,
+		},
 
-				clipObject = typeof clip === 'string' ?
-					AnimationClip.findByName( root, clip ) : clip,
+		getFilter: function () {
 
-				clipUuid = clipObject !== null ? clipObject.uuid : clip,
+			return this.getFilters()[ 0 ];
 
-				actionsForClip = this._actionsByClip[ clipUuid ],
-				prototypeAction = null;
+		},
 
-			if ( blendMode === undefined ) {
+		setFilter: function ( filter ) {
 
-				if ( clipObject !== null ) {
+			return this.setFilters( filter ? [ filter ] : [] );
 
-					blendMode = clipObject.blendMode;
+		},
 
-				} else {
+		setPlaybackRate: function ( value ) {
 
-					blendMode = NormalAnimationBlendMode;
+			if ( this.hasPlaybackControl === false ) {
 
-				}
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return;
 
 			}
 
-			if ( actionsForClip !== undefined ) {
+			this.playbackRate = value;
 
-				var existingAction =
-						actionsForClip.actionByRoot[ rootUuid ];
+			if ( this.isPlaying === true ) {
 
-				if ( existingAction !== undefined && existingAction.blendMode === blendMode ) {
+				this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
 
-					return existingAction;
+			}
 
-				}
+			return this;
 
-				// we know the clip, so we don't have to parse all
-				// the bindings again but can just copy
-				prototypeAction = actionsForClip.knownActions[ 0 ];
+		},
 
-				// also, take the clip from the prototype action
-				if ( clipObject === null )
-					{ clipObject = prototypeAction._clip; }
+		getPlaybackRate: function () {
 
-			}
+			return this.playbackRate;
 
-			// clip must be known when specified via string
-			if ( clipObject === null ) { return null; }
+		},
 
-			// allocate all resources required to run it
-			var newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode );
+		onEnded: function () {
 
-			this._bindAction( newAction, prototypeAction );
+			this.isPlaying = false;
 
-			// and make the action known to the memory manager
-			this._addInactiveAction( newAction, clipUuid, rootUuid );
+		},
 
-			return newAction;
+		getLoop: function () {
 
-		},
+			if ( this.hasPlaybackControl === false ) {
 
-		// get an existing action
-		existingAction: function ( clip, optionalRoot ) {
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return false;
 
-			var root = optionalRoot || this._root,
-				rootUuid = root.uuid,
+			}
 
-				clipObject = typeof clip === 'string' ?
-					AnimationClip.findByName( root, clip ) : clip,
+			return this.loop;
 
-				clipUuid = clipObject ? clipObject.uuid : clip,
+		},
 
-				actionsForClip = this._actionsByClip[ clipUuid ];
+		setLoop: function ( value ) {
 
-			if ( actionsForClip !== undefined ) {
+			if ( this.hasPlaybackControl === false ) {
 
-				return actionsForClip.actionByRoot[ rootUuid ] || null;
+				console.warn( 'THREE.Audio: this Audio has no playback control.' );
+				return;
 
 			}
 
-			return null;
+			this.loop = value;
 
-		},
+			if ( this.isPlaying === true ) {
 
-		// deactivates all previously scheduled actions
-		stopAllAction: function () {
+				this.source.loop = this.loop;
 
-			var actions = this._actions,
-				nActions = this._nActiveActions,
-				bindings = this._bindings,
-				nBindings = this._nActiveBindings;
+			}
 
-			this._nActiveActions = 0;
-			this._nActiveBindings = 0;
+			return this;
 
-			for ( var i = 0; i !== nActions; ++ i ) {
+		},
 
-				actions[ i ].reset();
+		setLoopStart: function ( value ) {
 
-			}
+			this.loopStart = value;
 
-			for ( var i = 0; i !== nBindings; ++ i ) {
+			return this;
 
-				bindings[ i ].useCount = 0;
+		},
 
-			}
+		setLoopEnd: function ( value ) {
+
+			this.loopEnd = value;
 
 			return this;
 
 		},
 
-		// advance the time and update apply the animation
-		update: function ( deltaTime ) {
-
-			deltaTime *= this.timeScale;
+		getVolume: function () {
 
-			var actions = this._actions,
-				nActions = this._nActiveActions,
+			return this.gain.gain.value;
 
-				time = this.time += deltaTime,
-				timeDirection = Math.sign( deltaTime ),
+		},
 
-				accuIndex = this._accuIndex ^= 1;
+		setVolume: function ( value ) {
 
-			// run active actions
+			this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
 
-			for ( var i = 0; i !== nActions; ++ i ) {
+			return this;
 
-				var action = actions[ i ];
+		}
 
-				action._update( time, deltaTime, timeDirection, accuIndex );
+	} );
 
-			}
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			// update scene graph
+	var _position$3 = new Vector3();
+	var _quaternion$4 = new Quaternion();
+	var _scale$2 = new Vector3();
+	var _orientation$1 = new Vector3();
 
-			var bindings = this._bindings,
-				nBindings = this._nActiveBindings;
+	function PositionalAudio( listener ) {
 
-			for ( var i = 0; i !== nBindings; ++ i ) {
+		Audio.call( this, listener );
 
-				bindings[ i ].apply( accuIndex );
+		this.panner = this.context.createPanner();
+		this.panner.panningModel = 'HRTF';
+		this.panner.connect( this.gain );
 
-			}
+	}
 
-			return this;
+	PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
 
-		},
+		constructor: PositionalAudio,
 
-		// Allows you to seek to a specific time in an animation.
-		setTime: function ( timeInSeconds ) {
+		getOutput: function () {
 
-			this.time = 0; // Zero out time attribute for AnimationMixer object;
-			for ( var i = 0; i < this._actions.length; i ++ ) {
+			return this.panner;
 
-				this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects.
+		},
 
-			}
+		getRefDistance: function () {
 
-			return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object.
+			return this.panner.refDistance;
 
 		},
 
-		// return this mixer's root target object
-		getRoot: function () {
+		setRefDistance: function ( value ) {
 
-			return this._root;
+			this.panner.refDistance = value;
+
+			return this;
 
 		},
 
-		// free all resources specific to a particular clip
-		uncacheClip: function ( clip ) {
+		getRolloffFactor: function () {
 
-			var actions = this._actions,
-				clipUuid = clip.uuid,
-				actionsByClip = this._actionsByClip,
-				actionsForClip = actionsByClip[ clipUuid ];
+			return this.panner.rolloffFactor;
 
-			if ( actionsForClip !== undefined ) {
+		},
 
-				// note: just calling _removeInactiveAction would mess up the
-				// iteration state and also require updating the state we can
-				// just throw away
+		setRolloffFactor: function ( value ) {
 
-				var actionsToRemove = actionsForClip.knownActions;
+			this.panner.rolloffFactor = value;
 
-				for ( var i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
+			return this;
 
-					var action = actionsToRemove[ i ];
+		},
 
-					this._deactivateAction( action );
+		getDistanceModel: function () {
 
-					var cacheIndex = action._cacheIndex,
-						lastInactiveAction = actions[ actions.length - 1 ];
+			return this.panner.distanceModel;
 
-					action._cacheIndex = null;
-					action._byClipCacheIndex = null;
+		},
 
-					lastInactiveAction._cacheIndex = cacheIndex;
-					actions[ cacheIndex ] = lastInactiveAction;
-					actions.pop();
+		setDistanceModel: function ( value ) {
 
-					this._removeInactiveBindingsForAction( action );
+			this.panner.distanceModel = value;
 
-				}
+			return this;
 
-				delete actionsByClip[ clipUuid ];
+		},
 
-			}
+		getMaxDistance: function () {
+
+			return this.panner.maxDistance;
 
 		},
 
-		// free all resources specific to a particular root target object
-		uncacheRoot: function ( root ) {
+		setMaxDistance: function ( value ) {
 
-			var rootUuid = root.uuid,
-				actionsByClip = this._actionsByClip;
+			this.panner.maxDistance = value;
 
-			for ( var clipUuid in actionsByClip ) {
+			return this;
 
-				var actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
-					action = actionByRoot[ rootUuid ];
+		},
 
-				if ( action !== undefined ) {
+		setDirectionalCone: function ( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
 
-					this._deactivateAction( action );
-					this._removeInactiveAction( action );
+			this.panner.coneInnerAngle = coneInnerAngle;
+			this.panner.coneOuterAngle = coneOuterAngle;
+			this.panner.coneOuterGain = coneOuterGain;
 
-				}
+			return this;
 
-			}
+		},
 
-			var bindingsByRoot = this._bindingsByRootAndName,
-				bindingByName = bindingsByRoot[ rootUuid ];
+		updateMatrixWorld: function ( force ) {
 
-			if ( bindingByName !== undefined ) {
+			Object3D.prototype.updateMatrixWorld.call( this, force );
 
-				for ( var trackName in bindingByName ) {
+			if ( this.hasPlaybackControl === true && this.isPlaying === false ) { return; }
 
-					var binding = bindingByName[ trackName ];
-					binding.restoreOriginalState();
-					this._removeInactiveBinding( binding );
+			this.matrixWorld.decompose( _position$3, _quaternion$4, _scale$2 );
 
-				}
+			_orientation$1.set( 0, 0, 1 ).applyQuaternion( _quaternion$4 );
 
-			}
+			var panner = this.panner;
 
-		},
+			if ( panner.positionX ) {
 
-		// remove a targeted clip from the cache
-		uncacheAction: function ( clip, optionalRoot ) {
+				// code path for Chrome and Firefox (see #14393)
 
-			var action = this.existingAction( clip, optionalRoot );
+				var endTime = this.context.currentTime + this.listener.timeDelta;
 
-			if ( action !== null ) {
+				panner.positionX.linearRampToValueAtTime( _position$3.x, endTime );
+				panner.positionY.linearRampToValueAtTime( _position$3.y, endTime );
+				panner.positionZ.linearRampToValueAtTime( _position$3.z, endTime );
+				panner.orientationX.linearRampToValueAtTime( _orientation$1.x, endTime );
+				panner.orientationY.linearRampToValueAtTime( _orientation$1.y, endTime );
+				panner.orientationZ.linearRampToValueAtTime( _orientation$1.z, endTime );
 
-				this._deactivateAction( action );
-				this._removeInactiveAction( action );
+			} else {
+
+				panner.setPosition( _position$3.x, _position$3.y, _position$3.z );
+				panner.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z );
 
 			}
 
@@ -54316,5711 +61945,8465 @@ function simpleEnd(buf) {
 	 * @author mrdoob / http://mrdoob.com/
 	 */
 
-	function Uniform( value ) {
-
-		if ( typeof value === 'string' ) {
+	function AudioAnalyser( audio, fftSize ) {
 
-			console.warn( 'THREE.Uniform: Type parameter is no longer needed.' );
-			value = arguments[ 1 ];
+		this.analyser = audio.context.createAnalyser();
+		this.analyser.fftSize = fftSize !== undefined ? fftSize : 2048;
 
-		}
+		this.data = new Uint8Array( this.analyser.frequencyBinCount );
 
-		this.value = value;
+		audio.getOutput().connect( this.analyser );
 
 	}
 
-	Uniform.prototype.clone = function () {
+	Object.assign( AudioAnalyser.prototype, {
 
-		return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() );
+		getFrequencyData: function () {
 
-	};
+			this.analyser.getByteFrequencyData( this.data );
 
-	/**
-	 * @author benaadams / https://twitter.com/ben_a_adams
-	 */
+			return this.data;
 
-	function InstancedInterleavedBuffer( array, stride, meshPerAttribute ) {
+		},
 
-		InterleavedBuffer.call( this, array, stride );
+		getAverageFrequency: function () {
 
-		this.meshPerAttribute = meshPerAttribute || 1;
+			var value = 0, data = this.getFrequencyData();
 
-	}
+			for ( var i = 0; i < data.length; i ++ ) {
 
-	InstancedInterleavedBuffer.prototype = Object.assign( Object.create( InterleavedBuffer.prototype ), {
-
-		constructor: InstancedInterleavedBuffer,
-
-		isInstancedInterleavedBuffer: true,
-
-		copy: function ( source ) {
-
-			InterleavedBuffer.prototype.copy.call( this, source );
+				value += data[ i ];
 
-			this.meshPerAttribute = source.meshPerAttribute;
+			}
 
-			return this;
+			return value / data.length;
 
 		}
 
 	} );
 
 	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author bhouston / http://clara.io/
-	 * @author stephomi / http://stephaneginier.com/
+	 *
+	 * Buffered scene graph property that allows weighted accumulation.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
 	 */
 
-	function Raycaster( origin, direction, near, far ) {
-
-		this.ray = new Ray( origin, direction );
-		// direction is assumed to be normalized (for accurate distance calculations)
+	function PropertyMixer( binding, typeName, valueSize ) {
 
-		this.near = near || 0;
-		this.far = far || Infinity;
-		this.camera = null;
-		this.layers = new Layers();
+		this.binding = binding;
+		this.valueSize = valueSize;
 
-		this.params = {
-			Mesh: {},
-			Line: { threshold: 1 },
-			LOD: {},
-			Points: { threshold: 1 },
-			Sprite: {}
-		};
+		var mixFunction,
+			mixFunctionAdditive,
+			setIdentity;
 
-		Object.defineProperties( this.params, {
-			PointCloud: {
-				get: function () {
+		// buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ]
+		//
+		// interpolators can use .buffer as their .result
+		// the data then goes to 'incoming'
+		//
+		// 'accu0' and 'accu1' are used frame-interleaved for
+		// the cumulative result and are compared to detect
+		// changes
+		//
+		// 'orig' stores the original state of the property
+		//
+		// 'add' is used for additive cumulative results
+		//
+		// 'work' is optional and is only present for quaternion types. It is used
+		// to store intermediate quaternion multiplication results
 
-					console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
-					return this.Points;
+		switch ( typeName ) {
 
-				}
-			}
-		} );
+			case 'quaternion':
+				mixFunction = this._slerp;
+				mixFunctionAdditive = this._slerpAdditive;
+				setIdentity = this._setAdditiveIdentityQuaternion;
 
-	}
+				this.buffer = new Float64Array( valueSize * 6 );
+				this._workIndex = 5;
+				break;
 
-	function ascSort( a, b ) {
+			case 'string':
+			case 'bool':
+				mixFunction = this._select;
 
-		return a.distance - b.distance;
+				// Use the regular mix function and for additive on these types,
+				// additive is not relevant for non-numeric types
+				mixFunctionAdditive = this._select;
 
-	}
+				setIdentity = this._setAdditiveIdentityOther;
 
-	function intersectObject( object, raycaster, intersects, recursive ) {
+				this.buffer = new Array( valueSize * 5 );
+				break;
 
-		if ( object.layers.test( raycaster.layers ) ) {
+			default:
+				mixFunction = this._lerp;
+				mixFunctionAdditive = this._lerpAdditive;
+				setIdentity = this._setAdditiveIdentityNumeric;
 
-			object.raycast( raycaster, intersects );
+				this.buffer = new Float64Array( valueSize * 5 );
 
 		}
 
-		if ( recursive === true ) {
-
-			var children = object.children;
-
-			for ( var i = 0, l = children.length; i < l; i ++ ) {
-
-				intersectObject( children[ i ], raycaster, intersects, true );
+		this._mixBufferRegion = mixFunction;
+		this._mixBufferRegionAdditive = mixFunctionAdditive;
+		this._setIdentity = setIdentity;
+		this._origIndex = 3;
+		this._addIndex = 4;
 
-			}
+		this.cumulativeWeight = 0;
+		this.cumulativeWeightAdditive = 0;
 
-		}
+		this.useCount = 0;
+		this.referenceCount = 0;
 
 	}
 
-	Object.assign( Raycaster.prototype, {
+	Object.assign( PropertyMixer.prototype, {
 
-		set: function ( origin, direction ) {
+		// accumulate data in the 'incoming' region into 'accu<i>'
+		accumulate: function ( accuIndex, weight ) {
 
-			// direction is assumed to be normalized (for accurate distance calculations)
+			// note: happily accumulating nothing when weight = 0, the caller knows
+			// the weight and shouldn't have made the call in the first place
 
-			this.ray.set( origin, direction );
+			var buffer = this.buffer,
+				stride = this.valueSize,
+				offset = accuIndex * stride + stride,
 
-		},
+				currentWeight = this.cumulativeWeight;
 
-		setFromCamera: function ( coords, camera ) {
+			if ( currentWeight === 0 ) {
 
-			if ( ( camera && camera.isPerspectiveCamera ) ) {
+				// accuN := incoming * weight
 
-				this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
-				this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
-				this.camera = camera;
+				for ( var i = 0; i !== stride; ++ i ) {
 
-			} else if ( ( camera && camera.isOrthographicCamera ) ) {
+					buffer[ offset + i ] = buffer[ i ];
 
-				this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
-				this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
-				this.camera = camera;
+				}
+
+				currentWeight = weight;
 
 			} else {
 
-				console.error( 'THREE.Raycaster: Unsupported camera type.' );
+				// accuN := accuN + incoming * weight
+
+				currentWeight += weight;
+				var mix = weight / currentWeight;
+				this._mixBufferRegion( buffer, offset, 0, mix, stride );
 
 			}
 
+			this.cumulativeWeight = currentWeight;
+
 		},
 
-		intersectObject: function ( object, recursive, optionalTarget ) {
+		// accumulate data in the 'incoming' region into 'add'
+		accumulateAdditive: function ( weight ) {
 
-			var intersects = optionalTarget || [];
+			var buffer = this.buffer,
+				stride = this.valueSize,
+				offset = stride * this._addIndex;
 
-			intersectObject( object, this, intersects, recursive );
+			if ( this.cumulativeWeightAdditive === 0 ) {
 
-			intersects.sort( ascSort );
+				// add = identity
 
-			return intersects;
+				this._setIdentity();
 
-		},
+			}
 
-		intersectObjects: function ( objects, recursive, optionalTarget ) {
+			// add := add + incoming * weight
 
-			var intersects = optionalTarget || [];
+			this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride );
+			this.cumulativeWeightAdditive += weight;
 
-			if ( Array.isArray( objects ) === false ) {
+		},
 
-				console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
-				return intersects;
+		// apply the state of 'accu<i>' to the binding when accus differ
+		apply: function ( accuIndex ) {
 
-			}
+			var stride = this.valueSize,
+				buffer = this.buffer,
+				offset = accuIndex * stride + stride,
 
-			for ( var i = 0, l = objects.length; i < l; i ++ ) {
+				weight = this.cumulativeWeight,
+				weightAdditive = this.cumulativeWeightAdditive,
 
-				intersectObject( objects[ i ], this, intersects, recursive );
+				binding = this.binding;
 
-			}
+			this.cumulativeWeight = 0;
+			this.cumulativeWeightAdditive = 0;
 
-			intersects.sort( ascSort );
+			if ( weight < 1 ) {
 
-			return intersects;
+				// accuN := accuN + original * ( 1 - cumulativeWeight )
 
-		}
+				var originalValueOffset = stride * this._origIndex;
 
-	} );
+				this._mixBufferRegion(
+					buffer, offset, originalValueOffset, 1 - weight, stride );
 
-	/**
-	 * @author bhouston / http://clara.io
-	 * @author WestLangley / http://github.com/WestLangley
-	 *
-	 * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
-	 *
-	 * The polar angle (phi) is measured from the positive y-axis. The positive y-axis is up.
-	 * The azimuthal angle (theta) is measured from the positive z-axis.
-	 */
+			}
 
-	function Spherical( radius, phi, theta ) {
+			if ( weightAdditive > 0 ) {
 
-		this.radius = ( radius !== undefined ) ? radius : 1.0;
-		this.phi = ( phi !== undefined ) ? phi : 0; // polar angle
-		this.theta = ( theta !== undefined ) ? theta : 0; // azimuthal angle
+				// accuN := accuN + additive accuN
 
-		return this;
+				this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride );
 
-	}
+			}
 
-	Object.assign( Spherical.prototype, {
+			for ( var i = stride, e = stride + stride; i !== e; ++ i ) {
 
-		set: function ( radius, phi, theta ) {
+				if ( buffer[ i ] !== buffer[ i + stride ] ) {
 
-			this.radius = radius;
-			this.phi = phi;
-			this.theta = theta;
+					// value has changed -> update scene graph
 
-			return this;
+					binding.setValue( buffer, offset );
+					break;
+
+				}
+
+			}
 
 		},
 
-		clone: function () {
+		// remember the state of the bound property and copy it to both accus
+		saveOriginalState: function () {
 
-			return new this.constructor().copy( this );
+			var binding = this.binding;
 
-		},
+			var buffer = this.buffer,
+				stride = this.valueSize,
 
-		copy: function ( other ) {
+				originalValueOffset = stride * this._origIndex;
 
-			this.radius = other.radius;
-			this.phi = other.phi;
-			this.theta = other.theta;
+			binding.getValue( buffer, originalValueOffset );
 
-			return this;
+			// accu[0..1] := orig -- initially detect changes against the original
+			for ( var i = stride, e = originalValueOffset; i !== e; ++ i ) {
 
-		},
+				buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ];
 
-		// restrict phi to be betwee EPS and PI-EPS
-		makeSafe: function () {
+			}
 
-			var EPS = 0.000001;
-			this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
+			// Add to identity for additive
+			this._setIdentity();
 
-			return this;
+			this.cumulativeWeight = 0;
+			this.cumulativeWeightAdditive = 0;
 
 		},
 
-		setFromVector3: function ( v ) {
+		// apply the state previously taken via 'saveOriginalState' to the binding
+		restoreOriginalState: function () {
 
-			return this.setFromCartesianCoords( v.x, v.y, v.z );
+			var originalValueOffset = this.valueSize * 3;
+			this.binding.setValue( this.buffer, originalValueOffset );
 
 		},
 
-		setFromCartesianCoords: function ( x, y, z ) {
+		_setAdditiveIdentityNumeric: function () {
 
-			this.radius = Math.sqrt( x * x + y * y + z * z );
+			var startIndex = this._addIndex * this.valueSize;
 
-			if ( this.radius === 0 ) {
+			this.buffer.fill( 0, startIndex, startIndex + this.valueSize );
 
-				this.theta = 0;
-				this.phi = 0;
+		},
 
-			} else {
+		_setAdditiveIdentityQuaternion: function () {
 
-				this.theta = Math.atan2( x, z );
-				this.phi = Math.acos( MathUtils.clamp( y / this.radius, - 1, 1 ) );
+			this._setAdditiveIdentityNumeric();
+			this.buffer[ this._addIndex * 4 + 3 ] = 1;
 
-			}
+		},
 
-			return this;
+		_setAdditiveIdentityOther: function () {
 
-		}
+			var startIndex = this._origIndex * this.valueSize;
+			var targetIndex = this._addIndex * this.valueSize;
 
-	} );
+			this.buffer.copyWithin( targetIndex, startIndex, this.valueSize );
 
-	/**
-	 * @author Mugen87 / https://github.com/Mugen87
-	 *
-	 * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system
-	 *
-	 */
+		},
 
-	function Cylindrical( radius, theta, y ) {
 
-		this.radius = ( radius !== undefined ) ? radius : 1.0; // distance from the origin to a point in the x-z plane
-		this.theta = ( theta !== undefined ) ? theta : 0; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis
-		this.y = ( y !== undefined ) ? y : 0; // height above the x-z plane
+		// mix functions
 
-		return this;
+		_select: function ( buffer, dstOffset, srcOffset, t, stride ) {
 
-	}
+			if ( t >= 0.5 ) {
 
-	Object.assign( Cylindrical.prototype, {
+				for ( var i = 0; i !== stride; ++ i ) {
 
-		set: function ( radius, theta, y ) {
+					buffer[ dstOffset + i ] = buffer[ srcOffset + i ];
 
-			this.radius = radius;
-			this.theta = theta;
-			this.y = y;
+				}
 
-			return this;
+			}
 
 		},
 
-		clone: function () {
+		_slerp: function ( buffer, dstOffset, srcOffset, t ) {
 
-			return new this.constructor().copy( this );
+			Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t );
 
 		},
 
-		copy: function ( other ) {
+		_slerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) {
 
-			this.radius = other.radius;
-			this.theta = other.theta;
-			this.y = other.y;
+			var workOffset = this._workIndex * stride;
 
-			return this;
+			// Store result in intermediate buffer offset
+			Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset );
+
+			// Slerp to the intermediate result
+			Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t );
 
 		},
 
-		setFromVector3: function ( v ) {
+		_lerp: function ( buffer, dstOffset, srcOffset, t, stride ) {
 
-			return this.setFromCartesianCoords( v.x, v.y, v.z );
+			var s = 1 - t;
+
+			for ( var i = 0; i !== stride; ++ i ) {
+
+				var j = dstOffset + i;
+
+				buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t;
+
+			}
 
 		},
 
-		setFromCartesianCoords: function ( x, y, z ) {
+		_lerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) {
 
-			this.radius = Math.sqrt( x * x + z * z );
-			this.theta = Math.atan2( x, z );
-			this.y = y;
+			for ( var i = 0; i !== stride; ++ i ) {
 
-			return this;
+				var j = dstOffset + i;
+
+				buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t;
+
+			}
 
 		}
 
 	} );
 
 	/**
-	 * @author bhouston / http://clara.io
+	 *
+	 * A reference to a real property in the scene graph.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
 	 */
 
-	var _vector$7 = new Vector2();
-
-	function Box2( min, max ) {
-
-		this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
-		this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );
+	// Characters [].:/ are reserved for track binding syntax.
+	var _RESERVED_CHARS_RE = '\\[\\]\\.:\\/';
+	var _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' );
 
-	}
+	// Attempts to allow node names from any language. ES5's `\w` regexp matches
+	// only latin characters, and the unicode \p{L} is not yet supported. So
+	// instead, we exclude reserved characters and match everything else.
+	var _wordChar = '[^' + _RESERVED_CHARS_RE + ']';
+	var _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']';
 
-	Object.assign( Box2.prototype, {
+	// Parent directories, delimited by '/' or ':'. Currently unused, but must
+	// be matched to parse the rest of the track name.
+	var _directoryRe = /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar );
 
-		set: function ( min, max ) {
+	// Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'.
+	var _nodeRe = /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot );
 
-			this.min.copy( min );
-			this.max.copy( max );
+	// Object on target node, and accessor. May not contain reserved
+	// characters. Accessor may contain any character except closing bracket.
+	var _objectRe = /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar );
 
-			return this;
+	// Property and accessor. May not contain reserved characters. Accessor may
+	// contain any non-bracket characters.
+	var _propertyRe = /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar );
 
-		},
+	var _trackRe = new RegExp( ''
+		+ '^'
+		+ _directoryRe
+		+ _nodeRe
+		+ _objectRe
+		+ _propertyRe
+		+ '$'
+	);
 
-		setFromPoints: function ( points ) {
+	var _supportedObjectNames = [ 'material', 'materials', 'bones' ];
 
-			this.makeEmpty();
+	function Composite( targetGroup, path, optionalParsedPath ) {
 
-			for ( var i = 0, il = points.length; i < il; i ++ ) {
+		var parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
 
-				this.expandByPoint( points[ i ] );
+		this._targetGroup = targetGroup;
+		this._bindings = targetGroup.subscribe_( path, parsedPath );
 
-			}
+	}
 
-			return this;
+	Object.assign( Composite.prototype, {
 
-		},
+		getValue: function ( array, offset ) {
 
-		setFromCenterAndSize: function ( center, size ) {
+			this.bind(); // bind all binding
 
-			var halfSize = _vector$7.copy( size ).multiplyScalar( 0.5 );
-			this.min.copy( center ).sub( halfSize );
-			this.max.copy( center ).add( halfSize );
+			var firstValidIndex = this._targetGroup.nCachedObjects_,
+				binding = this._bindings[ firstValidIndex ];
 
-			return this;
+			// and only call .getValue on the first
+			if ( binding !== undefined ) { binding.getValue( array, offset ); }
 
 		},
 
-		clone: function () {
-
-			return new this.constructor().copy( this );
+		setValue: function ( array, offset ) {
 
-		},
+			var bindings = this._bindings;
 
-		copy: function ( box ) {
+			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
 
-			this.min.copy( box.min );
-			this.max.copy( box.max );
+				bindings[ i ].setValue( array, offset );
 
-			return this;
+			}
 
 		},
 
-		makeEmpty: function () {
-
-			this.min.x = this.min.y = + Infinity;
-			this.max.x = this.max.y = - Infinity;
-
-			return this;
+		bind: function () {
 
-		},
+			var bindings = this._bindings;
 
-		isEmpty: function () {
+			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
 
-			// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
+				bindings[ i ].bind();
 
-			return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
+			}
 
 		},
 
-		getCenter: function ( target ) {
-
-			if ( target === undefined ) {
-
-				console.warn( 'THREE.Box2: .getCenter() target is now required' );
-				target = new Vector2();
+		unbind: function () {
 
-			}
+			var bindings = this._bindings;
 
-			return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
+			for ( var i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
 
-		},
+				bindings[ i ].unbind();
 
-		getSize: function ( target ) {
+			}
 
-			if ( target === undefined ) {
+		}
 
-				console.warn( 'THREE.Box2: .getSize() target is now required' );
-				target = new Vector2();
+	} );
 
-			}
 
-			return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
+	function PropertyBinding( rootNode, path, parsedPath ) {
 
-		},
+		this.path = path;
+		this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
 
-		expandByPoint: function ( point ) {
+		this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode;
 
-			this.min.min( point );
-			this.max.max( point );
+		this.rootNode = rootNode;
 
-			return this;
+	}
 
-		},
+	Object.assign( PropertyBinding, {
 
-		expandByVector: function ( vector ) {
+		Composite: Composite,
 
-			this.min.sub( vector );
-			this.max.add( vector );
+		create: function ( root, path, parsedPath ) {
 
-			return this;
+			if ( ! ( root && root.isAnimationObjectGroup ) ) {
 
-		},
+				return new PropertyBinding( root, path, parsedPath );
 
-		expandByScalar: function ( scalar ) {
+			} else {
 
-			this.min.addScalar( - scalar );
-			this.max.addScalar( scalar );
+				return new PropertyBinding.Composite( root, path, parsedPath );
 
-			return this;
+			}
 
 		},
 
-		containsPoint: function ( point ) {
-
-			return point.x < this.min.x || point.x > this.max.x ||
-				point.y < this.min.y || point.y > this.max.y ? false : true;
-
-		},
-
-		containsBox: function ( box ) {
+		/**
+		 * Replaces spaces with underscores and removes unsupported characters from
+		 * node names, to ensure compatibility with parseTrackName().
+		 *
+		 * @param {string} name Node name to be sanitized.
+		 * @return {string}
+		 */
+		sanitizeNodeName: function ( name ) {
 
-			return this.min.x <= box.min.x && box.max.x <= this.max.x &&
-				this.min.y <= box.min.y && box.max.y <= this.max.y;
+			return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
 
 		},
 
-		getParameter: function ( point, target ) {
+		parseTrackName: function ( trackName ) {
 
-			// This can potentially have a divide by zero if the box
-			// has a size dimension of 0.
+			var matches = _trackRe.exec( trackName );
 
-			if ( target === undefined ) {
+			if ( ! matches ) {
 
-				console.warn( 'THREE.Box2: .getParameter() target is now required' );
-				target = new Vector2();
+				throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName );
 
 			}
 
-			return target.set(
-				( point.x - this.min.x ) / ( this.max.x - this.min.x ),
-				( point.y - this.min.y ) / ( this.max.y - this.min.y )
-			);
-
-		},
-
-		intersectsBox: function ( box ) {
+			var results = {
+				// directoryName: matches[ 1 ], // (tschw) currently unused
+				nodeName: matches[ 2 ],
+				objectName: matches[ 3 ],
+				objectIndex: matches[ 4 ],
+				propertyName: matches[ 5 ], // required
+				propertyIndex: matches[ 6 ]
+			};
 
-			// using 4 splitting planes to rule out intersections
+			var lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' );
 
-			return box.max.x < this.min.x || box.min.x > this.max.x ||
-				box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
+			if ( lastDot !== undefined && lastDot !== - 1 ) {
 
-		},
+				var objectName = results.nodeName.substring( lastDot + 1 );
 
-		clampPoint: function ( point, target ) {
+				// Object names must be checked against a whitelist. Otherwise, there
+				// is no way to parse 'foo.bar.baz': 'baz' must be a property, but
+				// 'bar' could be the objectName, or part of a nodeName (which can
+				// include '.' characters).
+				if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) {
 
-			if ( target === undefined ) {
+					results.nodeName = results.nodeName.substring( 0, lastDot );
+					results.objectName = objectName;
 
-				console.warn( 'THREE.Box2: .clampPoint() target is now required' );
-				target = new Vector2();
+				}
 
 			}
 
-			return target.copy( point ).clamp( this.min, this.max );
+			if ( results.propertyName === null || results.propertyName.length === 0 ) {
 
-		},
+				throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName );
 
-		distanceToPoint: function ( point ) {
+			}
 
-			var clampedPoint = _vector$7.copy( point ).clamp( this.min, this.max );
-			return clampedPoint.sub( point ).length();
+			return results;
 
 		},
 
-		intersect: function ( box ) {
+		findNode: function ( root, nodeName ) {
 
-			this.min.max( box.min );
-			this.max.min( box.max );
+			if ( ! nodeName || nodeName === "" || nodeName === "." || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
 
-			return this;
+				return root;
 
-		},
+			}
 
-		union: function ( box ) {
+			// search into skeleton bones.
+			if ( root.skeleton ) {
 
-			this.min.min( box.min );
-			this.max.max( box.max );
+				var bone = root.skeleton.getBoneByName( nodeName );
 
-			return this;
+				if ( bone !== undefined ) {
 
-		},
+					return bone;
 
-		translate: function ( offset ) {
+				}
 
-			this.min.add( offset );
-			this.max.add( offset );
+			}
 
-			return this;
+			// search into node subtree.
+			if ( root.children ) {
 
-		},
+				var searchNodeSubtree = function ( children ) {
 
-		equals: function ( box ) {
+					for ( var i = 0; i < children.length; i ++ ) {
 
-			return box.min.equals( this.min ) && box.max.equals( this.max );
+						var childNode = children[ i ];
 
-		}
+						if ( childNode.name === nodeName || childNode.uuid === nodeName ) {
 
-	} );
+							return childNode;
 
-	/**
-	 * @author bhouston / http://clara.io
-	 */
+						}
 
-	var _startP = new Vector3();
-	var _startEnd = new Vector3();
+						var result = searchNodeSubtree( childNode.children );
 
-	function Line3( start, end ) {
+						if ( result ) { return result; }
 
-		this.start = ( start !== undefined ) ? start : new Vector3();
-		this.end = ( end !== undefined ) ? end : new Vector3();
+					}
 
-	}
+					return null;
 
-	Object.assign( Line3.prototype, {
+				};
 
-		set: function ( start, end ) {
+				var subTreeNode = searchNodeSubtree( root.children );
 
-			this.start.copy( start );
-			this.end.copy( end );
+				if ( subTreeNode ) {
 
-			return this;
+					return subTreeNode;
 
-		},
+				}
 
-		clone: function () {
+			}
 
-			return new this.constructor().copy( this );
+			return null;
 
-		},
+		}
 
-		copy: function ( line ) {
+	} );
 
-			this.start.copy( line.start );
-			this.end.copy( line.end );
+	Object.assign( PropertyBinding.prototype, { // prototype, continued
 
-			return this;
+		// these are used to "bind" a nonexistent property
+		_getValue_unavailable: function () {},
+		_setValue_unavailable: function () {},
 
+		BindingType: {
+			Direct: 0,
+			EntireArray: 1,
+			ArrayElement: 2,
+			HasFromToArray: 3
 		},
 
-		getCenter: function ( target ) {
-
-			if ( target === undefined ) {
+		Versioning: {
+			None: 0,
+			NeedsUpdate: 1,
+			MatrixWorldNeedsUpdate: 2
+		},
 
-				console.warn( 'THREE.Line3: .getCenter() target is now required' );
-				target = new Vector3();
+		GetterByBindingType: [
 
-			}
+			function getValue_direct( buffer, offset ) {
 
-			return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
+				buffer[ offset ] = this.node[ this.propertyName ];
 
-		},
+			},
 
-		delta: function ( target ) {
+			function getValue_array( buffer, offset ) {
 
-			if ( target === undefined ) {
+				var source = this.resolvedProperty;
 
-				console.warn( 'THREE.Line3: .delta() target is now required' );
-				target = new Vector3();
+				for ( var i = 0, n = source.length; i !== n; ++ i ) {
 
-			}
+					buffer[ offset ++ ] = source[ i ];
 
-			return target.subVectors( this.end, this.start );
+				}
 
-		},
+			},
 
-		distanceSq: function () {
+			function getValue_arrayElement( buffer, offset ) {
 
-			return this.start.distanceToSquared( this.end );
+				buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
 
-		},
+			},
 
-		distance: function () {
+			function getValue_toArray( buffer, offset ) {
 
-			return this.start.distanceTo( this.end );
+				this.resolvedProperty.toArray( buffer, offset );
 
-		},
+			}
 
-		at: function ( t, target ) {
+		],
 
-			if ( target === undefined ) {
+		SetterByBindingTypeAndVersioning: [
 
-				console.warn( 'THREE.Line3: .at() target is now required' );
-				target = new Vector3();
+			[
+				// Direct
 
-			}
+				function setValue_direct( buffer, offset ) {
 
-			return this.delta( target ).multiplyScalar( t ).add( this.start );
+					this.targetObject[ this.propertyName ] = buffer[ offset ];
 
-		},
+				},
 
-		closestPointToPointParameter: function ( point, clampToLine ) {
+				function setValue_direct_setNeedsUpdate( buffer, offset ) {
 
-			_startP.subVectors( point, this.start );
-			_startEnd.subVectors( this.end, this.start );
+					this.targetObject[ this.propertyName ] = buffer[ offset ];
+					this.targetObject.needsUpdate = true;
 
-			var startEnd2 = _startEnd.dot( _startEnd );
-			var startEnd_startP = _startEnd.dot( _startP );
+				},
 
-			var t = startEnd_startP / startEnd2;
+				function setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
-			if ( clampToLine ) {
+					this.targetObject[ this.propertyName ] = buffer[ offset ];
+					this.targetObject.matrixWorldNeedsUpdate = true;
 
-				t = MathUtils.clamp( t, 0, 1 );
+				}
 
-			}
+			], [
 
-			return t;
+				// EntireArray
 
-		},
+				function setValue_array( buffer, offset ) {
 
-		closestPointToPoint: function ( point, clampToLine, target ) {
+					var dest = this.resolvedProperty;
 
-			var t = this.closestPointToPointParameter( point, clampToLine );
+					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
 
-			if ( target === undefined ) {
+						dest[ i ] = buffer[ offset ++ ];
 
-				console.warn( 'THREE.Line3: .closestPointToPoint() target is now required' );
-				target = new Vector3();
+					}
 
-			}
+				},
 
-			return this.delta( target ).multiplyScalar( t ).add( this.start );
+				function setValue_array_setNeedsUpdate( buffer, offset ) {
 
-		},
+					var dest = this.resolvedProperty;
 
-		applyMatrix4: function ( matrix ) {
+					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
 
-			this.start.applyMatrix4( matrix );
-			this.end.applyMatrix4( matrix );
+						dest[ i ] = buffer[ offset ++ ];
 
-			return this;
+					}
 
-		},
+					this.targetObject.needsUpdate = true;
 
-		equals: function ( line ) {
+				},
 
-			return line.start.equals( this.start ) && line.end.equals( this.end );
+				function setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
-		}
+					var dest = this.resolvedProperty;
 
-	} );
+					for ( var i = 0, n = dest.length; i !== n; ++ i ) {
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 */
+						dest[ i ] = buffer[ offset ++ ];
 
-	function ImmediateRenderObject( material ) {
+					}
 
-		Object3D.call( this );
+					this.targetObject.matrixWorldNeedsUpdate = true;
 
-		this.material = material;
-		this.render = function ( /* renderCallback */ ) {};
+				}
 
-	}
+			], [
 
-	ImmediateRenderObject.prototype = Object.create( Object3D.prototype );
-	ImmediateRenderObject.prototype.constructor = ImmediateRenderObject;
+				// ArrayElement
 
-	ImmediateRenderObject.prototype.isImmediateRenderObject = true;
+				function setValue_arrayElement( buffer, offset ) {
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
 
-	var _vector$8 = new Vector3();
+				},
 
-	function SpotLightHelper( light, color ) {
+				function setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
 
-		Object3D.call( this );
+					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+					this.targetObject.needsUpdate = true;
 
-		this.light = light;
-		this.light.updateMatrixWorld();
+				},
 
-		this.matrix = light.matrixWorld;
-		this.matrixAutoUpdate = false;
+				function setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
-		this.color = color;
+					this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
+					this.targetObject.matrixWorldNeedsUpdate = true;
 
-		var geometry = new BufferGeometry();
+				}
 
-		var positions = [
-			0, 0, 0, 	0, 0, 1,
-			0, 0, 0, 	1, 0, 1,
-			0, 0, 0,	- 1, 0, 1,
-			0, 0, 0, 	0, 1, 1,
-			0, 0, 0, 	0, - 1, 1
-		];
+			], [
 
-		for ( var i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
+				// HasToFromArray
 
-			var p1 = ( i / l ) * Math.PI * 2;
-			var p2 = ( j / l ) * Math.PI * 2;
+				function setValue_fromArray( buffer, offset ) {
 
-			positions.push(
-				Math.cos( p1 ), Math.sin( p1 ), 1,
-				Math.cos( p2 ), Math.sin( p2 ), 1
-			);
+					this.resolvedProperty.fromArray( buffer, offset );
 
-		}
+				},
 
-		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
+				function setValue_fromArray_setNeedsUpdate( buffer, offset ) {
 
-		var material = new LineBasicMaterial( { fog: false, toneMapped: false } );
+					this.resolvedProperty.fromArray( buffer, offset );
+					this.targetObject.needsUpdate = true;
 
-		this.cone = new LineSegments( geometry, material );
-		this.add( this.cone );
+				},
 
-		this.update();
+				function setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
 
-	}
+					this.resolvedProperty.fromArray( buffer, offset );
+					this.targetObject.matrixWorldNeedsUpdate = true;
 
-	SpotLightHelper.prototype = Object.create( Object3D.prototype );
-	SpotLightHelper.prototype.constructor = SpotLightHelper;
+				}
 
-	SpotLightHelper.prototype.dispose = function () {
+			]
 
-		this.cone.geometry.dispose();
-		this.cone.material.dispose();
+		],
 
-	};
+		getValue: function getValue_unbound( targetArray, offset ) {
 
-	SpotLightHelper.prototype.update = function () {
+			this.bind();
+			this.getValue( targetArray, offset );
 
-		this.light.updateMatrixWorld();
+			// Note: This class uses a State pattern on a per-method basis:
+			// 'bind' sets 'this.getValue' / 'setValue' and shadows the
+			// prototype version of these methods with one that represents
+			// the bound state. When the property is not found, the methods
+			// become no-ops.
 
-		var coneLength = this.light.distance ? this.light.distance : 1000;
-		var coneWidth = coneLength * Math.tan( this.light.angle );
+		},
 
-		this.cone.scale.set( coneWidth, coneWidth, coneLength );
+		setValue: function getValue_unbound( sourceArray, offset ) {
 
-		_vector$8.setFromMatrixPosition( this.light.target.matrixWorld );
+			this.bind();
+			this.setValue( sourceArray, offset );
 
-		this.cone.lookAt( _vector$8 );
+		},
 
-		if ( this.color !== undefined ) {
+		// create getter / setter pair for a property in the scene graph
+		bind: function () {
 
-			this.cone.material.color.set( this.color );
+			var targetObject = this.node,
+				parsedPath = this.parsedPath,
 
-		} else {
+				objectName = parsedPath.objectName,
+				propertyName = parsedPath.propertyName,
+				propertyIndex = parsedPath.propertyIndex;
 
-			this.cone.material.color.copy( this.light.color );
+			if ( ! targetObject ) {
 
-		}
+				targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName ) || this.rootNode;
 
-	};
+				this.node = targetObject;
 
-	/**
-	 * @author Sean Griffin / http://twitter.com/sgrif
-	 * @author Michael Guerrero / http://realitymeltdown.com
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author ikerr / http://verold.com
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+			}
 
-	var _vector$9 = new Vector3();
-	var _boneMatrix = new Matrix4();
-	var _matrixWorldInv = new Matrix4();
+			// set fail state so we can just 'return' on error
+			this.getValue = this._getValue_unavailable;
+			this.setValue = this._setValue_unavailable;
 
-	function getBoneList( object ) {
+			// ensure there is a value node
+			if ( ! targetObject ) {
 
-		var boneList = [];
+				console.error( 'THREE.PropertyBinding: Trying to update node for track: ' + this.path + ' but it wasn\'t found.' );
+				return;
 
-		if ( object && object.isBone ) {
+			}
 
-			boneList.push( object );
+			if ( objectName ) {
 
-		}
+				var objectIndex = parsedPath.objectIndex;
 
-		for ( var i = 0; i < object.children.length; i ++ ) {
+				// special cases were we need to reach deeper into the hierarchy to get the face materials....
+				switch ( objectName ) {
 
-			boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
+					case 'materials':
 
-		}
+						if ( ! targetObject.material ) {
 
-		return boneList;
+							console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
+							return;
 
-	}
+						}
 
-	function SkeletonHelper( object ) {
+						if ( ! targetObject.material.materials ) {
 
-		var bones = getBoneList( object );
+							console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this );
+							return;
 
-		var geometry = new BufferGeometry();
+						}
 
-		var vertices = [];
-		var colors = [];
+						targetObject = targetObject.material.materials;
 
-		var color1 = new Color( 0, 0, 1 );
-		var color2 = new Color( 0, 1, 0 );
+						break;
 
-		for ( var i = 0; i < bones.length; i ++ ) {
+					case 'bones':
 
-			var bone = bones[ i ];
+						if ( ! targetObject.skeleton ) {
 
-			if ( bone.parent && bone.parent.isBone ) {
+							console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this );
+							return;
 
-				vertices.push( 0, 0, 0 );
-				vertices.push( 0, 0, 0 );
-				colors.push( color1.r, color1.g, color1.b );
-				colors.push( color2.r, color2.g, color2.b );
+						}
 
-			}
+						// potential future optimization: skip this if propertyIndex is already an integer
+						// and convert the integer string to a true integer.
 
-		}
+						targetObject = targetObject.skeleton.bones;
 
-		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
+						// support resolving morphTarget names into indices.
+						for ( var i = 0; i < targetObject.length; i ++ ) {
 
-		var material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } );
+							if ( targetObject[ i ].name === objectIndex ) {
 
-		LineSegments.call( this, geometry, material );
+								objectIndex = i;
+								break;
 
-		this.type = 'SkeletonHelper';
+							}
 
-		this.root = object;
-		this.bones = bones;
+						}
 
-		this.matrix = object.matrixWorld;
-		this.matrixAutoUpdate = false;
+						break;
 
-	}
+					default:
 
-	SkeletonHelper.prototype = Object.create( LineSegments.prototype );
-	SkeletonHelper.prototype.constructor = SkeletonHelper;
+						if ( targetObject[ objectName ] === undefined ) {
 
-	SkeletonHelper.prototype.isSkeletonHelper = true;
+							console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this );
+							return;
 
-	SkeletonHelper.prototype.updateMatrixWorld = function ( force ) {
+						}
 
-		var bones = this.bones;
+						targetObject = targetObject[ objectName ];
 
-		var geometry = this.geometry;
-		var position = geometry.getAttribute( 'position' );
+				}
 
-		_matrixWorldInv.getInverse( this.root.matrixWorld );
 
-		for ( var i = 0, j = 0; i < bones.length; i ++ ) {
+				if ( objectIndex !== undefined ) {
 
-			var bone = bones[ i ];
+					if ( targetObject[ objectIndex ] === undefined ) {
 
-			if ( bone.parent && bone.parent.isBone ) {
+						console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject );
+						return;
 
-				_boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld );
-				_vector$9.setFromMatrixPosition( _boneMatrix );
-				position.setXYZ( j, _vector$9.x, _vector$9.y, _vector$9.z );
+					}
 
-				_boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld );
-				_vector$9.setFromMatrixPosition( _boneMatrix );
-				position.setXYZ( j + 1, _vector$9.x, _vector$9.y, _vector$9.z );
+					targetObject = targetObject[ objectIndex ];
 
-				j += 2;
+				}
 
 			}
 
-		}
-
-		geometry.getAttribute( 'position' ).needsUpdate = true;
+			// resolve property
+			var nodeProperty = targetObject[ propertyName ];
 
-		Object3D.prototype.updateMatrixWorld.call( this, force );
+			if ( nodeProperty === undefined ) {
 
-	};
+				var nodeName = parsedPath.nodeName;
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+				console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName +
+					'.' + propertyName + ' but it wasn\'t found.', targetObject );
+				return;
 
-	function PointLightHelper( light, sphereSize, color ) {
+			}
 
-		this.light = light;
-		this.light.updateMatrixWorld();
+			// determine versioning scheme
+			var versioning = this.Versioning.None;
 
-		this.color = color;
+			this.targetObject = targetObject;
 
-		var geometry = new SphereBufferGeometry( sphereSize, 4, 2 );
-		var material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
+			if ( targetObject.needsUpdate !== undefined ) { // material
 
-		Mesh.call( this, geometry, material );
+				versioning = this.Versioning.NeedsUpdate;
 
-		this.type = 'PointLightHelper';
+			} else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform
 
-		this.matrix = this.light.matrixWorld;
-		this.matrixAutoUpdate = false;
+				versioning = this.Versioning.MatrixWorldNeedsUpdate;
 
-		this.update();
+			}
 
+			// determine how the property gets bound
+			var bindingType = this.BindingType.Direct;
 
-		/*
-		var distanceGeometry = new THREE.IcosahedronBufferGeometry( 1, 2 );
-		var distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } );
+			if ( propertyIndex !== undefined ) {
 
-		this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
-		this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial );
+				// access a sub element of the property array (only primitives are supported right now)
 
-		var d = light.distance;
+				if ( propertyName === "morphTargetInfluences" ) {
 
-		if ( d === 0.0 ) {
+					// potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer.
 
-			this.lightDistance.visible = false;
+					// support resolving morphTarget names into indices.
+					if ( ! targetObject.geometry ) {
 
-		} else {
+						console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this );
+						return;
 
-			this.lightDistance.scale.set( d, d, d );
+					}
 
-		}
+					if ( targetObject.geometry.isBufferGeometry ) {
 
-		this.add( this.lightDistance );
-		*/
+						if ( ! targetObject.geometry.morphAttributes ) {
 
-	}
+							console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this );
+							return;
 
-	PointLightHelper.prototype = Object.create( Mesh.prototype );
-	PointLightHelper.prototype.constructor = PointLightHelper;
+						}
 
-	PointLightHelper.prototype.dispose = function () {
+						for ( var i = 0; i < this.node.geometry.morphAttributes.position.length; i ++ ) {
 
-		this.geometry.dispose();
-		this.material.dispose();
+							if ( targetObject.geometry.morphAttributes.position[ i ].name === propertyIndex ) {
 
-	};
+								propertyIndex = i;
+								break;
 
-	PointLightHelper.prototype.update = function () {
+							}
 
-		if ( this.color !== undefined ) {
+						}
 
-			this.material.color.set( this.color );
 
-		} else {
+					} else {
 
-			this.material.color.copy( this.light.color );
+						if ( ! targetObject.geometry.morphTargets ) {
 
-		}
+							console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphTargets.', this );
+							return;
 
-		/*
-		var d = this.light.distance;
+						}
 
-		if ( d === 0.0 ) {
+						for ( var i = 0; i < this.node.geometry.morphTargets.length; i ++ ) {
 
-			this.lightDistance.visible = false;
+							if ( targetObject.geometry.morphTargets[ i ].name === propertyIndex ) {
 
-		} else {
+								propertyIndex = i;
+								break;
 
-			this.lightDistance.visible = true;
-			this.lightDistance.scale.set( d, d, d );
+							}
 
-		}
-		*/
+						}
 
-	};
+					}
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / https://github.com/Mugen87
-	 */
+				}
 
-	var _vector$a = new Vector3();
-	var _color1 = new Color();
-	var _color2 = new Color();
+				bindingType = this.BindingType.ArrayElement;
 
-	function HemisphereLightHelper( light, size, color ) {
+				this.resolvedProperty = nodeProperty;
+				this.propertyIndex = propertyIndex;
 
-		Object3D.call( this );
+			} else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) {
 
-		this.light = light;
-		this.light.updateMatrixWorld();
+				// must use copy for Object3D.Euler/Quaternion
 
-		this.matrix = light.matrixWorld;
-		this.matrixAutoUpdate = false;
+				bindingType = this.BindingType.HasFromToArray;
 
-		this.color = color;
+				this.resolvedProperty = nodeProperty;
 
-		var geometry = new OctahedronBufferGeometry( size );
-		geometry.rotateY( Math.PI * 0.5 );
+			} else if ( Array.isArray( nodeProperty ) ) {
 
-		this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
-		if ( this.color === undefined ) { this.material.vertexColors = true; }
+				bindingType = this.BindingType.EntireArray;
 
-		var position = geometry.getAttribute( 'position' );
-		var colors = new Float32Array( position.count * 3 );
+				this.resolvedProperty = nodeProperty;
 
-		geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
+			} else {
 
-		this.add( new Mesh( geometry, this.material ) );
+				this.propertyName = propertyName;
 
-		this.update();
+			}
 
-	}
+			// select getter / setter
+			this.getValue = this.GetterByBindingType[ bindingType ];
+			this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
 
-	HemisphereLightHelper.prototype = Object.create( Object3D.prototype );
-	HemisphereLightHelper.prototype.constructor = HemisphereLightHelper;
+		},
 
-	HemisphereLightHelper.prototype.dispose = function () {
+		unbind: function () {
 
-		this.children[ 0 ].geometry.dispose();
-		this.children[ 0 ].material.dispose();
+			this.node = null;
 
-	};
+			// back to the prototype version of getValue / setValue
+			// note: avoiding to mutate the shape of 'this' via 'delete'
+			this.getValue = this._getValue_unbound;
+			this.setValue = this._setValue_unbound;
 
-	HemisphereLightHelper.prototype.update = function () {
+		}
 
-		var mesh = this.children[ 0 ];
+	} );
 
-		if ( this.color !== undefined ) {
+	// DECLARE ALIAS AFTER assign prototype
+	Object.assign( PropertyBinding.prototype, {
 
-			this.material.color.set( this.color );
+		// initial state of these methods that calls 'bind'
+		_getValue_unbound: PropertyBinding.prototype.getValue,
+		_setValue_unbound: PropertyBinding.prototype.setValue,
 
-		} else {
+	} );
 
-			var colors = mesh.geometry.getAttribute( 'color' );
+	/**
+	 *
+	 * A group of objects that receives a shared animation state.
+	 *
+	 * Usage:
+	 *
+	 *  - Add objects you would otherwise pass as 'root' to the
+	 *    constructor or the .clipAction method of AnimationMixer.
+	 *
+	 *  - Instead pass this object as 'root'.
+	 *
+	 *  - You can also add and remove objects later when the mixer
+	 *    is running.
+	 *
+	 * Note:
+	 *
+	 *    Objects of this class appear as one object to the mixer,
+	 *    so cache control of the individual objects must be done
+	 *    on the group.
+	 *
+	 * Limitation:
+	 *
+	 *  - The animated properties must be compatible among the
+	 *    all objects in the group.
+	 *
+	 *  - A single property can either be controlled through a
+	 *    target group or directly, but not both.
+	 *
+	 * @author tschw
+	 */
 
-			_color1.copy( this.light.color );
-			_color2.copy( this.light.groundColor );
+	function AnimationObjectGroup() {
 
-			for ( var i = 0, l = colors.count; i < l; i ++ ) {
+		this.uuid = MathUtils.generateUUID();
 
-				var color = ( i < ( l / 2 ) ) ? _color1 : _color2;
+		// cached objects followed by the active ones
+		this._objects = Array.prototype.slice.call( arguments );
 
-				colors.setXYZ( i, color.r, color.g, color.b );
+		this.nCachedObjects_ = 0; // threshold
+		// note: read by PropertyBinding.Composite
 
-			}
+		var indices = {};
+		this._indicesByUUID = indices; // for bookkeeping
 
-			colors.needsUpdate = true;
+		for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
+
+			indices[ arguments[ i ].uuid ] = i;
 
 		}
 
-		mesh.lookAt( _vector$a.setFromMatrixPosition( this.light.matrixWorld ).negate() );
+		this._paths = []; // inside: string
+		this._parsedPaths = []; // inside: { we don't care, here }
+		this._bindings = []; // inside: Array< PropertyBinding >
+		this._bindingsIndicesByPath = {}; // inside: indices in these arrays
 
-	};
+		var scope = this;
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		this.stats = {
 
-	function GridHelper( size, divisions, color1, color2 ) {
+			objects: {
+				get total() {
 
-		size = size || 10;
-		divisions = divisions || 10;
-		color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
-		color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
+					return scope._objects.length;
 
-		var center = divisions / 2;
-		var step = size / divisions;
-		var halfSize = size / 2;
+				},
+				get inUse() {
 
-		var vertices = [], colors = [];
+					return this.total - scope.nCachedObjects_;
 
-		for ( var i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
+				}
+			},
+			get bindingsPerObject() {
 
-			vertices.push( - halfSize, 0, k, halfSize, 0, k );
-			vertices.push( k, 0, - halfSize, k, 0, halfSize );
+				return scope._bindings.length;
 
-			var color = i === center ? color1 : color2;
+			}
 
-			color.toArray( colors, j ); j += 3;
-			color.toArray( colors, j ); j += 3;
-			color.toArray( colors, j ); j += 3;
-			color.toArray( colors, j ); j += 3;
+		};
 
-		}
+	}
 
-		var geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
+	Object.assign( AnimationObjectGroup.prototype, {
 
-		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
+		isAnimationObjectGroup: true,
 
-		LineSegments.call( this, geometry, material );
+		add: function () {
 
-		this.type = 'GridHelper';
+			var objects = this._objects,
+				nObjects = objects.length,
+				nCachedObjects = this.nCachedObjects_,
+				indicesByUUID = this._indicesByUUID,
+				paths = this._paths,
+				parsedPaths = this._parsedPaths,
+				bindings = this._bindings,
+				nBindings = bindings.length,
+				knownObject = undefined;
 
-	}
+			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
 
-	GridHelper.prototype = Object.assign( Object.create( LineSegments.prototype ), {
+				var object = arguments[ i ],
+					uuid = object.uuid,
+					index = indicesByUUID[ uuid ];
 
-		constructor: GridHelper,
+				if ( index === undefined ) {
 
-		copy: function ( source ) {
+					// unknown object -> add it to the ACTIVE region
 
-			LineSegments.prototype.copy.call( this, source );
+					index = nObjects ++;
+					indicesByUUID[ uuid ] = index;
+					objects.push( object );
 
-			this.geometry.copy( source.geometry );
-			this.material.copy( source.material );
+					// accounting is done, now do the same for all bindings
 
-			return this;
+					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
 
-		},
+						bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
 
-		clone: function () {
+					}
 
-			return new this.constructor().copy( this );
+				} else if ( index < nCachedObjects ) {
 
-		}
+					knownObject = objects[ index ];
 
-	} );
+					// move existing object to the ACTIVE region
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / http://github.com/Mugen87
-	 * @author Hectate / http://www.github.com/Hectate
-	 */
+					var firstActiveIndex = -- nCachedObjects,
+						lastCachedObject = objects[ firstActiveIndex ];
 
-	function PolarGridHelper( radius, radials, circles, divisions, color1, color2 ) {
+					indicesByUUID[ lastCachedObject.uuid ] = index;
+					objects[ index ] = lastCachedObject;
 
-		radius = radius || 10;
-		radials = radials || 16;
-		circles = circles || 8;
-		divisions = divisions || 64;
-		color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
-		color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
+					indicesByUUID[ uuid ] = firstActiveIndex;
+					objects[ firstActiveIndex ] = object;
 
-		var vertices = [];
-		var colors = [];
+					// accounting is done, now do the same for all bindings
 
-		var x, z;
-		var v, i, j, r, color;
+					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
 
-		// create the radials
+						var bindingsForPath = bindings[ j ],
+							lastCached = bindingsForPath[ firstActiveIndex ],
+							binding = bindingsForPath[ index ];
 
-		for ( i = 0; i <= radials; i ++ ) {
+						bindingsForPath[ index ] = lastCached;
 
-			v = ( i / radials ) * ( Math.PI * 2 );
+						if ( binding === undefined ) {
 
-			x = Math.sin( v ) * radius;
-			z = Math.cos( v ) * radius;
+							// since we do not bother to create new bindings
+							// for objects that are cached, the binding may
+							// or may not exist
 
-			vertices.push( 0, 0, 0 );
-			vertices.push( x, 0, z );
+							binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
 
-			color = ( i & 1 ) ? color1 : color2;
+						}
 
-			colors.push( color.r, color.g, color.b );
-			colors.push( color.r, color.g, color.b );
+						bindingsForPath[ firstActiveIndex ] = binding;
 
-		}
+					}
 
-		// create the circles
+				} else if ( objects[ index ] !== knownObject ) {
 
-		for ( i = 0; i <= circles; i ++ ) {
+					console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
+						'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
 
-			color = ( i & 1 ) ? color1 : color2;
+				} // else the object is already where we want it to be
 
-			r = radius - ( radius / circles * i );
+			} // for arguments
 
-			for ( j = 0; j < divisions; j ++ ) {
+			this.nCachedObjects_ = nCachedObjects;
 
-				// first vertex
+		},
 
-				v = ( j / divisions ) * ( Math.PI * 2 );
+		remove: function () {
 
-				x = Math.sin( v ) * r;
-				z = Math.cos( v ) * r;
+			var objects = this._objects,
+				nCachedObjects = this.nCachedObjects_,
+				indicesByUUID = this._indicesByUUID,
+				bindings = this._bindings,
+				nBindings = bindings.length;
 
-				vertices.push( x, 0, z );
-				colors.push( color.r, color.g, color.b );
+			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
 
-				// second vertex
+				var object = arguments[ i ],
+					uuid = object.uuid,
+					index = indicesByUUID[ uuid ];
 
-				v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
+				if ( index !== undefined && index >= nCachedObjects ) {
 
-				x = Math.sin( v ) * r;
-				z = Math.cos( v ) * r;
+					// move existing object into the CACHED region
 
-				vertices.push( x, 0, z );
-				colors.push( color.r, color.g, color.b );
+					var lastCachedIndex = nCachedObjects ++,
+						firstActiveObject = objects[ lastCachedIndex ];
 
-			}
+					indicesByUUID[ firstActiveObject.uuid ] = index;
+					objects[ index ] = firstActiveObject;
 
-		}
+					indicesByUUID[ uuid ] = lastCachedIndex;
+					objects[ lastCachedIndex ] = object;
 
-		var geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
+					// accounting is done, now do the same for all bindings
 
-		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
+					for ( var j = 0, m = nBindings; j !== m; ++ j ) {
 
-		LineSegments.call( this, geometry, material );
+						var bindingsForPath = bindings[ j ],
+							firstActive = bindingsForPath[ lastCachedIndex ],
+							binding = bindingsForPath[ index ];
 
-		this.type = 'PolarGridHelper';
+						bindingsForPath[ index ] = firstActive;
+						bindingsForPath[ lastCachedIndex ] = binding;
 
-	}
+					}
 
-	PolarGridHelper.prototype = Object.create( LineSegments.prototype );
-	PolarGridHelper.prototype.constructor = PolarGridHelper;
+				}
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+			} // for arguments
 
-	var _v1$5 = new Vector3();
-	var _v2$3 = new Vector3();
-	var _v3$1 = new Vector3();
+			this.nCachedObjects_ = nCachedObjects;
 
-	function DirectionalLightHelper( light, size, color ) {
+		},
 
-		Object3D.call( this );
+		// remove & forget
+		uncache: function () {
 
-		this.light = light;
-		this.light.updateMatrixWorld();
+			var objects = this._objects,
+				nObjects = objects.length,
+				nCachedObjects = this.nCachedObjects_,
+				indicesByUUID = this._indicesByUUID,
+				bindings = this._bindings,
+				nBindings = bindings.length;
 
-		this.matrix = light.matrixWorld;
-		this.matrixAutoUpdate = false;
+			for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
 
-		this.color = color;
+				var object = arguments[ i ],
+					uuid = object.uuid,
+					index = indicesByUUID[ uuid ];
 
-		if ( size === undefined ) { size = 1; }
+				if ( index !== undefined ) {
 
-		var geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( [
-			- size, size, 0,
-			size, size, 0,
-			size, - size, 0,
-			- size, - size, 0,
-			- size, size, 0
-		], 3 ) );
+					delete indicesByUUID[ uuid ];
 
-		var material = new LineBasicMaterial( { fog: false, toneMapped: false } );
+					if ( index < nCachedObjects ) {
 
-		this.lightPlane = new Line( geometry, material );
-		this.add( this.lightPlane );
+						// object is cached, shrink the CACHED region
 
-		geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
+						var firstActiveIndex = -- nCachedObjects,
+							lastCachedObject = objects[ firstActiveIndex ],
+							lastIndex = -- nObjects,
+							lastObject = objects[ lastIndex ];
 
-		this.targetLine = new Line( geometry, material );
-		this.add( this.targetLine );
+						// last cached object takes this object's place
+						indicesByUUID[ lastCachedObject.uuid ] = index;
+						objects[ index ] = lastCachedObject;
 
-		this.update();
+						// last object goes to the activated slot and pop
+						indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
+						objects[ firstActiveIndex ] = lastObject;
+						objects.pop();
 
-	}
+						// accounting is done, now do the same for all bindings
 
-	DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
-	DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
+						for ( var j = 0, m = nBindings; j !== m; ++ j ) {
 
-	DirectionalLightHelper.prototype.dispose = function () {
+							var bindingsForPath = bindings[ j ],
+								lastCached = bindingsForPath[ firstActiveIndex ],
+								last = bindingsForPath[ lastIndex ];
 
-		this.lightPlane.geometry.dispose();
-		this.lightPlane.material.dispose();
-		this.targetLine.geometry.dispose();
-		this.targetLine.material.dispose();
+							bindingsForPath[ index ] = lastCached;
+							bindingsForPath[ firstActiveIndex ] = last;
+							bindingsForPath.pop();
 
-	};
+						}
 
-	DirectionalLightHelper.prototype.update = function () {
+					} else {
 
-		_v1$5.setFromMatrixPosition( this.light.matrixWorld );
-		_v2$3.setFromMatrixPosition( this.light.target.matrixWorld );
-		_v3$1.subVectors( _v2$3, _v1$5 );
+						// object is active, just swap with the last and pop
 
-		this.lightPlane.lookAt( _v2$3 );
+						var lastIndex = -- nObjects,
+							lastObject = objects[ lastIndex ];
 
-		if ( this.color !== undefined ) {
+						indicesByUUID[ lastObject.uuid ] = index;
+						objects[ index ] = lastObject;
+						objects.pop();
 
-			this.lightPlane.material.color.set( this.color );
-			this.targetLine.material.color.set( this.color );
+						// accounting is done, now do the same for all bindings
 
-		} else {
+						for ( var j = 0, m = nBindings; j !== m; ++ j ) {
 
-			this.lightPlane.material.color.copy( this.light.color );
-			this.targetLine.material.color.copy( this.light.color );
+							var bindingsForPath = bindings[ j ];
 
-		}
+							bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
+							bindingsForPath.pop();
 
-		this.targetLine.lookAt( _v2$3 );
-		this.targetLine.scale.z = _v3$1.length();
+						}
 
-	};
+					} // cached or active
 
-	/**
-	 * @author alteredq / http://alteredqualia.com/
-	 * @author Mugen87 / https://github.com/Mugen87
-	 *
-	 *	- shows frustum, line of sight and up of the camera
-	 *	- suitable for fast updates
-	 * 	- based on frustum visualization in lightgl.js shadowmap example
-	 *		http://evanw.github.com/lightgl.js/tests/shadowmap.html
-	 */
+				} // if object is known
 
-	var _vector$b = new Vector3();
-	var _camera = new Camera();
+			} // for arguments
 
-	function CameraHelper( camera ) {
+			this.nCachedObjects_ = nCachedObjects;
 
-		var geometry = new BufferGeometry();
-		var material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } );
+		},
 
-		var vertices = [];
-		var colors = [];
+		// Internal interface used by befriended PropertyBinding.Composite:
 
-		var pointMap = {};
+		subscribe_: function ( path, parsedPath ) {
 
-		// colors
+			// returns an array of bindings for the given path that is changed
+			// according to the contained objects in the group
 
-		var colorFrustum = new Color( 0xffaa00 );
-		var colorCone = new Color( 0xff0000 );
-		var colorUp = new Color( 0x00aaff );
-		var colorTarget = new Color( 0xffffff );
-		var colorCross = new Color( 0x333333 );
+			var indicesByPath = this._bindingsIndicesByPath,
+				index = indicesByPath[ path ],
+				bindings = this._bindings;
 
-		// near
+			if ( index !== undefined ) { return bindings[ index ]; }
 
-		addLine( 'n1', 'n2', colorFrustum );
-		addLine( 'n2', 'n4', colorFrustum );
-		addLine( 'n4', 'n3', colorFrustum );
-		addLine( 'n3', 'n1', colorFrustum );
+			var paths = this._paths,
+				parsedPaths = this._parsedPaths,
+				objects = this._objects,
+				nObjects = objects.length,
+				nCachedObjects = this.nCachedObjects_,
+				bindingsForPath = new Array( nObjects );
 
-		// far
+			index = bindings.length;
 
-		addLine( 'f1', 'f2', colorFrustum );
-		addLine( 'f2', 'f4', colorFrustum );
-		addLine( 'f4', 'f3', colorFrustum );
-		addLine( 'f3', 'f1', colorFrustum );
+			indicesByPath[ path ] = index;
 
-		// sides
+			paths.push( path );
+			parsedPaths.push( parsedPath );
+			bindings.push( bindingsForPath );
 
-		addLine( 'n1', 'f1', colorFrustum );
-		addLine( 'n2', 'f2', colorFrustum );
-		addLine( 'n3', 'f3', colorFrustum );
-		addLine( 'n4', 'f4', colorFrustum );
+			for ( var i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
 
-		// cone
+				var object = objects[ i ];
+				bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
 
-		addLine( 'p', 'n1', colorCone );
-		addLine( 'p', 'n2', colorCone );
-		addLine( 'p', 'n3', colorCone );
-		addLine( 'p', 'n4', colorCone );
+			}
 
-		// up
+			return bindingsForPath;
 
-		addLine( 'u1', 'u2', colorUp );
-		addLine( 'u2', 'u3', colorUp );
-		addLine( 'u3', 'u1', colorUp );
+		},
 
-		// target
+		unsubscribe_: function ( path ) {
 
-		addLine( 'c', 't', colorTarget );
-		addLine( 'p', 'c', colorCross );
+			// tells the group to forget about a property path and no longer
+			// update the array previously obtained with 'subscribe_'
 
-		// cross
+			var indicesByPath = this._bindingsIndicesByPath,
+				index = indicesByPath[ path ];
 
-		addLine( 'cn1', 'cn2', colorCross );
-		addLine( 'cn3', 'cn4', colorCross );
+			if ( index !== undefined ) {
 
-		addLine( 'cf1', 'cf2', colorCross );
-		addLine( 'cf3', 'cf4', colorCross );
+				var paths = this._paths,
+					parsedPaths = this._parsedPaths,
+					bindings = this._bindings,
+					lastBindingsIndex = bindings.length - 1,
+					lastBindings = bindings[ lastBindingsIndex ],
+					lastBindingsPath = path[ lastBindingsIndex ];
 
-		function addLine( a, b, color ) {
+				indicesByPath[ lastBindingsPath ] = index;
 
-			addPoint( a, color );
-			addPoint( b, color );
+				bindings[ index ] = lastBindings;
+				bindings.pop();
+
+				parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
+				parsedPaths.pop();
+
+				paths[ index ] = paths[ lastBindingsIndex ];
+				paths.pop();
+
+			}
 
 		}
 
-		function addPoint( id, color ) {
+	} );
 
-			vertices.push( 0, 0, 0 );
-			colors.push( color.r, color.g, color.b );
+	/**
+	 *
+	 * Action provided by AnimationMixer for scheduling clip playback on specific
+	 * objects.
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 *
+	 */
 
-			if ( pointMap[ id ] === undefined ) {
+	function AnimationAction( mixer, clip, localRoot, blendMode ) {
 
-				pointMap[ id ] = [];
+		this._mixer = mixer;
+		this._clip = clip;
+		this._localRoot = localRoot || null;
+		this.blendMode = blendMode || clip.blendMode;
 
-			}
+		var tracks = clip.tracks,
+			nTracks = tracks.length,
+			interpolants = new Array( nTracks );
 
-			pointMap[ id ].push( ( vertices.length / 3 ) - 1 );
+		var interpolantSettings = {
+			endingStart: ZeroCurvatureEnding,
+			endingEnd: ZeroCurvatureEnding
+		};
+
+		for ( var i = 0; i !== nTracks; ++ i ) {
+
+			var interpolant = tracks[ i ].createInterpolant( null );
+			interpolants[ i ] = interpolant;
+			interpolant.settings = interpolantSettings;
 
 		}
 
-		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
+		this._interpolantSettings = interpolantSettings;
 
-		LineSegments.call( this, geometry, material );
+		this._interpolants = interpolants; // bound by the mixer
 
-		this.type = 'CameraHelper';
+		// inside: PropertyMixer (managed by the mixer)
+		this._propertyBindings = new Array( nTracks );
 
-		this.camera = camera;
-		if ( this.camera.updateProjectionMatrix ) { this.camera.updateProjectionMatrix(); }
+		this._cacheIndex = null; // for the memory manager
+		this._byClipCacheIndex = null; // for the memory manager
 
-		this.matrix = camera.matrixWorld;
-		this.matrixAutoUpdate = false;
+		this._timeScaleInterpolant = null;
+		this._weightInterpolant = null;
 
-		this.pointMap = pointMap;
+		this.loop = LoopRepeat;
+		this._loopCount = - 1;
 
-		this.update();
+		// global mixer time when the action is to be started
+		// it's set back to 'null' upon start of the action
+		this._startTime = null;
 
-	}
+		// scaled local time of the action
+		// gets clamped or wrapped to 0..clip.duration according to loop
+		this.time = 0;
 
-	CameraHelper.prototype = Object.create( LineSegments.prototype );
-	CameraHelper.prototype.constructor = CameraHelper;
+		this.timeScale = 1;
+		this._effectiveTimeScale = 1;
 
-	CameraHelper.prototype.update = function () {
+		this.weight = 1;
+		this._effectiveWeight = 1;
 
-		var geometry = this.geometry;
-		var pointMap = this.pointMap;
+		this.repetitions = Infinity; // no. of repetitions when looping
 
-		var w = 1, h = 1;
+		this.paused = false; // true -> zero effective time scale
+		this.enabled = true; // false -> zero effective weight
 
-		// we need just camera projection matrix inverse
-		// world matrix must be identity
+		this.clampWhenFinished = false;// keep feeding the last frame?
 
-		_camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );
+		this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate
+		this.zeroSlopeAtEnd = true;// clips for start, loop and end
 
-		// center / target
+	}
 
-		setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 );
-		setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );
+	Object.assign( AnimationAction.prototype, {
 
-		// near
+		// State & Scheduling
 
-		setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 );
-		setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 );
-		setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 );
-		setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 );
+		play: function () {
 
-		// far
+			this._mixer._activateAction( this );
 
-		setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 );
-		setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 );
-		setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 );
-		setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 );
+			return this;
 
-		// up
+		},
 
-		setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 );
-		setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 );
-		setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 );
+		stop: function () {
 
-		// cross
+			this._mixer._deactivateAction( this );
 
-		setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 );
-		setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 );
-		setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
-		setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );
+			return this.reset();
 
-		setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 );
-		setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 );
-		setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 );
-		setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 );
+		},
 
-		geometry.getAttribute( 'position' ).needsUpdate = true;
+		reset: function () {
 
-	};
+			this.paused = false;
+			this.enabled = true;
 
-	function setPoint( point, pointMap, geometry, camera, x, y, z ) {
+			this.time = 0; // restart clip
+			this._loopCount = - 1;// forget previous loops
+			this._startTime = null;// forget scheduling
 
-		_vector$b.set( x, y, z ).unproject( camera );
+			return this.stopFading().stopWarping();
 
-		var points = pointMap[ point ];
+		},
 
-		if ( points !== undefined ) {
+		isRunning: function () {
 
-			var position = geometry.getAttribute( 'position' );
+			return this.enabled && ! this.paused && this.timeScale !== 0 &&
+				this._startTime === null && this._mixer._isActiveAction( this );
 
-			for ( var i = 0, l = points.length; i < l; i ++ ) {
+		},
 
-				position.setXYZ( points[ i ], _vector$b.x, _vector$b.y, _vector$b.z );
+		// return true when play has been called
+		isScheduled: function () {
 
-			}
+			return this._mixer._isActiveAction( this );
 
-		}
+		},
 
-	}
+		startAt: function ( time ) {
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 * @author Mugen87 / http://github.com/Mugen87
-	 */
+			this._startTime = time;
 
-	var _box$3 = new Box3();
+			return this;
 
-	function BoxHelper( object, color ) {
+		},
 
-		this.object = object;
+		setLoop: function ( mode, repetitions ) {
 
-		if ( color === undefined ) { color = 0xffff00; }
+			this.loop = mode;
+			this.repetitions = repetitions;
 
-		var indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
-		var positions = new Float32Array( 8 * 3 );
+			return this;
 
-		var geometry = new BufferGeometry();
-		geometry.setIndex( new BufferAttribute( indices, 1 ) );
-		geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
+		},
 
-		LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+		// Weight
 
-		this.type = 'BoxHelper';
+		// set the weight stopping any scheduled fading
+		// although .enabled = false yields an effective weight of zero, this
+		// method does *not* change .enabled, because it would be confusing
+		setEffectiveWeight: function ( weight ) {
 
-		this.matrixAutoUpdate = false;
+			this.weight = weight;
 
-		this.update();
+			// note: same logic as when updated at runtime
+			this._effectiveWeight = this.enabled ? weight : 0;
 
-	}
+			return this.stopFading();
 
-	BoxHelper.prototype = Object.create( LineSegments.prototype );
-	BoxHelper.prototype.constructor = BoxHelper;
+		},
 
-	BoxHelper.prototype.update = function ( object ) {
+		// return the weight considering fading and .enabled
+		getEffectiveWeight: function () {
 
-		if ( object !== undefined ) {
+			return this._effectiveWeight;
 
-			console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' );
+		},
 
-		}
+		fadeIn: function ( duration ) {
 
-		if ( this.object !== undefined ) {
+			return this._scheduleFading( duration, 0, 1 );
 
-			_box$3.setFromObject( this.object );
+		},
 
-		}
+		fadeOut: function ( duration ) {
 
-		if ( _box$3.isEmpty() ) { return; }
+			return this._scheduleFading( duration, 1, 0 );
 
-		var min = _box$3.min;
-		var max = _box$3.max;
+		},
 
-		/*
-		  5____4
-		1/___0/|
-		| 6__|_7
-		2/___3/
+		crossFadeFrom: function ( fadeOutAction, duration, warp ) {
 
-		0: max.x, max.y, max.z
-		1: min.x, max.y, max.z
-		2: min.x, min.y, max.z
-		3: max.x, min.y, max.z
-		4: max.x, max.y, min.z
-		5: min.x, max.y, min.z
-		6: min.x, min.y, min.z
-		7: max.x, min.y, min.z
-		*/
+			fadeOutAction.fadeOut( duration );
+			this.fadeIn( duration );
 
-		var position = this.geometry.attributes.position;
-		var array = position.array;
+			if ( warp ) {
 
-		array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
-		array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
-		array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
-		array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
-		array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
-		array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
-		array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
-		array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
+				var fadeInDuration = this._clip.duration,
+					fadeOutDuration = fadeOutAction._clip.duration,
 
-		position.needsUpdate = true;
+					startEndRatio = fadeOutDuration / fadeInDuration,
+					endStartRatio = fadeInDuration / fadeOutDuration;
 
-		this.geometry.computeBoundingSphere();
+				fadeOutAction.warp( 1.0, startEndRatio, duration );
+				this.warp( endStartRatio, 1.0, duration );
 
+			}
 
-	};
+			return this;
 
-	BoxHelper.prototype.setFromObject = function ( object ) {
+		},
 
-		this.object = object;
-		this.update();
+		crossFadeTo: function ( fadeInAction, duration, warp ) {
 
-		return this;
+			return fadeInAction.crossFadeFrom( this, duration, warp );
 
-	};
+		},
 
-	BoxHelper.prototype.copy = function ( source ) {
+		stopFading: function () {
 
-		LineSegments.prototype.copy.call( this, source );
+			var weightInterpolant = this._weightInterpolant;
 
-		this.object = source.object;
+			if ( weightInterpolant !== null ) {
 
-		return this;
+				this._weightInterpolant = null;
+				this._mixer._takeBackControlInterpolant( weightInterpolant );
 
-	};
+			}
 
-	BoxHelper.prototype.clone = function () {
+			return this;
 
-		return new this.constructor().copy( this );
+		},
 
-	};
+		// Time Scale Control
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+		// set the time scale stopping any scheduled warping
+		// although .paused = true yields an effective time scale of zero, this
+		// method does *not* change .paused, because it would be confusing
+		setEffectiveTimeScale: function ( timeScale ) {
 
-	function Box3Helper( box, color ) {
+			this.timeScale = timeScale;
+			this._effectiveTimeScale = this.paused ? 0 : timeScale;
 
-		this.type = 'Box3Helper';
+			return this.stopWarping();
 
-		this.box = box;
+		},
 
-		color = color || 0xffff00;
+		// return the time scale considering warping and .paused
+		getEffectiveTimeScale: function () {
 
-		var indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
+			return this._effectiveTimeScale;
 
-		var positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
+		},
 
-		var geometry = new BufferGeometry();
+		setDuration: function ( duration ) {
 
-		geometry.setIndex( new BufferAttribute( indices, 1 ) );
+			this.timeScale = this._clip.duration / duration;
 
-		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
+			return this.stopWarping();
 
-		LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+		},
 
-		this.type = 'Box3Helper';
+		syncWith: function ( action ) {
 
-		this.geometry.computeBoundingSphere();
+			this.time = action.time;
+			this.timeScale = action.timeScale;
 
-	}
+			return this.stopWarping();
 
-	Box3Helper.prototype = Object.create( LineSegments.prototype );
-	Box3Helper.prototype.constructor = Box3Helper;
+		},
 
-	Box3Helper.prototype.updateMatrixWorld = function ( force ) {
+		halt: function ( duration ) {
 
-		var box = this.box;
+			return this.warp( this._effectiveTimeScale, 0, duration );
 
-		if ( box.isEmpty() ) { return; }
+		},
 
-		box.getCenter( this.position );
+		warp: function ( startTimeScale, endTimeScale, duration ) {
 
-		box.getSize( this.scale );
+			var mixer = this._mixer, now = mixer.time,
+				interpolant = this._timeScaleInterpolant,
 
-		this.scale.multiplyScalar( 0.5 );
+				timeScale = this.timeScale;
 
-		Object3D.prototype.updateMatrixWorld.call( this, force );
+			if ( interpolant === null ) {
 
-	};
+				interpolant = mixer._lendControlInterpolant();
+				this._timeScaleInterpolant = interpolant;
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 */
+			}
 
-	function PlaneHelper( plane, size, hex ) {
+			var times = interpolant.parameterPositions,
+				values = interpolant.sampleValues;
 
-		this.plane = plane;
+			times[ 0 ] = now;
+			times[ 1 ] = now + duration;
 
-		this.size = ( size === undefined ) ? 1 : size;
+			values[ 0 ] = startTimeScale / timeScale;
+			values[ 1 ] = endTimeScale / timeScale;
 
-		var color = ( hex !== undefined ) ? hex : 0xffff00;
+			return this;
 
-		var positions = [ 1, - 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0 ];
+		},
 
-		var geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
-		geometry.computeBoundingSphere();
+		stopWarping: function () {
 
-		Line.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+			var timeScaleInterpolant = this._timeScaleInterpolant;
 
-		this.type = 'PlaneHelper';
+			if ( timeScaleInterpolant !== null ) {
 
-		//
+				this._timeScaleInterpolant = null;
+				this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
 
-		var positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
+			}
 
-		var geometry2 = new BufferGeometry();
-		geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
-		geometry2.computeBoundingSphere();
+			return this;
 
-		this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
+		},
 
-	}
+		// Object Accessors
 
-	PlaneHelper.prototype = Object.create( Line.prototype );
-	PlaneHelper.prototype.constructor = PlaneHelper;
+		getMixer: function () {
 
-	PlaneHelper.prototype.updateMatrixWorld = function ( force ) {
+			return this._mixer;
 
-		var scale = - this.plane.constant;
+		},
 
-		if ( Math.abs( scale ) < 1e-8 ) { scale = 1e-8; } // sign does not matter
+		getClip: function () {
 
-		this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
+			return this._clip;
 
-		this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here
+		},
 
-		this.lookAt( this.plane.normal );
+		getRoot: function () {
 
-		Object3D.prototype.updateMatrixWorld.call( this, force );
+			return this._localRoot || this._mixer._root;
 
-	};
+		},
 
-	/**
-	 * @author WestLangley / http://github.com/WestLangley
-	 * @author zz85 / http://github.com/zz85
-	 * @author bhouston / http://clara.io
-	 *
-	 * Creates an arrow for visualizing directions
-	 *
-	 * Parameters:
-	 *  dir - Vector3
-	 *  origin - Vector3
-	 *  length - Number
-	 *  color - color in hex value
-	 *  headLength - Number
-	 *  headWidth - Number
-	 */
+		// Interna
 
-	var _axis = new Vector3();
-	var _lineGeometry, _coneGeometry;
+		_update: function ( time, deltaTime, timeDirection, accuIndex ) {
 
-	function ArrowHelper( dir, origin, length, color, headLength, headWidth ) {
+			// called by the mixer
 
-		// dir is assumed to be normalized
+			if ( ! this.enabled ) {
 
-		Object3D.call( this );
+				// call ._updateWeight() to update ._effectiveWeight
 
-		this.type = 'ArrowHelper';
+				this._updateWeight( time );
+				return;
 
-		if ( dir === undefined ) { dir = new Vector3( 0, 0, 1 ); }
-		if ( origin === undefined ) { origin = new Vector3( 0, 0, 0 ); }
-		if ( length === undefined ) { length = 1; }
-		if ( color === undefined ) { color = 0xffff00; }
-		if ( headLength === undefined ) { headLength = 0.2 * length; }
-		if ( headWidth === undefined ) { headWidth = 0.2 * headLength; }
+			}
 
-		if ( _lineGeometry === undefined ) {
+			var startTime = this._startTime;
 
-			_lineGeometry = new BufferGeometry();
-			_lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
+			if ( startTime !== null ) {
 
-			_coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
-			_coneGeometry.translate( 0, - 0.5, 0 );
+				// check for scheduled start of action
 
-		}
+				var timeRunning = ( time - startTime ) * timeDirection;
+				if ( timeRunning < 0 || timeDirection === 0 ) {
 
-		this.position.copy( origin );
+					return; // yet to come / don't decide when delta = 0
 
-		this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
-		this.line.matrixAutoUpdate = false;
-		this.add( this.line );
+				}
 
-		this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
-		this.cone.matrixAutoUpdate = false;
-		this.add( this.cone );
+				// start
 
-		this.setDirection( dir );
-		this.setLength( length, headLength, headWidth );
+				this._startTime = null; // unschedule
+				deltaTime = timeDirection * timeRunning;
 
-	}
+			}
 
-	ArrowHelper.prototype = Object.create( Object3D.prototype );
-	ArrowHelper.prototype.constructor = ArrowHelper;
+			// apply time scale and advance time
 
-	ArrowHelper.prototype.setDirection = function ( dir ) {
+			deltaTime *= this._updateTimeScale( time );
+			var clipTime = this._updateTime( deltaTime );
 
-		// dir is assumed to be normalized
+			// note: _updateTime may disable the action resulting in
+			// an effective weight of 0
 
-		if ( dir.y > 0.99999 ) {
+			var weight = this._updateWeight( time );
 
-			this.quaternion.set( 0, 0, 0, 1 );
+			if ( weight > 0 ) {
 
-		} else if ( dir.y < - 0.99999 ) {
+				var interpolants = this._interpolants;
+				var propertyMixers = this._propertyBindings;
 
-			this.quaternion.set( 1, 0, 0, 0 );
+				switch ( this.blendMode ) {
 
-		} else {
+					case AdditiveAnimationBlendMode:
 
-			_axis.set( dir.z, 0, - dir.x ).normalize();
+						for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
 
-			var radians = Math.acos( dir.y );
+							interpolants[ j ].evaluate( clipTime );
+							propertyMixers[ j ].accumulateAdditive( weight );
 
-			this.quaternion.setFromAxisAngle( _axis, radians );
+						}
 
-		}
+						break;
 
-	};
+					case NormalAnimationBlendMode:
+					default:
 
-	ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
+						for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
 
-		if ( headLength === undefined ) { headLength = 0.2 * length; }
-		if ( headWidth === undefined ) { headWidth = 0.2 * headLength; }
+							interpolants[ j ].evaluate( clipTime );
+							propertyMixers[ j ].accumulate( accuIndex, weight );
 
-		this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
-		this.line.updateMatrix();
+						}
 
-		this.cone.scale.set( headWidth, headLength, headWidth );
-		this.cone.position.y = length;
-		this.cone.updateMatrix();
+				}
 
-	};
+			}
 
-	ArrowHelper.prototype.setColor = function ( color ) {
+		},
 
-		this.line.material.color.set( color );
-		this.cone.material.color.set( color );
+		_updateWeight: function ( time ) {
 
-	};
+			var weight = 0;
 
-	ArrowHelper.prototype.copy = function ( source ) {
+			if ( this.enabled ) {
 
-		Object3D.prototype.copy.call( this, source, false );
+				weight = this.weight;
+				var interpolant = this._weightInterpolant;
 
-		this.line.copy( source.line );
-		this.cone.copy( source.cone );
+				if ( interpolant !== null ) {
 
-		return this;
+					var interpolantValue = interpolant.evaluate( time )[ 0 ];
 
-	};
+					weight *= interpolantValue;
 
-	ArrowHelper.prototype.clone = function () {
+					if ( time > interpolant.parameterPositions[ 1 ] ) {
 
-		return new this.constructor().copy( this );
+						this.stopFading();
 
-	};
+						if ( interpolantValue === 0 ) {
 
-	/**
-	 * @author sroucheray / http://sroucheray.org/
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+							// faded out, disable
+							this.enabled = false;
 
-	function AxesHelper( size ) {
+						}
 
-		size = size || 1;
+					}
 
-		var vertices = [
-			0, 0, 0,	size, 0, 0,
-			0, 0, 0,	0, size, 0,
-			0, 0, 0,	0, 0, size
-		];
+				}
 
-		var colors = [
-			1, 0, 0,	1, 0.6, 0,
-			0, 1, 0,	0.6, 1, 0,
-			0, 0, 1,	0, 0.6, 1
-		];
+			}
 
-		var geometry = new BufferGeometry();
-		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
-		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
+			this._effectiveWeight = weight;
+			return weight;
 
-		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
+		},
 
-		LineSegments.call( this, geometry, material );
+		_updateTimeScale: function ( time ) {
 
-		this.type = 'AxesHelper';
+			var timeScale = 0;
 
-	}
+			if ( ! this.paused ) {
 
-	AxesHelper.prototype = Object.create( LineSegments.prototype );
-	AxesHelper.prototype.constructor = AxesHelper;
+				timeScale = this.timeScale;
 
-	/**
-	 * @author Emmett Lalish / elalish
-	 *
-	 * This class generates a Prefiltered, Mipmapped Radiance Environment Map
-	 * (PMREM) from a cubeMap environment texture. This allows different levels of
-	 * blur to be quickly accessed based on material roughness. It is packed into a
-	 * special CubeUV format that allows us to perform custom interpolation so that
-	 * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap
-	 * chain, it only goes down to the LOD_MIN level (above), and then creates extra
-	 * even more filtered 'mips' at the same LOD_MIN resolution, associated with
-	 * higher roughness levels. In this way we maintain resolution to smoothly
-	 * interpolate diffuse lighting while limiting sampling computation.
-	 */
+				var interpolant = this._timeScaleInterpolant;
 
-	var LOD_MIN = 4;
-	var LOD_MAX = 8;
-	var SIZE_MAX = Math.pow( 2, LOD_MAX );
+				if ( interpolant !== null ) {
 
-	// The standard deviations (radians) associated with the extra mips. These are
-	// chosen to approximate a Trowbridge-Reitz distribution function times the
-	// geometric shadowing function. These sigma values squared must match the
-	// variance #defines in cube_uv_reflection_fragment.glsl.js.
-	var EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ];
+					var interpolantValue = interpolant.evaluate( time )[ 0 ];
 
-	var TOTAL_LODS = LOD_MAX - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length;
+					timeScale *= interpolantValue;
 
-	// The maximum length of the blur for loop. Smaller sigmas will use fewer
-	// samples and exit early, but not recompile the shader.
-	var MAX_SAMPLES = 20;
+					if ( time > interpolant.parameterPositions[ 1 ] ) {
 
-	var ENCODINGS = {};
-	ENCODINGS[ LinearEncoding ] = 0;
-	ENCODINGS[ sRGBEncoding ] = 1;
-	ENCODINGS[ RGBEEncoding ] = 2;
-	ENCODINGS[ RGBM7Encoding ] = 3;
-	ENCODINGS[ RGBM16Encoding ] = 4;
-	ENCODINGS[ RGBDEncoding ] = 5;
-	ENCODINGS[ GammaEncoding ] = 6;
+						this.stopWarping();
 
-	var _flatCamera = new OrthographicCamera();
-	var ref = _createPlanes();
-	var _lodPlanes = ref._lodPlanes;
-	var _sizeLods = ref._sizeLods;
-	var _sigmas = ref._sigmas;
-	var _oldTarget = null;
+						if ( timeScale === 0 ) {
 
-	// Golden Ratio
-	var PHI = ( 1 + Math.sqrt( 5 ) ) / 2;
-	var INV_PHI = 1 / PHI;
+							// motion has halted, pause
+							this.paused = true;
 
-	// Vertices of a dodecahedron (except the opposites, which represent the
-	// same axis), used as axis directions evenly spread on a sphere.
-	var _axisDirections = [
-		new Vector3( 1, 1, 1 ),
-		new Vector3( - 1, 1, 1 ),
-		new Vector3( 1, 1, - 1 ),
-		new Vector3( - 1, 1, - 1 ),
-		new Vector3( 0, PHI, INV_PHI ),
-		new Vector3( 0, PHI, - INV_PHI ),
-		new Vector3( INV_PHI, 0, PHI ),
-		new Vector3( - INV_PHI, 0, PHI ),
-		new Vector3( PHI, INV_PHI, 0 ),
-		new Vector3( - PHI, INV_PHI, 0 ) ];
+						} else {
 
-	function PMREMGenerator( renderer ) {
+							// warp done - apply final time scale
+							this.timeScale = timeScale;
 
-		this._renderer = renderer;
-		this._pingPongRenderTarget = null;
+						}
 
-		this._blurMaterial = _getBlurShader( MAX_SAMPLES );
-		this._equirectShader = null;
-		this._cubemapShader = null;
+					}
 
-		this._compileMaterial( this._blurMaterial );
+				}
 
-	}
+			}
 
-	PMREMGenerator.prototype = {
+			this._effectiveTimeScale = timeScale;
+			return timeScale;
 
-		constructor: PMREMGenerator,
+		},
 
-		/**
-		 * Generates a PMREM from a supplied Scene, which can be faster than using an
-		 * image if networking bandwidth is low. Optional sigma specifies a blur radius
-		 * in radians to be applied to the scene before PMREM generation. Optional near
-		 * and far planes ensure the scene is rendered in its entirety (the cubeCamera
-		 * is placed at the origin).
-		 */
-		fromScene: function ( scene, sigma, near, far ) {
-			if ( sigma === void 0 ) sigma = 0;
-			if ( near === void 0 ) near = 0.1;
-			if ( far === void 0 ) far = 100;
+		_updateTime: function ( deltaTime ) {
 
+			var time = this.time + deltaTime;
+			var duration = this._clip.duration;
+			var loop = this.loop;
+			var loopCount = this._loopCount;
 
-			_oldTarget = this._renderer.getRenderTarget();
-			var cubeUVRenderTarget = this._allocateTargets();
+			var pingPong = ( loop === LoopPingPong );
 
-			this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
-			if ( sigma > 0 ) {
+			if ( deltaTime === 0 ) {
 
-				this._blur( cubeUVRenderTarget, 0, 0, sigma );
+				if ( loopCount === - 1 ) { return time; }
+
+				return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
 
 			}
 
-			this._applyPMREM( cubeUVRenderTarget );
-			this._cleanup( cubeUVRenderTarget );
+			if ( loop === LoopOnce ) {
 
-			return cubeUVRenderTarget;
+				if ( loopCount === - 1 ) {
 
-		},
+					// just started
 
-		/**
-		 * Generates a PMREM from an equirectangular texture, which can be either LDR
-		 * (RGBFormat) or HDR (RGBEFormat). The ideal input image size is 1k (1024 x 512),
-		 * as this matches best with the 256 x 256 cubemap output.
-		 */
-		fromEquirectangular: function ( equirectangular ) {
+					this._loopCount = 0;
+					this._setEndings( true, true, false );
 
-			equirectangular.magFilter = NearestFilter;
-			equirectangular.minFilter = NearestFilter;
-			equirectangular.generateMipmaps = false;
+				}
 
-			return this.fromCubemap( equirectangular );
+				handle_stop: {
 
-		},
+					if ( time >= duration ) {
 
-		/**
-		 * Generates a PMREM from an cubemap texture, which can be either LDR
-		 * (RGBFormat) or HDR (RGBEFormat). The ideal input cube size is 256 x 256,
-		 * as this matches best with the 256 x 256 cubemap output.
-		 */
-		fromCubemap: function ( cubemap ) {
+						time = duration;
 
-			_oldTarget = this._renderer.getRenderTarget();
-			var cubeUVRenderTarget = this._allocateTargets( cubemap );
-			this._textureToCubeUV( cubemap, cubeUVRenderTarget );
-			this._applyPMREM( cubeUVRenderTarget );
-			this._cleanup( cubeUVRenderTarget );
+					} else if ( time < 0 ) {
 
-			return cubeUVRenderTarget;
+						time = 0;
 
-		},
+					} else {
 
-		/**
-		 * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during
-		 * your texture's network fetch for increased concurrency.
-		 */
-		compileCubemapShader: function () {
+						this.time = time;
 
-			if ( this._cubemapShader === null ) {
+						break handle_stop;
 
-				this._cubemapShader = _getCubemapShader();
-				this._compileMaterial( this._cubemapShader );
+					}
 
-			}
+					if ( this.clampWhenFinished ) { this.paused = true; }
+					else { this.enabled = false; }
 
-		},
+					this.time = time;
 
-		/**
-		 * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during
-		 * your texture's network fetch for increased concurrency.
-		 */
-		compileEquirectangularShader: function () {
+					this._mixer.dispatchEvent( {
+						type: 'finished', action: this,
+						direction: deltaTime < 0 ? - 1 : 1
+					} );
 
-			if ( this._equirectShader === null ) {
+				}
 
-				this._equirectShader = _getEquirectShader();
-				this._compileMaterial( this._equirectShader );
+			} else { // repetitive Repeat or PingPong
 
-			}
+				if ( loopCount === - 1 ) {
 
-		},
+					// just started
 
-		/**
-		 * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class,
-		 * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on
-		 * one of them will cause any others to also become unusable.
-		 */
-		dispose: function () {
+					if ( deltaTime >= 0 ) {
 
-			this._blurMaterial.dispose();
+						loopCount = 0;
 
-			if ( this._cubemapShader !== null ) { this._cubemapShader.dispose(); }
-			if ( this._equirectShader !== null ) { this._equirectShader.dispose(); }
+						this._setEndings( true, this.repetitions === 0, pingPong );
 
-			for ( var i = 0; i < _lodPlanes.length; i ++ ) {
+					} else {
 
-				_lodPlanes[ i ].dispose();
+						// when looping in reverse direction, the initial
+						// transition through zero counts as a repetition,
+						// so leave loopCount at -1
 
-			}
+						this._setEndings( this.repetitions === 0, true, pingPong );
 
-		},
+					}
 
-		// private interface
+				}
 
-		_cleanup: function ( outputTarget ) {
+				if ( time >= duration || time < 0 ) {
 
-			this._pingPongRenderTarget.dispose();
-			this._renderer.setRenderTarget( _oldTarget );
-			outputTarget.scissorTest = false;
-			// reset viewport and scissor
-			outputTarget.setSize( outputTarget.width, outputTarget.height );
+					// wrap around
 
-		},
+					var loopDelta = Math.floor( time / duration ); // signed
+					time -= duration * loopDelta;
 
-		_allocateTargets: function ( equirectangular ) {
+					loopCount += Math.abs( loopDelta );
 
-			var params = {
-				magFilter: NearestFilter,
-				minFilter: NearestFilter,
-				generateMipmaps: false,
-				type: UnsignedByteType,
-				format: RGBEFormat,
-				encoding: _isLDR( equirectangular ) ? equirectangular.encoding : RGBEEncoding,
-				depthBuffer: false,
-				stencilBuffer: false
-			};
+					var pending = this.repetitions - loopCount;
 
-			var cubeUVRenderTarget = _createRenderTarget( params );
-			cubeUVRenderTarget.depthBuffer = equirectangular ? false : true;
-			this._pingPongRenderTarget = _createRenderTarget( params );
-			return cubeUVRenderTarget;
+					if ( pending <= 0 ) {
 
-		},
+						// have to stop (switch state, clamp time, fire event)
 
-		_compileMaterial: function ( material ) {
+						if ( this.clampWhenFinished ) { this.paused = true; }
+						else { this.enabled = false; }
 
-			var tmpScene = new Scene();
-			tmpScene.add( new Mesh( _lodPlanes[ 0 ], material ) );
-			this._renderer.compile( tmpScene, _flatCamera );
+						time = deltaTime > 0 ? duration : 0;
 
-		},
+						this.time = time;
 
-		_sceneToCubeUV: function ( scene, near, far, cubeUVRenderTarget ) {
+						this._mixer.dispatchEvent( {
+							type: 'finished', action: this,
+							direction: deltaTime > 0 ? 1 : - 1
+						} );
 
-			var fov = 90;
-			var aspect = 1;
-			var cubeCamera = new PerspectiveCamera( fov, aspect, near, far );
-			var upSign = [ 1, 1, 1, 1, - 1, 1 ];
-			var forwardSign = [ 1, 1, - 1, - 1, - 1, 1 ];
-			var renderer = this._renderer;
+					} else {
 
-			var outputEncoding = renderer.outputEncoding;
-			var toneMapping = renderer.toneMapping;
-			var toneMappingExposure = renderer.toneMappingExposure;
-			var clearColor = renderer.getClearColor();
-			var clearAlpha = renderer.getClearAlpha();
+						// keep running
 
-			renderer.toneMapping = LinearToneMapping;
-			renderer.toneMappingExposure = 1.0;
-			renderer.outputEncoding = LinearEncoding;
-			scene.scale.z *= - 1;
+						if ( pending === 1 ) {
 
-			var background = scene.background;
-			if ( background && background.isColor ) {
+							// entering the last round
 
-				background.convertSRGBToLinear();
-				// Convert linear to RGBE
-				var maxComponent = Math.max( background.r, background.g, background.b );
-				var fExp = Math.min( Math.max( Math.ceil( Math.log2( maxComponent ) ), - 128.0 ), 127.0 );
-				background = background.multiplyScalar( Math.pow( 2.0, - fExp ) );
-				var alpha = ( fExp + 128.0 ) / 255.0;
-				renderer.setClearColor( background, alpha );
-				scene.background = null;
+							var atStart = deltaTime < 0;
+							this._setEndings( atStart, ! atStart, pingPong );
 
-			}
+						} else {
 
-			for ( var i = 0; i < 6; i ++ ) {
+							this._setEndings( false, false, pingPong );
 
-				var col = i % 3;
-				if ( col == 0 ) {
+						}
 
-					cubeCamera.up.set( 0, upSign[ i ], 0 );
-					cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
+						this._loopCount = loopCount;
 
-				} else if ( col == 1 ) {
+						this.time = time;
 
-					cubeCamera.up.set( 0, 0, upSign[ i ] );
-					cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
+						this._mixer.dispatchEvent( {
+							type: 'loop', action: this, loopDelta: loopDelta
+						} );
+
+					}
 
 				} else {
 
-					cubeCamera.up.set( 0, upSign[ i ], 0 );
-					cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
+					this.time = time;
 
 				}
 
-				_setViewport( cubeUVRenderTarget,
-					col * SIZE_MAX, i > 2 ? SIZE_MAX : 0, SIZE_MAX, SIZE_MAX );
-				renderer.setRenderTarget( cubeUVRenderTarget );
-				renderer.render( scene, cubeCamera );
+				if ( pingPong && ( loopCount & 1 ) === 1 ) {
+
+					// invert time for the "pong round"
+
+					return duration - time;
+
+				}
 
 			}
 
-			renderer.toneMapping = toneMapping;
-			renderer.toneMappingExposure = toneMappingExposure;
-			renderer.outputEncoding = outputEncoding;
-			renderer.setClearColor( clearColor, clearAlpha );
-			scene.scale.z *= - 1;
+			return time;
 
 		},
 
-		_textureToCubeUV: function ( texture, cubeUVRenderTarget ) {
+		_setEndings: function ( atStart, atEnd, pingPong ) {
 
-			var scene = new Scene();
-			var renderer = this._renderer;
+			var settings = this._interpolantSettings;
 
-			if ( texture.isCubeTexture ) {
+			if ( pingPong ) {
 
-				if ( this._cubemapShader == null ) {
+				settings.endingStart = ZeroSlopeEnding;
+				settings.endingEnd = ZeroSlopeEnding;
 
-					this._cubemapShader = _getCubemapShader();
+			} else {
 
-				}
+				// assuming for LoopOnce atStart == atEnd == true
 
-			} else {
+				if ( atStart ) {
 
-				if ( this._equirectShader == null ) {
+					settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
 
-					this._equirectShader = _getEquirectShader();
+				} else {
+
+					settings.endingStart = WrapAroundEnding;
 
 				}
 
-			}
+				if ( atEnd ) {
 
-			var material = texture.isCubeTexture ? this._cubemapShader : this._equirectShader;
-			scene.add( new Mesh( _lodPlanes[ 0 ], material ) );
-
-			var uniforms = material.uniforms;
+					settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
 
-			uniforms[ 'envMap' ].value = texture;
+				} else {
 
-			if ( ! texture.isCubeTexture ) {
+					settings.endingEnd 	 = WrapAroundEnding;
 
-				uniforms[ 'texelSize' ].value.set( 1.0 / texture.image.width, 1.0 / texture.image.height );
+				}
 
 			}
 
-			uniforms[ 'inputEncoding' ].value = ENCODINGS[ texture.encoding ];
-			uniforms[ 'outputEncoding' ].value = ENCODINGS[ cubeUVRenderTarget.texture.encoding ];
-
-			_setViewport( cubeUVRenderTarget, 0, 0, 3 * SIZE_MAX, 2 * SIZE_MAX );
+		},
 
-			renderer.setRenderTarget( cubeUVRenderTarget );
-			renderer.render( scene, _flatCamera );
+		_scheduleFading: function ( duration, weightNow, weightThen ) {
 
-		},
+			var mixer = this._mixer, now = mixer.time,
+				interpolant = this._weightInterpolant;
 
-		_applyPMREM: function ( cubeUVRenderTarget ) {
+			if ( interpolant === null ) {
 
-			var renderer = this._renderer;
-			var autoClear = renderer.autoClear;
-			renderer.autoClear = false;
+				interpolant = mixer._lendControlInterpolant();
+				this._weightInterpolant = interpolant;
 
-			for ( var i = 1; i < TOTAL_LODS; i ++ ) {
+			}
 
-				var sigma = Math.sqrt( _sigmas[ i ] * _sigmas[ i ] - _sigmas[ i - 1 ] * _sigmas[ i - 1 ] );
+			var times = interpolant.parameterPositions,
+				values = interpolant.sampleValues;
 
-				var poleAxis = _axisDirections[ ( i - 1 ) % _axisDirections.length ];
+			times[ 0 ] = now;
+			values[ 0 ] = weightNow;
+			times[ 1 ] = now + duration;
+			values[ 1 ] = weightThen;
 
-				this._blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis );
+			return this;
 
-			}
+		}
 
-			renderer.autoClear = autoClear;
+	} );
 
-		},
+	/**
+	 *
+	 * Player for AnimationClips.
+	 *
+	 *
+	 * @author Ben Houston / http://clara.io/
+	 * @author David Sarno / http://lighthaus.us/
+	 * @author tschw
+	 */
 
-		/**
-		 * This is a two-pass Gaussian blur for a cubemap. Normally this is done
-		 * vertically and horizontally, but this breaks down on a cube. Here we apply
-		 * the blur latitudinally (around the poles), and then longitudinally (towards
-		 * the poles) to approximate the orthogonally-separable blur. It is least
-		 * accurate at the poles, but still does a decent job.
-		 */
-		_blur: function ( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) {
+	function AnimationMixer( root ) {
 
-			var pingPongRenderTarget = this._pingPongRenderTarget;
+		this._root = root;
+		this._initMemoryManager();
+		this._accuIndex = 0;
 
-			this._halfBlur(
-				cubeUVRenderTarget,
-				pingPongRenderTarget,
-				lodIn,
-				lodOut,
-				sigma,
-				'latitudinal',
-				poleAxis );
+		this.time = 0;
 
-			this._halfBlur(
-				pingPongRenderTarget,
-				cubeUVRenderTarget,
-				lodOut,
-				lodOut,
-				sigma,
-				'longitudinal',
-				poleAxis );
+		this.timeScale = 1.0;
 
-		},
+	}
 
-		_halfBlur: function ( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) {
+	AnimationMixer.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
-			var renderer = this._renderer;
-			var blurMaterial = this._blurMaterial;
+		constructor: AnimationMixer,
 
-			if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
+		_bindAction: function ( action, prototypeAction ) {
 
-				console.error(
-					'blur direction must be either latitudinal or longitudinal!' );
+			var root = action._localRoot || this._root,
+				tracks = action._clip.tracks,
+				nTracks = tracks.length,
+				bindings = action._propertyBindings,
+				interpolants = action._interpolants,
+				rootUuid = root.uuid,
+				bindingsByRoot = this._bindingsByRootAndName,
+				bindingsByName = bindingsByRoot[ rootUuid ];
 
-			}
+			if ( bindingsByName === undefined ) {
 
-			// Number of standard deviations at which to cut off the discrete approximation.
-			var STANDARD_DEVIATIONS = 3;
+				bindingsByName = {};
+				bindingsByRoot[ rootUuid ] = bindingsByName;
 
-			var blurScene = new Scene();
-			blurScene.add( new Mesh( _lodPlanes[ lodOut ], blurMaterial ) );
-			var blurUniforms = blurMaterial.uniforms;
+			}
 
-			var pixels = _sizeLods[ lodIn ] - 1;
-			var radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 );
-			var sigmaPixels = sigmaRadians / radiansPerPixel;
-			var samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES;
+			for ( var i = 0; i !== nTracks; ++ i ) {
 
-			if ( samples > MAX_SAMPLES ) {
+				var track = tracks[ i ],
+					trackName = track.name,
+					binding = bindingsByName[ trackName ];
 
-				console.warn( ("sigmaRadians, " + sigmaRadians + ", is too large and will clip, as it requested " + samples + " samples when the maximum is set to " + MAX_SAMPLES) );
+				if ( binding !== undefined ) {
 
-			}
+					bindings[ i ] = binding;
 
-			var weights = [];
-			var sum = 0;
+				} else {
 
-			for ( var i = 0; i < MAX_SAMPLES; ++ i ) {
+					binding = bindings[ i ];
 
-				var x = i / sigmaPixels;
-				var weight = Math.exp( - x * x / 2 );
-				weights.push( weight );
+					if ( binding !== undefined ) {
 
-				if ( i == 0 ) {
+						// existing binding, make sure the cache knows
 
-					sum += weight;
+						if ( binding._cacheIndex === null ) {
 
-				} else if ( i < samples ) {
+							++ binding.referenceCount;
+							this._addInactiveBinding( binding, rootUuid, trackName );
 
-					sum += 2 * weight;
+						}
 
-				}
+						continue;
 
-			}
+					}
 
-			for ( var i = 0; i < weights.length; i ++ ) {
+					var path = prototypeAction && prototypeAction.
+						_propertyBindings[ i ].binding.parsedPath;
 
-				weights[ i ] = weights[ i ] / sum;
+					binding = new PropertyMixer(
+						PropertyBinding.create( root, trackName, path ),
+						track.ValueTypeName, track.getValueSize() );
 
-			}
+					++ binding.referenceCount;
+					this._addInactiveBinding( binding, rootUuid, trackName );
 
-			blurUniforms[ 'envMap' ].value = targetIn.texture;
-			blurUniforms[ 'samples' ].value = samples;
-			blurUniforms[ 'weights' ].value = weights;
-			blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal';
+					bindings[ i ] = binding;
 
-			if ( poleAxis ) {
+				}
 
-				blurUniforms[ 'poleAxis' ].value = poleAxis;
+				interpolants[ i ].resultBuffer = binding.buffer;
 
 			}
 
-			blurUniforms[ 'dTheta' ].value = radiansPerPixel;
-			blurUniforms[ 'mipInt' ].value = LOD_MAX - lodIn;
-			blurUniforms[ 'inputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
-			blurUniforms[ 'outputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
-
-			var outputSize = _sizeLods[ lodOut ];
-			var x = 3 * Math.max( 0, SIZE_MAX - 2 * outputSize );
-			var y = ( lodOut === 0 ? 0 : 2 * SIZE_MAX ) + 2 * outputSize * ( lodOut > LOD_MAX - LOD_MIN ? lodOut - LOD_MAX + LOD_MIN : 0 );
+		},
 
-			_setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize );
-			renderer.setRenderTarget( targetOut );
-			renderer.render( blurScene, _flatCamera );
+		_activateAction: function ( action ) {
 
-		}
+			if ( ! this._isActiveAction( action ) ) {
 
-	};
+				if ( action._cacheIndex === null ) {
 
-	function _isLDR( texture ) {
+					// this action has been forgotten by the cache, but the user
+					// appears to be still using it -> rebind
 
-		if ( texture === undefined || texture.type !== UnsignedByteType ) { return false; }
+					var rootUuid = ( action._localRoot || this._root ).uuid,
+						clipUuid = action._clip.uuid,
+						actionsForClip = this._actionsByClip[ clipUuid ];
 
-		return texture.encoding === LinearEncoding || texture.encoding === sRGBEncoding || texture.encoding === GammaEncoding;
+					this._bindAction( action,
+						actionsForClip && actionsForClip.knownActions[ 0 ] );
 
-	}
+					this._addInactiveAction( action, clipUuid, rootUuid );
 
-	function _createPlanes() {
+				}
 
-		var _lodPlanes = [];
-		var _sizeLods = [];
-		var _sigmas = [];
+				var bindings = action._propertyBindings;
 
-		var lod = LOD_MAX;
+				// increment reference counts / sort out state
+				for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
 
-		for ( var i = 0; i < TOTAL_LODS; i ++ ) {
+					var binding = bindings[ i ];
 
-			var sizeLod = Math.pow( 2, lod );
-			_sizeLods.push( sizeLod );
-			var sigma = 1.0 / sizeLod;
+					if ( binding.useCount ++ === 0 ) {
 
-			if ( i > LOD_MAX - LOD_MIN ) {
+						this._lendBinding( binding );
+						binding.saveOriginalState();
 
-				sigma = EXTRA_LOD_SIGMA[ i - LOD_MAX + LOD_MIN - 1 ];
+					}
 
-			} else if ( i == 0 ) {
+				}
 
-				sigma = 0;
+				this._lendAction( action );
 
 			}
 
-			_sigmas.push( sigma );
+		},
 
-			var texelSize = 1.0 / ( sizeLod - 1 );
-			var min = - texelSize / 2;
-			var max = 1 + texelSize / 2;
-			var uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
+		_deactivateAction: function ( action ) {
 
-			var cubeFaces = 6;
-			var vertices = 6;
-			var positionSize = 3;
-			var uvSize = 2;
-			var faceIndexSize = 1;
+			if ( this._isActiveAction( action ) ) {
 
-			var position = new Float32Array( positionSize * vertices * cubeFaces );
-			var uv = new Float32Array( uvSize * vertices * cubeFaces );
-			var faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces );
+				var bindings = action._propertyBindings;
 
-			for ( var face = 0; face < cubeFaces; face ++ ) {
+				// decrement reference counts / sort out state
+				for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
 
-				var x = ( face % 3 ) * 2 / 3 - 1;
-				var y = face > 2 ? 0 : - 1;
-				var coordinates = [
-					x, y, 0,
-					x + 2 / 3, y, 0,
-					x + 2 / 3, y + 1, 0,
-					x, y, 0,
-					x + 2 / 3, y + 1, 0,
-					x, y + 1, 0
-				];
-				position.set( coordinates, positionSize * vertices * face );
-				uv.set( uv1, uvSize * vertices * face );
-				var fill = [ face, face, face, face, face, face ];
-				faceIndex.set( fill, faceIndexSize * vertices * face );
+					var binding = bindings[ i ];
 
-			}
+					if ( -- binding.useCount === 0 ) {
 
-			var planes = new BufferGeometry();
-			planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) );
-			planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
-			planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
-			_lodPlanes.push( planes );
+						binding.restoreOriginalState();
+						this._takeBackBinding( binding );
 
-			if ( lod > LOD_MIN ) {
+					}
 
-				lod --;
+				}
+
+				this._takeBackAction( action );
 
 			}
 
-		}
+		},
 
-		return { _lodPlanes: _lodPlanes, _sizeLods: _sizeLods, _sigmas: _sigmas };
+		// Memory manager
 
-	}
+		_initMemoryManager: function () {
 
-	function _createRenderTarget( params ) {
+			this._actions = []; // 'nActiveActions' followed by inactive ones
+			this._nActiveActions = 0;
 
-		var cubeUVRenderTarget = new WebGLRenderTarget( 3 * SIZE_MAX, 3 * SIZE_MAX, params );
-		cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
-		cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
-		cubeUVRenderTarget.scissorTest = true;
-		return cubeUVRenderTarget;
+			this._actionsByClip = {};
+			// inside:
+			// {
+			// 	knownActions: Array< AnimationAction > - used as prototypes
+			// 	actionByRoot: AnimationAction - lookup
+			// }
 
-	}
 
-	function _setViewport( target, x, y, width, height ) {
+			this._bindings = []; // 'nActiveBindings' followed by inactive ones
+			this._nActiveBindings = 0;
 
-		target.viewport.set( x, y, width, height );
-		target.scissor.set( x, y, width, height );
+			this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
 
-	}
 
-	function _getBlurShader( maxSamples ) {
+			this._controlInterpolants = []; // same game as above
+			this._nActiveControlInterpolants = 0;
 
-		var weights = new Float32Array( maxSamples );
-		var poleAxis = new Vector3( 0, 1, 0 );
-		var shaderMaterial = new RawShaderMaterial( {
+			var scope = this;
 
-			defines: { 'n': maxSamples },
+			this.stats = {
 
-			uniforms: {
-				'envMap': { value: null },
-				'samples': { value: 1 },
-				'weights': { value: weights },
-				'latitudinal': { value: false },
-				'dTheta': { value: 0 },
-				'mipInt': { value: 0 },
-				'poleAxis': { value: poleAxis },
-				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
-				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
-			},
+				actions: {
+					get total() {
 
-			vertexShader: _getCommonVertexShader(),
+						return scope._actions.length;
 
-			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform sampler2D envMap;\nuniform int samples;\nuniform float weights[n];\nuniform bool latitudinal;\nuniform float dTheta;\nuniform float mipInt;\nuniform vec3 poleAxis;\n\n" + (_getEncodings()) + "\n\n#define ENVMAP_TYPE_CUBE_UV\n#include <cube_uv_reflection_fragment>\n\nvec3 getSample(float theta, vec3 axis) {\n\tfloat cosTheta = cos(theta);\n\t// Rodrigues' axis-angle rotation\n\tvec3 sampleDirection = vOutputDirection * cosTheta\n\t\t+ cross(axis, vOutputDirection) * sin(theta)\n\t\t+ axis * dot(axis, vOutputDirection) * (1.0 - cosTheta);\n\treturn bilinearCubeUV(envMap, sampleDirection, mipInt);\n}\n\nvoid main() {\n\tvec3 axis = latitudinal ? poleAxis : cross(poleAxis, vOutputDirection);\n\tif (all(equal(axis, vec3(0.0))))\n\t\taxis = vec3(vOutputDirection.z, 0.0, - vOutputDirection.x);\n\taxis = normalize(axis);\n\tgl_FragColor = vec4(0.0);\n\tgl_FragColor.rgb += weights[0] * getSample(0.0, axis);\n\tfor (int i = 1; i < n; i++) {\n\t\tif (i >= samples)\n\t\t\tbreak;\n\t\tfloat theta = dTheta * float(i);\n\t\tgl_FragColor.rgb += weights[i] * getSample(-1.0 * theta, axis);\n\t\tgl_FragColor.rgb += weights[i] * getSample(theta, axis);\n\t}\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+					},
+					get inUse() {
 
-			blending: NoBlending,
-			depthTest: false,
-			depthWrite: false
+						return scope._nActiveActions;
 
-		} );
+					}
+				},
+				bindings: {
+					get total() {
 
-		shaderMaterial.type = 'SphericalGaussianBlur';
+						return scope._bindings.length;
 
-		return shaderMaterial;
+					},
+					get inUse() {
 
-	}
+						return scope._nActiveBindings;
 
-	function _getEquirectShader() {
+					}
+				},
+				controlInterpolants: {
+					get total() {
 
-		var texelSize = new Vector2( 1, 1 );
-		var shaderMaterial = new RawShaderMaterial( {
+						return scope._controlInterpolants.length;
 
-			uniforms: {
-				'envMap': { value: null },
-				'texelSize': { value: texelSize },
-				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
-				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
-			},
+					},
+					get inUse() {
 
-			vertexShader: _getCommonVertexShader(),
+						return scope._nActiveControlInterpolants;
 
-			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform sampler2D envMap;\nuniform vec2 texelSize;\n\n" + (_getEncodings()) + "\n\n#define RECIPROCAL_PI 0.31830988618\n#define RECIPROCAL_PI2 0.15915494\n\nvoid main() {\n\tgl_FragColor = vec4(0.0);\n\tvec3 outputDirection = normalize(vOutputDirection);\n\tvec2 uv;\n\tuv.y = asin(clamp(outputDirection.y, -1.0, 1.0)) * RECIPROCAL_PI + 0.5;\n\tuv.x = atan(outputDirection.z, outputDirection.x) * RECIPROCAL_PI2 + 0.5;\n\tvec2 f = fract(uv / texelSize - 0.5);\n\tuv -= f * texelSize;\n\tvec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.x += texelSize.x;\n\tvec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.y += texelSize.y;\n\tvec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.x -= texelSize.x;\n\tvec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tvec3 tm = mix(tl, tr, f.x);\n\tvec3 bm = mix(bl, br, f.x);\n\tgl_FragColor.rgb = mix(tm, bm, f.y);\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+					}
+				}
 
-			blending: NoBlending,
-			depthTest: false,
-			depthWrite: false
+			};
 
-		} );
+		},
 
-		shaderMaterial.type = 'EquirectangularToCubeUV';
+		// Memory management for AnimationAction objects
 
-		return shaderMaterial;
+		_isActiveAction: function ( action ) {
 
-	}
+			var index = action._cacheIndex;
+			return index !== null && index < this._nActiveActions;
 
-	function _getCubemapShader() {
+		},
 
-		var shaderMaterial = new RawShaderMaterial( {
+		_addInactiveAction: function ( action, clipUuid, rootUuid ) {
 
-			uniforms: {
-				'envMap': { value: null },
-				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
-				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
-			},
+			var actions = this._actions,
+				actionsByClip = this._actionsByClip,
+				actionsForClip = actionsByClip[ clipUuid ];
 
-			vertexShader: _getCommonVertexShader(),
+			if ( actionsForClip === undefined ) {
 
-			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform samplerCube envMap;\n\n" + (_getEncodings()) + "\n\nvoid main() {\n\tgl_FragColor = vec4(0.0);\n\tgl_FragColor.rgb = envMapTexelToLinear(textureCube(envMap, vec3( - vOutputDirection.x, vOutputDirection.yz ))).rgb;\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+				actionsForClip = {
 
-			blending: NoBlending,
-			depthTest: false,
-			depthWrite: false
+					knownActions: [ action ],
+					actionByRoot: {}
 
-		} );
+				};
 
-		shaderMaterial.type = 'CubemapToCubeUV';
+				action._byClipCacheIndex = 0;
 
-		return shaderMaterial;
+				actionsByClip[ clipUuid ] = actionsForClip;
 
-	}
+			} else {
 
-	function _getCommonVertexShader() {
+				var knownActions = actionsForClip.knownActions;
 
-		return "\nprecision mediump float;\nprecision mediump int;\nattribute vec3 position;\nattribute vec2 uv;\nattribute float faceIndex;\nvarying vec3 vOutputDirection;\nvec3 getDirection(vec2 uv, float face) {\n\tuv = 2.0 * uv - 1.0;\n\tvec3 direction = vec3(uv, 1.0);\n\tif (face == 0.0) {\n\t\tdirection = direction.zyx;\n\t\tdirection.z *= -1.0;\n\t} else if (face == 1.0) {\n\t\tdirection = direction.xzy;\n\t\tdirection.z *= -1.0;\n\t} else if (face == 3.0) {\n\t\tdirection = direction.zyx;\n\t\tdirection.x *= -1.0;\n\t} else if (face == 4.0) {\n\t\tdirection = direction.xzy;\n\t\tdirection.y *= -1.0;\n\t} else if (face == 5.0) {\n\t\tdirection.xz *= -1.0;\n\t}\n\treturn direction;\n}\nvoid main() {\n\tvOutputDirection = getDirection(uv, faceIndex);\n\tgl_Position = vec4( position, 1.0 );\n}\n\t";
+				action._byClipCacheIndex = knownActions.length;
+				knownActions.push( action );
 
-	}
+			}
 
-	function _getEncodings() {
+			action._cacheIndex = actions.length;
+			actions.push( action );
 
-		return "\nuniform int inputEncoding;\nuniform int outputEncoding;\n\n#include <encodings_pars_fragment>\n\nvec4 inputTexelToLinear(vec4 value){\n\tif(inputEncoding == 0){\n\t\treturn value;\n\t}else if(inputEncoding == 1){\n\t\treturn sRGBToLinear(value);\n\t}else if(inputEncoding == 2){\n\t\treturn RGBEToLinear(value);\n\t}else if(inputEncoding == 3){\n\t\treturn RGBMToLinear(value, 7.0);\n\t}else if(inputEncoding == 4){\n\t\treturn RGBMToLinear(value, 16.0);\n\t}else if(inputEncoding == 5){\n\t\treturn RGBDToLinear(value, 256.0);\n\t}else{\n\t\treturn GammaToLinear(value, 2.2);\n\t}\n}\n\nvec4 linearToOutputTexel(vec4 value){\n\tif(outputEncoding == 0){\n\t\treturn value;\n\t}else if(outputEncoding == 1){\n\t\treturn LinearTosRGB(value);\n\t}else if(outputEncoding == 2){\n\t\treturn LinearToRGBE(value);\n\t}else if(outputEncoding == 3){\n\t\treturn LinearToRGBM(value, 7.0);\n\t}else if(outputEncoding == 4){\n\t\treturn LinearToRGBM(value, 16.0);\n\t}else if(outputEncoding == 5){\n\t\treturn LinearToRGBD(value, 256.0);\n\t}else{\n\t\treturn LinearToGamma(value, 2.2);\n\t}\n}\n\nvec4 envMapTexelToLinear(vec4 color) {\n\treturn inputTexelToLinear(color);\n}\n\t";
+			actionsForClip.actionByRoot[ rootUuid ] = action;
 
-	}
+		},
 
-	/**
-	 * @author mrdoob / http://mrdoob.com/
-	 */
+		_removeInactiveAction: function ( action ) {
 
-	function Face4( a, b, c, d, normal, color, materialIndex ) {
+			var actions = this._actions,
+				lastInactiveAction = actions[ actions.length - 1 ],
+				cacheIndex = action._cacheIndex;
 
-		console.warn( 'THREE.Face4 has been removed. A THREE.Face3 will be created instead.' );
-		return new Face3( a, b, c, normal, color, materialIndex );
+			lastInactiveAction._cacheIndex = cacheIndex;
+			actions[ cacheIndex ] = lastInactiveAction;
+			actions.pop();
 
-	}
+			action._cacheIndex = null;
 
-	var LineStrip = 0;
-	var LinePieces = 1;
-	var NoColors = 0;
-	var FaceColors = 1;
-	var VertexColors = 2;
 
-	function MeshFaceMaterial( materials ) {
+			var clipUuid = action._clip.uuid,
+				actionsByClip = this._actionsByClip,
+				actionsForClip = actionsByClip[ clipUuid ],
+				knownActionsForClip = actionsForClip.knownActions,
 
-		console.warn( 'THREE.MeshFaceMaterial has been removed. Use an Array instead.' );
-		return materials;
+				lastKnownAction =
+					knownActionsForClip[ knownActionsForClip.length - 1 ],
 
-	}
+				byClipCacheIndex = action._byClipCacheIndex;
 
-	function MultiMaterial( materials ) {
+			lastKnownAction._byClipCacheIndex = byClipCacheIndex;
+			knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
+			knownActionsForClip.pop();
 
-		if ( materials === undefined ) { materials = []; }
+			action._byClipCacheIndex = null;
 
-		console.warn( 'THREE.MultiMaterial has been removed. Use an Array instead.' );
-		materials.isMultiMaterial = true;
-		materials.materials = materials;
-		materials.clone = function () {
 
-			return materials.slice();
+			var actionByRoot = actionsForClip.actionByRoot,
+				rootUuid = ( action._localRoot || this._root ).uuid;
 
-		};
+			delete actionByRoot[ rootUuid ];
 
-		return materials;
+			if ( knownActionsForClip.length === 0 ) {
 
-	}
+				delete actionsByClip[ clipUuid ];
 
-	function PointCloud( geometry, material ) {
+			}
 
-		console.warn( 'THREE.PointCloud has been renamed to THREE.Points.' );
-		return new Points( geometry, material );
+			this._removeInactiveBindingsForAction( action );
 
-	}
+		},
 
-	function Particle( material ) {
+		_removeInactiveBindingsForAction: function ( action ) {
 
-		console.warn( 'THREE.Particle has been renamed to THREE.Sprite.' );
-		return new Sprite( material );
+			var bindings = action._propertyBindings;
+			for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
 
-	}
+				var binding = bindings[ i ];
 
-	function ParticleSystem( geometry, material ) {
+				if ( -- binding.referenceCount === 0 ) {
 
-		console.warn( 'THREE.ParticleSystem has been renamed to THREE.Points.' );
-		return new Points( geometry, material );
+					this._removeInactiveBinding( binding );
 
-	}
+				}
 
-	function PointCloudMaterial( parameters ) {
+			}
 
-		console.warn( 'THREE.PointCloudMaterial has been renamed to THREE.PointsMaterial.' );
-		return new PointsMaterial( parameters );
+		},
 
-	}
+		_lendAction: function ( action ) {
 
-	function ParticleBasicMaterial( parameters ) {
+			// [ active actions |  inactive actions  ]
+			// [  active actions >| inactive actions ]
+			//                 s        a
+			//                  <-swap->
+			//                 a        s
 
-		console.warn( 'THREE.ParticleBasicMaterial has been renamed to THREE.PointsMaterial.' );
-		return new PointsMaterial( parameters );
+			var actions = this._actions,
+				prevIndex = action._cacheIndex,
 
-	}
+				lastActiveIndex = this._nActiveActions ++,
 
-	function ParticleSystemMaterial( parameters ) {
+				firstInactiveAction = actions[ lastActiveIndex ];
 
-		console.warn( 'THREE.ParticleSystemMaterial has been renamed to THREE.PointsMaterial.' );
-		return new PointsMaterial( parameters );
+			action._cacheIndex = lastActiveIndex;
+			actions[ lastActiveIndex ] = action;
 
-	}
+			firstInactiveAction._cacheIndex = prevIndex;
+			actions[ prevIndex ] = firstInactiveAction;
 
-	function Vertex( x, y, z ) {
+		},
 
-		console.warn( 'THREE.Vertex has been removed. Use THREE.Vector3 instead.' );
-		return new Vector3( x, y, z );
+		_takeBackAction: function ( action ) {
 
-	}
+			// [  active actions  | inactive actions ]
+			// [ active actions |< inactive actions  ]
+			//        a        s
+			//         <-swap->
+			//        s        a
 
-	//
+			var actions = this._actions,
+				prevIndex = action._cacheIndex,
 
-	function DynamicBufferAttribute( array, itemSize ) {
+				firstInactiveIndex = -- this._nActiveActions,
 
-		console.warn( 'THREE.DynamicBufferAttribute has been removed. Use new THREE.BufferAttribute().setUsage( THREE.DynamicDrawUsage ) instead.' );
-		return new BufferAttribute( array, itemSize ).setUsage( DynamicDrawUsage );
+				lastActiveAction = actions[ firstInactiveIndex ];
 
-	}
+			action._cacheIndex = firstInactiveIndex;
+			actions[ firstInactiveIndex ] = action;
 
-	function Int8Attribute( array, itemSize ) {
+			lastActiveAction._cacheIndex = prevIndex;
+			actions[ prevIndex ] = lastActiveAction;
 
-		console.warn( 'THREE.Int8Attribute has been removed. Use new THREE.Int8BufferAttribute() instead.' );
-		return new Int8BufferAttribute( array, itemSize );
+		},
 
-	}
+		// Memory management for PropertyMixer objects
 
-	function Uint8Attribute( array, itemSize ) {
+		_addInactiveBinding: function ( binding, rootUuid, trackName ) {
 
-		console.warn( 'THREE.Uint8Attribute has been removed. Use new THREE.Uint8BufferAttribute() instead.' );
-		return new Uint8BufferAttribute( array, itemSize );
+			var bindingsByRoot = this._bindingsByRootAndName,
+				bindingByName = bindingsByRoot[ rootUuid ],
 
-	}
+				bindings = this._bindings;
 
-	function Uint8ClampedAttribute( array, itemSize ) {
+			if ( bindingByName === undefined ) {
 
-		console.warn( 'THREE.Uint8ClampedAttribute has been removed. Use new THREE.Uint8ClampedBufferAttribute() instead.' );
-		return new Uint8ClampedBufferAttribute( array, itemSize );
+				bindingByName = {};
+				bindingsByRoot[ rootUuid ] = bindingByName;
 
-	}
+			}
 
-	function Int16Attribute( array, itemSize ) {
+			bindingByName[ trackName ] = binding;
 
-		console.warn( 'THREE.Int16Attribute has been removed. Use new THREE.Int16BufferAttribute() instead.' );
-		return new Int16BufferAttribute( array, itemSize );
+			binding._cacheIndex = bindings.length;
+			bindings.push( binding );
 
-	}
+		},
 
-	function Uint16Attribute( array, itemSize ) {
+		_removeInactiveBinding: function ( binding ) {
 
-		console.warn( 'THREE.Uint16Attribute has been removed. Use new THREE.Uint16BufferAttribute() instead.' );
-		return new Uint16BufferAttribute( array, itemSize );
+			var bindings = this._bindings,
+				propBinding = binding.binding,
+				rootUuid = propBinding.rootNode.uuid,
+				trackName = propBinding.path,
+				bindingsByRoot = this._bindingsByRootAndName,
+				bindingByName = bindingsByRoot[ rootUuid ],
 
-	}
+				lastInactiveBinding = bindings[ bindings.length - 1 ],
+				cacheIndex = binding._cacheIndex;
 
-	function Int32Attribute( array, itemSize ) {
+			lastInactiveBinding._cacheIndex = cacheIndex;
+			bindings[ cacheIndex ] = lastInactiveBinding;
+			bindings.pop();
 
-		console.warn( 'THREE.Int32Attribute has been removed. Use new THREE.Int32BufferAttribute() instead.' );
-		return new Int32BufferAttribute( array, itemSize );
+			delete bindingByName[ trackName ];
 
-	}
+			if ( Object.keys( bindingByName ).length === 0 ) {
 
-	function Uint32Attribute( array, itemSize ) {
+				delete bindingsByRoot[ rootUuid ];
 
-		console.warn( 'THREE.Uint32Attribute has been removed. Use new THREE.Uint32BufferAttribute() instead.' );
-		return new Uint32BufferAttribute( array, itemSize );
+			}
 
-	}
+		},
 
-	function Float32Attribute( array, itemSize ) {
+		_lendBinding: function ( binding ) {
 
-		console.warn( 'THREE.Float32Attribute has been removed. Use new THREE.Float32BufferAttribute() instead.' );
-		return new Float32BufferAttribute( array, itemSize );
+			var bindings = this._bindings,
+				prevIndex = binding._cacheIndex,
 
-	}
+				lastActiveIndex = this._nActiveBindings ++,
 
-	function Float64Attribute( array, itemSize ) {
+				firstInactiveBinding = bindings[ lastActiveIndex ];
 
-		console.warn( 'THREE.Float64Attribute has been removed. Use new THREE.Float64BufferAttribute() instead.' );
-		return new Float64BufferAttribute( array, itemSize );
+			binding._cacheIndex = lastActiveIndex;
+			bindings[ lastActiveIndex ] = binding;
 
-	}
+			firstInactiveBinding._cacheIndex = prevIndex;
+			bindings[ prevIndex ] = firstInactiveBinding;
 
-	//
+		},
 
-	Curve.create = function ( construct, getPoint ) {
+		_takeBackBinding: function ( binding ) {
 
-		console.log( 'THREE.Curve.create() has been deprecated' );
+			var bindings = this._bindings,
+				prevIndex = binding._cacheIndex,
 
-		construct.prototype = Object.create( Curve.prototype );
-		construct.prototype.constructor = construct;
-		construct.prototype.getPoint = getPoint;
+				firstInactiveIndex = -- this._nActiveBindings,
 
-		return construct;
+				lastActiveBinding = bindings[ firstInactiveIndex ];
 
-	};
+			binding._cacheIndex = firstInactiveIndex;
+			bindings[ firstInactiveIndex ] = binding;
 
-	//
+			lastActiveBinding._cacheIndex = prevIndex;
+			bindings[ prevIndex ] = lastActiveBinding;
 
-	Object.assign( CurvePath.prototype, {
+		},
 
-		createPointsGeometry: function ( divisions ) {
 
-			console.warn( 'THREE.CurvePath: .createPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+		// Memory management of Interpolants for weight and time scale
 
-			// generate geometry from path points (for Line or Points objects)
+		_lendControlInterpolant: function () {
 
-			var pts = this.getPoints( divisions );
-			return this.createGeometry( pts );
+			var interpolants = this._controlInterpolants,
+				lastActiveIndex = this._nActiveControlInterpolants ++,
+				interpolant = interpolants[ lastActiveIndex ];
 
-		},
+			if ( interpolant === undefined ) {
 
-		createSpacedPointsGeometry: function ( divisions ) {
+				interpolant = new LinearInterpolant(
+					new Float32Array( 2 ), new Float32Array( 2 ),
+					1, this._controlInterpolantsResultBuffer );
 
-			console.warn( 'THREE.CurvePath: .createSpacedPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+				interpolant.__cacheIndex = lastActiveIndex;
+				interpolants[ lastActiveIndex ] = interpolant;
 
-			// generate geometry from equidistant sampling along the path
+			}
 
-			var pts = this.getSpacedPoints( divisions );
-			return this.createGeometry( pts );
+			return interpolant;
 
 		},
 
-		createGeometry: function ( points ) {
+		_takeBackControlInterpolant: function ( interpolant ) {
 
-			console.warn( 'THREE.CurvePath: .createGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+			var interpolants = this._controlInterpolants,
+				prevIndex = interpolant.__cacheIndex,
 
-			var geometry = new Geometry();
+				firstInactiveIndex = -- this._nActiveControlInterpolants,
 
-			for ( var i = 0, l = points.length; i < l; i ++ ) {
+				lastActiveInterpolant = interpolants[ firstInactiveIndex ];
 
-				var point = points[ i ];
-				geometry.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
+			interpolant.__cacheIndex = firstInactiveIndex;
+			interpolants[ firstInactiveIndex ] = interpolant;
 
-			}
+			lastActiveInterpolant.__cacheIndex = prevIndex;
+			interpolants[ prevIndex ] = lastActiveInterpolant;
 
-			return geometry;
+		},
 
-		}
+		_controlInterpolantsResultBuffer: new Float32Array( 1 ),
 
-	} );
+		// return an action for a clip optionally using a custom root target
+		// object (this method allocates a lot of dynamic memory in case a
+		// previously unknown clip/root combination is specified)
+		clipAction: function ( clip, optionalRoot, blendMode ) {
 
-	//
+			var root = optionalRoot || this._root,
+				rootUuid = root.uuid,
 
-	Object.assign( Path.prototype, {
+				clipObject = typeof clip === 'string' ?
+					AnimationClip.findByName( root, clip ) : clip,
 
-		fromPoints: function ( points ) {
+				clipUuid = clipObject !== null ? clipObject.uuid : clip,
 
-			console.warn( 'THREE.Path: .fromPoints() has been renamed to .setFromPoints().' );
-			return this.setFromPoints( points );
+				actionsForClip = this._actionsByClip[ clipUuid ],
+				prototypeAction = null;
 
-		}
+			if ( blendMode === undefined ) {
 
-	} );
+				if ( clipObject !== null ) {
 
-	//
+					blendMode = clipObject.blendMode;
 
-	function ClosedSplineCurve3( points ) {
+				} else {
 
-		console.warn( 'THREE.ClosedSplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' );
+					blendMode = NormalAnimationBlendMode;
 
-		CatmullRomCurve3.call( this, points );
-		this.type = 'catmullrom';
-		this.closed = true;
+				}
 
-	}
+			}
 
-	ClosedSplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype );
+			if ( actionsForClip !== undefined ) {
 
-	//
+				var existingAction =
+						actionsForClip.actionByRoot[ rootUuid ];
 
-	function SplineCurve3( points ) {
+				if ( existingAction !== undefined && existingAction.blendMode === blendMode ) {
 
-		console.warn( 'THREE.SplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' );
+					return existingAction;
 
-		CatmullRomCurve3.call( this, points );
-		this.type = 'catmullrom';
+				}
 
-	}
+				// we know the clip, so we don't have to parse all
+				// the bindings again but can just copy
+				prototypeAction = actionsForClip.knownActions[ 0 ];
 
-	SplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype );
+				// also, take the clip from the prototype action
+				if ( clipObject === null )
+					{ clipObject = prototypeAction._clip; }
 
-	//
+			}
 
-	function Spline( points ) {
+			// clip must be known when specified via string
+			if ( clipObject === null ) { return null; }
 
-		console.warn( 'THREE.Spline has been removed. Use THREE.CatmullRomCurve3 instead.' );
+			// allocate all resources required to run it
+			var newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode );
 
-		CatmullRomCurve3.call( this, points );
-		this.type = 'catmullrom';
+			this._bindAction( newAction, prototypeAction );
 
-	}
+			// and make the action known to the memory manager
+			this._addInactiveAction( newAction, clipUuid, rootUuid );
 
-	Spline.prototype = Object.create( CatmullRomCurve3.prototype );
+			return newAction;
 
-	Object.assign( Spline.prototype, {
+		},
 
-		initFromArray: function ( /* a */ ) {
+		// get an existing action
+		existingAction: function ( clip, optionalRoot ) {
 
-			console.error( 'THREE.Spline: .initFromArray() has been removed.' );
+			var root = optionalRoot || this._root,
+				rootUuid = root.uuid,
 
-		},
-		getControlPointsArray: function ( /* optionalTarget */ ) {
+				clipObject = typeof clip === 'string' ?
+					AnimationClip.findByName( root, clip ) : clip,
 
-			console.error( 'THREE.Spline: .getControlPointsArray() has been removed.' );
+				clipUuid = clipObject ? clipObject.uuid : clip,
 
-		},
-		reparametrizeByArcLength: function ( /* samplingCoef */ ) {
+				actionsForClip = this._actionsByClip[ clipUuid ];
 
-			console.error( 'THREE.Spline: .reparametrizeByArcLength() has been removed.' );
+			if ( actionsForClip !== undefined ) {
 
-		}
+				return actionsForClip.actionByRoot[ rootUuid ] || null;
 
-	} );
+			}
 
-	//
+			return null;
 
-	function AxisHelper( size ) {
+		},
 
-		console.warn( 'THREE.AxisHelper has been renamed to THREE.AxesHelper.' );
-		return new AxesHelper( size );
+		// deactivates all previously scheduled actions
+		stopAllAction: function () {
 
-	}
+			var actions = this._actions,
+				nActions = this._nActiveActions,
+				bindings = this._bindings,
+				nBindings = this._nActiveBindings;
 
-	function BoundingBoxHelper( object, color ) {
+			this._nActiveActions = 0;
+			this._nActiveBindings = 0;
 
-		console.warn( 'THREE.BoundingBoxHelper has been deprecated. Creating a THREE.BoxHelper instead.' );
-		return new BoxHelper( object, color );
+			for ( var i = 0; i !== nActions; ++ i ) {
 
-	}
+				actions[ i ].reset();
 
-	function EdgesHelper( object, hex ) {
+			}
 
-		console.warn( 'THREE.EdgesHelper has been removed. Use THREE.EdgesGeometry instead.' );
-		return new LineSegments( new EdgesGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) );
+			for ( var i = 0; i !== nBindings; ++ i ) {
 
-	}
+				bindings[ i ].useCount = 0;
 
-	GridHelper.prototype.setColors = function () {
+			}
 
-		console.error( 'THREE.GridHelper: setColors() has been deprecated, pass them in the constructor instead.' );
+			return this;
 
-	};
+		},
 
-	SkeletonHelper.prototype.update = function () {
+		// advance the time and update apply the animation
+		update: function ( deltaTime ) {
 
-		console.error( 'THREE.SkeletonHelper: update() no longer needs to be called.' );
+			deltaTime *= this.timeScale;
 
-	};
+			var actions = this._actions,
+				nActions = this._nActiveActions,
 
-	function WireframeHelper( object, hex ) {
+				time = this.time += deltaTime,
+				timeDirection = Math.sign( deltaTime ),
 
-		console.warn( 'THREE.WireframeHelper has been removed. Use THREE.WireframeGeometry instead.' );
-		return new LineSegments( new WireframeGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) );
+				accuIndex = this._accuIndex ^= 1;
 
-	}
+			// run active actions
 
-	//
+			for ( var i = 0; i !== nActions; ++ i ) {
 
-	Object.assign( Loader.prototype, {
+				var action = actions[ i ];
 
-		extractUrlBase: function ( url ) {
+				action._update( time, deltaTime, timeDirection, accuIndex );
 
-			console.warn( 'THREE.Loader: .extractUrlBase() has been deprecated. Use THREE.LoaderUtils.extractUrlBase() instead.' );
-			return LoaderUtils.extractUrlBase( url );
+			}
 
-		}
+			// update scene graph
 
-	} );
+			var bindings = this._bindings,
+				nBindings = this._nActiveBindings;
 
-	Loader.Handlers = {
+			for ( var i = 0; i !== nBindings; ++ i ) {
 
-		add: function ( /* regex, loader */ ) {
+				bindings[ i ].apply( accuIndex );
 
-			console.error( 'THREE.Loader: Handlers.add() has been removed. Use LoadingManager.addHandler() instead.' );
+			}
+
+			return this;
 
 		},
 
-		get: function ( /* file */ ) {
+		// Allows you to seek to a specific time in an animation.
+		setTime: function ( timeInSeconds ) {
 
-			console.error( 'THREE.Loader: Handlers.get() has been removed. Use LoadingManager.getHandler() instead.' );
+			this.time = 0; // Zero out time attribute for AnimationMixer object;
+			for ( var i = 0; i < this._actions.length; i ++ ) {
 
-		}
+				this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects.
 
-	};
+			}
 
-	function XHRLoader( manager ) {
+			return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object.
 
-		console.warn( 'THREE.XHRLoader has been renamed to THREE.FileLoader.' );
-		return new FileLoader( manager );
+		},
 
-	}
+		// return this mixer's root target object
+		getRoot: function () {
 
-	function BinaryTextureLoader( manager ) {
+			return this._root;
 
-		console.warn( 'THREE.BinaryTextureLoader has been renamed to THREE.DataTextureLoader.' );
-		return new DataTextureLoader( manager );
+		},
 
-	}
+		// free all resources specific to a particular clip
+		uncacheClip: function ( clip ) {
 
-	Object.assign( ObjectLoader.prototype, {
+			var actions = this._actions,
+				clipUuid = clip.uuid,
+				actionsByClip = this._actionsByClip,
+				actionsForClip = actionsByClip[ clipUuid ];
 
-		setTexturePath: function ( value ) {
+			if ( actionsForClip !== undefined ) {
 
-			console.warn( 'THREE.ObjectLoader: .setTexturePath() has been renamed to .setResourcePath().' );
-			return this.setResourcePath( value );
+				// note: just calling _removeInactiveAction would mess up the
+				// iteration state and also require updating the state we can
+				// just throw away
 
-		}
+				var actionsToRemove = actionsForClip.knownActions;
 
-	} );
+				for ( var i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
 
-	//
+					var action = actionsToRemove[ i ];
 
-	Object.assign( Box2.prototype, {
+					this._deactivateAction( action );
 
-		center: function ( optionalTarget ) {
+					var cacheIndex = action._cacheIndex,
+						lastInactiveAction = actions[ actions.length - 1 ];
 
-			console.warn( 'THREE.Box2: .center() has been renamed to .getCenter().' );
-			return this.getCenter( optionalTarget );
+					action._cacheIndex = null;
+					action._byClipCacheIndex = null;
 
-		},
-		empty: function () {
+					lastInactiveAction._cacheIndex = cacheIndex;
+					actions[ cacheIndex ] = lastInactiveAction;
+					actions.pop();
 
-			console.warn( 'THREE.Box2: .empty() has been renamed to .isEmpty().' );
-			return this.isEmpty();
+					this._removeInactiveBindingsForAction( action );
 
-		},
-		isIntersectionBox: function ( box ) {
+				}
 
-			console.warn( 'THREE.Box2: .isIntersectionBox() has been renamed to .intersectsBox().' );
-			return this.intersectsBox( box );
+				delete actionsByClip[ clipUuid ];
+
+			}
 
 		},
-		size: function ( optionalTarget ) {
 
-			console.warn( 'THREE.Box2: .size() has been renamed to .getSize().' );
-			return this.getSize( optionalTarget );
+		// free all resources specific to a particular root target object
+		uncacheRoot: function ( root ) {
 
-		}
-	} );
+			var rootUuid = root.uuid,
+				actionsByClip = this._actionsByClip;
 
-	Object.assign( Box3.prototype, {
+			for ( var clipUuid in actionsByClip ) {
 
-		center: function ( optionalTarget ) {
+				var actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
+					action = actionByRoot[ rootUuid ];
 
-			console.warn( 'THREE.Box3: .center() has been renamed to .getCenter().' );
-			return this.getCenter( optionalTarget );
+				if ( action !== undefined ) {
 
-		},
-		empty: function () {
+					this._deactivateAction( action );
+					this._removeInactiveAction( action );
 
-			console.warn( 'THREE.Box3: .empty() has been renamed to .isEmpty().' );
-			return this.isEmpty();
+				}
 
-		},
-		isIntersectionBox: function ( box ) {
+			}
 
-			console.warn( 'THREE.Box3: .isIntersectionBox() has been renamed to .intersectsBox().' );
-			return this.intersectsBox( box );
+			var bindingsByRoot = this._bindingsByRootAndName,
+				bindingByName = bindingsByRoot[ rootUuid ];
 
-		},
-		isIntersectionSphere: function ( sphere ) {
+			if ( bindingByName !== undefined ) {
 
-			console.warn( 'THREE.Box3: .isIntersectionSphere() has been renamed to .intersectsSphere().' );
-			return this.intersectsSphere( sphere );
+				for ( var trackName in bindingByName ) {
+
+					var binding = bindingByName[ trackName ];
+					binding.restoreOriginalState();
+					this._removeInactiveBinding( binding );
+
+				}
+
+			}
 
 		},
-		size: function ( optionalTarget ) {
 
-			console.warn( 'THREE.Box3: .size() has been renamed to .getSize().' );
-			return this.getSize( optionalTarget );
+		// remove a targeted clip from the cache
+		uncacheAction: function ( clip, optionalRoot ) {
 
-		}
-	} );
+			var action = this.existingAction( clip, optionalRoot );
 
-	Object.assign( Sphere.prototype, {
+			if ( action !== null ) {
 
-		empty: function () {
+				this._deactivateAction( action );
+				this._removeInactiveAction( action );
 
-			console.warn( 'THREE.Sphere: .empty() has been renamed to .isEmpty().' );
-			return this.isEmpty();
+			}
 
-		},
+		}
 
 	} );
 
-	Frustum.prototype.setFromMatrix = function ( m ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-		console.warn( 'THREE.Frustum: .setFromMatrix() has been renamed to .setFromProjectionMatrix().' );
-		return this.setFromProjectionMatrix( m );
+	function Uniform( value ) {
 
-	};
+		if ( typeof value === 'string' ) {
 
-	Line3.prototype.center = function ( optionalTarget ) {
+			console.warn( 'THREE.Uniform: Type parameter is no longer needed.' );
+			value = arguments[ 1 ];
 
-		console.warn( 'THREE.Line3: .center() has been renamed to .getCenter().' );
-		return this.getCenter( optionalTarget );
+		}
 
-	};
+		this.value = value;
 
-	Object.assign( MathUtils, {
+	}
 
-		random16: function () {
+	Uniform.prototype.clone = function () {
 
-			console.warn( 'THREE.Math: .random16() has been deprecated. Use Math.random() instead.' );
-			return Math.random();
+		return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() );
 
-		},
+	};
 
-		nearestPowerOfTwo: function ( value ) {
+	/**
+	 * @author benaadams / https://twitter.com/ben_a_adams
+	 */
 
-			console.warn( 'THREE.Math: .nearestPowerOfTwo() has been renamed to .floorPowerOfTwo().' );
-			return MathUtils.floorPowerOfTwo( value );
+	function InstancedInterleavedBuffer( array, stride, meshPerAttribute ) {
 
-		},
+		InterleavedBuffer.call( this, array, stride );
 
-		nextPowerOfTwo: function ( value ) {
+		this.meshPerAttribute = meshPerAttribute || 1;
 
-			console.warn( 'THREE.Math: .nextPowerOfTwo() has been renamed to .ceilPowerOfTwo().' );
-			return MathUtils.ceilPowerOfTwo( value );
+	}
 
-		}
+	InstancedInterleavedBuffer.prototype = Object.assign( Object.create( InterleavedBuffer.prototype ), {
 
-	} );
+		constructor: InstancedInterleavedBuffer,
 
-	Object.assign( Matrix3.prototype, {
+		isInstancedInterleavedBuffer: true,
 
-		flattenToArrayOffset: function ( array, offset ) {
+		copy: function ( source ) {
 
-			console.warn( "THREE.Matrix3: .flattenToArrayOffset() has been deprecated. Use .toArray() instead." );
-			return this.toArray( array, offset );
+			InterleavedBuffer.prototype.copy.call( this, source );
 
-		},
-		multiplyVector3: function ( vector ) {
+			this.meshPerAttribute = source.meshPerAttribute;
 
-			console.warn( 'THREE.Matrix3: .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' );
-			return vector.applyMatrix3( this );
+			return this;
 
-		},
-		multiplyVector3Array: function ( /* a */ ) {
+		}
 
-			console.error( 'THREE.Matrix3: .multiplyVector3Array() has been removed.' );
+	} );
 
-		},
-		applyToBufferAttribute: function ( attribute ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author bhouston / http://clara.io/
+	 * @author stephomi / http://stephaneginier.com/
+	 */
 
-			console.warn( 'THREE.Matrix3: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix3( matrix ) instead.' );
-			return attribute.applyMatrix3( this );
+	function Raycaster( origin, direction, near, far ) {
 
-		},
-		applyToVector3Array: function ( /* array, offset, length */ ) {
+		this.ray = new Ray( origin, direction );
+		// direction is assumed to be normalized (for accurate distance calculations)
 
-			console.error( 'THREE.Matrix3: .applyToVector3Array() has been removed.' );
+		this.near = near || 0;
+		this.far = far || Infinity;
+		this.camera = null;
+		this.layers = new Layers();
 
-		}
+		this.params = {
+			Mesh: {},
+			Line: { threshold: 1 },
+			LOD: {},
+			Points: { threshold: 1 },
+			Sprite: {}
+		};
 
-	} );
+		Object.defineProperties( this.params, {
+			PointCloud: {
+				get: function () {
 
-	Object.assign( Matrix4.prototype, {
+					console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
+					return this.Points;
 
-		extractPosition: function ( m ) {
+				}
+			}
+		} );
 
-			console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
-			return this.copyPosition( m );
+	}
 
-		},
-		flattenToArrayOffset: function ( array, offset ) {
+	function ascSort( a, b ) {
 
-			console.warn( "THREE.Matrix4: .flattenToArrayOffset() has been deprecated. Use .toArray() instead." );
-			return this.toArray( array, offset );
+		return a.distance - b.distance;
 
-		},
-		getPosition: function () {
+	}
 
-			console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
-			return new Vector3().setFromMatrixColumn( this, 3 );
+	function intersectObject( object, raycaster, intersects, recursive ) {
 
-		},
-		setRotationFromQuaternion: function ( q ) {
+		if ( object.layers.test( raycaster.layers ) ) {
 
-			console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
-			return this.makeRotationFromQuaternion( q );
+			object.raycast( raycaster, intersects );
 
-		},
-		multiplyToArray: function () {
+		}
 
-			console.warn( 'THREE.Matrix4: .multiplyToArray() has been removed.' );
+		if ( recursive === true ) {
 
-		},
-		multiplyVector3: function ( vector ) {
+			var children = object.children;
 
-			console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
-			return vector.applyMatrix4( this );
+			for ( var i = 0, l = children.length; i < l; i ++ ) {
 
-		},
-		multiplyVector4: function ( vector ) {
+				intersectObject( children[ i ], raycaster, intersects, true );
 
-			console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
-			return vector.applyMatrix4( this );
+			}
 
-		},
-		multiplyVector3Array: function ( /* a */ ) {
+		}
 
-			console.error( 'THREE.Matrix4: .multiplyVector3Array() has been removed.' );
+	}
 
-		},
-		rotateAxis: function ( v ) {
+	Object.assign( Raycaster.prototype, {
 
-			console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
-			v.transformDirection( this );
+		set: function ( origin, direction ) {
 
-		},
-		crossVector: function ( vector ) {
+			// direction is assumed to be normalized (for accurate distance calculations)
 
-			console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
-			return vector.applyMatrix4( this );
+			this.ray.set( origin, direction );
 
 		},
-		translate: function () {
 
-			console.error( 'THREE.Matrix4: .translate() has been removed.' );
+		setFromCamera: function ( coords, camera ) {
 
-		},
-		rotateX: function () {
+			if ( ( camera && camera.isPerspectiveCamera ) ) {
 
-			console.error( 'THREE.Matrix4: .rotateX() has been removed.' );
+				this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
+				this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
+				this.camera = camera;
 
-		},
-		rotateY: function () {
+			} else if ( ( camera && camera.isOrthographicCamera ) ) {
 
-			console.error( 'THREE.Matrix4: .rotateY() has been removed.' );
+				this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
+				this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
+				this.camera = camera;
 
-		},
-		rotateZ: function () {
+			} else {
 
-			console.error( 'THREE.Matrix4: .rotateZ() has been removed.' );
+				console.error( 'THREE.Raycaster: Unsupported camera type.' );
+
+			}
 
 		},
-		rotateByAxis: function () {
 
-			console.error( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
+		intersectObject: function ( object, recursive, optionalTarget ) {
 
-		},
-		applyToBufferAttribute: function ( attribute ) {
+			var intersects = optionalTarget || [];
 
-			console.warn( 'THREE.Matrix4: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix4( matrix ) instead.' );
-			return attribute.applyMatrix4( this );
+			intersectObject( object, this, intersects, recursive );
 
-		},
-		applyToVector3Array: function ( /* array, offset, length */ ) {
+			intersects.sort( ascSort );
 
-			console.error( 'THREE.Matrix4: .applyToVector3Array() has been removed.' );
+			return intersects;
 
 		},
-		makeFrustum: function ( left, right, bottom, top, near, far ) {
 
-			console.warn( 'THREE.Matrix4: .makeFrustum() has been removed. Use .makePerspective( left, right, top, bottom, near, far ) instead.' );
-			return this.makePerspective( left, right, top, bottom, near, far );
+		intersectObjects: function ( objects, recursive, optionalTarget ) {
 
-		}
+			var intersects = optionalTarget || [];
 
-	} );
+			if ( Array.isArray( objects ) === false ) {
 
-	Plane.prototype.isIntersectionLine = function ( line ) {
+				console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
+				return intersects;
 
-		console.warn( 'THREE.Plane: .isIntersectionLine() has been renamed to .intersectsLine().' );
-		return this.intersectsLine( line );
+			}
 
-	};
+			for ( var i = 0, l = objects.length; i < l; i ++ ) {
 
-	Quaternion.prototype.multiplyVector3 = function ( vector ) {
+				intersectObject( objects[ i ], this, intersects, recursive );
 
-		console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
-		return vector.applyQuaternion( this );
+			}
 
-	};
+			intersects.sort( ascSort );
 
-	Object.assign( Ray.prototype, {
+			return intersects;
 
-		isIntersectionBox: function ( box ) {
+		}
 
-			console.warn( 'THREE.Ray: .isIntersectionBox() has been renamed to .intersectsBox().' );
-			return this.intersectsBox( box );
+	} );
 
-		},
-		isIntersectionPlane: function ( plane ) {
+	/**
+	 * @author bhouston / http://clara.io
+	 * @author WestLangley / http://github.com/WestLangley
+	 *
+	 * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
+	 *
+	 * The polar angle (phi) is measured from the positive y-axis. The positive y-axis is up.
+	 * The azimuthal angle (theta) is measured from the positive z-axis.
+	 */
 
-			console.warn( 'THREE.Ray: .isIntersectionPlane() has been renamed to .intersectsPlane().' );
-			return this.intersectsPlane( plane );
+	function Spherical( radius, phi, theta ) {
 
-		},
-		isIntersectionSphere: function ( sphere ) {
+		this.radius = ( radius !== undefined ) ? radius : 1.0;
+		this.phi = ( phi !== undefined ) ? phi : 0; // polar angle
+		this.theta = ( theta !== undefined ) ? theta : 0; // azimuthal angle
 
-			console.warn( 'THREE.Ray: .isIntersectionSphere() has been renamed to .intersectsSphere().' );
-			return this.intersectsSphere( sphere );
+		return this;
 
-		}
+	}
 
-	} );
+	Object.assign( Spherical.prototype, {
 
-	Object.assign( Triangle.prototype, {
+		set: function ( radius, phi, theta ) {
 
-		area: function () {
+			this.radius = radius;
+			this.phi = phi;
+			this.theta = theta;
 
-			console.warn( 'THREE.Triangle: .area() has been renamed to .getArea().' );
-			return this.getArea();
+			return this;
 
 		},
-		barycoordFromPoint: function ( point, target ) {
 
-			console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' );
-			return this.getBarycoord( point, target );
+		clone: function () {
+
+			return new this.constructor().copy( this );
 
 		},
-		midpoint: function ( target ) {
 
-			console.warn( 'THREE.Triangle: .midpoint() has been renamed to .getMidpoint().' );
-			return this.getMidpoint( target );
+		copy: function ( other ) {
 
-		},
-		normal: function ( target ) {
+			this.radius = other.radius;
+			this.phi = other.phi;
+			this.theta = other.theta;
 
-			console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' );
-			return this.getNormal( target );
+			return this;
 
 		},
-		plane: function ( target ) {
 
-			console.warn( 'THREE.Triangle: .plane() has been renamed to .getPlane().' );
-			return this.getPlane( target );
+		// restrict phi to be betwee EPS and PI-EPS
+		makeSafe: function () {
 
-		}
+			var EPS = 0.000001;
+			this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
 
-	} );
+			return this;
 
-	Object.assign( Triangle, {
+		},
 
-		barycoordFromPoint: function ( point, a, b, c, target ) {
+		setFromVector3: function ( v ) {
 
-			console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' );
-			return Triangle.getBarycoord( point, a, b, c, target );
+			return this.setFromCartesianCoords( v.x, v.y, v.z );
 
 		},
-		normal: function ( a, b, c, target ) {
-
-			console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' );
-			return Triangle.getNormal( a, b, c, target );
-
-		}
 
-	} );
+		setFromCartesianCoords: function ( x, y, z ) {
 
-	Object.assign( Shape.prototype, {
+			this.radius = Math.sqrt( x * x + y * y + z * z );
 
-		extractAllPoints: function ( divisions ) {
+			if ( this.radius === 0 ) {
 
-			console.warn( 'THREE.Shape: .extractAllPoints() has been removed. Use .extractPoints() instead.' );
-			return this.extractPoints( divisions );
+				this.theta = 0;
+				this.phi = 0;
 
-		},
-		extrude: function ( options ) {
+			} else {
 
-			console.warn( 'THREE.Shape: .extrude() has been removed. Use ExtrudeGeometry() instead.' );
-			return new ExtrudeGeometry( this, options );
+				this.theta = Math.atan2( x, z );
+				this.phi = Math.acos( MathUtils.clamp( y / this.radius, - 1, 1 ) );
 
-		},
-		makeGeometry: function ( options ) {
+			}
 
-			console.warn( 'THREE.Shape: .makeGeometry() has been removed. Use ShapeGeometry() instead.' );
-			return new ShapeGeometry( this, options );
+			return this;
 
 		}
 
 	} );
 
-	Object.assign( Vector2.prototype, {
+	/**
+	 * @author Mugen87 / https://github.com/Mugen87
+	 *
+	 * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system
+	 *
+	 */
 
-		fromAttribute: function ( attribute, index, offset ) {
+	function Cylindrical( radius, theta, y ) {
 
-			console.warn( 'THREE.Vector2: .fromAttribute() has been renamed to .fromBufferAttribute().' );
-			return this.fromBufferAttribute( attribute, index, offset );
+		this.radius = ( radius !== undefined ) ? radius : 1.0; // distance from the origin to a point in the x-z plane
+		this.theta = ( theta !== undefined ) ? theta : 0; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis
+		this.y = ( y !== undefined ) ? y : 0; // height above the x-z plane
 
-		},
-		distanceToManhattan: function ( v ) {
+		return this;
 
-			console.warn( 'THREE.Vector2: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' );
-			return this.manhattanDistanceTo( v );
+	}
 
-		},
-		lengthManhattan: function () {
+	Object.assign( Cylindrical.prototype, {
 
-			console.warn( 'THREE.Vector2: .lengthManhattan() has been renamed to .manhattanLength().' );
-			return this.manhattanLength();
+		set: function ( radius, theta, y ) {
 
-		}
+			this.radius = radius;
+			this.theta = theta;
+			this.y = y;
 
-	} );
+			return this;
 
-	Object.assign( Vector3.prototype, {
+		},
 
-		setEulerFromRotationMatrix: function () {
+		clone: function () {
 
-			console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
+			return new this.constructor().copy( this );
 
 		},
-		setEulerFromQuaternion: function () {
 
-			console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
+		copy: function ( other ) {
 
-		},
-		getPositionFromMatrix: function ( m ) {
+			this.radius = other.radius;
+			this.theta = other.theta;
+			this.y = other.y;
 
-			console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
-			return this.setFromMatrixPosition( m );
+			return this;
 
 		},
-		getScaleFromMatrix: function ( m ) {
 
-			console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
-			return this.setFromMatrixScale( m );
+		setFromVector3: function ( v ) {
+
+			return this.setFromCartesianCoords( v.x, v.y, v.z );
 
 		},
-		getColumnFromMatrix: function ( index, matrix ) {
 
-			console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
-			return this.setFromMatrixColumn( matrix, index );
+		setFromCartesianCoords: function ( x, y, z ) {
 
-		},
-		applyProjection: function ( m ) {
+			this.radius = Math.sqrt( x * x + z * z );
+			this.theta = Math.atan2( x, z );
+			this.y = y;
 
-			console.warn( 'THREE.Vector3: .applyProjection() has been removed. Use .applyMatrix4( m ) instead.' );
-			return this.applyMatrix4( m );
+			return this;
 
-		},
-		fromAttribute: function ( attribute, index, offset ) {
+		}
 
-			console.warn( 'THREE.Vector3: .fromAttribute() has been renamed to .fromBufferAttribute().' );
-			return this.fromBufferAttribute( attribute, index, offset );
+	} );
 
-		},
-		distanceToManhattan: function ( v ) {
+	/**
+	 * @author bhouston / http://clara.io
+	 */
 
-			console.warn( 'THREE.Vector3: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' );
-			return this.manhattanDistanceTo( v );
+	var _vector$7 = new Vector2();
 
-		},
-		lengthManhattan: function () {
+	function Box2( min, max ) {
 
-			console.warn( 'THREE.Vector3: .lengthManhattan() has been renamed to .manhattanLength().' );
-			return this.manhattanLength();
+		this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
+		this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );
 
-		}
+	}
 
-	} );
+	Object.assign( Box2.prototype, {
 
-	Object.assign( Vector4.prototype, {
+		set: function ( min, max ) {
 
-		fromAttribute: function ( attribute, index, offset ) {
+			this.min.copy( min );
+			this.max.copy( max );
 
-			console.warn( 'THREE.Vector4: .fromAttribute() has been renamed to .fromBufferAttribute().' );
-			return this.fromBufferAttribute( attribute, index, offset );
+			return this;
 
 		},
-		lengthManhattan: function () {
-
-			console.warn( 'THREE.Vector4: .lengthManhattan() has been renamed to .manhattanLength().' );
-			return this.manhattanLength();
 
-		}
+		setFromPoints: function ( points ) {
 
-	} );
+			this.makeEmpty();
 
-	//
+			for ( var i = 0, il = points.length; i < il; i ++ ) {
 
-	Object.assign( Geometry.prototype, {
+				this.expandByPoint( points[ i ] );
 
-		computeTangents: function () {
+			}
 
-			console.error( 'THREE.Geometry: .computeTangents() has been removed.' );
+			return this;
 
 		},
-		computeLineDistances: function () {
 
-			console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' );
+		setFromCenterAndSize: function ( center, size ) {
 
-		},
-		applyMatrix: function ( matrix ) {
+			var halfSize = _vector$7.copy( size ).multiplyScalar( 0.5 );
+			this.min.copy( center ).sub( halfSize );
+			this.max.copy( center ).add( halfSize );
 
-			console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' );
-			return this.applyMatrix4( matrix );
+			return this;
 
-		}
+		},
 
-	} );
+		clone: function () {
 
-	Object.assign( Object3D.prototype, {
+			return new this.constructor().copy( this );
 
-		getChildByName: function ( name ) {
+		},
 
-			console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
-			return this.getObjectByName( name );
+		copy: function ( box ) {
 
-		},
-		renderDepth: function () {
+			this.min.copy( box.min );
+			this.max.copy( box.max );
 
-			console.warn( 'THREE.Object3D: .renderDepth has been removed. Use .renderOrder, instead.' );
+			return this;
 
 		},
-		translate: function ( distance, axis ) {
 
-			console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
-			return this.translateOnAxis( axis, distance );
+		makeEmpty: function () {
 
-		},
-		getWorldRotation: function () {
+			this.min.x = this.min.y = + Infinity;
+			this.max.x = this.max.y = - Infinity;
 
-			console.error( 'THREE.Object3D: .getWorldRotation() has been removed. Use THREE.Object3D.getWorldQuaternion( target ) instead.' );
+			return this;
 
 		},
-		applyMatrix: function ( matrix ) {
-
-			console.warn( 'THREE.Object3D: .applyMatrix() has been renamed to .applyMatrix4().' );
-			return this.applyMatrix4( matrix );
 
-		}
+		isEmpty: function () {
 
-	} );
+			// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
 
-	Object.defineProperties( Object3D.prototype, {
+			return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
 
-		eulerOrder: {
-			get: function () {
+		},
 
-				console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' );
-				return this.rotation.order;
+		getCenter: function ( target ) {
 
-			},
-			set: function ( value ) {
+			if ( target === undefined ) {
 
-				console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' );
-				this.rotation.order = value;
+				console.warn( 'THREE.Box2: .getCenter() target is now required' );
+				target = new Vector2();
 
 			}
+
+			return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
+
 		},
-		useQuaternion: {
-			get: function () {
 
-				console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
+		getSize: function ( target ) {
 
-			},
-			set: function () {
+			if ( target === undefined ) {
 
-				console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
+				console.warn( 'THREE.Box2: .getSize() target is now required' );
+				target = new Vector2();
 
 			}
-		}
 
-	} );
+			return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
 
-	Object.assign( Mesh.prototype, {
+		},
 
-		setDrawMode: function () {
+		expandByPoint: function ( point ) {
 
-			console.error( 'THREE.Mesh: .setDrawMode() has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' );
+			this.min.min( point );
+			this.max.max( point );
+
+			return this;
 
 		},
 
-	} );
+		expandByVector: function ( vector ) {
 
-	Object.defineProperties( Mesh.prototype, {
+			this.min.sub( vector );
+			this.max.add( vector );
 
-		drawMode: {
-			get: function () {
+			return this;
 
-				console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode.' );
-				return TrianglesDrawMode;
+		},
 
-			},
-			set: function () {
+		expandByScalar: function ( scalar ) {
 
-				console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' );
+			this.min.addScalar( - scalar );
+			this.max.addScalar( scalar );
 
-			}
-		}
+			return this;
 
-	} );
+		},
 
-	Object.defineProperties( LOD.prototype, {
+		containsPoint: function ( point ) {
 
-		objects: {
-			get: function () {
+			return point.x < this.min.x || point.x > this.max.x ||
+				point.y < this.min.y || point.y > this.max.y ? false : true;
 
-				console.warn( 'THREE.LOD: .objects has been renamed to .levels.' );
-				return this.levels;
+		},
 
-			}
-		}
-
-	} );
-
-	Object.defineProperty( Skeleton.prototype, 'useVertexTexture', {
-
-		get: function () {
+		containsBox: function ( box ) {
 
-			console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' );
+			return this.min.x <= box.min.x && box.max.x <= this.max.x &&
+				this.min.y <= box.min.y && box.max.y <= this.max.y;
 
 		},
-		set: function () {
 
-			console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' );
+		getParameter: function ( point, target ) {
 
-		}
+			// This can potentially have a divide by zero if the box
+			// has a size dimension of 0.
 
-	} );
+			if ( target === undefined ) {
 
-	SkinnedMesh.prototype.initBones = function () {
+				console.warn( 'THREE.Box2: .getParameter() target is now required' );
+				target = new Vector2();
 
-		console.error( 'THREE.SkinnedMesh: initBones() has been removed.' );
+			}
 
-	};
+			return target.set(
+				( point.x - this.min.x ) / ( this.max.x - this.min.x ),
+				( point.y - this.min.y ) / ( this.max.y - this.min.y )
+			);
 
-	Object.defineProperty( Curve.prototype, '__arcLengthDivisions', {
+		},
 
-		get: function () {
+		intersectsBox: function ( box ) {
 
-			console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
-			return this.arcLengthDivisions;
+			// using 4 splitting planes to rule out intersections
+
+			return box.max.x < this.min.x || box.min.x > this.max.x ||
+				box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
 
 		},
-		set: function ( value ) {
 
-			console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
-			this.arcLengthDivisions = value;
+		clampPoint: function ( point, target ) {
 
-		}
+			if ( target === undefined ) {
 
-	} );
+				console.warn( 'THREE.Box2: .clampPoint() target is now required' );
+				target = new Vector2();
 
-	//
+			}
 
-	PerspectiveCamera.prototype.setLens = function ( focalLength, filmGauge ) {
+			return target.copy( point ).clamp( this.min, this.max );
 
-		console.warn( "THREE.PerspectiveCamera.setLens is deprecated. " +
-				"Use .setFocalLength and .filmGauge for a photographic setup." );
+		},
 
-		if ( filmGauge !== undefined ) { this.filmGauge = filmGauge; }
-		this.setFocalLength( focalLength );
+		distanceToPoint: function ( point ) {
 
-	};
+			var clampedPoint = _vector$7.copy( point ).clamp( this.min, this.max );
+			return clampedPoint.sub( point ).length();
 
-	//
+		},
 
-	Object.defineProperties( Light.prototype, {
-		onlyShadow: {
-			set: function () {
+		intersect: function ( box ) {
 
-				console.warn( 'THREE.Light: .onlyShadow has been removed.' );
+			this.min.max( box.min );
+			this.max.min( box.max );
+
+			return this;
 
-			}
 		},
-		shadowCameraFov: {
-			set: function ( value ) {
 
-				console.warn( 'THREE.Light: .shadowCameraFov is now .shadow.camera.fov.' );
-				this.shadow.camera.fov = value;
+		union: function ( box ) {
 
-			}
-		},
-		shadowCameraLeft: {
-			set: function ( value ) {
+			this.min.min( box.min );
+			this.max.max( box.max );
 
-				console.warn( 'THREE.Light: .shadowCameraLeft is now .shadow.camera.left.' );
-				this.shadow.camera.left = value;
+			return this;
 
-			}
 		},
-		shadowCameraRight: {
-			set: function ( value ) {
 
-				console.warn( 'THREE.Light: .shadowCameraRight is now .shadow.camera.right.' );
-				this.shadow.camera.right = value;
+		translate: function ( offset ) {
 
-			}
-		},
-		shadowCameraTop: {
-			set: function ( value ) {
+			this.min.add( offset );
+			this.max.add( offset );
 
-				console.warn( 'THREE.Light: .shadowCameraTop is now .shadow.camera.top.' );
-				this.shadow.camera.top = value;
+			return this;
 
-			}
 		},
-		shadowCameraBottom: {
-			set: function ( value ) {
 
-				console.warn( 'THREE.Light: .shadowCameraBottom is now .shadow.camera.bottom.' );
-				this.shadow.camera.bottom = value;
+		equals: function ( box ) {
 
-			}
-		},
-		shadowCameraNear: {
-			set: function ( value ) {
+			return box.min.equals( this.min ) && box.max.equals( this.max );
 
-				console.warn( 'THREE.Light: .shadowCameraNear is now .shadow.camera.near.' );
-				this.shadow.camera.near = value;
+		}
 
-			}
-		},
-		shadowCameraFar: {
-			set: function ( value ) {
+	} );
 
-				console.warn( 'THREE.Light: .shadowCameraFar is now .shadow.camera.far.' );
-				this.shadow.camera.far = value;
+	/**
+	 * @author bhouston / http://clara.io
+	 */
 
-			}
-		},
-		shadowCameraVisible: {
-			set: function () {
+	var _startP = new Vector3();
+	var _startEnd = new Vector3();
 
-				console.warn( 'THREE.Light: .shadowCameraVisible has been removed. Use new THREE.CameraHelper( light.shadow.camera ) instead.' );
+	function Line3( start, end ) {
 
-			}
-		},
-		shadowBias: {
-			set: function ( value ) {
+		this.start = ( start !== undefined ) ? start : new Vector3();
+		this.end = ( end !== undefined ) ? end : new Vector3();
 
-				console.warn( 'THREE.Light: .shadowBias is now .shadow.bias.' );
-				this.shadow.bias = value;
+	}
 
-			}
-		},
-		shadowDarkness: {
-			set: function () {
+	Object.assign( Line3.prototype, {
 
-				console.warn( 'THREE.Light: .shadowDarkness has been removed.' );
+		set: function ( start, end ) {
 
-			}
-		},
-		shadowMapWidth: {
-			set: function ( value ) {
+			this.start.copy( start );
+			this.end.copy( end );
 
-				console.warn( 'THREE.Light: .shadowMapWidth is now .shadow.mapSize.width.' );
-				this.shadow.mapSize.width = value;
+			return this;
 
-			}
 		},
-		shadowMapHeight: {
-			set: function ( value ) {
 
-				console.warn( 'THREE.Light: .shadowMapHeight is now .shadow.mapSize.height.' );
-				this.shadow.mapSize.height = value;
+		clone: function () {
 
-			}
-		}
-	} );
+			return new this.constructor().copy( this );
 
-	//
+		},
 
-	Object.defineProperties( BufferAttribute.prototype, {
+		copy: function ( line ) {
 
-		length: {
-			get: function () {
+			this.start.copy( line.start );
+			this.end.copy( line.end );
 
-				console.warn( 'THREE.BufferAttribute: .length has been deprecated. Use .count instead.' );
-				return this.array.length;
+			return this;
 
-			}
 		},
-		dynamic: {
-			get: function () {
 
-				console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' );
-				return this.usage === DynamicDrawUsage;
+		getCenter: function ( target ) {
 
-			},
-			set: function ( /* value */ ) {
+			if ( target === undefined ) {
 
-				console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' );
-				this.setUsage( DynamicDrawUsage );
+				console.warn( 'THREE.Line3: .getCenter() target is now required' );
+				target = new Vector3();
 
 			}
-		}
 
-	} );
+			return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
 
-	Object.assign( BufferAttribute.prototype, {
-		setDynamic: function ( value ) {
+		},
 
-			console.warn( 'THREE.BufferAttribute: .setDynamic() has been deprecated. Use .setUsage() instead.' );
-			this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage );
-			return this;
+		delta: function ( target ) {
 
-		},
-		copyIndicesArray: function ( /* indices */ ) {
+			if ( target === undefined ) {
 
-			console.error( 'THREE.BufferAttribute: .copyIndicesArray() has been removed.' );
+				console.warn( 'THREE.Line3: .delta() target is now required' );
+				target = new Vector3();
+
+			}
+
+			return target.subVectors( this.end, this.start );
 
 		},
-		setArray: function ( /* array */ ) {
 
-			console.error( 'THREE.BufferAttribute: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' );
+		distanceSq: function () {
 
-		}
-	} );
+			return this.start.distanceToSquared( this.end );
 
-	Object.assign( BufferGeometry.prototype, {
+		},
 
-		addIndex: function ( index ) {
+		distance: function () {
 
-			console.warn( 'THREE.BufferGeometry: .addIndex() has been renamed to .setIndex().' );
-			this.setIndex( index );
+			return this.start.distanceTo( this.end );
 
 		},
-		addAttribute: function ( name, attribute ) {
-
-			console.warn( 'THREE.BufferGeometry: .addAttribute() has been renamed to .setAttribute().' );
 
-			if ( ! ( attribute && attribute.isBufferAttribute ) && ! ( attribute && attribute.isInterleavedBufferAttribute ) ) {
+		at: function ( t, target ) {
 
-				console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
+			if ( target === undefined ) {
 
-				return this.setAttribute( name, new BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
+				console.warn( 'THREE.Line3: .at() target is now required' );
+				target = new Vector3();
 
 			}
 
-			if ( name === 'index' ) {
+			return this.delta( target ).multiplyScalar( t ).add( this.start );
 
-				console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' );
-				this.setIndex( attribute );
+		},
 
-				return this;
+		closestPointToPointParameter: function ( point, clampToLine ) {
 
-			}
+			_startP.subVectors( point, this.start );
+			_startEnd.subVectors( this.end, this.start );
 
-			return this.setAttribute( name, attribute );
+			var startEnd2 = _startEnd.dot( _startEnd );
+			var startEnd_startP = _startEnd.dot( _startP );
 
-		},
-		addDrawCall: function ( start, count, indexOffset ) {
+			var t = startEnd_startP / startEnd2;
 
-			if ( indexOffset !== undefined ) {
+			if ( clampToLine ) {
 
-				console.warn( 'THREE.BufferGeometry: .addDrawCall() no longer supports indexOffset.' );
+				t = MathUtils.clamp( t, 0, 1 );
 
 			}
 
-			console.warn( 'THREE.BufferGeometry: .addDrawCall() is now .addGroup().' );
-			this.addGroup( start, count );
+			return t;
 
 		},
-		clearDrawCalls: function () {
 
-			console.warn( 'THREE.BufferGeometry: .clearDrawCalls() is now .clearGroups().' );
-			this.clearGroups();
+		closestPointToPoint: function ( point, clampToLine, target ) {
 
-		},
-		computeTangents: function () {
+			var t = this.closestPointToPointParameter( point, clampToLine );
 
-			console.warn( 'THREE.BufferGeometry: .computeTangents() has been removed.' );
+			if ( target === undefined ) {
 
-		},
-		computeOffsets: function () {
+				console.warn( 'THREE.Line3: .closestPointToPoint() target is now required' );
+				target = new Vector3();
 
-			console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.' );
+			}
+
+			return this.delta( target ).multiplyScalar( t ).add( this.start );
 
 		},
-		removeAttribute: function ( name ) {
 
-			console.warn( 'THREE.BufferGeometry: .removeAttribute() has been renamed to .deleteAttribute().' );
+		applyMatrix4: function ( matrix ) {
 
-			return this.deleteAttribute( name );
+			this.start.applyMatrix4( matrix );
+			this.end.applyMatrix4( matrix );
+
+			return this;
 
 		},
-		applyMatrix: function ( matrix ) {
 
-			console.warn( 'THREE.BufferGeometry: .applyMatrix() has been renamed to .applyMatrix4().' );
-			return this.applyMatrix4( matrix );
+		equals: function ( line ) {
+
+			return line.start.equals( this.start ) && line.end.equals( this.end );
 
 		}
 
 	} );
 
-	Object.defineProperties( BufferGeometry.prototype, {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 */
 
-		drawcalls: {
-			get: function () {
+	function ImmediateRenderObject( material ) {
 
-				console.error( 'THREE.BufferGeometry: .drawcalls has been renamed to .groups.' );
-				return this.groups;
+		Object3D.call( this );
 
-			}
-		},
-		offsets: {
-			get: function () {
+		this.material = material;
+		this.render = function ( /* renderCallback */ ) {};
 
-				console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .groups.' );
-				return this.groups;
+	}
 
-			}
-		}
+	ImmediateRenderObject.prototype = Object.create( Object3D.prototype );
+	ImmediateRenderObject.prototype.constructor = ImmediateRenderObject;
 
-	} );
+	ImmediateRenderObject.prototype.isImmediateRenderObject = true;
 
-	Object.defineProperties( Raycaster.prototype, {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-		linePrecision: {
-			get: function () {
+	var _vector$8 = new Vector3();
 
-				console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' );
-				return this.params.Line.threshold;
+	function SpotLightHelper( light, color ) {
 
-			},
-			set: function ( value ) {
+		Object3D.call( this );
 
-				console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' );
-				this.params.Line.threshold = value;
+		this.light = light;
+		this.light.updateMatrixWorld();
 
-			}
-		}
+		this.matrix = light.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-	} );
+		this.color = color;
 
-	Object.defineProperties( InterleavedBuffer.prototype, {
+		var geometry = new BufferGeometry();
 
-		dynamic: {
-			get: function () {
+		var positions = [
+			0, 0, 0, 	0, 0, 1,
+			0, 0, 0, 	1, 0, 1,
+			0, 0, 0,	- 1, 0, 1,
+			0, 0, 0, 	0, 1, 1,
+			0, 0, 0, 	0, - 1, 1
+		];
 
-				console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' );
-				return this.usage === DynamicDrawUsage;
+		for ( var i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
 
-			},
-			set: function ( value ) {
+			var p1 = ( i / l ) * Math.PI * 2;
+			var p2 = ( j / l ) * Math.PI * 2;
 
-				console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' );
-				this.setUsage( value );
+			positions.push(
+				Math.cos( p1 ), Math.sin( p1 ), 1,
+				Math.cos( p2 ), Math.sin( p2 ), 1
+			);
 
-			}
 		}
 
-	} );
+		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
 
-	Object.assign( InterleavedBuffer.prototype, {
-		setDynamic: function ( value ) {
+		var material = new LineBasicMaterial( { fog: false, toneMapped: false } );
 
-			console.warn( 'THREE.InterleavedBuffer: .setDynamic() has been deprecated. Use .setUsage() instead.' );
-			this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage );
-			return this;
+		this.cone = new LineSegments( geometry, material );
+		this.add( this.cone );
 
-		},
-		setArray: function ( /* array */ ) {
+		this.update();
 
-			console.error( 'THREE.InterleavedBuffer: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' );
+	}
 
-		}
-	} );
+	SpotLightHelper.prototype = Object.create( Object3D.prototype );
+	SpotLightHelper.prototype.constructor = SpotLightHelper;
 
-	//
+	SpotLightHelper.prototype.dispose = function () {
 
-	Object.assign( ExtrudeBufferGeometry.prototype, {
+		this.cone.geometry.dispose();
+		this.cone.material.dispose();
 
-		getArrays: function () {
+	};
 
-			console.error( 'THREE.ExtrudeBufferGeometry: .getArrays() has been removed.' );
+	SpotLightHelper.prototype.update = function () {
 
-		},
+		this.light.updateMatrixWorld();
 
-		addShapeList: function () {
+		var coneLength = this.light.distance ? this.light.distance : 1000;
+		var coneWidth = coneLength * Math.tan( this.light.angle );
 
-			console.error( 'THREE.ExtrudeBufferGeometry: .addShapeList() has been removed.' );
+		this.cone.scale.set( coneWidth, coneWidth, coneLength );
 
-		},
+		_vector$8.setFromMatrixPosition( this.light.target.matrixWorld );
 
-		addShape: function () {
+		this.cone.lookAt( _vector$8 );
 
-			console.error( 'THREE.ExtrudeBufferGeometry: .addShape() has been removed.' );
+		if ( this.color !== undefined ) {
+
+			this.cone.material.color.set( this.color );
+
+		} else {
+
+			this.cone.material.color.copy( this.light.color );
 
 		}
 
-	} );
+	};
 
-	//
+	/**
+	 * @author Sean Griffin / http://twitter.com/sgrif
+	 * @author Michael Guerrero / http://realitymeltdown.com
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author ikerr / http://verold.com
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-	Object.defineProperties( Uniform.prototype, {
+	var _vector$9 = new Vector3();
+	var _boneMatrix = new Matrix4();
+	var _matrixWorldInv = new Matrix4();
 
-		dynamic: {
-			set: function () {
+	function getBoneList( object ) {
 
-				console.warn( 'THREE.Uniform: .dynamic has been removed. Use object.onBeforeRender() instead.' );
+		var boneList = [];
 
-			}
-		},
-		onUpdate: {
-			value: function () {
+		if ( object && object.isBone ) {
 
-				console.warn( 'THREE.Uniform: .onUpdate() has been removed. Use object.onBeforeRender() instead.' );
-				return this;
+			boneList.push( object );
 
-			}
 		}
 
-	} );
-
-	//
+		for ( var i = 0; i < object.children.length; i ++ ) {
 
-	Object.defineProperties( Material.prototype, {
+			boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
 
-		wrapAround: {
-			get: function () {
+		}
 
-				console.warn( 'THREE.Material: .wrapAround has been removed.' );
+		return boneList;
 
-			},
-			set: function () {
+	}
 
-				console.warn( 'THREE.Material: .wrapAround has been removed.' );
+	function SkeletonHelper( object ) {
 
-			}
-		},
+		var bones = getBoneList( object );
 
-		overdraw: {
-			get: function () {
+		var geometry = new BufferGeometry();
 
-				console.warn( 'THREE.Material: .overdraw has been removed.' );
+		var vertices = [];
+		var colors = [];
 
-			},
-			set: function () {
+		var color1 = new Color( 0, 0, 1 );
+		var color2 = new Color( 0, 1, 0 );
 
-				console.warn( 'THREE.Material: .overdraw has been removed.' );
+		for ( var i = 0; i < bones.length; i ++ ) {
 
-			}
-		},
+			var bone = bones[ i ];
 
-		wrapRGB: {
-			get: function () {
+			if ( bone.parent && bone.parent.isBone ) {
 
-				console.warn( 'THREE.Material: .wrapRGB has been removed.' );
-				return new Color();
+				vertices.push( 0, 0, 0 );
+				vertices.push( 0, 0, 0 );
+				colors.push( color1.r, color1.g, color1.b );
+				colors.push( color2.r, color2.g, color2.b );
 
 			}
-		},
 
-		shading: {
-			get: function () {
+		}
 
-				console.error( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
+		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
 
-			},
-			set: function ( value ) {
+		var material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } );
 
-				console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
-				this.flatShading = ( value === FlatShading );
+		LineSegments.call( this, geometry, material );
 
-			}
-		},
+		this.type = 'SkeletonHelper';
 
-		stencilMask: {
-			get: function () {
+		this.root = object;
+		this.bones = bones;
 
-				console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' );
-				return this.stencilFuncMask;
+		this.matrix = object.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-			},
-			set: function ( value ) {
+	}
 
-				console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' );
-				this.stencilFuncMask = value;
-
-			}
-		}
-
-	} );
-
-	Object.defineProperties( MeshPhongMaterial.prototype, {
+	SkeletonHelper.prototype = Object.create( LineSegments.prototype );
+	SkeletonHelper.prototype.constructor = SkeletonHelper;
 
-		metal: {
-			get: function () {
+	SkeletonHelper.prototype.isSkeletonHelper = true;
 
-				console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead.' );
-				return false;
+	SkeletonHelper.prototype.updateMatrixWorld = function ( force ) {
 
-			},
-			set: function () {
+		var bones = this.bones;
 
-				console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead' );
+		var geometry = this.geometry;
+		var position = geometry.getAttribute( 'position' );
 
-			}
-		}
+		_matrixWorldInv.getInverse( this.root.matrixWorld );
 
-	} );
+		for ( var i = 0, j = 0; i < bones.length; i ++ ) {
 
-	Object.defineProperties( ShaderMaterial.prototype, {
+			var bone = bones[ i ];
 
-		derivatives: {
-			get: function () {
+			if ( bone.parent && bone.parent.isBone ) {
 
-				console.warn( 'THREE.ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' );
-				return this.extensions.derivatives;
+				_boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld );
+				_vector$9.setFromMatrixPosition( _boneMatrix );
+				position.setXYZ( j, _vector$9.x, _vector$9.y, _vector$9.z );
 
-			},
-			set: function ( value ) {
+				_boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld );
+				_vector$9.setFromMatrixPosition( _boneMatrix );
+				position.setXYZ( j + 1, _vector$9.x, _vector$9.y, _vector$9.z );
 
-				console.warn( 'THREE. ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' );
-				this.extensions.derivatives = value;
+				j += 2;
 
 			}
+
 		}
 
-	} );
+		geometry.getAttribute( 'position' ).needsUpdate = true;
 
-	//
+		Object3D.prototype.updateMatrixWorld.call( this, force );
 
-	Object.assign( WebGLRenderer.prototype, {
+	};
 
-		clearTarget: function ( renderTarget, color, depth, stencil ) {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			console.warn( 'THREE.WebGLRenderer: .clearTarget() has been deprecated. Use .setRenderTarget() and .clear() instead.' );
-			this.setRenderTarget( renderTarget );
-			this.clear( color, depth, stencil );
+	function PointLightHelper( light, sphereSize, color ) {
 
-		},
-		animate: function ( callback ) {
+		this.light = light;
+		this.light.updateMatrixWorld();
 
-			console.warn( 'THREE.WebGLRenderer: .animate() is now .setAnimationLoop().' );
-			this.setAnimationLoop( callback );
+		this.color = color;
 
-		},
-		getCurrentRenderTarget: function () {
+		var geometry = new SphereBufferGeometry( sphereSize, 4, 2 );
+		var material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
 
-			console.warn( 'THREE.WebGLRenderer: .getCurrentRenderTarget() is now .getRenderTarget().' );
-			return this.getRenderTarget();
+		Mesh.call( this, geometry, material );
 
-		},
-		getMaxAnisotropy: function () {
+		this.type = 'PointLightHelper';
 
-			console.warn( 'THREE.WebGLRenderer: .getMaxAnisotropy() is now .capabilities.getMaxAnisotropy().' );
-			return this.capabilities.getMaxAnisotropy();
+		this.matrix = this.light.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-		},
-		getPrecision: function () {
+		this.update();
 
-			console.warn( 'THREE.WebGLRenderer: .getPrecision() is now .capabilities.precision.' );
-			return this.capabilities.precision;
 
-		},
-		resetGLState: function () {
+		/*
+		var distanceGeometry = new THREE.IcosahedronBufferGeometry( 1, 2 );
+		var distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } );
 
-			console.warn( 'THREE.WebGLRenderer: .resetGLState() is now .state.reset().' );
-			return this.state.reset();
+		this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
+		this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial );
 
-		},
-		supportsFloatTextures: function () {
+		var d = light.distance;
 
-			console.warn( 'THREE.WebGLRenderer: .supportsFloatTextures() is now .extensions.get( \'OES_texture_float\' ).' );
-			return this.extensions.get( 'OES_texture_float' );
+		if ( d === 0.0 ) {
 
-		},
-		supportsHalfFloatTextures: function () {
+			this.lightDistance.visible = false;
 
-			console.warn( 'THREE.WebGLRenderer: .supportsHalfFloatTextures() is now .extensions.get( \'OES_texture_half_float\' ).' );
-			return this.extensions.get( 'OES_texture_half_float' );
+		} else {
 
-		},
-		supportsStandardDerivatives: function () {
+			this.lightDistance.scale.set( d, d, d );
 
-			console.warn( 'THREE.WebGLRenderer: .supportsStandardDerivatives() is now .extensions.get( \'OES_standard_derivatives\' ).' );
-			return this.extensions.get( 'OES_standard_derivatives' );
+		}
 
-		},
-		supportsCompressedTextureS3TC: function () {
+		this.add( this.lightDistance );
+		*/
 
-			console.warn( 'THREE.WebGLRenderer: .supportsCompressedTextureS3TC() is now .extensions.get( \'WEBGL_compressed_texture_s3tc\' ).' );
-			return this.extensions.get( 'WEBGL_compressed_texture_s3tc' );
+	}
 
-		},
-		supportsCompressedTexturePVRTC: function () {
+	PointLightHelper.prototype = Object.create( Mesh.prototype );
+	PointLightHelper.prototype.constructor = PointLightHelper;
 
-			console.warn( 'THREE.WebGLRenderer: .supportsCompressedTexturePVRTC() is now .extensions.get( \'WEBGL_compressed_texture_pvrtc\' ).' );
-			return this.extensions.get( 'WEBGL_compressed_texture_pvrtc' );
+	PointLightHelper.prototype.dispose = function () {
 
-		},
-		supportsBlendMinMax: function () {
+		this.geometry.dispose();
+		this.material.dispose();
 
-			console.warn( 'THREE.WebGLRenderer: .supportsBlendMinMax() is now .extensions.get( \'EXT_blend_minmax\' ).' );
-			return this.extensions.get( 'EXT_blend_minmax' );
+	};
 
-		},
-		supportsVertexTextures: function () {
+	PointLightHelper.prototype.update = function () {
 
-			console.warn( 'THREE.WebGLRenderer: .supportsVertexTextures() is now .capabilities.vertexTextures.' );
-			return this.capabilities.vertexTextures;
+		if ( this.color !== undefined ) {
 
-		},
-		supportsInstancedArrays: function () {
+			this.material.color.set( this.color );
 
-			console.warn( 'THREE.WebGLRenderer: .supportsInstancedArrays() is now .extensions.get( \'ANGLE_instanced_arrays\' ).' );
-			return this.extensions.get( 'ANGLE_instanced_arrays' );
+		} else {
 
-		},
-		enableScissorTest: function ( boolean ) {
+			this.material.color.copy( this.light.color );
 
-			console.warn( 'THREE.WebGLRenderer: .enableScissorTest() is now .setScissorTest().' );
-			this.setScissorTest( boolean );
+		}
 
-		},
-		initMaterial: function () {
+		/*
+		var d = this.light.distance;
 
-			console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
+		if ( d === 0.0 ) {
 
-		},
-		addPrePlugin: function () {
+			this.lightDistance.visible = false;
 
-			console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
+		} else {
 
-		},
-		addPostPlugin: function () {
+			this.lightDistance.visible = true;
+			this.lightDistance.scale.set( d, d, d );
 
-			console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
+		}
+		*/
 
-		},
-		updateShadowMap: function () {
+	};
 
-			console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / https://github.com/Mugen87
+	 */
 
-		},
-		setFaceCulling: function () {
+	var _vector$a = new Vector3();
+	var _color1 = new Color();
+	var _color2 = new Color();
 
-			console.warn( 'THREE.WebGLRenderer: .setFaceCulling() has been removed.' );
+	function HemisphereLightHelper( light, size, color ) {
 
-		},
-		allocTextureUnit: function () {
+		Object3D.call( this );
 
-			console.warn( 'THREE.WebGLRenderer: .allocTextureUnit() has been removed.' );
+		this.light = light;
+		this.light.updateMatrixWorld();
 
-		},
-		setTexture: function () {
+		this.matrix = light.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-			console.warn( 'THREE.WebGLRenderer: .setTexture() has been removed.' );
+		this.color = color;
 
-		},
-		setTexture2D: function () {
+		var geometry = new OctahedronBufferGeometry( size );
+		geometry.rotateY( Math.PI * 0.5 );
 
-			console.warn( 'THREE.WebGLRenderer: .setTexture2D() has been removed.' );
+		this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
+		if ( this.color === undefined ) { this.material.vertexColors = true; }
 
-		},
-		setTextureCube: function () {
+		var position = geometry.getAttribute( 'position' );
+		var colors = new Float32Array( position.count * 3 );
 
-			console.warn( 'THREE.WebGLRenderer: .setTextureCube() has been removed.' );
+		geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
 
-		},
-		getActiveMipMapLevel: function () {
+		this.add( new Mesh( geometry, this.material ) );
 
-			console.warn( 'THREE.WebGLRenderer: .getActiveMipMapLevel() is now .getActiveMipmapLevel().' );
-			return this.getActiveMipmapLevel();
+		this.update();
 
-		}
+	}
 
-	} );
+	HemisphereLightHelper.prototype = Object.create( Object3D.prototype );
+	HemisphereLightHelper.prototype.constructor = HemisphereLightHelper;
 
-	Object.defineProperties( WebGLRenderer.prototype, {
+	HemisphereLightHelper.prototype.dispose = function () {
 
-		shadowMapEnabled: {
-			get: function () {
+		this.children[ 0 ].geometry.dispose();
+		this.children[ 0 ].material.dispose();
 
-				return this.shadowMap.enabled;
+	};
 
-			},
-			set: function ( value ) {
+	HemisphereLightHelper.prototype.update = function () {
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' );
-				this.shadowMap.enabled = value;
+		var mesh = this.children[ 0 ];
 
-			}
-		},
-		shadowMapType: {
-			get: function () {
+		if ( this.color !== undefined ) {
 
-				return this.shadowMap.type;
+			this.material.color.set( this.color );
 
-			},
-			set: function ( value ) {
+		} else {
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' );
-				this.shadowMap.type = value;
+			var colors = mesh.geometry.getAttribute( 'color' );
 
-			}
-		},
-		shadowMapCullFace: {
-			get: function () {
+			_color1.copy( this.light.color );
+			_color2.copy( this.light.groundColor );
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' );
-				return undefined;
+			for ( var i = 0, l = colors.count; i < l; i ++ ) {
 
-			},
-			set: function ( /* value */ ) {
+				var color = ( i < ( l / 2 ) ) ? _color1 : _color2;
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' );
+				colors.setXYZ( i, color.r, color.g, color.b );
 
 			}
-		},
-		context: {
-			get: function () {
 
-				console.warn( 'THREE.WebGLRenderer: .context has been removed. Use .getContext() instead.' );
-				return this.getContext();
+			colors.needsUpdate = true;
 
-			}
-		},
-		vr: {
-			get: function () {
+		}
 
-				console.warn( 'THREE.WebGLRenderer: .vr has been renamed to .xr' );
-				return this.xr;
+		mesh.lookAt( _vector$a.setFromMatrixPosition( this.light.matrixWorld ).negate() );
 
-			}
-		},
-		gammaInput: {
-			get: function () {
+	};
 
-				console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' );
-				return false;
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-			},
-			set: function () {
+	function GridHelper( size, divisions, color1, color2 ) {
 
-				console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' );
+		size = size || 10;
+		divisions = divisions || 10;
+		color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
+		color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
 
-			}
-		},
-		gammaOutput: {
-			get: function () {
+		var center = divisions / 2;
+		var step = size / divisions;
+		var halfSize = size / 2;
 
-				console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' );
-				return false;
+		var vertices = [], colors = [];
 
-			},
-			set: function ( value ) {
+		for ( var i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
 
-				console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' );
-				this.outputEncoding = ( value === true ) ? sRGBEncoding : LinearEncoding;
+			vertices.push( - halfSize, 0, k, halfSize, 0, k );
+			vertices.push( k, 0, - halfSize, k, 0, halfSize );
+
+			var color = i === center ? color1 : color2;
+
+			color.toArray( colors, j ); j += 3;
+			color.toArray( colors, j ); j += 3;
+			color.toArray( colors, j ); j += 3;
+			color.toArray( colors, j ); j += 3;
 
-			}
 		}
 
-	} );
+		var geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
 
-	Object.defineProperties( WebGLShadowMap.prototype, {
+		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
 
-		cullFace: {
-			get: function () {
+		LineSegments.call( this, geometry, material );
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' );
-				return undefined;
+		this.type = 'GridHelper';
 
-			},
-			set: function ( /* cullFace */ ) {
+	}
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' );
+	GridHelper.prototype = Object.assign( Object.create( LineSegments.prototype ), {
 
-			}
-		},
-		renderReverseSided: {
-			get: function () {
+		constructor: GridHelper,
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' );
-				return undefined;
+		copy: function ( source ) {
 
-			},
-			set: function () {
+			LineSegments.prototype.copy.call( this, source );
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' );
+			this.geometry.copy( source.geometry );
+			this.material.copy( source.material );
 
-			}
-		},
-		renderSingleSided: {
-			get: function () {
+			return this;
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' );
-				return undefined;
+		},
 
-			},
-			set: function () {
+		clone: function () {
 
-				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' );
+			return new this.constructor().copy( this );
 
-			}
 		}
 
 	} );
 
-	function WebGLRenderTargetCube( width, height, options ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / http://github.com/Mugen87
+	 * @author Hectate / http://www.github.com/Hectate
+	 */
 
-		console.warn( 'THREE.WebGLRenderTargetCube( width, height, options ) is now WebGLCubeRenderTarget( size, options ).' );
-		return new WebGLCubeRenderTarget( width, options );
+	function PolarGridHelper( radius, radials, circles, divisions, color1, color2 ) {
 
-	}
+		radius = radius || 10;
+		radials = radials || 16;
+		circles = circles || 8;
+		divisions = divisions || 64;
+		color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
+		color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
 
-	//
+		var vertices = [];
+		var colors = [];
 
-	Object.defineProperties( WebGLRenderTarget.prototype, {
+		var x, z;
+		var v, i, j, r, color;
 
-		wrapS: {
-			get: function () {
+		// create the radials
 
-				console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' );
-				return this.texture.wrapS;
+		for ( i = 0; i <= radials; i ++ ) {
 
-			},
-			set: function ( value ) {
+			v = ( i / radials ) * ( Math.PI * 2 );
 
-				console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' );
-				this.texture.wrapS = value;
+			x = Math.sin( v ) * radius;
+			z = Math.cos( v ) * radius;
 
-			}
-		},
-		wrapT: {
-			get: function () {
+			vertices.push( 0, 0, 0 );
+			vertices.push( x, 0, z );
 
-				console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' );
-				return this.texture.wrapT;
+			color = ( i & 1 ) ? color1 : color2;
 
-			},
-			set: function ( value ) {
+			colors.push( color.r, color.g, color.b );
+			colors.push( color.r, color.g, color.b );
 
-				console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' );
-				this.texture.wrapT = value;
+		}
 
-			}
-		},
-		magFilter: {
-			get: function () {
+		// create the circles
 
-				console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' );
-				return this.texture.magFilter;
+		for ( i = 0; i <= circles; i ++ ) {
 
-			},
-			set: function ( value ) {
+			color = ( i & 1 ) ? color1 : color2;
 
-				console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' );
-				this.texture.magFilter = value;
+			r = radius - ( radius / circles * i );
 
-			}
-		},
-		minFilter: {
-			get: function () {
+			for ( j = 0; j < divisions; j ++ ) {
 
-				console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' );
-				return this.texture.minFilter;
+				// first vertex
 
-			},
-			set: function ( value ) {
+				v = ( j / divisions ) * ( Math.PI * 2 );
 
-				console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' );
-				this.texture.minFilter = value;
+				x = Math.sin( v ) * r;
+				z = Math.cos( v ) * r;
 
-			}
-		},
-		anisotropy: {
-			get: function () {
+				vertices.push( x, 0, z );
+				colors.push( color.r, color.g, color.b );
 
-				console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' );
-				return this.texture.anisotropy;
+				// second vertex
 
-			},
-			set: function ( value ) {
+				v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
 
-				console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' );
-				this.texture.anisotropy = value;
+				x = Math.sin( v ) * r;
+				z = Math.cos( v ) * r;
+
+				vertices.push( x, 0, z );
+				colors.push( color.r, color.g, color.b );
 
 			}
-		},
-		offset: {
-			get: function () {
 
-				console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' );
-				return this.texture.offset;
+		}
 
-			},
-			set: function ( value ) {
+		var geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
 
-				console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' );
-				this.texture.offset = value;
+		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
 
-			}
-		},
-		repeat: {
-			get: function () {
+		LineSegments.call( this, geometry, material );
 
-				console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' );
-				return this.texture.repeat;
+		this.type = 'PolarGridHelper';
 
-			},
-			set: function ( value ) {
+	}
 
-				console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' );
-				this.texture.repeat = value;
+	PolarGridHelper.prototype = Object.create( LineSegments.prototype );
+	PolarGridHelper.prototype.constructor = PolarGridHelper;
 
-			}
-		},
-		format: {
-			get: function () {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-				console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' );
-				return this.texture.format;
+	var _v1$5 = new Vector3();
+	var _v2$3 = new Vector3();
+	var _v3$1 = new Vector3();
 
-			},
-			set: function ( value ) {
+	function DirectionalLightHelper( light, size, color ) {
 
-				console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' );
-				this.texture.format = value;
+		Object3D.call( this );
 
-			}
-		},
-		type: {
-			get: function () {
+		this.light = light;
+		this.light.updateMatrixWorld();
 
-				console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' );
-				return this.texture.type;
+		this.matrix = light.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-			},
-			set: function ( value ) {
+		this.color = color;
 
-				console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' );
-				this.texture.type = value;
+		if ( size === undefined ) { size = 1; }
 
-			}
-		},
-		generateMipmaps: {
-			get: function () {
+		var geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( [
+			- size, size, 0,
+			size, size, 0,
+			size, - size, 0,
+			- size, - size, 0,
+			- size, size, 0
+		], 3 ) );
 
-				console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' );
-				return this.texture.generateMipmaps;
+		var material = new LineBasicMaterial( { fog: false, toneMapped: false } );
 
-			},
-			set: function ( value ) {
+		this.lightPlane = new Line( geometry, material );
+		this.add( this.lightPlane );
 
-				console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' );
-				this.texture.generateMipmaps = value;
+		geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
+
+		this.targetLine = new Line( geometry, material );
+		this.add( this.targetLine );
+
+		this.update();
+
+	}
+
+	DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
+	DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
+
+	DirectionalLightHelper.prototype.dispose = function () {
+
+		this.lightPlane.geometry.dispose();
+		this.lightPlane.material.dispose();
+		this.targetLine.geometry.dispose();
+		this.targetLine.material.dispose();
+
+	};
+
+	DirectionalLightHelper.prototype.update = function () {
+
+		_v1$5.setFromMatrixPosition( this.light.matrixWorld );
+		_v2$3.setFromMatrixPosition( this.light.target.matrixWorld );
+		_v3$1.subVectors( _v2$3, _v1$5 );
+
+		this.lightPlane.lookAt( _v2$3 );
+
+		if ( this.color !== undefined ) {
+
+			this.lightPlane.material.color.set( this.color );
+			this.targetLine.material.color.set( this.color );
+
+		} else {
+
+			this.lightPlane.material.color.copy( this.light.color );
+			this.targetLine.material.color.copy( this.light.color );
 
-			}
 		}
 
-	} );
+		this.targetLine.lookAt( _v2$3 );
+		this.targetLine.scale.z = _v3$1.length();
 
-	//
+	};
 
-	Object.defineProperties( Audio.prototype, {
+	/**
+	 * @author alteredq / http://alteredqualia.com/
+	 * @author Mugen87 / https://github.com/Mugen87
+	 *
+	 *	- shows frustum, line of sight and up of the camera
+	 *	- suitable for fast updates
+	 * 	- based on frustum visualization in lightgl.js shadowmap example
+	 *		http://evanw.github.com/lightgl.js/tests/shadowmap.html
+	 */
 
-		load: {
-			value: function ( file ) {
+	var _vector$b = new Vector3();
+	var _camera = new Camera();
 
-				console.warn( 'THREE.Audio: .load has been deprecated. Use THREE.AudioLoader instead.' );
-				var scope = this;
-				var audioLoader = new AudioLoader();
-				audioLoader.load( file, function ( buffer ) {
+	function CameraHelper( camera ) {
 
-					scope.setBuffer( buffer );
+		var geometry = new BufferGeometry();
+		var material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } );
 
-				} );
-				return this;
+		var vertices = [];
+		var colors = [];
 
-			}
-		},
-		startTime: {
-			set: function () {
+		var pointMap = {};
 
-				console.warn( 'THREE.Audio: .startTime is now .play( delay ).' );
+		// colors
+
+		var colorFrustum = new Color( 0xffaa00 );
+		var colorCone = new Color( 0xff0000 );
+		var colorUp = new Color( 0x00aaff );
+		var colorTarget = new Color( 0xffffff );
+		var colorCross = new Color( 0x333333 );
+
+		// near
+
+		addLine( 'n1', 'n2', colorFrustum );
+		addLine( 'n2', 'n4', colorFrustum );
+		addLine( 'n4', 'n3', colorFrustum );
+		addLine( 'n3', 'n1', colorFrustum );
+
+		// far
+
+		addLine( 'f1', 'f2', colorFrustum );
+		addLine( 'f2', 'f4', colorFrustum );
+		addLine( 'f4', 'f3', colorFrustum );
+		addLine( 'f3', 'f1', colorFrustum );
+
+		// sides
+
+		addLine( 'n1', 'f1', colorFrustum );
+		addLine( 'n2', 'f2', colorFrustum );
+		addLine( 'n3', 'f3', colorFrustum );
+		addLine( 'n4', 'f4', colorFrustum );
+
+		// cone
+
+		addLine( 'p', 'n1', colorCone );
+		addLine( 'p', 'n2', colorCone );
+		addLine( 'p', 'n3', colorCone );
+		addLine( 'p', 'n4', colorCone );
+
+		// up
+
+		addLine( 'u1', 'u2', colorUp );
+		addLine( 'u2', 'u3', colorUp );
+		addLine( 'u3', 'u1', colorUp );
+
+		// target
+
+		addLine( 'c', 't', colorTarget );
+		addLine( 'p', 'c', colorCross );
+
+		// cross
+
+		addLine( 'cn1', 'cn2', colorCross );
+		addLine( 'cn3', 'cn4', colorCross );
+
+		addLine( 'cf1', 'cf2', colorCross );
+		addLine( 'cf3', 'cf4', colorCross );
+
+		function addLine( a, b, color ) {
+
+			addPoint( a, color );
+			addPoint( b, color );
+
+		}
+
+		function addPoint( id, color ) {
+
+			vertices.push( 0, 0, 0 );
+			colors.push( color.r, color.g, color.b );
+
+			if ( pointMap[ id ] === undefined ) {
+
+				pointMap[ id ] = [];
 
 			}
+
+			pointMap[ id ].push( ( vertices.length / 3 ) - 1 );
+
 		}
 
-	} );
+		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
 
-	AudioAnalyser.prototype.getData = function () {
+		LineSegments.call( this, geometry, material );
 
-		console.warn( 'THREE.AudioAnalyser: .getData() is now .getFrequencyData().' );
-		return this.getFrequencyData();
+		this.type = 'CameraHelper';
 
-	};
+		this.camera = camera;
+		if ( this.camera.updateProjectionMatrix ) { this.camera.updateProjectionMatrix(); }
 
-	//
+		this.matrix = camera.matrixWorld;
+		this.matrixAutoUpdate = false;
 
-	CubeCamera.prototype.updateCubeMap = function ( renderer, scene ) {
+		this.pointMap = pointMap;
 
-		console.warn( 'THREE.CubeCamera: .updateCubeMap() is now .update().' );
-		return this.update( renderer, scene );
+		this.update();
+
+	}
+
+	CameraHelper.prototype = Object.create( LineSegments.prototype );
+	CameraHelper.prototype.constructor = CameraHelper;
+
+	CameraHelper.prototype.update = function () {
+
+		var geometry = this.geometry;
+		var pointMap = this.pointMap;
+
+		var w = 1, h = 1;
+
+		// we need just camera projection matrix inverse
+		// world matrix must be identity
+
+		_camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );
+
+		// center / target
+
+		setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 );
+		setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );
+
+		// near
+
+		setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 );
+		setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 );
+		setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 );
+		setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 );
+
+		// far
+
+		setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 );
+		setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 );
+		setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 );
+		setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 );
+
+		// up
+
+		setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 );
+		setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 );
+		setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 );
+
+		// cross
+
+		setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 );
+		setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 );
+		setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
+		setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );
+
+		setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 );
+		setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 );
+		setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 );
+		setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 );
+
+		geometry.getAttribute( 'position' ).needsUpdate = true;
 
 	};
 
-	//
+	function setPoint( point, pointMap, geometry, camera, x, y, z ) {
 
-	var GeometryUtils = {
+		_vector$b.set( x, y, z ).unproject( camera );
 
-		merge: function ( geometry1, geometry2, materialIndexOffset ) {
+		var points = pointMap[ point ];
 
-			console.warn( 'THREE.GeometryUtils: .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' );
-			var matrix;
+		if ( points !== undefined ) {
 
-			if ( geometry2.isMesh ) {
+			var position = geometry.getAttribute( 'position' );
 
-				geometry2.matrixAutoUpdate && geometry2.updateMatrix();
+			for ( var i = 0, l = points.length; i < l; i ++ ) {
 
-				matrix = geometry2.matrix;
-				geometry2 = geometry2.geometry;
+				position.setXYZ( points[ i ], _vector$b.x, _vector$b.y, _vector$b.z );
 
 			}
 
-			geometry1.merge( geometry2, matrix, materialIndexOffset );
+		}
 
-		},
+	}
 
-		center: function ( geometry ) {
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 * @author Mugen87 / http://github.com/Mugen87
+	 */
 
-			console.warn( 'THREE.GeometryUtils: .center() has been moved to Geometry. Use geometry.center() instead.' );
-			return geometry.center();
+	var _box$3 = new Box3();
+
+	function BoxHelper( object, color ) {
+
+		this.object = object;
+
+		if ( color === undefined ) { color = 0xffff00; }
+
+		var indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
+		var positions = new Float32Array( 8 * 3 );
+
+		var geometry = new BufferGeometry();
+		geometry.setIndex( new BufferAttribute( indices, 1 ) );
+		geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
+
+		LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+
+		this.type = 'BoxHelper';
+
+		this.matrixAutoUpdate = false;
+
+		this.update();
+
+	}
+
+	BoxHelper.prototype = Object.create( LineSegments.prototype );
+	BoxHelper.prototype.constructor = BoxHelper;
+
+	BoxHelper.prototype.update = function ( object ) {
+
+		if ( object !== undefined ) {
+
+			console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' );
 
 		}
 
-	};
+		if ( this.object !== undefined ) {
 
-	ImageUtils.crossOrigin = undefined;
+			_box$3.setFromObject( this.object );
 
-	ImageUtils.loadTexture = function ( url, mapping, onLoad, onError ) {
+		}
 
-		console.warn( 'THREE.ImageUtils.loadTexture has been deprecated. Use THREE.TextureLoader() instead.' );
+		if ( _box$3.isEmpty() ) { return; }
 
-		var loader = new TextureLoader();
-		loader.setCrossOrigin( this.crossOrigin );
+		var min = _box$3.min;
+		var max = _box$3.max;
 
-		var texture = loader.load( url, onLoad, undefined, onError );
+		/*
+		  5____4
+		1/___0/|
+		| 6__|_7
+		2/___3/
 
-		if ( mapping ) { texture.mapping = mapping; }
+		0: max.x, max.y, max.z
+		1: min.x, max.y, max.z
+		2: min.x, min.y, max.z
+		3: max.x, min.y, max.z
+		4: max.x, max.y, min.z
+		5: min.x, max.y, min.z
+		6: min.x, min.y, min.z
+		7: max.x, min.y, min.z
+		*/
 
-		return texture;
+		var position = this.geometry.attributes.position;
+		var array = position.array;
 
-	};
+		array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
+		array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
+		array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
+		array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
+		array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
+		array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
+		array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
+		array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
 
-	ImageUtils.loadTextureCube = function ( urls, mapping, onLoad, onError ) {
+		position.needsUpdate = true;
 
-		console.warn( 'THREE.ImageUtils.loadTextureCube has been deprecated. Use THREE.CubeTextureLoader() instead.' );
+		this.geometry.computeBoundingSphere();
 
-		var loader = new CubeTextureLoader();
-		loader.setCrossOrigin( this.crossOrigin );
 
-		var texture = loader.load( urls, onLoad, undefined, onError );
+	};
 
-		if ( mapping ) { texture.mapping = mapping; }
+	BoxHelper.prototype.setFromObject = function ( object ) {
 
-		return texture;
+		this.object = object;
+		this.update();
+
+		return this;
 
 	};
 
-	ImageUtils.loadCompressedTexture = function () {
+	BoxHelper.prototype.copy = function ( source ) {
 
-		console.error( 'THREE.ImageUtils.loadCompressedTexture has been removed. Use THREE.DDSLoader instead.' );
+		LineSegments.prototype.copy.call( this, source );
+
+		this.object = source.object;
+
+		return this;
 
 	};
 
-	ImageUtils.loadCompressedTextureCube = function () {
+	BoxHelper.prototype.clone = function () {
 
-		console.error( 'THREE.ImageUtils.loadCompressedTextureCube has been removed. Use THREE.DDSLoader instead.' );
+		return new this.constructor().copy( this );
 
 	};
 
-	//
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-	function CanvasRenderer() {
+	function Box3Helper( box, color ) {
 
-		console.error( 'THREE.CanvasRenderer has been removed' );
+		this.type = 'Box3Helper';
 
-	}
+		this.box = box;
 
-	//
+		color = color || 0xffff00;
 
-	function JSONLoader() {
+		var indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
 
-		console.error( 'THREE.JSONLoader has been removed.' );
+		var positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
 
-	}
+		var geometry = new BufferGeometry();
 
-	//
+		geometry.setIndex( new BufferAttribute( indices, 1 ) );
 
-	var SceneUtils = {
+		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
 
-		createMultiMaterialObject: function ( /* geometry, materials */ ) {
+		LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
 
-			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+		this.type = 'Box3Helper';
 
-		},
+		this.geometry.computeBoundingSphere();
 
-		detach: function ( /* child, parent, scene */ ) {
+	}
 
-			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+	Box3Helper.prototype = Object.create( LineSegments.prototype );
+	Box3Helper.prototype.constructor = Box3Helper;
 
-		},
+	Box3Helper.prototype.updateMatrixWorld = function ( force ) {
 
-		attach: function ( /* child, scene, parent */ ) {
+		var box = this.box;
 
-			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+		if ( box.isEmpty() ) { return; }
 
-		}
+		box.getCenter( this.position );
+
+		box.getSize( this.scale );
+
+		this.scale.multiplyScalar( 0.5 );
+
+		Object3D.prototype.updateMatrixWorld.call( this, force );
 
 	};
 
-	//
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 */
 
-	function LensFlare() {
+	function PlaneHelper( plane, size, hex ) {
 
-		console.error( 'THREE.LensFlare has been moved to /examples/jsm/objects/Lensflare.js' );
+		this.plane = plane;
 
-	}
+		this.size = ( size === undefined ) ? 1 : size;
 
-	if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
+		var color = ( hex !== undefined ) ? hex : 0xffff00;
 
-		/* eslint-disable no-undef */
-		__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: {
-			revision: REVISION,
-		} } ) );
-		/* eslint-enable no-undef */
+		var positions = [ 1, - 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0 ];
+
+		var geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
+		geometry.computeBoundingSphere();
+
+		Line.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+
+		this.type = 'PlaneHelper';
+
+		//
+
+		var positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
+
+		var geometry2 = new BufferGeometry();
+		geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
+		geometry2.computeBoundingSphere();
+
+		this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
 
 	}
 
-	exports.ACESFilmicToneMapping = ACESFilmicToneMapping;
-	exports.AddEquation = AddEquation;
-	exports.AddOperation = AddOperation;
-	exports.AdditiveAnimationBlendMode = AdditiveAnimationBlendMode;
-	exports.AdditiveBlending = AdditiveBlending;
-	exports.AlphaFormat = AlphaFormat;
-	exports.AlwaysDepth = AlwaysDepth;
-	exports.AlwaysStencilFunc = AlwaysStencilFunc;
-	exports.AmbientLight = AmbientLight;
-	exports.AmbientLightProbe = AmbientLightProbe;
-	exports.AnimationClip = AnimationClip;
-	exports.AnimationLoader = AnimationLoader;
-	exports.AnimationMixer = AnimationMixer;
-	exports.AnimationObjectGroup = AnimationObjectGroup;
-	exports.AnimationUtils = AnimationUtils;
-	exports.ArcCurve = ArcCurve;
-	exports.ArrayCamera = ArrayCamera;
-	exports.ArrowHelper = ArrowHelper;
-	exports.Audio = Audio;
-	exports.AudioAnalyser = AudioAnalyser;
-	exports.AudioContext = AudioContext;
-	exports.AudioListener = AudioListener;
-	exports.AudioLoader = AudioLoader;
-	exports.AxesHelper = AxesHelper;
-	exports.AxisHelper = AxisHelper;
-	exports.BackSide = BackSide;
-	exports.BasicDepthPacking = BasicDepthPacking;
-	exports.BasicShadowMap = BasicShadowMap;
-	exports.BinaryTextureLoader = BinaryTextureLoader;
-	exports.Bone = Bone;
-	exports.BooleanKeyframeTrack = BooleanKeyframeTrack;
-	exports.BoundingBoxHelper = BoundingBoxHelper;
-	exports.Box2 = Box2;
-	exports.Box3 = Box3;
-	exports.Box3Helper = Box3Helper;
-	exports.BoxBufferGeometry = BoxBufferGeometry;
-	exports.BoxGeometry = BoxGeometry;
-	exports.BoxHelper = BoxHelper;
-	exports.BufferAttribute = BufferAttribute;
-	exports.BufferGeometry = BufferGeometry;
-	exports.BufferGeometryLoader = BufferGeometryLoader;
-	exports.ByteType = ByteType;
-	exports.Cache = Cache;
-	exports.Camera = Camera;
-	exports.CameraHelper = CameraHelper;
-	exports.CanvasRenderer = CanvasRenderer;
-	exports.CanvasTexture = CanvasTexture;
-	exports.CatmullRomCurve3 = CatmullRomCurve3;
-	exports.CineonToneMapping = CineonToneMapping;
-	exports.CircleBufferGeometry = CircleBufferGeometry;
-	exports.CircleGeometry = CircleGeometry;
-	exports.ClampToEdgeWrapping = ClampToEdgeWrapping;
-	exports.Clock = Clock;
-	exports.ClosedSplineCurve3 = ClosedSplineCurve3;
-	exports.Color = Color;
-	exports.ColorKeyframeTrack = ColorKeyframeTrack;
-	exports.CompressedTexture = CompressedTexture;
-	exports.CompressedTextureLoader = CompressedTextureLoader;
-	exports.ConeBufferGeometry = ConeBufferGeometry;
-	exports.ConeGeometry = ConeGeometry;
-	exports.CubeCamera = CubeCamera;
-	exports.CubeGeometry = BoxGeometry;
-	exports.CubeReflectionMapping = CubeReflectionMapping;
-	exports.CubeRefractionMapping = CubeRefractionMapping;
-	exports.CubeTexture = CubeTexture;
-	exports.CubeTextureLoader = CubeTextureLoader;
-	exports.CubeUVReflectionMapping = CubeUVReflectionMapping;
-	exports.CubeUVRefractionMapping = CubeUVRefractionMapping;
-	exports.CubicBezierCurve = CubicBezierCurve;
-	exports.CubicBezierCurve3 = CubicBezierCurve3;
-	exports.CubicInterpolant = CubicInterpolant;
-	exports.CullFaceBack = CullFaceBack;
-	exports.CullFaceFront = CullFaceFront;
-	exports.CullFaceFrontBack = CullFaceFrontBack;
-	exports.CullFaceNone = CullFaceNone;
-	exports.Curve = Curve;
-	exports.CurvePath = CurvePath;
-	exports.CustomBlending = CustomBlending;
-	exports.CylinderBufferGeometry = CylinderBufferGeometry;
-	exports.CylinderGeometry = CylinderGeometry;
-	exports.Cylindrical = Cylindrical;
-	exports.DataTexture = DataTexture;
-	exports.DataTexture2DArray = DataTexture2DArray;
-	exports.DataTexture3D = DataTexture3D;
-	exports.DataTextureLoader = DataTextureLoader;
-	exports.DecrementStencilOp = DecrementStencilOp;
-	exports.DecrementWrapStencilOp = DecrementWrapStencilOp;
-	exports.DefaultLoadingManager = DefaultLoadingManager;
-	exports.DepthFormat = DepthFormat;
-	exports.DepthStencilFormat = DepthStencilFormat;
-	exports.DepthTexture = DepthTexture;
-	exports.DirectionalLight = DirectionalLight;
-	exports.DirectionalLightHelper = DirectionalLightHelper;
-	exports.DirectionalLightShadow = DirectionalLightShadow;
-	exports.DiscreteInterpolant = DiscreteInterpolant;
-	exports.DodecahedronBufferGeometry = DodecahedronBufferGeometry;
-	exports.DodecahedronGeometry = DodecahedronGeometry;
-	exports.DoubleSide = DoubleSide;
-	exports.DstAlphaFactor = DstAlphaFactor;
-	exports.DstColorFactor = DstColorFactor;
-	exports.DynamicBufferAttribute = DynamicBufferAttribute;
-	exports.DynamicCopyUsage = DynamicCopyUsage;
-	exports.DynamicDrawUsage = DynamicDrawUsage;
-	exports.DynamicReadUsage = DynamicReadUsage;
-	exports.EdgesGeometry = EdgesGeometry;
-	exports.EdgesHelper = EdgesHelper;
-	exports.EllipseCurve = EllipseCurve;
-	exports.EqualDepth = EqualDepth;
-	exports.EqualStencilFunc = EqualStencilFunc;
-	exports.EquirectangularReflectionMapping = EquirectangularReflectionMapping;
-	exports.EquirectangularRefractionMapping = EquirectangularRefractionMapping;
-	exports.Euler = Euler;
-	exports.EventDispatcher = EventDispatcher;
-	exports.ExtrudeBufferGeometry = ExtrudeBufferGeometry;
-	exports.ExtrudeGeometry = ExtrudeGeometry;
-	exports.Face3 = Face3;
-	exports.Face4 = Face4;
-	exports.FaceColors = FaceColors;
-	exports.FileLoader = FileLoader;
-	exports.FlatShading = FlatShading;
-	exports.Float32Attribute = Float32Attribute;
-	exports.Float32BufferAttribute = Float32BufferAttribute;
-	exports.Float64Attribute = Float64Attribute;
-	exports.Float64BufferAttribute = Float64BufferAttribute;
-	exports.FloatType = FloatType;
-	exports.Fog = Fog;
-	exports.FogExp2 = FogExp2;
-	exports.Font = Font;
-	exports.FontLoader = FontLoader;
-	exports.FrontFaceDirectionCCW = FrontFaceDirectionCCW;
-	exports.FrontFaceDirectionCW = FrontFaceDirectionCW;
-	exports.FrontSide = FrontSide;
-	exports.Frustum = Frustum;
-	exports.GammaEncoding = GammaEncoding;
-	exports.Geometry = Geometry;
-	exports.GeometryUtils = GeometryUtils;
-	exports.GreaterDepth = GreaterDepth;
-	exports.GreaterEqualDepth = GreaterEqualDepth;
-	exports.GreaterEqualStencilFunc = GreaterEqualStencilFunc;
-	exports.GreaterStencilFunc = GreaterStencilFunc;
-	exports.GridHelper = GridHelper;
-	exports.Group = Group;
-	exports.HalfFloatType = HalfFloatType;
-	exports.HemisphereLight = HemisphereLight;
-	exports.HemisphereLightHelper = HemisphereLightHelper;
-	exports.HemisphereLightProbe = HemisphereLightProbe;
-	exports.IcosahedronBufferGeometry = IcosahedronBufferGeometry;
-	exports.IcosahedronGeometry = IcosahedronGeometry;
-	exports.ImageBitmapLoader = ImageBitmapLoader;
-	exports.ImageLoader = ImageLoader;
-	exports.ImageUtils = ImageUtils;
-	exports.ImmediateRenderObject = ImmediateRenderObject;
-	exports.IncrementStencilOp = IncrementStencilOp;
-	exports.IncrementWrapStencilOp = IncrementWrapStencilOp;
-	exports.InstancedBufferAttribute = InstancedBufferAttribute;
-	exports.InstancedBufferGeometry = InstancedBufferGeometry;
-	exports.InstancedInterleavedBuffer = InstancedInterleavedBuffer;
-	exports.InstancedMesh = InstancedMesh;
-	exports.Int16Attribute = Int16Attribute;
-	exports.Int16BufferAttribute = Int16BufferAttribute;
-	exports.Int32Attribute = Int32Attribute;
-	exports.Int32BufferAttribute = Int32BufferAttribute;
-	exports.Int8Attribute = Int8Attribute;
-	exports.Int8BufferAttribute = Int8BufferAttribute;
-	exports.IntType = IntType;
-	exports.InterleavedBuffer = InterleavedBuffer;
-	exports.InterleavedBufferAttribute = InterleavedBufferAttribute;
-	exports.Interpolant = Interpolant;
-	exports.InterpolateDiscrete = InterpolateDiscrete;
-	exports.InterpolateLinear = InterpolateLinear;
-	exports.InterpolateSmooth = InterpolateSmooth;
-	exports.InvertStencilOp = InvertStencilOp;
-	exports.JSONLoader = JSONLoader;
-	exports.KeepStencilOp = KeepStencilOp;
-	exports.KeyframeTrack = KeyframeTrack;
-	exports.LOD = LOD;
-	exports.LatheBufferGeometry = LatheBufferGeometry;
-	exports.LatheGeometry = LatheGeometry;
-	exports.Layers = Layers;
-	exports.LensFlare = LensFlare;
-	exports.LessDepth = LessDepth;
-	exports.LessEqualDepth = LessEqualDepth;
-	exports.LessEqualStencilFunc = LessEqualStencilFunc;
-	exports.LessStencilFunc = LessStencilFunc;
-	exports.Light = Light;
-	exports.LightProbe = LightProbe;
-	exports.LightShadow = LightShadow;
-	exports.Line = Line;
-	exports.Line3 = Line3;
-	exports.LineBasicMaterial = LineBasicMaterial;
-	exports.LineCurve = LineCurve;
-	exports.LineCurve3 = LineCurve3;
-	exports.LineDashedMaterial = LineDashedMaterial;
-	exports.LineLoop = LineLoop;
-	exports.LinePieces = LinePieces;
-	exports.LineSegments = LineSegments;
-	exports.LineStrip = LineStrip;
-	exports.LinearEncoding = LinearEncoding;
-	exports.LinearFilter = LinearFilter;
-	exports.LinearInterpolant = LinearInterpolant;
-	exports.LinearMipMapLinearFilter = LinearMipMapLinearFilter;
-	exports.LinearMipMapNearestFilter = LinearMipMapNearestFilter;
-	exports.LinearMipmapLinearFilter = LinearMipmapLinearFilter;
-	exports.LinearMipmapNearestFilter = LinearMipmapNearestFilter;
-	exports.LinearToneMapping = LinearToneMapping;
-	exports.Loader = Loader;
-	exports.LoaderUtils = LoaderUtils;
-	exports.LoadingManager = LoadingManager;
-	exports.LogLuvEncoding = LogLuvEncoding;
-	exports.LoopOnce = LoopOnce;
-	exports.LoopPingPong = LoopPingPong;
-	exports.LoopRepeat = LoopRepeat;
-	exports.LuminanceAlphaFormat = LuminanceAlphaFormat;
-	exports.LuminanceFormat = LuminanceFormat;
-	exports.MOUSE = MOUSE;
-	exports.Material = Material;
-	exports.MaterialLoader = MaterialLoader;
-	exports.Math = MathUtils;
-	exports.MathUtils = MathUtils;
-	exports.Matrix3 = Matrix3;
-	exports.Matrix4 = Matrix4;
-	exports.MaxEquation = MaxEquation;
-	exports.Mesh = Mesh;
-	exports.MeshBasicMaterial = MeshBasicMaterial;
-	exports.MeshDepthMaterial = MeshDepthMaterial;
-	exports.MeshDistanceMaterial = MeshDistanceMaterial;
-	exports.MeshFaceMaterial = MeshFaceMaterial;
-	exports.MeshLambertMaterial = MeshLambertMaterial;
-	exports.MeshMatcapMaterial = MeshMatcapMaterial;
-	exports.MeshNormalMaterial = MeshNormalMaterial;
-	exports.MeshPhongMaterial = MeshPhongMaterial;
-	exports.MeshPhysicalMaterial = MeshPhysicalMaterial;
-	exports.MeshStandardMaterial = MeshStandardMaterial;
-	exports.MeshToonMaterial = MeshToonMaterial;
-	exports.MinEquation = MinEquation;
-	exports.MirroredRepeatWrapping = MirroredRepeatWrapping;
-	exports.MixOperation = MixOperation;
-	exports.MultiMaterial = MultiMaterial;
-	exports.MultiplyBlending = MultiplyBlending;
-	exports.MultiplyOperation = MultiplyOperation;
-	exports.NearestFilter = NearestFilter;
-	exports.NearestMipMapLinearFilter = NearestMipMapLinearFilter;
-	exports.NearestMipMapNearestFilter = NearestMipMapNearestFilter;
-	exports.NearestMipmapLinearFilter = NearestMipmapLinearFilter;
-	exports.NearestMipmapNearestFilter = NearestMipmapNearestFilter;
-	exports.NeverDepth = NeverDepth;
-	exports.NeverStencilFunc = NeverStencilFunc;
-	exports.NoBlending = NoBlending;
-	exports.NoColors = NoColors;
-	exports.NoToneMapping = NoToneMapping;
-	exports.NormalAnimationBlendMode = NormalAnimationBlendMode;
-	exports.NormalBlending = NormalBlending;
-	exports.NotEqualDepth = NotEqualDepth;
-	exports.NotEqualStencilFunc = NotEqualStencilFunc;
-	exports.NumberKeyframeTrack = NumberKeyframeTrack;
-	exports.Object3D = Object3D;
-	exports.ObjectLoader = ObjectLoader;
-	exports.ObjectSpaceNormalMap = ObjectSpaceNormalMap;
-	exports.OctahedronBufferGeometry = OctahedronBufferGeometry;
-	exports.OctahedronGeometry = OctahedronGeometry;
-	exports.OneFactor = OneFactor;
-	exports.OneMinusDstAlphaFactor = OneMinusDstAlphaFactor;
-	exports.OneMinusDstColorFactor = OneMinusDstColorFactor;
-	exports.OneMinusSrcAlphaFactor = OneMinusSrcAlphaFactor;
-	exports.OneMinusSrcColorFactor = OneMinusSrcColorFactor;
-	exports.OrthographicCamera = OrthographicCamera;
-	exports.PCFShadowMap = PCFShadowMap;
-	exports.PCFSoftShadowMap = PCFSoftShadowMap;
-	exports.PMREMGenerator = PMREMGenerator;
-	exports.ParametricBufferGeometry = ParametricBufferGeometry;
-	exports.ParametricGeometry = ParametricGeometry;
-	exports.Particle = Particle;
-	exports.ParticleBasicMaterial = ParticleBasicMaterial;
-	exports.ParticleSystem = ParticleSystem;
-	exports.ParticleSystemMaterial = ParticleSystemMaterial;
-	exports.Path = Path;
-	exports.PerspectiveCamera = PerspectiveCamera;
-	exports.Plane = Plane;
-	exports.PlaneBufferGeometry = PlaneBufferGeometry;
-	exports.PlaneGeometry = PlaneGeometry;
-	exports.PlaneHelper = PlaneHelper;
-	exports.PointCloud = PointCloud;
-	exports.PointCloudMaterial = PointCloudMaterial;
-	exports.PointLight = PointLight;
-	exports.PointLightHelper = PointLightHelper;
-	exports.Points = Points;
-	exports.PointsMaterial = PointsMaterial;
-	exports.PolarGridHelper = PolarGridHelper;
-	exports.PolyhedronBufferGeometry = PolyhedronBufferGeometry;
-	exports.PolyhedronGeometry = PolyhedronGeometry;
-	exports.PositionalAudio = PositionalAudio;
-	exports.PropertyBinding = PropertyBinding;
-	exports.PropertyMixer = PropertyMixer;
-	exports.QuadraticBezierCurve = QuadraticBezierCurve;
-	exports.QuadraticBezierCurve3 = QuadraticBezierCurve3;
-	exports.Quaternion = Quaternion;
-	exports.QuaternionKeyframeTrack = QuaternionKeyframeTrack;
-	exports.QuaternionLinearInterpolant = QuaternionLinearInterpolant;
-	exports.REVISION = REVISION;
-	exports.RGBADepthPacking = RGBADepthPacking;
-	exports.RGBAFormat = RGBAFormat;
-	exports.RGBAIntegerFormat = RGBAIntegerFormat;
-	exports.RGBA_ASTC_10x10_Format = RGBA_ASTC_10x10_Format;
-	exports.RGBA_ASTC_10x5_Format = RGBA_ASTC_10x5_Format;
-	exports.RGBA_ASTC_10x6_Format = RGBA_ASTC_10x6_Format;
-	exports.RGBA_ASTC_10x8_Format = RGBA_ASTC_10x8_Format;
-	exports.RGBA_ASTC_12x10_Format = RGBA_ASTC_12x10_Format;
-	exports.RGBA_ASTC_12x12_Format = RGBA_ASTC_12x12_Format;
-	exports.RGBA_ASTC_4x4_Format = RGBA_ASTC_4x4_Format;
-	exports.RGBA_ASTC_5x4_Format = RGBA_ASTC_5x4_Format;
-	exports.RGBA_ASTC_5x5_Format = RGBA_ASTC_5x5_Format;
-	exports.RGBA_ASTC_6x5_Format = RGBA_ASTC_6x5_Format;
-	exports.RGBA_ASTC_6x6_Format = RGBA_ASTC_6x6_Format;
-	exports.RGBA_ASTC_8x5_Format = RGBA_ASTC_8x5_Format;
-	exports.RGBA_ASTC_8x6_Format = RGBA_ASTC_8x6_Format;
-	exports.RGBA_ASTC_8x8_Format = RGBA_ASTC_8x8_Format;
-	exports.RGBA_BPTC_Format = RGBA_BPTC_Format;
-	exports.RGBA_ETC2_EAC_Format = RGBA_ETC2_EAC_Format;
-	exports.RGBA_PVRTC_2BPPV1_Format = RGBA_PVRTC_2BPPV1_Format;
-	exports.RGBA_PVRTC_4BPPV1_Format = RGBA_PVRTC_4BPPV1_Format;
-	exports.RGBA_S3TC_DXT1_Format = RGBA_S3TC_DXT1_Format;
-	exports.RGBA_S3TC_DXT3_Format = RGBA_S3TC_DXT3_Format;
-	exports.RGBA_S3TC_DXT5_Format = RGBA_S3TC_DXT5_Format;
-	exports.RGBDEncoding = RGBDEncoding;
-	exports.RGBEEncoding = RGBEEncoding;
-	exports.RGBEFormat = RGBEFormat;
-	exports.RGBFormat = RGBFormat;
-	exports.RGBIntegerFormat = RGBIntegerFormat;
-	exports.RGBM16Encoding = RGBM16Encoding;
-	exports.RGBM7Encoding = RGBM7Encoding;
-	exports.RGB_ETC1_Format = RGB_ETC1_Format;
-	exports.RGB_ETC2_Format = RGB_ETC2_Format;
-	exports.RGB_PVRTC_2BPPV1_Format = RGB_PVRTC_2BPPV1_Format;
-	exports.RGB_PVRTC_4BPPV1_Format = RGB_PVRTC_4BPPV1_Format;
-	exports.RGB_S3TC_DXT1_Format = RGB_S3TC_DXT1_Format;
-	exports.RGFormat = RGFormat;
-	exports.RGIntegerFormat = RGIntegerFormat;
-	exports.RawShaderMaterial = RawShaderMaterial;
-	exports.Ray = Ray;
-	exports.Raycaster = Raycaster;
-	exports.RectAreaLight = RectAreaLight;
-	exports.RedFormat = RedFormat;
-	exports.RedIntegerFormat = RedIntegerFormat;
-	exports.ReinhardToneMapping = ReinhardToneMapping;
-	exports.RepeatWrapping = RepeatWrapping;
-	exports.ReplaceStencilOp = ReplaceStencilOp;
-	exports.ReverseSubtractEquation = ReverseSubtractEquation;
-	exports.RingBufferGeometry = RingBufferGeometry;
-	exports.RingGeometry = RingGeometry;
-	exports.SRGB8_ALPHA8_ASTC_10x10_Format = SRGB8_ALPHA8_ASTC_10x10_Format;
-	exports.SRGB8_ALPHA8_ASTC_10x5_Format = SRGB8_ALPHA8_ASTC_10x5_Format;
-	exports.SRGB8_ALPHA8_ASTC_10x6_Format = SRGB8_ALPHA8_ASTC_10x6_Format;
-	exports.SRGB8_ALPHA8_ASTC_10x8_Format = SRGB8_ALPHA8_ASTC_10x8_Format;
-	exports.SRGB8_ALPHA8_ASTC_12x10_Format = SRGB8_ALPHA8_ASTC_12x10_Format;
-	exports.SRGB8_ALPHA8_ASTC_12x12_Format = SRGB8_ALPHA8_ASTC_12x12_Format;
-	exports.SRGB8_ALPHA8_ASTC_4x4_Format = SRGB8_ALPHA8_ASTC_4x4_Format;
-	exports.SRGB8_ALPHA8_ASTC_5x4_Format = SRGB8_ALPHA8_ASTC_5x4_Format;
-	exports.SRGB8_ALPHA8_ASTC_5x5_Format = SRGB8_ALPHA8_ASTC_5x5_Format;
-	exports.SRGB8_ALPHA8_ASTC_6x5_Format = SRGB8_ALPHA8_ASTC_6x5_Format;
-	exports.SRGB8_ALPHA8_ASTC_6x6_Format = SRGB8_ALPHA8_ASTC_6x6_Format;
-	exports.SRGB8_ALPHA8_ASTC_8x5_Format = SRGB8_ALPHA8_ASTC_8x5_Format;
-	exports.SRGB8_ALPHA8_ASTC_8x6_Format = SRGB8_ALPHA8_ASTC_8x6_Format;
-	exports.SRGB8_ALPHA8_ASTC_8x8_Format = SRGB8_ALPHA8_ASTC_8x8_Format;
-	exports.Scene = Scene;
-	exports.SceneUtils = SceneUtils;
-	exports.ShaderChunk = ShaderChunk;
-	exports.ShaderLib = ShaderLib;
-	exports.ShaderMaterial = ShaderMaterial;
-	exports.ShadowMaterial = ShadowMaterial;
-	exports.Shape = Shape;
-	exports.ShapeBufferGeometry = ShapeBufferGeometry;
-	exports.ShapeGeometry = ShapeGeometry;
-	exports.ShapePath = ShapePath;
-	exports.ShapeUtils = ShapeUtils;
-	exports.ShortType = ShortType;
-	exports.Skeleton = Skeleton;
-	exports.SkeletonHelper = SkeletonHelper;
-	exports.SkinnedMesh = SkinnedMesh;
-	exports.SmoothShading = SmoothShading;
-	exports.Sphere = Sphere;
-	exports.SphereBufferGeometry = SphereBufferGeometry;
-	exports.SphereGeometry = SphereGeometry;
-	exports.Spherical = Spherical;
-	exports.SphericalHarmonics3 = SphericalHarmonics3;
-	exports.SphericalReflectionMapping = SphericalReflectionMapping;
-	exports.Spline = Spline;
-	exports.SplineCurve = SplineCurve;
-	exports.SplineCurve3 = SplineCurve3;
-	exports.SpotLight = SpotLight;
-	exports.SpotLightHelper = SpotLightHelper;
-	exports.SpotLightShadow = SpotLightShadow;
-	exports.Sprite = Sprite;
-	exports.SpriteMaterial = SpriteMaterial;
-	exports.SrcAlphaFactor = SrcAlphaFactor;
-	exports.SrcAlphaSaturateFactor = SrcAlphaSaturateFactor;
-	exports.SrcColorFactor = SrcColorFactor;
-	exports.StaticCopyUsage = StaticCopyUsage;
-	exports.StaticDrawUsage = StaticDrawUsage;
-	exports.StaticReadUsage = StaticReadUsage;
-	exports.StereoCamera = StereoCamera;
-	exports.StreamCopyUsage = StreamCopyUsage;
-	exports.StreamDrawUsage = StreamDrawUsage;
-	exports.StreamReadUsage = StreamReadUsage;
-	exports.StringKeyframeTrack = StringKeyframeTrack;
-	exports.SubtractEquation = SubtractEquation;
-	exports.SubtractiveBlending = SubtractiveBlending;
-	exports.TOUCH = TOUCH;
-	exports.TangentSpaceNormalMap = TangentSpaceNormalMap;
-	exports.TetrahedronBufferGeometry = TetrahedronBufferGeometry;
-	exports.TetrahedronGeometry = TetrahedronGeometry;
-	exports.TextBufferGeometry = TextBufferGeometry;
-	exports.TextGeometry = TextGeometry;
-	exports.Texture = Texture;
-	exports.TextureLoader = TextureLoader;
-	exports.TorusBufferGeometry = TorusBufferGeometry;
-	exports.TorusGeometry = TorusGeometry;
-	exports.TorusKnotBufferGeometry = TorusKnotBufferGeometry;
-	exports.TorusKnotGeometry = TorusKnotGeometry;
-	exports.Triangle = Triangle;
-	exports.TriangleFanDrawMode = TriangleFanDrawMode;
-	exports.TriangleStripDrawMode = TriangleStripDrawMode;
-	exports.TrianglesDrawMode = TrianglesDrawMode;
-	exports.TubeBufferGeometry = TubeBufferGeometry;
-	exports.TubeGeometry = TubeGeometry;
-	exports.UVMapping = UVMapping;
-	exports.Uint16Attribute = Uint16Attribute;
-	exports.Uint16BufferAttribute = Uint16BufferAttribute;
-	exports.Uint32Attribute = Uint32Attribute;
-	exports.Uint32BufferAttribute = Uint32BufferAttribute;
-	exports.Uint8Attribute = Uint8Attribute;
-	exports.Uint8BufferAttribute = Uint8BufferAttribute;
-	exports.Uint8ClampedAttribute = Uint8ClampedAttribute;
-	exports.Uint8ClampedBufferAttribute = Uint8ClampedBufferAttribute;
-	exports.Uncharted2ToneMapping = Uncharted2ToneMapping;
-	exports.Uniform = Uniform;
-	exports.UniformsLib = UniformsLib;
-	exports.UniformsUtils = UniformsUtils;
-	exports.UnsignedByteType = UnsignedByteType;
-	exports.UnsignedInt248Type = UnsignedInt248Type;
-	exports.UnsignedIntType = UnsignedIntType;
-	exports.UnsignedShort4444Type = UnsignedShort4444Type;
-	exports.UnsignedShort5551Type = UnsignedShort5551Type;
-	exports.UnsignedShort565Type = UnsignedShort565Type;
-	exports.UnsignedShortType = UnsignedShortType;
-	exports.VSMShadowMap = VSMShadowMap;
-	exports.Vector2 = Vector2;
-	exports.Vector3 = Vector3;
-	exports.Vector4 = Vector4;
-	exports.VectorKeyframeTrack = VectorKeyframeTrack;
-	exports.Vertex = Vertex;
-	exports.VertexColors = VertexColors;
-	exports.VideoTexture = VideoTexture;
-	exports.WebGLCubeRenderTarget = WebGLCubeRenderTarget;
-	exports.WebGLMultisampleRenderTarget = WebGLMultisampleRenderTarget;
-	exports.WebGLRenderTarget = WebGLRenderTarget;
-	exports.WebGLRenderTargetCube = WebGLRenderTargetCube;
-	exports.WebGLRenderer = WebGLRenderer;
-	exports.WebGLUtils = WebGLUtils;
-	exports.WireframeGeometry = WireframeGeometry;
-	exports.WireframeHelper = WireframeHelper;
-	exports.WrapAroundEnding = WrapAroundEnding;
-	exports.XHRLoader = XHRLoader;
-	exports.ZeroCurvatureEnding = ZeroCurvatureEnding;
-	exports.ZeroFactor = ZeroFactor;
-	exports.ZeroSlopeEnding = ZeroSlopeEnding;
-	exports.ZeroStencilOp = ZeroStencilOp;
-	exports.sRGBEncoding = sRGBEncoding;
+	PlaneHelper.prototype = Object.create( Line.prototype );
+	PlaneHelper.prototype.constructor = PlaneHelper;
 
-	Object.defineProperty(exports, '__esModule', { value: true });
+	PlaneHelper.prototype.updateMatrixWorld = function ( force ) {
 
-})));
+		var scale = - this.plane.constant;
 
-},{}],35:[function(require,module,exports){
-(function (setImmediate,clearImmediate){
-var nextTick = require('process/browser.js').nextTick;
-var apply = Function.prototype.apply;
-var slice = Array.prototype.slice;
-var immediateIds = {};
-var nextImmediateId = 0;
+		if ( Math.abs( scale ) < 1e-8 ) { scale = 1e-8; } // sign does not matter
 
-// DOM APIs, for completeness
+		this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
 
-exports.setTimeout = function() {
-  return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);
-};
-exports.setInterval = function() {
-  return new Timeout(apply.call(setInterval, window, arguments), clearInterval);
-};
-exports.clearTimeout =
-exports.clearInterval = function(timeout) { timeout.close(); };
+		this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here
 
-function Timeout(id, clearFn) {
-  this._id = id;
-  this._clearFn = clearFn;
-}
-Timeout.prototype.unref = Timeout.prototype.ref = function() {};
-Timeout.prototype.close = function() {
-  this._clearFn.call(window, this._id);
-};
+		this.lookAt( this.plane.normal );
 
-// Does not start the time, just sets up the members needed.
-exports.enroll = function(item, msecs) {
-  clearTimeout(item._idleTimeoutId);
-  item._idleTimeout = msecs;
-};
+		Object3D.prototype.updateMatrixWorld.call( this, force );
 
-exports.unenroll = function(item) {
-  clearTimeout(item._idleTimeoutId);
-  item._idleTimeout = -1;
-};
+	};
 
-exports._unrefActive = exports.active = function(item) {
-  clearTimeout(item._idleTimeoutId);
+	/**
+	 * @author WestLangley / http://github.com/WestLangley
+	 * @author zz85 / http://github.com/zz85
+	 * @author bhouston / http://clara.io
+	 *
+	 * Creates an arrow for visualizing directions
+	 *
+	 * Parameters:
+	 *  dir - Vector3
+	 *  origin - Vector3
+	 *  length - Number
+	 *  color - color in hex value
+	 *  headLength - Number
+	 *  headWidth - Number
+	 */
 
-  var msecs = item._idleTimeout;
-  if (msecs >= 0) {
-    item._idleTimeoutId = setTimeout(function onTimeout() {
-      if (item._onTimeout)
-        item._onTimeout();
-    }, msecs);
-  }
-};
+	var _axis = new Vector3();
+	var _lineGeometry, _coneGeometry;
 
-// That's not how node.js implements it but the exposed api is the same.
-exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
-  var id = nextImmediateId++;
-  var args = arguments.length < 2 ? false : slice.call(arguments, 1);
+	function ArrowHelper( dir, origin, length, color, headLength, headWidth ) {
 
-  immediateIds[id] = true;
+		// dir is assumed to be normalized
 
-  nextTick(function onNextTick() {
-    if (immediateIds[id]) {
-      // fn.call() is faster so we optimize for the common use-case
-      // @see http://jsperf.com/call-apply-segu
-      if (args) {
-        fn.apply(null, args);
-      } else {
-        fn.call(null);
-      }
-      // Prevent ids from leaking
-      exports.clearImmediate(id);
-    }
-  });
+		Object3D.call( this );
 
-  return id;
-};
+		this.type = 'ArrowHelper';
 
-exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) {
-  delete immediateIds[id];
-};
-}).call(this,require("timers").setImmediate,require("timers").clearImmediate)
-},{"process/browser.js":21,"timers":35}],36:[function(require,module,exports){
-(function (global){
+		if ( dir === undefined ) { dir = new Vector3( 0, 0, 1 ); }
+		if ( origin === undefined ) { origin = new Vector3( 0, 0, 0 ); }
+		if ( length === undefined ) { length = 1; }
+		if ( color === undefined ) { color = 0xffff00; }
+		if ( headLength === undefined ) { headLength = 0.2 * length; }
+		if ( headWidth === undefined ) { headWidth = 0.2 * headLength; }
 
-/**
- * Module exports.
- */
+		if ( _lineGeometry === undefined ) {
 
-module.exports = deprecate;
+			_lineGeometry = new BufferGeometry();
+			_lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
 
-/**
- * Mark that a method should not be used.
- * Returns a modified function which warns once by default.
- *
- * If `localStorage.noDeprecation = true` is set, then it is a no-op.
- *
- * If `localStorage.throwDeprecation = true` is set, then deprecated functions
- * will throw an Error when invoked.
- *
- * If `localStorage.traceDeprecation = true` is set, then deprecated functions
- * will invoke `console.trace()` instead of `console.error()`.
- *
- * @param {Function} fn - the function to deprecate
- * @param {String} msg - the string to print to the console when `fn` is invoked
- * @returns {Function} a new "deprecated" version of `fn`
- * @api public
- */
+			_coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
+			_coneGeometry.translate( 0, - 0.5, 0 );
 
-function deprecate (fn, msg) {
-  if (config('noDeprecation')) {
-    return fn;
-  }
+		}
 
-  var warned = false;
-  function deprecated() {
-    if (!warned) {
-      if (config('throwDeprecation')) {
-        throw new Error(msg);
-      } else if (config('traceDeprecation')) {
-        console.trace(msg);
-      } else {
-        console.warn(msg);
-      }
-      warned = true;
-    }
-    return fn.apply(this, arguments);
-  }
+		this.position.copy( origin );
 
-  return deprecated;
-}
+		this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
+		this.line.matrixAutoUpdate = false;
+		this.add( this.line );
 
-/**
- * Checks `localStorage` for boolean values for the given `name`.
- *
- * @param {String} name
- * @returns {Boolean}
- * @api private
- */
+		this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
+		this.cone.matrixAutoUpdate = false;
+		this.add( this.cone );
 
-function config (name) {
-  // accessing global.localStorage can trigger a DOMException in sandboxed iframes
-  try {
-    if (!global.localStorage) return false;
-  } catch (_) {
-    return false;
-  }
-  var val = global.localStorage[name];
-  if (null == val) return false;
-  return String(val).toLowerCase() === 'true';
-}
+		this.setDirection( dir );
+		this.setLength( length, headLength, headWidth );
 
-}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
-},{}],37:[function(require,module,exports){
-arguments[4][3][0].apply(exports,arguments)
-},{"dup":3}],38:[function(require,module,exports){
-arguments[4][4][0].apply(exports,arguments)
-},{"./support/isBuffer":37,"_process":21,"dup":4,"inherits":12}],39:[function(require,module,exports){
-var v1 = require('./v1');
-var v4 = require('./v4');
+	}
 
-var uuid = v4;
-uuid.v1 = v1;
-uuid.v4 = v4;
+	ArrowHelper.prototype = Object.create( Object3D.prototype );
+	ArrowHelper.prototype.constructor = ArrowHelper;
 
-module.exports = uuid;
+	ArrowHelper.prototype.setDirection = function ( dir ) {
 
-},{"./v1":42,"./v4":43}],40:[function(require,module,exports){
-/**
- * Convert array of 16 byte values to UUID string format of the form:
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
- */
-var byteToHex = [];
-for (var i = 0; i < 256; ++i) {
-  byteToHex[i] = (i + 0x100).toString(16).substr(1);
-}
+		// dir is assumed to be normalized
 
-function bytesToUuid(buf, offset) {
-  var i = offset || 0;
-  var bth = byteToHex;
-  // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
-  return ([
-    bth[buf[i++]], bth[buf[i++]],
-    bth[buf[i++]], bth[buf[i++]], '-',
-    bth[buf[i++]], bth[buf[i++]], '-',
-    bth[buf[i++]], bth[buf[i++]], '-',
-    bth[buf[i++]], bth[buf[i++]], '-',
-    bth[buf[i++]], bth[buf[i++]],
-    bth[buf[i++]], bth[buf[i++]],
-    bth[buf[i++]], bth[buf[i++]]
-  ]).join('');
-}
+		if ( dir.y > 0.99999 ) {
 
-module.exports = bytesToUuid;
+			this.quaternion.set( 0, 0, 0, 1 );
 
-},{}],41:[function(require,module,exports){
-// Unique ID creation requires a high quality random # generator.  In the
-// browser this is a little complicated due to unknown quality of Math.random()
-// and inconsistent support for the `crypto` API.  We do the best we can via
-// feature-detection
+		} else if ( dir.y < - 0.99999 ) {
 
-// getRandomValues needs to be invoked in a context where "this" is a Crypto
-// implementation. Also, find the complete implementation of crypto on IE11.
-var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) ||
-                      (typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto));
+			this.quaternion.set( 1, 0, 0, 0 );
 
-if (getRandomValues) {
-  // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
-  var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
+		} else {
 
-  module.exports = function whatwgRNG() {
-    getRandomValues(rnds8);
-    return rnds8;
-  };
-} else {
-  // Math.random()-based (RNG)
-  //
-  // If all else fails, use Math.random().  It's fast, but is of unspecified
-  // quality.
-  var rnds = new Array(16);
+			_axis.set( dir.z, 0, - dir.x ).normalize();
 
-  module.exports = function mathRNG() {
-    for (var i = 0, r; i < 16; i++) {
-      if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
-      rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
-    }
+			var radians = Math.acos( dir.y );
 
-    return rnds;
-  };
-}
+			this.quaternion.setFromAxisAngle( _axis, radians );
 
-},{}],42:[function(require,module,exports){
-var rng = require('./lib/rng');
-var bytesToUuid = require('./lib/bytesToUuid');
+		}
 
-// **`v1()` - Generate time-based UUID**
-//
-// Inspired by https://github.com/LiosK/UUID.js
-// and http://docs.python.org/library/uuid.html
+	};
 
-var _nodeId;
-var _clockseq;
+	ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
 
-// Previous uuid creation time
-var _lastMSecs = 0;
-var _lastNSecs = 0;
+		if ( headLength === undefined ) { headLength = 0.2 * length; }
+		if ( headWidth === undefined ) { headWidth = 0.2 * headLength; }
 
-// See https://github.com/uuidjs/uuid for API details
-function v1(options, buf, offset) {
-  var i = buf && offset || 0;
-  var b = buf || [];
+		this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
+		this.line.updateMatrix();
 
-  options = options || {};
-  var node = options.node || _nodeId;
-  var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
+		this.cone.scale.set( headWidth, headLength, headWidth );
+		this.cone.position.y = length;
+		this.cone.updateMatrix();
 
-  // node and clockseq need to be initialized to random values if they're not
-  // specified.  We do this lazily to minimize issues related to insufficient
-  // system entropy.  See #189
-  if (node == null || clockseq == null) {
-    var seedBytes = rng();
-    if (node == null) {
-      // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
-      node = _nodeId = [
-        seedBytes[0] | 0x01,
-        seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
-      ];
-    }
-    if (clockseq == null) {
-      // Per 4.2.2, randomize (14 bit) clockseq
-      clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
-    }
-  }
+	};
 
-  // UUID timestamps are 100 nano-second units since the Gregorian epoch,
-  // (1582-10-15 00:00).  JSNumbers aren't precise enough for this, so
-  // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
-  // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
-  var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
+	ArrowHelper.prototype.setColor = function ( color ) {
 
-  // Per 4.2.1.2, use count of uuid's generated during the current clock
-  // cycle to simulate higher resolution clock
-  var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
+		this.line.material.color.set( color );
+		this.cone.material.color.set( color );
 
-  // Time since last uuid creation (in msecs)
-  var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
+	};
 
-  // Per 4.2.1.2, Bump clockseq on clock regression
-  if (dt < 0 && options.clockseq === undefined) {
-    clockseq = clockseq + 1 & 0x3fff;
-  }
+	ArrowHelper.prototype.copy = function ( source ) {
 
-  // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
-  // time interval
-  if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
-    nsecs = 0;
-  }
+		Object3D.prototype.copy.call( this, source, false );
 
-  // Per 4.2.1.2 Throw error if too many uuids are requested
-  if (nsecs >= 10000) {
-    throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
-  }
+		this.line.copy( source.line );
+		this.cone.copy( source.cone );
 
-  _lastMSecs = msecs;
-  _lastNSecs = nsecs;
-  _clockseq = clockseq;
+		return this;
 
-  // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
-  msecs += 12219292800000;
+	};
 
-  // `time_low`
-  var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
-  b[i++] = tl >>> 24 & 0xff;
-  b[i++] = tl >>> 16 & 0xff;
-  b[i++] = tl >>> 8 & 0xff;
-  b[i++] = tl & 0xff;
+	ArrowHelper.prototype.clone = function () {
 
-  // `time_mid`
-  var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
-  b[i++] = tmh >>> 8 & 0xff;
-  b[i++] = tmh & 0xff;
+		return new this.constructor().copy( this );
 
-  // `time_high_and_version`
-  b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
-  b[i++] = tmh >>> 16 & 0xff;
+	};
 
-  // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
-  b[i++] = clockseq >>> 8 | 0x80;
+	/**
+	 * @author sroucheray / http://sroucheray.org/
+	 * @author mrdoob / http://mrdoob.com/
+	 */
 
-  // `clock_seq_low`
-  b[i++] = clockseq & 0xff;
+	function AxesHelper( size ) {
 
-  // `node`
-  for (var n = 0; n < 6; ++n) {
-    b[i + n] = node[n];
-  }
+		size = size || 1;
 
-  return buf ? buf : bytesToUuid(b);
-}
+		var vertices = [
+			0, 0, 0,	size, 0, 0,
+			0, 0, 0,	0, size, 0,
+			0, 0, 0,	0, 0, size
+		];
 
-module.exports = v1;
+		var colors = [
+			1, 0, 0,	1, 0.6, 0,
+			0, 1, 0,	0.6, 1, 0,
+			0, 0, 1,	0, 0.6, 1
+		];
 
-},{"./lib/bytesToUuid":40,"./lib/rng":41}],43:[function(require,module,exports){
-var rng = require('./lib/rng');
-var bytesToUuid = require('./lib/bytesToUuid');
+		var geometry = new BufferGeometry();
+		geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+		geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
 
-function v4(options, buf, offset) {
-  var i = buf && offset || 0;
+		var material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
 
-  if (typeof(options) == 'string') {
-    buf = options === 'binary' ? new Array(16) : null;
-    options = null;
-  }
-  options = options || {};
+		LineSegments.call( this, geometry, material );
 
-  var rnds = options.random || (options.rng || rng)();
+		this.type = 'AxesHelper';
 
-  // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
-  rnds[6] = (rnds[6] & 0x0f) | 0x40;
-  rnds[8] = (rnds[8] & 0x3f) | 0x80;
+	}
 
-  // Copy bytes to buffer, if provided
-  if (buf) {
-    for (var ii = 0; ii < 16; ++ii) {
-      buf[i + ii] = rnds[ii];
-    }
-  }
+	AxesHelper.prototype = Object.create( LineSegments.prototype );
+	AxesHelper.prototype.constructor = AxesHelper;
 
-  return buf || bytesToUuid(rnds);
-}
+	/**
+	 * @author Emmett Lalish / elalish
+	 *
+	 * This class generates a Prefiltered, Mipmapped Radiance Environment Map
+	 * (PMREM) from a cubeMap environment texture. This allows different levels of
+	 * blur to be quickly accessed based on material roughness. It is packed into a
+	 * special CubeUV format that allows us to perform custom interpolation so that
+	 * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap
+	 * chain, it only goes down to the LOD_MIN level (above), and then creates extra
+	 * even more filtered 'mips' at the same LOD_MIN resolution, associated with
+	 * higher roughness levels. In this way we maintain resolution to smoothly
+	 * interpolate diffuse lighting while limiting sampling computation.
+	 */
 
-module.exports = v4;
+	var LOD_MIN = 4;
+	var LOD_MAX = 8;
+	var SIZE_MAX = Math.pow( 2, LOD_MAX );
 
-},{"./lib/bytesToUuid":40,"./lib/rng":41}],44:[function(require,module,exports){
-(function (Buffer){
-const Peer = require('../../server/src/peer')
-const VideoConverter = require('./lib/dist/video-converter');
-const msgpack = require('msgpack5')();
-const rematrix = require('rematrix');
-const THREE = require('three');
-
-var debug = require("./lib/dist/util/debug");
-debug.setLogger(null,console.error);
-
-let current_data = {};
-let peer;
-
-/**
- * Validates that the user is logged in by sending the token 
- */
-checkIfLoggedIn = async () => {
-    //     const token = window.localStorage.getItem('token')
-    //     console.log(token)
-    //     if(!token){
-    //         console.log("You need to login")
-    //         renderLogin()
-    //     }else{
-
-    //         //Check if the token is valid
-    //         const response = await fetch('http://localhost:8080/auth/validation', {
-    //             method: 'POST',
-    //             headers: {'Authorization': token}
-    //         })
-    //         console.log('RESPONSE', response)
-            
-    //         //Token is valid, show available streams
-    //         if(response.status === 200){
-    //             console.log("SUCCESS")
-                 renderThumbnails()
-
-    //         }
-    //     }
-}
-
-/**
- * Returns a list of available streams
- */
-getAvailableStreams = async () => {
-    try{
-        const streamsInJson = await fetch(`./streams`);
-        const streams = await streamsInJson.json();
-        console.log('AVAILABLE', streams)
-        return streams;
-    }catch(err){
-        console.log(err)
-    }
-}
-
-
-createVideoPlayer = () => {
-	const containerDiv = document.getElementById('container');
-	containerDiv.innerHTML = '';
-    /*containerDiv.innerHTML = `<h1>Stream from source ${current_data.uri}</h1><br>
-        <button onclick="renderThumbnails(); closeStream()">Go back</button>
-        <button onclick="connectToStream('${current_data.uri}')">Start Stream</button><br>
-        <button onclick="webSocketTest()">WebSocket Test</button><br>
-        <video id="ftlab-stream-video" width="640" height="360"></video>`;
-    containerDiv.innerHTML += '<br>'
-    containerDiv.innerHTML += ''*/
-    createPeer();
-	//connectToStream();
-	window.ftlstream = new FTLStream(peer, current_data.uri, containerDiv);
-}
-
-/**
- * Creates thumbnail (image) for all available streams and adds them to div class='container'
- */
-renderThumbnails = async () => {
-    const thumbnails = await getAvailableStreams();
-    const containerDiv = document.getElementById('container')
-    containerDiv.innerHTML = '';
-    containerDiv.innerHTML = `<button onClick="configs()">change configs</button>`
-    containerDiv.innerHTML += `<div class="ftlab-stream-thumbnails"></div>`
-    if(thumbnails.length === 0){
-        containerDiv.innerHTML = `<h3>No streams running currently</h3>`
-    }else{
-        for(var i=0; i<thumbnails.length; i++){
-            const encodedURI = encodeURIComponent(thumbnails[i])
-            current_data.uri = thumbnails[i]
-            try{
-                const someData = await fetch(`./stream/rgb?uri=${encodedURI}`)
-                if(!someData.ok){
-                    throw new Error('Image not found')
-                }
-                const myBlob = await someData.blob();
-                const objectURL = URL.createObjectURL(myBlob);
-                // containerDiv.innerHTML += createCard()
-                containerDiv.innerHTML += createCard(objectURL, i+4)
-            }catch(err){
-                console.log("Couldn't create thumbnail");
-                console.log(err) 
-            }
-        }
-    }
-}
-
-
-/** 
- * Method to create a single thumbnail
- */
-createCard = (url, viewers) => {
-    return `<div class='ftlab-card-component' >
-                <img src='${url}' class="thumbnail-img" alt="Hups" width="250px"></img>
-                <p>Viewers: ${viewers}</p>
-                <button onclick="createVideoPlayer()">button</button>
-            </div>`
-}
-
-
-createPeer = () => {
-	// FOR PRODUCTION
-	console.log("HOST", location.host);
-    const ws = new WebSocket("ws://" + location.host + location.pathname);
-	//const ws = new WebSocket("ws://localhost:8080")
-    ws.binaryType = "arraybuffer";
-    peer = new Peer(ws)
-}
-
-webSocketTest = () => {
-    peer.send("update_cfg", "ftl://utu.fi#reconstruction_default/0/renderer/cool_effect", "true")    
-}
-
-function FTLFrameset(id) {
-	this.id = id;
-	this.sources = {};
-}
-
-function FTLStream(peer, uri, element) {
-	this.uri = uri;
-	this.peer = peer;
-
-	this.current = "";
-	this.current_fs = 0;
-	this.current_source = 0;
-	this.current_channel = 0;
-
-	this.framesets = {};
-
-	this.handlers = {};
-
-	//this.elements_ = {};
-	//this.converters_ = {};
-
-	//const element = document.getElementById('ftlab-stream-video');
-	this.outer = element;
-	this.outer.classList.add("ftl");
-	this.outer.classList.add("container");
-	this.element = document.createElement("VIDEO");
-	this.element.setAttribute("width", 640);
-	this.element.setAttribute("height", 360);
-	this.element.setAttribute("controls", true);
-	this.element.style.display = "none";
-	this.element.classList.add("ftl");
-	this.element.id = "ftl-video-element";
-	this.outer.appendChild(this.element);
-
-	//this.player = videojs('ftl-video-element');
-	//this.player.vr({projection: '360'});
-
-	if (false) {
-		this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
-	} else {
-		this.camera = new THREE.OrthographicCamera(window.innerWidth/-2, window.innerWidth/2, window.innerHeight/2, window.innerHeight/-2, 1, 4);
-	}
+	// The standard deviations (radians) associated with the extra mips. These are
+	// chosen to approximate a Trowbridge-Reitz distribution function times the
+	// geometric shadowing function. These sigma values squared must match the
+	// variance #defines in cube_uv_reflection_fragment.glsl.js.
+	var EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ];
+
+	var TOTAL_LODS = LOD_MAX - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length;
+
+	// The maximum length of the blur for loop. Smaller sigmas will use fewer
+	// samples and exit early, but not recompile the shader.
+	var MAX_SAMPLES = 20;
+
+	var ENCODINGS = {};
+	ENCODINGS[ LinearEncoding ] = 0;
+	ENCODINGS[ sRGBEncoding ] = 1;
+	ENCODINGS[ RGBEEncoding ] = 2;
+	ENCODINGS[ RGBM7Encoding ] = 3;
+	ENCODINGS[ RGBM16Encoding ] = 4;
+	ENCODINGS[ RGBDEncoding ] = 5;
+	ENCODINGS[ GammaEncoding ] = 6;
+
+	var _flatCamera = new OrthographicCamera();
+	var ref = _createPlanes();
+	var _lodPlanes = ref._lodPlanes;
+	var _sizeLods = ref._sizeLods;
+	var _sigmas = ref._sigmas;
+	var _oldTarget = null;
+
+	// Golden Ratio
+	var PHI = ( 1 + Math.sqrt( 5 ) ) / 2;
+	var INV_PHI = 1 / PHI;
+
+	// Vertices of a dodecahedron (except the opposites, which represent the
+	// same axis), used as axis directions evenly spread on a sphere.
+	var _axisDirections = [
+		new Vector3( 1, 1, 1 ),
+		new Vector3( - 1, 1, 1 ),
+		new Vector3( 1, 1, - 1 ),
+		new Vector3( - 1, 1, - 1 ),
+		new Vector3( 0, PHI, INV_PHI ),
+		new Vector3( 0, PHI, - INV_PHI ),
+		new Vector3( INV_PHI, 0, PHI ),
+		new Vector3( - INV_PHI, 0, PHI ),
+		new Vector3( PHI, INV_PHI, 0 ),
+		new Vector3( - PHI, INV_PHI, 0 ) ];
+
+	function PMREMGenerator( renderer ) {
+
+		this._renderer = renderer;
+		this._pingPongRenderTarget = null;
+
+		this._blurMaterial = _getBlurShader( MAX_SAMPLES );
+		this._equirectShader = null;
+		this._cubemapShader = null;
+
+		this._compileMaterial( this._blurMaterial );
+
+	}
+
+	PMREMGenerator.prototype = {
+
+		constructor: PMREMGenerator,
+
+		/**
+		 * Generates a PMREM from a supplied Scene, which can be faster than using an
+		 * image if networking bandwidth is low. Optional sigma specifies a blur radius
+		 * in radians to be applied to the scene before PMREM generation. Optional near
+		 * and far planes ensure the scene is rendered in its entirety (the cubeCamera
+		 * is placed at the origin).
+		 */
+		fromScene: function ( scene, sigma, near, far ) {
+			if ( sigma === void 0 ) sigma = 0;
+			if ( near === void 0 ) near = 0.1;
+			if ( far === void 0 ) far = 100;
+
+
+			_oldTarget = this._renderer.getRenderTarget();
+			var cubeUVRenderTarget = this._allocateTargets();
+
+			this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
+			if ( sigma > 0 ) {
+
+				this._blur( cubeUVRenderTarget, 0, 0, sigma );
+
+			}
+
+			this._applyPMREM( cubeUVRenderTarget );
+			this._cleanup( cubeUVRenderTarget );
+
+			return cubeUVRenderTarget;
+
+		},
+
+		/**
+		 * Generates a PMREM from an equirectangular texture, which can be either LDR
+		 * (RGBFormat) or HDR (RGBEFormat). The ideal input image size is 1k (1024 x 512),
+		 * as this matches best with the 256 x 256 cubemap output.
+		 */
+		fromEquirectangular: function ( equirectangular ) {
+
+			equirectangular.magFilter = NearestFilter;
+			equirectangular.minFilter = NearestFilter;
+			equirectangular.generateMipmaps = false;
+
+			return this.fromCubemap( equirectangular );
+
+		},
+
+		/**
+		 * Generates a PMREM from an cubemap texture, which can be either LDR
+		 * (RGBFormat) or HDR (RGBEFormat). The ideal input cube size is 256 x 256,
+		 * as this matches best with the 256 x 256 cubemap output.
+		 */
+		fromCubemap: function ( cubemap ) {
+
+			_oldTarget = this._renderer.getRenderTarget();
+			var cubeUVRenderTarget = this._allocateTargets( cubemap );
+			this._textureToCubeUV( cubemap, cubeUVRenderTarget );
+			this._applyPMREM( cubeUVRenderTarget );
+			this._cleanup( cubeUVRenderTarget );
+
+			return cubeUVRenderTarget;
+
+		},
+
+		/**
+		 * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during
+		 * your texture's network fetch for increased concurrency.
+		 */
+		compileCubemapShader: function () {
+
+			if ( this._cubemapShader === null ) {
+
+				this._cubemapShader = _getCubemapShader();
+				this._compileMaterial( this._cubemapShader );
+
+			}
+
+		},
+
+		/**
+		 * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during
+		 * your texture's network fetch for increased concurrency.
+		 */
+		compileEquirectangularShader: function () {
+
+			if ( this._equirectShader === null ) {
+
+				this._equirectShader = _getEquirectShader();
+				this._compileMaterial( this._equirectShader );
+
+			}
+
+		},
+
+		/**
+		 * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class,
+		 * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on
+		 * one of them will cause any others to also become unusable.
+		 */
+		dispose: function () {
+
+			this._blurMaterial.dispose();
+
+			if ( this._cubemapShader !== null ) { this._cubemapShader.dispose(); }
+			if ( this._equirectShader !== null ) { this._equirectShader.dispose(); }
+
+			for ( var i = 0; i < _lodPlanes.length; i ++ ) {
+
+				_lodPlanes[ i ].dispose();
+
+			}
+
+		},
+
+		// private interface
+
+		_cleanup: function ( outputTarget ) {
+
+			this._pingPongRenderTarget.dispose();
+			this._renderer.setRenderTarget( _oldTarget );
+			outputTarget.scissorTest = false;
+			// reset viewport and scissor
+			outputTarget.setSize( outputTarget.width, outputTarget.height );
+
+		},
+
+		_allocateTargets: function ( equirectangular ) {
+
+			var params = {
+				magFilter: NearestFilter,
+				minFilter: NearestFilter,
+				generateMipmaps: false,
+				type: UnsignedByteType,
+				format: RGBEFormat,
+				encoding: _isLDR( equirectangular ) ? equirectangular.encoding : RGBEEncoding,
+				depthBuffer: false,
+				stencilBuffer: false
+			};
+
+			var cubeUVRenderTarget = _createRenderTarget( params );
+			cubeUVRenderTarget.depthBuffer = equirectangular ? false : true;
+			this._pingPongRenderTarget = _createRenderTarget( params );
+			return cubeUVRenderTarget;
+
+		},
+
+		_compileMaterial: function ( material ) {
+
+			var tmpScene = new Scene();
+			tmpScene.add( new Mesh( _lodPlanes[ 0 ], material ) );
+			this._renderer.compile( tmpScene, _flatCamera );
+
+		},
+
+		_sceneToCubeUV: function ( scene, near, far, cubeUVRenderTarget ) {
+
+			var fov = 90;
+			var aspect = 1;
+			var cubeCamera = new PerspectiveCamera( fov, aspect, near, far );
+			var upSign = [ 1, 1, 1, 1, - 1, 1 ];
+			var forwardSign = [ 1, 1, - 1, - 1, - 1, 1 ];
+			var renderer = this._renderer;
+
+			var outputEncoding = renderer.outputEncoding;
+			var toneMapping = renderer.toneMapping;
+			var toneMappingExposure = renderer.toneMappingExposure;
+			var clearColor = renderer.getClearColor();
+			var clearAlpha = renderer.getClearAlpha();
+
+			renderer.toneMapping = LinearToneMapping;
+			renderer.toneMappingExposure = 1.0;
+			renderer.outputEncoding = LinearEncoding;
+			scene.scale.z *= - 1;
+
+			var background = scene.background;
+			if ( background && background.isColor ) {
+
+				background.convertSRGBToLinear();
+				// Convert linear to RGBE
+				var maxComponent = Math.max( background.r, background.g, background.b );
+				var fExp = Math.min( Math.max( Math.ceil( Math.log2( maxComponent ) ), - 128.0 ), 127.0 );
+				background = background.multiplyScalar( Math.pow( 2.0, - fExp ) );
+				var alpha = ( fExp + 128.0 ) / 255.0;
+				renderer.setClearColor( background, alpha );
+				scene.background = null;
+
+			}
+
+			for ( var i = 0; i < 6; i ++ ) {
+
+				var col = i % 3;
+				if ( col == 0 ) {
+
+					cubeCamera.up.set( 0, upSign[ i ], 0 );
+					cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
+
+				} else if ( col == 1 ) {
+
+					cubeCamera.up.set( 0, 0, upSign[ i ] );
+					cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
+
+				} else {
+
+					cubeCamera.up.set( 0, upSign[ i ], 0 );
+					cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
+
+				}
+
+				_setViewport( cubeUVRenderTarget,
+					col * SIZE_MAX, i > 2 ? SIZE_MAX : 0, SIZE_MAX, SIZE_MAX );
+				renderer.setRenderTarget( cubeUVRenderTarget );
+				renderer.render( scene, cubeCamera );
+
+			}
+
+			renderer.toneMapping = toneMapping;
+			renderer.toneMappingExposure = toneMappingExposure;
+			renderer.outputEncoding = outputEncoding;
+			renderer.setClearColor( clearColor, clearAlpha );
+			scene.scale.z *= - 1;
+
+		},
+
+		_textureToCubeUV: function ( texture, cubeUVRenderTarget ) {
+
+			var scene = new Scene();
+			var renderer = this._renderer;
+
+			if ( texture.isCubeTexture ) {
+
+				if ( this._cubemapShader == null ) {
+
+					this._cubemapShader = _getCubemapShader();
+
+				}
+
+			} else {
+
+				if ( this._equirectShader == null ) {
+
+					this._equirectShader = _getEquirectShader();
+
+				}
+
+			}
+
+			var material = texture.isCubeTexture ? this._cubemapShader : this._equirectShader;
+			scene.add( new Mesh( _lodPlanes[ 0 ], material ) );
+
+			var uniforms = material.uniforms;
+
+			uniforms[ 'envMap' ].value = texture;
+
+			if ( ! texture.isCubeTexture ) {
+
+				uniforms[ 'texelSize' ].value.set( 1.0 / texture.image.width, 1.0 / texture.image.height );
+
+			}
+
+			uniforms[ 'inputEncoding' ].value = ENCODINGS[ texture.encoding ];
+			uniforms[ 'outputEncoding' ].value = ENCODINGS[ cubeUVRenderTarget.texture.encoding ];
+
+			_setViewport( cubeUVRenderTarget, 0, 0, 3 * SIZE_MAX, 2 * SIZE_MAX );
+
+			renderer.setRenderTarget( cubeUVRenderTarget );
+			renderer.render( scene, _flatCamera );
+
+		},
+
+		_applyPMREM: function ( cubeUVRenderTarget ) {
+
+			var renderer = this._renderer;
+			var autoClear = renderer.autoClear;
+			renderer.autoClear = false;
+
+			for ( var i = 1; i < TOTAL_LODS; i ++ ) {
+
+				var sigma = Math.sqrt( _sigmas[ i ] * _sigmas[ i ] - _sigmas[ i - 1 ] * _sigmas[ i - 1 ] );
+
+				var poleAxis = _axisDirections[ ( i - 1 ) % _axisDirections.length ];
+
+				this._blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis );
+
+			}
+
+			renderer.autoClear = autoClear;
+
+		},
+
+		/**
+		 * This is a two-pass Gaussian blur for a cubemap. Normally this is done
+		 * vertically and horizontally, but this breaks down on a cube. Here we apply
+		 * the blur latitudinally (around the poles), and then longitudinally (towards
+		 * the poles) to approximate the orthogonally-separable blur. It is least
+		 * accurate at the poles, but still does a decent job.
+		 */
+		_blur: function ( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) {
+
+			var pingPongRenderTarget = this._pingPongRenderTarget;
+
+			this._halfBlur(
+				cubeUVRenderTarget,
+				pingPongRenderTarget,
+				lodIn,
+				lodOut,
+				sigma,
+				'latitudinal',
+				poleAxis );
+
+			this._halfBlur(
+				pingPongRenderTarget,
+				cubeUVRenderTarget,
+				lodOut,
+				lodOut,
+				sigma,
+				'longitudinal',
+				poleAxis );
+
+		},
+
+		_halfBlur: function ( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) {
+
+			var renderer = this._renderer;
+			var blurMaterial = this._blurMaterial;
+
+			if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
+
+				console.error(
+					'blur direction must be either latitudinal or longitudinal!' );
+
+			}
+
+			// Number of standard deviations at which to cut off the discrete approximation.
+			var STANDARD_DEVIATIONS = 3;
+
+			var blurScene = new Scene();
+			blurScene.add( new Mesh( _lodPlanes[ lodOut ], blurMaterial ) );
+			var blurUniforms = blurMaterial.uniforms;
+
+			var pixels = _sizeLods[ lodIn ] - 1;
+			var radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 );
+			var sigmaPixels = sigmaRadians / radiansPerPixel;
+			var samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES;
+
+			if ( samples > MAX_SAMPLES ) {
+
+				console.warn( ("sigmaRadians, " + sigmaRadians + ", is too large and will clip, as it requested " + samples + " samples when the maximum is set to " + MAX_SAMPLES) );
+
+			}
+
+			var weights = [];
+			var sum = 0;
+
+			for ( var i = 0; i < MAX_SAMPLES; ++ i ) {
+
+				var x = i / sigmaPixels;
+				var weight = Math.exp( - x * x / 2 );
+				weights.push( weight );
+
+				if ( i == 0 ) {
+
+					sum += weight;
+
+				} else if ( i < samples ) {
+
+					sum += 2 * weight;
+
+				}
+
+			}
+
+			for ( var i = 0; i < weights.length; i ++ ) {
+
+				weights[ i ] = weights[ i ] / sum;
+
+			}
+
+			blurUniforms[ 'envMap' ].value = targetIn.texture;
+			blurUniforms[ 'samples' ].value = samples;
+			blurUniforms[ 'weights' ].value = weights;
+			blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal';
+
+			if ( poleAxis ) {
+
+				blurUniforms[ 'poleAxis' ].value = poleAxis;
+
+			}
+
+			blurUniforms[ 'dTheta' ].value = radiansPerPixel;
+			blurUniforms[ 'mipInt' ].value = LOD_MAX - lodIn;
+			blurUniforms[ 'inputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
+			blurUniforms[ 'outputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ];
+
+			var outputSize = _sizeLods[ lodOut ];
+			var x = 3 * Math.max( 0, SIZE_MAX - 2 * outputSize );
+			var y = ( lodOut === 0 ? 0 : 2 * SIZE_MAX ) + 2 * outputSize * ( lodOut > LOD_MAX - LOD_MIN ? lodOut - LOD_MAX + LOD_MIN : 0 );
+
+			_setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize );
+			renderer.setRenderTarget( targetOut );
+			renderer.render( blurScene, _flatCamera );
+
+		}
+
+	};
+
+	function _isLDR( texture ) {
+
+		if ( texture === undefined || texture.type !== UnsignedByteType ) { return false; }
+
+		return texture.encoding === LinearEncoding || texture.encoding === sRGBEncoding || texture.encoding === GammaEncoding;
+
+	}
+
+	function _createPlanes() {
+
+		var _lodPlanes = [];
+		var _sizeLods = [];
+		var _sigmas = [];
+
+		var lod = LOD_MAX;
+
+		for ( var i = 0; i < TOTAL_LODS; i ++ ) {
+
+			var sizeLod = Math.pow( 2, lod );
+			_sizeLods.push( sizeLod );
+			var sigma = 1.0 / sizeLod;
+
+			if ( i > LOD_MAX - LOD_MIN ) {
+
+				sigma = EXTRA_LOD_SIGMA[ i - LOD_MAX + LOD_MIN - 1 ];
+
+			} else if ( i == 0 ) {
+
+				sigma = 0;
+
+			}
+
+			_sigmas.push( sigma );
+
+			var texelSize = 1.0 / ( sizeLod - 1 );
+			var min = - texelSize / 2;
+			var max = 1 + texelSize / 2;
+			var uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
+
+			var cubeFaces = 6;
+			var vertices = 6;
+			var positionSize = 3;
+			var uvSize = 2;
+			var faceIndexSize = 1;
+
+			var position = new Float32Array( positionSize * vertices * cubeFaces );
+			var uv = new Float32Array( uvSize * vertices * cubeFaces );
+			var faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces );
+
+			for ( var face = 0; face < cubeFaces; face ++ ) {
+
+				var x = ( face % 3 ) * 2 / 3 - 1;
+				var y = face > 2 ? 0 : - 1;
+				var coordinates = [
+					x, y, 0,
+					x + 2 / 3, y, 0,
+					x + 2 / 3, y + 1, 0,
+					x, y, 0,
+					x + 2 / 3, y + 1, 0,
+					x, y + 1, 0
+				];
+				position.set( coordinates, positionSize * vertices * face );
+				uv.set( uv1, uvSize * vertices * face );
+				var fill = [ face, face, face, face, face, face ];
+				faceIndex.set( fill, faceIndexSize * vertices * face );
+
+			}
+
+			var planes = new BufferGeometry();
+			planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) );
+			planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
+			planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
+			_lodPlanes.push( planes );
+
+			if ( lod > LOD_MIN ) {
+
+				lod --;
+
+			}
+
+		}
+
+		return { _lodPlanes: _lodPlanes, _sizeLods: _sizeLods, _sigmas: _sigmas };
+
+	}
+
+	function _createRenderTarget( params ) {
+
+		var cubeUVRenderTarget = new WebGLRenderTarget( 3 * SIZE_MAX, 3 * SIZE_MAX, params );
+		cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
+		cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
+		cubeUVRenderTarget.scissorTest = true;
+		return cubeUVRenderTarget;
+
+	}
+
+	function _setViewport( target, x, y, width, height ) {
+
+		target.viewport.set( x, y, width, height );
+		target.scissor.set( x, y, width, height );
+
+	}
+
+	function _getBlurShader( maxSamples ) {
+
+		var weights = new Float32Array( maxSamples );
+		var poleAxis = new Vector3( 0, 1, 0 );
+		var shaderMaterial = new RawShaderMaterial( {
+
+			defines: { 'n': maxSamples },
+
+			uniforms: {
+				'envMap': { value: null },
+				'samples': { value: 1 },
+				'weights': { value: weights },
+				'latitudinal': { value: false },
+				'dTheta': { value: 0 },
+				'mipInt': { value: 0 },
+				'poleAxis': { value: poleAxis },
+				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
+				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
+			},
+
+			vertexShader: _getCommonVertexShader(),
+
+			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform sampler2D envMap;\nuniform int samples;\nuniform float weights[n];\nuniform bool latitudinal;\nuniform float dTheta;\nuniform float mipInt;\nuniform vec3 poleAxis;\n\n" + (_getEncodings()) + "\n\n#define ENVMAP_TYPE_CUBE_UV\n#include <cube_uv_reflection_fragment>\n\nvec3 getSample(float theta, vec3 axis) {\n\tfloat cosTheta = cos(theta);\n\t// Rodrigues' axis-angle rotation\n\tvec3 sampleDirection = vOutputDirection * cosTheta\n\t\t+ cross(axis, vOutputDirection) * sin(theta)\n\t\t+ axis * dot(axis, vOutputDirection) * (1.0 - cosTheta);\n\treturn bilinearCubeUV(envMap, sampleDirection, mipInt);\n}\n\nvoid main() {\n\tvec3 axis = latitudinal ? poleAxis : cross(poleAxis, vOutputDirection);\n\tif (all(equal(axis, vec3(0.0))))\n\t\taxis = vec3(vOutputDirection.z, 0.0, - vOutputDirection.x);\n\taxis = normalize(axis);\n\tgl_FragColor = vec4(0.0);\n\tgl_FragColor.rgb += weights[0] * getSample(0.0, axis);\n\tfor (int i = 1; i < n; i++) {\n\t\tif (i >= samples)\n\t\t\tbreak;\n\t\tfloat theta = dTheta * float(i);\n\t\tgl_FragColor.rgb += weights[i] * getSample(-1.0 * theta, axis);\n\t\tgl_FragColor.rgb += weights[i] * getSample(theta, axis);\n\t}\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+
+			blending: NoBlending,
+			depthTest: false,
+			depthWrite: false
+
+		} );
+
+		shaderMaterial.type = 'SphericalGaussianBlur';
+
+		return shaderMaterial;
+
+	}
+
+	function _getEquirectShader() {
+
+		var texelSize = new Vector2( 1, 1 );
+		var shaderMaterial = new RawShaderMaterial( {
+
+			uniforms: {
+				'envMap': { value: null },
+				'texelSize': { value: texelSize },
+				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
+				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
+			},
+
+			vertexShader: _getCommonVertexShader(),
+
+			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform sampler2D envMap;\nuniform vec2 texelSize;\n\n" + (_getEncodings()) + "\n\n#define RECIPROCAL_PI 0.31830988618\n#define RECIPROCAL_PI2 0.15915494\n\nvoid main() {\n\tgl_FragColor = vec4(0.0);\n\tvec3 outputDirection = normalize(vOutputDirection);\n\tvec2 uv;\n\tuv.y = asin(clamp(outputDirection.y, -1.0, 1.0)) * RECIPROCAL_PI + 0.5;\n\tuv.x = atan(outputDirection.z, outputDirection.x) * RECIPROCAL_PI2 + 0.5;\n\tvec2 f = fract(uv / texelSize - 0.5);\n\tuv -= f * texelSize;\n\tvec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.x += texelSize.x;\n\tvec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.y += texelSize.y;\n\tvec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tuv.x -= texelSize.x;\n\tvec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;\n\tvec3 tm = mix(tl, tr, f.x);\n\tvec3 bm = mix(bl, br, f.x);\n\tgl_FragColor.rgb = mix(tm, bm, f.y);\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+
+			blending: NoBlending,
+			depthTest: false,
+			depthWrite: false
+
+		} );
+
+		shaderMaterial.type = 'EquirectangularToCubeUV';
+
+		return shaderMaterial;
+
+	}
+
+	function _getCubemapShader() {
+
+		var shaderMaterial = new RawShaderMaterial( {
+
+			uniforms: {
+				'envMap': { value: null },
+				'inputEncoding': { value: ENCODINGS[ LinearEncoding ] },
+				'outputEncoding': { value: ENCODINGS[ LinearEncoding ] }
+			},
+
+			vertexShader: _getCommonVertexShader(),
+
+			fragmentShader: ("\nprecision mediump float;\nprecision mediump int;\nvarying vec3 vOutputDirection;\nuniform samplerCube envMap;\n\n" + (_getEncodings()) + "\n\nvoid main() {\n\tgl_FragColor = vec4(0.0);\n\tgl_FragColor.rgb = envMapTexelToLinear(textureCube(envMap, vec3( - vOutputDirection.x, vOutputDirection.yz ))).rgb;\n\tgl_FragColor = linearToOutputTexel(gl_FragColor);\n}\n\t\t"),
+
+			blending: NoBlending,
+			depthTest: false,
+			depthWrite: false
+
+		} );
+
+		shaderMaterial.type = 'CubemapToCubeUV';
+
+		return shaderMaterial;
+
+	}
+
+	function _getCommonVertexShader() {
+
+		return "\nprecision mediump float;\nprecision mediump int;\nattribute vec3 position;\nattribute vec2 uv;\nattribute float faceIndex;\nvarying vec3 vOutputDirection;\nvec3 getDirection(vec2 uv, float face) {\n\tuv = 2.0 * uv - 1.0;\n\tvec3 direction = vec3(uv, 1.0);\n\tif (face == 0.0) {\n\t\tdirection = direction.zyx;\n\t\tdirection.z *= -1.0;\n\t} else if (face == 1.0) {\n\t\tdirection = direction.xzy;\n\t\tdirection.z *= -1.0;\n\t} else if (face == 3.0) {\n\t\tdirection = direction.zyx;\n\t\tdirection.x *= -1.0;\n\t} else if (face == 4.0) {\n\t\tdirection = direction.xzy;\n\t\tdirection.y *= -1.0;\n\t} else if (face == 5.0) {\n\t\tdirection.xz *= -1.0;\n\t}\n\treturn direction;\n}\nvoid main() {\n\tvOutputDirection = getDirection(uv, faceIndex);\n\tgl_Position = vec4( position, 1.0 );\n}\n\t";
+
+	}
+
+	function _getEncodings() {
+
+		return "\nuniform int inputEncoding;\nuniform int outputEncoding;\n\n#include <encodings_pars_fragment>\n\nvec4 inputTexelToLinear(vec4 value){\n\tif(inputEncoding == 0){\n\t\treturn value;\n\t}else if(inputEncoding == 1){\n\t\treturn sRGBToLinear(value);\n\t}else if(inputEncoding == 2){\n\t\treturn RGBEToLinear(value);\n\t}else if(inputEncoding == 3){\n\t\treturn RGBMToLinear(value, 7.0);\n\t}else if(inputEncoding == 4){\n\t\treturn RGBMToLinear(value, 16.0);\n\t}else if(inputEncoding == 5){\n\t\treturn RGBDToLinear(value, 256.0);\n\t}else{\n\t\treturn GammaToLinear(value, 2.2);\n\t}\n}\n\nvec4 linearToOutputTexel(vec4 value){\n\tif(outputEncoding == 0){\n\t\treturn value;\n\t}else if(outputEncoding == 1){\n\t\treturn LinearTosRGB(value);\n\t}else if(outputEncoding == 2){\n\t\treturn LinearToRGBE(value);\n\t}else if(outputEncoding == 3){\n\t\treturn LinearToRGBM(value, 7.0);\n\t}else if(outputEncoding == 4){\n\t\treturn LinearToRGBM(value, 16.0);\n\t}else if(outputEncoding == 5){\n\t\treturn LinearToRGBD(value, 256.0);\n\t}else{\n\t\treturn LinearToGamma(value, 2.2);\n\t}\n}\n\nvec4 envMapTexelToLinear(vec4 color) {\n\treturn inputTexelToLinear(color);\n}\n\t";
+
+	}
+
+	/**
+	 * @author mrdoob / http://mrdoob.com/
+	 */
+
+	function Face4( a, b, c, d, normal, color, materialIndex ) {
+
+		console.warn( 'THREE.Face4 has been removed. A THREE.Face3 will be created instead.' );
+		return new Face3( a, b, c, normal, color, materialIndex );
+
+	}
+
+	var LineStrip = 0;
+	var LinePieces = 1;
+	var NoColors = 0;
+	var FaceColors = 1;
+	var VertexColors = 2;
+
+	function MeshFaceMaterial( materials ) {
+
+		console.warn( 'THREE.MeshFaceMaterial has been removed. Use an Array instead.' );
+		return materials;
+
+	}
+
+	function MultiMaterial( materials ) {
+
+		if ( materials === undefined ) { materials = []; }
+
+		console.warn( 'THREE.MultiMaterial has been removed. Use an Array instead.' );
+		materials.isMultiMaterial = true;
+		materials.materials = materials;
+		materials.clone = function () {
+
+			return materials.slice();
+
+		};
+
+		return materials;
+
+	}
+
+	function PointCloud( geometry, material ) {
+
+		console.warn( 'THREE.PointCloud has been renamed to THREE.Points.' );
+		return new Points( geometry, material );
+
+	}
+
+	function Particle( material ) {
+
+		console.warn( 'THREE.Particle has been renamed to THREE.Sprite.' );
+		return new Sprite( material );
+
+	}
+
+	function ParticleSystem( geometry, material ) {
+
+		console.warn( 'THREE.ParticleSystem has been renamed to THREE.Points.' );
+		return new Points( geometry, material );
+
+	}
+
+	function PointCloudMaterial( parameters ) {
+
+		console.warn( 'THREE.PointCloudMaterial has been renamed to THREE.PointsMaterial.' );
+		return new PointsMaterial( parameters );
+
+	}
+
+	function ParticleBasicMaterial( parameters ) {
+
+		console.warn( 'THREE.ParticleBasicMaterial has been renamed to THREE.PointsMaterial.' );
+		return new PointsMaterial( parameters );
+
+	}
+
+	function ParticleSystemMaterial( parameters ) {
+
+		console.warn( 'THREE.ParticleSystemMaterial has been renamed to THREE.PointsMaterial.' );
+		return new PointsMaterial( parameters );
+
+	}
+
+	function Vertex( x, y, z ) {
+
+		console.warn( 'THREE.Vertex has been removed. Use THREE.Vector3 instead.' );
+		return new Vector3( x, y, z );
+
+	}
+
+	//
+
+	function DynamicBufferAttribute( array, itemSize ) {
+
+		console.warn( 'THREE.DynamicBufferAttribute has been removed. Use new THREE.BufferAttribute().setUsage( THREE.DynamicDrawUsage ) instead.' );
+		return new BufferAttribute( array, itemSize ).setUsage( DynamicDrawUsage );
+
+	}
+
+	function Int8Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Int8Attribute has been removed. Use new THREE.Int8BufferAttribute() instead.' );
+		return new Int8BufferAttribute( array, itemSize );
+
+	}
+
+	function Uint8Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Uint8Attribute has been removed. Use new THREE.Uint8BufferAttribute() instead.' );
+		return new Uint8BufferAttribute( array, itemSize );
+
+	}
+
+	function Uint8ClampedAttribute( array, itemSize ) {
+
+		console.warn( 'THREE.Uint8ClampedAttribute has been removed. Use new THREE.Uint8ClampedBufferAttribute() instead.' );
+		return new Uint8ClampedBufferAttribute( array, itemSize );
+
+	}
+
+	function Int16Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Int16Attribute has been removed. Use new THREE.Int16BufferAttribute() instead.' );
+		return new Int16BufferAttribute( array, itemSize );
+
+	}
+
+	function Uint16Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Uint16Attribute has been removed. Use new THREE.Uint16BufferAttribute() instead.' );
+		return new Uint16BufferAttribute( array, itemSize );
+
+	}
+
+	function Int32Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Int32Attribute has been removed. Use new THREE.Int32BufferAttribute() instead.' );
+		return new Int32BufferAttribute( array, itemSize );
+
+	}
+
+	function Uint32Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Uint32Attribute has been removed. Use new THREE.Uint32BufferAttribute() instead.' );
+		return new Uint32BufferAttribute( array, itemSize );
+
+	}
+
+	function Float32Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Float32Attribute has been removed. Use new THREE.Float32BufferAttribute() instead.' );
+		return new Float32BufferAttribute( array, itemSize );
+
+	}
+
+	function Float64Attribute( array, itemSize ) {
+
+		console.warn( 'THREE.Float64Attribute has been removed. Use new THREE.Float64BufferAttribute() instead.' );
+		return new Float64BufferAttribute( array, itemSize );
+
+	}
+
+	//
+
+	Curve.create = function ( construct, getPoint ) {
+
+		console.log( 'THREE.Curve.create() has been deprecated' );
+
+		construct.prototype = Object.create( Curve.prototype );
+		construct.prototype.constructor = construct;
+		construct.prototype.getPoint = getPoint;
+
+		return construct;
+
+	};
+
+	//
+
+	Object.assign( CurvePath.prototype, {
+
+		createPointsGeometry: function ( divisions ) {
+
+			console.warn( 'THREE.CurvePath: .createPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+
+			// generate geometry from path points (for Line or Points objects)
+
+			var pts = this.getPoints( divisions );
+			return this.createGeometry( pts );
+
+		},
+
+		createSpacedPointsGeometry: function ( divisions ) {
+
+			console.warn( 'THREE.CurvePath: .createSpacedPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+
+			// generate geometry from equidistant sampling along the path
+
+			var pts = this.getSpacedPoints( divisions );
+			return this.createGeometry( pts );
+
+		},
+
+		createGeometry: function ( points ) {
+
+			console.warn( 'THREE.CurvePath: .createGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' );
+
+			var geometry = new Geometry();
+
+			for ( var i = 0, l = points.length; i < l; i ++ ) {
+
+				var point = points[ i ];
+				geometry.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
+
+			}
+
+			return geometry;
+
+		}
+
+	} );
+
+	//
+
+	Object.assign( Path.prototype, {
+
+		fromPoints: function ( points ) {
+
+			console.warn( 'THREE.Path: .fromPoints() has been renamed to .setFromPoints().' );
+			return this.setFromPoints( points );
+
+		}
+
+	} );
+
+	//
+
+	function ClosedSplineCurve3( points ) {
+
+		console.warn( 'THREE.ClosedSplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' );
+
+		CatmullRomCurve3.call( this, points );
+		this.type = 'catmullrom';
+		this.closed = true;
+
+	}
+
+	ClosedSplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype );
+
+	//
+
+	function SplineCurve3( points ) {
+
+		console.warn( 'THREE.SplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' );
+
+		CatmullRomCurve3.call( this, points );
+		this.type = 'catmullrom';
+
+	}
+
+	SplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype );
+
+	//
+
+	function Spline( points ) {
+
+		console.warn( 'THREE.Spline has been removed. Use THREE.CatmullRomCurve3 instead.' );
+
+		CatmullRomCurve3.call( this, points );
+		this.type = 'catmullrom';
+
+	}
+
+	Spline.prototype = Object.create( CatmullRomCurve3.prototype );
+
+	Object.assign( Spline.prototype, {
+
+		initFromArray: function ( /* a */ ) {
+
+			console.error( 'THREE.Spline: .initFromArray() has been removed.' );
+
+		},
+		getControlPointsArray: function ( /* optionalTarget */ ) {
+
+			console.error( 'THREE.Spline: .getControlPointsArray() has been removed.' );
+
+		},
+		reparametrizeByArcLength: function ( /* samplingCoef */ ) {
+
+			console.error( 'THREE.Spline: .reparametrizeByArcLength() has been removed.' );
+
+		}
+
+	} );
+
+	//
+
+	function AxisHelper( size ) {
+
+		console.warn( 'THREE.AxisHelper has been renamed to THREE.AxesHelper.' );
+		return new AxesHelper( size );
+
+	}
+
+	function BoundingBoxHelper( object, color ) {
+
+		console.warn( 'THREE.BoundingBoxHelper has been deprecated. Creating a THREE.BoxHelper instead.' );
+		return new BoxHelper( object, color );
+
+	}
+
+	function EdgesHelper( object, hex ) {
+
+		console.warn( 'THREE.EdgesHelper has been removed. Use THREE.EdgesGeometry instead.' );
+		return new LineSegments( new EdgesGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) );
+
+	}
+
+	GridHelper.prototype.setColors = function () {
+
+		console.error( 'THREE.GridHelper: setColors() has been deprecated, pass them in the constructor instead.' );
+
+	};
+
+	SkeletonHelper.prototype.update = function () {
+
+		console.error( 'THREE.SkeletonHelper: update() no longer needs to be called.' );
+
+	};
+
+	function WireframeHelper( object, hex ) {
+
+		console.warn( 'THREE.WireframeHelper has been removed. Use THREE.WireframeGeometry instead.' );
+		return new LineSegments( new WireframeGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) );
+
+	}
+
+	//
+
+	Object.assign( Loader.prototype, {
+
+		extractUrlBase: function ( url ) {
+
+			console.warn( 'THREE.Loader: .extractUrlBase() has been deprecated. Use THREE.LoaderUtils.extractUrlBase() instead.' );
+			return LoaderUtils.extractUrlBase( url );
+
+		}
+
+	} );
+
+	Loader.Handlers = {
+
+		add: function ( /* regex, loader */ ) {
+
+			console.error( 'THREE.Loader: Handlers.add() has been removed. Use LoadingManager.addHandler() instead.' );
+
+		},
+
+		get: function ( /* file */ ) {
+
+			console.error( 'THREE.Loader: Handlers.get() has been removed. Use LoadingManager.getHandler() instead.' );
+
+		}
+
+	};
+
+	function XHRLoader( manager ) {
+
+		console.warn( 'THREE.XHRLoader has been renamed to THREE.FileLoader.' );
+		return new FileLoader( manager );
+
+	}
+
+	function BinaryTextureLoader( manager ) {
+
+		console.warn( 'THREE.BinaryTextureLoader has been renamed to THREE.DataTextureLoader.' );
+		return new DataTextureLoader( manager );
+
+	}
+
+	Object.assign( ObjectLoader.prototype, {
+
+		setTexturePath: function ( value ) {
+
+			console.warn( 'THREE.ObjectLoader: .setTexturePath() has been renamed to .setResourcePath().' );
+			return this.setResourcePath( value );
+
+		}
+
+	} );
+
+	//
+
+	Object.assign( Box2.prototype, {
+
+		center: function ( optionalTarget ) {
+
+			console.warn( 'THREE.Box2: .center() has been renamed to .getCenter().' );
+			return this.getCenter( optionalTarget );
+
+		},
+		empty: function () {
+
+			console.warn( 'THREE.Box2: .empty() has been renamed to .isEmpty().' );
+			return this.isEmpty();
+
+		},
+		isIntersectionBox: function ( box ) {
+
+			console.warn( 'THREE.Box2: .isIntersectionBox() has been renamed to .intersectsBox().' );
+			return this.intersectsBox( box );
+
+		},
+		size: function ( optionalTarget ) {
+
+			console.warn( 'THREE.Box2: .size() has been renamed to .getSize().' );
+			return this.getSize( optionalTarget );
+
+		}
+	} );
+
+	Object.assign( Box3.prototype, {
+
+		center: function ( optionalTarget ) {
+
+			console.warn( 'THREE.Box3: .center() has been renamed to .getCenter().' );
+			return this.getCenter( optionalTarget );
+
+		},
+		empty: function () {
+
+			console.warn( 'THREE.Box3: .empty() has been renamed to .isEmpty().' );
+			return this.isEmpty();
+
+		},
+		isIntersectionBox: function ( box ) {
+
+			console.warn( 'THREE.Box3: .isIntersectionBox() has been renamed to .intersectsBox().' );
+			return this.intersectsBox( box );
+
+		},
+		isIntersectionSphere: function ( sphere ) {
+
+			console.warn( 'THREE.Box3: .isIntersectionSphere() has been renamed to .intersectsSphere().' );
+			return this.intersectsSphere( sphere );
+
+		},
+		size: function ( optionalTarget ) {
+
+			console.warn( 'THREE.Box3: .size() has been renamed to .getSize().' );
+			return this.getSize( optionalTarget );
+
+		}
+	} );
+
+	Object.assign( Sphere.prototype, {
+
+		empty: function () {
+
+			console.warn( 'THREE.Sphere: .empty() has been renamed to .isEmpty().' );
+			return this.isEmpty();
+
+		},
+
+	} );
+
+	Frustum.prototype.setFromMatrix = function ( m ) {
+
+		console.warn( 'THREE.Frustum: .setFromMatrix() has been renamed to .setFromProjectionMatrix().' );
+		return this.setFromProjectionMatrix( m );
+
+	};
+
+	Line3.prototype.center = function ( optionalTarget ) {
+
+		console.warn( 'THREE.Line3: .center() has been renamed to .getCenter().' );
+		return this.getCenter( optionalTarget );
+
+	};
+
+	Object.assign( MathUtils, {
+
+		random16: function () {
+
+			console.warn( 'THREE.Math: .random16() has been deprecated. Use Math.random() instead.' );
+			return Math.random();
+
+		},
+
+		nearestPowerOfTwo: function ( value ) {
+
+			console.warn( 'THREE.Math: .nearestPowerOfTwo() has been renamed to .floorPowerOfTwo().' );
+			return MathUtils.floorPowerOfTwo( value );
+
+		},
+
+		nextPowerOfTwo: function ( value ) {
+
+			console.warn( 'THREE.Math: .nextPowerOfTwo() has been renamed to .ceilPowerOfTwo().' );
+			return MathUtils.ceilPowerOfTwo( value );
+
+		}
+
+	} );
+
+	Object.assign( Matrix3.prototype, {
+
+		flattenToArrayOffset: function ( array, offset ) {
+
+			console.warn( "THREE.Matrix3: .flattenToArrayOffset() has been deprecated. Use .toArray() instead." );
+			return this.toArray( array, offset );
+
+		},
+		multiplyVector3: function ( vector ) {
+
+			console.warn( 'THREE.Matrix3: .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' );
+			return vector.applyMatrix3( this );
+
+		},
+		multiplyVector3Array: function ( /* a */ ) {
+
+			console.error( 'THREE.Matrix3: .multiplyVector3Array() has been removed.' );
+
+		},
+		applyToBufferAttribute: function ( attribute ) {
+
+			console.warn( 'THREE.Matrix3: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix3( matrix ) instead.' );
+			return attribute.applyMatrix3( this );
+
+		},
+		applyToVector3Array: function ( /* array, offset, length */ ) {
+
+			console.error( 'THREE.Matrix3: .applyToVector3Array() has been removed.' );
+
+		}
+
+	} );
+
+	Object.assign( Matrix4.prototype, {
+
+		extractPosition: function ( m ) {
+
+			console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
+			return this.copyPosition( m );
+
+		},
+		flattenToArrayOffset: function ( array, offset ) {
+
+			console.warn( "THREE.Matrix4: .flattenToArrayOffset() has been deprecated. Use .toArray() instead." );
+			return this.toArray( array, offset );
+
+		},
+		getPosition: function () {
+
+			console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
+			return new Vector3().setFromMatrixColumn( this, 3 );
+
+		},
+		setRotationFromQuaternion: function ( q ) {
+
+			console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
+			return this.makeRotationFromQuaternion( q );
+
+		},
+		multiplyToArray: function () {
+
+			console.warn( 'THREE.Matrix4: .multiplyToArray() has been removed.' );
+
+		},
+		multiplyVector3: function ( vector ) {
+
+			console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
+			return vector.applyMatrix4( this );
+
+		},
+		multiplyVector4: function ( vector ) {
+
+			console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
+			return vector.applyMatrix4( this );
+
+		},
+		multiplyVector3Array: function ( /* a */ ) {
+
+			console.error( 'THREE.Matrix4: .multiplyVector3Array() has been removed.' );
+
+		},
+		rotateAxis: function ( v ) {
+
+			console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
+			v.transformDirection( this );
+
+		},
+		crossVector: function ( vector ) {
+
+			console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
+			return vector.applyMatrix4( this );
+
+		},
+		translate: function () {
+
+			console.error( 'THREE.Matrix4: .translate() has been removed.' );
+
+		},
+		rotateX: function () {
+
+			console.error( 'THREE.Matrix4: .rotateX() has been removed.' );
+
+		},
+		rotateY: function () {
+
+			console.error( 'THREE.Matrix4: .rotateY() has been removed.' );
+
+		},
+		rotateZ: function () {
+
+			console.error( 'THREE.Matrix4: .rotateZ() has been removed.' );
+
+		},
+		rotateByAxis: function () {
+
+			console.error( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
+
+		},
+		applyToBufferAttribute: function ( attribute ) {
+
+			console.warn( 'THREE.Matrix4: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix4( matrix ) instead.' );
+			return attribute.applyMatrix4( this );
+
+		},
+		applyToVector3Array: function ( /* array, offset, length */ ) {
+
+			console.error( 'THREE.Matrix4: .applyToVector3Array() has been removed.' );
+
+		},
+		makeFrustum: function ( left, right, bottom, top, near, far ) {
+
+			console.warn( 'THREE.Matrix4: .makeFrustum() has been removed. Use .makePerspective( left, right, top, bottom, near, far ) instead.' );
+			return this.makePerspective( left, right, top, bottom, near, far );
+
+		}
+
+	} );
+
+	Plane.prototype.isIntersectionLine = function ( line ) {
+
+		console.warn( 'THREE.Plane: .isIntersectionLine() has been renamed to .intersectsLine().' );
+		return this.intersectsLine( line );
+
+	};
+
+	Quaternion.prototype.multiplyVector3 = function ( vector ) {
+
+		console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
+		return vector.applyQuaternion( this );
+
+	};
+
+	Object.assign( Ray.prototype, {
+
+		isIntersectionBox: function ( box ) {
+
+			console.warn( 'THREE.Ray: .isIntersectionBox() has been renamed to .intersectsBox().' );
+			return this.intersectsBox( box );
+
+		},
+		isIntersectionPlane: function ( plane ) {
+
+			console.warn( 'THREE.Ray: .isIntersectionPlane() has been renamed to .intersectsPlane().' );
+			return this.intersectsPlane( plane );
+
+		},
+		isIntersectionSphere: function ( sphere ) {
+
+			console.warn( 'THREE.Ray: .isIntersectionSphere() has been renamed to .intersectsSphere().' );
+			return this.intersectsSphere( sphere );
+
+		}
+
+	} );
+
+	Object.assign( Triangle.prototype, {
+
+		area: function () {
+
+			console.warn( 'THREE.Triangle: .area() has been renamed to .getArea().' );
+			return this.getArea();
+
+		},
+		barycoordFromPoint: function ( point, target ) {
+
+			console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' );
+			return this.getBarycoord( point, target );
+
+		},
+		midpoint: function ( target ) {
+
+			console.warn( 'THREE.Triangle: .midpoint() has been renamed to .getMidpoint().' );
+			return this.getMidpoint( target );
+
+		},
+		normal: function ( target ) {
+
+			console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' );
+			return this.getNormal( target );
+
+		},
+		plane: function ( target ) {
+
+			console.warn( 'THREE.Triangle: .plane() has been renamed to .getPlane().' );
+			return this.getPlane( target );
+
+		}
+
+	} );
+
+	Object.assign( Triangle, {
+
+		barycoordFromPoint: function ( point, a, b, c, target ) {
+
+			console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' );
+			return Triangle.getBarycoord( point, a, b, c, target );
+
+		},
+		normal: function ( a, b, c, target ) {
+
+			console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' );
+			return Triangle.getNormal( a, b, c, target );
+
+		}
+
+	} );
+
+	Object.assign( Shape.prototype, {
+
+		extractAllPoints: function ( divisions ) {
+
+			console.warn( 'THREE.Shape: .extractAllPoints() has been removed. Use .extractPoints() instead.' );
+			return this.extractPoints( divisions );
+
+		},
+		extrude: function ( options ) {
+
+			console.warn( 'THREE.Shape: .extrude() has been removed. Use ExtrudeGeometry() instead.' );
+			return new ExtrudeGeometry( this, options );
+
+		},
+		makeGeometry: function ( options ) {
+
+			console.warn( 'THREE.Shape: .makeGeometry() has been removed. Use ShapeGeometry() instead.' );
+			return new ShapeGeometry( this, options );
+
+		}
+
+	} );
+
+	Object.assign( Vector2.prototype, {
+
+		fromAttribute: function ( attribute, index, offset ) {
+
+			console.warn( 'THREE.Vector2: .fromAttribute() has been renamed to .fromBufferAttribute().' );
+			return this.fromBufferAttribute( attribute, index, offset );
+
+		},
+		distanceToManhattan: function ( v ) {
+
+			console.warn( 'THREE.Vector2: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' );
+			return this.manhattanDistanceTo( v );
+
+		},
+		lengthManhattan: function () {
+
+			console.warn( 'THREE.Vector2: .lengthManhattan() has been renamed to .manhattanLength().' );
+			return this.manhattanLength();
+
+		}
+
+	} );
+
+	Object.assign( Vector3.prototype, {
+
+		setEulerFromRotationMatrix: function () {
+
+			console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
+
+		},
+		setEulerFromQuaternion: function () {
+
+			console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
+
+		},
+		getPositionFromMatrix: function ( m ) {
+
+			console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
+			return this.setFromMatrixPosition( m );
+
+		},
+		getScaleFromMatrix: function ( m ) {
+
+			console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
+			return this.setFromMatrixScale( m );
+
+		},
+		getColumnFromMatrix: function ( index, matrix ) {
+
+			console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
+			return this.setFromMatrixColumn( matrix, index );
+
+		},
+		applyProjection: function ( m ) {
+
+			console.warn( 'THREE.Vector3: .applyProjection() has been removed. Use .applyMatrix4( m ) instead.' );
+			return this.applyMatrix4( m );
+
+		},
+		fromAttribute: function ( attribute, index, offset ) {
+
+			console.warn( 'THREE.Vector3: .fromAttribute() has been renamed to .fromBufferAttribute().' );
+			return this.fromBufferAttribute( attribute, index, offset );
+
+		},
+		distanceToManhattan: function ( v ) {
+
+			console.warn( 'THREE.Vector3: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' );
+			return this.manhattanDistanceTo( v );
+
+		},
+		lengthManhattan: function () {
+
+			console.warn( 'THREE.Vector3: .lengthManhattan() has been renamed to .manhattanLength().' );
+			return this.manhattanLength();
+
+		}
+
+	} );
+
+	Object.assign( Vector4.prototype, {
+
+		fromAttribute: function ( attribute, index, offset ) {
+
+			console.warn( 'THREE.Vector4: .fromAttribute() has been renamed to .fromBufferAttribute().' );
+			return this.fromBufferAttribute( attribute, index, offset );
+
+		},
+		lengthManhattan: function () {
+
+			console.warn( 'THREE.Vector4: .lengthManhattan() has been renamed to .manhattanLength().' );
+			return this.manhattanLength();
+
+		}
+
+	} );
+
+	//
+
+	Object.assign( Geometry.prototype, {
+
+		computeTangents: function () {
+
+			console.error( 'THREE.Geometry: .computeTangents() has been removed.' );
+
+		},
+		computeLineDistances: function () {
+
+			console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' );
+
+		},
+		applyMatrix: function ( matrix ) {
+
+			console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' );
+			return this.applyMatrix4( matrix );
+
+		}
+
+	} );
+
+	Object.assign( Object3D.prototype, {
+
+		getChildByName: function ( name ) {
+
+			console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
+			return this.getObjectByName( name );
+
+		},
+		renderDepth: function () {
+
+			console.warn( 'THREE.Object3D: .renderDepth has been removed. Use .renderOrder, instead.' );
+
+		},
+		translate: function ( distance, axis ) {
+
+			console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
+			return this.translateOnAxis( axis, distance );
+
+		},
+		getWorldRotation: function () {
+
+			console.error( 'THREE.Object3D: .getWorldRotation() has been removed. Use THREE.Object3D.getWorldQuaternion( target ) instead.' );
+
+		},
+		applyMatrix: function ( matrix ) {
+
+			console.warn( 'THREE.Object3D: .applyMatrix() has been renamed to .applyMatrix4().' );
+			return this.applyMatrix4( matrix );
+
+		}
+
+	} );
+
+	Object.defineProperties( Object3D.prototype, {
+
+		eulerOrder: {
+			get: function () {
+
+				console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' );
+				return this.rotation.order;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' );
+				this.rotation.order = value;
+
+			}
+		},
+		useQuaternion: {
+			get: function () {
+
+				console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
+
+			}
+		}
+
+	} );
+
+	Object.assign( Mesh.prototype, {
+
+		setDrawMode: function () {
+
+			console.error( 'THREE.Mesh: .setDrawMode() has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' );
+
+		},
+
+	} );
+
+	Object.defineProperties( Mesh.prototype, {
+
+		drawMode: {
+			get: function () {
+
+				console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode.' );
+				return TrianglesDrawMode;
+
+			},
+			set: function () {
+
+				console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' );
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( LOD.prototype, {
+
+		objects: {
+			get: function () {
+
+				console.warn( 'THREE.LOD: .objects has been renamed to .levels.' );
+				return this.levels;
+
+			}
+		}
+
+	} );
+
+	Object.defineProperty( Skeleton.prototype, 'useVertexTexture', {
+
+		get: function () {
+
+			console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' );
+
+		},
+		set: function () {
+
+			console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' );
+
+		}
+
+	} );
+
+	SkinnedMesh.prototype.initBones = function () {
+
+		console.error( 'THREE.SkinnedMesh: initBones() has been removed.' );
+
+	};
+
+	Object.defineProperty( Curve.prototype, '__arcLengthDivisions', {
+
+		get: function () {
+
+			console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
+			return this.arcLengthDivisions;
+
+		},
+		set: function ( value ) {
+
+			console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' );
+			this.arcLengthDivisions = value;
+
+		}
+
+	} );
+
+	//
+
+	PerspectiveCamera.prototype.setLens = function ( focalLength, filmGauge ) {
+
+		console.warn( "THREE.PerspectiveCamera.setLens is deprecated. " +
+				"Use .setFocalLength and .filmGauge for a photographic setup." );
+
+		if ( filmGauge !== undefined ) { this.filmGauge = filmGauge; }
+		this.setFocalLength( focalLength );
+
+	};
+
+	//
+
+	Object.defineProperties( Light.prototype, {
+		onlyShadow: {
+			set: function () {
+
+				console.warn( 'THREE.Light: .onlyShadow has been removed.' );
+
+			}
+		},
+		shadowCameraFov: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraFov is now .shadow.camera.fov.' );
+				this.shadow.camera.fov = value;
+
+			}
+		},
+		shadowCameraLeft: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraLeft is now .shadow.camera.left.' );
+				this.shadow.camera.left = value;
+
+			}
+		},
+		shadowCameraRight: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraRight is now .shadow.camera.right.' );
+				this.shadow.camera.right = value;
+
+			}
+		},
+		shadowCameraTop: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraTop is now .shadow.camera.top.' );
+				this.shadow.camera.top = value;
+
+			}
+		},
+		shadowCameraBottom: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraBottom is now .shadow.camera.bottom.' );
+				this.shadow.camera.bottom = value;
+
+			}
+		},
+		shadowCameraNear: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraNear is now .shadow.camera.near.' );
+				this.shadow.camera.near = value;
+
+			}
+		},
+		shadowCameraFar: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowCameraFar is now .shadow.camera.far.' );
+				this.shadow.camera.far = value;
+
+			}
+		},
+		shadowCameraVisible: {
+			set: function () {
+
+				console.warn( 'THREE.Light: .shadowCameraVisible has been removed. Use new THREE.CameraHelper( light.shadow.camera ) instead.' );
+
+			}
+		},
+		shadowBias: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowBias is now .shadow.bias.' );
+				this.shadow.bias = value;
+
+			}
+		},
+		shadowDarkness: {
+			set: function () {
+
+				console.warn( 'THREE.Light: .shadowDarkness has been removed.' );
+
+			}
+		},
+		shadowMapWidth: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowMapWidth is now .shadow.mapSize.width.' );
+				this.shadow.mapSize.width = value;
+
+			}
+		},
+		shadowMapHeight: {
+			set: function ( value ) {
+
+				console.warn( 'THREE.Light: .shadowMapHeight is now .shadow.mapSize.height.' );
+				this.shadow.mapSize.height = value;
+
+			}
+		}
+	} );
+
+	//
+
+	Object.defineProperties( BufferAttribute.prototype, {
+
+		length: {
+			get: function () {
+
+				console.warn( 'THREE.BufferAttribute: .length has been deprecated. Use .count instead.' );
+				return this.array.length;
+
+			}
+		},
+		dynamic: {
+			get: function () {
+
+				console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' );
+				return this.usage === DynamicDrawUsage;
+
+			},
+			set: function ( /* value */ ) {
+
+				console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' );
+				this.setUsage( DynamicDrawUsage );
+
+			}
+		}
+
+	} );
+
+	Object.assign( BufferAttribute.prototype, {
+		setDynamic: function ( value ) {
+
+			console.warn( 'THREE.BufferAttribute: .setDynamic() has been deprecated. Use .setUsage() instead.' );
+			this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage );
+			return this;
+
+		},
+		copyIndicesArray: function ( /* indices */ ) {
+
+			console.error( 'THREE.BufferAttribute: .copyIndicesArray() has been removed.' );
+
+		},
+		setArray: function ( /* array */ ) {
+
+			console.error( 'THREE.BufferAttribute: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' );
+
+		}
+	} );
+
+	Object.assign( BufferGeometry.prototype, {
+
+		addIndex: function ( index ) {
+
+			console.warn( 'THREE.BufferGeometry: .addIndex() has been renamed to .setIndex().' );
+			this.setIndex( index );
+
+		},
+		addAttribute: function ( name, attribute ) {
+
+			console.warn( 'THREE.BufferGeometry: .addAttribute() has been renamed to .setAttribute().' );
+
+			if ( ! ( attribute && attribute.isBufferAttribute ) && ! ( attribute && attribute.isInterleavedBufferAttribute ) ) {
+
+				console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
+
+				return this.setAttribute( name, new BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
+
+			}
+
+			if ( name === 'index' ) {
+
+				console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' );
+				this.setIndex( attribute );
+
+				return this;
+
+			}
+
+			return this.setAttribute( name, attribute );
+
+		},
+		addDrawCall: function ( start, count, indexOffset ) {
+
+			if ( indexOffset !== undefined ) {
+
+				console.warn( 'THREE.BufferGeometry: .addDrawCall() no longer supports indexOffset.' );
+
+			}
+
+			console.warn( 'THREE.BufferGeometry: .addDrawCall() is now .addGroup().' );
+			this.addGroup( start, count );
+
+		},
+		clearDrawCalls: function () {
+
+			console.warn( 'THREE.BufferGeometry: .clearDrawCalls() is now .clearGroups().' );
+			this.clearGroups();
+
+		},
+		computeTangents: function () {
+
+			console.warn( 'THREE.BufferGeometry: .computeTangents() has been removed.' );
+
+		},
+		computeOffsets: function () {
+
+			console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.' );
+
+		},
+		removeAttribute: function ( name ) {
+
+			console.warn( 'THREE.BufferGeometry: .removeAttribute() has been renamed to .deleteAttribute().' );
+
+			return this.deleteAttribute( name );
+
+		},
+		applyMatrix: function ( matrix ) {
+
+			console.warn( 'THREE.BufferGeometry: .applyMatrix() has been renamed to .applyMatrix4().' );
+			return this.applyMatrix4( matrix );
+
+		}
+
+	} );
+
+	Object.defineProperties( BufferGeometry.prototype, {
+
+		drawcalls: {
+			get: function () {
+
+				console.error( 'THREE.BufferGeometry: .drawcalls has been renamed to .groups.' );
+				return this.groups;
+
+			}
+		},
+		offsets: {
+			get: function () {
+
+				console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .groups.' );
+				return this.groups;
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( Raycaster.prototype, {
+
+		linePrecision: {
+			get: function () {
+
+				console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' );
+				return this.params.Line.threshold;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' );
+				this.params.Line.threshold = value;
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( InterleavedBuffer.prototype, {
+
+		dynamic: {
+			get: function () {
+
+				console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' );
+				return this.usage === DynamicDrawUsage;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' );
+				this.setUsage( value );
+
+			}
+		}
+
+	} );
+
+	Object.assign( InterleavedBuffer.prototype, {
+		setDynamic: function ( value ) {
+
+			console.warn( 'THREE.InterleavedBuffer: .setDynamic() has been deprecated. Use .setUsage() instead.' );
+			this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage );
+			return this;
+
+		},
+		setArray: function ( /* array */ ) {
+
+			console.error( 'THREE.InterleavedBuffer: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' );
+
+		}
+	} );
+
+	//
+
+	Object.assign( ExtrudeBufferGeometry.prototype, {
+
+		getArrays: function () {
+
+			console.error( 'THREE.ExtrudeBufferGeometry: .getArrays() has been removed.' );
+
+		},
+
+		addShapeList: function () {
+
+			console.error( 'THREE.ExtrudeBufferGeometry: .addShapeList() has been removed.' );
+
+		},
+
+		addShape: function () {
+
+			console.error( 'THREE.ExtrudeBufferGeometry: .addShape() has been removed.' );
+
+		}
+
+	} );
+
+	//
+
+	Object.defineProperties( Uniform.prototype, {
+
+		dynamic: {
+			set: function () {
+
+				console.warn( 'THREE.Uniform: .dynamic has been removed. Use object.onBeforeRender() instead.' );
+
+			}
+		},
+		onUpdate: {
+			value: function () {
+
+				console.warn( 'THREE.Uniform: .onUpdate() has been removed. Use object.onBeforeRender() instead.' );
+				return this;
+
+			}
+		}
+
+	} );
+
+	//
+
+	Object.defineProperties( Material.prototype, {
+
+		wrapAround: {
+			get: function () {
+
+				console.warn( 'THREE.Material: .wrapAround has been removed.' );
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.Material: .wrapAround has been removed.' );
+
+			}
+		},
+
+		overdraw: {
+			get: function () {
+
+				console.warn( 'THREE.Material: .overdraw has been removed.' );
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.Material: .overdraw has been removed.' );
+
+			}
+		},
+
+		wrapRGB: {
+			get: function () {
+
+				console.warn( 'THREE.Material: .wrapRGB has been removed.' );
+				return new Color();
+
+			}
+		},
+
+		shading: {
+			get: function () {
+
+				console.error( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
+				this.flatShading = ( value === FlatShading );
+
+			}
+		},
+
+		stencilMask: {
+			get: function () {
+
+				console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' );
+				return this.stencilFuncMask;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' );
+				this.stencilFuncMask = value;
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( MeshPhongMaterial.prototype, {
+
+		metal: {
+			get: function () {
+
+				console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead.' );
+				return false;
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead' );
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( ShaderMaterial.prototype, {
+
+		derivatives: {
+			get: function () {
+
+				console.warn( 'THREE.ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' );
+				return this.extensions.derivatives;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE. ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' );
+				this.extensions.derivatives = value;
+
+			}
+		}
+
+	} );
+
+	//
+
+	Object.assign( WebGLRenderer.prototype, {
+
+		clearTarget: function ( renderTarget, color, depth, stencil ) {
+
+			console.warn( 'THREE.WebGLRenderer: .clearTarget() has been deprecated. Use .setRenderTarget() and .clear() instead.' );
+			this.setRenderTarget( renderTarget );
+			this.clear( color, depth, stencil );
+
+		},
+		animate: function ( callback ) {
+
+			console.warn( 'THREE.WebGLRenderer: .animate() is now .setAnimationLoop().' );
+			this.setAnimationLoop( callback );
+
+		},
+		getCurrentRenderTarget: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .getCurrentRenderTarget() is now .getRenderTarget().' );
+			return this.getRenderTarget();
+
+		},
+		getMaxAnisotropy: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .getMaxAnisotropy() is now .capabilities.getMaxAnisotropy().' );
+			return this.capabilities.getMaxAnisotropy();
+
+		},
+		getPrecision: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .getPrecision() is now .capabilities.precision.' );
+			return this.capabilities.precision;
+
+		},
+		resetGLState: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .resetGLState() is now .state.reset().' );
+			return this.state.reset();
+
+		},
+		supportsFloatTextures: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsFloatTextures() is now .extensions.get( \'OES_texture_float\' ).' );
+			return this.extensions.get( 'OES_texture_float' );
+
+		},
+		supportsHalfFloatTextures: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsHalfFloatTextures() is now .extensions.get( \'OES_texture_half_float\' ).' );
+			return this.extensions.get( 'OES_texture_half_float' );
+
+		},
+		supportsStandardDerivatives: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsStandardDerivatives() is now .extensions.get( \'OES_standard_derivatives\' ).' );
+			return this.extensions.get( 'OES_standard_derivatives' );
+
+		},
+		supportsCompressedTextureS3TC: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsCompressedTextureS3TC() is now .extensions.get( \'WEBGL_compressed_texture_s3tc\' ).' );
+			return this.extensions.get( 'WEBGL_compressed_texture_s3tc' );
+
+		},
+		supportsCompressedTexturePVRTC: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsCompressedTexturePVRTC() is now .extensions.get( \'WEBGL_compressed_texture_pvrtc\' ).' );
+			return this.extensions.get( 'WEBGL_compressed_texture_pvrtc' );
+
+		},
+		supportsBlendMinMax: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsBlendMinMax() is now .extensions.get( \'EXT_blend_minmax\' ).' );
+			return this.extensions.get( 'EXT_blend_minmax' );
+
+		},
+		supportsVertexTextures: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsVertexTextures() is now .capabilities.vertexTextures.' );
+			return this.capabilities.vertexTextures;
+
+		},
+		supportsInstancedArrays: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .supportsInstancedArrays() is now .extensions.get( \'ANGLE_instanced_arrays\' ).' );
+			return this.extensions.get( 'ANGLE_instanced_arrays' );
+
+		},
+		enableScissorTest: function ( boolean ) {
+
+			console.warn( 'THREE.WebGLRenderer: .enableScissorTest() is now .setScissorTest().' );
+			this.setScissorTest( boolean );
+
+		},
+		initMaterial: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
+
+		},
+		addPrePlugin: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
+
+		},
+		addPostPlugin: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
+
+		},
+		updateShadowMap: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
+
+		},
+		setFaceCulling: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .setFaceCulling() has been removed.' );
+
+		},
+		allocTextureUnit: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .allocTextureUnit() has been removed.' );
+
+		},
+		setTexture: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .setTexture() has been removed.' );
+
+		},
+		setTexture2D: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .setTexture2D() has been removed.' );
+
+		},
+		setTextureCube: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .setTextureCube() has been removed.' );
+
+		},
+		getActiveMipMapLevel: function () {
+
+			console.warn( 'THREE.WebGLRenderer: .getActiveMipMapLevel() is now .getActiveMipmapLevel().' );
+			return this.getActiveMipmapLevel();
+
+		}
+
+	} );
+
+	Object.defineProperties( WebGLRenderer.prototype, {
+
+		shadowMapEnabled: {
+			get: function () {
+
+				return this.shadowMap.enabled;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' );
+				this.shadowMap.enabled = value;
+
+			}
+		},
+		shadowMapType: {
+			get: function () {
+
+				return this.shadowMap.type;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' );
+				this.shadowMap.type = value;
+
+			}
+		},
+		shadowMapCullFace: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' );
+				return undefined;
+
+			},
+			set: function ( /* value */ ) {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' );
+
+			}
+		},
+		context: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .context has been removed. Use .getContext() instead.' );
+				return this.getContext();
+
+			}
+		},
+		vr: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .vr has been renamed to .xr' );
+				return this.xr;
+
+			}
+		},
+		gammaInput: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' );
+				return false;
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' );
+
+			}
+		},
+		gammaOutput: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' );
+				return false;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' );
+				this.outputEncoding = ( value === true ) ? sRGBEncoding : LinearEncoding;
+
+			}
+		}
+
+	} );
+
+	Object.defineProperties( WebGLShadowMap.prototype, {
+
+		cullFace: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' );
+				return undefined;
+
+			},
+			set: function ( /* cullFace */ ) {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' );
+
+			}
+		},
+		renderReverseSided: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' );
+				return undefined;
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' );
+
+			}
+		},
+		renderSingleSided: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' );
+				return undefined;
+
+			},
+			set: function () {
+
+				console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' );
+
+			}
+		}
+
+	} );
+
+	function WebGLRenderTargetCube( width, height, options ) {
+
+		console.warn( 'THREE.WebGLRenderTargetCube( width, height, options ) is now WebGLCubeRenderTarget( size, options ).' );
+		return new WebGLCubeRenderTarget( width, options );
+
+	}
+
+	//
+
+	Object.defineProperties( WebGLRenderTarget.prototype, {
+
+		wrapS: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' );
+				return this.texture.wrapS;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' );
+				this.texture.wrapS = value;
+
+			}
+		},
+		wrapT: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' );
+				return this.texture.wrapT;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' );
+				this.texture.wrapT = value;
+
+			}
+		},
+		magFilter: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' );
+				return this.texture.magFilter;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' );
+				this.texture.magFilter = value;
+
+			}
+		},
+		minFilter: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' );
+				return this.texture.minFilter;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' );
+				this.texture.minFilter = value;
+
+			}
+		},
+		anisotropy: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' );
+				return this.texture.anisotropy;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' );
+				this.texture.anisotropy = value;
+
+			}
+		},
+		offset: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' );
+				return this.texture.offset;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' );
+				this.texture.offset = value;
+
+			}
+		},
+		repeat: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' );
+				return this.texture.repeat;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' );
+				this.texture.repeat = value;
+
+			}
+		},
+		format: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' );
+				return this.texture.format;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' );
+				this.texture.format = value;
+
+			}
+		},
+		type: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' );
+				return this.texture.type;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' );
+				this.texture.type = value;
+
+			}
+		},
+		generateMipmaps: {
+			get: function () {
+
+				console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' );
+				return this.texture.generateMipmaps;
+
+			},
+			set: function ( value ) {
+
+				console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' );
+				this.texture.generateMipmaps = value;
+
+			}
+		}
+
+	} );
+
+	//
+
+	Object.defineProperties( Audio.prototype, {
+
+		load: {
+			value: function ( file ) {
+
+				console.warn( 'THREE.Audio: .load has been deprecated. Use THREE.AudioLoader instead.' );
+				var scope = this;
+				var audioLoader = new AudioLoader();
+				audioLoader.load( file, function ( buffer ) {
+
+					scope.setBuffer( buffer );
+
+				} );
+				return this;
+
+			}
+		},
+		startTime: {
+			set: function () {
+
+				console.warn( 'THREE.Audio: .startTime is now .play( delay ).' );
+
+			}
+		}
+
+	} );
+
+	AudioAnalyser.prototype.getData = function () {
+
+		console.warn( 'THREE.AudioAnalyser: .getData() is now .getFrequencyData().' );
+		return this.getFrequencyData();
+
+	};
+
+	//
+
+	CubeCamera.prototype.updateCubeMap = function ( renderer, scene ) {
+
+		console.warn( 'THREE.CubeCamera: .updateCubeMap() is now .update().' );
+		return this.update( renderer, scene );
+
+	};
+
+	//
+
+	var GeometryUtils = {
+
+		merge: function ( geometry1, geometry2, materialIndexOffset ) {
+
+			console.warn( 'THREE.GeometryUtils: .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' );
+			var matrix;
+
+			if ( geometry2.isMesh ) {
+
+				geometry2.matrixAutoUpdate && geometry2.updateMatrix();
+
+				matrix = geometry2.matrix;
+				geometry2 = geometry2.geometry;
+
+			}
+
+			geometry1.merge( geometry2, matrix, materialIndexOffset );
+
+		},
+
+		center: function ( geometry ) {
+
+			console.warn( 'THREE.GeometryUtils: .center() has been moved to Geometry. Use geometry.center() instead.' );
+			return geometry.center();
+
+		}
+
+	};
+
+	ImageUtils.crossOrigin = undefined;
+
+	ImageUtils.loadTexture = function ( url, mapping, onLoad, onError ) {
+
+		console.warn( 'THREE.ImageUtils.loadTexture has been deprecated. Use THREE.TextureLoader() instead.' );
+
+		var loader = new TextureLoader();
+		loader.setCrossOrigin( this.crossOrigin );
+
+		var texture = loader.load( url, onLoad, undefined, onError );
+
+		if ( mapping ) { texture.mapping = mapping; }
+
+		return texture;
+
+	};
+
+	ImageUtils.loadTextureCube = function ( urls, mapping, onLoad, onError ) {
+
+		console.warn( 'THREE.ImageUtils.loadTextureCube has been deprecated. Use THREE.CubeTextureLoader() instead.' );
+
+		var loader = new CubeTextureLoader();
+		loader.setCrossOrigin( this.crossOrigin );
+
+		var texture = loader.load( urls, onLoad, undefined, onError );
+
+		if ( mapping ) { texture.mapping = mapping; }
+
+		return texture;
+
+	};
+
+	ImageUtils.loadCompressedTexture = function () {
+
+		console.error( 'THREE.ImageUtils.loadCompressedTexture has been removed. Use THREE.DDSLoader instead.' );
+
+	};
+
+	ImageUtils.loadCompressedTextureCube = function () {
+
+		console.error( 'THREE.ImageUtils.loadCompressedTextureCube has been removed. Use THREE.DDSLoader instead.' );
+
+	};
+
+	//
+
+	function CanvasRenderer() {
+
+		console.error( 'THREE.CanvasRenderer has been removed' );
+
+	}
+
+	//
+
+	function JSONLoader() {
+
+		console.error( 'THREE.JSONLoader has been removed.' );
+
+	}
+
+	//
+
+	var SceneUtils = {
+
+		createMultiMaterialObject: function ( /* geometry, materials */ ) {
+
+			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+
+		},
+
+		detach: function ( /* child, parent, scene */ ) {
+
+			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+
+		},
+
+		attach: function ( /* child, scene, parent */ ) {
+
+			console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' );
+
+		}
+
+	};
+
+	//
+
+	function LensFlare() {
+
+		console.error( 'THREE.LensFlare has been moved to /examples/jsm/objects/Lensflare.js' );
+
+	}
+
+	if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
+
+		/* eslint-disable no-undef */
+		__THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: {
+			revision: REVISION,
+		} } ) );
+		/* eslint-enable no-undef */
+
+	}
+
+	exports.ACESFilmicToneMapping = ACESFilmicToneMapping;
+	exports.AddEquation = AddEquation;
+	exports.AddOperation = AddOperation;
+	exports.AdditiveAnimationBlendMode = AdditiveAnimationBlendMode;
+	exports.AdditiveBlending = AdditiveBlending;
+	exports.AlphaFormat = AlphaFormat;
+	exports.AlwaysDepth = AlwaysDepth;
+	exports.AlwaysStencilFunc = AlwaysStencilFunc;
+	exports.AmbientLight = AmbientLight;
+	exports.AmbientLightProbe = AmbientLightProbe;
+	exports.AnimationClip = AnimationClip;
+	exports.AnimationLoader = AnimationLoader;
+	exports.AnimationMixer = AnimationMixer;
+	exports.AnimationObjectGroup = AnimationObjectGroup;
+	exports.AnimationUtils = AnimationUtils;
+	exports.ArcCurve = ArcCurve;
+	exports.ArrayCamera = ArrayCamera;
+	exports.ArrowHelper = ArrowHelper;
+	exports.Audio = Audio;
+	exports.AudioAnalyser = AudioAnalyser;
+	exports.AudioContext = AudioContext;
+	exports.AudioListener = AudioListener;
+	exports.AudioLoader = AudioLoader;
+	exports.AxesHelper = AxesHelper;
+	exports.AxisHelper = AxisHelper;
+	exports.BackSide = BackSide;
+	exports.BasicDepthPacking = BasicDepthPacking;
+	exports.BasicShadowMap = BasicShadowMap;
+	exports.BinaryTextureLoader = BinaryTextureLoader;
+	exports.Bone = Bone;
+	exports.BooleanKeyframeTrack = BooleanKeyframeTrack;
+	exports.BoundingBoxHelper = BoundingBoxHelper;
+	exports.Box2 = Box2;
+	exports.Box3 = Box3;
+	exports.Box3Helper = Box3Helper;
+	exports.BoxBufferGeometry = BoxBufferGeometry;
+	exports.BoxGeometry = BoxGeometry;
+	exports.BoxHelper = BoxHelper;
+	exports.BufferAttribute = BufferAttribute;
+	exports.BufferGeometry = BufferGeometry;
+	exports.BufferGeometryLoader = BufferGeometryLoader;
+	exports.ByteType = ByteType;
+	exports.Cache = Cache;
+	exports.Camera = Camera;
+	exports.CameraHelper = CameraHelper;
+	exports.CanvasRenderer = CanvasRenderer;
+	exports.CanvasTexture = CanvasTexture;
+	exports.CatmullRomCurve3 = CatmullRomCurve3;
+	exports.CineonToneMapping = CineonToneMapping;
+	exports.CircleBufferGeometry = CircleBufferGeometry;
+	exports.CircleGeometry = CircleGeometry;
+	exports.ClampToEdgeWrapping = ClampToEdgeWrapping;
+	exports.Clock = Clock;
+	exports.ClosedSplineCurve3 = ClosedSplineCurve3;
+	exports.Color = Color;
+	exports.ColorKeyframeTrack = ColorKeyframeTrack;
+	exports.CompressedTexture = CompressedTexture;
+	exports.CompressedTextureLoader = CompressedTextureLoader;
+	exports.ConeBufferGeometry = ConeBufferGeometry;
+	exports.ConeGeometry = ConeGeometry;
+	exports.CubeCamera = CubeCamera;
+	exports.CubeGeometry = BoxGeometry;
+	exports.CubeReflectionMapping = CubeReflectionMapping;
+	exports.CubeRefractionMapping = CubeRefractionMapping;
+	exports.CubeTexture = CubeTexture;
+	exports.CubeTextureLoader = CubeTextureLoader;
+	exports.CubeUVReflectionMapping = CubeUVReflectionMapping;
+	exports.CubeUVRefractionMapping = CubeUVRefractionMapping;
+	exports.CubicBezierCurve = CubicBezierCurve;
+	exports.CubicBezierCurve3 = CubicBezierCurve3;
+	exports.CubicInterpolant = CubicInterpolant;
+	exports.CullFaceBack = CullFaceBack;
+	exports.CullFaceFront = CullFaceFront;
+	exports.CullFaceFrontBack = CullFaceFrontBack;
+	exports.CullFaceNone = CullFaceNone;
+	exports.Curve = Curve;
+	exports.CurvePath = CurvePath;
+	exports.CustomBlending = CustomBlending;
+	exports.CylinderBufferGeometry = CylinderBufferGeometry;
+	exports.CylinderGeometry = CylinderGeometry;
+	exports.Cylindrical = Cylindrical;
+	exports.DataTexture = DataTexture;
+	exports.DataTexture2DArray = DataTexture2DArray;
+	exports.DataTexture3D = DataTexture3D;
+	exports.DataTextureLoader = DataTextureLoader;
+	exports.DecrementStencilOp = DecrementStencilOp;
+	exports.DecrementWrapStencilOp = DecrementWrapStencilOp;
+	exports.DefaultLoadingManager = DefaultLoadingManager;
+	exports.DepthFormat = DepthFormat;
+	exports.DepthStencilFormat = DepthStencilFormat;
+	exports.DepthTexture = DepthTexture;
+	exports.DirectionalLight = DirectionalLight;
+	exports.DirectionalLightHelper = DirectionalLightHelper;
+	exports.DirectionalLightShadow = DirectionalLightShadow;
+	exports.DiscreteInterpolant = DiscreteInterpolant;
+	exports.DodecahedronBufferGeometry = DodecahedronBufferGeometry;
+	exports.DodecahedronGeometry = DodecahedronGeometry;
+	exports.DoubleSide = DoubleSide;
+	exports.DstAlphaFactor = DstAlphaFactor;
+	exports.DstColorFactor = DstColorFactor;
+	exports.DynamicBufferAttribute = DynamicBufferAttribute;
+	exports.DynamicCopyUsage = DynamicCopyUsage;
+	exports.DynamicDrawUsage = DynamicDrawUsage;
+	exports.DynamicReadUsage = DynamicReadUsage;
+	exports.EdgesGeometry = EdgesGeometry;
+	exports.EdgesHelper = EdgesHelper;
+	exports.EllipseCurve = EllipseCurve;
+	exports.EqualDepth = EqualDepth;
+	exports.EqualStencilFunc = EqualStencilFunc;
+	exports.EquirectangularReflectionMapping = EquirectangularReflectionMapping;
+	exports.EquirectangularRefractionMapping = EquirectangularRefractionMapping;
+	exports.Euler = Euler;
+	exports.EventDispatcher = EventDispatcher;
+	exports.ExtrudeBufferGeometry = ExtrudeBufferGeometry;
+	exports.ExtrudeGeometry = ExtrudeGeometry;
+	exports.Face3 = Face3;
+	exports.Face4 = Face4;
+	exports.FaceColors = FaceColors;
+	exports.FileLoader = FileLoader;
+	exports.FlatShading = FlatShading;
+	exports.Float32Attribute = Float32Attribute;
+	exports.Float32BufferAttribute = Float32BufferAttribute;
+	exports.Float64Attribute = Float64Attribute;
+	exports.Float64BufferAttribute = Float64BufferAttribute;
+	exports.FloatType = FloatType;
+	exports.Fog = Fog;
+	exports.FogExp2 = FogExp2;
+	exports.Font = Font;
+	exports.FontLoader = FontLoader;
+	exports.FrontFaceDirectionCCW = FrontFaceDirectionCCW;
+	exports.FrontFaceDirectionCW = FrontFaceDirectionCW;
+	exports.FrontSide = FrontSide;
+	exports.Frustum = Frustum;
+	exports.GammaEncoding = GammaEncoding;
+	exports.Geometry = Geometry;
+	exports.GeometryUtils = GeometryUtils;
+	exports.GreaterDepth = GreaterDepth;
+	exports.GreaterEqualDepth = GreaterEqualDepth;
+	exports.GreaterEqualStencilFunc = GreaterEqualStencilFunc;
+	exports.GreaterStencilFunc = GreaterStencilFunc;
+	exports.GridHelper = GridHelper;
+	exports.Group = Group;
+	exports.HalfFloatType = HalfFloatType;
+	exports.HemisphereLight = HemisphereLight;
+	exports.HemisphereLightHelper = HemisphereLightHelper;
+	exports.HemisphereLightProbe = HemisphereLightProbe;
+	exports.IcosahedronBufferGeometry = IcosahedronBufferGeometry;
+	exports.IcosahedronGeometry = IcosahedronGeometry;
+	exports.ImageBitmapLoader = ImageBitmapLoader;
+	exports.ImageLoader = ImageLoader;
+	exports.ImageUtils = ImageUtils;
+	exports.ImmediateRenderObject = ImmediateRenderObject;
+	exports.IncrementStencilOp = IncrementStencilOp;
+	exports.IncrementWrapStencilOp = IncrementWrapStencilOp;
+	exports.InstancedBufferAttribute = InstancedBufferAttribute;
+	exports.InstancedBufferGeometry = InstancedBufferGeometry;
+	exports.InstancedInterleavedBuffer = InstancedInterleavedBuffer;
+	exports.InstancedMesh = InstancedMesh;
+	exports.Int16Attribute = Int16Attribute;
+	exports.Int16BufferAttribute = Int16BufferAttribute;
+	exports.Int32Attribute = Int32Attribute;
+	exports.Int32BufferAttribute = Int32BufferAttribute;
+	exports.Int8Attribute = Int8Attribute;
+	exports.Int8BufferAttribute = Int8BufferAttribute;
+	exports.IntType = IntType;
+	exports.InterleavedBuffer = InterleavedBuffer;
+	exports.InterleavedBufferAttribute = InterleavedBufferAttribute;
+	exports.Interpolant = Interpolant;
+	exports.InterpolateDiscrete = InterpolateDiscrete;
+	exports.InterpolateLinear = InterpolateLinear;
+	exports.InterpolateSmooth = InterpolateSmooth;
+	exports.InvertStencilOp = InvertStencilOp;
+	exports.JSONLoader = JSONLoader;
+	exports.KeepStencilOp = KeepStencilOp;
+	exports.KeyframeTrack = KeyframeTrack;
+	exports.LOD = LOD;
+	exports.LatheBufferGeometry = LatheBufferGeometry;
+	exports.LatheGeometry = LatheGeometry;
+	exports.Layers = Layers;
+	exports.LensFlare = LensFlare;
+	exports.LessDepth = LessDepth;
+	exports.LessEqualDepth = LessEqualDepth;
+	exports.LessEqualStencilFunc = LessEqualStencilFunc;
+	exports.LessStencilFunc = LessStencilFunc;
+	exports.Light = Light;
+	exports.LightProbe = LightProbe;
+	exports.LightShadow = LightShadow;
+	exports.Line = Line;
+	exports.Line3 = Line3;
+	exports.LineBasicMaterial = LineBasicMaterial;
+	exports.LineCurve = LineCurve;
+	exports.LineCurve3 = LineCurve3;
+	exports.LineDashedMaterial = LineDashedMaterial;
+	exports.LineLoop = LineLoop;
+	exports.LinePieces = LinePieces;
+	exports.LineSegments = LineSegments;
+	exports.LineStrip = LineStrip;
+	exports.LinearEncoding = LinearEncoding;
+	exports.LinearFilter = LinearFilter;
+	exports.LinearInterpolant = LinearInterpolant;
+	exports.LinearMipMapLinearFilter = LinearMipMapLinearFilter;
+	exports.LinearMipMapNearestFilter = LinearMipMapNearestFilter;
+	exports.LinearMipmapLinearFilter = LinearMipmapLinearFilter;
+	exports.LinearMipmapNearestFilter = LinearMipmapNearestFilter;
+	exports.LinearToneMapping = LinearToneMapping;
+	exports.Loader = Loader;
+	exports.LoaderUtils = LoaderUtils;
+	exports.LoadingManager = LoadingManager;
+	exports.LogLuvEncoding = LogLuvEncoding;
+	exports.LoopOnce = LoopOnce;
+	exports.LoopPingPong = LoopPingPong;
+	exports.LoopRepeat = LoopRepeat;
+	exports.LuminanceAlphaFormat = LuminanceAlphaFormat;
+	exports.LuminanceFormat = LuminanceFormat;
+	exports.MOUSE = MOUSE;
+	exports.Material = Material;
+	exports.MaterialLoader = MaterialLoader;
+	exports.Math = MathUtils;
+	exports.MathUtils = MathUtils;
+	exports.Matrix3 = Matrix3;
+	exports.Matrix4 = Matrix4;
+	exports.MaxEquation = MaxEquation;
+	exports.Mesh = Mesh;
+	exports.MeshBasicMaterial = MeshBasicMaterial;
+	exports.MeshDepthMaterial = MeshDepthMaterial;
+	exports.MeshDistanceMaterial = MeshDistanceMaterial;
+	exports.MeshFaceMaterial = MeshFaceMaterial;
+	exports.MeshLambertMaterial = MeshLambertMaterial;
+	exports.MeshMatcapMaterial = MeshMatcapMaterial;
+	exports.MeshNormalMaterial = MeshNormalMaterial;
+	exports.MeshPhongMaterial = MeshPhongMaterial;
+	exports.MeshPhysicalMaterial = MeshPhysicalMaterial;
+	exports.MeshStandardMaterial = MeshStandardMaterial;
+	exports.MeshToonMaterial = MeshToonMaterial;
+	exports.MinEquation = MinEquation;
+	exports.MirroredRepeatWrapping = MirroredRepeatWrapping;
+	exports.MixOperation = MixOperation;
+	exports.MultiMaterial = MultiMaterial;
+	exports.MultiplyBlending = MultiplyBlending;
+	exports.MultiplyOperation = MultiplyOperation;
+	exports.NearestFilter = NearestFilter;
+	exports.NearestMipMapLinearFilter = NearestMipMapLinearFilter;
+	exports.NearestMipMapNearestFilter = NearestMipMapNearestFilter;
+	exports.NearestMipmapLinearFilter = NearestMipmapLinearFilter;
+	exports.NearestMipmapNearestFilter = NearestMipmapNearestFilter;
+	exports.NeverDepth = NeverDepth;
+	exports.NeverStencilFunc = NeverStencilFunc;
+	exports.NoBlending = NoBlending;
+	exports.NoColors = NoColors;
+	exports.NoToneMapping = NoToneMapping;
+	exports.NormalAnimationBlendMode = NormalAnimationBlendMode;
+	exports.NormalBlending = NormalBlending;
+	exports.NotEqualDepth = NotEqualDepth;
+	exports.NotEqualStencilFunc = NotEqualStencilFunc;
+	exports.NumberKeyframeTrack = NumberKeyframeTrack;
+	exports.Object3D = Object3D;
+	exports.ObjectLoader = ObjectLoader;
+	exports.ObjectSpaceNormalMap = ObjectSpaceNormalMap;
+	exports.OctahedronBufferGeometry = OctahedronBufferGeometry;
+	exports.OctahedronGeometry = OctahedronGeometry;
+	exports.OneFactor = OneFactor;
+	exports.OneMinusDstAlphaFactor = OneMinusDstAlphaFactor;
+	exports.OneMinusDstColorFactor = OneMinusDstColorFactor;
+	exports.OneMinusSrcAlphaFactor = OneMinusSrcAlphaFactor;
+	exports.OneMinusSrcColorFactor = OneMinusSrcColorFactor;
+	exports.OrthographicCamera = OrthographicCamera;
+	exports.PCFShadowMap = PCFShadowMap;
+	exports.PCFSoftShadowMap = PCFSoftShadowMap;
+	exports.PMREMGenerator = PMREMGenerator;
+	exports.ParametricBufferGeometry = ParametricBufferGeometry;
+	exports.ParametricGeometry = ParametricGeometry;
+	exports.Particle = Particle;
+	exports.ParticleBasicMaterial = ParticleBasicMaterial;
+	exports.ParticleSystem = ParticleSystem;
+	exports.ParticleSystemMaterial = ParticleSystemMaterial;
+	exports.Path = Path;
+	exports.PerspectiveCamera = PerspectiveCamera;
+	exports.Plane = Plane;
+	exports.PlaneBufferGeometry = PlaneBufferGeometry;
+	exports.PlaneGeometry = PlaneGeometry;
+	exports.PlaneHelper = PlaneHelper;
+	exports.PointCloud = PointCloud;
+	exports.PointCloudMaterial = PointCloudMaterial;
+	exports.PointLight = PointLight;
+	exports.PointLightHelper = PointLightHelper;
+	exports.Points = Points;
+	exports.PointsMaterial = PointsMaterial;
+	exports.PolarGridHelper = PolarGridHelper;
+	exports.PolyhedronBufferGeometry = PolyhedronBufferGeometry;
+	exports.PolyhedronGeometry = PolyhedronGeometry;
+	exports.PositionalAudio = PositionalAudio;
+	exports.PropertyBinding = PropertyBinding;
+	exports.PropertyMixer = PropertyMixer;
+	exports.QuadraticBezierCurve = QuadraticBezierCurve;
+	exports.QuadraticBezierCurve3 = QuadraticBezierCurve3;
+	exports.Quaternion = Quaternion;
+	exports.QuaternionKeyframeTrack = QuaternionKeyframeTrack;
+	exports.QuaternionLinearInterpolant = QuaternionLinearInterpolant;
+	exports.REVISION = REVISION;
+	exports.RGBADepthPacking = RGBADepthPacking;
+	exports.RGBAFormat = RGBAFormat;
+	exports.RGBAIntegerFormat = RGBAIntegerFormat;
+	exports.RGBA_ASTC_10x10_Format = RGBA_ASTC_10x10_Format;
+	exports.RGBA_ASTC_10x5_Format = RGBA_ASTC_10x5_Format;
+	exports.RGBA_ASTC_10x6_Format = RGBA_ASTC_10x6_Format;
+	exports.RGBA_ASTC_10x8_Format = RGBA_ASTC_10x8_Format;
+	exports.RGBA_ASTC_12x10_Format = RGBA_ASTC_12x10_Format;
+	exports.RGBA_ASTC_12x12_Format = RGBA_ASTC_12x12_Format;
+	exports.RGBA_ASTC_4x4_Format = RGBA_ASTC_4x4_Format;
+	exports.RGBA_ASTC_5x4_Format = RGBA_ASTC_5x4_Format;
+	exports.RGBA_ASTC_5x5_Format = RGBA_ASTC_5x5_Format;
+	exports.RGBA_ASTC_6x5_Format = RGBA_ASTC_6x5_Format;
+	exports.RGBA_ASTC_6x6_Format = RGBA_ASTC_6x6_Format;
+	exports.RGBA_ASTC_8x5_Format = RGBA_ASTC_8x5_Format;
+	exports.RGBA_ASTC_8x6_Format = RGBA_ASTC_8x6_Format;
+	exports.RGBA_ASTC_8x8_Format = RGBA_ASTC_8x8_Format;
+	exports.RGBA_BPTC_Format = RGBA_BPTC_Format;
+	exports.RGBA_ETC2_EAC_Format = RGBA_ETC2_EAC_Format;
+	exports.RGBA_PVRTC_2BPPV1_Format = RGBA_PVRTC_2BPPV1_Format;
+	exports.RGBA_PVRTC_4BPPV1_Format = RGBA_PVRTC_4BPPV1_Format;
+	exports.RGBA_S3TC_DXT1_Format = RGBA_S3TC_DXT1_Format;
+	exports.RGBA_S3TC_DXT3_Format = RGBA_S3TC_DXT3_Format;
+	exports.RGBA_S3TC_DXT5_Format = RGBA_S3TC_DXT5_Format;
+	exports.RGBDEncoding = RGBDEncoding;
+	exports.RGBEEncoding = RGBEEncoding;
+	exports.RGBEFormat = RGBEFormat;
+	exports.RGBFormat = RGBFormat;
+	exports.RGBIntegerFormat = RGBIntegerFormat;
+	exports.RGBM16Encoding = RGBM16Encoding;
+	exports.RGBM7Encoding = RGBM7Encoding;
+	exports.RGB_ETC1_Format = RGB_ETC1_Format;
+	exports.RGB_ETC2_Format = RGB_ETC2_Format;
+	exports.RGB_PVRTC_2BPPV1_Format = RGB_PVRTC_2BPPV1_Format;
+	exports.RGB_PVRTC_4BPPV1_Format = RGB_PVRTC_4BPPV1_Format;
+	exports.RGB_S3TC_DXT1_Format = RGB_S3TC_DXT1_Format;
+	exports.RGFormat = RGFormat;
+	exports.RGIntegerFormat = RGIntegerFormat;
+	exports.RawShaderMaterial = RawShaderMaterial;
+	exports.Ray = Ray;
+	exports.Raycaster = Raycaster;
+	exports.RectAreaLight = RectAreaLight;
+	exports.RedFormat = RedFormat;
+	exports.RedIntegerFormat = RedIntegerFormat;
+	exports.ReinhardToneMapping = ReinhardToneMapping;
+	exports.RepeatWrapping = RepeatWrapping;
+	exports.ReplaceStencilOp = ReplaceStencilOp;
+	exports.ReverseSubtractEquation = ReverseSubtractEquation;
+	exports.RingBufferGeometry = RingBufferGeometry;
+	exports.RingGeometry = RingGeometry;
+	exports.SRGB8_ALPHA8_ASTC_10x10_Format = SRGB8_ALPHA8_ASTC_10x10_Format;
+	exports.SRGB8_ALPHA8_ASTC_10x5_Format = SRGB8_ALPHA8_ASTC_10x5_Format;
+	exports.SRGB8_ALPHA8_ASTC_10x6_Format = SRGB8_ALPHA8_ASTC_10x6_Format;
+	exports.SRGB8_ALPHA8_ASTC_10x8_Format = SRGB8_ALPHA8_ASTC_10x8_Format;
+	exports.SRGB8_ALPHA8_ASTC_12x10_Format = SRGB8_ALPHA8_ASTC_12x10_Format;
+	exports.SRGB8_ALPHA8_ASTC_12x12_Format = SRGB8_ALPHA8_ASTC_12x12_Format;
+	exports.SRGB8_ALPHA8_ASTC_4x4_Format = SRGB8_ALPHA8_ASTC_4x4_Format;
+	exports.SRGB8_ALPHA8_ASTC_5x4_Format = SRGB8_ALPHA8_ASTC_5x4_Format;
+	exports.SRGB8_ALPHA8_ASTC_5x5_Format = SRGB8_ALPHA8_ASTC_5x5_Format;
+	exports.SRGB8_ALPHA8_ASTC_6x5_Format = SRGB8_ALPHA8_ASTC_6x5_Format;
+	exports.SRGB8_ALPHA8_ASTC_6x6_Format = SRGB8_ALPHA8_ASTC_6x6_Format;
+	exports.SRGB8_ALPHA8_ASTC_8x5_Format = SRGB8_ALPHA8_ASTC_8x5_Format;
+	exports.SRGB8_ALPHA8_ASTC_8x6_Format = SRGB8_ALPHA8_ASTC_8x6_Format;
+	exports.SRGB8_ALPHA8_ASTC_8x8_Format = SRGB8_ALPHA8_ASTC_8x8_Format;
+	exports.Scene = Scene;
+	exports.SceneUtils = SceneUtils;
+	exports.ShaderChunk = ShaderChunk;
+	exports.ShaderLib = ShaderLib;
+	exports.ShaderMaterial = ShaderMaterial;
+	exports.ShadowMaterial = ShadowMaterial;
+	exports.Shape = Shape;
+	exports.ShapeBufferGeometry = ShapeBufferGeometry;
+	exports.ShapeGeometry = ShapeGeometry;
+	exports.ShapePath = ShapePath;
+	exports.ShapeUtils = ShapeUtils;
+	exports.ShortType = ShortType;
+	exports.Skeleton = Skeleton;
+	exports.SkeletonHelper = SkeletonHelper;
+	exports.SkinnedMesh = SkinnedMesh;
+	exports.SmoothShading = SmoothShading;
+	exports.Sphere = Sphere;
+	exports.SphereBufferGeometry = SphereBufferGeometry;
+	exports.SphereGeometry = SphereGeometry;
+	exports.Spherical = Spherical;
+	exports.SphericalHarmonics3 = SphericalHarmonics3;
+	exports.SphericalReflectionMapping = SphericalReflectionMapping;
+	exports.Spline = Spline;
+	exports.SplineCurve = SplineCurve;
+	exports.SplineCurve3 = SplineCurve3;
+	exports.SpotLight = SpotLight;
+	exports.SpotLightHelper = SpotLightHelper;
+	exports.SpotLightShadow = SpotLightShadow;
+	exports.Sprite = Sprite;
+	exports.SpriteMaterial = SpriteMaterial;
+	exports.SrcAlphaFactor = SrcAlphaFactor;
+	exports.SrcAlphaSaturateFactor = SrcAlphaSaturateFactor;
+	exports.SrcColorFactor = SrcColorFactor;
+	exports.StaticCopyUsage = StaticCopyUsage;
+	exports.StaticDrawUsage = StaticDrawUsage;
+	exports.StaticReadUsage = StaticReadUsage;
+	exports.StereoCamera = StereoCamera;
+	exports.StreamCopyUsage = StreamCopyUsage;
+	exports.StreamDrawUsage = StreamDrawUsage;
+	exports.StreamReadUsage = StreamReadUsage;
+	exports.StringKeyframeTrack = StringKeyframeTrack;
+	exports.SubtractEquation = SubtractEquation;
+	exports.SubtractiveBlending = SubtractiveBlending;
+	exports.TOUCH = TOUCH;
+	exports.TangentSpaceNormalMap = TangentSpaceNormalMap;
+	exports.TetrahedronBufferGeometry = TetrahedronBufferGeometry;
+	exports.TetrahedronGeometry = TetrahedronGeometry;
+	exports.TextBufferGeometry = TextBufferGeometry;
+	exports.TextGeometry = TextGeometry;
+	exports.Texture = Texture;
+	exports.TextureLoader = TextureLoader;
+	exports.TorusBufferGeometry = TorusBufferGeometry;
+	exports.TorusGeometry = TorusGeometry;
+	exports.TorusKnotBufferGeometry = TorusKnotBufferGeometry;
+	exports.TorusKnotGeometry = TorusKnotGeometry;
+	exports.Triangle = Triangle;
+	exports.TriangleFanDrawMode = TriangleFanDrawMode;
+	exports.TriangleStripDrawMode = TriangleStripDrawMode;
+	exports.TrianglesDrawMode = TrianglesDrawMode;
+	exports.TubeBufferGeometry = TubeBufferGeometry;
+	exports.TubeGeometry = TubeGeometry;
+	exports.UVMapping = UVMapping;
+	exports.Uint16Attribute = Uint16Attribute;
+	exports.Uint16BufferAttribute = Uint16BufferAttribute;
+	exports.Uint32Attribute = Uint32Attribute;
+	exports.Uint32BufferAttribute = Uint32BufferAttribute;
+	exports.Uint8Attribute = Uint8Attribute;
+	exports.Uint8BufferAttribute = Uint8BufferAttribute;
+	exports.Uint8ClampedAttribute = Uint8ClampedAttribute;
+	exports.Uint8ClampedBufferAttribute = Uint8ClampedBufferAttribute;
+	exports.Uncharted2ToneMapping = Uncharted2ToneMapping;
+	exports.Uniform = Uniform;
+	exports.UniformsLib = UniformsLib;
+	exports.UniformsUtils = UniformsUtils;
+	exports.UnsignedByteType = UnsignedByteType;
+	exports.UnsignedInt248Type = UnsignedInt248Type;
+	exports.UnsignedIntType = UnsignedIntType;
+	exports.UnsignedShort4444Type = UnsignedShort4444Type;
+	exports.UnsignedShort5551Type = UnsignedShort5551Type;
+	exports.UnsignedShort565Type = UnsignedShort565Type;
+	exports.UnsignedShortType = UnsignedShortType;
+	exports.VSMShadowMap = VSMShadowMap;
+	exports.Vector2 = Vector2;
+	exports.Vector3 = Vector3;
+	exports.Vector4 = Vector4;
+	exports.VectorKeyframeTrack = VectorKeyframeTrack;
+	exports.Vertex = Vertex;
+	exports.VertexColors = VertexColors;
+	exports.VideoTexture = VideoTexture;
+	exports.WebGLCubeRenderTarget = WebGLCubeRenderTarget;
+	exports.WebGLMultisampleRenderTarget = WebGLMultisampleRenderTarget;
+	exports.WebGLRenderTarget = WebGLRenderTarget;
+	exports.WebGLRenderTargetCube = WebGLRenderTargetCube;
+	exports.WebGLRenderer = WebGLRenderer;
+	exports.WebGLUtils = WebGLUtils;
+	exports.WireframeGeometry = WireframeGeometry;
+	exports.WireframeHelper = WireframeHelper;
+	exports.WrapAroundEnding = WrapAroundEnding;
+	exports.XHRLoader = XHRLoader;
+	exports.ZeroCurvatureEnding = ZeroCurvatureEnding;
+	exports.ZeroFactor = ZeroFactor;
+	exports.ZeroSlopeEnding = ZeroSlopeEnding;
+	exports.ZeroStencilOp = ZeroStencilOp;
+	exports.sRGBEncoding = sRGBEncoding;
+
+	Object.defineProperty(exports, '__esModule', { value: true });
+
+})));
+
+},{}],77:[function(require,module,exports){
+(function (setImmediate,clearImmediate){
+var nextTick = require('process/browser.js').nextTick;
+var apply = Function.prototype.apply;
+var slice = Array.prototype.slice;
+var immediateIds = {};
+var nextImmediateId = 0;
+
+// DOM APIs, for completeness
+
+exports.setTimeout = function() {
+  return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);
+};
+exports.setInterval = function() {
+  return new Timeout(apply.call(setInterval, window, arguments), clearInterval);
+};
+exports.clearTimeout =
+exports.clearInterval = function(timeout) { timeout.close(); };
+
+function Timeout(id, clearFn) {
+  this._id = id;
+  this._clearFn = clearFn;
+}
+Timeout.prototype.unref = Timeout.prototype.ref = function() {};
+Timeout.prototype.close = function() {
+  this._clearFn.call(window, this._id);
+};
+
+// Does not start the time, just sets up the members needed.
+exports.enroll = function(item, msecs) {
+  clearTimeout(item._idleTimeoutId);
+  item._idleTimeout = msecs;
+};
+
+exports.unenroll = function(item) {
+  clearTimeout(item._idleTimeoutId);
+  item._idleTimeout = -1;
+};
+
+exports._unrefActive = exports.active = function(item) {
+  clearTimeout(item._idleTimeoutId);
+
+  var msecs = item._idleTimeout;
+  if (msecs >= 0) {
+    item._idleTimeoutId = setTimeout(function onTimeout() {
+      if (item._onTimeout)
+        item._onTimeout();
+    }, msecs);
+  }
+};
+
+// That's not how node.js implements it but the exposed api is the same.
+exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
+  var id = nextImmediateId++;
+  var args = arguments.length < 2 ? false : slice.call(arguments, 1);
+
+  immediateIds[id] = true;
+
+  nextTick(function onNextTick() {
+    if (immediateIds[id]) {
+      // fn.call() is faster so we optimize for the common use-case
+      // @see http://jsperf.com/call-apply-segu
+      if (args) {
+        fn.apply(null, args);
+      } else {
+        fn.call(null);
+      }
+      // Prevent ids from leaking
+      exports.clearImmediate(id);
+    }
+  });
+
+  return id;
+};
+
+exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) {
+  delete immediateIds[id];
+};
+}).call(this,require("timers").setImmediate,require("timers").clearImmediate)
+},{"process/browser.js":63,"timers":77}],78:[function(require,module,exports){
+(function (global){
+
+/**
+ * Module exports.
+ */
+
+module.exports = deprecate;
+
+/**
+ * Mark that a method should not be used.
+ * Returns a modified function which warns once by default.
+ *
+ * If `localStorage.noDeprecation = true` is set, then it is a no-op.
+ *
+ * If `localStorage.throwDeprecation = true` is set, then deprecated functions
+ * will throw an Error when invoked.
+ *
+ * If `localStorage.traceDeprecation = true` is set, then deprecated functions
+ * will invoke `console.trace()` instead of `console.error()`.
+ *
+ * @param {Function} fn - the function to deprecate
+ * @param {String} msg - the string to print to the console when `fn` is invoked
+ * @returns {Function} a new "deprecated" version of `fn`
+ * @api public
+ */
+
+function deprecate (fn, msg) {
+  if (config('noDeprecation')) {
+    return fn;
+  }
+
+  var warned = false;
+  function deprecated() {
+    if (!warned) {
+      if (config('throwDeprecation')) {
+        throw new Error(msg);
+      } else if (config('traceDeprecation')) {
+        console.trace(msg);
+      } else {
+        console.warn(msg);
+      }
+      warned = true;
+    }
+    return fn.apply(this, arguments);
+  }
+
+  return deprecated;
+}
+
+/**
+ * Checks `localStorage` for boolean values for the given `name`.
+ *
+ * @param {String} name
+ * @returns {Boolean}
+ * @api private
+ */
+
+function config (name) {
+  // accessing global.localStorage can trigger a DOMException in sandboxed iframes
+  try {
+    if (!global.localStorage) return false;
+  } catch (_) {
+    return false;
+  }
+  var val = global.localStorage[name];
+  if (null == val) return false;
+  return String(val).toLowerCase() === 'true';
+}
+
+}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
+},{}],79:[function(require,module,exports){
+arguments[4][3][0].apply(exports,arguments)
+},{"dup":3}],80:[function(require,module,exports){
+arguments[4][4][0].apply(exports,arguments)
+},{"./support/isBuffer":79,"_process":63,"dup":4,"inherits":12}],81:[function(require,module,exports){
+(function (Buffer){
+const Peer = require('../../server/src/peer')
+const msgpack = require('msgpack5')();
+const rematrix = require('rematrix');
+const THREE = require('three');
+const MUXJS = require('mux.js');
+const MP4 = MUXJS.mp4.generator;
+const H264Stream = MUXJS.codecs.h264.H264Stream;
+const VIDEO_PROPERTIES = require('../../node_modules/mux.js/lib/constants/video-properties.js');
+
+let current_data = {};
+let peer;
+
+/**
+ * Validates that the user is logged in by sending the token 
+ */
+checkIfLoggedIn = async () => {
+    //     const token = window.localStorage.getItem('token')
+    //     console.log(token)
+    //     if(!token){
+    //         console.log("You need to login")
+    //         renderLogin()
+    //     }else{
+
+    //         //Check if the token is valid
+    //         const response = await fetch('http://localhost:8080/auth/validation', {
+    //             method: 'POST',
+    //             headers: {'Authorization': token}
+    //         })
+    //         console.log('RESPONSE', response)
+            
+    //         //Token is valid, show available streams
+    //         if(response.status === 200){
+    //             console.log("SUCCESS")
+                 renderThumbnails()
+
+    //         }
+    //     }
+}
+
+/**
+ * Returns a list of available streams
+ */
+getAvailableStreams = async () => {
+    try{
+        const streamsInJson = await fetch(`./streams`);
+        const streams = await streamsInJson.json();
+        console.log('AVAILABLE', streams)
+        return streams;
+    }catch(err){
+        console.log(err)
+    }
+}
+
+
+createVideoPlayer = () => {
+	const containerDiv = document.getElementById('container');
+	containerDiv.innerHTML = '';
+    /*containerDiv.innerHTML = `<h1>Stream from source ${current_data.uri}</h1><br>
+        <button onclick="renderThumbnails(); closeStream()">Go back</button>
+        <button onclick="connectToStream('${current_data.uri}')">Start Stream</button><br>
+        <button onclick="webSocketTest()">WebSocket Test</button><br>
+        <video id="ftlab-stream-video" width="640" height="360"></video>`;
+    containerDiv.innerHTML += '<br>'
+    containerDiv.innerHTML += ''*/
+    createPeer();
+	//connectToStream();
+	window.ftlstream = new FTLStream(peer, current_data.uri, containerDiv);
+}
+
+/**
+ * Creates thumbnail (image) for all available streams and adds them to div class='container'
+ */
+renderThumbnails = async () => {
+    const thumbnails = await getAvailableStreams();
+    const containerDiv = document.getElementById('container')
+    containerDiv.innerHTML = '';
+    containerDiv.innerHTML = `<button onClick="configs()">change configs</button>`
+    containerDiv.innerHTML += `<div class="ftlab-stream-thumbnails"></div>`
+    if(thumbnails.length === 0){
+        containerDiv.innerHTML = `<h3>No streams running currently</h3>`
+    }else{
+        for(var i=0; i<thumbnails.length; i++){
+            const encodedURI = encodeURIComponent(thumbnails[i])
+            current_data.uri = thumbnails[i]
+            try{
+                const someData = await fetch(`./stream/rgb?uri=${encodedURI}`)
+                if(!someData.ok){
+                    throw new Error('Image not found')
+                }
+                const myBlob = await someData.blob();
+                const objectURL = URL.createObjectURL(myBlob);
+                // containerDiv.innerHTML += createCard()
+                containerDiv.innerHTML += createCard(objectURL, i+4)
+            }catch(err){
+                console.log("Couldn't create thumbnail");
+                console.log(err) 
+            }
+        }
+    }
+}
+
+
+/** 
+ * Method to create a single thumbnail
+ */
+createCard = (url, viewers) => {
+    return `<div class='ftlab-card-component' >
+                <img src='${url}' class="thumbnail-img" alt="Hups" width="250px"></img>
+                <p>Viewers: ${viewers}</p>
+                <button onclick="createVideoPlayer()">button</button>
+            </div>`
+}
+
+
+createPeer = () => {
+	// FOR PRODUCTION
+	console.log("HOST", location.host);
+    const ws = new WebSocket("ws://" + location.host + location.pathname);
+	//const ws = new WebSocket("ws://localhost:8080")
+    ws.binaryType = "arraybuffer";
+    peer = new Peer(ws)
+}
+
+webSocketTest = () => {
+    peer.send("update_cfg", "ftl://utu.fi#reconstruction_default/0/renderer/cool_effect", "true")    
+}
+
+function FTLFrameset(id) {
+	this.id = id;
+	this.sources = {};
+}
+
+function getNALType(data) {
+	return (data.length > 4) ? data.readUInt8(4) & 0x1F : 0;
+}
+
+function isKeyFrame(data) {
+	return getNALType(data) == 7;  // SPS
+}
+
+function concatNals(sample) {
+	let length = sample.size;
+	let data = new Uint8Array(length);
+	let view = new DataView(data.buffer);
+	let dataOffset = 0;
+
+	for (var i=0; i<sample.units.length; ++i) {
+		view.setUint32(dataOffset, sample.units[i].data.byteLength);
+        dataOffset += 4;
+        data.set(sample.units[i].data, dataOffset);
+        dataOffset += sample.units[i].data.byteLength;
+	}
+
+	sample.data = data;
+}
+
+var createDefaultSample = function() {
+	return {
+	  units: [],
+	  data: null,
+	  size: 0,
+	  compositionTimeOffset: 1,
+	  duration: 0,
+	  dataOffset: 0,
+	  flags: {
+		isLeading: 0,
+		dependsOn: 1,
+		isDependedOn: 0,
+		hasRedundancy: 0,
+		degradationPriority: 0,
+		isNonSyncSample: 1
+	  },
+	  keyFrame: true
+	};
+  };
+
+function FTLStream(peer, uri, element) {
+	this.uri = uri;
+	this.peer = peer;
+
+	this.current = "";
+	this.current_fs = 0;
+	this.current_source = 0;
+	this.current_channel = 0;
+
+	this.framesets = {};
+
+	this.handlers = {};
+
+	//this.elements_ = {};
+	//this.converters_ = {};
+
+	//const element = document.getElementById('ftlab-stream-video');
+	this.outer = element;
+	this.outer.classList.add("ftl");
+	this.outer.classList.add("container");
+	this.element = document.createElement("VIDEO");
+	this.element.setAttribute("width", 640);
+	this.element.setAttribute("height", 360);
+	this.element.setAttribute("controls", true);
+	this.element.style.display = "none";
+	this.element.classList.add("ftl");
+	this.element.id = "ftl-video-element";
+	this.outer.appendChild(this.element);
+
+	//this.player = videojs('ftl-video-element');
+	//this.player.vr({projection: '360'});
+
+	if (false) {
+		this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
+	} else {
+		this.camera = new THREE.OrthographicCamera(window.innerWidth/-2, window.innerWidth/2, window.innerHeight/2, window.innerHeight/-2, 1, 4);
+	}
 	this.camera.target = new THREE.Vector3( 0, 0, 0 );
 
 	this.scene = new THREE.Scene();
@@ -60144,6 +70527,7 @@ function FTLStream(peer, uri, element) {
 	this.overlay.appendChild(this.pause_button);
 
 	this.paused = false;
+	this.active = false;
 
 	this.overlay.addEventListener('keydown', (event) => {
 		console.log(event);
@@ -60178,14 +70562,159 @@ function FTLStream(peer, uri, element) {
 		//this.setPose(pose);
 	//}
 
-    this.converter = null;
+	//this.converter = null;
+	
+	/*this.converter = new JMuxer({
+		node: 'ftl-video-element',
+		mode: 'video',
+		//fps: 1000/dts,
+		fps: 30,
+		flushingTime: 1,
+		clearBuffer: false
+	});*/
 
     let rxcount = 0;
     let ts = 0;
 	let dts = 0;
 
+	let seen_keyframe = false;
+	let init_seg = false;
+
+	this.h264 = new H264Stream();
+
+	this.doAppend = function(data) {
+		if (this.sourceBuffer.updating) {
+			this.queue.push(data);
+		} else {
+			//console.log("Direct append: ", data);
+			this.sourceBuffer.appendBuffer(data);
+		}
+	}
+
+	this.h264.on('data', (nalUnit) => {
+		//console.log("NAL", nalUnit);
+		//this.segstream.push(data);
+
+		//trackDecodeInfo.collectDtsInfo(track, nalUnit);
+
+		// record the track config
+		if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp') {
+			this.track.config = nalUnit.config;
+			this.track.sps = [nalUnit.data];
+
+			VIDEO_PROPERTIES.forEach(function(prop) {
+				this.track[prop] = nalUnit.config[prop];
+			}, this);
+		}
+
+		if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp') {
+			//pps = nalUnit.data;
+			this.track.pps = [nalUnit.data];
+		}
+
+		if (!init_seg && this.track.sps && this.track.pps) {
+			init_seg = true;
+			console.log("Init", this.track);
+			this.doAppend(MP4.initSegment([this.track]));
+		}
+
+		let keyFrame = nalUnit.nalUnitType == 'slice_layer_without_partitioning_rbsp_idr';
+
+		// buffer video until flush() is called
+		//nalUnits.push(nalUnit);
+		//if (keyFrame || !nalUnit.hasOwnProperty("nalUnitType")) {
+			/*this.track.samples.push({
+				data: nalUnit.data,
+				duration: 1,
+				pts: nalUnit.pts,
+				dts: nalUnit.dts,
+				flags: {
+					isLeading: 0,
+					isDependedOn: 0,
+					hasRedundancy: 0,
+					degradPrio: 0,
+					isNonSyncSample: keyFrame ? 0 : 1,
+					dependsOn: keyFrame ? 2 : 1,
+					paddingValue: 0,
+				}
+			});*/
+
+			let sample = this.track.samples[0];
+			sample.units.push(nalUnit);
+			sample.size += nalUnit.data.byteLength + 4;
+
+			sample.keyFrame &= keyFrame;
+			
+			if (keyFrame) {
+				sample.flags.isNonSyncSample = 0;
+				sample.flags.dependsOn = 2;
+			}
+		//}
+	});
+
+	this.track = {
+		timelineStartInfo: {
+			baseMediaDecodeTime: 0
+		},
+		baseMediaDecodeTime: 0,
+		id: 0,
+		codec: 'avc',
+		type: 'video',
+		samples: [],
+		duration: 0
+	};
+	
+	this.mediaSource = new MediaSource();
+	//this.element.play();
+	this.sourceBuffer = null;
+
+	this.element.addEventListener('pause', (e) => {
+		console.log("pause");
+		this.active = false;
+	});
+
+	this.element.addEventListener('play', (e) => {
+		console.log("Play");
+		init_seg = false;
+		seen_keyframe = false;
+		ts = 0;
+		this.track.baseMediaDecodeTime = 0;
+		this.sequenceNo = 0;
+		this.active = true;
+		this.start(0,0,0);
+	});
+
+	this.mediaSource.addEventListener('sourceopen', (e) => {
+		console.log("Source Open");
+		URL.revokeObjectURL(this.element.src);
+		console.log(this.mediaSource.readyState);
+		this.sourceBuffer = e.target.addSourceBuffer(this.mime);
+		this.sourceBuffer.mode = 'sequence';
+		this.active = true;
+
+		this.sourceBuffer.addEventListener('error', (e) => {
+			console.error("SourceBuffer: ", e);
+			this.active = false;
+		});
+
+		this.sourceBuffer.addEventListener('updateend', () => {
+			if (this.queue.length > 0) {
+				let s = this.queue[0];
+				this.queue.shift();
+				//console.log("Append", s);
+				this.sourceBuffer.appendBuffer(s);
+			}
+		});
+	});
+
+	this.mime = 'video/mp4; codecs="avc1.640028"';
+	this.queue = [];
+	this.sequenceNo = 0;
+
+	this.element.src = URL.createObjectURL(this.mediaSource);
+
     this.peer.bind(uri, (latency, streampckg, pckg) => {
-		if (this.paused) {
+		if (this.paused || !this.active) {
 			return;
 		}
 
@@ -60199,26 +70728,92 @@ function FTLStream(peer, uri, element) {
 					peer.send(uri, 0, [1,0,255,0],[255,7,35,0,0,Buffer.alloc(0)]);
 					//peer.send(current_data.uri, 0, [255,7,35,0,0,Buffer.alloc(0)], [1,0,255,0]);
 				}
+
+				//console.log("NALU", getNALType(pckg[5]));
+
+				if (!seen_keyframe) {
+					if (isKeyFrame(pckg[5])) {
+						console.log("Key frame ", streampckg[0]);
+						seen_keyframe = true;
+					}
+				}
 			
-				if (this.converter) {
+				if (seen_keyframe) {
+					if (ts == 0) ts = streampckg[0];
+					//if (this.track.samples.length > 0) console.error("Unfinished sample");
+					dts += streampckg[0]-ts;
+
+					this.track.samples.push(createDefaultSample());
+
+					this.h264.push({
+						type: 'video',
+						dts: dts,
+						pts: streampckg[0],
+						data: pckg[5],
+						trackId: 0
+					});
+					this.h264.flush();
+
+					let sample = this.track.samples[0];
+					/*if (sample.keyFrame) {
+						sample.flags.isNonSyncSample = 0;
+						sample.flags.dependsOn = 2;
+					}*/
+
+					concatNals(sample);
+					let delta = (streampckg[0]-ts)*90;
+					sample.duration = (delta > 0) ? delta : 1000;
+
+					let moof = MP4.moof(this.sequenceNo++, [this.track]);
+					let mdat = MP4.mdat(sample.data);
+					let result = new Uint8Array(moof.byteLength + mdat.byteLength);
+					//result.set(MP4.STYP);
+					result.set(moof);
+					result.set(mdat, moof.byteLength);
+					this.doAppend(result);
+				
+					//this.doAppend();
+					//this.doAppend(MP4.mdat(sample.data));
+					this.track.samples = [];
+					this.track.baseMediaDecodeTime += delta;
+
+					//this.segstream.flush();
+					//if (isKeyFrame(pckg[5])) console.log("Key frame ", streampckg[0]);
 					/*function decode(value){
 						this.converter.appendRawData(value);
 					}
 					decode(pckg[5]);*/
-					if (this.converter.sourceBuffer && this.converter.sourceBuffer.mode != "sequence") {
-						this.converter.sourceBuffer.mode = 'sequence';
-					}
-					this.converter.appendRawData(pckg[5], (streampckg[0]-ts));
-					this.converter.play();
-				} else {
+					//if (this.converter.sourceBuffer && this.converter.sourceBuffer.mode != "sequence") {
+					//	this.converter.sourceBuffer.mode = 'sequence';
+					//}
+					//this.converter.appendRawData(pckg[5], (streampckg[0]-ts));
+					//this.converter.play();
+
+					/*if (ts > 0) {
+						this.converter.feed({
+							video: pckg[5],
+							duration: streampckg[0]-ts
+						});
+					} else {
+						this.converter.feed({
+							video: pckg[5]
+						});
+					}*/
+
+					ts = streampckg[0];
+				}
+				
+				
+				/*else {
 					if (ts > 0) {
 						dts = streampckg[0] - ts;
 						console.log("Framerate = ", 1000/dts);
-						this.converter = new VideoConverter.default(this.element, 31, 1);
+						//this.converter = new VideoConverter.default(this.element, 31, 1);
+						
 						dts = 0;
 					}
 					ts = streampckg[0];
-				}
+				}*/
 			}
         } else if (pckg[0] === 103) {
 			//console.log(msgpack.decode(pckg[5]));
@@ -60233,6 +70828,8 @@ function FTLStream(peer, uri, element) {
 			this.start(0,0,0);
 		});
 	}
+
+	this.element.play();
 }
 
 FTLStream.prototype.on = function(name, cb) {
@@ -60399,1344 +70996,375 @@ saveConfigs = async () => {
     });
     const content = await rawResp.json();
 }
-}).call(this,require("buffer").Buffer)
-},{"../../server/src/peer":53,"./lib/dist/util/debug":50,"./lib/dist/video-converter":52,"buffer":8,"msgpack5":15,"rematrix":31,"three":34}],45:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var bit_stream_1 = require("./util/bit-stream");
-var debug = require("./util/debug");
-var NALU_1 = require("./util/NALU");
-var H264Parser = (function () {
-    function H264Parser(remuxer) {
-        this.remuxer = remuxer;
-        this.track = remuxer.mp4track;
-    }
-    H264Parser.prototype.parseSEI = function (sei) {
-        var messages = H264Parser.readSEI(sei);
-        for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
-            var m = messages_1[_i];
-            switch (m.type) {
-                case 0:
-                    this.track.seiBuffering = true;
-                    break;
-                case 5:
-                    return true;
-                default:
-                    break;
-            }
-        }
-        return false;
-    };
-    H264Parser.prototype.parseSPS = function (sps) {
-        var config = H264Parser.readSPS(sps);
-        this.track.width = config.width;
-        this.track.height = config.height;
-        this.track.sps = [sps];
-        this.track.codec = 'avc1.';
-        var codecArray = new DataView(sps.buffer, sps.byteOffset + 1, 4);
-        for (var i = 0; i < 3; ++i) {
-            var h = codecArray.getUint8(i).toString(16);
-            if (h.length < 2) {
-                h = '0' + h;
-            }
-            this.track.codec += h;
-        }
-    };
-    H264Parser.prototype.parsePPS = function (pps) {
-        this.track.pps = [pps];
-    };
-    H264Parser.prototype.parseNAL = function (unit) {
-        if (!unit) {
-            return false;
-        }
-        var push = false;
-        switch (unit.type()) {
-            case NALU_1.default.NDR:
-            case NALU_1.default.IDR:
-                push = true;
-                break;
-            case NALU_1.default.SEI:
-                push = this.parseSEI(unit.getData().subarray(4));
-                break;
-            case NALU_1.default.SPS:
-                if (this.track.sps.length === 0) {
-                    this.parseSPS(unit.getData().subarray(4));
-                    debug.log(" Found SPS type NALU frame.");
-                    if (!this.remuxer.readyToDecode && this.track.pps.length > 0 && this.track.sps.length > 0) {
-                        this.remuxer.readyToDecode = true;
-                    }
-                }
-                break;
-            case NALU_1.default.PPS:
-                if (this.track.pps.length === 0) {
-                    this.parsePPS(unit.getData().subarray(4));
-                    debug.log(" Found PPS type NALU frame.");
-                    if (!this.remuxer.readyToDecode && this.track.pps.length > 0 && this.track.sps.length > 0) {
-                        this.remuxer.readyToDecode = true;
-                    }
-                }
-                break;
-            default:
-                debug.log(" Found Unknown type NALU frame. type=" + unit.type());
-                break;
-        }
-        return push;
-    };
-    H264Parser.skipScalingList = function (decoder, count) {
-        var lastScale = 8;
-        var nextScale = 8;
-        for (var j = 0; j < count; j++) {
-            if (nextScale !== 0) {
-                var deltaScale = decoder.readEG();
-                nextScale = (lastScale + deltaScale + 256) % 256;
-            }
-            lastScale = (nextScale === 0) ? lastScale : nextScale;
-        }
-    };
-    H264Parser.readSPS = function (data) {
-        var decoder = new bit_stream_1.default(data);
-        var frameCropLeftOffset = 0;
-        var frameCropRightOffset = 0;
-        var frameCropTopOffset = 0;
-        var frameCropBottomOffset = 0;
-        var sarScale = 1;
-        decoder.readUByte();
-        var profileIdc = decoder.readUByte();
-        decoder.skipBits(5);
-        decoder.skipBits(3);
-        decoder.skipBits(8);
-        decoder.skipUEG();
-        if (profileIdc === 100 ||
-            profileIdc === 110 ||
-            profileIdc === 122 ||
-            profileIdc === 244 ||
-            profileIdc === 44 ||
-            profileIdc === 83 ||
-            profileIdc === 86 ||
-            profileIdc === 118 ||
-            profileIdc === 128) {
-            var chromaFormatIdc = decoder.readUEG();
-            if (chromaFormatIdc === 3) {
-                decoder.skipBits(1);
-            }
-            decoder.skipUEG();
-            decoder.skipUEG();
-            decoder.skipBits(1);
-            if (decoder.readBoolean()) {
-                var scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12;
-                for (var i = 0; i < scalingListCount; ++i) {
-                    if (decoder.readBoolean()) {
-                        if (i < 6) {
-                            H264Parser.skipScalingList(decoder, 16);
-                        }
-                        else {
-                            H264Parser.skipScalingList(decoder, 64);
-                        }
-                    }
-                }
-            }
-        }
-        decoder.skipUEG();
-        var picOrderCntType = decoder.readUEG();
-        if (picOrderCntType === 0) {
-            decoder.readUEG();
-        }
-        else if (picOrderCntType === 1) {
-            decoder.skipBits(1);
-            decoder.skipEG();
-            decoder.skipEG();
-            var numRefFramesInPicOrderCntCycle = decoder.readUEG();
-            for (var i = 0; i < numRefFramesInPicOrderCntCycle; ++i) {
-                decoder.skipEG();
-            }
-        }
-        decoder.skipUEG();
-        decoder.skipBits(1);
-        var picWidthInMbsMinus1 = decoder.readUEG();
-        var picHeightInMapUnitsMinus1 = decoder.readUEG();
-        var frameMbsOnlyFlag = decoder.readBits(1);
-        if (frameMbsOnlyFlag === 0) {
-            decoder.skipBits(1);
-        }
-        decoder.skipBits(1);
-        if (decoder.readBoolean()) {
-            frameCropLeftOffset = decoder.readUEG();
-            frameCropRightOffset = decoder.readUEG();
-            frameCropTopOffset = decoder.readUEG();
-            frameCropBottomOffset = decoder.readUEG();
-        }
-        if (decoder.readBoolean()) {
-            if (decoder.readBoolean()) {
-                var sarRatio = void 0;
-                var aspectRatioIdc = decoder.readUByte();
-                switch (aspectRatioIdc) {
-                    case 1:
-                        sarRatio = [1, 1];
-                        break;
-                    case 2:
-                        sarRatio = [12, 11];
-                        break;
-                    case 3:
-                        sarRatio = [10, 11];
-                        break;
-                    case 4:
-                        sarRatio = [16, 11];
-                        break;
-                    case 5:
-                        sarRatio = [40, 33];
-                        break;
-                    case 6:
-                        sarRatio = [24, 11];
-                        break;
-                    case 7:
-                        sarRatio = [20, 11];
-                        break;
-                    case 8:
-                        sarRatio = [32, 11];
-                        break;
-                    case 9:
-                        sarRatio = [80, 33];
-                        break;
-                    case 10:
-                        sarRatio = [18, 11];
-                        break;
-                    case 11:
-                        sarRatio = [15, 11];
-                        break;
-                    case 12:
-                        sarRatio = [64, 33];
-                        break;
-                    case 13:
-                        sarRatio = [160, 99];
-                        break;
-                    case 14:
-                        sarRatio = [4, 3];
-                        break;
-                    case 15:
-                        sarRatio = [3, 2];
-                        break;
-                    case 16:
-                        sarRatio = [2, 1];
-                        break;
-                    case 255: {
-                        sarRatio = [decoder.readUByte() << 8 | decoder.readUByte(), decoder.readUByte() << 8 | decoder.readUByte()];
-                        break;
-                    }
-                    default: {
-                        debug.error("  H264: Unknown aspectRatioIdc=" + aspectRatioIdc);
-                    }
-                }
-                if (sarRatio) {
-                    sarScale = sarRatio[0] / sarRatio[1];
-                }
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipBits(1);
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipBits(4);
-                if (decoder.readBoolean()) {
-                    decoder.skipBits(24);
-                }
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipUEG();
-                decoder.skipUEG();
-            }
-            if (decoder.readBoolean()) {
-                var unitsInTick = decoder.readUInt();
-                var timeScale = decoder.readUInt();
-                var fixedFrameRate = decoder.readBoolean();
-                var frameDuration = timeScale / (2 * unitsInTick);
-                debug.log("timescale: " + timeScale + "; unitsInTick: " + unitsInTick + "; " +
-                    ("fixedFramerate: " + fixedFrameRate + "; avgFrameDuration: " + frameDuration));
-            }
-        }
-        return {
-            width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
-            height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) -
-                ((frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)),
-        };
-    };
-    H264Parser.readSEI = function (data) {
-        var decoder = new bit_stream_1.default(data);
-        decoder.skipBits(8);
-        var result = [];
-        while (decoder.bitsAvailable > 3 * 8) {
-            result.push(this.readSEIMessage(decoder));
-        }
-        return result;
-    };
-    H264Parser.readSEIMessage = function (decoder) {
-        function get() {
-            var result = 0;
-            while (true) {
-                var value = decoder.readUByte();
-                result += value;
-                if (value !== 0xff) {
-                    break;
-                }
-            }
-            return result;
-        }
-        var payloadType = get();
-        var payloadSize = get();
-        return this.readSEIPayload(decoder, payloadType, payloadSize);
-    };
-    H264Parser.readSEIPayload = function (decoder, type, size) {
-        var result;
-        switch (type) {
-            default:
-                result = { type: type };
-                decoder.skipBits(size * 8);
-        }
-        decoder.skipBits(decoder.bitsAvailable % 8);
-        return result;
-    };
-    return H264Parser;
-}());
-exports.default = H264Parser;
-
-},{"./util/NALU":48,"./util/bit-stream":49,"./util/debug":50}],46:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_parser_1 = require("./h264-parser");
-var debug = require("./util/debug");
-var NALU_1 = require("./util/NALU");
-var trackId = 1;
-var H264Remuxer = (function () {
-    function H264Remuxer(fps, framePerFragment, timescale) {
-        this.fps = fps;
-        this.framePerFragment = framePerFragment;
-        this.timescale = timescale;
-        this.readyToDecode = false;
-        this.totalDTS = 0;
-        this.stepDTS = Math.round(this.timescale / this.fps);
-        this.frameCount = 0;
-        this.seq = 1;
-        this.mp4track = {
-            id: H264Remuxer.getTrackID(),
-            type: 'video',
-            len: 0,
-            codec: '',
-            sps: [],
-            pps: [],
-            seiBuffering: false,
-            width: 0,
-            height: 0,
-            timescale: timescale,
-            duration: timescale,
-            samples: [],
-            isKeyFrame: true,
-        };
-        this.unitSamples = [[]];
-        this.parser = new h264_parser_1.default(this);
-    }
-    H264Remuxer.getTrackID = function () {
-        return trackId++;
-    };
-    Object.defineProperty(H264Remuxer.prototype, "seqNum", {
-        get: function () {
-            return this.seq;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    H264Remuxer.prototype.remux = function (nalu) {
-        if (this.mp4track.seiBuffering && nalu.type() === NALU_1.default.SEI) {
-            return this.createNextFrame();
-        }
-        if (this.parser.parseNAL(nalu)) {
-            this.unitSamples[this.unitSamples.length - 1].push(nalu);
-            this.mp4track.len += nalu.getSize();
-        }
-        if (!this.mp4track.seiBuffering && (nalu.type() === NALU_1.default.IDR || nalu.type() === NALU_1.default.NDR)) {
-            return this.createNextFrame();
-        }
-        return;
-    };
-    H264Remuxer.prototype.createNextFrame = function () {
-        if (this.mp4track.len > 0) {
-            this.frameCount++;
-            if (this.frameCount % this.framePerFragment === 0) {
-                var fragment = this.getFragment();
-                if (fragment) {
-                    var dts = this.totalDTS;
-                    this.totalDTS = this.stepDTS * this.frameCount;
-                    return [dts, fragment];
-                }
-                else {
-                    debug.log("No mp4 sample data.");
-                }
-            }
-            this.unitSamples.push([]);
-        }
-        return;
-    };
-    H264Remuxer.prototype.flush = function () {
-        this.seq++;
-        this.mp4track.len = 0;
-        this.mp4track.samples = [];
-        this.mp4track.isKeyFrame = false;
-        this.unitSamples = [[]];
-    };
-    H264Remuxer.prototype.getFragment = function () {
-        if (!this.checkReadyToDecode()) {
-            return undefined;
-        }
-        var payload = new Uint8Array(this.mp4track.len);
-        this.mp4track.samples = [];
-        var offset = 0;
-        for (var i = 0, len = this.unitSamples.length; i < len; i++) {
-            var units = this.unitSamples[i];
-            if (units.length === 0) {
-                continue;
-            }
-            var mp4Sample = {
-                size: 0,
-                cts: this.stepDTS * i,
-            };
-            for (var _i = 0, units_1 = units; _i < units_1.length; _i++) {
-                var unit = units_1[_i];
-                mp4Sample.size += unit.getSize();
-                payload.set(unit.getData(), offset);
-                offset += unit.getSize();
-            }
-            this.mp4track.samples.push(mp4Sample);
-        }
-        if (offset === 0) {
-            return undefined;
-        }
-        return payload;
-    };
-    H264Remuxer.prototype.checkReadyToDecode = function () {
-        if (!this.readyToDecode || this.unitSamples.filter(function (array) { return array.length > 0; }).length === 0) {
-            debug.log("Not ready to decode! readyToDecode(" + this.readyToDecode + ") is false or units is empty.");
-            return false;
-        }
-        return true;
-    };
-    return H264Remuxer;
-}());
-exports.default = H264Remuxer;
-
-},{"./h264-parser":45,"./util/NALU":48,"./util/debug":50}],47:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var MP4 = (function () {
-    function MP4() {
-    }
-    MP4.init = function () {
-        MP4.initalized = true;
-        MP4.types = {
-            avc1: [],
-            avcC: [],
-            btrt: [],
-            dinf: [],
-            dref: [],
-            esds: [],
-            ftyp: [],
-            hdlr: [],
-            mdat: [],
-            mdhd: [],
-            mdia: [],
-            mfhd: [],
-            minf: [],
-            moof: [],
-            moov: [],
-            mp4a: [],
-            mvex: [],
-            mvhd: [],
-            sdtp: [],
-            stbl: [],
-            stco: [],
-            stsc: [],
-            stsd: [],
-            stsz: [],
-            stts: [],
-            styp: [],
-            tfdt: [],
-            tfhd: [],
-            traf: [],
-            trak: [],
-            trun: [],
-            trep: [],
-            trex: [],
-            tkhd: [],
-            vmhd: [],
-            smhd: [],
-        };
-        for (var type in MP4.types) {
-            if (MP4.types.hasOwnProperty(type)) {
-                MP4.types[type] = [
-                    type.charCodeAt(0),
-                    type.charCodeAt(1),
-                    type.charCodeAt(2),
-                    type.charCodeAt(3),
-                ];
-            }
-        }
-        var hdlr = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x76, 0x69, 0x64, 0x65,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x56, 0x69, 0x64, 0x65,
-            0x6f, 0x48, 0x61, 0x6e,
-            0x64, 0x6c, 0x65, 0x72, 0x00,
-        ]);
-        var dref = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x0c,
-            0x75, 0x72, 0x6c, 0x20,
-            0x00,
-            0x00, 0x00, 0x01,
-        ]);
-        var stco = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-        ]);
-        MP4.STTS = MP4.STSC = MP4.STCO = stco;
-        MP4.STSZ = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-        ]);
-        MP4.VMHD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x01,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-        ]);
-        MP4.SMHD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-        ]);
-        MP4.STSD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01
-        ]);
-        MP4.FTYP = MP4.box(MP4.types.ftyp, new Uint8Array([
-            0x69, 0x73, 0x6f, 0x35,
-            0x00, 0x00, 0x00, 0x01,
-            0x61, 0x76, 0x63, 0x31,
-            0x69, 0x73, 0x6f, 0x35,
-            0x64, 0x61, 0x73, 0x68,
-        ]));
-        MP4.STYP = MP4.box(MP4.types.styp, new Uint8Array([
-            0x6d, 0x73, 0x64, 0x68,
-            0x00, 0x00, 0x00, 0x00,
-            0x6d, 0x73, 0x64, 0x68,
-            0x6d, 0x73, 0x69, 0x78,
-        ]));
-        MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
-        MP4.HDLR = MP4.box(MP4.types.hdlr, hdlr);
-    };
-    MP4.box = function (type) {
-        var payload = [];
-        for (var _i = 1; _i < arguments.length; _i++) {
-            payload[_i - 1] = arguments[_i];
-        }
-        var size = 8;
-        for (var _a = 0, payload_1 = payload; _a < payload_1.length; _a++) {
-            var p = payload_1[_a];
-            size += p.byteLength;
-        }
-        var result = new Uint8Array(size);
-        result[0] = (size >> 24) & 0xff;
-        result[1] = (size >> 16) & 0xff;
-        result[2] = (size >> 8) & 0xff;
-        result[3] = size & 0xff;
-        result.set(type, 4);
-        size = 8;
-        for (var _b = 0, payload_2 = payload; _b < payload_2.length; _b++) {
-            var box = payload_2[_b];
-            result.set(box, size);
-            size += box.byteLength;
-        }
-        return result;
-    };
-    MP4.mdat = function (data) {
-        return MP4.box(MP4.types.mdat, data);
-    };
-    MP4.mdhd = function (timescale) {
-        return MP4.box(MP4.types.mdhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (timescale >> 24) & 0xFF,
-            (timescale >> 16) & 0xFF,
-            (timescale >> 8) & 0xFF,
-            timescale & 0xFF,
-            0x00, 0x00, 0x00, 0x00,
-            0x55, 0xc4,
-            0x00, 0x00,
-        ]));
-    };
-    MP4.mdia = function (track) {
-        return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale), MP4.HDLR, MP4.minf(track));
-    };
-    MP4.mfhd = function (sequenceNumber) {
-        return MP4.box(MP4.types.mfhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (sequenceNumber >> 24),
-            (sequenceNumber >> 16) & 0xFF,
-            (sequenceNumber >> 8) & 0xFF,
-            sequenceNumber & 0xFF,
-        ]));
-    };
-    MP4.minf = function (track) {
-        return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
-    };
-    MP4.moof = function (sn, baseMediaDecodeTime, track) {
-        return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
-    };
-    MP4.moov = function (tracks, duration, timescale) {
-        var boxes = [];
-        for (var _i = 0, tracks_1 = tracks; _i < tracks_1.length; _i++) {
-            var track = tracks_1[_i];
-            boxes.push(MP4.trak(track));
-        }
-        return MP4.box.apply(MP4, [MP4.types.moov, MP4.mvhd(timescale, duration), MP4.mvex(tracks)].concat(boxes));
-    };
-    MP4.mvhd = function (timescale, duration) {
-        var bytes = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (timescale >> 24) & 0xFF,
-            (timescale >> 16) & 0xFF,
-            (timescale >> 8) & 0xFF,
-            timescale & 0xFF,
-            (duration >> 24) & 0xFF,
-            (duration >> 16) & 0xFF,
-            (duration >> 8) & 0xFF,
-            duration & 0xFF,
-            0x00, 0x01, 0x00, 0x00,
-            0x01, 0x00,
-            0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x40, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x02,
-        ]);
-        return MP4.box(MP4.types.mvhd, bytes);
-    };
-    MP4.mvex = function (tracks) {
-        var boxes = [];
-        for (var _i = 0, tracks_2 = tracks; _i < tracks_2.length; _i++) {
-            var track = tracks_2[_i];
-            boxes.push(MP4.trex(track));
-        }
-        return MP4.box.apply(MP4, [MP4.types.mvex].concat(boxes, [MP4.trep()]));
-    };
-    MP4.trep = function () {
-        return MP4.box(MP4.types.trep, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-        ]));
-    };
-    MP4.stbl = function (track) {
-        return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
-    };
-    MP4.avc1 = function (track) {
-        var sps = [];
-        var pps = [];
-        for (var _i = 0, _a = track.sps; _i < _a.length; _i++) {
-            var data = _a[_i];
-            var len = data.byteLength;
-            sps.push((len >>> 8) & 0xFF);
-            sps.push((len & 0xFF));
-            sps = sps.concat(Array.prototype.slice.call(data));
-        }
-        for (var _b = 0, _c = track.pps; _b < _c.length; _b++) {
-            var data = _c[_b];
-            var len = data.byteLength;
-            pps.push((len >>> 8) & 0xFF);
-            pps.push((len & 0xFF));
-            pps = pps.concat(Array.prototype.slice.call(data));
-        }
-        var avcc = MP4.box(MP4.types.avcC, new Uint8Array([
-            0x01,
-            sps[3],
-            sps[4],
-            sps[5],
-            0xfc | 3,
-            0xE0 | track.sps.length,
-        ].concat(sps).concat([
-            track.pps.length,
-        ]).concat(pps)));
-        var width = track.width;
-        var height = track.height;
-        return MP4.box(MP4.types.avc1, new Uint8Array([
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x01,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            (width >> 8) & 0xFF,
-            width & 0xff,
-            (height >> 8) & 0xFF,
-            height & 0xff,
-            0x00, 0x48, 0x00, 0x00,
-            0x00, 0x48, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01,
-            0x12,
-            0x62, 0x69, 0x6E, 0x65,
-            0x6C, 0x70, 0x72, 0x6F,
-            0x2E, 0x72, 0x75, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x18,
-            0x11, 0x11
-        ]), avcc, MP4.box(MP4.types.btrt, new Uint8Array([
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x2d, 0xc6, 0xc0,
-            0x00, 0x2d, 0xc6, 0xc0,
-        ])));
-    };
-    MP4.stsd = function (track) {
-        return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
-    };
-    MP4.tkhd = function (track) {
-        var id = track.id;
-        var width = track.width;
-        var height = track.height;
-        return MP4.box(MP4.types.tkhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (id >> 24) & 0xFF,
-            (id >> 16) & 0xFF,
-            (id >> 8) & 0xFF,
-            id & 0xFF,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-            (track.type === 'audio' ? 0x01 : 0x00), 0x00,
-            0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x40, 0x00, 0x00, 0x00,
-            (width >> 8) & 0xFF,
-            width & 0xFF,
-            0x00, 0x00,
-            (height >> 8) & 0xFF,
-            height & 0xFF,
-            0x00, 0x00,
-        ]));
-    };
-    MP4.traf = function (track, baseMediaDecodeTime) {
-        var id = track.id;
-        return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([
-            0x00,
-            0x02, 0x00, 0x00,
-            (id >> 24),
-            (id >> 16) & 0XFF,
-            (id >> 8) & 0XFF,
-            (id & 0xFF),
-        ])), MP4.box(MP4.types.tfdt, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (baseMediaDecodeTime >> 24),
-            (baseMediaDecodeTime >> 16) & 0XFF,
-            (baseMediaDecodeTime >> 8) & 0XFF,
-            (baseMediaDecodeTime & 0xFF),
-        ])), MP4.trun(track, 16 +
-            16 +
-            8 +
-            16 +
-            8 +
-            8));
-    };
-    MP4.trak = function (track) {
-        track.duration = track.duration || 0xffffffff;
-        return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
-    };
-    MP4.trex = function (track) {
-        var id = track.id;
-        return MP4.box(MP4.types.trex, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (id >> 24),
-            (id >> 16) & 0XFF,
-            (id >> 8) & 0XFF,
-            (id & 0xFF),
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x3c,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-        ]));
-    };
-    MP4.trun = function (track, offset) {
-        var samples = track.samples || [];
-        var len = samples.length;
-        var additionalLen = track.isKeyFrame ? 4 : 0;
-        var arraylen = 12 + additionalLen + (4 * len);
-        var array = new Uint8Array(arraylen);
-        offset += 8 + arraylen;
-        array.set([
-            0x00,
-            0x00, 0x02, (track.isKeyFrame ? 0x05 : 0x01),
-            (len >>> 24) & 0xFF,
-            (len >>> 16) & 0xFF,
-            (len >>> 8) & 0xFF,
-            len & 0xFF,
-            (offset >>> 24) & 0xFF,
-            (offset >>> 16) & 0xFF,
-            (offset >>> 8) & 0xFF,
-            offset & 0xFF,
-        ], 0);
-        if (track.isKeyFrame) {
-            array.set([
-                0x00, 0x00, 0x00, 0x00,
-            ], 12);
-        }
-        for (var i = 0; i < len; i++) {
-            var sample = samples[i];
-            var size = sample.size;
-            array.set([
-                (size >>> 24) & 0xFF,
-                (size >>> 16) & 0xFF,
-                (size >>> 8) & 0xFF,
-                size & 0xFF,
-            ], 12 + additionalLen + 4 * i);
-        }
-        return MP4.box(MP4.types.trun, array);
-    };
-    MP4.initSegment = function (tracks, duration, timescale) {
-        if (!MP4.initalized) {
-            MP4.init();
-        }
-        var movie = MP4.moov(tracks, duration, timescale);
-        var result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
-        result.set(MP4.FTYP);
-        result.set(movie, MP4.FTYP.byteLength);
-        return result;
-    };
-    MP4.fragmentSegment = function (sn, baseMediaDecodeTime, track, payload) {
-        var moof = MP4.moof(sn, baseMediaDecodeTime, track);
-        var mdat = MP4.mdat(payload);
-        var result = new Uint8Array(MP4.STYP.byteLength + moof.byteLength + mdat.byteLength);
-        result.set(MP4.STYP);
-        result.set(moof, MP4.STYP.byteLength);
-        result.set(mdat, MP4.STYP.byteLength + moof.byteLength);
-        return result;
-    };
-    return MP4;
-}());
-MP4.types = {};
-MP4.initalized = false;
-exports.default = MP4;
-
-},{}],48:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var NALU = (function () {
-    function NALU(data) {
-        this.data = data;
-        this.nri = (data[0] & 0x60) >> 5;
-        this.ntype = data[0] & 0x1f;
-    }
-    Object.defineProperty(NALU, "NDR", {
-        get: function () { return 1; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "IDR", {
-        get: function () { return 5; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "SEI", {
-        get: function () { return 6; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "SPS", {
-        get: function () { return 7; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "PPS", {
-        get: function () { return 8; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "TYPES", {
-        get: function () {
-            return _a = {},
-                _a[NALU.IDR] = 'IDR',
-                _a[NALU.SEI] = 'SEI',
-                _a[NALU.SPS] = 'SPS',
-                _a[NALU.PPS] = 'PPS',
-                _a[NALU.NDR] = 'NDR',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    NALU.type = function (nalu) {
-        if (nalu.ntype in NALU.TYPES) {
-            return NALU.TYPES[nalu.ntype];
-        }
-        else {
-            return 'UNKNOWN';
-        }
-    };
-    NALU.prototype.type = function () {
-        return this.ntype;
-    };
-    NALU.prototype.isKeyframe = function () {
-        return this.ntype === NALU.IDR;
-    };
-    NALU.prototype.getSize = function () {
-        return 4 + this.data.byteLength;
-    };
-    NALU.prototype.getData = function () {
-        var result = new Uint8Array(this.getSize());
-        var view = new DataView(result.buffer);
-        view.setUint32(0, this.getSize() - 4);
-        result.set(this.data, 4);
-        return result;
-    };
-    return NALU;
-}());
-exports.default = NALU;
-
-},{}],49:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var BitStream = (function () {
-    function BitStream(data) {
-        this.data = data;
-        this.index = 0;
-        this.bitLength = data.byteLength * 8;
-    }
-    Object.defineProperty(BitStream.prototype, "bitsAvailable", {
-        get: function () {
-            return this.bitLength - this.index;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    BitStream.prototype.skipBits = function (size) {
-        if (this.bitsAvailable < size) {
-            throw new Error('no bytes available');
-        }
-        this.index += size;
-    };
-    BitStream.prototype.readBits = function (size) {
-        var result = this.getBits(size, this.index);
-        return result;
-    };
-    BitStream.prototype.getBits = function (size, offsetBits, moveIndex) {
-        if (moveIndex === void 0) { moveIndex = true; }
-        if (this.bitsAvailable < size) {
-            throw new Error('no bytes available');
-        }
-        var offset = offsetBits % 8;
-        var byte = this.data[(offsetBits / 8) | 0] & (0xff >>> offset);
-        var bits = 8 - offset;
-        if (bits >= size) {
-            if (moveIndex) {
-                this.index += size;
-            }
-            return byte >> (bits - size);
-        }
-        else {
-            if (moveIndex) {
-                this.index += bits;
-            }
-            var nextSize = size - bits;
-            return (byte << nextSize) | this.getBits(nextSize, offsetBits + bits, moveIndex);
-        }
-    };
-    BitStream.prototype.skipLZ = function () {
-        var leadingZeroCount;
-        for (leadingZeroCount = 0; leadingZeroCount < this.bitLength - this.index; ++leadingZeroCount) {
-            if (0 !== this.getBits(1, this.index + leadingZeroCount, false)) {
-                this.index += leadingZeroCount;
-                return leadingZeroCount;
-            }
-        }
-        return leadingZeroCount;
-    };
-    BitStream.prototype.skipUEG = function () {
-        this.skipBits(1 + this.skipLZ());
-    };
-    BitStream.prototype.skipEG = function () {
-        this.skipBits(1 + this.skipLZ());
-    };
-    BitStream.prototype.readUEG = function () {
-        var prefix = this.skipLZ();
-        return this.readBits(prefix + 1) - 1;
-    };
-    BitStream.prototype.readEG = function () {
-        var value = this.readUEG();
-        if (0x01 & value) {
-            return (1 + value) >>> 1;
-        }
-        else {
-            return -1 * (value >>> 1);
-        }
-    };
-    BitStream.prototype.readBoolean = function () {
-        return 1 === this.readBits(1);
-    };
-    BitStream.prototype.readUByte = function () {
-        return this.readBits(8);
-    };
-    BitStream.prototype.readUShort = function () {
-        return this.readBits(16);
-    };
-    BitStream.prototype.readUInt = function () {
-        return this.readBits(32);
-    };
-    return BitStream;
-}());
-exports.default = BitStream;
+}).call(this,require("buffer").Buffer)
+},{"../../node_modules/mux.js/lib/constants/video-properties.js":25,"../../server/src/peer":108,"buffer":8,"msgpack5":15,"mux.js":33,"rematrix":73,"three":76}],82:[function(require,module,exports){
+arguments[4][6][0].apply(exports,arguments)
+},{"dup":6,"readable-stream":99,"safe-buffer":100,"util":80}],83:[function(require,module,exports){
+(function (Buffer){
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// NOTE: These type checking functions intentionally don't use `instanceof`
+// because it is fragile and can be easily faked with `Object.create()`.
+
+function isArray(arg) {
+  if (Array.isArray) {
+    return Array.isArray(arg);
+  }
+  return objectToString(arg) === '[object Array]';
+}
+exports.isArray = isArray;
+
+function isBoolean(arg) {
+  return typeof arg === 'boolean';
+}
+exports.isBoolean = isBoolean;
+
+function isNull(arg) {
+  return arg === null;
+}
+exports.isNull = isNull;
 
-},{}],50:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var logger;
-var errorLogger;
-function setLogger(log, error) {
-    logger = log;
-    errorLogger = error != null ? error : log;
+function isNullOrUndefined(arg) {
+  return arg == null;
 }
-exports.setLogger = setLogger;
-function isEnable() {
-    return logger != null;
+exports.isNullOrUndefined = isNullOrUndefined;
+
+function isNumber(arg) {
+  return typeof arg === 'number';
 }
-exports.isEnable = isEnable;
-function log(message) {
-    var optionalParams = [];
-    for (var _i = 1; _i < arguments.length; _i++) {
-        optionalParams[_i - 1] = arguments[_i];
-    }
-    if (logger) {
-        logger.apply(void 0, [message].concat(optionalParams));
+exports.isNumber = isNumber;
+
+function isString(arg) {
+  return typeof arg === 'string';
+}
+exports.isString = isString;
+
+function isSymbol(arg) {
+  return typeof arg === 'symbol';
+}
+exports.isSymbol = isSymbol;
+
+function isUndefined(arg) {
+  return arg === void 0;
+}
+exports.isUndefined = isUndefined;
+
+function isRegExp(re) {
+  return objectToString(re) === '[object RegExp]';
+}
+exports.isRegExp = isRegExp;
+
+function isObject(arg) {
+  return typeof arg === 'object' && arg !== null;
+}
+exports.isObject = isObject;
+
+function isDate(d) {
+  return objectToString(d) === '[object Date]';
+}
+exports.isDate = isDate;
+
+function isError(e) {
+  return (objectToString(e) === '[object Error]' || e instanceof Error);
+}
+exports.isError = isError;
+
+function isFunction(arg) {
+  return typeof arg === 'function';
+}
+exports.isFunction = isFunction;
+
+function isPrimitive(arg) {
+  return arg === null ||
+         typeof arg === 'boolean' ||
+         typeof arg === 'number' ||
+         typeof arg === 'string' ||
+         typeof arg === 'symbol' ||  // ES6 symbol
+         typeof arg === 'undefined';
+}
+exports.isPrimitive = isPrimitive;
+
+exports.isBuffer = Buffer.isBuffer;
+
+function objectToString(o) {
+  return Object.prototype.toString.call(o);
+}
+
+}).call(this,{"isBuffer":require("../../../../node_modules/is-buffer/index.js")})
+},{"../../../../node_modules/is-buffer/index.js":13}],84:[function(require,module,exports){
+arguments[4][2][0].apply(exports,arguments)
+},{"dup":2}],85:[function(require,module,exports){
+arguments[4][14][0].apply(exports,arguments)
+},{"dup":14}],86:[function(require,module,exports){
+arguments[4][15][0].apply(exports,arguments)
+},{"./lib/decoder":87,"./lib/encoder":88,"./lib/streams":89,"assert":1,"bl":82,"dup":15,"safe-buffer":100}],87:[function(require,module,exports){
+arguments[4][16][0].apply(exports,arguments)
+},{"bl":82,"dup":16,"util":80}],88:[function(require,module,exports){
+arguments[4][17][0].apply(exports,arguments)
+},{"bl":82,"dup":17,"safe-buffer":100}],89:[function(require,module,exports){
+arguments[4][18][0].apply(exports,arguments)
+},{"bl":82,"dup":18,"inherits":84,"readable-stream":99}],90:[function(require,module,exports){
+arguments[4][62][0].apply(exports,arguments)
+},{"_process":63,"dup":62}],91:[function(require,module,exports){
+arguments[4][64][0].apply(exports,arguments)
+},{"./_stream_readable":93,"./_stream_writable":95,"core-util-is":83,"dup":64,"inherits":84,"process-nextick-args":90}],92:[function(require,module,exports){
+arguments[4][65][0].apply(exports,arguments)
+},{"./_stream_transform":94,"core-util-is":83,"dup":65,"inherits":84}],93:[function(require,module,exports){
+arguments[4][66][0].apply(exports,arguments)
+},{"./_stream_duplex":91,"./internal/streams/BufferList":96,"./internal/streams/destroy":97,"./internal/streams/stream":98,"_process":63,"core-util-is":83,"dup":66,"events":10,"inherits":84,"isarray":85,"process-nextick-args":90,"safe-buffer":100,"string_decoder/":101,"util":7}],94:[function(require,module,exports){
+arguments[4][67][0].apply(exports,arguments)
+},{"./_stream_duplex":91,"core-util-is":83,"dup":67,"inherits":84}],95:[function(require,module,exports){
+arguments[4][68][0].apply(exports,arguments)
+},{"./_stream_duplex":91,"./internal/streams/destroy":97,"./internal/streams/stream":98,"_process":63,"core-util-is":83,"dup":68,"inherits":84,"process-nextick-args":90,"safe-buffer":100,"timers":77,"util-deprecate":102}],96:[function(require,module,exports){
+arguments[4][69][0].apply(exports,arguments)
+},{"dup":69,"safe-buffer":100,"util":7}],97:[function(require,module,exports){
+arguments[4][70][0].apply(exports,arguments)
+},{"dup":70,"process-nextick-args":90}],98:[function(require,module,exports){
+arguments[4][71][0].apply(exports,arguments)
+},{"dup":71,"events":10}],99:[function(require,module,exports){
+arguments[4][72][0].apply(exports,arguments)
+},{"./lib/_stream_duplex.js":91,"./lib/_stream_passthrough.js":92,"./lib/_stream_readable.js":93,"./lib/_stream_transform.js":94,"./lib/_stream_writable.js":95,"dup":72}],100:[function(require,module,exports){
+arguments[4][74][0].apply(exports,arguments)
+},{"buffer":8,"dup":74}],101:[function(require,module,exports){
+arguments[4][75][0].apply(exports,arguments)
+},{"dup":75,"safe-buffer":100}],102:[function(require,module,exports){
+arguments[4][78][0].apply(exports,arguments)
+},{"dup":78}],103:[function(require,module,exports){
+var v1 = require('./v1');
+var v4 = require('./v4');
+
+var uuid = v4;
+uuid.v1 = v1;
+uuid.v4 = v4;
+
+module.exports = uuid;
+
+},{"./v1":106,"./v4":107}],104:[function(require,module,exports){
+/**
+ * Convert array of 16 byte values to UUID string format of the form:
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
+ */
+var byteToHex = [];
+for (var i = 0; i < 256; ++i) {
+  byteToHex[i] = (i + 0x100).toString(16).substr(1);
+}
+
+function bytesToUuid(buf, offset) {
+  var i = offset || 0;
+  var bth = byteToHex;
+  // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
+  return ([
+    bth[buf[i++]], bth[buf[i++]],
+    bth[buf[i++]], bth[buf[i++]], '-',
+    bth[buf[i++]], bth[buf[i++]], '-',
+    bth[buf[i++]], bth[buf[i++]], '-',
+    bth[buf[i++]], bth[buf[i++]], '-',
+    bth[buf[i++]], bth[buf[i++]],
+    bth[buf[i++]], bth[buf[i++]],
+    bth[buf[i++]], bth[buf[i++]]
+  ]).join('');
+}
+
+module.exports = bytesToUuid;
+
+},{}],105:[function(require,module,exports){
+// Unique ID creation requires a high quality random # generator.  In the
+// browser this is a little complicated due to unknown quality of Math.random()
+// and inconsistent support for the `crypto` API.  We do the best we can via
+// feature-detection
+
+// getRandomValues needs to be invoked in a context where "this" is a Crypto
+// implementation. Also, find the complete implementation of crypto on IE11.
+var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) ||
+                      (typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto));
+
+if (getRandomValues) {
+  // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
+  var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
+
+  module.exports = function whatwgRNG() {
+    getRandomValues(rnds8);
+    return rnds8;
+  };
+} else {
+  // Math.random()-based (RNG)
+  //
+  // If all else fails, use Math.random().  It's fast, but is of unspecified
+  // quality.
+  var rnds = new Array(16);
+
+  module.exports = function mathRNG() {
+    for (var i = 0, r; i < 16; i++) {
+      if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
+      rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
     }
+
+    return rnds;
+  };
 }
-exports.log = log;
-function error(message) {
-    var optionalParams = [];
-    for (var _i = 1; _i < arguments.length; _i++) {
-        optionalParams[_i - 1] = arguments[_i];
+
+},{}],106:[function(require,module,exports){
+var rng = require('./lib/rng');
+var bytesToUuid = require('./lib/bytesToUuid');
+
+// **`v1()` - Generate time-based UUID**
+//
+// Inspired by https://github.com/LiosK/UUID.js
+// and http://docs.python.org/library/uuid.html
+
+var _nodeId;
+var _clockseq;
+
+// Previous uuid creation time
+var _lastMSecs = 0;
+var _lastNSecs = 0;
+
+// See https://github.com/uuidjs/uuid for API details
+function v1(options, buf, offset) {
+  var i = buf && offset || 0;
+  var b = buf || [];
+
+  options = options || {};
+  var node = options.node || _nodeId;
+  var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
+
+  // node and clockseq need to be initialized to random values if they're not
+  // specified.  We do this lazily to minimize issues related to insufficient
+  // system entropy.  See #189
+  if (node == null || clockseq == null) {
+    var seedBytes = rng();
+    if (node == null) {
+      // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
+      node = _nodeId = [
+        seedBytes[0] | 0x01,
+        seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
+      ];
     }
-    if (errorLogger) {
-        errorLogger.apply(void 0, [message].concat(optionalParams));
+    if (clockseq == null) {
+      // Per 4.2.2, randomize (14 bit) clockseq
+      clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
     }
+  }
+
+  // UUID timestamps are 100 nano-second units since the Gregorian epoch,
+  // (1582-10-15 00:00).  JSNumbers aren't precise enough for this, so
+  // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
+  // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
+  var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
+
+  // Per 4.2.1.2, use count of uuid's generated during the current clock
+  // cycle to simulate higher resolution clock
+  var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
+
+  // Time since last uuid creation (in msecs)
+  var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
+
+  // Per 4.2.1.2, Bump clockseq on clock regression
+  if (dt < 0 && options.clockseq === undefined) {
+    clockseq = clockseq + 1 & 0x3fff;
+  }
+
+  // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
+  // time interval
+  if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
+    nsecs = 0;
+  }
+
+  // Per 4.2.1.2 Throw error if too many uuids are requested
+  if (nsecs >= 10000) {
+    throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
+  }
+
+  _lastMSecs = msecs;
+  _lastNSecs = nsecs;
+  _clockseq = clockseq;
+
+  // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
+  msecs += 12219292800000;
+
+  // `time_low`
+  var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
+  b[i++] = tl >>> 24 & 0xff;
+  b[i++] = tl >>> 16 & 0xff;
+  b[i++] = tl >>> 8 & 0xff;
+  b[i++] = tl & 0xff;
+
+  // `time_mid`
+  var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
+  b[i++] = tmh >>> 8 & 0xff;
+  b[i++] = tmh & 0xff;
+
+  // `time_high_and_version`
+  b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
+  b[i++] = tmh >>> 16 & 0xff;
+
+  // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
+  b[i++] = clockseq >>> 8 | 0x80;
+
+  // `clock_seq_low`
+  b[i++] = clockseq & 0xff;
+
+  // `node`
+  for (var n = 0; n < 6; ++n) {
+    b[i + n] = node[n];
+  }
+
+  return buf ? buf : bytesToUuid(b);
 }
-exports.error = error;
 
-},{}],51:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var NALU_1 = require("./NALU");
-var VideoStreamBuffer = (function () {
-    function VideoStreamBuffer() {
+module.exports = v1;
+
+},{"./lib/bytesToUuid":104,"./lib/rng":105}],107:[function(require,module,exports){
+var rng = require('./lib/rng');
+var bytesToUuid = require('./lib/bytesToUuid');
+
+function v4(options, buf, offset) {
+  var i = buf && offset || 0;
+
+  if (typeof(options) == 'string') {
+    buf = options === 'binary' ? new Array(16) : null;
+    options = null;
+  }
+  options = options || {};
+
+  var rnds = options.random || (options.rng || rng)();
+
+  // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
+  rnds[6] = (rnds[6] & 0x0f) | 0x40;
+  rnds[8] = (rnds[8] & 0x3f) | 0x80;
+
+  // Copy bytes to buffer, if provided
+  if (buf) {
+    for (var ii = 0; ii < 16; ++ii) {
+      buf[i + ii] = rnds[ii];
     }
-    VideoStreamBuffer.prototype.clear = function () {
-        this.buffer = undefined;
-    };
-    VideoStreamBuffer.prototype.append = function (value) {
-        var nextNalHeader = function (b) {
-            var i = 3;
-            return function () {
-                var count = 0;
-                for (; i < b.length; i++) {
-                    switch (b[i]) {
-                        case 0:
-                            count++;
-                            break;
-                        case 1:
-                            if (count === 3) {
-                                return i - 3;
-                            }
-                        default:
-                            count = 0;
-                    }
-                }
-                return;
-            };
-        };
-        var result = [];
-        var buffer;
-        if (this.buffer) {
-            if (value[3] === 1 && value[2] === 0 && value[1] === 0 && value[0] === 0) {
-                result.push(new NALU_1.default(this.buffer.subarray(4)));
-                buffer = Uint8Array.from(value);
-            }
-        }
-        if (buffer == null) {
-            buffer = this.mergeBuffer(value);
-        }
-        var lastIndex = 0;
-        var f = nextNalHeader(buffer);
-        for (var index = f(); index != null; index = f()) {
-            result.push(new NALU_1.default(buffer.subarray(lastIndex + 4, index)));
-            lastIndex = index;
-        }
-        this.buffer = buffer.subarray(lastIndex);
-        return result;
-    };
-    VideoStreamBuffer.prototype.mergeBuffer = function (value) {
-        if (this.buffer == null) {
-            return Uint8Array.from(value);
-        }
-        else {
-            var newBuffer = new Uint8Array(this.buffer.byteLength + value.length);
-            if (this.buffer.byteLength > 0) {
-                newBuffer.set(this.buffer, 0);
-            }
-            newBuffer.set(value, this.buffer.byteLength);
-            return newBuffer;
-        }
-    };
-    return VideoStreamBuffer;
-}());
-exports.default = VideoStreamBuffer;
-
-},{"./NALU":48}],52:[function(require,module,exports){
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_remuxer_1 = require("./h264-remuxer");
-var mp4_generator_1 = require("./mp4-generator");
-var debug = require("./util/debug");
-var nalu_stream_buffer_1 = require("./util/nalu-stream-buffer");
-exports.mimeType = 'video/mp4; codecs="avc1.42E01E"';
-var VideoConverter = (function () {
-    function VideoConverter(element, fps, fpf) {
-        if (fps === void 0) { fps = 60; }
-        if (fpf === void 0) { fpf = fps; }
-        this.element = element;
-        this.fps = fps;
-        this.fpf = fpf;
-        this.receiveBuffer = new nalu_stream_buffer_1.default();
-        this.queue = [];
-        if (!MediaSource || !MediaSource.isTypeSupported(exports.mimeType)) {
-            throw new Error("Your browser is not supported: " + exports.mimeType);
-        }
-        this.reset();
-    }
-    Object.defineProperty(VideoConverter, "errorNotes", {
-        get: function () {
-            return _a = {},
-                _a[MediaError.MEDIA_ERR_ABORTED] = 'fetching process aborted by user',
-                _a[MediaError.MEDIA_ERR_NETWORK] = 'error occurred when downloading',
-                _a[MediaError.MEDIA_ERR_DECODE] = 'error occurred when decoding',
-                _a[MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED] = 'audio/video not supported',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    VideoConverter.prototype.setup = function () {
-        var _this = this;
-        this.mediaReadyPromise = new Promise(function (resolve, _reject) {
-            _this.mediaSource.addEventListener('sourceopen', function () {
-                debug.log("Media Source opened.");
-                _this.sourceBuffer = _this.mediaSource.addSourceBuffer(exports.mimeType);
-                _this.sourceBuffer.addEventListener('updateend', function () {
-                    debug.log("  SourceBuffer updateend");
-                    debug.log("    sourceBuffer.buffered.length=" + _this.sourceBuffer.buffered.length);
-                    for (var i = 0, len = _this.sourceBuffer.buffered.length; i < len; i++) {
-                        debug.log("    sourceBuffer.buffered [" + i + "]: " +
-                            (_this.sourceBuffer.buffered.start(i) + ", " + _this.sourceBuffer.buffered.end(i)));
-                    }
-                    debug.log("  mediasource.duration=" + _this.mediaSource.duration);
-                    debug.log("  mediasource.readyState=" + _this.mediaSource.readyState);
-                    debug.log("  video.duration=" + _this.element.duration);
-                    debug.log("    video.buffered.length=" + _this.element.buffered.length);
-                    if (debug.isEnable()) {
-                        for (var i = 0, len = _this.element.buffered.length; i < len; i++) {
-                            debug.log("    video.buffered [" + i + "]: " + _this.element.buffered.start(i) + ", " + _this.element.buffered.end(i));
-                        }
-                    }
-                    debug.log("  video.currentTime=" + _this.element.currentTime);
-                    debug.log("  video.readyState=" + _this.element.readyState);
-                    var data = _this.queue.shift();
-                    if (data) {
-                        _this.writeBuffer(data);
-                    }
-                });
-                _this.sourceBuffer.addEventListener('error', function () {
-                    debug.error('  SourceBuffer errored!');
-                });
-                _this.mediaReady = true;
-                resolve();
-            }, false);
-            _this.mediaSource.addEventListener('sourceclose', function () {
-                debug.log("Media Source closed.");
-                _this.mediaReady = false;
-            }, false);
-            _this.element.src = URL.createObjectURL(_this.mediaSource);
-        });
-        return this.mediaReadyPromise;
-    };
-    VideoConverter.prototype.play = function () {
-        var _this = this;
-        if (!this.element.paused) {
-            return;
-        }
-        if (this.mediaReady && this.element.readyState >= 2) {
-            this.element.play();
-        }
-        else {
-            var handler_1 = function () {
-                _this.play();
-                _this.element.removeEventListener('canplaythrough', handler_1);
-            };
-            this.element.addEventListener('canplaythrough', handler_1);
-        }
-    };
-    VideoConverter.prototype.pause = function () {
-        if (this.element.paused) {
-            return;
-        }
-        this.element.pause();
-    };
-    VideoConverter.prototype.reset = function () {
-        this.receiveBuffer.clear();
-        if (this.mediaSource && this.mediaSource.readyState === 'open') {
-            this.mediaSource.duration = 0;
-            this.mediaSource.endOfStream();
-        }
-        this.mediaSource = new MediaSource();
-        this.remuxer = new h264_remuxer_1.default(this.fps, this.fpf, this.fps * 60);
-        this.mediaReady = false;
-        this.mediaReadyPromise = undefined;
-        this.queue = [];
-        this.isFirstFrame = true;
-        this.setup();
-    };
-    VideoConverter.prototype.appendRawData = function (data, dts) {
-        var nalus = this.receiveBuffer.append(data);
-        for (var _i = 0, nalus_1 = nalus; _i < nalus_1.length; _i++) {
-            var nalu = nalus_1[_i];
-            var ret = this.remuxer.remux(nalu);
-            if (ret) {
-				this.writeFragment(ret[0], ret[1]);  // ret[0]
-            }
-        }
-    };
-    VideoConverter.prototype.writeFragment = function (dts, pay) {
-        var remuxer = this.remuxer;
-        if (remuxer.mp4track.isKeyFrame) {
-            this.writeBuffer(mp4_generator_1.default.initSegment([remuxer.mp4track], Infinity, remuxer.timescale));
-        }
-        if (pay && pay.byteLength) {
-            debug.log(" Put fragment: " + remuxer.seqNum + ", frames=" + remuxer.mp4track.samples.length + ", size=" + pay.byteLength);
-            var fragment = mp4_generator_1.default.fragmentSegment(remuxer.seqNum, dts, remuxer.mp4track, pay);
-            this.writeBuffer(fragment);
-            remuxer.flush();
-        }
-        else {
-            debug.error("Nothing payload!");
-        }
-    };
-    VideoConverter.prototype.writeBuffer = function (data) {
-        var _this = this;
-        if (this.mediaReady) {
-            if (this.sourceBuffer.updating) {
-                this.queue.push(data);
-            }
-            else {
-				if (this.queue.length > 0) console.error("DATA IN QUEUE");
-                this.doAppend(data);
-            }
-        }
-        else {
-            this.queue.push(data);
-            if (this.mediaReadyPromise) {
-                this.mediaReadyPromise.then(function () {
-                    if (!_this.sourceBuffer.updating) {
-                        var d = _this.queue.shift();
-                        if (d) {
-                            _this.writeBuffer(d);
-                        }
-                    }
-                });
-                this.mediaReadyPromise = undefined;
-            }
-        }
-    };
-    VideoConverter.prototype.doAppend = function (data) {
-        var error = this.element.error;
-        if (error) {
-            debug.error("MSE Error Occured: " + VideoConverter.errorNotes[error.code]);
-            this.element.pause();
-            if (this.mediaSource.readyState === 'open') {
-                this.mediaSource.endOfStream();
-            }
-        }
-        else {
-            try {
-                this.sourceBuffer.appendBuffer(data);
-                debug.log("  appended buffer: size=" + data.byteLength);
-            }
-            catch (err) {
-                debug.error("MSE Error occured while appending buffer. " + err.name + ": " + err.message);
-            }
-        }
-    };
-    return VideoConverter;
-}());
-exports.default = VideoConverter;
+  }
+
+  return buf || bytesToUuid(rnds);
+}
+
+module.exports = v4;
 
-},{"./h264-remuxer":46,"./mp4-generator":47,"./util/debug":50,"./util/nalu-stream-buffer":51}],53:[function(require,module,exports){
+},{"./lib/bytesToUuid":104,"./lib/rng":105}],108:[function(require,module,exports){
 (function (Buffer){
 const msgpack = require('msgpack5')()
   , encode  = msgpack.encode
@@ -62025,7 +71653,7 @@ Peer.prototype.getUuid = function() {
 module.exports = Peer;
 
 }).call(this,require("buffer").Buffer)
-},{"./utils/uuidParser":54,"buffer":8,"msgpack5":15,"uuid":39}],54:[function(require,module,exports){
+},{"./utils/uuidParser":109,"buffer":8,"msgpack5":86,"uuid":103}],109:[function(require,module,exports){
 // Maps for number <-> hex string conversion
 var _byteToHex = [];
 var _hexToByte = {};
@@ -62080,4 +71708,4 @@ module.exports = {
   parse: parse,
   unparse: unparse
 };
-},{}]},{},[44]);
+},{}]},{},[81]);
diff --git a/web-service/public/js/index.js b/web-service/public/js/index.js
index b2047a55260c3d44994d6575155bf03e8bcfa7b5..56d70d3bfc599ed1e89f24d1aba1505e3f819931 100644
--- a/web-service/public/js/index.js
+++ b/web-service/public/js/index.js
@@ -1,11 +1,11 @@
 const Peer = require('../../server/src/peer')
-const VideoConverter = require('./lib/dist/video-converter');
 const msgpack = require('msgpack5')();
 const rematrix = require('rematrix');
 const THREE = require('three');
-
-var debug = require("./lib/dist/util/debug");
-debug.setLogger(null,console.error);
+const MUXJS = require('mux.js');
+const MP4 = MUXJS.mp4.generator;
+const H264Stream = MUXJS.codecs.h264.H264Stream;
+const VIDEO_PROPERTIES = require('../../node_modules/mux.js/lib/constants/video-properties.js');
 
 let current_data = {};
 let peer;
@@ -130,6 +130,50 @@ function FTLFrameset(id) {
 	this.sources = {};
 }
 
+function getNALType(data) {
+	return (data.length > 4) ? data.readUInt8(4) & 0x1F : 0;
+}
+
+function isKeyFrame(data) {
+	return getNALType(data) == 7;  // SPS
+}
+
+function concatNals(sample) {
+	let length = sample.size;
+	let data = new Uint8Array(length);
+	let view = new DataView(data.buffer);
+	let dataOffset = 0;
+
+	for (var i=0; i<sample.units.length; ++i) {
+		view.setUint32(dataOffset, sample.units[i].data.byteLength);
+        dataOffset += 4;
+        data.set(sample.units[i].data, dataOffset);
+        dataOffset += sample.units[i].data.byteLength;
+	}
+
+	sample.data = data;
+}
+
+var createDefaultSample = function() {
+	return {
+	  units: [],
+	  data: null,
+	  size: 0,
+	  compositionTimeOffset: 1,
+	  duration: 0,
+	  dataOffset: 0,
+	  flags: {
+		isLeading: 0,
+		dependsOn: 1,
+		isDependedOn: 0,
+		hasRedundancy: 0,
+		degradationPriority: 0,
+		isNonSyncSample: 1
+	  },
+	  keyFrame: true
+	};
+  };
+
 function FTLStream(peer, uri, element) {
 	this.uri = uri;
 	this.peer = peer;
@@ -290,6 +334,7 @@ function FTLStream(peer, uri, element) {
 	this.overlay.appendChild(this.pause_button);
 
 	this.paused = false;
+	this.active = false;
 
 	this.overlay.addEventListener('keydown', (event) => {
 		console.log(event);
@@ -324,14 +369,159 @@ function FTLStream(peer, uri, element) {
 		//this.setPose(pose);
 	//}
 
-    this.converter = null;
+	//this.converter = null;
+	
+	/*this.converter = new JMuxer({
+		node: 'ftl-video-element',
+		mode: 'video',
+		//fps: 1000/dts,
+		fps: 30,
+		flushingTime: 1,
+		clearBuffer: false
+	});*/
 
     let rxcount = 0;
     let ts = 0;
 	let dts = 0;
 
+	let seen_keyframe = false;
+	let init_seg = false;
+
+	this.h264 = new H264Stream();
+
+	this.doAppend = function(data) {
+		if (this.sourceBuffer.updating) {
+			this.queue.push(data);
+		} else {
+			//console.log("Direct append: ", data);
+			this.sourceBuffer.appendBuffer(data);
+		}
+	}
+
+	this.h264.on('data', (nalUnit) => {
+		//console.log("NAL", nalUnit);
+		//this.segstream.push(data);
+
+		//trackDecodeInfo.collectDtsInfo(track, nalUnit);
+
+		// record the track config
+		if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp') {
+			this.track.config = nalUnit.config;
+			this.track.sps = [nalUnit.data];
+
+			VIDEO_PROPERTIES.forEach(function(prop) {
+				this.track[prop] = nalUnit.config[prop];
+			}, this);
+		}
+
+		if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp') {
+			//pps = nalUnit.data;
+			this.track.pps = [nalUnit.data];
+		}
+
+		if (!init_seg && this.track.sps && this.track.pps) {
+			init_seg = true;
+			console.log("Init", this.track);
+			this.doAppend(MP4.initSegment([this.track]));
+		}
+
+		let keyFrame = nalUnit.nalUnitType == 'slice_layer_without_partitioning_rbsp_idr';
+
+		// buffer video until flush() is called
+		//nalUnits.push(nalUnit);
+		//if (keyFrame || !nalUnit.hasOwnProperty("nalUnitType")) {
+			/*this.track.samples.push({
+				data: nalUnit.data,
+				duration: 1,
+				pts: nalUnit.pts,
+				dts: nalUnit.dts,
+				flags: {
+					isLeading: 0,
+					isDependedOn: 0,
+					hasRedundancy: 0,
+					degradPrio: 0,
+					isNonSyncSample: keyFrame ? 0 : 1,
+					dependsOn: keyFrame ? 2 : 1,
+					paddingValue: 0,
+				}
+			});*/
+
+			let sample = this.track.samples[0];
+			sample.units.push(nalUnit);
+			sample.size += nalUnit.data.byteLength + 4;
+
+			sample.keyFrame &= keyFrame;
+			
+			if (keyFrame) {
+				sample.flags.isNonSyncSample = 0;
+				sample.flags.dependsOn = 2;
+			}
+		//}
+	});
+
+	this.track = {
+		timelineStartInfo: {
+			baseMediaDecodeTime: 0
+		},
+		baseMediaDecodeTime: 0,
+		id: 0,
+		codec: 'avc',
+		type: 'video',
+		samples: [],
+		duration: 0
+	};
+	
+	this.mediaSource = new MediaSource();
+	//this.element.play();
+	this.sourceBuffer = null;
+
+	this.element.addEventListener('pause', (e) => {
+		console.log("pause");
+		this.active = false;
+	});
+
+	this.element.addEventListener('play', (e) => {
+		console.log("Play");
+		init_seg = false;
+		seen_keyframe = false;
+		ts = 0;
+		this.track.baseMediaDecodeTime = 0;
+		this.sequenceNo = 0;
+		this.active = true;
+		this.start(0,0,0);
+	});
+
+	this.mediaSource.addEventListener('sourceopen', (e) => {
+		console.log("Source Open");
+		URL.revokeObjectURL(this.element.src);
+		console.log(this.mediaSource.readyState);
+		this.sourceBuffer = e.target.addSourceBuffer(this.mime);
+		this.sourceBuffer.mode = 'sequence';
+		this.active = true;
+
+		this.sourceBuffer.addEventListener('error', (e) => {
+			console.error("SourceBuffer: ", e);
+			this.active = false;
+		});
+
+		this.sourceBuffer.addEventListener('updateend', () => {
+			if (this.queue.length > 0) {
+				let s = this.queue[0];
+				this.queue.shift();
+				//console.log("Append", s);
+				this.sourceBuffer.appendBuffer(s);
+			}
+		});
+	});
+
+	this.mime = 'video/mp4; codecs="avc1.640028"';
+	this.queue = [];
+	this.sequenceNo = 0;
+
+	this.element.src = URL.createObjectURL(this.mediaSource);
+
     this.peer.bind(uri, (latency, streampckg, pckg) => {
-		if (this.paused) {
+		if (this.paused || !this.active) {
 			return;
 		}
 
@@ -345,26 +535,92 @@ function FTLStream(peer, uri, element) {
 					peer.send(uri, 0, [1,0,255,0],[255,7,35,0,0,Buffer.alloc(0)]);
 					//peer.send(current_data.uri, 0, [255,7,35,0,0,Buffer.alloc(0)], [1,0,255,0]);
 				}
+
+				//console.log("NALU", getNALType(pckg[5]));
+
+				if (!seen_keyframe) {
+					if (isKeyFrame(pckg[5])) {
+						console.log("Key frame ", streampckg[0]);
+						seen_keyframe = true;
+					}
+				}
 			
-				if (this.converter) {
+				if (seen_keyframe) {
+					if (ts == 0) ts = streampckg[0];
+					//if (this.track.samples.length > 0) console.error("Unfinished sample");
+					dts += streampckg[0]-ts;
+
+					this.track.samples.push(createDefaultSample());
+
+					this.h264.push({
+						type: 'video',
+						dts: dts,
+						pts: streampckg[0],
+						data: pckg[5],
+						trackId: 0
+					});
+					this.h264.flush();
+
+					let sample = this.track.samples[0];
+					/*if (sample.keyFrame) {
+						sample.flags.isNonSyncSample = 0;
+						sample.flags.dependsOn = 2;
+					}*/
+
+					concatNals(sample);
+					let delta = (streampckg[0]-ts)*90;
+					sample.duration = (delta > 0) ? delta : 1000;
+
+					let moof = MP4.moof(this.sequenceNo++, [this.track]);
+					let mdat = MP4.mdat(sample.data);
+					let result = new Uint8Array(moof.byteLength + mdat.byteLength);
+					//result.set(MP4.STYP);
+					result.set(moof);
+					result.set(mdat, moof.byteLength);
+					this.doAppend(result);
+				
+					//this.doAppend();
+					//this.doAppend(MP4.mdat(sample.data));
+					this.track.samples = [];
+					this.track.baseMediaDecodeTime += delta;
+
+					//this.segstream.flush();
+					//if (isKeyFrame(pckg[5])) console.log("Key frame ", streampckg[0]);
 					/*function decode(value){
 						this.converter.appendRawData(value);
 					}
 					decode(pckg[5]);*/
-					if (this.converter.sourceBuffer && this.converter.sourceBuffer.mode != "sequence") {
-						this.converter.sourceBuffer.mode = 'sequence';
-					}
-					this.converter.appendRawData(pckg[5], (streampckg[0]-ts));
-					this.converter.play();
-				} else {
+					//if (this.converter.sourceBuffer && this.converter.sourceBuffer.mode != "sequence") {
+					//	this.converter.sourceBuffer.mode = 'sequence';
+					//}
+					//this.converter.appendRawData(pckg[5], (streampckg[0]-ts));
+					//this.converter.play();
+
+					/*if (ts > 0) {
+						this.converter.feed({
+							video: pckg[5],
+							duration: streampckg[0]-ts
+						});
+					} else {
+						this.converter.feed({
+							video: pckg[5]
+						});
+					}*/
+
+					ts = streampckg[0];
+				}
+				
+				
+				/*else {
 					if (ts > 0) {
 						dts = streampckg[0] - ts;
 						console.log("Framerate = ", 1000/dts);
-						this.converter = new VideoConverter.default(this.element, 31, 1);
+						//this.converter = new VideoConverter.default(this.element, 31, 1);
+						
 						dts = 0;
 					}
 					ts = streampckg[0];
-				}
+				}*/
 			}
         } else if (pckg[0] === 103) {
 			//console.log(msgpack.decode(pckg[5]));
@@ -379,6 +635,8 @@ function FTLStream(peer, uri, element) {
 			this.start(0,0,0);
 		});
 	}
+
+	this.element.play();
 }
 
 FTLStream.prototype.on = function(name, cb) {
diff --git a/web-service/public/js/lib/dist/controller.d.ts b/web-service/public/js/lib/dist/controller.d.ts
deleted file mode 100644
index e9d88fa431d98d570ab9c5e6721092a7596aca00..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/controller.d.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-export declare const mimeType = "video/mp4; codecs=\"avc1.42E01E\"";
-export declare class VideoController {
-    private element;
-    private mediaSource;
-    private sourceBuffer;
-    private receiveBuffer;
-    private remuxer;
-    private mediaReady;
-    private mediaReadyPromise;
-    private queue;
-    private isFirstFrame;
-    static readonly errorNotes: {
-        [x: number]: string;
-    };
-    constructor(element: HTMLVideoElement);
-    setup(): Promise<void>;
-    play(): void;
-    pause(): void;
-    reset(): void;
-    appendRawData(data: ArrayLike<number>): void;
-    private writeFragment(dts, pay);
-    private writeBuffer(data);
-    private doAppend(data);
-}
diff --git a/web-service/public/js/lib/dist/controller.js b/web-service/public/js/lib/dist/controller.js
deleted file mode 100644
index b944a55db158280547417f5bf829b1e2e43cf5df..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/controller.js
+++ /dev/null
@@ -1,241 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_1 = require("./h264");
-var mp4_generator_1 = require("./mp4-generator");
-var NALU_1 = require("./util/NALU");
-exports.mimeType = 'video/mp4; codecs="avc1.42E01E"';
-var fps = 30;
-var fpf = 6;
-var VideoController = (function () {
-    function VideoController(element) {
-        this.element = element;
-        this.receiveBuffer = new VideoStreamBuffer();
-        this.queue = [];
-        if (!MediaSource || !MediaSource.isTypeSupported(exports.mimeType)) {
-            throw new Error("Your browser is not supported: " + exports.mimeType);
-        }
-        this.reset();
-    }
-    Object.defineProperty(VideoController, "errorNotes", {
-        get: function () {
-            return _a = {},
-                _a[MediaError.MEDIA_ERR_ABORTED] = 'fetching process aborted by user',
-                _a[MediaError.MEDIA_ERR_NETWORK] = 'error occurred when downloading',
-                _a[MediaError.MEDIA_ERR_DECODE] = 'error occurred when decoding',
-                _a[MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED] = 'audio/video not supported',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    VideoController.prototype.setup = function () {
-        var _this = this;
-        this.mediaReadyPromise = new Promise(function (resolve, _reject) {
-            _this.mediaSource.addEventListener('sourceopen', function () {
-                console.log("Media Source opened.");
-                _this.sourceBuffer = _this.mediaSource.addSourceBuffer(exports.mimeType);
-                _this.sourceBuffer.addEventListener('updateend', function () {
-                    console.log("  SourceBuffer updateend");
-                    console.log("    sourceBuffer.buffered.length=" + _this.sourceBuffer.buffered.length);
-                    for (var i = 0, len = _this.sourceBuffer.buffered.length; i < len; i++) {
-                        console.log("    sourceBuffer.buffered [" + i + "]: " + _this.sourceBuffer.buffered.start(i) + ", " + _this.sourceBuffer.buffered.end(i));
-                    }
-                    console.log("  mediasource.duration=" + _this.mediaSource.duration);
-                    console.log("  mediasource.readyState=" + _this.mediaSource.readyState);
-                    console.log("  video.duration=" + _this.element.duration);
-                    console.log("    video.buffered.length=" + _this.element.buffered.length);
-                    for (var i = 0, len = _this.element.buffered.length; i < len; i++) {
-                        console.log("    video.buffered [" + i + "]: " + _this.element.buffered.start(i) + ", " + _this.element.buffered.end(i));
-                    }
-                    console.log("  video.currentTimen=" + _this.element.currentTime);
-                    console.log("  video.readyState=" + _this.element.readyState);
-                    var data = _this.queue.shift();
-                    if (data) {
-                        _this.writeBuffer(data);
-                    }
-                });
-                _this.sourceBuffer.addEventListener('error', function () {
-                    console.error('  SourceBuffer errored!');
-                });
-                _this.mediaReady = true;
-                resolve();
-            }, false);
-            _this.mediaSource.addEventListener('sourceclose', function () {
-                console.log("Media Source closed.");
-                _this.mediaReady = false;
-            }, false);
-            _this.element.src = URL.createObjectURL(_this.mediaSource);
-        });
-        return this.mediaReadyPromise;
-    };
-    VideoController.prototype.play = function () {
-        var _this = this;
-        if (!this.element.paused) {
-            return;
-        }
-        if (this.mediaReady && this.element.readyState >= 2) {
-            this.element.play();
-        }
-        else {
-            var handler_1 = function () {
-                _this.play();
-                _this.element.removeEventListener('canplaythrough', handler_1);
-            };
-            this.element.addEventListener('canplaythrough', handler_1);
-        }
-    };
-    VideoController.prototype.pause = function () {
-        if (this.element.paused) {
-            return;
-        }
-        this.element.pause();
-    };
-    VideoController.prototype.reset = function () {
-        this.receiveBuffer.clear();
-        if (this.mediaSource && this.mediaSource.readyState === 'open') {
-            this.mediaSource.duration = 0;
-            this.mediaSource.endOfStream();
-        }
-        this.mediaSource = new MediaSource();
-        this.remuxer = new h264_1.H264Remuxer(fps, fpf, fps * 60);
-        this.mediaReady = false;
-        this.mediaReadyPromise = undefined;
-        this.queue = [];
-        this.isFirstFrame = true;
-    };
-    VideoController.prototype.appendRawData = function (data) {
-        var nalus = this.receiveBuffer.append(data);
-        for (var _i = 0, nalus_1 = nalus; _i < nalus_1.length; _i++) {
-            var nalu = nalus_1[_i];
-            var ret = this.remuxer.remux(nalu);
-            if (ret) {
-                this.writeFragment(ret[0], ret[1]);
-            }
-        }
-    };
-    VideoController.prototype.writeFragment = function (dts, pay) {
-        var remuxer = this.remuxer;
-        if (remuxer.mp4track.isKeyFrame) {
-            this.writeBuffer(mp4_generator_1.MP4.initSegment([remuxer.mp4track], Infinity, remuxer.timescale));
-        }
-        if (pay && pay.byteLength) {
-            console.log(" Put framgment: " + remuxer.seq + ", frames=" + remuxer.mp4track.samples.length + ", size=" + pay.byteLength);
-            var fragment = mp4_generator_1.MP4.fragmentSegment(remuxer.seq, dts, remuxer.mp4track, pay);
-            this.writeBuffer(fragment);
-            remuxer.flush();
-        }
-        else {
-            console.error("Nothing payload!");
-        }
-    };
-    VideoController.prototype.writeBuffer = function (data) {
-        var _this = this;
-        if (this.mediaReady) {
-            if (this.sourceBuffer.updating) {
-                this.queue.push(data);
-            }
-            else {
-                this.doAppend(data);
-            }
-        }
-        else {
-            this.queue.push(data);
-            if (this.mediaReadyPromise) {
-                this.mediaReadyPromise.then(function () {
-                    if (!_this.sourceBuffer.updating) {
-                        var d = _this.queue.shift();
-                        if (d) {
-                            _this.writeBuffer(d);
-                        }
-                    }
-                });
-                this.mediaReadyPromise = undefined;
-            }
-        }
-    };
-    VideoController.prototype.doAppend = function (data) {
-        var error = this.element.error;
-        if (error) {
-            console.error("MSE Error Occured: " + VideoController.errorNotes[error.code]);
-            this.element.pause();
-            if (this.mediaSource.readyState === 'open') {
-                this.mediaSource.endOfStream();
-            }
-        }
-        else {
-            try {
-                this.sourceBuffer.appendBuffer(data);
-                console.log("  appended buffer: size=" + data.byteLength);
-            }
-            catch (err) {
-                console.error("MSE Error occured while appending buffer. " + err.name + ": " + err.message);
-            }
-        }
-    };
-    return VideoController;
-}());
-exports.VideoController = VideoController;
-var VideoStreamBuffer = (function () {
-    function VideoStreamBuffer() {
-    }
-    VideoStreamBuffer.prototype.clear = function () {
-        this.buffer = undefined;
-    };
-    VideoStreamBuffer.prototype.append = function (value) {
-        var nextNalHeader = function (b) {
-            var i = 3;
-            return function () {
-                var count = 0;
-                for (; i < b.length; i++) {
-                    switch (b[i]) {
-                        case 0:
-                            count++;
-                            break;
-                        case 1:
-                            if (count === 3) {
-                                return i - 3;
-                            }
-                        default:
-                            count = 0;
-                    }
-                }
-                return;
-            };
-        };
-        var result = [];
-        var buffer;
-        if (this.buffer) {
-            if (value[3] === 1 && value[2] === 0 && value[1] === 0 && value[0] === 0) {
-                result.push(new NALU_1.NALU(this.buffer.subarray(4)));
-                buffer = Uint8Array.from(value);
-            }
-        }
-        if (buffer == null) {
-            buffer = this.mergeBuffer(value);
-        }
-        var index;
-        var lastIndex = 0;
-        var f = nextNalHeader(buffer);
-        while (index = f()) {
-            result.push(new NALU_1.NALU(buffer.subarray(lastIndex + 4, index)));
-            lastIndex = index;
-        }
-        this.buffer = buffer.subarray(lastIndex);
-        return result;
-    };
-    VideoStreamBuffer.prototype.mergeBuffer = function (value) {
-        if (this.buffer == null) {
-            return Uint8Array.from(value);
-        }
-        else {
-            var newBuffer = new Uint8Array(this.buffer.byteLength + value.length);
-            if (this.buffer.byteLength > 0) {
-                newBuffer.set(this.buffer, 0);
-            }
-            newBuffer.set(value, this.buffer.byteLength);
-            return newBuffer;
-        }
-    };
-    return VideoStreamBuffer;
-}());
diff --git a/web-service/public/js/lib/dist/h264-parser.d.ts b/web-service/public/js/lib/dist/h264-parser.d.ts
deleted file mode 100644
index 8bea94fb4455a7ea35f76402744136828d306d24..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264-parser.d.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import H264Remuxer from './h264-remuxer';
-import NALU from './util/NALU';
-export interface SEIMessage {
-    type: number;
-}
-export default class H264Parser {
-    private remuxer;
-    private track;
-    constructor(remuxer: H264Remuxer);
-    private parseSEI(sei);
-    private parseSPS(sps);
-    private parsePPS(pps);
-    parseNAL(unit: NALU): boolean;
-    private static skipScalingList(decoder, count);
-    private static readSPS(data);
-    private static readSEI(data);
-    private static readSEIMessage(decoder);
-    private static readSEIPayload(decoder, type, size);
-}
diff --git a/web-service/public/js/lib/dist/h264-parser.js b/web-service/public/js/lib/dist/h264-parser.js
deleted file mode 100644
index 2cb5245f880a9508d1c4560546ed05f9f1041c7f..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264-parser.js
+++ /dev/null
@@ -1,295 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var bit_stream_1 = require("./util/bit-stream");
-var debug = require("./util/debug");
-var NALU_1 = require("./util/NALU");
-var H264Parser = (function () {
-    function H264Parser(remuxer) {
-        this.remuxer = remuxer;
-        this.track = remuxer.mp4track;
-    }
-    H264Parser.prototype.parseSEI = function (sei) {
-        var messages = H264Parser.readSEI(sei);
-        for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
-            var m = messages_1[_i];
-            switch (m.type) {
-                case 0:
-                    this.track.seiBuffering = true;
-                    break;
-                case 5:
-                    return true;
-                default:
-                    break;
-            }
-        }
-        return false;
-    };
-    H264Parser.prototype.parseSPS = function (sps) {
-        var config = H264Parser.readSPS(sps);
-        this.track.width = config.width;
-        this.track.height = config.height;
-        this.track.sps = [sps];
-        this.track.codec = 'avc1.';
-        var codecArray = new DataView(sps.buffer, sps.byteOffset + 1, 4);
-        for (var i = 0; i < 3; ++i) {
-            var h = codecArray.getUint8(i).toString(16);
-            if (h.length < 2) {
-                h = '0' + h;
-            }
-            this.track.codec += h;
-        }
-    };
-    H264Parser.prototype.parsePPS = function (pps) {
-        this.track.pps = [pps];
-    };
-    H264Parser.prototype.parseNAL = function (unit) {
-        if (!unit) {
-            return false;
-        }
-        var push = false;
-        switch (unit.type()) {
-            case NALU_1.default.NDR:
-            case NALU_1.default.IDR:
-                push = true;
-                break;
-            case NALU_1.default.SEI:
-                push = this.parseSEI(unit.getData().subarray(4));
-                break;
-            case NALU_1.default.SPS:
-                if (this.track.sps.length === 0) {
-                    this.parseSPS(unit.getData().subarray(4));
-                    debug.log(" Found SPS type NALU frame.");
-                    if (!this.remuxer.readyToDecode && this.track.pps.length > 0 && this.track.sps.length > 0) {
-                        this.remuxer.readyToDecode = true;
-                    }
-                }
-                break;
-            case NALU_1.default.PPS:
-                if (this.track.pps.length === 0) {
-                    this.parsePPS(unit.getData().subarray(4));
-                    debug.log(" Found PPS type NALU frame.");
-                    if (!this.remuxer.readyToDecode && this.track.pps.length > 0 && this.track.sps.length > 0) {
-                        this.remuxer.readyToDecode = true;
-                    }
-                }
-                break;
-            default:
-                debug.log(" Found Unknown type NALU frame. type=" + unit.type());
-                break;
-        }
-        return push;
-    };
-    H264Parser.skipScalingList = function (decoder, count) {
-        var lastScale = 8;
-        var nextScale = 8;
-        for (var j = 0; j < count; j++) {
-            if (nextScale !== 0) {
-                var deltaScale = decoder.readEG();
-                nextScale = (lastScale + deltaScale + 256) % 256;
-            }
-            lastScale = (nextScale === 0) ? lastScale : nextScale;
-        }
-    };
-    H264Parser.readSPS = function (data) {
-        var decoder = new bit_stream_1.default(data);
-        var frameCropLeftOffset = 0;
-        var frameCropRightOffset = 0;
-        var frameCropTopOffset = 0;
-        var frameCropBottomOffset = 0;
-        var sarScale = 1;
-        decoder.readUByte();
-        var profileIdc = decoder.readUByte();
-        decoder.skipBits(5);
-        decoder.skipBits(3);
-        decoder.skipBits(8);
-        decoder.skipUEG();
-        if (profileIdc === 100 ||
-            profileIdc === 110 ||
-            profileIdc === 122 ||
-            profileIdc === 244 ||
-            profileIdc === 44 ||
-            profileIdc === 83 ||
-            profileIdc === 86 ||
-            profileIdc === 118 ||
-            profileIdc === 128) {
-            var chromaFormatIdc = decoder.readUEG();
-            if (chromaFormatIdc === 3) {
-                decoder.skipBits(1);
-            }
-            decoder.skipUEG();
-            decoder.skipUEG();
-            decoder.skipBits(1);
-            if (decoder.readBoolean()) {
-                var scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12;
-                for (var i = 0; i < scalingListCount; ++i) {
-                    if (decoder.readBoolean()) {
-                        if (i < 6) {
-                            H264Parser.skipScalingList(decoder, 16);
-                        }
-                        else {
-                            H264Parser.skipScalingList(decoder, 64);
-                        }
-                    }
-                }
-            }
-        }
-        decoder.skipUEG();
-        var picOrderCntType = decoder.readUEG();
-        if (picOrderCntType === 0) {
-            decoder.readUEG();
-        }
-        else if (picOrderCntType === 1) {
-            decoder.skipBits(1);
-            decoder.skipEG();
-            decoder.skipEG();
-            var numRefFramesInPicOrderCntCycle = decoder.readUEG();
-            for (var i = 0; i < numRefFramesInPicOrderCntCycle; ++i) {
-                decoder.skipEG();
-            }
-        }
-        decoder.skipUEG();
-        decoder.skipBits(1);
-        var picWidthInMbsMinus1 = decoder.readUEG();
-        var picHeightInMapUnitsMinus1 = decoder.readUEG();
-        var frameMbsOnlyFlag = decoder.readBits(1);
-        if (frameMbsOnlyFlag === 0) {
-            decoder.skipBits(1);
-        }
-        decoder.skipBits(1);
-        if (decoder.readBoolean()) {
-            frameCropLeftOffset = decoder.readUEG();
-            frameCropRightOffset = decoder.readUEG();
-            frameCropTopOffset = decoder.readUEG();
-            frameCropBottomOffset = decoder.readUEG();
-        }
-        if (decoder.readBoolean()) {
-            if (decoder.readBoolean()) {
-                var sarRatio = void 0;
-                var aspectRatioIdc = decoder.readUByte();
-                switch (aspectRatioIdc) {
-                    case 1:
-                        sarRatio = [1, 1];
-                        break;
-                    case 2:
-                        sarRatio = [12, 11];
-                        break;
-                    case 3:
-                        sarRatio = [10, 11];
-                        break;
-                    case 4:
-                        sarRatio = [16, 11];
-                        break;
-                    case 5:
-                        sarRatio = [40, 33];
-                        break;
-                    case 6:
-                        sarRatio = [24, 11];
-                        break;
-                    case 7:
-                        sarRatio = [20, 11];
-                        break;
-                    case 8:
-                        sarRatio = [32, 11];
-                        break;
-                    case 9:
-                        sarRatio = [80, 33];
-                        break;
-                    case 10:
-                        sarRatio = [18, 11];
-                        break;
-                    case 11:
-                        sarRatio = [15, 11];
-                        break;
-                    case 12:
-                        sarRatio = [64, 33];
-                        break;
-                    case 13:
-                        sarRatio = [160, 99];
-                        break;
-                    case 14:
-                        sarRatio = [4, 3];
-                        break;
-                    case 15:
-                        sarRatio = [3, 2];
-                        break;
-                    case 16:
-                        sarRatio = [2, 1];
-                        break;
-                    case 255: {
-                        sarRatio = [decoder.readUByte() << 8 | decoder.readUByte(), decoder.readUByte() << 8 | decoder.readUByte()];
-                        break;
-                    }
-                    default: {
-                        debug.error("  H264: Unknown aspectRatioIdc=" + aspectRatioIdc);
-                    }
-                }
-                if (sarRatio) {
-                    sarScale = sarRatio[0] / sarRatio[1];
-                }
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipBits(1);
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipBits(4);
-                if (decoder.readBoolean()) {
-                    decoder.skipBits(24);
-                }
-            }
-            if (decoder.readBoolean()) {
-                decoder.skipUEG();
-                decoder.skipUEG();
-            }
-            if (decoder.readBoolean()) {
-                var unitsInTick = decoder.readUInt();
-                var timeScale = decoder.readUInt();
-                var fixedFrameRate = decoder.readBoolean();
-                var frameDuration = timeScale / (2 * unitsInTick);
-                debug.log("timescale: " + timeScale + "; unitsInTick: " + unitsInTick + "; " +
-                    ("fixedFramerate: " + fixedFrameRate + "; avgFrameDuration: " + frameDuration));
-            }
-        }
-        return {
-            width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
-            height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) -
-                ((frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)),
-        };
-    };
-    H264Parser.readSEI = function (data) {
-        var decoder = new bit_stream_1.default(data);
-        decoder.skipBits(8);
-        var result = [];
-        while (decoder.bitsAvailable > 3 * 8) {
-            result.push(this.readSEIMessage(decoder));
-        }
-        return result;
-    };
-    H264Parser.readSEIMessage = function (decoder) {
-        function get() {
-            var result = 0;
-            while (true) {
-                var value = decoder.readUByte();
-                result += value;
-                if (value !== 0xff) {
-                    break;
-                }
-            }
-            return result;
-        }
-        var payloadType = get();
-        var payloadSize = get();
-        return this.readSEIPayload(decoder, payloadType, payloadSize);
-    };
-    H264Parser.readSEIPayload = function (decoder, type, size) {
-        var result;
-        switch (type) {
-            default:
-                result = { type: type };
-                decoder.skipBits(size * 8);
-        }
-        decoder.skipBits(decoder.bitsAvailable % 8);
-        return result;
-    };
-    return H264Parser;
-}());
-exports.default = H264Parser;
diff --git a/web-service/public/js/lib/dist/h264-remuxer.d.ts b/web-service/public/js/lib/dist/h264-remuxer.d.ts
deleted file mode 100644
index 0042feddbb3ab7ffc01027f330988d477e7c8f91..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264-remuxer.d.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { Track } from './types';
-import NALU from './util/NALU';
-export default class H264Remuxer {
-    fps: number;
-    framePerFragment: number;
-    timescale: number;
-    readyToDecode: boolean;
-    private totalDTS;
-    private stepDTS;
-    private frameCount;
-    private seq;
-    mp4track: Track;
-    private unitSamples;
-    private parser;
-    private static getTrackID();
-    constructor(fps: number, framePerFragment: number, timescale: number);
-    readonly seqNum: number;
-    remux(nalu: NALU): [number, Uint8Array] | undefined;
-    private createNextFrame();
-    flush(): void;
-    private getFragment();
-    private checkReadyToDecode();
-}
diff --git a/web-service/public/js/lib/dist/h264-remuxer.js b/web-service/public/js/lib/dist/h264-remuxer.js
deleted file mode 100644
index f3d181c811e3bb19da2415c2fce5315b500a0f71..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264-remuxer.js
+++ /dev/null
@@ -1,121 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_parser_1 = require("./h264-parser");
-var debug = require("./util/debug");
-var NALU_1 = require("./util/NALU");
-var trackId = 1;
-var H264Remuxer = (function () {
-    function H264Remuxer(fps, framePerFragment, timescale) {
-        this.fps = fps;
-        this.framePerFragment = framePerFragment;
-        this.timescale = timescale;
-        this.readyToDecode = false;
-        this.totalDTS = 0;
-        this.stepDTS = Math.round(this.timescale / this.fps);
-        this.frameCount = 0;
-        this.seq = 1;
-        this.mp4track = {
-            id: H264Remuxer.getTrackID(),
-            type: 'video',
-            len: 0,
-            codec: '',
-            sps: [],
-            pps: [],
-            seiBuffering: false,
-            width: 0,
-            height: 0,
-            timescale: timescale,
-            duration: timescale,
-            samples: [],
-            isKeyFrame: true,
-        };
-        this.unitSamples = [[]];
-        this.parser = new h264_parser_1.default(this);
-    }
-    H264Remuxer.getTrackID = function () {
-        return trackId++;
-    };
-    Object.defineProperty(H264Remuxer.prototype, "seqNum", {
-        get: function () {
-            return this.seq;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    H264Remuxer.prototype.remux = function (nalu) {
-        if (this.mp4track.seiBuffering && nalu.type() === NALU_1.default.SEI) {
-            return this.createNextFrame();
-        }
-        if (this.parser.parseNAL(nalu)) {
-            this.unitSamples[this.unitSamples.length - 1].push(nalu);
-            this.mp4track.len += nalu.getSize();
-        }
-        if (!this.mp4track.seiBuffering && (nalu.type() === NALU_1.default.IDR || nalu.type() === NALU_1.default.NDR)) {
-            return this.createNextFrame();
-        }
-        return;
-    };
-    H264Remuxer.prototype.createNextFrame = function () {
-        if (this.mp4track.len > 0) {
-            this.frameCount++;
-            if (this.frameCount % this.framePerFragment === 0) {
-                var fragment = this.getFragment();
-                if (fragment) {
-                    var dts = this.totalDTS;
-                    this.totalDTS = this.stepDTS * this.frameCount;
-                    return [dts, fragment];
-                }
-                else {
-                    debug.log("No mp4 sample data.");
-                }
-            }
-            this.unitSamples.push([]);
-        }
-        return;
-    };
-    H264Remuxer.prototype.flush = function () {
-        this.seq++;
-        this.mp4track.len = 0;
-        this.mp4track.samples = [];
-        this.mp4track.isKeyFrame = false;
-        this.unitSamples = [[]];
-    };
-    H264Remuxer.prototype.getFragment = function () {
-        if (!this.checkReadyToDecode()) {
-            return undefined;
-        }
-        var payload = new Uint8Array(this.mp4track.len);
-        this.mp4track.samples = [];
-        var offset = 0;
-        for (var i = 0, len = this.unitSamples.length; i < len; i++) {
-            var units = this.unitSamples[i];
-            if (units.length === 0) {
-                continue;
-            }
-            var mp4Sample = {
-                size: 0,
-                cts: this.stepDTS * i,
-            };
-            for (var _i = 0, units_1 = units; _i < units_1.length; _i++) {
-                var unit = units_1[_i];
-                mp4Sample.size += unit.getSize();
-                payload.set(unit.getData(), offset);
-                offset += unit.getSize();
-            }
-            this.mp4track.samples.push(mp4Sample);
-        }
-        if (offset === 0) {
-            return undefined;
-        }
-        return payload;
-    };
-    H264Remuxer.prototype.checkReadyToDecode = function () {
-        if (!this.readyToDecode || this.unitSamples.filter(function (array) { return array.length > 0; }).length === 0) {
-            debug.log("Not ready to decode! readyToDecode(" + this.readyToDecode + ") is false or units is empty.");
-            return false;
-        }
-        return true;
-    };
-    return H264Remuxer;
-}());
-exports.default = H264Remuxer;
diff --git a/web-service/public/js/lib/dist/h264.d.ts b/web-service/public/js/lib/dist/h264.d.ts
deleted file mode 100644
index 6b55d9904fbe5b408ae079622fdc2ed1c6e2f80a..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264.d.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { Track } from './types';
-import { NALU } from './util/NALU';
-export default class H264Remuxer {
-    fps: number;
-    framePerFragment: number;
-    timescale: number;
-    readyToDecode: boolean;
-    private totalDTS;
-    private stepDTS;
-    private frameCount;
-    private seq;
-    mp4track: Track;
-    private unitSamples;
-    private parser;
-    private static getTrackID();
-    constructor(fps: number, framePerFragment: number, timescale: number);
-    readonly seqNum: number;
-    remux(nalu: NALU): [number, Uint8Array] | undefined;
-    private createNextFrame();
-    flush(): void;
-    private getFragment();
-    private checkReadyToDecode();
-}
diff --git a/web-service/public/js/lib/dist/h264.js b/web-service/public/js/lib/dist/h264.js
deleted file mode 100644
index 206ad79f4a12725e18662d249e0d3fb64d13616d..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/h264.js
+++ /dev/null
@@ -1,118 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_parser_1 = require("./h264-parser");
-var NALU_1 = require("./util/NALU");
-var trackId = 1;
-var H264Remuxer = (function () {
-    function H264Remuxer(fps, framePerFragment, timescale) {
-        this.fps = fps;
-        this.framePerFragment = framePerFragment;
-        this.timescale = timescale;
-        this.readyToDecode = false;
-        this.totalDTS = 0;
-        this.stepDTS = Math.round(this.timescale / this.fps);
-        this.frameCount = 0;
-        this.seq = 1;
-        this.mp4track = {
-            id: H264Remuxer.getTrackID(),
-            type: 'video',
-            len: 0,
-            codec: '',
-            sps: [],
-            pps: [],
-            seiBuffering: false,
-            width: 0,
-            height: 0,
-            timescale: timescale,
-            duration: timescale,
-            samples: [],
-            isKeyFrame: true,
-        };
-        this.unitSamples = [[]];
-        this.parser = new h264_parser_1.H264Parser(this);
-    }
-    H264Remuxer.getTrackID = function () {
-        return trackId++;
-    };
-    Object.defineProperty(H264Remuxer.prototype, "seqNum", {
-        get: function () {
-            return this.seq;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    H264Remuxer.prototype.remux = function (nalu) {
-        if (this.mp4track.seiBuffering && nalu.type() === NALU_1.NALU.SEI) {
-            return this.createNextFrame();
-        }
-        if (this.parser.parseNAL(nalu)) {
-            this.unitSamples[this.unitSamples.length - 1].push(nalu);
-            this.mp4track.len += nalu.getSize();
-        }
-        if (!this.mp4track.seiBuffering && (nalu.type() === NALU_1.NALU.IDR || nalu.type() === NALU_1.NALU.NDR)) {
-            return this.createNextFrame();
-        }
-        return;
-    };
-    H264Remuxer.prototype.createNextFrame = function () {
-        if (this.mp4track.len > 0) {
-            this.frameCount++;
-            if (this.frameCount % this.framePerFragment === 0) {
-                var fragment = this.getFragment();
-                if (fragment) {
-                    var dts = this.totalDTS;
-                    this.totalDTS = this.stepDTS * this.frameCount;
-                    return [dts, fragment];
-                }
-            }
-            this.unitSamples.push([]);
-        }
-        return;
-    };
-    H264Remuxer.prototype.flush = function () {
-        this.seq++;
-        this.mp4track.len = 0;
-        this.mp4track.samples = [];
-        this.mp4track.isKeyFrame = false;
-        this.unitSamples = [[]];
-    };
-    H264Remuxer.prototype.getFragment = function () {
-        if (!this.checkReadyToDecode()) {
-            return undefined;
-        }
-        var payload = new Uint8Array(this.mp4track.len);
-        this.mp4track.samples = [];
-        var offset = 0;
-        for (var i = 0, len = this.unitSamples.length; i < len; i++) {
-            var units = this.unitSamples[i];
-            if (units.length === 0) {
-                continue;
-            }
-            var mp4Sample = {
-                size: 0,
-                cts: this.stepDTS * i,
-            };
-            for (var _i = 0, units_1 = units; _i < units_1.length; _i++) {
-                var unit = units_1[_i];
-                mp4Sample.size += unit.getSize();
-                payload.set(unit.getData(), offset);
-                offset += unit.getSize();
-            }
-            this.mp4track.samples.push(mp4Sample);
-        }
-        if (offset === 0) {
-            console.log("No mp4 sample data.");
-            return undefined;
-        }
-        return payload;
-    };
-    H264Remuxer.prototype.checkReadyToDecode = function () {
-        if (!this.readyToDecode || this.unitSamples.filter(function (array) { return array.length > 0; }).length === 0) {
-            console.log("Not ready to decode! readyToDecode(" + this.readyToDecode + ") is false or units is empty.");
-            return false;
-        }
-        return true;
-    };
-    return H264Remuxer;
-}());
-exports.default = H264Remuxer;
diff --git a/web-service/public/js/lib/dist/index.d.ts b/web-service/public/js/lib/dist/index.d.ts
deleted file mode 100644
index 12f0901b72982089e0fcdaab54fd95f6dcccb58c..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/index.d.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-export declare const mimeType = "video/mp4; codecs=\"avc1.42E01E\"";
-export { setLogger } from './util/debug';
-export default class VideoConverter {
-    private element;
-    private fps;
-    private fpf;
-    private mediaSource;
-    private sourceBuffer;
-    private receiveBuffer;
-    private remuxer;
-    private mediaReady;
-    private mediaReadyPromise;
-    private queue;
-    private isFirstFrame;
-    static readonly errorNotes: {
-        [x: number]: string;
-    };
-    constructor(element: HTMLVideoElement, fps?: number, fpf?: number);
-    private setup();
-    play(): void;
-    pause(): void;
-    reset(): void;
-    appendRawData(data: ArrayLike<number>): void;
-    private writeFragment(dts, pay);
-    private writeBuffer(data);
-    private doAppend(data);
-}
diff --git a/web-service/public/js/lib/dist/index.js b/web-service/public/js/lib/dist/index.js
deleted file mode 100644
index cefdec014662f8021818d7618c37bbaa28277787..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/index.js
+++ /dev/null
@@ -1,187 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_remuxer_1 = require("./h264-remuxer");
-var mp4_generator_1 = require("./mp4-generator");
-var debug = require("./util/debug");
-var nalu_stream_buffer_1 = require("./util/nalu-stream-buffer");
-exports.mimeType = 'video/mp4; codecs="avc1.42E01E"';
-var debug_1 = require("./util/debug");
-exports.setLogger = debug_1.setLogger;
-var VideoConverter = (function () {
-    function VideoConverter(element, fps, fpf) {
-        if (fps === void 0) { fps = 60; }
-        if (fpf === void 0) { fpf = fps; }
-        this.element = element;
-        this.fps = fps;
-        this.fpf = fpf;
-        this.receiveBuffer = new nalu_stream_buffer_1.default();
-        this.queue = [];
-        if (!MediaSource || !MediaSource.isTypeSupported(exports.mimeType)) {
-            throw new Error("Your browser is not supported: " + exports.mimeType);
-        }
-        this.reset();
-    }
-    Object.defineProperty(VideoConverter, "errorNotes", {
-        get: function () {
-            return _a = {},
-                _a[MediaError.MEDIA_ERR_ABORTED] = 'fetching process aborted by user',
-                _a[MediaError.MEDIA_ERR_NETWORK] = 'error occurred when downloading',
-                _a[MediaError.MEDIA_ERR_DECODE] = 'error occurred when decoding',
-                _a[MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED] = 'audio/video not supported',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    VideoConverter.prototype.setup = function () {
-        var _this = this;
-        this.mediaReadyPromise = new Promise(function (resolve, _reject) {
-            _this.mediaSource.addEventListener('sourceopen', function () {
-                debug.log("Media Source opened.");
-                _this.sourceBuffer = _this.mediaSource.addSourceBuffer(exports.mimeType);
-                _this.sourceBuffer.addEventListener('updateend', function () {
-                    debug.log("  SourceBuffer updateend");
-                    debug.log("    sourceBuffer.buffered.length=" + _this.sourceBuffer.buffered.length);
-                    for (var i = 0, len = _this.sourceBuffer.buffered.length; i < len; i++) {
-                        debug.log("    sourceBuffer.buffered [" + i + "]: " +
-                            (_this.sourceBuffer.buffered.start(i) + ", " + _this.sourceBuffer.buffered.end(i)));
-                    }
-                    debug.log("  mediasource.duration=" + _this.mediaSource.duration);
-                    debug.log("  mediasource.readyState=" + _this.mediaSource.readyState);
-                    debug.log("  video.duration=" + _this.element.duration);
-                    debug.log("    video.buffered.length=" + _this.element.buffered.length);
-                    if (debug.isEnable()) {
-                        for (var i = 0, len = _this.element.buffered.length; i < len; i++) {
-                            debug.log("    video.buffered [" + i + "]: " + _this.element.buffered.start(i) + ", " + _this.element.buffered.end(i));
-                        }
-                    }
-                    debug.log("  video.currentTime=" + _this.element.currentTime);
-                    debug.log("  video.readyState=" + _this.element.readyState);
-                    var data = _this.queue.shift();
-                    if (data) {
-                        _this.writeBuffer(data);
-                    }
-                });
-                _this.sourceBuffer.addEventListener('error', function () {
-                    debug.error('  SourceBuffer errored!');
-                });
-                _this.mediaReady = true;
-                resolve();
-            }, false);
-            _this.mediaSource.addEventListener('sourceclose', function () {
-                debug.log("Media Source closed.");
-                _this.mediaReady = false;
-            }, false);
-            _this.element.src = URL.createObjectURL(_this.mediaSource);
-        });
-        return this.mediaReadyPromise;
-    };
-    VideoConverter.prototype.play = function () {
-        var _this = this;
-        if (!this.element.paused) {
-            return;
-        }
-        if (this.mediaReady && this.element.readyState >= 2) {
-            this.element.play();
-        }
-        else {
-            var handler_1 = function () {
-                _this.play();
-                _this.element.removeEventListener('canplaythrough', handler_1);
-            };
-            this.element.addEventListener('canplaythrough', handler_1);
-        }
-    };
-    VideoConverter.prototype.pause = function () {
-        if (this.element.paused) {
-            return;
-        }
-        this.element.pause();
-    };
-    VideoConverter.prototype.reset = function () {
-        this.receiveBuffer.clear();
-        if (this.mediaSource && this.mediaSource.readyState === 'open') {
-            this.mediaSource.duration = 0;
-            this.mediaSource.endOfStream();
-        }
-        this.mediaSource = new MediaSource();
-        this.remuxer = new h264_remuxer_1.default(this.fps, this.fpf, this.fps * 60);
-        this.mediaReady = false;
-        this.mediaReadyPromise = undefined;
-        this.queue = [];
-        this.isFirstFrame = true;
-        this.setup();
-    };
-    VideoConverter.prototype.appendRawData = function (data) {
-        var nalus = this.receiveBuffer.append(data);
-        for (var _i = 0, nalus_1 = nalus; _i < nalus_1.length; _i++) {
-            var nalu = nalus_1[_i];
-            var ret = this.remuxer.remux(nalu);
-            if (ret) {
-                this.writeFragment(ret[0], ret[1]);
-            }
-        }
-    };
-    VideoConverter.prototype.writeFragment = function (dts, pay) {
-        var remuxer = this.remuxer;
-        if (remuxer.mp4track.isKeyFrame) {
-            this.writeBuffer(mp4_generator_1.default.initSegment([remuxer.mp4track], Infinity, remuxer.timescale));
-        }
-        if (pay && pay.byteLength) {
-            debug.log(" Put fragment: " + remuxer.seqNum + ", frames=" + remuxer.mp4track.samples.length + ", size=" + pay.byteLength);
-            var fragment = mp4_generator_1.default.fragmentSegment(remuxer.seqNum, dts, remuxer.mp4track, pay);
-            this.writeBuffer(fragment);
-            remuxer.flush();
-        }
-        else {
-            debug.error("Nothing payload!");
-        }
-    };
-    VideoConverter.prototype.writeBuffer = function (data) {
-        var _this = this;
-        if (this.mediaReady) {
-            if (this.sourceBuffer.updating) {
-                this.queue.push(data);
-            }
-            else {
-                this.doAppend(data);
-            }
-        }
-        else {
-            this.queue.push(data);
-            if (this.mediaReadyPromise) {
-                this.mediaReadyPromise.then(function () {
-                    if (!_this.sourceBuffer.updating) {
-                        var d = _this.queue.shift();
-                        if (d) {
-                            _this.writeBuffer(d);
-                        }
-                    }
-                });
-                this.mediaReadyPromise = undefined;
-            }
-        }
-    };
-    VideoConverter.prototype.doAppend = function (data) {
-        var error = this.element.error;
-        if (error) {
-            debug.error("MSE Error Occured: " + VideoConverter.errorNotes[error.code]);
-            this.element.pause();
-            if (this.mediaSource.readyState === 'open') {
-                this.mediaSource.endOfStream();
-            }
-        }
-        else {
-            try {
-                this.sourceBuffer.appendBuffer(data);
-                debug.log("  appended buffer: size=" + data.byteLength);
-            }
-            catch (err) {
-                debug.error("MSE Error occured while appending buffer. " + err.name + ": " + err.message);
-            }
-        }
-    };
-    return VideoConverter;
-}());
-exports.default = VideoConverter;
diff --git a/web-service/public/js/lib/dist/mp4-generator.d.ts b/web-service/public/js/lib/dist/mp4-generator.d.ts
deleted file mode 100644
index c3ac851fa3c1c3ec72fe3918fe0552379abe2a42..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/mp4-generator.d.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { Track } from './types';
-export default class MP4 {
-    private static types;
-    private static initalized;
-    private static FTYP;
-    private static HDLR;
-    private static DINF;
-    private static STSD;
-    private static SMHD;
-    private static VMHD;
-    private static STSZ;
-    private static STTS;
-    private static STSC;
-    private static STCO;
-    private static STYP;
-    private static init();
-    static box(type: number[], ...payload: Uint8Array[]): Uint8Array;
-    static mdat(data: Uint8Array): Uint8Array;
-    static mdhd(timescale: number): Uint8Array;
-    static mdia(track: Track): Uint8Array;
-    static mfhd(sequenceNumber: number): Uint8Array;
-    static minf(track: Track): Uint8Array;
-    static moof(sn: number, baseMediaDecodeTime: number, track: Track): Uint8Array;
-    static moov(tracks: Track[], duration: number, timescale: number): Uint8Array;
-    static mvhd(timescale: number, duration: number): Uint8Array;
-    static mvex(tracks: Track[]): Uint8Array;
-    static trep(): Uint8Array;
-    static stbl(track: Track): Uint8Array;
-    static avc1(track: Track): Uint8Array;
-    static stsd(track: Track): Uint8Array;
-    static tkhd(track: Track): Uint8Array;
-    static traf(track: Track, baseMediaDecodeTime: number): Uint8Array;
-    static trak(track: Track): Uint8Array;
-    static trex(track: Track): Uint8Array;
-    static trun(track: Track, offset: number): Uint8Array;
-    static initSegment(tracks: Track[], duration: number, timescale: number): Uint8Array;
-    static fragmentSegment(sn: number, baseMediaDecodeTime: number, track: Track, payload: Uint8Array): Uint8Array;
-}
diff --git a/web-service/public/js/lib/dist/mp4-generator.js b/web-service/public/js/lib/dist/mp4-generator.js
deleted file mode 100644
index a91748998c479aa7a398b66379c8a0c0bfdd43b0..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/mp4-generator.js
+++ /dev/null
@@ -1,454 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var MP4 = (function () {
-    function MP4() {
-    }
-    MP4.init = function () {
-        MP4.initalized = true;
-        MP4.types = {
-            avc1: [],
-            avcC: [],
-            btrt: [],
-            dinf: [],
-            dref: [],
-            esds: [],
-            ftyp: [],
-            hdlr: [],
-            mdat: [],
-            mdhd: [],
-            mdia: [],
-            mfhd: [],
-            minf: [],
-            moof: [],
-            moov: [],
-            mp4a: [],
-            mvex: [],
-            mvhd: [],
-            sdtp: [],
-            stbl: [],
-            stco: [],
-            stsc: [],
-            stsd: [],
-            stsz: [],
-            stts: [],
-            styp: [],
-            tfdt: [],
-            tfhd: [],
-            traf: [],
-            trak: [],
-            trun: [],
-            trep: [],
-            trex: [],
-            tkhd: [],
-            vmhd: [],
-            smhd: [],
-        };
-        for (var type in MP4.types) {
-            if (MP4.types.hasOwnProperty(type)) {
-                MP4.types[type] = [
-                    type.charCodeAt(0),
-                    type.charCodeAt(1),
-                    type.charCodeAt(2),
-                    type.charCodeAt(3),
-                ];
-            }
-        }
-        var hdlr = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x76, 0x69, 0x64, 0x65,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x56, 0x69, 0x64, 0x65,
-            0x6f, 0x48, 0x61, 0x6e,
-            0x64, 0x6c, 0x65, 0x72, 0x00,
-        ]);
-        var dref = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x0c,
-            0x75, 0x72, 0x6c, 0x20,
-            0x00,
-            0x00, 0x00, 0x01,
-        ]);
-        var stco = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-        ]);
-        MP4.STTS = MP4.STSC = MP4.STCO = stco;
-        MP4.STSZ = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-        ]);
-        MP4.VMHD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x01,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-        ]);
-        MP4.SMHD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-        ]);
-        MP4.STSD = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01
-        ]);
-        MP4.FTYP = MP4.box(MP4.types.ftyp, new Uint8Array([
-            0x69, 0x73, 0x6f, 0x35,
-            0x00, 0x00, 0x00, 0x01,
-            0x61, 0x76, 0x63, 0x31,
-            0x69, 0x73, 0x6f, 0x35,
-            0x64, 0x61, 0x73, 0x68,
-        ]));
-        MP4.STYP = MP4.box(MP4.types.styp, new Uint8Array([
-            0x6d, 0x73, 0x64, 0x68,
-            0x00, 0x00, 0x00, 0x00,
-            0x6d, 0x73, 0x64, 0x68,
-            0x6d, 0x73, 0x69, 0x78,
-        ]));
-        MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
-        MP4.HDLR = MP4.box(MP4.types.hdlr, hdlr);
-    };
-    MP4.box = function (type) {
-        var payload = [];
-        for (var _i = 1; _i < arguments.length; _i++) {
-            payload[_i - 1] = arguments[_i];
-        }
-        var size = 8;
-        for (var _a = 0, payload_1 = payload; _a < payload_1.length; _a++) {
-            var p = payload_1[_a];
-            size += p.byteLength;
-        }
-        var result = new Uint8Array(size);
-        result[0] = (size >> 24) & 0xff;
-        result[1] = (size >> 16) & 0xff;
-        result[2] = (size >> 8) & 0xff;
-        result[3] = size & 0xff;
-        result.set(type, 4);
-        size = 8;
-        for (var _b = 0, payload_2 = payload; _b < payload_2.length; _b++) {
-            var box = payload_2[_b];
-            result.set(box, size);
-            size += box.byteLength;
-        }
-        return result;
-    };
-    MP4.mdat = function (data) {
-        return MP4.box(MP4.types.mdat, data);
-    };
-    MP4.mdhd = function (timescale) {
-        return MP4.box(MP4.types.mdhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (timescale >> 24) & 0xFF,
-            (timescale >> 16) & 0xFF,
-            (timescale >> 8) & 0xFF,
-            timescale & 0xFF,
-            0x00, 0x00, 0x00, 0x00,
-            0x55, 0xc4,
-            0x00, 0x00,
-        ]));
-    };
-    MP4.mdia = function (track) {
-        return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale), MP4.HDLR, MP4.minf(track));
-    };
-    MP4.mfhd = function (sequenceNumber) {
-        return MP4.box(MP4.types.mfhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (sequenceNumber >> 24),
-            (sequenceNumber >> 16) & 0xFF,
-            (sequenceNumber >> 8) & 0xFF,
-            sequenceNumber & 0xFF,
-        ]));
-    };
-    MP4.minf = function (track) {
-        return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
-    };
-    MP4.moof = function (sn, baseMediaDecodeTime, track) {
-        return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
-    };
-    MP4.moov = function (tracks, duration, timescale) {
-        var boxes = [];
-        for (var _i = 0, tracks_1 = tracks; _i < tracks_1.length; _i++) {
-            var track = tracks_1[_i];
-            boxes.push(MP4.trak(track));
-        }
-        return MP4.box.apply(MP4, [MP4.types.moov, MP4.mvhd(timescale, duration), MP4.mvex(tracks)].concat(boxes));
-    };
-    MP4.mvhd = function (timescale, duration) {
-        var bytes = new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (timescale >> 24) & 0xFF,
-            (timescale >> 16) & 0xFF,
-            (timescale >> 8) & 0xFF,
-            timescale & 0xFF,
-            (duration >> 24) & 0xFF,
-            (duration >> 16) & 0xFF,
-            (duration >> 8) & 0xFF,
-            duration & 0xFF,
-            0x00, 0x01, 0x00, 0x00,
-            0x01, 0x00,
-            0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x40, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x02,
-        ]);
-        return MP4.box(MP4.types.mvhd, bytes);
-    };
-    MP4.mvex = function (tracks) {
-        var boxes = [];
-        for (var _i = 0, tracks_2 = tracks; _i < tracks_2.length; _i++) {
-            var track = tracks_2[_i];
-            boxes.push(MP4.trex(track));
-        }
-        return MP4.box.apply(MP4, [MP4.types.mvex].concat(boxes, [MP4.trep()]));
-    };
-    MP4.trep = function () {
-        return MP4.box(MP4.types.trep, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x01,
-        ]));
-    };
-    MP4.stbl = function (track) {
-        return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
-    };
-    MP4.avc1 = function (track) {
-        var sps = [];
-        var pps = [];
-        for (var _i = 0, _a = track.sps; _i < _a.length; _i++) {
-            var data = _a[_i];
-            var len = data.byteLength;
-            sps.push((len >>> 8) & 0xFF);
-            sps.push((len & 0xFF));
-            sps = sps.concat(Array.prototype.slice.call(data));
-        }
-        for (var _b = 0, _c = track.pps; _b < _c.length; _b++) {
-            var data = _c[_b];
-            var len = data.byteLength;
-            pps.push((len >>> 8) & 0xFF);
-            pps.push((len & 0xFF));
-            pps = pps.concat(Array.prototype.slice.call(data));
-        }
-        var avcc = MP4.box(MP4.types.avcC, new Uint8Array([
-            0x01,
-            sps[3],
-            sps[4],
-            sps[5],
-            0xfc | 3,
-            0xE0 | track.sps.length,
-        ].concat(sps).concat([
-            track.pps.length,
-        ]).concat(pps)));
-        var width = track.width;
-        var height = track.height;
-        return MP4.box(MP4.types.avc1, new Uint8Array([
-            0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x01,
-            0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            (width >> 8) & 0xFF,
-            width & 0xff,
-            (height >> 8) & 0xFF,
-            height & 0xff,
-            0x00, 0x48, 0x00, 0x00,
-            0x00, 0x48, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01,
-            0x12,
-            0x62, 0x69, 0x6E, 0x65,
-            0x6C, 0x70, 0x72, 0x6F,
-            0x2E, 0x72, 0x75, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00,
-            0x00, 0x18,
-            0x11, 0x11
-        ]), avcc, MP4.box(MP4.types.btrt, new Uint8Array([
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x2d, 0xc6, 0xc0,
-            0x00, 0x2d, 0xc6, 0xc0,
-        ])));
-    };
-    MP4.stsd = function (track) {
-        return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
-    };
-    MP4.tkhd = function (track) {
-        var id = track.id;
-        var width = track.width;
-        var height = track.height;
-        return MP4.box(MP4.types.tkhd, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x02,
-            (id >> 24) & 0xFF,
-            (id >> 16) & 0xFF,
-            (id >> 8) & 0xFF,
-            id & 0xFF,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00,
-            0x00, 0x00,
-            (track.type === 'audio' ? 0x01 : 0x00), 0x00,
-            0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0x40, 0x00, 0x00, 0x00,
-            (width >> 8) & 0xFF,
-            width & 0xFF,
-            0x00, 0x00,
-            (height >> 8) & 0xFF,
-            height & 0xFF,
-            0x00, 0x00,
-        ]));
-    };
-    MP4.traf = function (track, baseMediaDecodeTime) {
-        var id = track.id;
-        return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([
-            0x00,
-            0x02, 0x00, 0x00,
-            (id >> 24),
-            (id >> 16) & 0XFF,
-            (id >> 8) & 0XFF,
-            (id & 0xFF),
-        ])), MP4.box(MP4.types.tfdt, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (baseMediaDecodeTime >> 24),
-            (baseMediaDecodeTime >> 16) & 0XFF,
-            (baseMediaDecodeTime >> 8) & 0XFF,
-            (baseMediaDecodeTime & 0xFF),
-        ])), MP4.trun(track, 16 +
-            16 +
-            8 +
-            16 +
-            8 +
-            8));
-    };
-    MP4.trak = function (track) {
-        track.duration = track.duration || 0xffffffff;
-        return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
-    };
-    MP4.trex = function (track) {
-        var id = track.id;
-        return MP4.box(MP4.types.trex, new Uint8Array([
-            0x00,
-            0x00, 0x00, 0x00,
-            (id >> 24),
-            (id >> 16) & 0XFF,
-            (id >> 8) & 0XFF,
-            (id & 0xFF),
-            0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x3c,
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x01, 0x00, 0x00,
-        ]));
-    };
-    MP4.trun = function (track, offset) {
-        var samples = track.samples || [];
-        var len = samples.length;
-        var additionalLen = track.isKeyFrame ? 4 : 0;
-        var arraylen = 12 + additionalLen + (4 * len);
-        var array = new Uint8Array(arraylen);
-        offset += 8 + arraylen;
-        array.set([
-            0x00,
-            0x00, 0x02, (track.isKeyFrame ? 0x05 : 0x01),
-            (len >>> 24) & 0xFF,
-            (len >>> 16) & 0xFF,
-            (len >>> 8) & 0xFF,
-            len & 0xFF,
-            (offset >>> 24) & 0xFF,
-            (offset >>> 16) & 0xFF,
-            (offset >>> 8) & 0xFF,
-            offset & 0xFF,
-        ], 0);
-        if (track.isKeyFrame) {
-            array.set([
-                0x00, 0x00, 0x00, 0x00,
-            ], 12);
-        }
-        for (var i = 0; i < len; i++) {
-            var sample = samples[i];
-            var size = sample.size;
-            array.set([
-                (size >>> 24) & 0xFF,
-                (size >>> 16) & 0xFF,
-                (size >>> 8) & 0xFF,
-                size & 0xFF,
-            ], 12 + additionalLen + 4 * i);
-        }
-        return MP4.box(MP4.types.trun, array);
-    };
-    MP4.initSegment = function (tracks, duration, timescale) {
-        if (!MP4.initalized) {
-            MP4.init();
-        }
-        var movie = MP4.moov(tracks, duration, timescale);
-        var result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
-        result.set(MP4.FTYP);
-        result.set(movie, MP4.FTYP.byteLength);
-        return result;
-    };
-    MP4.fragmentSegment = function (sn, baseMediaDecodeTime, track, payload) {
-        var moof = MP4.moof(sn, baseMediaDecodeTime, track);
-        var mdat = MP4.mdat(payload);
-        var result = new Uint8Array(MP4.STYP.byteLength + moof.byteLength + mdat.byteLength);
-        result.set(MP4.STYP);
-        result.set(moof, MP4.STYP.byteLength);
-        result.set(mdat, MP4.STYP.byteLength + moof.byteLength);
-        return result;
-    };
-    return MP4;
-}());
-MP4.types = {};
-MP4.initalized = false;
-exports.default = MP4;
diff --git a/web-service/public/js/lib/dist/types.d.ts b/web-service/public/js/lib/dist/types.d.ts
deleted file mode 100644
index 9805ff829dd34868f2bf97357420c37044b5b8fb..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/types.d.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-export interface Track {
-    id: number;
-    type: 'video' | 'audio';
-    len: number;
-    codec: string;
-    sps: Uint8Array[];
-    pps: Uint8Array[];
-    seiBuffering: boolean;
-    width: number;
-    height: number;
-    timescale: number;
-    duration: number;
-    samples: TrackSample[];
-    isKeyFrame: boolean;
-}
-export interface TrackSample {
-    size: number;
-}
diff --git a/web-service/public/js/lib/dist/types.js b/web-service/public/js/lib/dist/types.js
deleted file mode 100644
index c8ad2e549bdc6801e0d1c80b0308d4b9bd4985ce..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/types.js
+++ /dev/null
@@ -1,2 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
diff --git a/web-service/public/js/lib/dist/util/NALU.d.ts b/web-service/public/js/lib/dist/util/NALU.d.ts
deleted file mode 100644
index 10d1b657a1c59ca9a35d465c7d5cf09bfa802017..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/NALU.d.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export default class NALU {
-    data: Uint8Array;
-    nri: number;
-    ntype: number;
-    static readonly NDR: number;
-    static readonly IDR: number;
-    static readonly SEI: number;
-    static readonly SPS: number;
-    static readonly PPS: number;
-    static readonly TYPES: {
-        [x: number]: string;
-    };
-    static type(nalu: NALU): string;
-    constructor(data: Uint8Array);
-    type(): number;
-    isKeyframe(): boolean;
-    getSize(): number;
-    getData(): Uint8Array;
-}
diff --git a/web-service/public/js/lib/dist/util/NALU.js b/web-service/public/js/lib/dist/util/NALU.js
deleted file mode 100644
index f9c66be6a79aa16971997f6066bedecf585937c4..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/NALU.js
+++ /dev/null
@@ -1,74 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var NALU = (function () {
-    function NALU(data) {
-        this.data = data;
-        this.nri = (data[0] & 0x60) >> 5;
-        this.ntype = data[0] & 0x1f;
-    }
-    Object.defineProperty(NALU, "NDR", {
-        get: function () { return 1; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "IDR", {
-        get: function () { return 5; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "SEI", {
-        get: function () { return 6; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "SPS", {
-        get: function () { return 7; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "PPS", {
-        get: function () { return 8; },
-        enumerable: true,
-        configurable: true
-    });
-    Object.defineProperty(NALU, "TYPES", {
-        get: function () {
-            return _a = {},
-                _a[NALU.IDR] = 'IDR',
-                _a[NALU.SEI] = 'SEI',
-                _a[NALU.SPS] = 'SPS',
-                _a[NALU.PPS] = 'PPS',
-                _a[NALU.NDR] = 'NDR',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    NALU.type = function (nalu) {
-        if (nalu.ntype in NALU.TYPES) {
-            return NALU.TYPES[nalu.ntype];
-        }
-        else {
-            return 'UNKNOWN';
-        }
-    };
-    NALU.prototype.type = function () {
-        return this.ntype;
-    };
-    NALU.prototype.isKeyframe = function () {
-        return this.ntype === NALU.IDR;
-    };
-    NALU.prototype.getSize = function () {
-        return 4 + this.data.byteLength;
-    };
-    NALU.prototype.getData = function () {
-        var result = new Uint8Array(this.getSize());
-        var view = new DataView(result.buffer);
-        view.setUint32(0, this.getSize() - 4);
-        result.set(this.data, 4);
-        return result;
-    };
-    return NALU;
-}());
-exports.default = NALU;
diff --git a/web-service/public/js/lib/dist/util/bit-stream.d.ts b/web-service/public/js/lib/dist/util/bit-stream.d.ts
deleted file mode 100644
index d2a1b9c1013ebf799f2bce6fa28cdac8830eb4d2..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/bit-stream.d.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export default class BitStream {
-    private data;
-    private index;
-    private bitLength;
-    constructor(data: Uint8Array);
-    readonly bitsAvailable: number;
-    skipBits(size: number): void;
-    readBits(size: number): number;
-    private getBits(size, offsetBits, moveIndex?);
-    skipLZ(): number;
-    skipUEG(): void;
-    skipEG(): void;
-    readUEG(): number;
-    readEG(): number;
-    readBoolean(): boolean;
-    readUByte(): number;
-    readUShort(): number;
-    readUInt(): number;
-}
diff --git a/web-service/public/js/lib/dist/util/bit-stream.js b/web-service/public/js/lib/dist/util/bit-stream.js
deleted file mode 100644
index 6983ed17d9192d6cabfdc9766ce9bc37b4a660af..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/bit-stream.js
+++ /dev/null
@@ -1,91 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var BitStream = (function () {
-    function BitStream(data) {
-        this.data = data;
-        this.index = 0;
-        this.bitLength = data.byteLength * 8;
-    }
-    Object.defineProperty(BitStream.prototype, "bitsAvailable", {
-        get: function () {
-            return this.bitLength - this.index;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    BitStream.prototype.skipBits = function (size) {
-        if (this.bitsAvailable < size) {
-            throw new Error('no bytes available');
-        }
-        this.index += size;
-    };
-    BitStream.prototype.readBits = function (size) {
-        var result = this.getBits(size, this.index);
-        return result;
-    };
-    BitStream.prototype.getBits = function (size, offsetBits, moveIndex) {
-        if (moveIndex === void 0) { moveIndex = true; }
-        if (this.bitsAvailable < size) {
-            throw new Error('no bytes available');
-        }
-        var offset = offsetBits % 8;
-        var byte = this.data[(offsetBits / 8) | 0] & (0xff >>> offset);
-        var bits = 8 - offset;
-        if (bits >= size) {
-            if (moveIndex) {
-                this.index += size;
-            }
-            return byte >> (bits - size);
-        }
-        else {
-            if (moveIndex) {
-                this.index += bits;
-            }
-            var nextSize = size - bits;
-            return (byte << nextSize) | this.getBits(nextSize, offsetBits + bits, moveIndex);
-        }
-    };
-    BitStream.prototype.skipLZ = function () {
-        var leadingZeroCount;
-        for (leadingZeroCount = 0; leadingZeroCount < this.bitLength - this.index; ++leadingZeroCount) {
-            if (0 !== this.getBits(1, this.index + leadingZeroCount, false)) {
-                this.index += leadingZeroCount;
-                return leadingZeroCount;
-            }
-        }
-        return leadingZeroCount;
-    };
-    BitStream.prototype.skipUEG = function () {
-        this.skipBits(1 + this.skipLZ());
-    };
-    BitStream.prototype.skipEG = function () {
-        this.skipBits(1 + this.skipLZ());
-    };
-    BitStream.prototype.readUEG = function () {
-        var prefix = this.skipLZ();
-        return this.readBits(prefix + 1) - 1;
-    };
-    BitStream.prototype.readEG = function () {
-        var value = this.readUEG();
-        if (0x01 & value) {
-            return (1 + value) >>> 1;
-        }
-        else {
-            return -1 * (value >>> 1);
-        }
-    };
-    BitStream.prototype.readBoolean = function () {
-        return 1 === this.readBits(1);
-    };
-    BitStream.prototype.readUByte = function () {
-        return this.readBits(8);
-    };
-    BitStream.prototype.readUShort = function () {
-        return this.readBits(16);
-    };
-    BitStream.prototype.readUInt = function () {
-        return this.readBits(32);
-    };
-    return BitStream;
-}());
-exports.default = BitStream;
diff --git a/web-service/public/js/lib/dist/util/debug.d.ts b/web-service/public/js/lib/dist/util/debug.d.ts
deleted file mode 100644
index c7801f1030732d051a163064646352216c66502d..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/debug.d.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export declare type Logger = (message?: any, ...optionalParams: any[]) => void;
-export declare function setLogger(log: Logger, error?: Logger): void;
-export declare function isEnable(): boolean;
-export declare function log(message?: any, ...optionalParams: any[]): void;
-export declare function error(message?: any, ...optionalParams: any[]): void;
diff --git a/web-service/public/js/lib/dist/util/debug.js b/web-service/public/js/lib/dist/util/debug.js
deleted file mode 100644
index 6e4354cf09a7414c655324df9843453f1e707de5..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/debug.js
+++ /dev/null
@@ -1,33 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var logger;
-var errorLogger;
-function setLogger(log, error) {
-    logger = log;
-    errorLogger = error != null ? error : log;
-}
-exports.setLogger = setLogger;
-function isEnable() {
-    return logger != null;
-}
-exports.isEnable = isEnable;
-function log(message) {
-    var optionalParams = [];
-    for (var _i = 1; _i < arguments.length; _i++) {
-        optionalParams[_i - 1] = arguments[_i];
-    }
-    if (logger) {
-        logger.apply(void 0, [message].concat(optionalParams));
-    }
-}
-exports.log = log;
-function error(message) {
-    var optionalParams = [];
-    for (var _i = 1; _i < arguments.length; _i++) {
-        optionalParams[_i - 1] = arguments[_i];
-    }
-    if (errorLogger) {
-        errorLogger.apply(void 0, [message].concat(optionalParams));
-    }
-}
-exports.error = error;
diff --git a/web-service/public/js/lib/dist/util/nalu-stream-buffer.d.ts b/web-service/public/js/lib/dist/util/nalu-stream-buffer.d.ts
deleted file mode 100644
index 591ab4aae4c4e07fd15e59a915a9166b8d478b2e..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/nalu-stream-buffer.d.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import NALU from './NALU';
-export default class VideoStreamBuffer {
-    private buffer;
-    clear(): void;
-    append(value: ArrayLike<number>): NALU[];
-    private mergeBuffer(value);
-}
diff --git a/web-service/public/js/lib/dist/util/nalu-stream-buffer.js b/web-service/public/js/lib/dist/util/nalu-stream-buffer.js
deleted file mode 100644
index 9d76aa0f657a9b0a77ab1e6b69c746cfcb2d3c72..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/util/nalu-stream-buffer.js
+++ /dev/null
@@ -1,66 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var NALU_1 = require("./NALU");
-var VideoStreamBuffer = (function () {
-    function VideoStreamBuffer() {
-    }
-    VideoStreamBuffer.prototype.clear = function () {
-        this.buffer = undefined;
-    };
-    VideoStreamBuffer.prototype.append = function (value) {
-        var nextNalHeader = function (b) {
-            var i = 3;
-            return function () {
-                var count = 0;
-                for (; i < b.length; i++) {
-                    switch (b[i]) {
-                        case 0:
-                            count++;
-                            break;
-                        case 1:
-                            if (count === 3) {
-                                return i - 3;
-                            }
-                        default:
-                            count = 0;
-                    }
-                }
-                return;
-            };
-        };
-        var result = [];
-        var buffer;
-        if (this.buffer) {
-            if (value[3] === 1 && value[2] === 0 && value[1] === 0 && value[0] === 0) {
-                result.push(new NALU_1.default(this.buffer.subarray(4)));
-                buffer = Uint8Array.from(value);
-            }
-        }
-        if (buffer == null) {
-            buffer = this.mergeBuffer(value);
-        }
-        var lastIndex = 0;
-        var f = nextNalHeader(buffer);
-        for (var index = f(); index != null; index = f()) {
-            result.push(new NALU_1.default(buffer.subarray(lastIndex + 4, index)));
-            lastIndex = index;
-        }
-        this.buffer = buffer.subarray(lastIndex);
-        return result;
-    };
-    VideoStreamBuffer.prototype.mergeBuffer = function (value) {
-        if (this.buffer == null) {
-            return Uint8Array.from(value);
-        }
-        else {
-            var newBuffer = new Uint8Array(this.buffer.byteLength + value.length);
-            if (this.buffer.byteLength > 0) {
-                newBuffer.set(this.buffer, 0);
-            }
-            newBuffer.set(value, this.buffer.byteLength);
-            return newBuffer;
-        }
-    };
-    return VideoStreamBuffer;
-}());
-exports.default = VideoStreamBuffer;
diff --git a/web-service/public/js/lib/dist/video-converter.d.ts b/web-service/public/js/lib/dist/video-converter.d.ts
deleted file mode 100644
index aa02344c1c60c8fda36923659b5a35631453169a..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/video-converter.d.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-export declare const mimeType = "video/mp4; codecs=\"avc1.42E01E\"";
-export default class VideoConverter {
-    private element;
-    private fps;
-    private fpf;
-    private mediaSource;
-    private sourceBuffer;
-    private receiveBuffer;
-    private remuxer;
-    private mediaReady;
-    private mediaReadyPromise;
-    private queue;
-    private isFirstFrame;
-    static readonly errorNotes: {
-        [x: number]: string;
-    };
-    constructor(element: HTMLVideoElement, fps?: number, fpf?: number);
-    private setup();
-    play(): void;
-    pause(): void;
-    reset(): void;
-    appendRawData(data: ArrayLike<number>): void;
-    private writeFragment(dts, pay);
-    private writeBuffer(data);
-    private doAppend(data);
-}
diff --git a/web-service/public/js/lib/dist/video-converter.js b/web-service/public/js/lib/dist/video-converter.js
deleted file mode 100644
index 2e91f4b25ee260c83bc7482fd3d5091970326724..0000000000000000000000000000000000000000
--- a/web-service/public/js/lib/dist/video-converter.js
+++ /dev/null
@@ -1,186 +0,0 @@
-"use strict";
-Object.defineProperty(exports, "__esModule", { value: true });
-var h264_remuxer_1 = require("./h264-remuxer");
-var mp4_generator_1 = require("./mp4-generator");
-var debug = require("./util/debug");
-var nalu_stream_buffer_1 = require("./util/nalu-stream-buffer");
-exports.mimeType = 'video/mp4; codecs="avc1.42E01E"';
-var VideoConverter = (function () {
-    function VideoConverter(element, fps, fpf) {
-        if (fps === void 0) { fps = 60; }
-        if (fpf === void 0) { fpf = fps; }
-        this.element = element;
-        this.fps = fps;
-        this.fpf = fpf;
-        this.receiveBuffer = new nalu_stream_buffer_1.default();
-        this.queue = [];
-        if (!MediaSource || !MediaSource.isTypeSupported(exports.mimeType)) {
-            throw new Error("Your browser is not supported: " + exports.mimeType);
-        }
-        this.reset();
-    }
-    Object.defineProperty(VideoConverter, "errorNotes", {
-        get: function () {
-            return _a = {},
-                _a[MediaError.MEDIA_ERR_ABORTED] = 'fetching process aborted by user',
-                _a[MediaError.MEDIA_ERR_NETWORK] = 'error occurred when downloading',
-                _a[MediaError.MEDIA_ERR_DECODE] = 'error occurred when decoding',
-                _a[MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED] = 'audio/video not supported',
-                _a;
-            var _a;
-        },
-        enumerable: true,
-        configurable: true
-    });
-    VideoConverter.prototype.setup = function () {
-        var _this = this;
-        this.mediaReadyPromise = new Promise(function (resolve, _reject) {
-            _this.mediaSource.addEventListener('sourceopen', function () {
-                debug.log("Media Source opened.");
-                _this.sourceBuffer = _this.mediaSource.addSourceBuffer(exports.mimeType);
-                _this.sourceBuffer.addEventListener('updateend', function () {
-                    debug.log("  SourceBuffer updateend");
-                    debug.log("    sourceBuffer.buffered.length=" + _this.sourceBuffer.buffered.length);
-                    for (var i = 0, len = _this.sourceBuffer.buffered.length; i < len; i++) {
-                        debug.log("    sourceBuffer.buffered [" + i + "]: " +
-                            (_this.sourceBuffer.buffered.start(i) + ", " + _this.sourceBuffer.buffered.end(i)));
-                    }
-                    debug.log("  mediasource.duration=" + _this.mediaSource.duration);
-                    debug.log("  mediasource.readyState=" + _this.mediaSource.readyState);
-                    debug.log("  video.duration=" + _this.element.duration);
-                    debug.log("    video.buffered.length=" + _this.element.buffered.length);
-                    if (debug.isEnable()) {
-                        for (var i = 0, len = _this.element.buffered.length; i < len; i++) {
-                            debug.log("    video.buffered [" + i + "]: " + _this.element.buffered.start(i) + ", " + _this.element.buffered.end(i));
-                        }
-                    }
-                    debug.log("  video.currentTime=" + _this.element.currentTime);
-                    debug.log("  video.readyState=" + _this.element.readyState);
-                    var data = _this.queue.shift();
-                    if (data) {
-                        _this.writeBuffer(data);
-                    }
-                });
-                _this.sourceBuffer.addEventListener('error', function () {
-                    debug.error('  SourceBuffer errored!');
-                });
-                _this.mediaReady = true;
-                resolve();
-            }, false);
-            _this.mediaSource.addEventListener('sourceclose', function () {
-                debug.log("Media Source closed.");
-                _this.mediaReady = false;
-            }, false);
-            _this.element.src = URL.createObjectURL(_this.mediaSource);
-        });
-        return this.mediaReadyPromise;
-    };
-    VideoConverter.prototype.play = function () {
-        var _this = this;
-        if (!this.element.paused) {
-            return;
-        }
-        if (this.mediaReady && this.element.readyState >= 2) {
-            this.element.play();
-        }
-        else {
-            var handler_1 = function () {
-                _this.play();
-                _this.element.removeEventListener('canplaythrough', handler_1);
-            };
-            this.element.addEventListener('canplaythrough', handler_1);
-        }
-    };
-    VideoConverter.prototype.pause = function () {
-        if (this.element.paused) {
-            return;
-        }
-        this.element.pause();
-    };
-    VideoConverter.prototype.reset = function () {
-        this.receiveBuffer.clear();
-        if (this.mediaSource && this.mediaSource.readyState === 'open') {
-            this.mediaSource.duration = 0;
-            this.mediaSource.endOfStream();
-        }
-        this.mediaSource = new MediaSource();
-        this.remuxer = new h264_remuxer_1.default(this.fps, this.fpf, this.fps * 60);
-        this.mediaReady = false;
-        this.mediaReadyPromise = undefined;
-        this.queue = [];
-        this.isFirstFrame = true;
-        this.setup();
-    };
-    VideoConverter.prototype.appendRawData = function (data, dts) {
-        var nalus = this.receiveBuffer.append(data);
-        for (var _i = 0, nalus_1 = nalus; _i < nalus_1.length; _i++) {
-            var nalu = nalus_1[_i];
-            var ret = this.remuxer.remux(nalu);
-            if (ret) {
-				this.writeFragment(ret[0], ret[1]);  // ret[0]
-            }
-        }
-    };
-    VideoConverter.prototype.writeFragment = function (dts, pay) {
-        var remuxer = this.remuxer;
-        if (remuxer.mp4track.isKeyFrame) {
-            this.writeBuffer(mp4_generator_1.default.initSegment([remuxer.mp4track], Infinity, remuxer.timescale));
-        }
-        if (pay && pay.byteLength) {
-            debug.log(" Put fragment: " + remuxer.seqNum + ", frames=" + remuxer.mp4track.samples.length + ", size=" + pay.byteLength);
-            var fragment = mp4_generator_1.default.fragmentSegment(remuxer.seqNum, dts, remuxer.mp4track, pay);
-            this.writeBuffer(fragment);
-            remuxer.flush();
-        }
-        else {
-            debug.error("Nothing payload!");
-        }
-    };
-    VideoConverter.prototype.writeBuffer = function (data) {
-        var _this = this;
-        if (this.mediaReady) {
-            if (this.sourceBuffer.updating) {
-                this.queue.push(data);
-            }
-            else {
-				if (this.queue.length > 0) console.error("DATA IN QUEUE");
-                this.doAppend(data);
-            }
-        }
-        else {
-            this.queue.push(data);
-            if (this.mediaReadyPromise) {
-                this.mediaReadyPromise.then(function () {
-                    if (!_this.sourceBuffer.updating) {
-                        var d = _this.queue.shift();
-                        if (d) {
-                            _this.writeBuffer(d);
-                        }
-                    }
-                });
-                this.mediaReadyPromise = undefined;
-            }
-        }
-    };
-    VideoConverter.prototype.doAppend = function (data) {
-        var error = this.element.error;
-        if (error) {
-            debug.error("MSE Error Occured: " + VideoConverter.errorNotes[error.code]);
-            this.element.pause();
-            if (this.mediaSource.readyState === 'open') {
-                this.mediaSource.endOfStream();
-            }
-        }
-        else {
-            try {
-                this.sourceBuffer.appendBuffer(data);
-                debug.log("  appended buffer: size=" + data.byteLength);
-            }
-            catch (err) {
-                debug.error("MSE Error occured while appending buffer. " + err.name + ": " + err.message);
-            }
-        }
-    };
-    return VideoConverter;
-}());
-exports.default = VideoConverter;